1# TODO: Default to a list of known compatible rules until the toolchain emits 2# firmware images with a .elf extension. When binaries have a .elf suffix, 3# this can change to ["*"] and another attribute that allows extension-based 4# filtering can be added to more easily support a wider array of file types. 5_SUPPORTED_BINARY_TYPES = ",".join([ 6 "cc_binary", 7 "cc_test", 8]) 9 10def _pico_uf2_aspect_impl(target, ctx): 11 allowed_types = ctx.attr.from_rules.split(",") 12 if ctx.rule.kind not in allowed_types and "*" not in allowed_types: 13 return [] 14 15 binary_to_convert = target[DefaultInfo].files_to_run.executable 16 uf2_output = ctx.actions.declare_file(binary_to_convert.basename + ".uf2") 17 ctx.actions.run( 18 outputs = [uf2_output], 19 inputs = [binary_to_convert], 20 tools = [ctx.executable._picotool], 21 executable = ctx.executable._picotool, 22 arguments = [ 23 "uf2", 24 "convert", 25 "--quiet", 26 "-t", 27 "elf", 28 binary_to_convert.path, 29 uf2_output.path, 30 ], 31 ) 32 return [ 33 OutputGroupInfo( 34 pico_uf2_files = depset([uf2_output]), 35 ), 36 ] 37 38pico_uf2_aspect = aspect( 39 implementation = _pico_uf2_aspect_impl, 40 doc = """An aspect for generating UF2 images from ELF binaries. 41 42Normally with Bazel, a cc_binary or other rule cannot be "extended" to emit 43additional outputs. However, this aspect may be used as a secondary, adjacent 44step that generates UF2 images from all ELF artifacts. 45 46This can be used from a build to produce UF2 files alongside the regular 47outputs: 48 49``` 50bazel build --platforms=@pico-sdk//bazel/platform:rp2040 \\ 51 --aspects @pico-sdk//tools:uf2_aspect.bzl%pico_uf2_aspect \\ 52 --output_groups=+pico_uf2_files \\ 53 //... 54``` 55 56It's also possible to use this aspect within a custom macro (e.g. my_cc_binary) 57to produce UF2 images alongside ELF files. However, with that method UF2 images 58will only be produced when you explicitly use your custom macro. 59""", 60 attrs = { 61 "from_rules": attr.string( 62 default = _SUPPORTED_BINARY_TYPES, 63 doc = "A comma-separated list of rule kinds to apply the UF2 aspect to", 64 ), 65 "_picotool": attr.label(default = "@picotool//:picotool", executable = True, cfg = "exec"), 66 }, 67) 68