Skip to content

Commit 07d6dc1

Browse files
[Bazel] Fix bazel build, add presubmit (#1973)
* [Bazel] Fix bazel build, add presubmit * Fixes a missing dep in the Bazel build breaking the host build. * Automagically finds all board headers. * Improves presubmit script polish for GH Action readiness. * Adds a GitHub action workflow for the Bazel build. * Disable failing checks * Disables Windows, as there's a mix of real build errors and overly-ambitious checks that don't work on Windows. * Disables extra checks temporarily since it's currently failing.
1 parent 03a82f3 commit 07d6dc1

File tree

8 files changed

+150
-119
lines changed

8 files changed

+150
-119
lines changed

.github/workflows/bazel_build.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Bazel presubmit checks
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
bazel-build-check:
9+
strategy:
10+
matrix:
11+
# TODO: Windows is currently broken.
12+
os: [ubuntu-latest, macos-latest]
13+
fail-fast: false
14+
runs-on: ${{ matrix.os }}
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
- name: Get Bazel
21+
uses: bazel-contrib/[email protected]
22+
with:
23+
# Avoid downloading Bazel every time.
24+
bazelisk-cache: true
25+
# Store build cache per workflow.
26+
disk-cache: ${{ github.workflow }}
27+
# Share repository cache between workflows.
28+
repository-cache: true
29+
# Only needed to drive the presbumit scripts.
30+
- name: Setup Python
31+
uses: actions/setup-python@v5
32+
with:
33+
python-version: '3.10'
34+
- name: Fetch latest Picotool
35+
uses: actions/checkout@v4
36+
with:
37+
repository: raspberrypi/picotool
38+
ref: develop
39+
fetch-depth: 0
40+
path: lib/picotool
41+
- name: Full Bazel build with develop Picotool
42+
run: python3 tools/run_all_bazel_checks.py --program=build --picotool-dir=lib/picotool
43+
# Checks that the current BCR-requested version of Picotool builds.
44+
- name: Bazel Picotool backwards compatibility
45+
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

src/boards/BUILD.bazel

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,15 @@ load("//bazel/util:multiple_choice_flag.bzl", "declare_flag_choices", "flag_choi
33

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

6-
# Known board choices:
6+
# Find all boards.
7+
BOARD_CHOICE_FILES = glob(["include/boards/*.h"])
8+
9+
# Extract just the name of the board.
710
BOARD_CHOICES = [
8-
"0xcb_helios",
9-
"adafruit_feather_rp2040_usb_host",
10-
"adafruit_feather_rp2040",
11-
"adafruit_feather_rp2350",
12-
"adafruit_itsybitsy_rp2040",
13-
"adafruit_kb2040",
14-
"adafruit_macropad_rp2040",
15-
"adafruit_qtpy_rp2040",
16-
"adafruit_trinkey_qt2040",
17-
"amethyst_fpga",
18-
"archi",
19-
"arduino_nano_rp2040_connect",
20-
"cytron_maker_pi_rp2040",
21-
"datanoisetv_rp2040_dsp",
22-
"datanoisetv_rp2350_dsp",
23-
"defcon32_badge",
24-
"eetree_gamekit_rp2040",
25-
"garatronic_pybstick26_rp2040",
26-
"gen4_rp2350_24",
27-
"gen4_rp2350_24ct",
28-
"gen4_rp2350_24t",
29-
"gen4_rp2350_28",
30-
"gen4_rp2350_28ct",
31-
"gen4_rp2350_28t",
32-
"gen4_rp2350_32",
33-
"gen4_rp2350_32ct",
34-
"gen4_rp2350_32t",
35-
"gen4_rp2350_35",
36-
"gen4_rp2350_35ct",
37-
"gen4_rp2350_35t",
38-
"hellbender_2350A_devboard",
39-
"ilabs_challenger_rp2350_bconnect",
40-
"ilabs_challenger_rp2350_wifi_ble",
41-
"ilabs_opendec02",
42-
"melopero_perpetuo_rp2350_lora",
43-
"melopero_shake_rp2040",
44-
"metrotech_xerxes_rp2040",
45-
"net8086_usb_interposer",
46-
"none",
47-
"nullbits_bit_c_pro",
48-
"phyx_rick_tny_rp2350",
49-
"pi-plates_micropi",
50-
"pico_w",
51-
"pico",
52-
"pico2",
53-
"pimoroni_badger2040",
54-
"pimoroni_interstate75",
55-
"pimoroni_keybow2040",
56-
"pimoroni_motor2040",
57-
"pimoroni_pga2040",
58-
"pimoroni_pga2350",
59-
"pimoroni_pico_plus2_rp2350",
60-
"pimoroni_picolipo_16mb",
61-
"pimoroni_picolipo_4mb",
62-
"pimoroni_picosystem",
63-
"pimoroni_plasma2040",
64-
"pimoroni_plasma2350",
65-
"pimoroni_servo2040",
66-
"pimoroni_tiny2040_2mb",
67-
"pimoroni_tiny2040",
68-
"pimoroni_tiny2350",
69-
"pololu_3pi_2040_robot",
70-
"pololu_zumo_2040_robot",
71-
"seeed_xiao_rp2040",
72-
"seeed_xiao_rp2350",
73-
"solderparty_rp2040_stamp_carrier",
74-
"solderparty_rp2040_stamp_round_carrier",
75-
"solderparty_rp2040_stamp",
76-
"solderparty_rp2350_stamp_xl",
77-
"solderparty_rp2350_stamp",
78-
"sparkfun_micromod",
79-
"sparkfun_promicro_rp2350",
80-
"sparkfun_promicro",
81-
"sparkfun_thingplus",
82-
"switchscience_picossci2_conta_base",
83-
"switchscience_picossci2_dev_board",
84-
"switchscience_picossci2_micro",
85-
"switchscience_picossci2_rp2350_breakout",
86-
"switchscience_picossci2_tiny",
87-
"tinycircuits_thumby_color_rp2350",
88-
"vgaboard",
89-
"waveshare_rp2040_lcd_0.96",
90-
"waveshare_rp2040_lcd_1.28",
91-
"waveshare_rp2040_one",
92-
"waveshare_rp2040_plus_16mb",
93-
"waveshare_rp2040_plus_4mb",
94-
"waveshare_rp2040_zero",
95-
"weact_studio_rp2040_16mb",
96-
"weact_studio_rp2040_2mb",
97-
"weact_studio_rp2040_4mb",
98-
"weact_studio_rp2040_8mb",
99-
"wiznet_w5100s_evb_pico",
11+
path.removeprefix("include/boards/").removesuffix(".h")
12+
for path in BOARD_CHOICE_FILES
10013
]
10114

102-
BOARD_CHOICE_FILES = ["include/boards/" + c + ".h" for c in BOARD_CHOICES]
103-
10415
BOARD_CHOICE_MAP = {c: [":{}".format(c)] for c in BOARD_CHOICES}
10516

10617
# PICO_BUILD_DEFINE: PICO_BOARD, Name of board, type=string, default=CMake PICO_BOARD variable, group=pico_base

src/host/hardware_timer/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ cc_library(
1515
defines = _DEFINES,
1616
includes = ["include"],
1717
target_compatible_with = ["//bazel/constraint:host"],
18-
visibility = ["//src/common/pico_time:__pkg__"],
18+
visibility = [
19+
"//src/common/pico_time:__pkg__",
20+
"//src/host/pico_platform:__pkg__",
21+
],
1922
deps = ["//src/common/pico_base_headers"],
2023
)
2124

src/host/pico_platform/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ cc_library(
3030
":pico_platform_internal",
3131
":platform_defs",
3232
"//src/common/pico_base_headers",
33+
"//src/host/hardware_timer:hardware_timer_headers",
3334
],
3435
)

tools/bazel_build.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
override_picotool_arg,
1616
parse_common_args,
1717
print_framed_string,
18+
print_to_stderr,
1819
run_bazel,
1920
setup_logging,
2021
)
@@ -196,12 +197,12 @@ def build_all_configurations(picotool_dir):
196197
)
197198
if result.returncode != 0:
198199
failed_builds.append(config["name"])
199-
print()
200+
print_to_stderr()
200201

201202
if failed_builds:
202203
print_framed_string("ERROR: One or more builds failed.")
203204
for build in failed_builds:
204-
print(f" * FAILED: {build} build")
205+
print_to_stderr(f" * FAILED: {build} build")
205206
return 1
206207

207208
print_framed_string("All builds successfully passed!")

tools/bazel_common.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import shlex
1414
import shutil
1515
import subprocess
16+
import sys
1617

1718

1819
_LOG = logging.getLogger(__file__)
@@ -34,13 +35,16 @@
3435

3536
def parse_common_args():
3637
parser = argparse.ArgumentParser()
38+
add_common_args(parser)
39+
return parser.parse_args()
40+
41+
def add_common_args(parser):
3742
parser.add_argument(
3843
"--picotool-dir",
3944
help="Use a local copy of Picotool rather than the dynamically fetching it",
4045
default=None,
4146
type=Path,
4247
)
43-
return parser.parse_args()
4448

4549
def override_picotool_arg(picotool_dir):
4650
return f"--override_module=picotool={picotool_dir.resolve()}"
@@ -49,7 +53,7 @@ def override_picotool_arg(picotool_dir):
4953
def bazel_command() -> str:
5054
"""Return the path to bazelisk or bazel."""
5155
if shutil.which("bazelisk"):
52-
return "bazelisk"
56+
return shutil.which("bazelisk")
5357
if shutil.which("bazel"):
5458
return "bazel"
5559

@@ -93,12 +97,16 @@ def run_bazel(args, check=False, **kwargs):
9397
return proc
9498

9599

100+
def print_to_stderr(*args, **kwargs):
101+
print(*args, file=sys.stderr, **kwargs)
102+
103+
96104
def print_framed_string(s):
97105
"""Frames a string of text and prints it to highlight script steps."""
98106
header_spacer = "#" * (len(s) + 12)
99-
print(header_spacer)
100-
print("### " + s + " ###")
101-
print(header_spacer)
107+
print_to_stderr(header_spacer)
108+
print_to_stderr("### " + s + " ###")
109+
print_to_stderr(header_spacer)
102110

103111

104112
def setup_logging():

tools/compare_build_systems.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414

1515
from dataclasses import dataclass
1616
import glob
17+
import logging
1718
import os
1819
import re
1920
import sys
2021
from typing import Dict
2122

22-
from bazel_common import SDK_ROOT
23+
from bazel_common import SDK_ROOT, setup_logging
24+
25+
_LOG = logging.getLogger(__file__)
2326

2427
CMAKE_FILE_TYPES = (
2528
"**/CMakeLists.txt",
@@ -182,17 +185,17 @@ def OptionsAreEqual(bazel_option, cmake_option):
182185
if bazel_option is None:
183186
if cmake_option.name in CMAKE_ONLY_ALLOWLIST:
184187
return True
185-
print(f" {cmake_option.name} does not exist in Bazel")
188+
_LOG.warning(f" {cmake_option.name} does not exist in Bazel")
186189
return False
187190
elif cmake_option is None:
188191
if bazel_option.name in BAZEL_ONLY_ALLOWLIST:
189192
return True
190-
print(f" {bazel_option.name} does not exist in CMake")
193+
_LOG.warning(f" {bazel_option.name} does not exist in CMake")
191194
return False
192195
elif not bazel_option.matches(cmake_option):
193-
print(" Bazel and CMAKE definitions do not match:")
194-
print(f" [CMAKE] {bazel_option}")
195-
print(f" [BAZEL] {cmake_option}")
196+
_LOG.error(" Bazel and CMAKE definitions do not match:")
197+
_LOG.error(f" [CMAKE] {bazel_option}")
198+
_LOG.error(f" [BAZEL] {cmake_option}")
196199
return False
197200

198201
return True
@@ -224,22 +227,23 @@ def compare_build_systems():
224227
for f in glob.glob(os.path.join(SDK_ROOT, p), recursive=True)
225228
]
226229

227-
print("[1/2] Checking build system configuration flags...")
230+
_LOG.info("[1/2] Checking build system configuration flags...")
228231
build_options_ok = CompareOptions(
229232
"PICO_BAZEL_CONFIG", bazel_files, "PICO_CMAKE_CONFIG", cmake_files
230233
)
231234

232-
print("[2/2] Checking build system defines...")
235+
_LOG.info("[2/2] Checking build system defines...")
233236
build_defines_ok = CompareOptions(
234237
"PICO_BUILD_DEFINE", bazel_files, "PICO_BUILD_DEFINE", cmake_files
235238
)
236239

237240
if build_options_ok and build_defines_ok:
238-
print("OK")
241+
_LOG.info("OK")
239242
return 0
240243

241244
return 1
242245

243246

244247
if __name__ == "__main__":
248+
setup_logging()
245249
sys.exit(compare_build_systems())

0 commit comments

Comments
 (0)