pico-sdk/bazel/toolchain/BUILD.bazel
armandomontanez 6ff3e4fab2
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
2024-06-13 09:50:04 -05:00

197 lines
6.4 KiB
Python

load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list")
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
load("configurable_feature.bzl", "configurable_toolchain_feature")
package(default_visibility = ["//visibility:public"])
cc_args(
name = "cortex-m0",
actions = [
"@rules_cc//cc/toolchains/actions:compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = [
"-mcpu=cortex-m0plus",
"-mthumb",
],
)
cc_args(
name = "bazel_no_absolute_paths",
actions = ["@rules_cc//cc/toolchains/actions:compile_actions"],
args = [
"-fno-canonical-system-headers",
"-no-canonical-prefixes",
],
)
cc_args_list(
name = "all_unconditional_args",
args = [
":cortex-m0",
":bazel_no_absolute_paths",
],
)
cc_args(
name = "opt_debug_args",
actions = [
"@rules_cc//cc/toolchains/actions:compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = [
"-Og", # TODO: Make this configurable.
"-g3",
],
)
configurable_toolchain_feature(
name = "gc_sections",
copts = [
"-ffunction-sections",
"-fdata-sections",
],
disable_if = "//bazel/constraint:pico_no_gc_sections_enabled",
linkopts = ["-Wl,--gc-sections"],
)
configurable_toolchain_feature(
name = "cxx_no_exceptions",
cxxopts = [
"-fno-exceptions",
"-fno-unwind-tables",
],
disable_if = "//bazel/constraint:pico_cxx_enable_exceptions_enabled",
)
configurable_toolchain_feature(
name = "cxx_no_rtti",
cxxopts = ["-fno-rtti"],
disable_if = "//bazel/constraint:pico_cxx_enable_rtti_enabled",
)
configurable_toolchain_feature(
name = "cxx_no_cxa_atexit",
cxxopts = ["-fno-use-cxa-atexit"],
disable_if = "//bazel/constraint:pico_cxx_enable_cxa_atexit_enabled",
)
configurable_toolchain_feature(
name = "override_max_page_size",
disable_if = "//bazel/constraint:pico_use_default_max_page_size_enabled",
linkopts = ["-Wl,-z,max-page-size=4096"],
)
# TODO: Make this shim unnecessary.
cc_args_list(
name = "all_opt_debug_args",
args = [":opt_debug_args"],
)
cc_feature(
name = "override_debug",
args = [":all_opt_debug_args"],
enabled = True,
overrides = "@rules_cc//cc/toolchains/features:dbg",
)
# TODO: https://github.com/bazelbuild/rules_cc/issues/224 - This is required for
# now, but hopefully will eventually go away.
cc_feature(
name = "legacy_features",
args = [],
enabled = True,
feature_name = "force_legacy_features",
implies = [
"@rules_cc//cc/toolchains/features/legacy:archiver_flags",
"@rules_cc//cc/toolchains/features/legacy:build_interface_libraries",
"@rules_cc//cc/toolchains/features/legacy:dynamic_library_linker_tool",
"@rules_cc//cc/toolchains/features/legacy:strip_debug_symbols",
"@rules_cc//cc/toolchains/features/legacy:linkstamps",
"@rules_cc//cc/toolchains/features/legacy:output_execpath_flags",
"@rules_cc//cc/toolchains/features/legacy:runtime_library_search_directories",
"@rules_cc//cc/toolchains/features/legacy:library_search_directories",
"@rules_cc//cc/toolchains/features/legacy:libraries_to_link",
"@rules_cc//cc/toolchains/features/legacy:force_pic_flags",
"@rules_cc//cc/toolchains/features/legacy:user_link_flags",
"@rules_cc//cc/toolchains/features/legacy:legacy_link_flags",
"@rules_cc//cc/toolchains/features/legacy:linker_param_file",
"@rules_cc//cc/toolchains/features/legacy:fission_support",
"@rules_cc//cc/toolchains/features/legacy:sysroot",
],
)
HOSTS = (
("linux", "x86_64"),
("win", "x86_64"),
("mac", "x86_64"),
("mac", "aarch64"),
)
_HOST_OS_CONSTRAINTS = {
"linux": "@platforms//os:linux",
"win": "@platforms//os:windows",
"mac": "@platforms//os:macos",
}
_HOST_CPU_CONSTRAINTS = {
"x86_64": "@platforms//cpu:x86_64",
"aarch64": "@platforms//cpu:aarch64",
}
[cc_toolchain(
name = "arm_gcc_{}-{}_toolchain_cortex-m".format(host_os, host_cpu),
action_type_configs = [
"@arm_gcc_{}-{}//:arm-none-eabi-ar".format(host_os, host_cpu),
"@arm_gcc_{}-{}//:arm-none-eabi-gcc".format(host_os, host_cpu),
"@arm_gcc_{}-{}//:arm-none-eabi-g++".format(host_os, host_cpu),
"@arm_gcc_{}-{}//:arm-none-eabi-ld".format(host_os, host_cpu),
"@arm_gcc_{}-{}//:arm-none-eabi-objcopy".format(host_os, host_cpu),
"@arm_gcc_{}-{}//:arm-none-eabi-strip".format(host_os, host_cpu),
],
args = ["@pico-sdk//bazel/toolchain:all_unconditional_args"],
compiler = "gcc", # Useful for distinguishing gcc vs clang.
cxx_builtin_include_directories = [
"%sysroot%/arm-none-eabi/include/newlib-nano",
"%sysroot%/arm-none-eabi/include/c++/13.2.1",
"%sysroot%/arm-none-eabi/include/c++/13.2.1/arm-none-eabi",
"%sysroot%/arm-none-eabi/include/c++/13.2.1/backward",
"%sysroot%/lib/gcc/arm-none-eabi/13.2.1/include",
"%sysroot%/lib/gcc/arm-none-eabi/13.2.1/include-fixed",
"%sysroot%/arm-none-eabi/include",
],
exec_compatible_with = [
_HOST_CPU_CONSTRAINTS[host_cpu],
_HOST_OS_CONSTRAINTS[host_os],
],
sysroot = "external/arm_gcc_{}-{}".format(host_os, host_cpu),
tags = ["manual"], # Don't try to build this in wildcard builds.
target_compatible_with = [
"@pico-sdk//bazel/constraint:rp2040",
],
toolchain_features = [
"@pico-sdk//bazel/toolchain:legacy_features",
"@pico-sdk//bazel/toolchain:override_debug",
"@pico-sdk//bazel/toolchain:gc_sections",
"@pico-sdk//bazel/toolchain:cxx_no_exceptions",
"@pico-sdk//bazel/toolchain:cxx_no_rtti",
"@pico-sdk//bazel/toolchain:cxx_no_cxa_atexit",
"@pico-sdk//bazel/toolchain:override_max_page_size",
],
) for host_os, host_cpu in HOSTS]
[toolchain(
name = "arm_gcc_{}-{}".format(host_os, host_cpu),
exec_compatible_with = [
_HOST_CPU_CONSTRAINTS[host_cpu],
_HOST_OS_CONSTRAINTS[host_os],
],
target_compatible_with = [
"@pico-sdk//bazel/constraint:rp2040",
],
toolchain = ":arm_gcc_{}-{}_toolchain_cortex-m".format(host_os, host_cpu),
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
) for host_os, host_cpu in HOSTS]