1# Copyright 2019 The Hafnium Authors.
2#
3# Use of this source code is governed by a BSD-style
4# license that can be found in the LICENSE file or at
5# https://opensource.org/licenses/BSD-3-Clause.
6
7template("source_dir") {
8  action("${target_name}") {
9    depfile = "${target_out_dir}/${target_name}.d"
10    outputs = [ "$target_out_dir/${target_name}.script.stamp" ]
11
12    script = "//build/linux/gen_depfile.py"
13    args = [
14      rebase_path(invoker.path, root_build_dir),
15      rebase_path(outputs[0], root_build_dir),
16      rebase_path(depfile, root_build_dir),
17    ]
18  }
19}
20
21template("source_dir_copy") {
22  source_dir_target = "${target_name}__source_dir"
23
24  source_dir(source_dir_target) {
25    path = invoker.path
26  }
27
28  action("${target_name}") {
29    script = "//build/linux/copy_dirs.py"
30    outputs = [ "$target_out_dir/${target_name}.script.stamp" ]
31    args = [
32      rebase_path(invoker.path),
33      rebase_path(target_out_dir),
34      rebase_path(outputs[0]),
35    ]
36    deps = [ ":${source_dir_target}" ]
37  }
38}
39
40template("linux_kernel") {
41  source_target = "${target_name}__source"
42  defconfig_target = "${target_name}__defconfig"
43  prebuilt_target = "${target_name}__prebuilt"
44
45  kernel_dir = "./"
46
47  # Args to build/make.py to start the Linux build.
48  shared_args = [
49    "--directory",
50    rebase_path(kernel_dir),
51
52    # TODO: Build with toolchain cc instead of a hardcoded one.
53    "ARCH=arm64",
54    "LLVM=1",
55    "LLVM_IAS=1",
56    "CROSS_COMPILE=aarch64-linux-gnu-",
57
58    # Build out-of-tree in `target_out_dir`.
59    "O=" + rebase_path(target_out_dir),
60
61    # TODO: assess if this setting is really required because the ninja
62    # top level invocation already cares about parallel build.
63    "-j24",
64  ]
65
66  # Subtarget which generates a depfile with all files in the Linux tree
67  # and gets invalidated if any of them change.
68  source_dir(source_target) {
69    path = kernel_dir
70  }
71
72  # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
73  # which do not require the whole kernel to have been built.
74  action(defconfig_target) {
75    script = "//build/make.py"
76    args = shared_args + [
77             "defconfig",
78             "modules_prepare",
79           ]
80
81    # We never use the output but GN requires each target to have one, and for
82    # its timestamp to change after a recompile. Use the .config file.
83    outputs = [ "${target_out_dir}/.config" ]
84    deps = [ ":${source_target}" ]
85  }
86
87  action(target_name) {
88    script = "//build/make.py"
89    output_file = "${target_out_dir}/${target_name}.bin"
90    args = shared_args + [
91             "--copy_out_file",
92             rebase_path("${target_out_dir}/arch/arm64/boot/Image"),
93             rebase_path(output_file),
94           ]
95    outputs = [ output_file ]
96    deps = [
97      ":${defconfig_target}",
98      ":${source_target}",
99    ]
100  }
101
102  # Subtarget for a prebuilt image, if defined.
103  if (defined(invoker.prebuilt)) {
104    copy(prebuilt_target) {
105      sources = [ invoker.prebuilt ]
106      outputs = [ "${target_out_dir}/${prebuilt_target}.bin" ]
107    }
108  }
109}
110
111template("linux_kernel_module") {
112  # Out-of-tree modules cannot be built outside of their directory.
113  # So as to avoid parallel builds under different toolchains clashing,
114  # work around by copying source files to `target_out_dir`.
115
116  source_target = "${target_name}__source"
117
118  module_dir = "./"
119
120  source_dir_copy(source_target) {
121    path = module_dir
122  }
123
124  action(target_name) {
125    forward_variables_from(invoker, [ "testonly" ])
126    script = "//build/make.py"
127    args = [
128      "--directory",
129      rebase_path(target_out_dir),
130      "HAFNIUM_PATH=" + rebase_path("//"),
131      "KERNEL_PATH=" + rebase_path(invoker.kernel_dir),
132      "O=" +
133          rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")),
134      "ARCH=arm64",
135      "LLVM=1",
136      "LLVM_IAS=1",
137      "CROSS_COMPILE=aarch64-linux-gnu-",
138    ]
139    outputs = [ "${target_out_dir}/${invoker.module_name}.ko" ]
140    deps = [
141      ":${source_target}",
142      "${invoker.kernel_target}__defconfig",
143    ]
144  }
145}
146