Skip to content

Commit

Permalink
Merge branch 'develop' into rtt-stdio-support
Browse files Browse the repository at this point in the history
  • Loading branch information
kilograham authored Jul 21, 2024
2 parents 801a16c + c93c3f4 commit 336a672
Show file tree
Hide file tree
Showing 277 changed files with 25,033 additions and 16,889 deletions.
6 changes: 6 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Required for new toolchain resolution API.
build --incompatible_enable_cc_toolchain_resolution
build --@rules_cc//cc/toolchains:experimental_enable_rule_based_toolchains

# Produce useful output when the build fails.
common --verbose_failures
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.0.2
3 changes: 1 addition & 2 deletions .github/workflows/macOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ jobs:
- name: Install dependencies
run: |
brew install cmake
brew tap ArmMbed/homebrew-formulae
brew install arm-none-eabi-gcc
brew install --cask gcc-arm-embedded
- name: Build Project
# bash required otherwise this mysteriously (no error) fails at "Generating cyw43_bus_pio_spi.pio.h"
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
cmake-*
.DS_Store
build

bazel-*

# Ignore until https://github.com/bazelbuild/bazel/issues/20369 is fixed.
MODULE.bazel.lock
Empty file added BUILD.bazel
Empty file.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.13...3.27)

# Note: this CMakeLists.txt can be used as a top-level CMakeLists.txt for the SDK itself. For all other uses
# it is included as a subdirectory via the pico_sdk_init() method provided by pico_sdk_init.cmake
Expand Down Expand Up @@ -61,4 +61,3 @@ if (NOT TARGET _pico_sdk_inclusion_marker)
pico_promote_common_scope_vars()
endif()
endif()

98 changes: 98 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module(name = "pico-sdk", version = "1.6.0-rc1")

bazel_dep(name = "platforms", version = "0.0.8")

bazel_dep(name = "bazel_skylib", version = "1.6.1")

# Note: rules_cc is special-cased repository; a dependency on rules_cc in a
# 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.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
# project's MODULE.bazel too.
archive_override(
module_name = "rules_cc",
urls = "https://github.com/bazelbuild/rules_cc/archive/1acf5213b6170f1f0133e273cb85ede0e732048f.zip",
strip_prefix = "rules_cc-1acf5213b6170f1f0133e273cb85ede0e732048f",
integrity = "sha256-NddP6xi6LzsIHT8bMSVJ2NtoURbN+l3xpjvmIgB6aSg=",
)

http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "arm_gcc_linux-x86_64",
url = "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz",
strip_prefix = "arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi",
build_file = "//bazel/toolchain:gcc_arm_none_eabi.BUILD",
sha256 = "6cd1bbc1d9ae57312bcd169ae283153a9572bd6a8e4eeae2fedfbc33b115fdbb",
)

http_archive(
name = "arm_gcc_win-x86_64",
url = "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-mingw-w64-i686-arm-none-eabi.zip",
strip_prefix = "arm-gnu-toolchain-13.2.Rel1-mingw-w64-i686-arm-none-eabi",
build_file = "//bazel/toolchain:gcc_arm_none_eabi.BUILD",
sha256 = "51d933f00578aa28016c5e3c84f94403274ea7915539f8e56c13e2196437d18f",
)

http_archive(
name = "arm_gcc_mac-x86_64",
url = "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-darwin-x86_64-arm-none-eabi.tar.xz",
strip_prefix = "arm-gnu-toolchain-13.2.Rel1-darwin-x86_64-arm-none-eabi",
build_file = "//bazel/toolchain:gcc_arm_none_eabi.BUILD",
sha256 = "075faa4f3e8eb45e59144858202351a28706f54a6ec17eedd88c9fb9412372cc",
)

http_archive(
name = "arm_gcc_mac-aarch64",
url = "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-darwin-arm64-arm-none-eabi.tar.xz",
strip_prefix = "arm-gnu-toolchain-13.2.Rel1-darwin-arm64-arm-none-eabi",
build_file = "//bazel/toolchain:gcc_arm_none_eabi.BUILD",
sha256 = "39c44f8af42695b7b871df42e346c09fee670ea8dfc11f17083e296ea2b0d279",
)

# TODO: Provide tinyusb as a proper Bazel module.
http_archive(
name = "tinyusb",
url = "https://github.com/hathach/tinyusb/archive/86c416d4c0fb38432460b3e11b08b9de76941bf5.zip",
strip_prefix = "tinyusb-86c416d4c0fb38432460b3e11b08b9de76941bf5",
build_file = "//src/rp2_common/tinyusb:tinyusb.BUILD",
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",
"//bazel/toolchain:arm_gcc_mac-x86_64",
"//bazel/toolchain:arm_gcc_mac-aarch64",
)
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ instructions for other platforms, and just in general, we recommend you see [Ras
3. Setup a `CMakeLists.txt` like:

```cmake
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.13...3.27)
# initialize the SDK based on PICO_SDK_PATH
# note: this must happen before project()
Expand All @@ -79,7 +79,7 @@ instructions for other platforms, and just in general, we recommend you see [Ras
1. Setup a `CMakeLists.txt` like:

```cmake
cmake_minimum_required(VERSION 3.13)
cmake_minimum_required(VERSION 3.13...3.27)
# initialize pico-sdk from submodule
# note: this must happen before project()
Expand Down
57 changes: 57 additions & 0 deletions bazel/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
load("@rules_python//python:defs.bzl", "py_binary")

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

py_binary(
name = "generate_version_header",
srcs = ["generate_version_header.py"],
visibility = ["//:__subpackages__"],
)

# This isn't actually generated, it just uses the same name
# to make it show up easier in searches.
#
# Rather than generating lists of headers to match CMake, the Bazel build
# opts to have a static header that transitively include two known headers.
# By default, empty header files are included, and users/platforms are expected
# to specify an appropriate `cc_library` to replace them.
#
# You tell bazel which `cc_library` provides the respective headers by
# configuring these `label_flag`s:
#
# # Specify the library that provides "pico_config_extra_headers.h"
# --@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_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_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",
)
116 changes: 116 additions & 0 deletions bazel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Bazel build

## Using the Pico SDK in a Bazel project.

### Add pico-sdk as a dependency
First, in your `MODULE.bazel` file, add a dependency on the Pico SDK:
```python
bazel_dep(
name = "pico-sdk",
version = "1.6.0-rc1",
)
```
Second, in the same file you'll need to add an explicit dependency on
`rules_cc`, as it's a special-cased Bazel module:
```python
# Note: rules_cc is special-cased repository; a dependency on rules_cc in a
# 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.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
# project's MODULE.bazel too.
archive_override(
module_name = "rules_cc",
urls = "https://github.com/bazelbuild/rules_cc/archive/1acf5213b6170f1f0133e273cb85ede0e732048f.zip",
strip_prefix = "rules_cc-1acf5213b6170f1f0133e273cb85ede0e732048f",
integrity = "sha256-NddP6xi6LzsIHT8bMSVJ2NtoURbN+l3xpjvmIgB6aSg=",
)
```

### Register toolchains
These toolchains tell Bazel how to compile for ARM cores. Add the following
to the `MODULE.bazel` for your project:
```python
register_toolchains(
"@pico-sdk//bazel/toolchain:arm_gcc_linux-x86_64",
"@pico-sdk//bazel/toolchain:arm_gcc_win-x86_64",
"@pico-sdk//bazel/toolchain:arm_gcc_mac-x86_64",
"@pico-sdk//bazel/toolchain:arm_gcc_mac-aarch64",
)
```

### Enable required .bazelrc flags
To use the toolchains provided by the Pico SDK, you'll need to enable a few
new features. In your project's `.bazelrc`, add the following
```
# Required for new toolchain resolution API.
build --incompatible_enable_cc_toolchain_resolution
build --@rules_cc//cc/toolchains:experimental_enable_rule_based_toolchains
```

### Ready to build!
You're now ready to start building Pico Projects in Bazel! When building,
don't forget to specify `--platforms` so Bazel knows you're targeting the
Raspberry Pi Pico:
```console
$ bazelisk build --platforms=@pico-sdk//bazel/platform:rp2040 //...
```

## SDK configuration
An exhaustive list of build system configuration options is available in
`//bazel/config:BUILD.bazel`.

### Selecting a different board
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

### First time setup
You'll need Bazel (v7.0.0 or higher) or Bazelisk (a self-updating Bazel
launcher) to build the Pico SDK.

We strongly recommend you set up
[Bazelisk](https://bazel.build/install/bazelisk).

### Building
To build all of the Pico SDK, run the following command:
```console
$ bazelisk build --platforms=//bazel/platform:rp2040 //...
```

## Known issues and limitations
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 336a672

Please sign in to comment.