Skip to content

Commit

Permalink
Initial Bazel Pico W support
Browse files Browse the repository at this point in the history
  • Loading branch information
armandomontanez committed Jun 12, 2024
1 parent ad43f3b commit fb7fa3d
Show file tree
Hide file tree
Showing 19 changed files with 921 additions and 113 deletions.
27 changes: 27 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ http_archive(
sha256 = "ac57109bba00d26ffa33312d5f334990ec9a9a4d82bf890ed8b825b4610d1da2",
)

# TODO: Provide btstack as a proper Bazel module.
http_archive(
name = "btstack",
url = "https://github.com/bluekitchen/btstack/archive/72ef1732c954d938091467961e41f4aa9b976b34.zip",
strip_prefix = "btstack-72ef1732c954d938091467961e41f4aa9b976b34",
build_file = "//src/rp2_common/pico_btstack:btstack.BUILD",
sha256 = "f45d72b5d404dd2f8e311287de6f2ba3561fc8ae956737eeb611b277aadc2391",
)

# TODO: Provide btstack as a proper Bazel module.
http_archive(
name = "cyw43-driver",
url = "https://github.com/georgerobotics/cyw43-driver/archive/8ef38a6d32c54f850bff8f189bdca19ded33792a.zip",
strip_prefix = "cyw43-driver-8ef38a6d32c54f850bff8f189bdca19ded33792a",
build_file = "//src/rp2_common/pico_cyw43_driver:cyw43-driver.BUILD",
sha256 = "0b44a19ea58537ee954357606cde5ed20c3a42be77adfebb07b7c0e4740f6228",
)

# TODO: Provide lwip as a proper Bazel module.
http_archive(
name = "lwip",
url = "https://github.com/lwip-tcpip/lwip/archive/239918ccc173cb2c2a62f41a40fd893f57faf1d6.zip",
strip_prefix = "lwip-239918ccc173cb2c2a62f41a40fd893f57faf1d6",
build_file = "//src/rp2_common/pico_lwip:lwip.BUILD",
sha256 = "7ee9e02f2719c0422377e1fcce5a21716ca2e2e855cca56695f9ef7cb020e5dd",
)

register_toolchains(
"//bazel/toolchain:arm_gcc_linux-x86_64",
"//bazel/toolchain:arm_gcc_win-x86_64",
Expand Down
18 changes: 18 additions & 0 deletions bazel/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,21 @@ label_flag(
name = "PICO_CONFIG_HEADER",
build_setting_default = "//src/boards:default",
)

# PICO_BAZEL_CONFIG: PICO_BTSTACK_CONFIG, [Bazel only] The cc_library that provides btstack_config.h, default=//bazel:empty_cc_lib, group=wireless
label_flag(
name = "PICO_BTSTACK_CONFIG",
build_setting_default = "//bazel:empty_cc_lib",
)

# PICO_BAZEL_CONFIG: PICO_LWIP_CONFIG, [Bazel only] The cc_library that provides lwipopts.h, default=//bazel:empty_cc_lib, group=wireless
label_flag(
name = "PICO_LWIP_CONFIG",
build_setting_default = "//bazel:empty_cc_lib",
)

# PICO_BAZEL_CONFIG: PICO_FREERTOS_LIB, [Bazel only] The cc_library that provides FreeRTOS, default=//bazel:empty_cc_lib, group=wireless
label_flag(
name = "PICO_FREERTOS_LIB",
build_setting_default = "//bazel:empty_cc_lib",
)
35 changes: 35 additions & 0 deletions bazel/constraint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ config_setting(
],
)

constraint_setting(
name = "wireless_support",
default_constraint_value = "no_wireless",
)

constraint_value(
name = "no_wireless",
constraint_setting = ":wireless_support",
)

constraint_value(
name = "cyw43_wireless",
constraint_setting = ":wireless_support",
)

config_setting(
name = "is_pico_w",
flag_values = {"//bazel/config:PICO_BOARD": "pico_w"},
)

config_setting(
name = "pico_baremetal_enabled",
flag_values = {"//bazel/config:PICO_BARE_METAL": "True"},
Expand Down Expand Up @@ -79,3 +99,18 @@ config_setting(
name = "pico_no_target_name_enabled",
flag_values = {"//bazel/config:PICO_NO_TARGET_NAME": "True"},
)

config_setting(
name = "pico_btstack_config_unset",
flag_values = {"//bazel/config:PICO_BTSTACK_CONFIG": "@pico-sdk//bazel:empty_cc_lib"},
)

config_setting(
name = "pico_lwip_config_unset",
flag_values = {"//bazel/config:PICO_LWIP_CONFIG": "@pico-sdk//bazel:empty_cc_lib"},
)

config_setting(
name = "pico_freertos_unset",
flag_values = {"//bazel/config:PICO_FREERTOS_LIB": "@pico-sdk//bazel:empty_cc_lib"},
)
40 changes: 40 additions & 0 deletions bazel/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def _pico_generate_pio_header_impl(ctx):
generated_headers = []
for f in ctx.files.srcs:
out = ctx.actions.declare_file(
"{}_pio_generated/{}.h".format(ctx.label.name, f.basename)
)
generated_headers.append(out)
ctx.actions.run(
executable = ctx.executable._pioasm_tool,
arguments = [
"-o",
"c-sdk",
f.path,
out.path,
],
inputs = [f],
outputs = [out],
)

cc_ctx = cc_common.create_compilation_context(
headers = depset(direct = generated_headers),
includes = depset(direct = [generated_headers[0].dirname]),
)
return [
DefaultInfo(files=depset(direct = generated_headers)),
CcInfo(compilation_context = cc_ctx),
]

pico_generate_pio_header = rule(
implementation = _pico_generate_pio_header_impl,
attrs = {
"srcs": attr.label_list(mandatory = True, allow_files = True),
"_pioasm_tool": attr.label(
default = "@pico-sdk//tools/pioasm:pioasm",
cfg = "exec",
executable = True
),
},
provides = [CcInfo],
)
2 changes: 1 addition & 1 deletion bazel/util/sdk_define.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def _pico_sdk_define_impl(ctx):
val = 1 if val else 0
cc_ctx = cc_common.create_compilation_context(
defines = depset(
["{}={}".format(ctx.attr.define_name, val)]
direct = ["{}={}".format(ctx.attr.define_name, val)]
)
)
return [CcInfo(compilation_context = cc_ctx)]
Expand Down
11 changes: 11 additions & 0 deletions bazel/util/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ rp2040_bootloader_binary = declare_transtion(
"//command_line_option:custom_malloc": "_malloc",
},
)

kitchen_sink_test_binary = declare_transtion(
attrs = {
"bt_stack_config": attr.label(mandatory = True),
"lwip_config": attr.label(mandatory = True),
},
flag_overrides = {
"@pico-sdk//bazel/config:PICO_BTSTACK_CONFIG": "bt_stack_config",
"@pico-sdk//bazel/config:PICO_LWIP_CONFIG": "lwip_config",
},
)
2 changes: 1 addition & 1 deletion src/rp2_common/boot_stage2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ cc_binary(
# this does nothing if someone passes --custom_malloc, so the
# rp2040_bootloader_binary transition forcibly clobbers --custom_malloc.
malloc = "//bazel:empty_cc_lib",
tags = ["manual"],
tags = ["manual"], # Only build as an explicit dependency.
target_compatible_with = select({
"//bazel/constraint:rp2": [],
"//conditions:default": ["@platforms//:incompatible"],
Expand Down
71 changes: 57 additions & 14 deletions src/rp2_common/pico_async_context/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,73 @@ package(default_visibility = ["//visibility:public"])

cc_library(
name = "pico_async_context",
srcs = [
"async_context_base.c",
"async_context_freertos.c",
"async_context_poll.c",
"async_context_threadsafe_background.c",
],
srcs = ["async_context_base.c"],
hdrs = [
"include/pico/async_context.h",
"include/pico/async_context_base.h",
"include/pico/async_context_freertos.h",
"include/pico/async_context_poll.h",
"include/pico/async_context_threadsafe_background.h",
],
includes = ["include"],
# Missing deps for:
# FreeRTOS.h
# semphr.h
# timers.h
tags = ["manual"],
target_compatible_with = select({
"//bazel/constraint:rp2": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
"//src/common/pico_base",
"//src/common/pico_time",
],
)

cc_library(
name = "pico_async_context_freertos",
srcs = ["async_context_freertos.c"],
hdrs = ["include/pico/async_context_freertos.h"],
includes = ["include"],
target_compatible_with = select({
"//bazel/constraint:rp2": [],
"//conditions:default": ["@platforms//:incompatible"],
}) + select({
"//bazel/constraint:pico_freertos_unset": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
deps = [
"//bazel/config:PICO_FREERTOS_LIB",
":pico_async_context",
"//src/common/pico_base",
"//src/common/pico_sync",
"//src/common/pico_time",
"//src/rp2_common/hardware_irq",
],
)

cc_library(
name = "pico_async_context_poll",
srcs = ["async_context_poll.c"],
hdrs = ["include/pico/async_context_poll.h"],
includes = ["include"],
target_compatible_with = select({
"//bazel/constraint:rp2": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
":pico_async_context",
"//src/common/pico_base",
"//src/common/pico_sync",
"//src/common/pico_time",
],
)


cc_library(
name = "pico_async_context_threadsafe_background",
srcs = ["async_context_threadsafe_background.c"],
hdrs = ["include/pico/async_context_threadsafe_background.h"],
includes = ["include"],
target_compatible_with = select({
"//bazel/constraint:rp2": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
":pico_async_context",
"//src/common/pico_base",
"//src/common/pico_sync",
"//src/common/pico_time",
Expand Down
90 changes: 71 additions & 19 deletions src/rp2_common/pico_btstack/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,32 +1,84 @@
package(default_visibility = ["//visibility:public"])

# Prefer these aliases to directly referencing @btstack, as it's possible that
# name may change.
alias(
name = "pico_btstack_base",
actual = "@btstack//:pico_btstack_base",
)

alias(
name = "pico_btstack_ble",
actual = "@btstack//:pico_btstack_ble",
)

alias(
name = "pico_btstack_classic",
actual = "@btstack//:pico_btstack_classic",
)

alias(
name = "pico_btstack_sbc_encoder",
actual = "@btstack//:pico_btstack_classic",
)

alias(
name = "pico_btstack_bnep_lwip",
actual = "@btstack//:pico_btstack_bnep_lwip",
)

alias(
name = "pico_btstack_bnep_lwip_sys_freertos",
actual = "@btstack//:pico_btstack_bnep_lwip_sys_freertos",
)

cc_library(
name = "pico_btstack",
srcs = [
"btstack_flash_bank.c",
"btstack_run_loop_async_context.c",
"btstack_stdin_pico.c",
],
hdrs = [
"include/pico/btstack_flash_bank.h",
"include/pico/btstack_run_loop_async_context.h",
name = "pico_btstack_flash_bank",
includes = ["include"],
hdrs = ["include/pico/btstack_flash_bank.h"],
srcs = ["btstack_flash_bank.c"],
deps = [
"//src/common/pico_base",
"//src/rp2_common/pico_flash",
":pico_btstack_base",
],
target_compatible_with = select({
"//bazel/constraint:cyw43_wireless": [],
"//bazel/constraint:is_pico_w": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)

cc_library(
name = "btstack_run_loop_async_context",
includes = ["include"],
# Missing deps for:
# btstack_config.h
# btstack_run_loop.h
# btstack_stdin.h
# hal_flash_bank.h
tags = ["manual"],
hdrs = ["include/pico/btstack_run_loop_async_context.h"],
srcs = ["btstack_run_loop_async_context.c"],
deps = [
"//src/rp2_common/hardware_sync",
"//src/rp2_common/pico_async_context:pico_async_context",
"@btstack//:pico_btstack_base",
],
target_compatible_with = select({
"//bazel/constraint:rp2": [],
"//bazel/constraint:cyw43_wireless": [],
"//bazel/constraint:is_pico_w": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)

cc_library(
name = "pico_btstack_stdin",
srcs = ["btstack_stdin_pico.c"],
target_compatible_with = select({
"//bazel/constraint:pico_btstack_config_unset": ["@platforms//:incompatible"],
"//conditions:default": [],
}) + select({
"//bazel/constraint:cyw43_wireless": [],
"//bazel/constraint:is_pico_w": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
"//src/common/pico_base",
"//src/rp2_common/hardware_sync",
"//src/rp2_common/pico_async_context",
"//src/rp2_common/pico_flash",
"//src/rp2_common/pico_stdio",
],
)
Loading

0 comments on commit fb7fa3d

Please sign in to comment.