Skip to content

Commit

Permalink
Expand bazel build to include configuration options and broader suppo…
Browse files Browse the repository at this point in the history
…rt. (raspberrypi#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
  • Loading branch information
armandomontanez authored Jun 13, 2024
1 parent 0dc17e5 commit 6ff3e4f
Show file tree
Hide file tree
Showing 114 changed files with 2,917 additions and 453 deletions.
29 changes: 28 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bazel_dep(name = "bazel_skylib", version = "1.6.1")
# module will not ensure that the root Bazel module has that same version of
# rules_cc. For that reason, this primarily acts as a FYI. You'll still need
# to explicitly list this dependency in your own project's MODULE.bazel file.
bazel_dep(name = "rules_cc", version = "0.0.10")
bazel_dep(name = "rules_cc", version = "0.0.9")

# rules_cc v0.0.10 is not yet cut, so manually pull in the desired version.
# This does not apply to dependent projects, so it needs to be copied to your
Expand Down Expand Up @@ -63,6 +63,33 @@ http_archive(
sha256 = "ac57109bba00d26ffa33312d5f334990ec9a9a4d82bf890ed8b825b4610d1da2",
)

# TODO: Provide btstack as a proper Bazel module.
http_archive(
name = "btstack",
url = "https://github.com/bluekitchen/btstack/archive/72ef1732c954d938091467961e41f4aa9b976b34.zip",
strip_prefix = "btstack-72ef1732c954d938091467961e41f4aa9b976b34",
build_file = "//src/rp2_common/pico_btstack:btstack.BUILD",
sha256 = "f45d72b5d404dd2f8e311287de6f2ba3561fc8ae956737eeb611b277aadc2391",
)

# TODO: Provide btstack as a proper Bazel module.
http_archive(
name = "cyw43-driver",
url = "https://github.com/georgerobotics/cyw43-driver/archive/8ef38a6d32c54f850bff8f189bdca19ded33792a.zip",
strip_prefix = "cyw43-driver-8ef38a6d32c54f850bff8f189bdca19ded33792a",
build_file = "//src/rp2_common/pico_cyw43_driver:cyw43-driver.BUILD",
sha256 = "0b44a19ea58537ee954357606cde5ed20c3a42be77adfebb07b7c0e4740f6228",
)

# TODO: Provide lwip as a proper Bazel module.
http_archive(
name = "lwip",
url = "https://github.com/lwip-tcpip/lwip/archive/239918ccc173cb2c2a62f41a40fd893f57faf1d6.zip",
strip_prefix = "lwip-239918ccc173cb2c2a62f41a40fd893f57faf1d6",
build_file = "//src/rp2_common/pico_lwip:lwip.BUILD",
sha256 = "7ee9e02f2719c0422377e1fcce5a21716ca2e2e855cca56695f9ef7cb020e5dd",
)

register_toolchains(
"//bazel/toolchain:arm_gcc_linux-x86_64",
"//bazel/toolchain:arm_gcc_win-x86_64",
Expand Down
31 changes: 27 additions & 4 deletions bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_python//python:defs.bzl", "py_binary")

package(default_visibility = ["//visibility:public"])

py_binary(
Expand All @@ -18,17 +20,38 @@ py_binary(
# configuring these `label_flag`s:
#
# # Specify the library that provides "pico_config_extra_headers.h"
# --@pico-sdk//bazel/config:pico_config_extra_headers=//my_proj:my_custom_headers
# --@pico-sdk//bazel/config:PICO_CONFIG_EXTRA_HEADER=//my_proj:my_custom_headers
#
# # Specify the library that provides "pico_config_platform_headers.h"
# --@pico-sdk//bazel/config:pico_config_platform_headers=//my_proj:my_custom_platform_headers
# --@pico-sdk//bazel/config:PICO_CONFIG_PLATFORM_HEADER=//my_proj:my_custom_platform_headers
cc_library(
name = "generate_config_header",
hdrs = ["include/pico/config_autogen.h"],
includes = ["include"],
visibility = ["//:__subpackages__"],
deps = [
"//bazel/config:pico_config_extra_headers",
"//bazel/config:pico_config_platform_headers",
"//bazel/config:PICO_CONFIG_EXTRA_HEADER",
"//bazel/config:PICO_CONFIG_PLATFORM_HEADER",
],
)

genrule(
name = "empty_extra_headers_file",
outs = ["generated_extra_include/pico_config_extra_headers.h"],
cmd = "echo > $@",
cmd_bat = "copy NUL $@",
visibility = ["//visibility:private"],
)

cc_library(
name = "no_extra_headers",
hdrs = ["generated_extra_include/pico_config_extra_headers.h"],
includes = ["generated_extra_include"],
visibility = ["//visibility:private"],
)

# An empty stub, useful for label_flag flags that need to point to a library,
# but for some purposes the library needs to be a no-op.
cc_library(
name = "empty_cc_lib",
)
93 changes: 36 additions & 57 deletions bazel/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# Bazel build
The Bazel build for the Pico SDK is currently community-maintained, and should
be considered an experimental work-in-progress. There are missing features,
and you may encounter significant breakages with future versions.

You are welcome and encouraged to file issues for any problems you encounter
along the way.

## Using the Pico SDK in a Bazel project.

Expand All @@ -23,7 +17,7 @@ Second, in the same file you'll need to add an explicit dependency on
# module will not ensure that the root Bazel module has that same version of
# rules_cc. For that reason, this primarily acts as a FYI. You'll still need
# to explicitly list this dependency in your own project's MODULE.bazel file.
bazel_dep(name = "rules_cc", version = "0.0.10")
bazel_dep(name = "rules_cc", version = "0.0.9")

# rules_cc v0.0.10 is not yet cut, so manually pull in the desired version.
# This does not apply to dependent projects, so it needs to be copied to your
Expand Down Expand Up @@ -65,45 +59,33 @@ Raspberry Pi Pico:
$ bazelisk build --platforms=@pico-sdk//bazel/platform:rp2040 //...
```

## SDK configuration [experimental]
These configuration options are a work in progress and may see significant
breaking changes in future versions.
## SDK configuration
An exhaustive list of build system configuration options is available in
`//bazel/config:BUILD.bazel`.

### Selecting a different board
Currently there are three configurable flags for targeting a different board:
1. `pico_config_extra_headers`: This should always point to a `cc_library `that
provides a `"pico_config_extra_headers.h"` header. You can configure this
by including a flag like the following in your build invocation:
```
--@pico-sdk//bazel/config:pico_config_extra_headers=//path/to:custom_extra_headers
```
2. `pico_config_platform_headers`: This should always point to a `cc_library`
that provides a `"pico_config_platform_headers.h"` header.
```
--@pico-sdk//bazel/config:pico_config_platform_headers=//path/to:custom_platform_headers
```
3. `pico_config_header`: This should point to a `cc_library` that sets all
necessary SDK defines. Most notably, `PICO_BOARD`, `PICO_CONFIG_HEADER`,
`PICO_ON_DEVICE`, `PICO_NO_HARDWARE`, and `PICO_BUILD`. See
`//src/boards:BUILD.bazel` for working examples. Any `defines` set on this
library will propagate to the rest of the Pico SDK. To set this configuration
option, pass a flag like the following in your Bazel build invocation:
```
--@pico-sdk//bazel/config:pico_config_platform_headers=//path/to:pico_board_config
```

### Selecting a stdio mode
To select a different stdio mode, add it to your `platform` definition. For
example:
```python
platform(
name = "rp2040",
constraint_values = [
"@pico-sdk//bazel/constraint:rp2040",
"@pico-sdk//bazel/constraint:stdio_usb", # Configures stdio_mode.
"@platforms//cpu:armv6-m",
],
)
A different board can be selected specifying `--@pico-sdk//bazel/config:PICO_BOARD`:
```console
$ bazelisk build --platforms=//bazel/platform:rp2040 --@pico-sdk//bazel/config:PICO_BOARD=pico_w //...
```

If you have a bespoke board definition, you can configure the Pico SDK to use it
by pointing `--@pico-sdk//bazel/config:PICO_CONFIG_HEADER` to a `cc_library`
that defines `PICO_BOARD` and either a `PICO_CONFIG_HEADER` define or a
`pico/config_autogen.h` header. Make sure any required `includes`, `hdrs`, and
`deps` are also provided.

## Generating UF2 firmware images
Creation of UF2 images can be done as explicit build steps on a per-binary
rule basis, or through an aspect. Running a wildcard build with the
`pico_uf2_aspect` enabled is the easiest way to create a UF2 for every ELF
firmware image.

```console
$ bazel build --platforms=@pico-sdk//bazel/platform:rp2040 \
--aspects @pico-sdk//tools:uf2_aspect.bzl%pico_uf2_aspect \
--output_groups=+pico_uf2_files \
//...
```

## Building the Pico SDK itself
Expand All @@ -121,17 +103,14 @@ To build all of the Pico SDK, run the following command:
$ bazelisk build --platforms=//bazel/platform:rp2040 //...
```

**Note:** Since the Bazel build does not yet have any `cc_binary` rules with a
`main()` function, there won't be any binaries to flash on your board. For now,
this only builds the SDK as a collection of libraries.

## Known issues and limitations
The Bazel build is currently experimental and incomplete. At this time, only the
stock Pi Pico board is supported, and the only configuration options are
changing the STDIO mode between UART and USB serial.

Keep in mind the following limitations:
* Pico-W is not yet supported.
* Selecting an alternative board is not yet supported.
* Nearly all preexisting CMake configuration options are not yet supported.
* Targeting the host build of the Pico SDK is not yet supported.
The Bazel build for the Pico SDK is relatively new, but most features and
configuration options available in the CMake build are also available in Bazel.
You are welcome and encouraged to file issues for any problems and limitations
you encounter along the way.

Currently, the following features are not supported:

* "None" variants of pico_double, pico_float, and pico_printf are not yet
supported.
* The pioasm parser cannot be built from source via Bazel.
Loading

0 comments on commit 6ff3e4f

Please sign in to comment.