blob: 65cc9dfefbb451245c220539fddd68222e170be6 [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.
59 "CC=" + rebase_path("//prebuilts/linux-x64/clang/bin/clang"),
Andrew Walbran45ac1d22019-10-21 16:54:24 +010060 "LD=" +
61 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ld"),
62 "AR=" +
63 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ar"),
64 "NM=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-nm"),
65 "OBJCOPY=" + rebase_path(
66 "//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-objcopy"),
67 "OBJDUMP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-objdump"),
68 "STRIP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-strip"),
Andrew Walbranba7f1712020-01-13 16:23:00 +000069 "GCC_TOOLCHAIN_DIR=" + rebase_path("//prebuilts/linux-x64/gcc/bin"),
David Brazdil3f509e02019-07-01 12:42:25 +010070 "ARCH=arm64",
71 "CROSS_COMPILE=aarch64-linux-gnu-",
72
73 # Build out-of-tree in `target_out_dir`.
74 "O=" + rebase_path(target_out_dir),
75
76 # TODO: Remove/replace.
77 "-j24",
78 ]
79
David Brazdilb4802bc2019-07-30 12:39:41 +010080 # Subtarget which generates a depfile with all files in the Linux tree
81 # and gets invalidated if any of them change.
82 source_dir(source_target) {
David Brazdil33cc5c02019-12-10 10:44:14 +000083 path = kernel_dir
David Brazdilb4802bc2019-07-30 12:39:41 +010084 }
85
David Brazdil3f509e02019-07-01 12:42:25 +010086 # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
87 # which do not require the whole kernel to have been built.
David Brazdilb4802bc2019-07-30 12:39:41 +010088 action(defconfig_target) {
David Brazdil3f509e02019-07-01 12:42:25 +010089 script = "//build/make.py"
90 args = shared_args + [
91 "defconfig",
92 "modules_prepare",
93 ]
94
95 # We never use the output but GN requires each target to have one, and for
96 # its timestamp to change after a recompile. Use the .config file.
97 outputs = [
98 "${target_out_dir}/.config",
99 ]
David Brazdilb4802bc2019-07-30 12:39:41 +0100100 deps = [
101 ":${source_target}",
102 ]
David Brazdil3f509e02019-07-01 12:42:25 +0100103 }
104
105 action(target_name) {
106 script = "//build/make.py"
107 output_file = "${target_out_dir}/${target_name}.bin"
108 args = shared_args + [
109 "--copy_out_file",
110 rebase_path("${target_out_dir}/arch/arm64/boot/Image"),
111 rebase_path(output_file),
112 ]
113 outputs = [
114 output_file,
115 ]
116 deps = [
David Brazdilb4802bc2019-07-30 12:39:41 +0100117 ":${defconfig_target}",
118 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100119 ]
120 }
David Brazdil4b0e1112019-07-30 18:28:01 +0100121
122 # Subtarget for a prebuilt image, if defined.
123 if (defined(invoker.prebuilt)) {
124 copy(prebuilt_target) {
125 sources = [
126 invoker.prebuilt,
127 ]
128 outputs = [
129 "${target_out_dir}/${prebuilt_target}.bin",
130 ]
131 }
132 }
David Brazdil3f509e02019-07-01 12:42:25 +0100133}
134
135template("linux_kernel_module") {
136 # Out-of-tree modules cannot be built outside of their directory.
137 # So as to avoid parallel builds under different toolchains clashing,
138 # work around by copying source files to `target_out_dir`.
David Brazdile7a5a1f2019-08-09 18:48:38 +0100139
140 source_target = "${target_name}__source"
141
David Brazdilcb92ba12019-12-23 14:15:08 +0000142 module_dir = "./"
143
David Brazdile7a5a1f2019-08-09 18:48:38 +0100144 source_dir_copy(source_target) {
David Brazdilcb92ba12019-12-23 14:15:08 +0000145 path = module_dir
David Brazdil3f509e02019-07-01 12:42:25 +0100146 }
147
148 action(target_name) {
149 forward_variables_from(invoker, [ "testonly" ])
150 script = "//build/make.py"
151 args = [
152 "--directory",
153 rebase_path(target_out_dir),
154 "HAFNIUM_PATH=" + rebase_path("//"),
David Brazdile7a5a1f2019-08-09 18:48:38 +0100155 "KERNEL_PATH=" + rebase_path(invoker.kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +0100156 "O=" +
157 rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")),
158 "CC=" + rebase_path("//prebuilts/linux-x64/clang/bin/clang"),
Andrew Walbran45ac1d22019-10-21 16:54:24 +0100159 "LD=" +
160 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ld"),
161 "AR=" +
162 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ar"),
163 "NM=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-nm"),
164 "OBJCOPY=" + rebase_path(
165 "//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-objcopy"),
166 "OBJDUMP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-objdump"),
167 "STRIP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-strip"),
Andrew Walbranba7f1712020-01-13 16:23:00 +0000168 "GCC_TOOLCHAIN_DIR=" + rebase_path("//prebuilts/linux-x64/gcc/bin"),
David Brazdil3f509e02019-07-01 12:42:25 +0100169 "ARCH=arm64",
170 "CROSS_COMPILE=aarch64-linux-gnu-",
171 ]
172 outputs = [
173 "${target_out_dir}/${invoker.module_name}.ko",
174 ]
175 deps = [
David Brazdile7a5a1f2019-08-09 18:48:38 +0100176 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100177 "${invoker.kernel_target}__defconfig",
178 ]
179 }
180}