Skip to content

Commit

Permalink
Improve PICO_CONFIG_HEADER correctness in Bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
armandomontanez committed Jun 12, 2024
1 parent 13e92a9 commit 048e206
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 58 deletions.
15 changes: 15 additions & 0 deletions bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ cc_library(
],
)

genrule(
name = "empty_extra_headers_file",
outs = ["generated_extra_include/pico_config_extra_headers.h"],
cmd = "echo > $@",
cmd_bat = "copy NUL $@",
visibility = ["//visibility:private"],
)

cc_library(
name = "no_extra_headers",
hdrs = ["generated_extra_include/pico_config_extra_headers.h"],
includes = ["generated_extra_include"],
visibility = ["//visibility:private"],
)

# An empty stub, useful for label_flag flags that need to point to a library,
# but for some purposes the library needs to be a no-op.
cc_library(
Expand Down
10 changes: 5 additions & 5 deletions bazel/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,22 @@ bool_flag(
build_setting_default = False,
)

# PICO_BAZEL_CONFIG: PICO_CONFIG_EXTRA_HEADER, [Bazel only] The cc_library that provides "pico_config_extra_headers.h", default=//src/common/pico_base:no_extra_headers, group=pico_base
# PICO_BAZEL_CONFIG: PICO_CONFIG_EXTRA_HEADER, [Bazel only] The cc_library that provides "pico_config_extra_headers.h", default=//bazel:no_extra_headers, group=pico_base
label_flag(
name = "PICO_CONFIG_EXTRA_HEADER",
build_setting_default = "//src/common/pico_base:no_extra_headers",
build_setting_default = "//bazel:no_extra_headers",
)

# PICO_BAZEL_CONFIG: PICO_CONFIG_PLATFORM_HEADER, [Bazel only] The cc_library that provides "pico_config_platform_headers.h", default=//src/common/pico_base:default_platform_headers, group=pico_base
label_flag(
name = "PICO_CONFIG_PLATFORM_HEADER",
build_setting_default = "//src/common/pico_base:default_platform_headers",
build_setting_default = "//src/boards:default",
)

# PICO_BAZEL_CONFIG: PICO_CONFIG_HEADER, [Bazel only] The cc_library that defines PICO_BOARD, PICO_CONFIG_HEADER, and other SDK critical defines (overrides PICO_BOARD setting), default=//src/boards:default, group=pico_base
# PICO_BAZEL_CONFIG: PICO_CONFIG_HEADER, [Bazel only] The cc_library that defines PICO_BOARD, PICO_CONFIG_HEADER, and other SDK critical defines (overrides PICO_BOARD setting), default=//bazel:generate_config_header, group=pico_base
label_flag(
name = "PICO_CONFIG_HEADER",
build_setting_default = "//src/boards:default",
build_setting_default = "//bazel:generate_config_header",
)

# PICO_BAZEL_CONFIG: PICO_BTSTACK_CONFIG, [Bazel only] The cc_library that provides btstack_config.h, default=//bazel:empty_cc_lib, group=wireless
Expand Down
27 changes: 27 additions & 0 deletions bazel/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_cc//cc:defs.bzl", "cc_library")

def _pico_generate_pio_header_impl(ctx):
generated_headers = []
for f in ctx.files.srcs:
Expand Down Expand Up @@ -87,3 +90,27 @@ def compatible_with_pico_w():
"@pico-sdk//bazel/constraint:is_pico_w": [],
"//conditions:default": ["@platforms//:incompatible"],
})

def pico_board_config(name, platform_includes, **kwargs):
"""A helper macro for declaring a Pico board to use with PICO_CONFIG_HEADER.
This generates pico_config_platform_headers.h using the list of
includes provided in `platform_includes`, and the final artifact is
a cc_library that you can configure //bazel/config:PICO_CONFIG_HEADER to
point to.
"""
_hdr_dir = "{}_generated_includes".format(name)
_hdr_path = "{}/pico_config_platform_headers.h".format(_hdr_dir)
write_file(
name = "{}_platform_headers_file".format(name),
out = _hdr_path,
content = ['#include "{}"'.format(inc) for inc in platform_includes],
)
kwargs.setdefault("hdrs", [])
kwargs["hdrs"].append(_hdr_path)
kwargs.setdefault("includes", [])
kwargs["includes"].append(_hdr_dir)
cc_library(
name = name,
**kwargs
)
1 change: 1 addition & 0 deletions bazel/util/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def declare_transtion(attrs, flag_overrides = None, append_to_flags = None, exec
from. The common `src` attr tells the transition which build rule to apply
the transition to.
"""

def _flag_override_impl(settings, attrs):
final_overrides = {}
if flag_overrides != None:
Expand Down
9 changes: 7 additions & 2 deletions src/boards/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("//bazel:defs.bzl", "pico_board_config")
load("//bazel/util:multiple_choice_flag.bzl", "declare_flag_choices", "flag_choice")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -62,14 +63,18 @@ BOARD_CHOICE_MAP = {c: [":{}".format(c)] for c in BOARD_CHOICES}

# PICO_BUILD_DEFINE: PICO_BOARD, Name of board, type=string, default=CMake PICO_BOARD variable, group=pico_base
[
cc_library(
pico_board_config(
name = board,
hdrs = BOARD_CHOICE_FILES,
defines = [
'PICO_BOARD=\\"{}\\"'.format(board),
'PICO_CONFIG_HEADER="boards/{}.h"'.format(board),
],
includes = ["include"],
platform_includes = [
"cmsis/rename_exceptions.h",
"boards/{}.h".format(board),
],
deps = ["//src/rp2_common/cmsis:rename_exceptions"],
)
for board in BOARD_CHOICES
]
Expand Down
47 changes: 0 additions & 47 deletions src/common/pico_base/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@bazel_skylib//rules:write_file.bzl", "write_file")

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

Expand Down Expand Up @@ -154,49 +153,3 @@ cc_library(
":pico_base_interface",
],
)

genrule(
name = "empty_extra_headers_file",
outs = ["generated_include/pico_config_extra_headers.h"],
cmd = "echo > $@",
cmd_bat = "copy NUL $@",
visibility = ["//visibility:private"],
)

_RP2040_DEFAULT_HEADER_CONTENTS = [
'#include "cmsis/rename_exceptions.h"',
]

write_file(
name = "rp2040_default_platform_headers_file",
out = "generated_rp2040_include/pico_config_platform_headers.h",
content = _RP2040_DEFAULT_HEADER_CONTENTS,
)

genrule(
name = "empty_paltform_headers_file",
outs = ["generated_include/pico_config_platform_headers.h"],
cmd = "echo > $@",
cmd_bat = "copy NUL $@",
visibility = ["//visibility:private"],
)

cc_library(
name = "no_extra_headers",
hdrs = ["generated_include/pico_config_extra_headers.h"],
includes = ["generated_include"],
visibility = ["//visibility:private"],
)

cc_library(
name = "default_platform_headers",
hdrs = select({
"//bazel/constraint:host": ["generated_include/pico_config_platform_headers.h"],
"//conditions:default": ["generated_rp2040_include/pico_config_platform_headers.h"],
}),
includes = select({
"//bazel/constraint:host": ["generated_include"],
"//conditions:default": ["generated_rp2040_include"],
}),
visibility = ["//visibility:private"],
)
10 changes: 6 additions & 4 deletions src/rp2_common/cmsis/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ cc_library(
name = "rename_exceptions",
hdrs = ["include/cmsis/rename_exceptions.h"],
# This is mildly odd, but intentional. We really don't want this header
# to have extra deps, but this should always be defined.
defines = ["LIB_CMSIS_CORE=1"],
# to have extra deps, and this header is touched by the host build.
defines = select({
"//bazel/constraint:host": [],
"//conditions:default": ["LIB_CMSIS_CORE=1"],
}),
includes = ["include"],
target_compatible_with = compatible_with_rp2(),
)

cc_library(
name = "cmsis_core",
defines = ["LIB_CMSIS_CORE=1"],
srcs = [
"stub/CMSIS/Device/RaspberryPi/RP2040/Source/system_RP2040.c",
],
Expand All @@ -33,6 +34,7 @@ cc_library(
"stub/CMSIS/Device/RaspberryPi/RP2040/Include/RP2040.h",
"stub/CMSIS/Device/RaspberryPi/RP2040/Include/system_RP2040.h",
],
defines = ["LIB_CMSIS_CORE=1"],
includes = [
"stub/CMSIS/Core/Include",
"stub/CMSIS/Device/RaspberryPi/RP2040/Include",
Expand Down
1 change: 1 addition & 0 deletions src/rp2_common/pico_standard_link/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ cc_library(
"//bazel/config:PICO_DEFAULT_BINARY_INFO",
"//bazel/config:PICO_DEFAULT_LINKER_SCRIPT",
"//src/common/pico_base:pico_base_interface",
"//src/rp2_common/cmsis:cmsis_core",
"//src/rp2_common/pico_bootrom",
"//src/rp2_common/pico_platform",
"//src/rp2_common/pico_platform:hardware_regs",
Expand Down

0 comments on commit 048e206

Please sign in to comment.