blob: 5712932214cce369358fcdf9f9494ef66a3016e7 [file] [log] [blame]
David Brazdil3f509e02019-07-01 12:42:25 +01001# Copyright 2019 The Hafnium Authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
David Brazdilb4802bc2019-07-30 12:39:41 +010015template("source_dir") {
16 action("${target_name}") {
17 depfile = "${target_out_dir}/${target_name}.d"
18 outputs = [
19 "$target_out_dir/${target_name}.script.stamp",
20 ]
21
22 script = "//build/linux/gen_depfile.py"
23 args = [
24 rebase_path(invoker.path, root_build_dir),
25 rebase_path(outputs[0], root_build_dir),
26 rebase_path(depfile, root_build_dir),
27 ]
28 }
29}
30
David Brazdile7a5a1f2019-08-09 18:48:38 +010031template("source_dir_copy") {
32 source_dir_target = "${target_name}__source_dir"
33
34 source_dir(source_dir_target) {
35 path = invoker.path
36 }
37
38 action("${target_name}") {
39 script = "//build/linux/copy_dirs.py"
40 outputs = [
41 "$target_out_dir/${target_name}.script.stamp",
42 ]
43 args = [
44 rebase_path(invoker.path),
45 rebase_path(target_out_dir),
46 rebase_path(outputs[0]),
47 ]
48 deps = [
49 ":${source_dir_target}",
50 ]
51 }
52}
53
David Brazdil3f509e02019-07-01 12:42:25 +010054template("linux_kernel") {
David Brazdilb4802bc2019-07-30 12:39:41 +010055 source_target = "${target_name}__source"
56 defconfig_target = "${target_name}__defconfig"
David Brazdil4b0e1112019-07-30 18:28:01 +010057 prebuilt_target = "${target_name}__prebuilt"
David Brazdil3f509e02019-07-01 12:42:25 +010058
59 # Args to build/make.py to start the Linux build.
60 shared_args = [
61 "--directory",
62 rebase_path(invoker.kernel_dir),
63
64 # TODO: Build with toolchain cc instead of a hardcoded one.
65 "CC=" + rebase_path("//prebuilts/linux-x64/clang/bin/clang"),
Andrew Walbran45ac1d22019-10-21 16:54:24 +010066 "LD=" +
67 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ld"),
68 "AR=" +
69 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ar"),
70 "NM=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-nm"),
71 "OBJCOPY=" + rebase_path(
72 "//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-objcopy"),
73 "OBJDUMP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-objdump"),
74 "STRIP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-strip"),
David Brazdil3f509e02019-07-01 12:42:25 +010075 "ARCH=arm64",
76 "CROSS_COMPILE=aarch64-linux-gnu-",
77
78 # Build out-of-tree in `target_out_dir`.
79 "O=" + rebase_path(target_out_dir),
80
81 # TODO: Remove/replace.
82 "-j24",
83 ]
84
David Brazdilb4802bc2019-07-30 12:39:41 +010085 # Subtarget which generates a depfile with all files in the Linux tree
86 # and gets invalidated if any of them change.
87 source_dir(source_target) {
88 path = invoker.kernel_dir
89 }
90
David Brazdil3f509e02019-07-01 12:42:25 +010091 # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
92 # which do not require the whole kernel to have been built.
David Brazdilb4802bc2019-07-30 12:39:41 +010093 action(defconfig_target) {
David Brazdil3f509e02019-07-01 12:42:25 +010094 script = "//build/make.py"
95 args = shared_args + [
96 "defconfig",
97 "modules_prepare",
98 ]
99
100 # We never use the output but GN requires each target to have one, and for
101 # its timestamp to change after a recompile. Use the .config file.
102 outputs = [
103 "${target_out_dir}/.config",
104 ]
David Brazdilb4802bc2019-07-30 12:39:41 +0100105 deps = [
106 ":${source_target}",
107 ]
David Brazdil3f509e02019-07-01 12:42:25 +0100108 }
109
110 action(target_name) {
111 script = "//build/make.py"
112 output_file = "${target_out_dir}/${target_name}.bin"
113 args = shared_args + [
114 "--copy_out_file",
115 rebase_path("${target_out_dir}/arch/arm64/boot/Image"),
116 rebase_path(output_file),
117 ]
118 outputs = [
119 output_file,
120 ]
121 deps = [
David Brazdilb4802bc2019-07-30 12:39:41 +0100122 ":${defconfig_target}",
123 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100124 ]
125 }
David Brazdil4b0e1112019-07-30 18:28:01 +0100126
127 # Subtarget for a prebuilt image, if defined.
128 if (defined(invoker.prebuilt)) {
129 copy(prebuilt_target) {
130 sources = [
131 invoker.prebuilt,
132 ]
133 outputs = [
134 "${target_out_dir}/${prebuilt_target}.bin",
135 ]
136 }
137 }
David Brazdil3f509e02019-07-01 12:42:25 +0100138}
139
140template("linux_kernel_module") {
141 # Out-of-tree modules cannot be built outside of their directory.
142 # So as to avoid parallel builds under different toolchains clashing,
143 # work around by copying source files to `target_out_dir`.
David Brazdile7a5a1f2019-08-09 18:48:38 +0100144
145 source_target = "${target_name}__source"
146
147 source_dir_copy(source_target) {
148 path = invoker.module_dir
David Brazdil3f509e02019-07-01 12:42:25 +0100149 }
150
151 action(target_name) {
152 forward_variables_from(invoker, [ "testonly" ])
153 script = "//build/make.py"
154 args = [
155 "--directory",
156 rebase_path(target_out_dir),
157 "HAFNIUM_PATH=" + rebase_path("//"),
David Brazdile7a5a1f2019-08-09 18:48:38 +0100158 "KERNEL_PATH=" + rebase_path(invoker.kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +0100159 "O=" +
160 rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")),
161 "CC=" + rebase_path("//prebuilts/linux-x64/clang/bin/clang"),
Andrew Walbran45ac1d22019-10-21 16:54:24 +0100162 "LD=" +
163 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ld"),
164 "AR=" +
165 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ar"),
166 "NM=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-nm"),
167 "OBJCOPY=" + rebase_path(
168 "//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-objcopy"),
169 "OBJDUMP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-objdump"),
170 "STRIP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-strip"),
David Brazdil3f509e02019-07-01 12:42:25 +0100171 "ARCH=arm64",
172 "CROSS_COMPILE=aarch64-linux-gnu-",
173 ]
174 outputs = [
175 "${target_out_dir}/${invoker.module_name}.ko",
176 ]
177 deps = [
David Brazdile7a5a1f2019-08-09 18:48:38 +0100178 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100179 "${invoker.kernel_target}__defconfig",
180 ]
181 }
182}