Skip to content

Commit 1b64cac

Browse files
Extend build system checks (#2039)
* Adds check that ensures the Bazel and CMake Pico SDK version strings stay in sync. * Adds check that ensures the Bazel pins for external dependencies stays in sync with the git submodule pins. * Updates cyw43-driver pin in Bazel. * Makes the checks for some build configurability options non-blocking.
1 parent 06b7c1c commit 1b64cac

File tree

3 files changed

+156
-71
lines changed

3 files changed

+156
-71
lines changed

.github/workflows/bazel_build.yml

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,33 @@ jobs:
4343
# Checks that the current BCR-requested version of Picotool builds.
4444
- name: Bazel Picotool backwards compatibility
4545
run: bazel build @picotool//:picotool
46-
# Add back when it begins to pass.
47-
# other-bazel-checks:
48-
# runs-on: ubuntu-latest
49-
# steps:
50-
# - name: Checkout
51-
# uses: actions/checkout@v4
52-
# with:
53-
# fetch-depth: 0
54-
# - name: Get Bazel
55-
# uses: bazel-contrib/[email protected]
56-
# with:
57-
# # Avoid downloading Bazel every time.
58-
# bazelisk-cache: true
59-
# # Store build cache per workflow.
60-
# disk-cache: ${{ github.workflow }}
61-
# # Share repository cache between workflows.
62-
# repository-cache: true
63-
# # Only needed to drive the presbumit scripts.
64-
# - name: Setup Python
65-
# uses: actions/setup-python@v5
66-
# with:
67-
# python-version: '3.10'
68-
# - name: Fetch latest Picotool
69-
# uses: actions/checkout@v4
70-
# with:
71-
# repository: raspberrypi/picotool
72-
# ref: develop
73-
# fetch-depth: 0
74-
# path: lib/picotool
75-
# - name: Other Bazel checks
76-
# run: python3 tools/run_all_bazel_checks.py --program=other --picotool-dir=lib/picotool
46+
other-bazel-checks:
47+
runs-on: ubuntu-latest
48+
steps:
49+
- name: Checkout
50+
uses: actions/checkout@v4
51+
with:
52+
fetch-depth: 0
53+
- name: Get Bazel
54+
uses: bazel-contrib/[email protected]
55+
with:
56+
# Avoid downloading Bazel every time.
57+
bazelisk-cache: true
58+
# Store build cache per workflow.
59+
disk-cache: ${{ github.workflow }}
60+
# Share repository cache between workflows.
61+
repository-cache: true
62+
# Only needed to drive the presbumit scripts.
63+
- name: Setup Python
64+
uses: actions/setup-python@v5
65+
with:
66+
python-version: '3.10'
67+
- name: Fetch latest Picotool
68+
uses: actions/checkout@v4
69+
with:
70+
repository: raspberrypi/picotool
71+
ref: develop
72+
fetch-depth: 0
73+
path: lib/picotool
74+
- name: Other Bazel checks
75+
run: python3 tools/run_all_bazel_checks.py --program=other --picotool-dir=lib/picotool

MODULE.bazel

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,38 @@ http_archive(
8484
url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/mac-arm64/+/git_revision:2b0a708f41dd6291ee744704d43febc975e3d026",
8585
)
8686

87+
new_git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
88+
8789
# TODO: Provide tinyusb as a proper Bazel module.
88-
http_archive(
90+
new_git_repository(
8991
name = "tinyusb",
9092
build_file = "//src/rp2_common/tinyusb:tinyusb.BUILD",
91-
sha256 = "d64728aef58b80d5ce3747cad133f520da46e2b7ea3aadfda0e981aba6b658b3",
92-
strip_prefix = "tinyusb-4232642899362fa5e9cf0dc59bad6f1f6d32c563",
93-
url = "https://github.com/hathach/tinyusb/archive/4232642899362fa5e9cf0dc59bad6f1f6d32c563.tar.gz",
93+
commit = "4232642899362fa5e9cf0dc59bad6f1f6d32c563", # keep-in-sync-with-submodule: tinyusb
94+
remote = "https://github.com/hathach/tinyusb.git",
9495
)
9596

9697
# TODO: Provide btstack as a proper Bazel module.
97-
http_archive(
98+
new_git_repository(
9899
name = "btstack",
99100
build_file = "//src/rp2_common/pico_btstack:btstack.BUILD",
100-
sha256 = "64e86d9cf82b346e743fe1d4818b9380712b17abdb3f2c3524e92464b5ef3d19",
101-
strip_prefix = "btstack-2b49e57bd1fae85ac32ac1f41cdb7c794de335f6",
102-
url = "https://github.com/bluekitchen/btstack/archive/2b49e57bd1fae85ac32ac1f41cdb7c794de335f6.tar.gz",
101+
commit = "2b49e57bd1fae85ac32ac1f41cdb7c794de335f6", # keep-in-sync-with-submodule: btstack
102+
remote = "https://github.com/bluekitchen/btstack.git",
103103
)
104104

105-
# TODO: Provide btstack as a proper Bazel module.
106-
http_archive(
105+
# TODO: Provide cyw43-driver as a proper Bazel module.
106+
new_git_repository(
107107
name = "cyw43-driver",
108108
build_file = "//src/rp2_common/pico_cyw43_driver:cyw43-driver.BUILD",
109-
sha256 = "0fcc7707fef95dd562d5572604713266613a27caeeae2f10afeccee9592a53ce",
110-
strip_prefix = "cyw43-driver-faf36381bad1f668a30172b6336c9a970966ef4c",
111-
url = "https://github.com/georgerobotics/cyw43-driver/archive/faf36381bad1f668a30172b6336c9a970966ef4c.tar.gz",
109+
commit = "cf924bb04c8984675ca0fc2178f082e404e048c3", # keep-in-sync-with-submodule: cyw43-driver
110+
remote = "https://github.com/georgerobotics/cyw43-driver.git",
112111
)
113112

114113
# TODO: Provide lwip as a proper Bazel module.
115-
http_archive(
114+
new_git_repository(
116115
name = "lwip",
117116
build_file = "//src/rp2_common/pico_lwip:lwip.BUILD",
118-
sha256 = "72856d557f72911cf6826ef745c23c54822df83a474557823241164a1d1361aa",
119-
strip_prefix = "lwip-0a0452b2c39bdd91e252aef045c115f88f6ca773",
120-
url = "https://github.com/lwip-tcpip/lwip/archive/0a0452b2c39bdd91e252aef045c115f88f6ca773.tar.gz",
117+
commit = "0a0452b2c39bdd91e252aef045c115f88f6ca773", # keep-in-sync-with-submodule: lwip
118+
remote = "https://github.com/lwip-tcpip/lwip.git",
121119
)
122120

123121
register_toolchains(

tools/compare_build_systems.py

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import glob
1717
import logging
1818
import os
19+
from pathlib import Path
1920
import re
21+
import subprocess
2022
import sys
2123
from typing import Dict
2224

@@ -37,6 +39,12 @@
3739

3840
ATTR_REGEX = re.compile(r",?\s*(?P<key>[^=]+)=(?P<value>[^,]+)")
3941

42+
BAZEL_MODULE_REGEX = re.compile(r'\s*commit\s*=\s*\"(?P<commit>[0-9a-fA-F]+)\"\s*,\s*#\s*keep-in-sync-with-submodule:\s*(?P<dependency>\S*)')
43+
44+
BAZEL_VERSION_REGEX = re.compile(r'module\(\s*name\s*=\s*"pico-sdk",\s*version\s*=\s*"(?P<sdk_version>[^"]+)",?\s*\)')
45+
46+
CMAKE_VERSION_REGEX = re.compile(r'^[^#]*set\(PICO_SDK_VERSION_(?P<part>\S+)\s+(?P<value>\S+)\)')
47+
4048
# Sometimes the build systems are supposed to be implemented differently. This
4149
# allowlist permits the descriptions to differ between CMake and Bazel.
4250
BUILD_SYSTEM_DESCRIPTION_DIFFERENCE_ALLOWLIST = (
@@ -60,7 +68,7 @@
6068
"PICO_TOOLCHAIN_PATH",
6169
# Bazel uses native --platforms mechanics.
6270
"PICO_PLATFORM",
63-
# TODO: No built-in, pre-configured clang offering for Bazel yet.
71+
# Named PICO_TOOLCHAIN in Bazel.
6472
"PICO_COMPILER",
6573
# Entirely irrelevant to Bazel, use Bazel platforms:
6674
# https://bazel.build/extending/platforms
@@ -87,14 +95,17 @@
8795
"PICO_DEFAULT_PIOASM_OUTPUT_FORMAT",
8896
# Bazel always has picotool.
8997
"PICO_NO_PICOTOOL",
90-
# TODO: Eventualy support.
91-
"PICO_NO_COPRO_DIS",
92-
"PICO_DEFAULT_RP2350_PLATFORM",
93-
"PICO_GCC_TRIPLE",
94-
"PICO_NO_FLASH",
95-
"PICO_COPY_TO_RAM",
96-
"PICO_RP2350_ARM_S_CONFIG_HEADER_FILES",
97-
"PICO_RP2350_RISCV_CONFIG_HEADER_FILES",
98+
# These aren't supported as build flags in Bazel. Prefer to
99+
# set these in board header files like other SDK defines.
100+
"CYW43_DEFAULT_PIN_WL_REG_ON",
101+
"CYW43_DEFAULT_PIN_WL_DATA_OUT",
102+
"CYW43_DEFAULT_PIN_WL_DATA_IN",
103+
"CYW43_DEFAULT_PIN_WL_HOST_WAKE",
104+
"CYW43_DEFAULT_PIN_WL_CLOCK",
105+
"CYW43_DEFAULT_PIN_WL_CS",
106+
"CYW43_PIO_CLOCK_DIV_INT",
107+
"CYW43_PIO_CLOCK_DIV_FRAC",
108+
"CYW43_PIO_CLOCK_DIV_DYNAMIC",
98109
)
99110

100111
BAZEL_ONLY_ALLOWLIST = (
@@ -181,17 +192,17 @@ def FindKnownOptions(option_pattern_matcher, file_paths):
181192
return options
182193

183194

184-
def OptionsAreEqual(bazel_option, cmake_option):
195+
def OptionsAreEqual(bazel_option, cmake_option, warnings_as_errors):
185196
if bazel_option is None:
186197
if cmake_option.name in CMAKE_ONLY_ALLOWLIST:
187198
return True
188199
_LOG.warning(f" {cmake_option.name} does not exist in Bazel")
189-
return False
200+
return not warnings_as_errors
190201
elif cmake_option is None:
191202
if bazel_option.name in BAZEL_ONLY_ALLOWLIST:
192203
return True
193204
_LOG.warning(f" {bazel_option.name} does not exist in CMake")
194-
return False
205+
return not warnings_as_errors
195206
elif not bazel_option.matches(cmake_option):
196207
_LOG.error(" Bazel and CMAKE definitions do not match:")
197208
_LOG.error(f" [CMAKE] {bazel_option}")
@@ -201,7 +212,7 @@ def OptionsAreEqual(bazel_option, cmake_option):
201212
return True
202213

203214

204-
def CompareOptions(bazel_pattern, bazel_files, cmake_pattern, cmake_files):
215+
def CompareOptions(bazel_pattern, bazel_files, cmake_pattern, cmake_files, warnings_as_errors=True):
205216
bazel_options = FindKnownOptions(bazel_pattern, bazel_files)
206217
cmake_options = FindKnownOptions(cmake_pattern, cmake_files)
207218

@@ -210,10 +221,72 @@ def CompareOptions(bazel_pattern, bazel_files, cmake_pattern, cmake_files):
210221
both.update(bazel_options)
211222
both.update(cmake_options)
212223
for k in both.keys():
213-
if not OptionsAreEqual(bazel_options.get(k, None), cmake_options.get(k, None)):
224+
if not OptionsAreEqual(
225+
bazel_options.get(k, None),
226+
cmake_options.get(k, None),
227+
warnings_as_errors,
228+
):
214229
are_equal = False
215230
return are_equal
216231

232+
def CompareExternalDependencyVersions():
233+
pattern = re.compile(BAZEL_MODULE_REGEX)
234+
all_okay = True
235+
with open(Path(SDK_ROOT) / "MODULE.bazel", "r") as bazel_module_file:
236+
for line in bazel_module_file:
237+
maybe_match = pattern.match(line)
238+
if not maybe_match:
239+
continue
240+
241+
current_submodule_pin = subprocess.run(
242+
("git", "-C", SDK_ROOT, "rev-parse", f'HEAD:lib/{maybe_match.group("dependency")}'),
243+
text=True,
244+
check=True,
245+
capture_output=True,
246+
).stdout.strip()
247+
if current_submodule_pin != maybe_match.group("commit"):
248+
_LOG.error(" External pins for %s do not match:", maybe_match.group("dependency"))
249+
_LOG.error(" [CMAKE] %s", current_submodule_pin)
250+
_LOG.error(" [BAZEL] %s", maybe_match.group("commit"))
251+
all_okay = False
252+
else:
253+
_LOG.info(" External pins for %s match!", maybe_match.group("dependency"))
254+
255+
return all_okay
256+
257+
def CompareSdkVersion():
258+
# Find version string specified in Bazel.
259+
bazel_module_file_path = Path(SDK_ROOT) / "MODULE.bazel"
260+
bazel_module_file_contents = bazel_module_file_path.read_text()
261+
bazel_sdk_version = BAZEL_VERSION_REGEX.search(bazel_module_file_contents)
262+
if not bazel_sdk_version:
263+
_LOG.error(" Failed to find Bazel Pico SDK version string")
264+
return False
265+
bazel_version_string = bazel_sdk_version.group("sdk_version")
266+
267+
# Find version string specified in CMake.
268+
cmake_version_parts = {}
269+
with open(Path(SDK_ROOT) / "pico_sdk_version.cmake", "r") as cmake_version_file:
270+
for line in cmake_version_file:
271+
match = CMAKE_VERSION_REGEX.match(line)
272+
if match:
273+
cmake_version_parts[match.group("part")] = match.group("value")
274+
if len(cmake_version_parts) < 3:
275+
_LOG.error(" Failed to find CMake Pico SDK version string")
276+
return False
277+
cmake_version_string = ".".join((
278+
cmake_version_parts["MAJOR"],
279+
cmake_version_parts["MINOR"],
280+
cmake_version_parts["REVISION"],
281+
))
282+
if "PRE_RELEASE_ID" in cmake_version_parts:
283+
cmake_version_string += "-" + cmake_version_parts["PRE_RELEASE_ID"]
284+
285+
if cmake_version_string != bazel_version_string:
286+
_LOG.error(" Declared CMake SDK version is %s and Bazel is %s", cmake_version_string, bazel_version_string)
287+
return False
288+
289+
return True
217290

218291
def compare_build_systems():
219292
cmake_files = [
@@ -227,20 +300,35 @@ def compare_build_systems():
227300
for f in glob.glob(os.path.join(SDK_ROOT, p), recursive=True)
228301
]
229302

230-
_LOG.info("[1/2] Checking build system configuration flags...")
231-
build_options_ok = CompareOptions(
232-
"PICO_BAZEL_CONFIG", bazel_files, "PICO_CMAKE_CONFIG", cmake_files
233-
)
303+
results = []
304+
_LOG.info("[1/3] Checking build system configuration flags...")
305+
results.append(CompareOptions(
306+
"PICO_BAZEL_CONFIG",
307+
bazel_files,
308+
"PICO_CMAKE_CONFIG",
309+
cmake_files,
310+
# For now, allow CMake and Bazel to go out of sync when it comes to
311+
# build configurability since it's a big ask to make contributors
312+
# implement the same functionality in both builds.
313+
warnings_as_errors=False,
314+
))
234315

235-
_LOG.info("[2/2] Checking build system defines...")
236-
build_defines_ok = CompareOptions(
316+
_LOG.info("[2/4] Checking build system defines...")
317+
results.append(CompareOptions(
237318
"PICO_BUILD_DEFINE", bazel_files, "PICO_BUILD_DEFINE", cmake_files
238-
)
319+
))
320+
321+
_LOG.info("[3/4] Checking submodule pins...")
322+
results.append(CompareExternalDependencyVersions())
323+
324+
_LOG.info("[4/4] Checking version strings...")
325+
results.append(CompareSdkVersion())
239326

240-
if build_options_ok and build_defines_ok:
241-
_LOG.info("OK")
327+
if False not in results:
328+
_LOG.info("Passed with no blocking failures")
242329
return 0
243330

331+
_LOG.error("One or more blocking failures detected")
244332
return 1
245333

246334

0 commit comments

Comments
 (0)