diff --git a/bazel/BUILD.bazel b/bazel/BUILD.bazel index 037b513ca..e4ab8f31d 100644 --- a/bazel/BUILD.bazel +++ b/bazel/BUILD.bazel @@ -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( diff --git a/bazel/config/BUILD.bazel b/bazel/config/BUILD.bazel index bfd294114..88dc5d3cc 100644 --- a/bazel/config/BUILD.bazel +++ b/bazel/config/BUILD.bazel @@ -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 diff --git a/bazel/defs.bzl b/bazel/defs.bzl index 14364ad5a..0e9df1a26 100644 --- a/bazel/defs.bzl +++ b/bazel/defs.bzl @@ -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: @@ -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 + ) diff --git a/bazel/util/transition.bzl b/bazel/util/transition.bzl index 2a5e32c54..d0baf0d6e 100644 --- a/bazel/util/transition.bzl +++ b/bazel/util/transition.bzl @@ -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: diff --git a/src/boards/BUILD.bazel b/src/boards/BUILD.bazel index 2390ad077..ba0dfa27d 100644 --- a/src/boards/BUILD.bazel +++ b/src/boards/BUILD.bazel @@ -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"]) @@ -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 ] diff --git a/src/common/pico_base/BUILD.bazel b/src/common/pico_base/BUILD.bazel index e05d91605..73fd1f2a4 100644 --- a/src/common/pico_base/BUILD.bazel +++ b/src/common/pico_base/BUILD.bazel @@ -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"]) @@ -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"], -) diff --git a/src/rp2_common/cmsis/BUILD.bazel b/src/rp2_common/cmsis/BUILD.bazel index f7fca7be7..c3787295f 100644 --- a/src/rp2_common/cmsis/BUILD.bazel +++ b/src/rp2_common/cmsis/BUILD.bazel @@ -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", ], @@ -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", diff --git a/src/rp2_common/pico_standard_link/BUILD.bazel b/src/rp2_common/pico_standard_link/BUILD.bazel index 1117b2752..536909e66 100644 --- a/src/rp2_common/pico_standard_link/BUILD.bazel +++ b/src/rp2_common/pico_standard_link/BUILD.bazel @@ -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",