pico-sdk/tools/uf2_aspect.bzl
armandomontanez 6ff3e4fab2
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 09:50:04 -05:00

64 lines
2.2 KiB
Python

# TODO: Default to a list of known compatible rules until the toolchain emits
# firmware images with a .elf extension. When binaries have a .elf suffix,
# this can change to ["*"] and another attribute that allows extension-based
# filtering can be added to more easily support a wider array of file types.
_SUPPORTED_BINARY_TYPES = ",".join([
"cc_binary",
"cc_test",
])
def _pico_uf2_aspect_impl(target, ctx):
allowed_types = ctx.attr.from_rules.split(",")
if ctx.rule.kind not in allowed_types and "*" not in allowed_types:
return []
binary_to_convert = target[DefaultInfo].files_to_run.executable
uf2_output = ctx.actions.declare_file(binary_to_convert.basename + ".uf2")
ctx.actions.run(
outputs = [uf2_output],
inputs = [binary_to_convert],
tools = [ctx.executable._elf2uf2_tool],
executable = ctx.executable._elf2uf2_tool,
arguments = [
binary_to_convert.path,
uf2_output.path,
],
)
return [
OutputGroupInfo(
pico_uf2_files = depset([uf2_output]),
),
]
return []
pico_uf2_aspect = aspect(
implementation = _pico_uf2_aspect_impl,
doc = """An aspect for generating UF2 images from ELF binaries.
Normally with Bazel, a cc_binary or other rule cannot be "extended" to emit
additional outputs. However, this aspect may be used as a secondary, adjacent
step that generates UF2 images from all ELF artifacts.
This can be used from a build to produce UF2 files alongside the regular
outputs:
```
bazel build --platforms=@pico-sdk//bazel/platform:rp2040 \\
--aspects @pico-sdk//tools:uf2_aspect.bzl%pico_uf2_aspect \\
--output_groups=+pico_uf2_files \\
//...
```
It's also possible to use this aspect within a custom macro (e.g. my_cc_binary)
to produce UF2 images alongside ELF files. However, with that method UF2 images
will only be produced when you explicitly use your custom macro.
""",
attrs = {
"from_rules": attr.string(
default = _SUPPORTED_BINARY_TYPES,
doc = "A comma-separated list of rule kinds to apply the UF2 aspect to",
),
"_elf2uf2_tool": attr.label(default = "//tools/elf2uf2:elf2uf2", executable = True, cfg = "exec"),
},
)