David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 1 | # Copyright 2019 The Hafnium Authors. |
| 2 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 3 | # 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 Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 6 | |
David Brazdil | b4802bc | 2019-07-30 12:39:41 +0100 | [diff] [blame] | 7 | template("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 Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 23 | template("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 Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 46 | template("linux_kernel") { |
David Brazdil | b4802bc | 2019-07-30 12:39:41 +0100 | [diff] [blame] | 47 | source_target = "${target_name}__source" |
| 48 | defconfig_target = "${target_name}__defconfig" |
David Brazdil | 4b0e111 | 2019-07-30 18:28:01 +0100 | [diff] [blame] | 49 | prebuilt_target = "${target_name}__prebuilt" |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 50 | |
David Brazdil | 33cc5c0 | 2019-12-10 10:44:14 +0000 | [diff] [blame] | 51 | kernel_dir = "./" |
| 52 | |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 53 | # Args to build/make.py to start the Linux build. |
| 54 | shared_args = [ |
| 55 | "--directory", |
David Brazdil | 33cc5c0 | 2019-12-10 10:44:14 +0000 | [diff] [blame] | 56 | rebase_path(kernel_dir), |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 57 | |
| 58 | # TODO: Build with toolchain cc instead of a hardcoded one. |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 59 | "ARCH=arm64", |
Olivier Deprez | c16b0ec | 2021-09-23 11:39:39 +0200 | [diff] [blame] | 60 | "LLVM=1", |
| 61 | "LLVM_IAS=1", |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 62 | "CROSS_COMPILE=aarch64-linux-gnu-", |
| 63 | |
| 64 | # Build out-of-tree in `target_out_dir`. |
| 65 | "O=" + rebase_path(target_out_dir), |
| 66 | |
Olivier Deprez | c16b0ec | 2021-09-23 11:39:39 +0200 | [diff] [blame] | 67 | # TODO: assess if this setting is really required because the ninja |
| 68 | # top level invocation already cares about parallel build. |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 69 | "-j24", |
| 70 | ] |
| 71 | |
David Brazdil | b4802bc | 2019-07-30 12:39:41 +0100 | [diff] [blame] | 72 | # Subtarget which generates a depfile with all files in the Linux tree |
| 73 | # and gets invalidated if any of them change. |
| 74 | source_dir(source_target) { |
David Brazdil | 33cc5c0 | 2019-12-10 10:44:14 +0000 | [diff] [blame] | 75 | path = kernel_dir |
David Brazdil | b4802bc | 2019-07-30 12:39:41 +0100 | [diff] [blame] | 76 | } |
| 77 | |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 78 | # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets |
| 79 | # which do not require the whole kernel to have been built. |
David Brazdil | b4802bc | 2019-07-30 12:39:41 +0100 | [diff] [blame] | 80 | action(defconfig_target) { |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 81 | script = "//build/make.py" |
| 82 | args = shared_args + [ |
| 83 | "defconfig", |
| 84 | "modules_prepare", |
| 85 | ] |
| 86 | |
| 87 | # We never use the output but GN requires each target to have one, and for |
| 88 | # its timestamp to change after a recompile. Use the .config file. |
| 89 | outputs = [ |
| 90 | "${target_out_dir}/.config", |
| 91 | ] |
David Brazdil | b4802bc | 2019-07-30 12:39:41 +0100 | [diff] [blame] | 92 | deps = [ |
| 93 | ":${source_target}", |
| 94 | ] |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | action(target_name) { |
| 98 | script = "//build/make.py" |
| 99 | output_file = "${target_out_dir}/${target_name}.bin" |
| 100 | args = shared_args + [ |
| 101 | "--copy_out_file", |
| 102 | rebase_path("${target_out_dir}/arch/arm64/boot/Image"), |
| 103 | rebase_path(output_file), |
| 104 | ] |
| 105 | outputs = [ |
| 106 | output_file, |
| 107 | ] |
| 108 | deps = [ |
David Brazdil | b4802bc | 2019-07-30 12:39:41 +0100 | [diff] [blame] | 109 | ":${defconfig_target}", |
| 110 | ":${source_target}", |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 111 | ] |
| 112 | } |
David Brazdil | 4b0e111 | 2019-07-30 18:28:01 +0100 | [diff] [blame] | 113 | |
| 114 | # Subtarget for a prebuilt image, if defined. |
| 115 | if (defined(invoker.prebuilt)) { |
| 116 | copy(prebuilt_target) { |
| 117 | sources = [ |
| 118 | invoker.prebuilt, |
| 119 | ] |
| 120 | outputs = [ |
| 121 | "${target_out_dir}/${prebuilt_target}.bin", |
| 122 | ] |
| 123 | } |
| 124 | } |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | template("linux_kernel_module") { |
| 128 | # Out-of-tree modules cannot be built outside of their directory. |
| 129 | # So as to avoid parallel builds under different toolchains clashing, |
| 130 | # work around by copying source files to `target_out_dir`. |
David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 131 | |
| 132 | source_target = "${target_name}__source" |
| 133 | |
David Brazdil | cb92ba1 | 2019-12-23 14:15:08 +0000 | [diff] [blame] | 134 | module_dir = "./" |
| 135 | |
David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 136 | source_dir_copy(source_target) { |
David Brazdil | cb92ba1 | 2019-12-23 14:15:08 +0000 | [diff] [blame] | 137 | path = module_dir |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | action(target_name) { |
| 141 | forward_variables_from(invoker, [ "testonly" ]) |
| 142 | script = "//build/make.py" |
| 143 | args = [ |
| 144 | "--directory", |
| 145 | rebase_path(target_out_dir), |
| 146 | "HAFNIUM_PATH=" + rebase_path("//"), |
David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 147 | "KERNEL_PATH=" + rebase_path(invoker.kernel_dir), |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 148 | "O=" + |
| 149 | rebase_path(get_label_info(invoker.kernel_target, "target_out_dir")), |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 150 | "ARCH=arm64", |
Olivier Deprez | c16b0ec | 2021-09-23 11:39:39 +0200 | [diff] [blame] | 151 | "LLVM=1", |
| 152 | "LLVM_IAS=1", |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 153 | "CROSS_COMPILE=aarch64-linux-gnu-", |
| 154 | ] |
| 155 | outputs = [ |
| 156 | "${target_out_dir}/${invoker.module_name}.ko", |
| 157 | ] |
| 158 | deps = [ |
David Brazdil | e7a5a1f | 2019-08-09 18:48:38 +0100 | [diff] [blame] | 159 | ":${source_target}", |
David Brazdil | 3f509e0 | 2019-07-01 12:42:25 +0100 | [diff] [blame] | 160 | "${invoker.kernel_target}__defconfig", |
| 161 | ] |
| 162 | } |
| 163 | } |