Skip to content

Commit e48a2e1

Browse files
[Bazel] Update rules_cc to v0.0.10 (#1989)
Updates rules_cc to v0.0.10 to reduce the steps required to get started with creating a Bazel-based Pi Pico project.
1 parent d649c6c commit e48a2e1

File tree

7 files changed

+190
-302
lines changed

7 files changed

+190
-302
lines changed

.bazelrc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Required for new toolchain resolution API.
2-
build --incompatible_enable_cc_toolchain_resolution
3-
build --@rules_cc//cc/toolchains:experimental_enable_rule_based_toolchains
4-
51
# Silence all C/C++ warnings in external code.
62
common --per_file_copt=external/.*@-w
73
common --host_per_file_copt=external/.*@-w

MODULE.bazel

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,7 @@ bazel_dep(name = "platforms", version = "0.0.9")
77
bazel_dep(name = "bazel_skylib", version = "1.6.1")
88
bazel_dep(name = "rules_python", version = "0.22.1")
99
bazel_dep(name = "picotool", version = "2.0.0")
10-
11-
# Note: rules_cc is special-cased repository; a dependency on rules_cc in a
12-
# module will not ensure that the root Bazel module has that same version of
13-
# rules_cc. For that reason, this primarily acts as a FYI. You'll still need
14-
# to explicitly list this dependency in your own project's MODULE.bazel file.
15-
bazel_dep(name = "rules_cc", version = "0.0.9")
16-
17-
# rules_cc v0.0.10 is not yet cut, so manually pull in the desired version.
18-
# This does not apply to dependent projects, so it needs to be copied to your
19-
# project's MODULE.bazel too.
20-
archive_override(
21-
module_name = "rules_cc",
22-
integrity = "sha256-zdQo/pQWKdIAPKSflBxOSWZNwCbc86T7SechKZo/3Xw=",
23-
strip_prefix = "rules_cc-1acf5213b6170f1f0133e273cb85ede0e732048f",
24-
urls = "https://github.com/bazelbuild/rules_cc/archive/1acf5213b6170f1f0133e273cb85ede0e732048f.tar.gz",
25-
)
10+
bazel_dep(name = "rules_cc", version = "0.0.10")
2611

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

bazel/README.md

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,10 @@
33
## Using the Pico SDK in a Bazel project.
44

55
### Add pico-sdk as a dependency
6-
First, in your `MODULE.bazel` file, add a dependency on the Pico SDK:
6+
First, in your `MODULE.bazel` file, add a dependency on the Pico SDK and
7+
`rules_cc`:
78
```python
8-
bazel_dep(
9-
name = "pico-sdk",
10-
version = "2.0.0",
11-
)
12-
```
13-
14-
Second, in the same file you'll need to add an explicit dependency on
15-
`rules_cc`, as it's a special-cased Bazel module:
16-
```python
17-
# Note: rules_cc is special-cased repository; a dependency on rules_cc in a
18-
# module will not ensure that the root Bazel module has that same version of
19-
# rules_cc. For that reason, this primarily acts as a FYI. You'll still need
20-
# to explicitly list this dependency in your own project's MODULE.bazel file.
21-
bazel_dep(name = "rules_cc", version = "0.0.9")
22-
23-
# rules_cc v0.0.10 is not yet cut, so manually pull in the desired version.
24-
# This does not apply to dependent projects, so it needs to be copied to your
25-
# project's MODULE.bazel too.
26-
archive_override(
27-
module_name = "rules_cc",
28-
urls = "https://github.com/bazelbuild/rules_cc/archive/1acf5213b6170f1f0133e273cb85ede0e732048f.zip",
29-
strip_prefix = "rules_cc-1acf5213b6170f1f0133e273cb85ede0e732048f",
30-
integrity = "sha256-NddP6xi6LzsIHT8bMSVJ2NtoURbN+l3xpjvmIgB6aSg=",
31-
)
9+
bazel_dep(name = "pico-sdk", version = "2.0.1")
3210
```
3311

3412
### Register toolchains
@@ -47,15 +25,6 @@ register_toolchains(
4725
)
4826
```
4927

50-
### Enable required .bazelrc flags
51-
To use the toolchains provided by the Pico SDK, you'll need to enable a few
52-
new features. In your project's `.bazelrc`, add the following
53-
```
54-
# Required for new toolchain resolution API.
55-
build --incompatible_enable_cc_toolchain_resolution
56-
build --@rules_cc//cc/toolchains:experimental_enable_rule_based_toolchains
57-
```
58-
5928
### Ready to build!
6029
You're now ready to start building Pico Projects in Bazel! When building,
6130
don't forget to specify `--platforms` so Bazel knows you're targeting the

bazel/toolchain/BUILD.bazel

Lines changed: 64 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -51,36 +51,39 @@ cc_args(
5151
],
5252
)
5353

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

6064
cc_args(
61-
name = "no-canonical-prefixes",
65+
name = "no_canonical_prefixes",
6266
actions = ["@rules_cc//cc/toolchains/actions:compile_actions"],
6367
args = ["-no-canonical-prefixes"],
6468
)
6569

66-
cc_args(
67-
name = "nostdlibxx",
68-
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
69-
args = ["-nostdlib++"],
70-
)
71-
72-
cc_args(
73-
name = "nostartfiles",
74-
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
75-
args = ["-nostartfiles"],
76-
)
77-
7870
cc_args_list(
7971
name = "bazel_no_absolute_paths",
8072
args = select({
8173
"//bazel/constraint:pico_toolchain_clang_enabled": [],
82-
"//conditions:default": [":no-canonical-system-headers"],
83-
}) + [":no-canonical-prefixes"],
74+
"//conditions:default": [":no_canonical_system_headers"],
75+
}) + [":no_canonical_prefixes"],
76+
)
77+
78+
cc_args(
79+
name = "llvm-libc_args",
80+
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
81+
args = [
82+
"-nostdlib++",
83+
"-nostartfiles",
84+
"-Wl,-lc++",
85+
],
86+
visibility = ["//visibility:private"],
8487
)
8588

8689
cc_args(
@@ -101,7 +104,6 @@ configurable_toolchain_feature(
101104
"-ffunction-sections",
102105
"-fdata-sections",
103106
],
104-
disable_if = "//bazel/constraint:pico_no_gc_sections_enabled",
105107
linkopts = ["-Wl,--gc-sections"],
106108
)
107109

@@ -111,24 +113,20 @@ configurable_toolchain_feature(
111113
"-fno-exceptions",
112114
"-fno-unwind-tables",
113115
],
114-
disable_if = "//bazel/constraint:pico_cxx_enable_exceptions_enabled",
115116
)
116117

117118
configurable_toolchain_feature(
118119
name = "cxx_no_rtti",
119120
cxxopts = ["-fno-rtti"],
120-
disable_if = "//bazel/constraint:pico_cxx_enable_rtti_enabled",
121121
)
122122

123123
configurable_toolchain_feature(
124124
name = "cxx_no_cxa_atexit",
125125
cxxopts = ["-fno-use-cxa-atexit"],
126-
disable_if = "//bazel/constraint:pico_cxx_enable_cxa_atexit_enabled",
127126
)
128127

129128
configurable_toolchain_feature(
130129
name = "override_max_page_size",
131-
disable_if = "//bazel/constraint:pico_use_default_max_page_size_enabled",
132130
linkopts = ["-Wl,-z,max-page-size=4096"],
133131
)
134132

@@ -141,36 +139,9 @@ cc_args_list(
141139
cc_feature(
142140
name = "override_debug",
143141
args = [":all_opt_debug_args"],
144-
enabled = True,
145142
overrides = "@rules_cc//cc/toolchains/features:dbg",
146143
)
147144

148-
# TODO: https://github.com/bazelbuild/rules_cc/issues/224 - This is required for
149-
# now, but hopefully will eventually go away.
150-
cc_feature(
151-
name = "legacy_features",
152-
args = [],
153-
enabled = True,
154-
feature_name = "force_legacy_features",
155-
implies = [
156-
"@rules_cc//cc/toolchains/features/legacy:archiver_flags",
157-
"@rules_cc//cc/toolchains/features/legacy:build_interface_libraries",
158-
"@rules_cc//cc/toolchains/features/legacy:dynamic_library_linker_tool",
159-
"@rules_cc//cc/toolchains/features/legacy:strip_debug_symbols",
160-
"@rules_cc//cc/toolchains/features/legacy:linkstamps",
161-
"@rules_cc//cc/toolchains/features/legacy:output_execpath_flags",
162-
"@rules_cc//cc/toolchains/features/legacy:runtime_library_search_directories",
163-
"@rules_cc//cc/toolchains/features/legacy:library_search_directories",
164-
"@rules_cc//cc/toolchains/features/legacy:libraries_to_link",
165-
"@rules_cc//cc/toolchains/features/legacy:force_pic_flags",
166-
"@rules_cc//cc/toolchains/features/legacy:user_link_flags",
167-
"@rules_cc//cc/toolchains/features/legacy:legacy_link_flags",
168-
"@rules_cc//cc/toolchains/features/legacy:linker_param_file",
169-
"@rules_cc//cc/toolchains/features/legacy:fission_support",
170-
"@rules_cc//cc/toolchains/features/legacy:sysroot",
171-
],
172-
)
173-
174145
HOSTS = (
175146
("linux", "x86_64"),
176147
("linux", "aarch64"),
@@ -192,58 +163,52 @@ _HOST_CPU_CONSTRAINTS = {
192163

193164
[cc_toolchain(
194165
name = "arm_gcc_{}-{}_toolchain_cortex-m".format(host_os, host_cpu),
195-
action_type_configs = [
196-
"@arm_gcc_{}-{}//:arm-none-eabi-ar".format(host_os, host_cpu),
197-
"@arm_gcc_{}-{}//:arm-none-eabi-gcc".format(host_os, host_cpu),
198-
"@arm_gcc_{}-{}//:arm-none-eabi-g++".format(host_os, host_cpu),
199-
"@arm_gcc_{}-{}//:arm-none-eabi-ld".format(host_os, host_cpu),
200-
"@arm_gcc_{}-{}//:arm-none-eabi-objcopy".format(host_os, host_cpu),
201-
"@arm_gcc_{}-{}//:arm-none-eabi-strip".format(host_os, host_cpu),
202-
],
166+
tool_map = "@arm_gcc_{}-{}//:all_tools".format(host_os, host_cpu),
203167
args = select({
204168
"//bazel/constraint:rp2040": [":cortex-m0"],
205169
"//bazel/constraint:rp2350": [":cortex-m33"],
206170
"//conditions:default": [],
207171
}) + [
208172
":bazel_no_absolute_paths",
209173
],
210-
compiler = "gcc", # Useful for distinguishing gcc vs clang.
211-
cxx_builtin_include_directories = [
212-
"%sysroot%/arm-none-eabi/include/newlib-nano",
213-
"%sysroot%/arm-none-eabi/include/c++/13.2.1",
214-
"%sysroot%/arm-none-eabi/include/c++/13.2.1/arm-none-eabi",
215-
"%sysroot%/arm-none-eabi/include/c++/13.2.1/backward",
216-
"%sysroot%/lib/gcc/arm-none-eabi/13.2.1/include",
217-
"%sysroot%/lib/gcc/arm-none-eabi/13.2.1/include-fixed",
218-
"%sysroot%/arm-none-eabi/include",
219-
],
220174
exec_compatible_with = [
221175
_HOST_CPU_CONSTRAINTS[host_cpu],
222176
_HOST_OS_CONSTRAINTS[host_os],
223177
],
224-
sysroot = "external/arm_gcc_{}-{}".format(host_os, host_cpu),
225178
tags = ["manual"], # Don't try to build this in wildcard builds.
226-
toolchain_features = [
227-
"@pico-sdk//bazel/toolchain:legacy_features",
179+
known_features = [
180+
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
228181
"@pico-sdk//bazel/toolchain:override_debug",
229182
"@pico-sdk//bazel/toolchain:gc_sections",
230183
"@pico-sdk//bazel/toolchain:cxx_no_exceptions",
231184
"@pico-sdk//bazel/toolchain:cxx_no_rtti",
232185
"@pico-sdk//bazel/toolchain:cxx_no_cxa_atexit",
233186
"@pico-sdk//bazel/toolchain:override_max_page_size",
234187
],
188+
enabled_features = [
189+
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
190+
"@pico-sdk//bazel/toolchain:override_debug",
191+
] + select({
192+
"//bazel/constraint:pico_no_gc_sections_enabled": [],
193+
"//conditions:default": [":gc_sections"],
194+
}) + select({
195+
"//bazel/constraint:pico_cxx_enable_exceptions_enabled": [],
196+
"//conditions:default": [":cxx_no_exceptions"],
197+
}) + select({
198+
"//bazel/constraint:pico_cxx_enable_rtti_enabled": [],
199+
"//conditions:default": [":cxx_no_rtti"],
200+
}) + select({
201+
"//bazel/constraint:pico_cxx_enable_cxa_atexit_enabled": [],
202+
"//conditions:default": [":cxx_no_cxa_atexit"],
203+
}) + select({
204+
"//bazel/constraint:pico_use_default_max_page_size_enabled": [],
205+
"//conditions:default": [":override_max_page_size"],
206+
}),
235207
) for host_os, host_cpu in HOSTS]
236208

237209
[cc_toolchain(
238210
name = "clang_{}-{}_toolchain_cortex-m".format(host_os, host_cpu),
239-
action_type_configs = [
240-
"@clang_{}-{}//:llvm-ar".format(host_os, host_cpu),
241-
"@clang_{}-{}//:clang".format(host_os, host_cpu),
242-
"@clang_{}-{}//:clang++".format(host_os, host_cpu),
243-
"@clang_{}-{}//:lld".format(host_os, host_cpu),
244-
"@clang_{}-{}//:llvm-objcopy".format(host_os, host_cpu),
245-
"@clang_{}-{}//:llvm-strip".format(host_os, host_cpu),
246-
],
211+
tool_map = "@clang_{}-{}//:all_tools".format(host_os, host_cpu),
247212
args = select({
248213
"//bazel/constraint:rp2040": [
249214
":armv6m-none-eabi",
@@ -256,24 +221,41 @@ _HOST_CPU_CONSTRAINTS = {
256221
"//conditions:default": [],
257222
}) + [
258223
":bazel_no_absolute_paths",
259-
":nostdlibxx",
260-
":nostartfiles",
224+
":llvm-libc_args",
261225
],
262-
compiler = "clang", # Useful for distinguishing gcc vs clang.
263226
exec_compatible_with = [
264227
_HOST_CPU_CONSTRAINTS[host_cpu],
265228
_HOST_OS_CONSTRAINTS[host_os],
266229
],
267230
tags = ["manual"], # Don't try to build this in wildcard builds.
268-
toolchain_features = [
269-
"@pico-sdk//bazel/toolchain:legacy_features",
231+
known_features = [
232+
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
270233
"@pico-sdk//bazel/toolchain:override_debug",
271234
"@pico-sdk//bazel/toolchain:gc_sections",
272235
"@pico-sdk//bazel/toolchain:cxx_no_exceptions",
273236
"@pico-sdk//bazel/toolchain:cxx_no_rtti",
274237
"@pico-sdk//bazel/toolchain:cxx_no_cxa_atexit",
275238
"@pico-sdk//bazel/toolchain:override_max_page_size",
276239
],
240+
enabled_features = [
241+
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
242+
"@pico-sdk//bazel/toolchain:override_debug",
243+
] + select({
244+
"//bazel/constraint:pico_no_gc_sections_enabled": [],
245+
"//conditions:default": [":gc_sections"],
246+
}) + select({
247+
"//bazel/constraint:pico_cxx_enable_exceptions_enabled": [],
248+
"//conditions:default": [":cxx_no_exceptions"],
249+
}) + select({
250+
"//bazel/constraint:pico_cxx_enable_rtti_enabled": [],
251+
"//conditions:default": [":cxx_no_rtti"],
252+
}) + select({
253+
"//bazel/constraint:pico_cxx_enable_cxa_atexit_enabled": [],
254+
"//conditions:default": [":cxx_no_cxa_atexit"],
255+
}) + select({
256+
"//bazel/constraint:pico_use_default_max_page_size_enabled": [],
257+
"//conditions:default": [":override_max_page_size"],
258+
}),
277259
) for host_os, host_cpu in HOSTS]
278260

279261
[toolchain(

0 commit comments

Comments
 (0)