blob: 952bedc60daaf87dd3d3be49c2efa78e7edca437 [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
David Brazdil33cc5c02019-12-10 10:44:14 +000059 kernel_dir = "./"
60
David Brazdil3f509e02019-07-01 12:42:25 +010061 # Args to build/make.py to start the Linux build.
62 shared_args = [
63 "--directory",
David Brazdil33cc5c02019-12-10 10:44:14 +000064 rebase_path(kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +010065
66 # TODO: Build with toolchain cc instead of a hardcoded one.
67 "CC=" + rebase_path("//prebuilts/linux-x64/clang/bin/clang"),
Andrew Walbran45ac1d22019-10-21 16:54:24 +010068 "LD=" +
69 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ld"),
70 "AR=" +
71 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ar"),
72 "NM=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-nm"),
73 "OBJCOPY=" + rebase_path(
74 "//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-objcopy"),
75 "OBJDUMP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-objdump"),
76 "STRIP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-strip"),
Andrew Walbranba7f1712020-01-13 16:23:00 +000077 "GCC_TOOLCHAIN_DIR=" + rebase_path("//prebuilts/linux-x64/gcc/bin"),
David Brazdil3f509e02019-07-01 12:42:25 +010078 "ARCH=arm64",
79 "CROSS_COMPILE=aarch64-linux-gnu-",
80
81 # Build out-of-tree in `target_out_dir`.
82 "O=" + rebase_path(target_out_dir),
83
84 # TODO: Remove/replace.
85 "-j24",
86 ]
87
David Brazdilb4802bc2019-07-30 12:39:41 +010088 # Subtarget which generates a depfile with all files in the Linux tree
89 # and gets invalidated if any of them change.
90 source_dir(source_target) {
David Brazdil33cc5c02019-12-10 10:44:14 +000091 path = kernel_dir
David Brazdilb4802bc2019-07-30 12:39:41 +010092 }
93
David Brazdil3f509e02019-07-01 12:42:25 +010094 # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
95 # which do not require the whole kernel to have been built.
David Brazdilb4802bc2019-07-30 12:39:41 +010096 action(defconfig_target) {
David Brazdil3f509e02019-07-01 12:42:25 +010097 script = "//build/make.py"
98 args = shared_args + [
99 "defconfig",
100 "modules_prepare",
101 ]
102
103 # We never use the output but GN requires each target to have one, and for
104 # its timestamp to change after a recompile. Use the .config file.
105 outputs = [
106 "${target_out_dir}/.config",
107 ]
David Brazdilb4802bc2019-07-30 12:39:41 +0100108 deps = [
109 ":${source_target}",
110 ]
David Brazdil3f509e02019-07-01 12:42:25 +0100111 }
112
113 action(target_name) {
114 script = "//build/make.py"
115 output_file = "${target_out_dir}/${target_name}.bin"
116 args = shared_args + [
117 "--copy_out_file",
118 rebase_path("${target_out_dir}/arch/arm64/boot/Image"),
119 rebase_path(output_file),
120 ]
121 outputs = [
122 output_file,
123 ]
124 deps = [
David Brazdilb4802bc2019-07-30 12:39:41 +0100125 ":${defconfig_target}",
126 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100127 ]
128 }
David Brazdil4b0e1112019-07-30 18:28:01 +0100129
130 # Subtarget for a prebuilt image, if defined.
131 if (defined(invoker.prebuilt)) {
132 copy(prebuilt_target) {
133 sources = [
134 invoker.prebuilt,
135 ]
136 outputs = [
137 "${target_out_dir}/${prebuilt_target}.bin",
138 ]
139 }
140 }
David Brazdil3f509e02019-07-01 12:42:25 +0100141}
142
143template("linux_kernel_module") {
144 # Out-of-tree modules cannot be built outside of their directory.
145 # So as to avoid parallel builds under different toolchains clashing,
146 # work around by copying source files to `target_out_dir`.
David Brazdile7a5a1f2019-08-09 18:48:38 +0100147
148 source_target = "${target_name}__source"
149
David Brazdilcb92ba12019-12-23 14:15:08 +0000150 module_dir = "./"
151
David Brazdile7a5a1f2019-08-09 18:48:38 +0100152 source_dir_copy(source_target) {
David Brazdilcb92ba12019-12-23 14:15:08 +0000153 path = module_dir
David Brazdil3f509e02019-07-01 12:42:25 +0100154 }
155
156 action(target_name) {
157 forward_variables_from(invoker, [ "testonly" ])
158 script = "//build/make.py"
159 args = [
160 "--directory",
161 rebase_path(target_out_dir),
162 "HAFNIUM_PATH=" + rebase_path("//"),
David Brazdile7a5a1f2019-08-09 18:48:38 +0100163 "KERNEL_PATH=" + rebase_path(invoker.kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +0100164 "O=" +
165 rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")),
166 "CC=" + rebase_path("//prebuilts/linux-x64/clang/bin/clang"),
Andrew Walbran45ac1d22019-10-21 16:54:24 +0100167 "LD=" +
168 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ld"),
169 "AR=" +
170 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ar"),
171 "NM=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-nm"),
172 "OBJCOPY=" + rebase_path(
173 "//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-objcopy"),
174 "OBJDUMP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-objdump"),
175 "STRIP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-strip"),
Andrew Walbranba7f1712020-01-13 16:23:00 +0000176 "GCC_TOOLCHAIN_DIR=" + rebase_path("//prebuilts/linux-x64/gcc/bin"),
David Brazdil3f509e02019-07-01 12:42:25 +0100177 "ARCH=arm64",
178 "CROSS_COMPILE=aarch64-linux-gnu-",
179 ]
180 outputs = [
181 "${target_out_dir}/${invoker.module_name}.ko",
182 ]
183 deps = [
David Brazdile7a5a1f2019-08-09 18:48:38 +0100184 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100185 "${invoker.kernel_target}__defconfig",
186 ]
187 }
188}