| 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 | |
| 26 | load("//build/utils:detect_root.bzl", "detect_root") |
| 27 | |
| 28 | |
| Serge Bazanski | 5b13d81 | 2023-06-20 13:22:23 +0200 | [diff] [blame] | 29 | _new_settings = { |
| 30 | # This list should be expanded with any configuration options that end |
| 31 | # up reaching this rule with different values across different build |
| 32 | # graph paths, but that do not actually influence the kernel build. |
| 33 | # Force-setting them to a stable value forces the build configuration |
| 34 | # to a stable hash. |
| 35 | # See the transition's comment block for more information. |
| Tim Windelschmidt | 117006e | 2024-12-09 22:53:52 +0100 | [diff] [blame] | 36 | "@io_bazel_rules_go//go/config:pure": False, |
| 37 | "@io_bazel_rules_go//go/config:static": False, |
| Serge Bazanski | 5b13d81 | 2023-06-20 13:22:23 +0200 | [diff] [blame] | 38 | |
| Serge Bazanski | 5b13d81 | 2023-06-20 13:22:23 +0200 | [diff] [blame] | 39 | # Note: this toolchain is not actually used to perform the build. |
| 40 | "//command_line_option:platforms": "//build/platforms:linux_amd64_static", |
| 41 | } |
| 42 | |
| 43 | |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 44 | def _ignore_unused_configuration_impl(settings, attr): |
| Serge Bazanski | 5b13d81 | 2023-06-20 13:22:23 +0200 | [diff] [blame] | 45 | return _new_settings |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 46 | |
| 47 | # Transition to flip all known-unimportant but varying configuration options to |
| 48 | # a known, stable value. |
| 49 | # This is to prevent Bazel from creating extra configurations for possible |
| 50 | # combinations of options in case the linux_image rule is pulled through build |
| 51 | # graph fragments that have different options set. |
| 52 | # |
| 53 | # Ideally, Bazel would let us mark in a list that we only care about some set |
| 54 | # of options (or at least let us mark those that we explicitly don't care |
| 55 | # about, instead of manually setting them to some value). However, this doesn't |
| 56 | # seem to be possible, thus this transition is a bit of a hack. |
| 57 | ignore_unused_configuration = transition( |
| 58 | implementation = _ignore_unused_configuration_impl, |
| 59 | inputs = [], |
| Serge Bazanski | 5b13d81 | 2023-06-20 13:22:23 +0200 | [diff] [blame] | 60 | outputs = list(_new_settings.keys()), |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 61 | ) |
| 62 | |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 63 | def _linux_image_impl_resources(_os, _ninputs): |
| 64 | """ |
| 65 | Configures linux build resources. |
| 66 | |
| 67 | See `resource_set` documentation in builtins.actions Bazel docs. |
| 68 | """ |
| 69 | # 16 threads seems about right - this fits well in both our build machines and |
| 70 | # development machines. |
| 71 | cpu = 16 |
| 72 | # In MB. Picked based on observing build in htop. |
| 73 | mb_per_cpu = 256 |
| 74 | return { |
| 75 | 'cpu': cpu, |
| 76 | 'memory': cpu * mb_per_cpu, |
| 77 | 'local_test': 0, |
| 78 | } |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 79 | |
| 80 | def _linux_image_impl(ctx): |
| 81 | kernel_config = ctx.file.kernel_config |
| 82 | kernel_src = ctx.files.kernel_src |
| 83 | image_format = ctx.attr.image_format |
| 84 | |
| 85 | # Tuple containing information about how to build and access the resulting |
| 86 | # image. |
| 87 | # The first element (target) is the make target to build, the second |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 88 | # (image_source) is the resulting file to be copied and the last |
| 89 | # (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] | 90 | # rule. |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 91 | (target, image_source, image_name) = { |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 92 | 'vmlinux': ('vmlinux modules', 'vmlinux', 'vmlinux'), |
| 93 | 'bzImage': ('all modules', 'arch/x86/boot/bzImage', 'bzImage'), |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 94 | }[image_format] |
| 95 | |
| 96 | # Root of the given Linux sources. |
| 97 | root = detect_root(ctx.attr.kernel_src) |
| 98 | |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 99 | image = ctx.actions.declare_file(image_name) |
| 100 | modinfo = ctx.actions.declare_file("modules.builtin.modinfo") |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 101 | modules = ctx.actions.declare_directory("modules") |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 102 | ctx.actions.run_shell( |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 103 | outputs = [ image, modinfo, modules ], |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 104 | inputs = [ kernel_config ] + kernel_src, |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 105 | resource_set = _linux_image_impl_resources, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 106 | command = ''' |
| 107 | kconfig=$1 |
| 108 | target=$2 |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 109 | image_source=$3 |
| 110 | image=$4 |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 111 | root=$5 |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 112 | modinfo=$6 |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 113 | modules=$7 |
| 114 | |
| 115 | builddir=$(mktemp -d) |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 116 | |
| 117 | mkdir ${root}/.bin |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 118 | cp ${kconfig} ${builddir}/.config |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 119 | (cd ${root} && KBUILD_OUTPUT="${builddir}" make -j 16 ${target} >/dev/null) |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 120 | cp "${builddir}"/${image_source} ${image} |
| 121 | cp "${builddir}"/modules.builtin.modinfo ${modinfo} |
| 122 | # Not using modules_install as it tries to run depmod and friends |
| 123 | for f in $(find "${builddir}" -name '*.ko' -type f -printf "%P\n" ); do |
| 124 | install -D "${builddir}/$f" "${modules}/$f" |
| 125 | done |
| 126 | rm -Rf "$builddir" |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 127 | ''', |
| 128 | arguments = [ |
| 129 | kernel_config.path, |
| 130 | target, |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 131 | image_source, |
| 132 | image.path, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 133 | root, |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 134 | modinfo.path, |
| 135 | modules.path, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 136 | ], |
| 137 | use_default_shell_env = True, |
| 138 | ) |
| 139 | |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 140 | return [ |
| 141 | DefaultInfo( |
| 142 | files=depset([image]), |
| 143 | runfiles=ctx.runfiles(files=[image]) |
| 144 | ), |
| 145 | OutputGroupInfo( |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 146 | modinfo = depset([modinfo]), |
| 147 | modules = depset([modules]) |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 148 | ) |
| 149 | ] |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 150 | |
| 151 | linux_image = rule( |
| 152 | doc = ''' |
| 153 | Build Linux kernel image unhermetically in a given format. |
| 154 | ''', |
| 155 | implementation = _linux_image_impl, |
| 156 | cfg = ignore_unused_configuration, |
| 157 | attrs = { |
| 158 | "kernel_config": attr.label( |
| 159 | doc = ''' |
| 160 | Linux kernel configuration file to build this kernel image with. |
| 161 | ''', |
| 162 | allow_single_file = True, |
| 163 | default = ":linux-metropolis.config", |
| 164 | ), |
| 165 | "kernel_src": attr.label( |
| 166 | doc = ''' |
| 167 | Filegroup containing Linux kernel sources. |
| 168 | ''', |
| 169 | default = "@linux//:all", |
| 170 | ), |
| 171 | "image_format": attr.string( |
| 172 | doc = ''' |
| 173 | Format of generated Linux image, one of 'vmlinux' or 'bzImage', |
| 174 | ''', |
| 175 | values = [ |
| 176 | 'vmlinux', 'bzImage', |
| 177 | ], |
| 178 | default = 'bzImage', |
| 179 | ), |
| 180 | "_allowlist_function_transition": attr.label( |
| 181 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist" |
| 182 | ), |
| 183 | }, |
| 184 | ) |