| 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") |
| Tim Windelschmidt | 08054ca | 2025-04-04 01:11:56 +0200 | [diff] [blame^] | 27 | load("//osbase/build:def.bzl", "ignore_unused_configuration") |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 28 | |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 29 | def _linux_image_impl_resources(_os, _ninputs): |
| 30 | """ |
| 31 | Configures linux build resources. |
| 32 | |
| 33 | See `resource_set` documentation in builtins.actions Bazel docs. |
| 34 | """ |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 35 | |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 36 | # 16 threads seems about right - this fits well in both our build machines and |
| 37 | # development machines. |
| 38 | cpu = 16 |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 39 | |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 40 | # In MB. Picked based on observing build in htop. |
| 41 | mb_per_cpu = 256 |
| 42 | return { |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 43 | "cpu": cpu, |
| 44 | "memory": cpu * mb_per_cpu, |
| 45 | "local_test": 0, |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 46 | } |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 47 | |
| 48 | def _linux_image_impl(ctx): |
| 49 | kernel_config = ctx.file.kernel_config |
| 50 | kernel_src = ctx.files.kernel_src |
| 51 | image_format = ctx.attr.image_format |
| 52 | |
| 53 | # Tuple containing information about how to build and access the resulting |
| 54 | # image. |
| 55 | # The first element (target) is the make target to build, the second |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 56 | # (image_source) is the resulting file to be copied and the last |
| 57 | # (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] | 58 | # rule. |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 59 | (target, image_source, image_name) = { |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 60 | "vmlinux": ("vmlinux modules", "vmlinux", "vmlinux"), |
| 61 | "bzImage": ("all modules", "arch/x86/boot/bzImage", "bzImage"), |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 62 | }[image_format] |
| 63 | |
| 64 | # Root of the given Linux sources. |
| 65 | root = detect_root(ctx.attr.kernel_src) |
| 66 | |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 67 | image = ctx.actions.declare_file(image_name) |
| 68 | modinfo = ctx.actions.declare_file("modules.builtin.modinfo") |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 69 | modules = ctx.actions.declare_directory("modules") |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 70 | ctx.actions.run_shell( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 71 | outputs = [image, modinfo, modules], |
| 72 | inputs = [kernel_config] + kernel_src, |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 73 | resource_set = _linux_image_impl_resources, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 74 | command = ''' |
| 75 | kconfig=$1 |
| 76 | target=$2 |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 77 | image_source=$3 |
| 78 | image=$4 |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 79 | root=$5 |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 80 | modinfo=$6 |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 81 | modules=$7 |
| 82 | |
| 83 | builddir=$(mktemp -d) |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 84 | |
| 85 | mkdir ${root}/.bin |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 86 | cp ${kconfig} ${builddir}/.config |
| Serge Bazanski | f9c8249 | 2024-09-16 16:50:39 +0200 | [diff] [blame] | 87 | (cd ${root} && KBUILD_OUTPUT="${builddir}" make -j 16 ${target} >/dev/null) |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 88 | cp "${builddir}"/${image_source} ${image} |
| 89 | cp "${builddir}"/modules.builtin.modinfo ${modinfo} |
| 90 | # Not using modules_install as it tries to run depmod and friends |
| 91 | for f in $(find "${builddir}" -name '*.ko' -type f -printf "%P\n" ); do |
| 92 | install -D "${builddir}/$f" "${modules}/$f" |
| 93 | done |
| 94 | rm -Rf "$builddir" |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 95 | ''', |
| 96 | arguments = [ |
| 97 | kernel_config.path, |
| 98 | target, |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 99 | image_source, |
| 100 | image.path, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 101 | root, |
| Lorenz Brun | 6c45434 | 2023-06-01 12:23:38 +0200 | [diff] [blame] | 102 | modinfo.path, |
| 103 | modules.path, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 104 | ], |
| 105 | use_default_shell_env = True, |
| 106 | ) |
| 107 | |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 108 | return [ |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 109 | DefaultInfo( |
| 110 | files = depset([image]), |
| 111 | runfiles = ctx.runfiles(files = [image]), |
| 112 | ), |
| 113 | OutputGroupInfo( |
| 114 | modinfo = depset([modinfo]), |
| 115 | modules = depset([modules]), |
| 116 | ), |
| Mateusz Zalega | 9a66b18 | 2021-12-22 20:33:12 +0100 | [diff] [blame] | 117 | ] |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 118 | |
| 119 | linux_image = rule( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 120 | doc = """ |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 121 | Build Linux kernel image unhermetically in a given format. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 122 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 123 | implementation = _linux_image_impl, |
| 124 | cfg = ignore_unused_configuration, |
| 125 | attrs = { |
| 126 | "kernel_config": attr.label( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 127 | doc = """ |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 128 | Linux kernel configuration file to build this kernel image with. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 129 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 130 | allow_single_file = True, |
| 131 | default = ":linux-metropolis.config", |
| 132 | ), |
| 133 | "kernel_src": attr.label( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 134 | doc = """ |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 135 | Filegroup containing Linux kernel sources. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 136 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 137 | default = "@linux//:all", |
| 138 | ), |
| 139 | "image_format": attr.string( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 140 | doc = """ |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 141 | Format of generated Linux image, one of 'vmlinux' or 'bzImage', |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 142 | """, |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 143 | values = [ |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 144 | "vmlinux", |
| 145 | "bzImage", |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 146 | ], |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 147 | default = "bzImage", |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 148 | ), |
| 149 | "_allowlist_function_transition": attr.label( |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 150 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| Serge Bazanski | f055a7f | 2021-04-13 16:22:33 +0200 | [diff] [blame] | 151 | ), |
| 152 | }, |
| 153 | ) |