build: Export UAPI headers as Bazel cc_library

There's currently no way for userspace programs building with
Bazel to ingest the kernel UAPI headers. Kleaf generates a tarball
of the headers, but a Bazel cc_binary rule cannot use that as an
input.

Define a native Bazel header-only cc_library which userspace
programs can use to build with the kernel's UAPI headers. For
example:

cc_binary(
    name = "foo",
    srcs = ["foo.c"],
    deps = ["//msm-kernel:kalama_gki_uapi_header_library"],
)

Change-Id: Ib884d9123e4ffd0bead97e8cebb5a3456e73b2a6
Signed-off-by: John Moon <quic_johmoo@quicinc.com>
This commit is contained in:
John Moon 2022-11-09 08:50:52 -08:00
parent 8503690aa2
commit 13a5aa4fac
4 changed files with 56 additions and 0 deletions

View File

@ -23,9 +23,11 @@ load(":msm_common.bzl", "define_top_level_config", "gen_config_without_source_li
load(":msm_dtc.bzl", "define_dtc_dist")
load(":msm_abl.bzl", "define_abl_dist")
load(":super_image.bzl", "super_image")
load(":uapi_library.bzl", "define_uapi_library")
load(":image_opts.bzl", "boot_image_opts")
load(":target_variants.bzl", "la_variants")
def _define_build_config(
msm_target,
target,
@ -380,4 +382,6 @@ def define_msm_la(
define_dtc_dist(target)
define_uapi_library(target)
define_extras(target)

View File

@ -22,6 +22,7 @@ load(
load(":msm_common.bzl", "define_top_level_config", "gen_config_without_source_lines")
load(":msm_dtc.bzl", "define_dtc_dist")
load(":image_opts.bzl", "vm_image_opts")
load(":uapi_library.bzl", "define_uapi_library")
load(":target_variants.bzl", "vm_variants")
def _define_build_config(
@ -255,4 +256,6 @@ def define_msm_vm(
define_dtc_dist(target)
define_uapi_library(target)
define_extras(target, flavor = "vm")

18
uapi_library.bzl Normal file
View File

@ -0,0 +1,18 @@
load(":uapi_unpacker.bzl", "uapi_unpacker")
def define_uapi_library(target):
"""Create a header-only cc_library of the kernel's UAPI headers
Args:
target: name of main Bazel target (e.g. `kalama_gki`)
"""
uapi_unpacker(
name = "{}_uapi_unpacker".format(target),
kernel_uapi_headers = ":{}_uapi_headers".format(target),
)
native.cc_library(
name = "{}_uapi_header_library".format(target),
hdrs = [":{}_uapi_unpacker".format(target)],
includes = ["{}_uapi_unpacker_uapi_headers".format(target)],
)

31
uapi_unpacker.bzl Normal file
View File

@ -0,0 +1,31 @@
def _uapi_unpacker_impl(ctx):
input_tar = ctx.file.kernel_uapi_headers
out_dir = ctx.actions.declare_directory(ctx.label.name + "_uapi_headers")
ctx.actions.run_shell(
outputs = [out_dir],
inputs = [input_tar],
arguments = [input_tar.path, out_dir.path],
progress_message = "Unpacking UAPI headers",
command = """
tar_file="${PWD}/$1"
out_dir="${PWD}/$2"
mkdir -p "$out_dir"
cd "$out_dir"
tar --strip-components=2 -xzf "$tar_file"
""",
)
return [DefaultInfo(files = depset([out_dir]))]
uapi_unpacker = rule(
implementation = _uapi_unpacker_impl,
doc = """Unpack `kernel-uapi-headers.tar.gz`""",
attrs = {
"kernel_uapi_headers": attr.label(
allow_single_file = True,
mandatory = True,
doc = "the kernel_uapi_headers tarball or label",
),
},
)