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 |
| 21 | make/gcc/... from the host, and is only slightly better than a genrule. This |
| 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 | |
| 29 | def _ignore_unused_configuration_impl(settings, attr): |
| 30 | return { |
| 31 | # This list should be expanded with any configuration options that end |
| 32 | # up reaching this rule with different values across different build |
| 33 | # graph paths, but that do not actually influence the kernel build. |
| 34 | # Force-setting them to a stable value forces the build configuration |
| 35 | # to a stable hash. |
| 36 | # See the transition's comment block for more information. |
| 37 | "@io_bazel_rules_go//go/config:pure": True, |
| 38 | "@io_bazel_rules_go//go/config:static": True, |
| 39 | # Note: this toolchain is not actually used to perform the build. |
| 40 | "//command_line_option:crosstool_top": "//build/toolchain/musl-host-gcc:musl_host_cc_suite", |
| 41 | } |
| 42 | |
| 43 | # Transition to flip all known-unimportant but varying configuration options to |
| 44 | # a known, stable value. |
| 45 | # This is to prevent Bazel from creating extra configurations for possible |
| 46 | # combinations of options in case the linux_image rule is pulled through build |
| 47 | # graph fragments that have different options set. |
| 48 | # |
| 49 | # Ideally, Bazel would let us mark in a list that we only care about some set |
| 50 | # of options (or at least let us mark those that we explicitly don't care |
| 51 | # about, instead of manually setting them to some value). However, this doesn't |
| 52 | # seem to be possible, thus this transition is a bit of a hack. |
| 53 | ignore_unused_configuration = transition( |
| 54 | implementation = _ignore_unused_configuration_impl, |
| 55 | inputs = [], |
| 56 | outputs = [ |
| 57 | "@io_bazel_rules_go//go/config:pure", |
| 58 | "@io_bazel_rules_go//go/config:static", |
| 59 | "//command_line_option:crosstool_top", |
| 60 | ], |
| 61 | ) |
| 62 | |
| 63 | |
| 64 | def _linux_image_impl(ctx): |
| 65 | kernel_config = ctx.file.kernel_config |
| 66 | kernel_src = ctx.files.kernel_src |
| 67 | image_format = ctx.attr.image_format |
| 68 | |
| 69 | # Tuple containing information about how to build and access the resulting |
| 70 | # image. |
| 71 | # The first element (target) is the make target to build, the second |
| 72 | # (output_source) is the resulting file to be copied and the last |
| 73 | # (output_name) is the name of the output that will be generated by this |
| 74 | # rule. |
| 75 | (target, output_source, output_name) = { |
| 76 | 'vmlinux': ('vmlinux', 'vmlinux', 'vmlinux'), |
| 77 | 'bzImage': ('all', 'arch/x86/boot/bzImage', 'bzImage'), |
| 78 | }[image_format] |
| 79 | |
| 80 | # Root of the given Linux sources. |
| 81 | root = detect_root(ctx.attr.kernel_src) |
| 82 | |
| 83 | output = ctx.actions.declare_file(output_name) |
| 84 | ctx.actions.run_shell( |
| 85 | outputs = [ output ], |
| 86 | inputs = [ kernel_config ] + kernel_src, |
| 87 | command = ''' |
| 88 | kconfig=$1 |
| 89 | target=$2 |
| 90 | output_source=$3 |
| 91 | output=$4 |
| 92 | root=$5 |
| 93 | |
| 94 | mkdir ${root}/.bin |
| 95 | cp ${kconfig} ${root}/.config |
| 96 | (cd ${root} && make -j $(nproc) ${target} >/dev/null) |
| 97 | cp ${root}/${output_source} ${output} |
| 98 | ''', |
| 99 | arguments = [ |
| 100 | kernel_config.path, |
| 101 | target, |
| 102 | output_source, |
| 103 | output.path, |
| 104 | root, |
| 105 | ], |
| 106 | use_default_shell_env = True, |
| 107 | ) |
| 108 | |
| 109 | files = depset([output]) |
| 110 | runfiles = ctx.runfiles(files=[output]) |
| 111 | return [DefaultInfo(files=files, runfiles=runfiles)] |
| 112 | |
| 113 | |
| 114 | linux_image = rule( |
| 115 | doc = ''' |
| 116 | Build Linux kernel image unhermetically in a given format. |
| 117 | ''', |
| 118 | implementation = _linux_image_impl, |
| 119 | cfg = ignore_unused_configuration, |
| 120 | attrs = { |
| 121 | "kernel_config": attr.label( |
| 122 | doc = ''' |
| 123 | Linux kernel configuration file to build this kernel image with. |
| 124 | ''', |
| 125 | allow_single_file = True, |
| 126 | default = ":linux-metropolis.config", |
| 127 | ), |
| 128 | "kernel_src": attr.label( |
| 129 | doc = ''' |
| 130 | Filegroup containing Linux kernel sources. |
| 131 | ''', |
| 132 | default = "@linux//:all", |
| 133 | ), |
| 134 | "image_format": attr.string( |
| 135 | doc = ''' |
| 136 | Format of generated Linux image, one of 'vmlinux' or 'bzImage', |
| 137 | ''', |
| 138 | values = [ |
| 139 | 'vmlinux', 'bzImage', |
| 140 | ], |
| 141 | default = 'bzImage', |
| 142 | ), |
| 143 | "_allowlist_function_transition": attr.label( |
| 144 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist" |
| 145 | ), |
| 146 | }, |
| 147 | ) |