blob: 2090ed9a8beea0843fafbbbd095599b90a8933b2 [file] [log] [blame]
Serge Bazanskif055a7f2021-04-13 16:22:33 +02001# 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"""
18Rules for building Linux kernel images.
19
20This currently performs the build in a fully unhermetic manner, using
Leopoldbc93c2b2023-01-14 13:12:23 +010021make/gcc/... from the sandbox sysroot, and is only slightly better than a genrule. This
Serge Bazanskif055a7f2021-04-13 16:22:33 +020022should be replaced by a hermetic build that at least uses rules_cc toolchain
23information, or even better, just uses cc_library targets.
24"""
25
26load("//build/utils:detect_root.bzl", "detect_root")
27
28
29def _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.
Leopoldbc93c2b2023-01-14 13:12:23 +010040 "//command_line_option:platforms": "//build/platforms:linux_amd64_static",
Serge Bazanskif055a7f2021-04-13 16:22:33 +020041 }
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.
53ignore_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",
Leopoldbc93c2b2023-01-14 13:12:23 +010059 "//command_line_option:platforms",
Serge Bazanskif055a7f2021-04-13 16:22:33 +020060 ],
61)
62
63
64def _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
Mateusz Zalega9a66b182021-12-22 20:33:12 +010072 # (image_source) is the resulting file to be copied and the last
73 # (image_name) is the name of the image that will be generated by this
Serge Bazanskif055a7f2021-04-13 16:22:33 +020074 # rule.
Mateusz Zalega9a66b182021-12-22 20:33:12 +010075 (target, image_source, image_name) = {
Lorenz Brun6c454342023-06-01 12:23:38 +020076 'vmlinux': ('vmlinux modules', 'vmlinux', 'vmlinux'),
77 'bzImage': ('all modules', 'arch/x86/boot/bzImage', 'bzImage'),
Serge Bazanskif055a7f2021-04-13 16:22:33 +020078 }[image_format]
79
80 # Root of the given Linux sources.
81 root = detect_root(ctx.attr.kernel_src)
82
Mateusz Zalega9a66b182021-12-22 20:33:12 +010083 image = ctx.actions.declare_file(image_name)
84 modinfo = ctx.actions.declare_file("modules.builtin.modinfo")
Lorenz Brun6c454342023-06-01 12:23:38 +020085 modules = ctx.actions.declare_directory("modules")
Serge Bazanskif055a7f2021-04-13 16:22:33 +020086 ctx.actions.run_shell(
Lorenz Brun6c454342023-06-01 12:23:38 +020087 outputs = [ image, modinfo, modules ],
Serge Bazanskif055a7f2021-04-13 16:22:33 +020088 inputs = [ kernel_config ] + kernel_src,
89 command = '''
90 kconfig=$1
91 target=$2
Mateusz Zalega9a66b182021-12-22 20:33:12 +010092 image_source=$3
93 image=$4
Serge Bazanskif055a7f2021-04-13 16:22:33 +020094 root=$5
Mateusz Zalega9a66b182021-12-22 20:33:12 +010095 modinfo=$6
Lorenz Brun6c454342023-06-01 12:23:38 +020096 modules=$7
97
98 builddir=$(mktemp -d)
Serge Bazanskif055a7f2021-04-13 16:22:33 +020099
100 mkdir ${root}/.bin
Lorenz Brun6c454342023-06-01 12:23:38 +0200101 cp ${kconfig} ${builddir}/.config
102 (cd ${root} && KBUILD_OUTPUT="${builddir}" make -j $(nproc) ${target} >/dev/null)
103 cp "${builddir}"/${image_source} ${image}
104 cp "${builddir}"/modules.builtin.modinfo ${modinfo}
105 # Not using modules_install as it tries to run depmod and friends
106 for f in $(find "${builddir}" -name '*.ko' -type f -printf "%P\n" ); do
107 install -D "${builddir}/$f" "${modules}/$f"
108 done
109 rm -Rf "$builddir"
Serge Bazanskif055a7f2021-04-13 16:22:33 +0200110 ''',
111 arguments = [
112 kernel_config.path,
113 target,
Mateusz Zalega9a66b182021-12-22 20:33:12 +0100114 image_source,
115 image.path,
Serge Bazanskif055a7f2021-04-13 16:22:33 +0200116 root,
Lorenz Brun6c454342023-06-01 12:23:38 +0200117 modinfo.path,
118 modules.path,
Serge Bazanskif055a7f2021-04-13 16:22:33 +0200119 ],
120 use_default_shell_env = True,
121 )
122
Mateusz Zalega9a66b182021-12-22 20:33:12 +0100123 return [
124 DefaultInfo(
125 files=depset([image]),
126 runfiles=ctx.runfiles(files=[image])
127 ),
128 OutputGroupInfo(
Lorenz Brun6c454342023-06-01 12:23:38 +0200129 modinfo = depset([modinfo]),
130 modules = depset([modules])
Mateusz Zalega9a66b182021-12-22 20:33:12 +0100131 )
132 ]
Serge Bazanskif055a7f2021-04-13 16:22:33 +0200133
134linux_image = rule(
135 doc = '''
136 Build Linux kernel image unhermetically in a given format.
137 ''',
138 implementation = _linux_image_impl,
139 cfg = ignore_unused_configuration,
140 attrs = {
141 "kernel_config": attr.label(
142 doc = '''
143 Linux kernel configuration file to build this kernel image with.
144 ''',
145 allow_single_file = True,
146 default = ":linux-metropolis.config",
147 ),
148 "kernel_src": attr.label(
149 doc = '''
150 Filegroup containing Linux kernel sources.
151 ''',
152 default = "@linux//:all",
153 ),
154 "image_format": attr.string(
155 doc = '''
156 Format of generated Linux image, one of 'vmlinux' or 'bzImage',
157 ''',
158 values = [
159 'vmlinux', 'bzImage',
160 ],
161 default = 'bzImage',
162 ),
163 "_allowlist_function_transition": attr.label(
164 default = "@bazel_tools//tools/allowlists/function_transition_allowlist"
165 ),
166 },
167)