| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 1 | # Copyright 2020 The Monogon Project Authors. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | Rules for building Linux kernel images. |
| 19 | |
| 20 | This currently performs the build in a fully unhermetic manner, using |
| Leopold | bc93c2b | 2023-01-14 13:12:23 +0100 | [diff] [blame] | 21 | make/gcc/... from the sandbox sysroot, and is only slightly better than a genrule. This |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 22 | should be replaced by a hermetic build that at least uses rules_cc toolchain |
| 23 | information, or even better, just uses cc_library targets. |
| 24 | """ |
| 25 | |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 26 | load("@rules_cc//cc:action_names.bzl", "C_COMPILE_ACTION_NAME") |
| 27 | load("@rules_cc//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_ATTRS", "find_cpp_toolchain", "use_cc_toolchain") |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 28 | load("//build/utils:detect_root.bzl", "detect_root") |
| Tim Windelschmidt | 08054ca | 2025-04-04 01:11:56 +0200 | [diff] [blame] | 29 | load("//osbase/build:def.bzl", "ignore_unused_configuration") |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 30 | |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 31 | def _linux_image_impl_resources(_os, _ninputs): |
| 32 | """ |
| 33 | Configures linux build resources. |
| 34 | |
| 35 | See `resource_set` documentation in builtins.actions Bazel docs. |
| 36 | """ |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 37 | |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 38 | # 16 threads seems about right - this fits well in both our build machines and |
| 39 | # development machines. |
| 40 | cpu = 16 |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 41 | |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 42 | # In MB. Picked based on observing build in htop. |
| 43 | mb_per_cpu = 256 |
| 44 | return { |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 45 | "cpu": cpu, |
| 46 | "memory": cpu * mb_per_cpu, |
| 47 | "local_test": 0, |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 48 | } |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 49 | |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 50 | DISABLED_FEATURES = [] |
| 51 | |
| 52 | # NOTE: Multicall tool is called as path/to/llvm clang to workaround bug in out-of-process execution where tool name is repeated and parsing breaks. |
| 53 | |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 54 | def _linux_image_impl(ctx): |
| 55 | kernel_config = ctx.file.kernel_config |
| 56 | kernel_src = ctx.files.kernel_src |
| 57 | image_format = ctx.attr.image_format |
| 58 | |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 59 | # Root of the given Linux sources. |
| 60 | root = detect_root(ctx.attr.kernel_src) |
| 61 | |
| 62 | # Figure out target CC toolchain |
| 63 | cc_toolchain = find_cpp_toolchain(ctx) |
| 64 | feature_configuration = cc_common.configure_features( |
| 65 | ctx = ctx, |
| 66 | cc_toolchain = cc_toolchain, |
| 67 | requested_features = ctx.features, |
| 68 | unsupported_features = DISABLED_FEATURES + ctx.disabled_features, |
| 69 | ) |
| 70 | c_compiler_path = cc_common.get_tool_for_action( |
| 71 | feature_configuration = feature_configuration, |
| 72 | action_name = C_COMPILE_ACTION_NAME, |
| 73 | ) |
| 74 | |
| 75 | # Figure out Kbuild ARCH option |
| 76 | target_arch = None |
| 77 | compressed_image_name = None |
| 78 | |
| 79 | if ctx.target_platform_has_constraint(ctx.attr._constraint_x86_64[platform_common.ConstraintValueInfo]): |
| 80 | target_arch = "x86" |
| 81 | compressed_image_name = "bzImage" |
| 82 | |
| 83 | if ctx.target_platform_has_constraint(ctx.attr._constraint_aarch64[platform_common.ConstraintValueInfo]): |
| 84 | target_arch = "arm64" |
| 85 | compressed_image_name = "Image" |
| 86 | |
| 87 | if ctx.target_platform_has_constraint(ctx.attr._constraint_riscv64[platform_common.ConstraintValueInfo]): |
| 88 | target_arch = "riscv" |
| 89 | compressed_image_name = "Image" |
| 90 | |
| 91 | if not target_arch: |
| 92 | fail("Target platform does not match expected constraints: @platforms//cpu:x86_64, @platforms//cpu:aarch64, or @platforms//cpu:riscv64.") |
| 93 | |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 94 | # Tuple containing information about how to build and access the resulting |
| 95 | # image. |
| 96 | # The first element (target) is the make target to build, the second |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 97 | # (image_source) is the resulting file to be copied and the last |
| 98 | # (image_name) is the name of the image that will be generated by this |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 99 | # rule. |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 100 | (target, image_source, image_name) = { |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 101 | "vmlinux": ("vmlinux modules", "vmlinux", "vmlinux"), |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 102 | "Image": ("all modules", "arch/" + target_arch + "/boot/" + compressed_image_name, "Image"), |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 103 | }[image_format] |
| 104 | |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 105 | image = ctx.actions.declare_file(image_name) |
| 106 | modinfo = ctx.actions.declare_file("modules.builtin.modinfo") |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 107 | modules = ctx.actions.declare_directory("modules") |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 108 | ctx.actions.run_shell( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 109 | outputs = [image, modinfo, modules], |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 110 | inputs = depset([kernel_config] + kernel_src, transitive = [cc_toolchain.all_files]), |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 111 | resource_set = _linux_image_impl_resources, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 112 | command = ''' |
| 113 | kconfig=$1 |
| 114 | target=$2 |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 115 | image_source=$3 |
| 116 | image=$4 |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 117 | root=$5 |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 118 | modinfo=$6 |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 119 | modules=$7 |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 120 | arch=$8 |
| 121 | cc=$PWD/$9 |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 122 | |
| 123 | builddir=$(mktemp -d) |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 124 | |
| 125 | mkdir ${root}/.bin |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 126 | cp ${kconfig} ${builddir}/.config |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 127 | (cd ${root} && make -j 16 KBUILD_OUTPUT="${builddir}" ARCH="${arch}" CC="${cc//clang/llvm} clang" LD="${cc//clang/ld.lld}" OBJCOPY="${cc//clang/llvm-objcopy}" OBJDUMP="${cc//clang/llvm-objdump}" AR="${cc//clang/llvm-ar}" NM="${cc//clang/llvm-nm}" STRIP="${cc//clang/llvm-strip}" READELF="${cc//clang/llvm-readelf}" olddefconfig ${target} >/dev/null) |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 128 | cp "${builddir}"/${image_source} ${image} |
| 129 | cp "${builddir}"/modules.builtin.modinfo ${modinfo} |
| 130 | # Not using modules_install as it tries to run depmod and friends |
| 131 | for f in $(find "${builddir}" -name '*.ko' -type f -printf "%P\n" ); do |
| 132 | install -D "${builddir}/$f" "${modules}/$f" |
| 133 | done |
| 134 | rm -Rf "$builddir" |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 135 | ''', |
| 136 | arguments = [ |
| 137 | kernel_config.path, |
| 138 | target, |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 139 | image_source, |
| 140 | image.path, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 141 | root, |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 142 | modinfo.path, |
| 143 | modules.path, |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 144 | target_arch, |
| 145 | c_compiler_path, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 146 | ], |
| 147 | use_default_shell_env = True, |
| 148 | ) |
| 149 | |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 150 | return [ |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 151 | DefaultInfo( |
| 152 | files = depset([image]), |
| 153 | runfiles = ctx.runfiles(files = [image]), |
| 154 | ), |
| 155 | OutputGroupInfo( |
| 156 | modinfo = depset([modinfo]), |
| 157 | modules = depset([modules]), |
| 158 | ), |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 159 | ] |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 160 | |
| 161 | linux_image = rule( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 162 | doc = """ |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 163 | Build Linux kernel image unhermetically in a given format. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 164 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 165 | implementation = _linux_image_impl, |
| 166 | cfg = ignore_unused_configuration, |
| 167 | attrs = { |
| 168 | "kernel_config": attr.label( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 169 | doc = """ |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 170 | Linux kernel configuration file to build this kernel image with. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 171 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 172 | allow_single_file = True, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 173 | ), |
| 174 | "kernel_src": attr.label( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 175 | doc = """ |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 176 | Filegroup containing Linux kernel sources. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 177 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 178 | default = "@linux//:all", |
| 179 | ), |
| 180 | "image_format": attr.string( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 181 | doc = """ |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 182 | Format of generated Linux image, one of 'vmlinux' or 'Image', |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 183 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 184 | values = [ |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 185 | "vmlinux", |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 186 | "Image", |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 187 | ], |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 188 | default = "Image", |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 189 | ), |
| 190 | "_allowlist_function_transition": attr.label( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 191 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 192 | ), |
| Lorenz Brun | 6df40aa | 2025-05-22 15:35:44 +0200 | [diff] [blame] | 193 | # Bazel doesn't let you access the target platform directly, use these |
| 194 | "_constraint_x86_64": attr.label( |
| 195 | default = "@platforms//cpu:x86_64", |
| 196 | ), |
| 197 | "_constraint_aarch64": attr.label( |
| 198 | default = "@platforms//cpu:aarch64", |
| 199 | ), |
| 200 | "_constraint_riscv64": attr.label( |
| 201 | default = "@platforms//cpu:riscv64", |
| 202 | ), |
| 203 | } | CC_TOOLCHAIN_ATTRS, |
| 204 | toolchains = use_cc_toolchain(), |
| 205 | fragments = ["cpp"], |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 206 | ) |