Skip to content

Commit 6ff3e4f

Browse files
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
1 parent 0dc17e5 commit 6ff3e4f

File tree

114 files changed

+2917
-453
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+2917
-453
lines changed

MODULE.bazel

+28-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ bazel_dep(name = "bazel_skylib", version = "1.6.1")
88
# module will not ensure that the root Bazel module has that same version of
99
# rules_cc. For that reason, this primarily acts as a FYI. You'll still need
1010
# to explicitly list this dependency in your own project's MODULE.bazel file.
11-
bazel_dep(name = "rules_cc", version = "0.0.10")
11+
bazel_dep(name = "rules_cc", version = "0.0.9")
1212

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

66+
# TODO: Provide btstack as a proper Bazel module.
67+
http_archive(
68+
name = "btstack",
69+
url = "https://github.com/bluekitchen/btstack/archive/72ef1732c954d938091467961e41f4aa9b976b34.zip",
70+
strip_prefix = "btstack-72ef1732c954d938091467961e41f4aa9b976b34",
71+
build_file = "//src/rp2_common/pico_btstack:btstack.BUILD",
72+
sha256 = "f45d72b5d404dd2f8e311287de6f2ba3561fc8ae956737eeb611b277aadc2391",
73+
)
74+
75+
# TODO: Provide btstack as a proper Bazel module.
76+
http_archive(
77+
name = "cyw43-driver",
78+
url = "https://github.com/georgerobotics/cyw43-driver/archive/8ef38a6d32c54f850bff8f189bdca19ded33792a.zip",
79+
strip_prefix = "cyw43-driver-8ef38a6d32c54f850bff8f189bdca19ded33792a",
80+
build_file = "//src/rp2_common/pico_cyw43_driver:cyw43-driver.BUILD",
81+
sha256 = "0b44a19ea58537ee954357606cde5ed20c3a42be77adfebb07b7c0e4740f6228",
82+
)
83+
84+
# TODO: Provide lwip as a proper Bazel module.
85+
http_archive(
86+
name = "lwip",
87+
url = "https://github.com/lwip-tcpip/lwip/archive/239918ccc173cb2c2a62f41a40fd893f57faf1d6.zip",
88+
strip_prefix = "lwip-239918ccc173cb2c2a62f41a40fd893f57faf1d6",
89+
build_file = "//src/rp2_common/pico_lwip:lwip.BUILD",
90+
sha256 = "7ee9e02f2719c0422377e1fcce5a21716ca2e2e855cca56695f9ef7cb020e5dd",
91+
)
92+
6693
register_toolchains(
6794
"//bazel/toolchain:arm_gcc_linux-x86_64",
6895
"//bazel/toolchain:arm_gcc_win-x86_64",

bazel/BUILD.bazel

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
2+
13
package(default_visibility = ["//visibility:public"])
24

35
py_binary(
@@ -18,17 +20,38 @@ py_binary(
1820
# configuring these `label_flag`s:
1921
#
2022
# # Specify the library that provides "pico_config_extra_headers.h"
21-
# --@pico-sdk//bazel/config:pico_config_extra_headers=//my_proj:my_custom_headers
23+
# --@pico-sdk//bazel/config:PICO_CONFIG_EXTRA_HEADER=//my_proj:my_custom_headers
2224
#
2325
# # Specify the library that provides "pico_config_platform_headers.h"
24-
# --@pico-sdk//bazel/config:pico_config_platform_headers=//my_proj:my_custom_platform_headers
26+
# --@pico-sdk//bazel/config:PICO_CONFIG_PLATFORM_HEADER=//my_proj:my_custom_platform_headers
2527
cc_library(
2628
name = "generate_config_header",
2729
hdrs = ["include/pico/config_autogen.h"],
2830
includes = ["include"],
2931
visibility = ["//:__subpackages__"],
3032
deps = [
31-
"//bazel/config:pico_config_extra_headers",
32-
"//bazel/config:pico_config_platform_headers",
33+
"//bazel/config:PICO_CONFIG_EXTRA_HEADER",
34+
"//bazel/config:PICO_CONFIG_PLATFORM_HEADER",
3335
],
3436
)
37+
38+
genrule(
39+
name = "empty_extra_headers_file",
40+
outs = ["generated_extra_include/pico_config_extra_headers.h"],
41+
cmd = "echo > $@",
42+
cmd_bat = "copy NUL $@",
43+
visibility = ["//visibility:private"],
44+
)
45+
46+
cc_library(
47+
name = "no_extra_headers",
48+
hdrs = ["generated_extra_include/pico_config_extra_headers.h"],
49+
includes = ["generated_extra_include"],
50+
visibility = ["//visibility:private"],
51+
)
52+
53+
# An empty stub, useful for label_flag flags that need to point to a library,
54+
# but for some purposes the library needs to be a no-op.
55+
cc_library(
56+
name = "empty_cc_lib",
57+
)

bazel/README.md

+36-57
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
# Bazel build
2-
The Bazel build for the Pico SDK is currently community-maintained, and should
3-
be considered an experimental work-in-progress. There are missing features,
4-
and you may encounter significant breakages with future versions.
5-
6-
You are welcome and encouraged to file issues for any problems you encounter
7-
along the way.
82

93
## Using the Pico SDK in a Bazel project.
104

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

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

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

7266
### Selecting a different board
73-
Currently there are three configurable flags for targeting a different board:
74-
1. `pico_config_extra_headers`: This should always point to a `cc_library `that
75-
provides a `"pico_config_extra_headers.h"` header. You can configure this
76-
by including a flag like the following in your build invocation:
77-
```
78-
--@pico-sdk//bazel/config:pico_config_extra_headers=//path/to:custom_extra_headers
79-
```
80-
2. `pico_config_platform_headers`: This should always point to a `cc_library`
81-
that provides a `"pico_config_platform_headers.h"` header.
82-
```
83-
--@pico-sdk//bazel/config:pico_config_platform_headers=//path/to:custom_platform_headers
84-
```
85-
3. `pico_config_header`: This should point to a `cc_library` that sets all
86-
necessary SDK defines. Most notably, `PICO_BOARD`, `PICO_CONFIG_HEADER`,
87-
`PICO_ON_DEVICE`, `PICO_NO_HARDWARE`, and `PICO_BUILD`. See
88-
`//src/boards:BUILD.bazel` for working examples. Any `defines` set on this
89-
library will propagate to the rest of the Pico SDK. To set this configuration
90-
option, pass a flag like the following in your Bazel build invocation:
91-
```
92-
--@pico-sdk//bazel/config:pico_config_platform_headers=//path/to:pico_board_config
93-
```
94-
95-
### Selecting a stdio mode
96-
To select a different stdio mode, add it to your `platform` definition. For
97-
example:
98-
```python
99-
platform(
100-
name = "rp2040",
101-
constraint_values = [
102-
"@pico-sdk//bazel/constraint:rp2040",
103-
"@pico-sdk//bazel/constraint:stdio_usb", # Configures stdio_mode.
104-
"@platforms//cpu:armv6-m",
105-
],
106-
)
67+
A different board can be selected specifying `--@pico-sdk//bazel/config:PICO_BOARD`:
68+
```console
69+
$ bazelisk build --platforms=//bazel/platform:rp2040 --@pico-sdk//bazel/config:PICO_BOARD=pico_w //...
70+
```
71+
72+
If you have a bespoke board definition, you can configure the Pico SDK to use it
73+
by pointing `--@pico-sdk//bazel/config:PICO_CONFIG_HEADER` to a `cc_library`
74+
that defines `PICO_BOARD` and either a `PICO_CONFIG_HEADER` define or a
75+
`pico/config_autogen.h` header. Make sure any required `includes`, `hdrs`, and
76+
`deps` are also provided.
77+
78+
## Generating UF2 firmware images
79+
Creation of UF2 images can be done as explicit build steps on a per-binary
80+
rule basis, or through an aspect. Running a wildcard build with the
81+
`pico_uf2_aspect` enabled is the easiest way to create a UF2 for every ELF
82+
firmware image.
83+
84+
```console
85+
$ bazel build --platforms=@pico-sdk//bazel/platform:rp2040 \
86+
--aspects @pico-sdk//tools:uf2_aspect.bzl%pico_uf2_aspect \
87+
--output_groups=+pico_uf2_files \
88+
//...
10789
```
10890

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

124-
**Note:** Since the Bazel build does not yet have any `cc_binary` rules with a
125-
`main()` function, there won't be any binaries to flash on your board. For now,
126-
this only builds the SDK as a collection of libraries.
127-
128106
## Known issues and limitations
129-
The Bazel build is currently experimental and incomplete. At this time, only the
130-
stock Pi Pico board is supported, and the only configuration options are
131-
changing the STDIO mode between UART and USB serial.
132-
133-
Keep in mind the following limitations:
134-
* Pico-W is not yet supported.
135-
* Selecting an alternative board is not yet supported.
136-
* Nearly all preexisting CMake configuration options are not yet supported.
137-
* Targeting the host build of the Pico SDK is not yet supported.
107+
The Bazel build for the Pico SDK is relatively new, but most features and
108+
configuration options available in the CMake build are also available in Bazel.
109+
You are welcome and encouraged to file issues for any problems and limitations
110+
you encounter along the way.
111+
112+
Currently, the following features are not supported:
113+
114+
* "None" variants of pico_double, pico_float, and pico_printf are not yet
115+
supported.
116+
* The pioasm parser cannot be built from source via Bazel.

0 commit comments

Comments
 (0)