Skip to content

Commit 89730f7

Browse files
Improve correctness and configurability
1 parent fd64ca3 commit 89730f7

File tree

10 files changed

+223
-48
lines changed

10 files changed

+223
-48
lines changed

MODULE.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,19 @@ register_toolchains(
132132
# Require users to opt-in to the Pico SDK's toolchains.
133133
dev_dependency = True,
134134
)
135+
136+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
137+
python.toolchain(
138+
configure_coverage_tool = True,
139+
python_version = "3.9",
140+
)
141+
142+
use_repo(python, "pythons_hub")
143+
register_toolchains(
144+
"@pythons_hub//:all",
145+
dev_dependency = True,
146+
)
147+
register_toolchains(
148+
"@rules_python//python/runtime_env_toolchains:all",
149+
dev_dependency = True,
150+
)

bazel/config/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ string_flag(
245245
# PICO_BAZEL_CONFIG: PICO_BTSTACK_CONFIG, [Bazel only] The cc_library that provides btstack_config.h, default=//bazel:empty_cc_lib, group=wireless
246246
label_flag(
247247
name = "PICO_BTSTACK_CONFIG",
248-
build_setting_default = "//bazel:incompatible_cc_lib",
248+
build_setting_default = "//bazel:empty_cc_lib",
249249
)
250250

251251
# PICO_BAZEL_CONFIG: PICO_BT_ENABLE_BLE, [Bazel only] Whether or not to link in BLE portions of the btstack as part of //src/rp2_common/pico_btstack. Also defines ENABLE_BLE=1, type=bool, default=False, group=wireless
@@ -269,7 +269,7 @@ bool_flag(
269269
# PICO_BAZEL_CONFIG: PICO_LWIP_CONFIG, [Bazel only] The cc_library that provides lwipopts.h, default=//bazel:empty_cc_lib, group=wireless
270270
label_flag(
271271
name = "PICO_LWIP_CONFIG",
272-
build_setting_default = "//bazel:incompatible_cc_lib",
272+
build_setting_default = "@pico-sdk//bazel:empty_cc_lib",
273273
)
274274

275275
# PICO_BAZEL_CONFIG: PICO_FREERTOS_LIB, [Bazel only] The cc_library that provides FreeRTOS, default=//bazel:empty_cc_lib, group=wireless

bazel/constraint/BUILD.bazel

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("//bazel/util:label_flag_matches.bzl", "label_flag_matches")
2+
13
package(default_visibility = ["//visibility:public"])
24

35
# This constraint represents the dimension that guides the Pico SDK build. This
@@ -218,7 +220,20 @@ config_setting(
218220
flag_values = {"//bazel/config:PICO_BT_ENABLE_MESH": "True"},
219221
)
220222

221-
config_setting(
223+
label_flag_matches(
224+
name = "pico_lwip_config_unset",
225+
flag = "//bazel/config:PICO_LWIP_CONFIG",
226+
value = "//bazel:empty_cc_lib",
227+
)
228+
229+
label_flag_matches(
230+
name = "pico_btstack_config_unset",
231+
flag = "//bazel/config:PICO_BTSTACK_CONFIG",
232+
value = "//bazel:empty_cc_lib",
233+
)
234+
235+
label_flag_matches(
222236
name = "pico_freertos_unset",
223-
flag_values = {"//bazel/config:PICO_FREERTOS_LIB": "//bazel:empty_cc_lib"},
237+
flag = "//bazel/config:PICO_FREERTOS_LIB",
238+
value = "//bazel:empty_cc_lib",
224239
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cpp_toolchain", "use_cc_toolchain")
2+
3+
def _pico_btstack_make_gatt_header_impl(ctx):
4+
5+
cc_toolchain = find_cpp_toolchain(ctx)
6+
feature_configuration = cc_common.configure_features(
7+
ctx = ctx,
8+
cc_toolchain = cc_toolchain,
9+
requested_features = ctx.features,
10+
unsupported_features = ctx.disabled_features,
11+
)
12+
13+
out = ctx.actions.declare_file(
14+
"{}_gatt_generated/{}.h".format(ctx.label.name, ctx.file.src.basename.removesuffix(".gatt")),
15+
)
16+
17+
ctx.actions.run(
18+
executable = ctx.executable._make_gat_header_tool,
19+
arguments = [
20+
ctx.file.src.path,
21+
out.path,
22+
"-I",
23+
ctx.file._btstack_hdr.dirname,
24+
] + [
25+
26+
],
27+
inputs = [
28+
ctx.file.src,
29+
ctx.file._btstack_hdr,
30+
],
31+
outputs = [out],
32+
)
33+
34+
cc_ctx = cc_common.create_compilation_context(
35+
headers = depset(direct = [out]),
36+
includes = depset(direct = [out.dirname]),
37+
)
38+
39+
return [
40+
DefaultInfo(files = depset(direct = [out])),
41+
CcInfo(compilation_context = cc_ctx)
42+
]
43+
44+
pico_btstack_make_gatt_header = rule(
45+
implementation = _pico_btstack_make_gatt_header_impl,
46+
attrs = {
47+
"src": attr.label(mandatory = True, allow_single_file = True),
48+
"_btstack_hdr": attr.label(
49+
default = "@btstack//:src/bluetooth_gatt.h",
50+
allow_single_file = True,
51+
),
52+
"_make_gat_header_tool": attr.label(
53+
default = "@btstack//:compile_gatt",
54+
cfg = "exec",
55+
executable = True,
56+
),
57+
},
58+
fragments = ["cpp"],
59+
toolchains = use_cc_toolchain(),
60+
)

bazel/util/label_flag_matches.bzl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
2+
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
3+
4+
def _cc_toolchain_feature_is_enabled_impl(ctx):
5+
toolchain = find_cpp_toolchain(ctx)
6+
feature_configuration = cc_common.configure_features(
7+
ctx = ctx,
8+
cc_toolchain = toolchain,
9+
)
10+
val = cc_common.is_enabled(
11+
feature_configuration = feature_configuration,
12+
feature_name = ctx.attr.feature_name,
13+
)
14+
15+
def _match_label_flag_impl(ctx):
16+
matches = str(ctx.attr.expected_value.label) == str(ctx.attr.flag.label)
17+
return [
18+
config_common.FeatureFlagInfo(value = str(matches)),
19+
BuildSettingInfo(value = matches),
20+
]
21+
22+
_match_label_flag = rule(
23+
implementation = _match_label_flag_impl,
24+
attrs = {
25+
"expected_value": attr.label(
26+
mandatory = True,
27+
doc = "The expected flag value",
28+
),
29+
"flag": attr.label(
30+
mandatory = True,
31+
doc = "The flag to extract a value from",
32+
),
33+
},
34+
)
35+
36+
def label_flag_matches(*, name, flag, value):
37+
_match_label_flag(
38+
name = name + "._impl",
39+
expected_value = native.package_relative_label(value),
40+
flag = flag,
41+
)
42+
43+
native.config_setting(
44+
name = name,
45+
flag_values = {":{}".format(name + "._impl"): "True"},
46+
)

src/rp2_common/pico_btstack/btstack.BUILD

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
load("@rules_python//python:defs.bzl", "py_binary")
12
load("@pico-sdk//bazel:defs.bzl", "compatible_with_config")
2-
load("@pico-sdk//bazel/util:sdk_define.bzl", "pico_sdk_define")
33

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

6+
# Expose the gatt header for pico_btstack_make_gatt_header.
7+
exports_files(
8+
["src/bluetooth_gatt.h"],
9+
visibility = ["@pico-sdk//bazel:__pkg__"],
10+
)
11+
612
_DISABLE_WARNINGS = [
713
"-Wno-cast-qual",
814
"-Wno-format",
@@ -15,28 +21,20 @@ _DISABLE_WARNINGS = [
1521
"-Wno-unused-parameter",
1622
]
1723

18-
pico_sdk_define(
19-
name = "PICO_ENABLE_BLE",
20-
define_name = "ENABLE_BLE",
21-
from_flag = "@pico-sdk//bazel/config:PICO_BT_ENABLE_BLE",
22-
)
23-
24-
pico_sdk_define(
25-
name = "PICO_ENABLE_MESH",
26-
define_name = "ENABLE_MESH",
27-
from_flag = "@pico-sdk//bazel/config:PICO_BT_ENABLE_MESH",
28-
)
29-
30-
pico_sdk_define(
31-
name = "PICO_ENABLE_CLASSIC",
32-
define_name = "ENABLE_CLASSIC",
33-
from_flag = "@pico-sdk//bazel/config:PICO_BT_ENABLE_CLASSIC",
34-
)
35-
3624
cc_library(
3725
name = "pico_btstack_base_headers",
3826
hdrs = glob(["**/*.h"]),
3927
visibility = ["//visibility:private"],
28+
defines = select({
29+
"@pico-sdk//bazel/constraint:pico_bt_enable_ble_enabled": ["ENABLE_BLE=1"],
30+
"//conditions:default": [],
31+
}) + select({
32+
"@pico-sdk//bazel/constraint:pico_bt_enable_mesh_enabled": ["ENABLE_MESH=1"],
33+
"//conditions:default": [],
34+
}) + select({
35+
"@pico-sdk//bazel/constraint:pico_bt_enable_classic_enabled": ["ENABLE_CLASSIC=1"],
36+
"//conditions:default": [],
37+
}),
4038
includes = [
4139
".",
4240
"3rd-party/md5",
@@ -48,9 +46,6 @@ cc_library(
4846
"src",
4947
],
5048
deps = [
51-
":PICO_ENABLE_BLE",
52-
":PICO_ENABLE_MESH",
53-
":PICO_ENABLE_CLASSIC",
5449
"@pico-sdk//bazel/config:PICO_BTSTACK_CONFIG"
5550
],
5651
)
@@ -300,3 +295,12 @@ cc_library(
300295
"@pico-sdk//src/rp2_common/pico_lwip:pico_lwip_freertos",
301296
],
302297
)
298+
299+
py_binary(
300+
name = "compile_gatt",
301+
srcs = [
302+
"tool/compile_gatt.py",
303+
],
304+
# TODO: Add pip pins.
305+
# deps = ["@python_packages//pycryptodomex"]
306+
)

src/rp2_common/pico_cyw43_arch/BUILD.bazel

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ _CONFIGURATIONS = [
1313
]
1414

1515
# This produces the following labels:
16-
# pico_cyw43_arch_sys_freertos
17-
# pico_cyw43_arch_lwip_sys_freertos
16+
# pico_cyw43_arch_freertos
17+
# pico_cyw43_arch_lwip_freertos
1818
# pico_cyw43_arch_poll
1919
# pico_cyw43_arch_lwip_poll
2020
# pico_cyw43_arch_threadsafe_background
@@ -36,20 +36,24 @@ _CONFIGURATIONS = [
3636
defines = [
3737
"LIB_PICO_CYW43_ARCH=1",
3838
"PICO_CYW43_ARCH_{}=1".format(kind.upper()),
39-
"CYW43_LWIP={}".format(1 if use_lwip else 0),
4039
],
4140
includes = ["include"],
4241
target_compatible_with = compatible_with_pico_w() + (
4342
incompatible_with_config("//bazel/constraint:pico_freertos_unset") if kind == "freertos" else []
43+
) + (
44+
incompatible_with_config("//bazel/constraint:pico_lwip_config_unset") if use_lwip else []
4445
),
4546
deps = [
4647
"//src/rp2_common:pico_platform",
4748
"//src/rp2_common/pico_async_context:pico_async_context_{}".format(kind),
4849
"//src/rp2_common/pico_cyw43_driver",
49-
"//src/rp2_common/pico_lwip",
5050
"//src/rp2_common/pico_unique_id",
5151
] + (
52-
["//src/rp2_common/pico_lwip:pico_lwip_freertos"] if kind == "freertos" else ["//src/rp2_common/pico_lwip:pico_lwip_nosys"]
52+
["//src/rp2_common/pico_lwip:pico_lwip_freertos"] if kind == "freertos" and use_lwip else []
53+
) + (
54+
["//src/rp2_common/pico_lwip:pico_lwip_nosys"] if kind != "freertos" and use_lwip else []
55+
) + (
56+
["//src/rp2_common/pico_lwip"] if use_lwip else []
5357
),
5458
)
5559
for kind, use_lwip in _CONFIGURATIONS

src/rp2_common/pico_cyw43_driver/BUILD.bazel

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,27 @@ cc_library(
3535
"//src/rp2_common/hardware_pio",
3636
"//src/rp2_common/hardware_sync",
3737
"//src/rp2_common/pico_async_context",
38-
"//src/rp2_common/pico_btstack:pico_btstack",
3938
"//src/rp2_common/pico_unique_id",
4039
"@cyw43-driver//:cyw43_driver",
41-
],
40+
] + select({
41+
"//bazel/constraint:pico_btstack_config_unset": [],
42+
"//conditions:default": [
43+
"//src/rp2_common/pico_btstack:pico_btstack",
44+
],
45+
}),
4246
alwayslink = True,
4347
)
4448

4549
cc_library(
4650
name = "pico_btstack_cyw43",
47-
srcs = [
48-
"btstack_chipset_cyw43.c",
49-
"btstack_cyw43.c",
50-
"btstack_hci_transport_cyw43.c",
51-
],
51+
srcs = select({
52+
"//bazel/constraint:pico_btstack_config_unset": [],
53+
"//conditions:default": [
54+
"btstack_cyw43.c",
55+
"btstack_chipset_cyw43.c",
56+
"btstack_hci_transport_cyw43.c",
57+
],
58+
}),
5259
hdrs = [
5360
"include/pico/btstack_chipset_cyw43.h",
5461
"include/pico/btstack_cyw43.h",
@@ -58,13 +65,17 @@ cc_library(
5865
deps = [
5966
":cyw43_bus_pio",
6067
":cyw43_configport",
61-
"//bazel/config:PICO_BTSTACK_CONFIG",
62-
"//src/rp2_common/pico_btstack:btstack_run_loop_async_context",
63-
"//src/rp2_common/pico_btstack:pico_btstack",
64-
"//src/rp2_common/pico_btstack:pico_btstack_flash_bank",
65-
"//src/rp2_common/pico_cyw43_driver/cybt_shared_bus:cybt_shared_bus_driver",
6668
"@cyw43-driver//:cyw43_driver",
67-
],
69+
] + select({
70+
"//bazel/constraint:pico_btstack_config_unset": [],
71+
"//conditions:default": [
72+
"//bazel/config:PICO_BTSTACK_CONFIG",
73+
"//src/rp2_common/pico_cyw43_driver/cybt_shared_bus:cybt_shared_bus_driver",
74+
"//src/rp2_common/pico_btstack:btstack_run_loop_async_context",
75+
"//src/rp2_common/pico_btstack:pico_btstack",
76+
"//src/rp2_common/pico_btstack:pico_btstack_flash_bank",
77+
],
78+
}),
6879
alwayslink = True,
6980
)
7081

src/rp2_common/pico_cyw43_driver/cyw43-driver.BUILD

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,32 @@ cc_library(
1111
"src/cyw43_stats.c",
1212
],
1313
hdrs = glob(["**/*.h"]),
14-
defines = ["CYW43_ENABLE_BLUETOOTH=1"],
14+
defines = select({
15+
"@pico-sdk//bazel/constraint:pico_lwip_config_unset": [
16+
"CYW43_LWIP=0",
17+
],
18+
"//conditions:default": [
19+
"CYW43_LWIP=1",
20+
],
21+
})+ select({
22+
"@pico-sdk//bazel/constraint:pico_btstack_config_unset": [
23+
"CYW43_ENABLE_BLUETOOTH=0",
24+
],
25+
"//conditions:default": [
26+
"CYW43_ENABLE_BLUETOOTH=1",
27+
],
28+
}),
1529
includes = [
1630
"firmware",
1731
"src",
1832
],
1933
target_compatible_with = compatible_with_pico_w(),
2034
deps = [
2135
"@pico-sdk//src/rp2_common/pico_cyw43_driver:cyw43_configport",
22-
"@pico-sdk//src/rp2_common/pico_lwip",
23-
],
36+
] + select({
37+
"@pico-sdk//bazel/constraint:pico_lwip_config_unset": [],
38+
"//conditions:default": [
39+
"@pico-sdk//src/rp2_common/pico_lwip",
40+
],
41+
}),
2442
)

0 commit comments

Comments
 (0)