blob: bb50aa6dd527dc4f97fc30346292ecd5a1c431ad [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"),
David Brazdil3f509e02019-07-01 12:42:25 +010077 "ARCH=arm64",
78 "CROSS_COMPILE=aarch64-linux-gnu-",
79
80 # Build out-of-tree in `target_out_dir`.
81 "O=" + rebase_path(target_out_dir),
82
83 # TODO: Remove/replace.
84 "-j24",
85 ]
86
David Brazdilb4802bc2019-07-30 12:39:41 +010087 # Subtarget which generates a depfile with all files in the Linux tree
88 # and gets invalidated if any of them change.
89 source_dir(source_target) {
David Brazdil33cc5c02019-12-10 10:44:14 +000090 path = kernel_dir
David Brazdilb4802bc2019-07-30 12:39:41 +010091 }
92
David Brazdil3f509e02019-07-01 12:42:25 +010093 # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
94 # which do not require the whole kernel to have been built.
David Brazdilb4802bc2019-07-30 12:39:41 +010095 action(defconfig_target) {
David Brazdil3f509e02019-07-01 12:42:25 +010096 script = "//build/make.py"
97 args = shared_args + [
98 "defconfig",
99 "modules_prepare",
100 ]
101
102 # We never use the output but GN requires each target to have one, and for
103 # its timestamp to change after a recompile. Use the .config file.
104 outputs = [
105 "${target_out_dir}/.config",
106 ]
David Brazdilb4802bc2019-07-30 12:39:41 +0100107 deps = [
108 ":${source_target}",
109 ]
David Brazdil3f509e02019-07-01 12:42:25 +0100110 }
111
112 action(target_name) {
113 script = "//build/make.py"
114 output_file = "${target_out_dir}/${target_name}.bin"
115 args = shared_args + [
116 "--copy_out_file",
117 rebase_path("${target_out_dir}/arch/arm64/boot/Image"),
118 rebase_path(output_file),
119 ]
120 outputs = [
121 output_file,
122 ]
123 deps = [
David Brazdilb4802bc2019-07-30 12:39:41 +0100124 ":${defconfig_target}",
125 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100126 ]
127 }
David Brazdil4b0e1112019-07-30 18:28:01 +0100128
129 # Subtarget for a prebuilt image, if defined.
130 if (defined(invoker.prebuilt)) {
131 copy(prebuilt_target) {
132 sources = [
133 invoker.prebuilt,
134 ]
135 outputs = [
136 "${target_out_dir}/${prebuilt_target}.bin",
137 ]
138 }
139 }
David Brazdil3f509e02019-07-01 12:42:25 +0100140}
141
142template("linux_kernel_module") {
143 # Out-of-tree modules cannot be built outside of their directory.
144 # So as to avoid parallel builds under different toolchains clashing,
145 # work around by copying source files to `target_out_dir`.
David Brazdile7a5a1f2019-08-09 18:48:38 +0100146
147 source_target = "${target_name}__source"
148
149 source_dir_copy(source_target) {
150 path = invoker.module_dir
David Brazdil3f509e02019-07-01 12:42:25 +0100151 }
152
153 action(target_name) {
154 forward_variables_from(invoker, [ "testonly" ])
155 script = "//build/make.py"
156 args = [
157 "--directory",
158 rebase_path(target_out_dir),
159 "HAFNIUM_PATH=" + rebase_path("//"),
David Brazdile7a5a1f2019-08-09 18:48:38 +0100160 "KERNEL_PATH=" + rebase_path(invoker.kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +0100161 "O=" +
162 rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")),
163 "CC=" + rebase_path("//prebuilts/linux-x64/clang/bin/clang"),
Andrew Walbran45ac1d22019-10-21 16:54:24 +0100164 "LD=" +
165 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ld"),
166 "AR=" +
167 rebase_path("//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-ar"),
168 "NM=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-nm"),
169 "OBJCOPY=" + rebase_path(
170 "//prebuilts/linux-x64/gcc/bin/aarch64-linux-android-objcopy"),
171 "OBJDUMP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-objdump"),
172 "STRIP=" + rebase_path("//prebuilts/linux-x64/clang/bin/llvm-strip"),
David Brazdil3f509e02019-07-01 12:42:25 +0100173 "ARCH=arm64",
174 "CROSS_COMPILE=aarch64-linux-gnu-",
175 ]
176 outputs = [
177 "${target_out_dir}/${invoker.module_name}.ko",
178 ]
179 deps = [
David Brazdile7a5a1f2019-08-09 18:48:38 +0100180 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100181 "${invoker.kernel_target}__defconfig",
182 ]
183 }
184}