blob: 45860fab7716363c38f13ab2cca0b3a236555041 [file] [log] [blame]
David Brazdil3f509e02019-07-01 12:42:25 +01001# Copyright 2019 The Hafnium Authors.
2#
Andrew Walbrane959ec12020-06-17 15:01:09 +01003# 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.
David Brazdil3f509e02019-07-01 12:42:25 +01006
David Brazdilb4802bc2019-07-30 12:39:41 +01007template("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
David Brazdile7a5a1f2019-08-09 18:48:38 +010023template("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
David Brazdil3f509e02019-07-01 12:42:25 +010046template("linux_kernel") {
David Brazdilb4802bc2019-07-30 12:39:41 +010047 source_target = "${target_name}__source"
48 defconfig_target = "${target_name}__defconfig"
David Brazdil4b0e1112019-07-30 18:28:01 +010049 prebuilt_target = "${target_name}__prebuilt"
David Brazdil3f509e02019-07-01 12:42:25 +010050
David Brazdil33cc5c02019-12-10 10:44:14 +000051 kernel_dir = "./"
52
David Brazdil3f509e02019-07-01 12:42:25 +010053 # Args to build/make.py to start the Linux build.
54 shared_args = [
55 "--directory",
David Brazdil33cc5c02019-12-10 10:44:14 +000056 rebase_path(kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +010057
58 # TODO: Build with toolchain cc instead of a hardcoded one.
David Brazdil3f509e02019-07-01 12:42:25 +010059 "ARCH=arm64",
Olivier Deprezc16b0ec2021-09-23 11:39:39 +020060 "LLVM=1",
61 "LLVM_IAS=1",
David Brazdil3f509e02019-07-01 12:42:25 +010062 "CROSS_COMPILE=aarch64-linux-gnu-",
63
64 # Build out-of-tree in `target_out_dir`.
65 "O=" + rebase_path(target_out_dir),
66
Olivier Deprezc16b0ec2021-09-23 11:39:39 +020067 # TODO: assess if this setting is really required because the ninja
68 # top level invocation already cares about parallel build.
David Brazdil3f509e02019-07-01 12:42:25 +010069 "-j24",
70 ]
71
David Brazdilb4802bc2019-07-30 12:39:41 +010072 # 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) {
David Brazdil33cc5c02019-12-10 10:44:14 +000075 path = kernel_dir
David Brazdilb4802bc2019-07-30 12:39:41 +010076 }
77
David Brazdil3f509e02019-07-01 12:42:25 +010078 # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
79 # which do not require the whole kernel to have been built.
David Brazdilb4802bc2019-07-30 12:39:41 +010080 action(defconfig_target) {
David Brazdil3f509e02019-07-01 12:42:25 +010081 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 ]
David Brazdilb4802bc2019-07-30 12:39:41 +010092 deps = [
93 ":${source_target}",
94 ]
David Brazdil3f509e02019-07-01 12:42:25 +010095 }
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 = [
David Brazdilb4802bc2019-07-30 12:39:41 +0100109 ":${defconfig_target}",
110 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100111 ]
112 }
David Brazdil4b0e1112019-07-30 18:28:01 +0100113
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 }
David Brazdil3f509e02019-07-01 12:42:25 +0100125}
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`.
David Brazdile7a5a1f2019-08-09 18:48:38 +0100131
132 source_target = "${target_name}__source"
133
David Brazdilcb92ba12019-12-23 14:15:08 +0000134 module_dir = "./"
135
David Brazdile7a5a1f2019-08-09 18:48:38 +0100136 source_dir_copy(source_target) {
David Brazdilcb92ba12019-12-23 14:15:08 +0000137 path = module_dir
David Brazdil3f509e02019-07-01 12:42:25 +0100138 }
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("//"),
David Brazdile7a5a1f2019-08-09 18:48:38 +0100147 "KERNEL_PATH=" + rebase_path(invoker.kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +0100148 "O=" +
149 rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")),
David Brazdil3f509e02019-07-01 12:42:25 +0100150 "ARCH=arm64",
Olivier Deprezc16b0ec2021-09-23 11:39:39 +0200151 "LLVM=1",
152 "LLVM_IAS=1",
David Brazdil3f509e02019-07-01 12:42:25 +0100153 "CROSS_COMPILE=aarch64-linux-gnu-",
154 ]
155 outputs = [
156 "${target_out_dir}/${invoker.module_name}.ko",
157 ]
158 deps = [
David Brazdile7a5a1f2019-08-09 18:48:38 +0100159 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100160 "${invoker.kernel_target}__defconfig",
161 ]
162 }
163}