Skip to content

Commit

Permalink
[Bazel] Update rules_cc to v0.0.10 (#1989)
Browse files Browse the repository at this point in the history
Updates rules_cc to v0.0.10 to reduce the steps required to get started
with creating a Bazel-based Pi Pico project.
  • Loading branch information
armandomontanez authored Nov 5, 2024
1 parent d649c6c commit e48a2e1
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 302 deletions.
4 changes: 0 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Required for new toolchain resolution API.
build --incompatible_enable_cc_toolchain_resolution
build --@rules_cc//cc/toolchains:experimental_enable_rule_based_toolchains

# Silence all C/C++ warnings in external code.
common --per_file_copt=external/.*@-w
common --host_per_file_copt=external/.*@-w
Expand Down
17 changes: 1 addition & 16 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,7 @@ bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "rules_python", version = "0.22.1")
bazel_dep(name = "picotool", version = "2.0.0")

# 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",
integrity = "sha256-zdQo/pQWKdIAPKSflBxOSWZNwCbc86T7SechKZo/3Xw=",
strip_prefix = "rules_cc-1acf5213b6170f1f0133e273cb85ede0e732048f",
urls = "https://github.com/bazelbuild/rules_cc/archive/1acf5213b6170f1f0133e273cb85ede0e732048f.tar.gz",
)
bazel_dep(name = "rules_cc", version = "0.0.10")

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

Expand Down
37 changes: 3 additions & 34 deletions bazel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,10 @@
## 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:
First, in your `MODULE.bazel` file, add a dependency on the Pico SDK and
`rules_cc`:
```python
bazel_dep(
name = "pico-sdk",
version = "2.0.0",
)
```

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=",
)
bazel_dep(name = "pico-sdk", version = "2.0.1")
```

### Register toolchains
Expand All @@ -47,15 +25,6 @@ register_toolchains(
)
```

### 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
Expand Down
146 changes: 64 additions & 82 deletions bazel/toolchain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,39 @@ cc_args(
],
)

# :no_canonical_system_headers and :no_canonical_prefixes both prevent built-in
# compiler include directories from resolving to absolute paths. Prefer to use
# :bazel_no_absolute_paths, since it correctly guides based on the current
# compiler type.
cc_args(
name = "no-canonical-system-headers",
name = "no_canonical_system_headers",
actions = ["@rules_cc//cc/toolchains/actions:compile_actions"],
args = ["-fno-canonical-system-headers"],
)

cc_args(
name = "no-canonical-prefixes",
name = "no_canonical_prefixes",
actions = ["@rules_cc//cc/toolchains/actions:compile_actions"],
args = ["-no-canonical-prefixes"],
)

cc_args(
name = "nostdlibxx",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
args = ["-nostdlib++"],
)

cc_args(
name = "nostartfiles",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
args = ["-nostartfiles"],
)

cc_args_list(
name = "bazel_no_absolute_paths",
args = select({
"//bazel/constraint:pico_toolchain_clang_enabled": [],
"//conditions:default": [":no-canonical-system-headers"],
}) + [":no-canonical-prefixes"],
"//conditions:default": [":no_canonical_system_headers"],
}) + [":no_canonical_prefixes"],
)

cc_args(
name = "llvm-libc_args",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
args = [
"-nostdlib++",
"-nostartfiles",
"-Wl,-lc++",
],
visibility = ["//visibility:private"],
)

cc_args(
Expand All @@ -101,7 +104,6 @@ configurable_toolchain_feature(
"-ffunction-sections",
"-fdata-sections",
],
disable_if = "//bazel/constraint:pico_no_gc_sections_enabled",
linkopts = ["-Wl,--gc-sections"],
)

Expand All @@ -111,24 +113,20 @@ configurable_toolchain_feature(
"-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"],
)

Expand All @@ -141,36 +139,9 @@ cc_args_list(
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"),
("linux", "aarch64"),
Expand All @@ -192,58 +163,52 @@ _HOST_CPU_CONSTRAINTS = {

[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),
],
tool_map = "@arm_gcc_{}-{}//:all_tools".format(host_os, host_cpu),
args = select({
"//bazel/constraint:rp2040": [":cortex-m0"],
"//bazel/constraint:rp2350": [":cortex-m33"],
"//conditions:default": [],
}) + [
":bazel_no_absolute_paths",
],
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.
toolchain_features = [
"@pico-sdk//bazel/toolchain:legacy_features",
known_features = [
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_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",
],
enabled_features = [
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
"@pico-sdk//bazel/toolchain:override_debug",
] + select({
"//bazel/constraint:pico_no_gc_sections_enabled": [],
"//conditions:default": [":gc_sections"],
}) + select({
"//bazel/constraint:pico_cxx_enable_exceptions_enabled": [],
"//conditions:default": [":cxx_no_exceptions"],
}) + select({
"//bazel/constraint:pico_cxx_enable_rtti_enabled": [],
"//conditions:default": [":cxx_no_rtti"],
}) + select({
"//bazel/constraint:pico_cxx_enable_cxa_atexit_enabled": [],
"//conditions:default": [":cxx_no_cxa_atexit"],
}) + select({
"//bazel/constraint:pico_use_default_max_page_size_enabled": [],
"//conditions:default": [":override_max_page_size"],
}),
) for host_os, host_cpu in HOSTS]

[cc_toolchain(
name = "clang_{}-{}_toolchain_cortex-m".format(host_os, host_cpu),
action_type_configs = [
"@clang_{}-{}//:llvm-ar".format(host_os, host_cpu),
"@clang_{}-{}//:clang".format(host_os, host_cpu),
"@clang_{}-{}//:clang++".format(host_os, host_cpu),
"@clang_{}-{}//:lld".format(host_os, host_cpu),
"@clang_{}-{}//:llvm-objcopy".format(host_os, host_cpu),
"@clang_{}-{}//:llvm-strip".format(host_os, host_cpu),
],
tool_map = "@clang_{}-{}//:all_tools".format(host_os, host_cpu),
args = select({
"//bazel/constraint:rp2040": [
":armv6m-none-eabi",
Expand All @@ -256,24 +221,41 @@ _HOST_CPU_CONSTRAINTS = {
"//conditions:default": [],
}) + [
":bazel_no_absolute_paths",
":nostdlibxx",
":nostartfiles",
":llvm-libc_args",
],
compiler = "clang", # Useful for distinguishing gcc vs clang.
exec_compatible_with = [
_HOST_CPU_CONSTRAINTS[host_cpu],
_HOST_OS_CONSTRAINTS[host_os],
],
tags = ["manual"], # Don't try to build this in wildcard builds.
toolchain_features = [
"@pico-sdk//bazel/toolchain:legacy_features",
known_features = [
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_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",
],
enabled_features = [
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
"@pico-sdk//bazel/toolchain:override_debug",
] + select({
"//bazel/constraint:pico_no_gc_sections_enabled": [],
"//conditions:default": [":gc_sections"],
}) + select({
"//bazel/constraint:pico_cxx_enable_exceptions_enabled": [],
"//conditions:default": [":cxx_no_exceptions"],
}) + select({
"//bazel/constraint:pico_cxx_enable_rtti_enabled": [],
"//conditions:default": [":cxx_no_rtti"],
}) + select({
"//bazel/constraint:pico_cxx_enable_cxa_atexit_enabled": [],
"//conditions:default": [":cxx_no_cxa_atexit"],
}) + select({
"//bazel/constraint:pico_use_default_max_page_size_enabled": [],
"//conditions:default": [":override_max_page_size"],
}),
) for host_os, host_cpu in HOSTS]

[toolchain(
Expand Down
Loading

0 comments on commit e48a2e1

Please sign in to comment.