Note: You may view the documentation for the following Bazel rules and macros on Android Continuous Integration. See API Reference and Documentation for all rules.
WARNING: It is recommended to use the common Android kernel under //common (the so-called “mixed build”) instead of building a custom kernel.
You may define a kernel_build target to build a custom kernel. The name of the kernel_build target is usually the name of your device, e.g. tuna.
The outs attribute of the target should align with the FILES variable in build.config. This may include DTB files and kernel images, e.g. vmlinux.
The module_outs attribute of the target includes the list of in-tree drivers that you are building. See section to build in-tree drivers (Step 1) below.
load("//build/kernel/kleaf:kernel.bzl","kernel_build")
load("//build/kernel/kleaf:common_kernels.bzl", "arm64_outs")
kernel_build(
name = "tuna",
srcs = glob(
["**"],
exclude = [
"**/BUILD.bazel",
"**/*.bzl",
".git/**",
],
),
outs = arm64_outs,
build_config = "build.config.tuna",
)
If you have a separate kernel tree to build in-tree drivers, define a kernel_build target to build these modules. The name of the kernel_build target is usually the name of your device, e.g. tuna.
If you also have external kernel modules to be built, be sure to set visibility accordingly, so that the targets to build external kernel modules can refer to this kernel_build target.
If you are building a custom kernel, you may reuse the existing kernel_build target, and keep kernel images in outs. If you are building against GKI, set the base_kernel attribute accordingly (e.g. to //common:kernel_aarch64).
The makefile attribute should point to the Makefile under the kernel source tree. Usually, this is //common:Makefile.
The make_goals attribute should be the list of GNUMake goals you are building. Usually, this contains ["modules"]. Do not include dtbs; see Building devicetree.
The outs attribute of the target should align with the FILES variable in build.config. This is sometimes an empty list. For DTB files, see Building devicetree.
The module_outs attribute of the target includes the list of in-tree drivers that you are building.
Note: It is recommended that kernel modules are moved out of the kernel tree to be built as external kernel modules. This means keeping the list of module_outs empty or as short as possible. See Step 2 for building external kernel modules.
For other build configurations defined in the build.config file, see build_configs.md.
Example for Pixel 2021 (see the kernel_build target named slider):
See ddk/main.md for details.
depmodDefine a kernel_modules_install target that includes all external kernel modules created in Step 2. This is equivalent to running make modules_install, which runs depmod.
The name of the target is usually the name of your device with _modules_install appended to it, e.g. tuna_modules_install.
See Step 2 to determine the kernel_build attribute of the target.
Example for Pixel 2021 (see the kernel_modules_install target named slider_modules_install):
Define initramfs, vendor_boot_image, vendor_dlkm_image, system_dlkm_image etc.
The name of the target is usually the name of your device with the type of the image appended to it, e.g. tuna_initramfs.
If you do not need to build any partition images, skip this step.
Example for Pixel 2021 (see slider_initramfs and slider_vendor_dlkm_image):
Define a pkg_files target and a pkg_install target that includes the targets you want in the distribution directory. The name of this pkg_install target is usually the name of your device with _dist appended to it, e.g. tuna_dist.
You may set strip_prefix = strip_prefix.files_only() so the directory structure within destdir is flattened. If an alternative directory structure is desired, use other rules and functions in @rules_pkg//pkg:mappings.bzl to achieve this.
It is recommended to set destdir so you do not have to specify it in the command line every time.
Add the following to the srcs attribute of the pkg_files target:
kernel_build you have created in Step 1, e.g. :tuna. This adds all outs and module_outs to the distribution directory.kernel_modules_install target you have created in Step 3. You may skip the ddk_modules targets created in Step 2, because the kernel_modules_install target includes all ddk_modules targets. This copies all external kernel modules to the distribution directory.dtb_image and/or dtbo_image for distribution. See Building devicetree.//common:kernel_aarch64//common:kernel_aarch64_additional_artifacts//common:kernel_aarch64_uapi_headers//common:kernel_aarch64_modules.//common:kernel_aarch64/zram.ko//common:kernel_aarch64/zsmalloc.kozram.ko in kernel_build.module_outs) does not need to be specified, because module_outs are added to distribution.Then, add the pkg_files target to the srcs attribute of the pkg_install target.
Example:
load("@rules_pkg//pkg:install.bzl", "pkg_install")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
pkg_files(
name = "tuna_files",
srcs = [
":tuna",
":tuna_images",
":tuna_modules_install",
"//common:kernel_aarch64_uapi_headers",
"//common:kernel_aarch64",
"//common:kernel_aarch64_modules",
"//common:kernel_aarch64_additional_artifacts",
],
strip_prefix = strip_prefix.files_only(),
visibility = ["//visibility:private"],
)
pkg_install(
name = "tuna_dist",
srcs = [":tuna_files"],
destdir = "out/tuna/dist",
visibility = ["//visibility:private"],
)
See rules_pkg for details on how to use pkg_files, strip_prefix, and pkg_install properly.
# Optional: prepare the device by flashing a base build. # During development, you may want to wipe, disable verity and disable verification. # fastboot update tuna-img.zip -w --disable-verity --disable-verification # Assuming dist_dir=out/dist $ tools/bazel run //private/path/to/sources:tuna_dist # Flash static partitions $ fastboot flash boot out/dist/boot.img $ fastboot flash system_dlkm out/dist/system_dlkm.img $ fastboot flash vendor_boot out/dist/vendor_boot.img $ fastboot flash dtbo out/dist/dtbo.img $ fastboot reboot fastboot # Flash dynamic partitions $ fastboot flash vendor_dlkm out/dist/vendor_dlkm.img $ fastboot reboot
See errors.md.
See scmversion.md.
LTO is already disabled by default in gki_defconfig since 5.19.
select()See official Bazel documentation for select() here: https://docs.bazel.build/versions/main/configurable-attributes.html
In general, inputs to a target are configurable, while declared outputs are not. One exception is that the kernel_build rule provides limited support of select() in outs and module_outs attributes. See documentations of kernel_build for details.
By default, the .bazelrc (symlink to build/kernel/kleaf/common.bazelrc) tries to import the following two files if they exist:
device.bazelrc: Device-specific bazelrc file (e.g. GKI prebuilt settings)user.bazelrc: User-specific bazelrc file (e.g. LTO settings)To add device-specific configurations, you may create a device.bazelrc file in the device kernel tree, then create a symlink at the repo root.
Bazel builds are hermetic by default. Hermeticity is ensured by manually declaring each target to depend on //build/kernel:hermetic-tools.
At this time of writing (2025-01-03), the following binaries are still expected from the environement, or host machine, to build the kernel with Bazel, in addition to the list of the allowlist of host tools specified in //build/kernel:hermetic-tools. This is because the following usage does not depend on //build/kernel:hermetic-tools.
echo, readlink, git used by build/kernel/kleaf/workspace_status.shIf you use the following rules, there are known issues of non-hermeticity:
copy_file uses cp from the host machine