pico-sdk/bazel/defs.bzl

117 lines
3.7 KiB
Python
Raw Normal View History

Expand bazel build to include configuration options and broader support. (#1731) * Add host Bazel build Updates target_compatible_with across the repo to ensure that wildcard builds for both host and rp2040 succeed. * Get unit tests building * Add Python script to identify build system differences Uses the build system tags to make it easier to identify differences between the CMake and Bazel builds. * Temporarily disable pico divider test * Support PICO_BARE_METAL in Bazel * Support PICO_NO_GC_SECTIONS in Bazel * Support boot2 configuration in Bazel Adds support for PICO_DEFAULT_BOOT_STAGE2 and PICO_DEFAULT_BOOT_STAGE2_FILE in the Bazel build. * Allowlist some CMake-only options * Support CXX configuration options in Bazel * Move multiple_choice_flag.bzl * Support all pico boards * Support linking multiple stdio implementations Changes the Bazel build so stdio implementations are no longer mutually exclusive. * Add PICO_BOOT_STAGE2_LINK_IMAGE * Support PICO_CMSIS_PATH in Bazel * Support PICO_USE_DEFAULT_MAX_PAGE_SIZE in Bazel * Silence PICO_CMSIS_VENDOR and PICO_CMSIS_DEVICE differences * Support PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS in Bazel * Properly support version defines * Support embedding binary info in Bazel * Embed build type in binary * Support different linker scripts in Bazel build * Finish out missing PICO_BUILD_DEFINE in Bazel build * Support PICO_NO_TARGET_NAME * Reorganize initial configuration options in Bazel Cleans up and reorganizes some of the initial configuration options added to the Bazel build so everything is consistent. * Add builds for pioasm and elf2uf2 * Use Python rules from rules_python * Actually link in output formats in pioasm tool * Make tools have public visibility * Add UF2 Bazel aspect * Add TODOs for pioasm/uf2 helpers * Fix compile flag typo * Update Bazel SDK configuration strings to match recent CMake changes * Fix pico_divider test * Clean up straggling TODOs * Clarify pico_stdio_test compatibility * Initial Bazel Pico W support * Add new files from develop * Clean up compatibility expressions in Bazel build * Clean up rp2 constraint handling in Bazel * More Bazel docs cleanup * Format Bazel build files * Consolidate transitions in the Pico SDK * Make every _allowlist_function_transition explicit * More docs cleanup * Add a few missing defines * Improve PICO_CONFIG_HEADER correctness in Bazel * Minor docs clarifications
2024-06-13 22:50:04 +08:00
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_cc//cc:defs.bzl", "cc_library")
def _pico_generate_pio_header_impl(ctx):
generated_headers = []
for f in ctx.files.srcs:
out = ctx.actions.declare_file(
"{}_pio_generated/{}.h".format(ctx.label.name, f.basename),
)
generated_headers.append(out)
ctx.actions.run(
executable = ctx.executable._pioasm_tool,
arguments = [
"-o",
"c-sdk",
f.path,
out.path,
],
inputs = [f],
outputs = [out],
)
cc_ctx = cc_common.create_compilation_context(
headers = depset(direct = generated_headers),
includes = depset(direct = [generated_headers[0].dirname]),
)
return [
DefaultInfo(files = depset(direct = generated_headers)),
CcInfo(compilation_context = cc_ctx),
]
pico_generate_pio_header = rule(
implementation = _pico_generate_pio_header_impl,
doc = """Generates a .h header file for each listed pio source.
Each source file listed in `srcs` will be available as `[pio file name].h` on
the include path if you depend on this rule from a `cc_library`.
pico_generate_pio_header(
name = "my_fun_pio",
srcs = ["my_fun_pio.pio"],
)
# This library can #include "my_fun_pio.pio.h".
cc_library(
name = "libfoo",
deps = [":my_fun_pio"],
srcs = ["libfoo.c"],
)
""",
attrs = {
"srcs": attr.label_list(mandatory = True, allow_files = True),
"_pioasm_tool": attr.label(
default = "@pico-sdk//tools/pioasm:pioasm",
cfg = "exec",
executable = True,
),
},
provides = [CcInfo],
)
# Because the syntax for target_compatible_with when used with config_setting
# rules is both confusing and verbose, provide some helpers that make it much
# easier and clearer to express compatibility.
#
# Context: https://github.com/bazelbuild/bazel/issues/12614
def compatible_with_config(config_label):
"""Expresses compatibility with a config_setting."""
return select({
config_label: [],
"//conditions:default": ["@platforms//:incompatible"],
})
def incompatible_with_config(config_label):
"""Expresses incompatibility with a config_setting."""
return select({
config_label: ["@platforms//:incompatible"],
"//conditions:default": [],
})
def compatible_with_rp2():
"""Expresses a rule is compatible with the rp2 family."""
return incompatible_with_config("//bazel/constraint:host")
def compatible_with_pico_w():
"""Expresses a rule is compatible a Pico W."""
return select({
"@pico-sdk//bazel/constraint:cyw43_wireless": [],
"@pico-sdk//bazel/constraint:is_pico_w": [],
"//conditions:default": ["@platforms//:incompatible"],
})
def pico_board_config(name, platform_includes, **kwargs):
"""A helper macro for declaring a Pico board to use with PICO_CONFIG_HEADER.
This generates pico_config_platform_headers.h using the list of
includes provided in `platform_includes`, and the final artifact is
a cc_library that you can configure //bazel/config:PICO_CONFIG_HEADER to
point to.
"""
_hdr_dir = "{}_generated_includes".format(name)
_hdr_path = "{}/pico_config_platform_headers.h".format(_hdr_dir)
write_file(
name = "{}_platform_headers_file".format(name),
out = _hdr_path,
content = ['#include "{}"'.format(inc) for inc in platform_includes],
)
kwargs.setdefault("hdrs", [])
kwargs["hdrs"].append(_hdr_path)
kwargs.setdefault("includes", [])
kwargs["includes"].append(_hdr_dir)
cc_library(
name = name,
**kwargs
)