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