File: uf2_aspect.bzl

package info (click to toggle)
pico-sdk 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,552 kB
  • sloc: ansic: 146,841; asm: 13,423; python: 2,417; cpp: 2,171; yacc: 381; lex: 270; makefile: 32; sh: 13; javascript: 13
file content (67 lines) | stat: -rw-r--r-- 2,350 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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._picotool],
        executable = ctx.executable._picotool,
        arguments = [
            "uf2",
            "convert",
            "--quiet",
            "-t",
            "elf",
            binary_to_convert.path,
            uf2_output.path,
        ],
    )
    return [
        OutputGroupInfo(
            pico_uf2_files = depset([uf2_output]),
        ),
    ]

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",
        ),
        "_picotool": attr.label(default = "@picotool//:picotool", executable = True, cfg = "exec"),
    },
)