blob: 4979152901062cdd5d9eeea21f5a66e47bf15912 [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"
Olivier Deprez306d7812023-02-02 09:51:10 +010010 outputs = [ "$target_out_dir/${target_name}.script.stamp" ]
David Brazdilb4802bc2019-07-30 12:39:41 +010011
12 script = "//build/linux/gen_depfile.py"
13 args = [
14 rebase_path(invoker.path, root_build_dir),
15 rebase_path(outputs[0], root_build_dir),
16 rebase_path(depfile, root_build_dir),
17 ]
18 }
19}
20
David Brazdile7a5a1f2019-08-09 18:48:38 +010021template("source_dir_copy") {
22 source_dir_target = "${target_name}__source_dir"
23
24 source_dir(source_dir_target) {
25 path = invoker.path
26 }
27
28 action("${target_name}") {
29 script = "//build/linux/copy_dirs.py"
Olivier Deprez306d7812023-02-02 09:51:10 +010030 outputs = [ "$target_out_dir/${target_name}.script.stamp" ]
David Brazdile7a5a1f2019-08-09 18:48:38 +010031 args = [
32 rebase_path(invoker.path),
33 rebase_path(target_out_dir),
34 rebase_path(outputs[0]),
35 ]
Olivier Deprez306d7812023-02-02 09:51:10 +010036 deps = [ ":${source_dir_target}" ]
David Brazdile7a5a1f2019-08-09 18:48:38 +010037 }
38}
39
David Brazdil3f509e02019-07-01 12:42:25 +010040template("linux_kernel") {
David Brazdilb4802bc2019-07-30 12:39:41 +010041 source_target = "${target_name}__source"
42 defconfig_target = "${target_name}__defconfig"
David Brazdil4b0e1112019-07-30 18:28:01 +010043 prebuilt_target = "${target_name}__prebuilt"
David Brazdil3f509e02019-07-01 12:42:25 +010044
David Brazdil33cc5c02019-12-10 10:44:14 +000045 kernel_dir = "./"
46
David Brazdil3f509e02019-07-01 12:42:25 +010047 # Args to build/make.py to start the Linux build.
48 shared_args = [
49 "--directory",
David Brazdil33cc5c02019-12-10 10:44:14 +000050 rebase_path(kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +010051
52 # TODO: Build with toolchain cc instead of a hardcoded one.
David Brazdil3f509e02019-07-01 12:42:25 +010053 "ARCH=arm64",
Olivier Deprezc16b0ec2021-09-23 11:39:39 +020054 "LLVM=1",
55 "LLVM_IAS=1",
David Brazdil3f509e02019-07-01 12:42:25 +010056 "CROSS_COMPILE=aarch64-linux-gnu-",
57
58 # Build out-of-tree in `target_out_dir`.
59 "O=" + rebase_path(target_out_dir),
60
Olivier Deprezc16b0ec2021-09-23 11:39:39 +020061 # TODO: assess if this setting is really required because the ninja
62 # top level invocation already cares about parallel build.
David Brazdil3f509e02019-07-01 12:42:25 +010063 "-j24",
64 ]
65
David Brazdilb4802bc2019-07-30 12:39:41 +010066 # Subtarget which generates a depfile with all files in the Linux tree
67 # and gets invalidated if any of them change.
68 source_dir(source_target) {
David Brazdil33cc5c02019-12-10 10:44:14 +000069 path = kernel_dir
David Brazdilb4802bc2019-07-30 12:39:41 +010070 }
71
David Brazdil3f509e02019-07-01 12:42:25 +010072 # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
73 # which do not require the whole kernel to have been built.
David Brazdilb4802bc2019-07-30 12:39:41 +010074 action(defconfig_target) {
David Brazdil3f509e02019-07-01 12:42:25 +010075 script = "//build/make.py"
76 args = shared_args + [
77 "defconfig",
78 "modules_prepare",
79 ]
80
81 # We never use the output but GN requires each target to have one, and for
82 # its timestamp to change after a recompile. Use the .config file.
Olivier Deprez306d7812023-02-02 09:51:10 +010083 outputs = [ "${target_out_dir}/.config" ]
84 deps = [ ":${source_target}" ]
David Brazdil3f509e02019-07-01 12:42:25 +010085 }
86
87 action(target_name) {
88 script = "//build/make.py"
89 output_file = "${target_out_dir}/${target_name}.bin"
90 args = shared_args + [
91 "--copy_out_file",
92 rebase_path("${target_out_dir}/arch/arm64/boot/Image"),
93 rebase_path(output_file),
94 ]
Olivier Deprez306d7812023-02-02 09:51:10 +010095 outputs = [ output_file ]
David Brazdil3f509e02019-07-01 12:42:25 +010096 deps = [
David Brazdilb4802bc2019-07-30 12:39:41 +010097 ":${defconfig_target}",
98 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +010099 ]
100 }
David Brazdil4b0e1112019-07-30 18:28:01 +0100101
102 # Subtarget for a prebuilt image, if defined.
103 if (defined(invoker.prebuilt)) {
104 copy(prebuilt_target) {
Olivier Deprez306d7812023-02-02 09:51:10 +0100105 sources = [ invoker.prebuilt ]
106 outputs = [ "${target_out_dir}/${prebuilt_target}.bin" ]
David Brazdil4b0e1112019-07-30 18:28:01 +0100107 }
108 }
David Brazdil3f509e02019-07-01 12:42:25 +0100109}
110
111template("linux_kernel_module") {
112 # Out-of-tree modules cannot be built outside of their directory.
113 # So as to avoid parallel builds under different toolchains clashing,
114 # work around by copying source files to `target_out_dir`.
David Brazdile7a5a1f2019-08-09 18:48:38 +0100115
116 source_target = "${target_name}__source"
117
David Brazdilcb92ba12019-12-23 14:15:08 +0000118 module_dir = "./"
119
David Brazdile7a5a1f2019-08-09 18:48:38 +0100120 source_dir_copy(source_target) {
David Brazdilcb92ba12019-12-23 14:15:08 +0000121 path = module_dir
David Brazdil3f509e02019-07-01 12:42:25 +0100122 }
123
124 action(target_name) {
125 forward_variables_from(invoker, [ "testonly" ])
126 script = "//build/make.py"
127 args = [
128 "--directory",
129 rebase_path(target_out_dir),
130 "HAFNIUM_PATH=" + rebase_path("//"),
David Brazdile7a5a1f2019-08-09 18:48:38 +0100131 "KERNEL_PATH=" + rebase_path(invoker.kernel_dir),
David Brazdil3f509e02019-07-01 12:42:25 +0100132 "O=" +
133 rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")),
David Brazdil3f509e02019-07-01 12:42:25 +0100134 "ARCH=arm64",
Olivier Deprezc16b0ec2021-09-23 11:39:39 +0200135 "LLVM=1",
136 "LLVM_IAS=1",
David Brazdil3f509e02019-07-01 12:42:25 +0100137 "CROSS_COMPILE=aarch64-linux-gnu-",
138 ]
Olivier Deprez306d7812023-02-02 09:51:10 +0100139 outputs = [ "${target_out_dir}/${invoker.module_name}.ko" ]
David Brazdil3f509e02019-07-01 12:42:25 +0100140 deps = [
David Brazdile7a5a1f2019-08-09 18:48:38 +0100141 ":${source_target}",
David Brazdil3f509e02019-07-01 12:42:25 +0100142 "${invoker.kernel_target}__defconfig",
143 ]
144 }
145}