Introduce initial Bazel build (#1705)

* Build boot_stage2 with Bazel

Introduces the initial foundations of a Bazel build, including a
toolchain, critical generated headers, platform patterns, and enough
BUILD files to build boot_stage2.

* Bazel libraries to support picotool

* Move SDK defines to toolchain

* Switch to `archive_override` in MODULE.bazel

Uses archive_override where applicable to allow transitive bzlmod deps
to propagate.

* Multiplatform objcopy selection in Bazel build

Makes an objcopy alias that redirects to the objcopy tool for the
current exec platform, which allows boot_stage2 to build on Linux,
macOS, and Windows.

* Generate Bazel build files

Adds initial set of generated Bazel build files. Note that these do not
yet build, as dependency cycles are present.

* Fix dependency cycles in Bazel build

Fixes many dependency cycles, some were unintentionally created by the
build file generator, others are true dependency cycles that require
manual workarounds.

* Silence warning in pico_stdio Bazel build

Silences a stray warning in the Bazel build.

* Fix wildcard Bazel build

This makes `bazel build //...` succeed, and also prevents the fetching
of toolchains that aren't compatible with the current execution
environment (i.e. Windows computers will no longer try to download macOS
toolchains).

* Get the SDK working

Finishes out the remainder of the work required to successfully compile
a working blinky example.

* Fix UART stdio dependencies in Bazel build

Fixes some dependencies around pico_stdlib so that pico_stdlib links
properly and UART stdio works.

* Add linux support to Bazel build

* Get Bazel deps from registry

Adds external an external registry for resolving Bazel module
dependencies.

* Fix host configuration for picotool

Provides the appropriate defines for host builds to support the picotool
build.

* Remove -ffreestanding from Bazel toolchain

The -ffreestanding toolchain flag is quite strict, so remove it from the
Bazel toolchain.

* Remove unused .bzl file

* Reduce Bazel compiler flags

Cuts out most of the Bazel toolchain flags and only specifies the
bare-minimum set of flags. Also, adds wrapper linker flags for functions
the SDK wraps.

* Get USB serial working

Adds initial TinyUSB support and enough integration to get USB serial
working.

* Remove "Generated build file"

Removes comments that indicates BUILD.bazel files are generated. This
was used during initial bringup to indicate hand-crafted vs
automatically generated BUILD.bazel files.

* Do not build USB libraries unless configured

Prevents USB libraries from being built unless the build is properly
configured to use them.

* Switch to rules_cc toolchains

Moves toolchain configuration to use the new rules in rules_cc.

* Minor cleanup in parse_version.py

Cleans up trailing whitespace and runs the black formatter on
parse_version.py.

* Simplify constraint dimensions in Bazel build

Consolidates the class/chip constraint settings to be a single
constraint_setting with a config_setting that represents the rp2 class.

* Update pin of rules_cc in Bazel build

Includes a necessary fix for the target_compatible_with expression in
the cc_toolchain to work as intended.

* Move toolchains from pico.bzl to BUILD.bazel

Moves toolchain definitions from pico.bzl to BUILD.bazel to make them
easier to find and read.

* Run buildifier on Bazel build files

Fix trivial formatting issues by running buildifier on all BUILD.bazel
files.

* Make objcopy rule

Makes a simple objcopy rule to remove direct references to the ARM
toolchains.

* Fix link flags in Bazel build

Critical flags were not being applied to link steps. This applies -mcpu
and -mthumb to the link steps to make the produced binaries work again.

* Mention missing host build support

* Fix various Bazel library rules

* pico_bit_ops was incomplete.
* pico_double and pico_float were trying to link in the "none"
  implementation.

* Extend Bazel build documentation

Improves documentation and comments across the Bazel build.

* Clean up auxilary tools in Bazel build

Switches genrules to use skylib rules to simplify things. Reworks
version header generation to use the Bazel module version rather than
parsing CMake.

* Update boot_stage2 Bazel build file

Moves `includes` to be enumerated on the correct library.

* Add WORKSPACE version fallback

WORKSPACE Bazel projects don't support querying module version, so add a
fallback of '0.0.1-WORKSPACE' so the build can succeed.

* Fix malloc handling in Bazel build

* Fix Bazel dependency cycle in pico_malloc

* Prevent malloc from being linked into boot_stage2

Prevents Bazel from ever trying to link malloc into the boot_stage2
binary.

* Remove custom bootloader platform

A dedicated boot_stage2 platform introduces a lot of complexity that
needs to be more thought-through.
92 files changed
tree: 8bff34dc4c4dc7a96194cbf151c62890c08a75a0
  1. .github/
  2. bazel/
  3. cmake/
  4. docs/
  5. external/
  6. lib/
  7. src/
  8. test/
  9. tools/
  10. .bazelrc
  11. .bazelversion
  12. .gitignore
  13. .gitmodules
  14. BUILD.bazel
  15. CMakeLists.txt
  16. CONTRIBUTING.md
  17. LICENSE.TXT
  18. MODULE.bazel
  19. pico_sdk_init.cmake
  20. pico_sdk_version.cmake
  21. README.md
README.md

Raspberry Pi Pico SDK

The Raspberry Pi Pico SDK (henceforth the SDK) provides the headers, libraries and build system necessary to write programs for the RP2040-based devices such as the Raspberry Pi Pico in C, C++ or assembly language.

The SDK is designed to provide an API and programming environment that is familiar both to non-embedded C developers and embedded C developers alike. A single program runs on the device at a time and starts with a conventional main() method. Standard C/C++ libraries are supported along with C level libraries/APIs for accessing all of the RP2040's hardware include PIO (Programmable IO).

Additionally the SDK provides higher level libraries for dealing with timers, synchronization, USB (TinyUSB) and multi-core programming along with various utilities.

The SDK can be used to build anything from simple applications, to fully fledged runtime environments such as MicroPython, to low level software such as RP2040's on-chip bootrom itself.

Additional libraries/APIs that are not yet ready for inclusion in the SDK can be found in pico-extras.

Documentation

See Getting Started with the Raspberry Pi Pico for information on how to setup your hardware, IDE/environment and for how to build and debug software for the Raspberry Pi Pico and other RP2040-based devices.

See Connecting to the Internet with Raspberry Pi Pico W to learn more about writing applications for your Raspberry Pi Pico W that connect to the internet.

See Raspberry Pi Pico C/C++ SDK to learn more about programming using the SDK, to explore more advanced features, and for complete PDF-based API documentation.

See Online Raspberry Pi Pico SDK API docs for HTML-based API documentation.

Example code

See pico-examples for example code you can build.

Getting the latest SDK code

The master branch of pico-sdk on GitHub contains the latest stable release of the SDK. If you need or want to test upcoming features, you can try the develop branch instead.

Quick-start your own project

These instructions are extremely terse, and Linux-based only. For detailed steps, instructions for other platforms, and just in general, we recommend you see Raspberry Pi Pico C/C++ SDK

  1. Install CMake (at least version 3.13), and GCC cross compiler

    sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
    
  2. Set up your project to point to use the Raspberry Pi Pico SDK

    • Either by cloning the SDK locally (most common) :

      1. git clone this Raspberry Pi Pico SDK repository

      2. Copy pico_sdk_import.cmake from the SDK into your project directory

      3. Set PICO_SDK_PATH to the SDK location in your environment, or pass it (-DPICO_SDK_PATH=) to cmake later.

      4. Setup a CMakeLists.txt like:

        cmake_minimum_required(VERSION 3.13...3.27)
        
        # initialize the SDK based on PICO_SDK_PATH
        # note: this must happen before project()
        include(pico_sdk_import.cmake)
        
        project(my_project)
        
        # initialize the Raspberry Pi Pico SDK
        pico_sdk_init()
        
        # rest of your project
        
        
    • Or with the Raspberry Pi Pico SDK as a submodule :

      1. Clone the SDK as a submodule called pico-sdk

      2. Setup a CMakeLists.txt like:

        cmake_minimum_required(VERSION 3.13...3.27)
        
        # initialize pico-sdk from submodule
        # note: this must happen before project()
        include(pico-sdk/pico_sdk_init.cmake)
        
        project(my_project)
        
        # initialize the Raspberry Pi Pico SDK
        pico_sdk_init()
        
        # rest of your project
        
        
    • Or with automatic download from GitHub :

      1. Copy pico_sdk_import.cmake from the SDK into your project directory

      2. Setup a CMakeLists.txt like:

        cmake_minimum_required(VERSION 3.13)
        
        # initialize pico-sdk from GIT
        # (note this can come from environment, CMake cache etc)
        set(PICO_SDK_FETCH_FROM_GIT on)
        
        # pico_sdk_import.cmake is a single file copied from this SDK
        # note: this must happen before project()
        include(pico_sdk_import.cmake)
        
        project(my_project)
        
        # initialize the Raspberry Pi Pico SDK
        pico_sdk_init()
        
        # rest of your project
        
        
    • Or by cloning the SDK locally, but without copying pico_sdk_import.cmake:

      1. git clone this Raspberry Pi Pico SDK repository

      2. Setup a CMakeLists.txt like:

        cmake_minimum_required(VERSION 3.13)
        
        # initialize the SDK directly
        include(/path/to/pico-sdk/pico_sdk_init.cmake)
        
        project(my_project)
        
        # initialize the Raspberry Pi Pico SDK
        pico_sdk_init()
        
        # rest of your project
        
        
  3. Write your code (see pico-examples or the Raspberry Pi Pico C/C++ SDK documentation for more information)

    About the simplest you can do is a single source file (e.g. hello_world.c)

    #include <stdio.h>
    #include "pico/stdlib.h"
    
    int main() {
        setup_default_uart();
        printf("Hello, world!\n");
        return 0;
    }
    

    And add the following to your CMakeLists.txt:

    add_executable(hello_world
        hello_world.c
    )
    
    # Add pico_stdlib library which aggregates commonly used features
    target_link_libraries(hello_world pico_stdlib)
    
    # create map/bin/hex/uf2 file in addition to ELF.
    pico_add_extra_outputs(hello_world)
    

    Note this example uses the default UART for stdout; if you want to use the default USB see the hello-usb example.

  4. Setup a CMake build directory. For example, if not using an IDE:

    $ mkdir build
    $ cd build
    $ cmake ..
    

    When building for a board other than the Raspberry Pi Pico, you should pass -DPICO_BOARD=board_name to the cmake command above, e.g. cmake -DPICO_BOARD=pico_w .. to configure the SDK and build options accordingly for that particular board.

    Doing so sets up various compiler defines (e.g. default pin numbers for UART and other hardware) and in certain cases also enables the use of additional libraries (e.g. wireless support when building for PICO_BOARD=pico_w) which cannot be built without a board which provides the requisite functionality.

    For a list of boards defined in the SDK itself, look in this directory which has a header for each named board.

  5. Make your target from the build directory you created.

    $ make hello_world
    
  6. You now have hello_world.elf to load via a debugger, or hello_world.uf2 that can be installed and run on your Raspberry Pi Pico via drag and drop.