Skip to content

Commit

Permalink
targets/rp2040: Add bazel support for rp2040_utils pylib
Browse files Browse the repository at this point in the history
Bug: 342634966
Change-Id: I0f94732f5f697bd8355e41f04dd2af966e8e3348
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/211591
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Armando Montanez <[email protected]>
Reviewed-by: Taylor Cramer <[email protected]>
Commit-Queue: Erik Gilling <[email protected]>
  • Loading branch information
konkers authored and CQ Bot Account committed Jun 12, 2024
1 parent d1d6d43 commit 946880d
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ build:rp2040 --//pw_log:backend=@pigweed//pw_log_tokenized
build:rp2040 --//pw_log:backend_impl=@pigweed//pw_log_tokenized:impl
build:rp2040 --//pw_log_tokenized:handler_backend=@pigweed//pw_system:log_backend
build:rp2040 --//pw_system:extra_platform_libs=@pigweed//targets/rp2040:extra_platform_libs
build:rp2040 --//pw_unit_test:main=//targets/rp2040:unit_test_app
test:rp2040 --run_under=//targets/rp2040/py:unit_test_client

# Config for the stm32f429i_disc1 baremetal platform.
#
Expand Down
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ http_archive(
],
)

http_archive(
name = "rules_platform",
sha256 = "0aadd1bd350091aa1f9b6f2fbcac8cd98201476289454e475b28801ecf85d3fd",
urls = [
"https://github.com/bazelbuild/rules_platform/releases/download/0.1.0/rules_platform-0.1.0.tar.gz",
],
)

http_archive(
name = "rules_cc",
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
Expand Down
1 change: 1 addition & 0 deletions pw_cli/py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ py_library(
"pw_cli/tool_runner.py",
],
imports = ["."],
deps = ["@python_packages_psutil//:pkg"],
)

py_binary(
Expand Down
10 changes: 0 additions & 10 deletions pw_sys_io_rp2040/sys_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ void LazyInitSysIo() {
}
}

// Spin until host connects.
void WaitForConnect() {
// In order to stop this sleep polling, we could register a shared IRQ handler
// for the USB interrupt and block on a signal from that.
while (!stdio_usb_connected()) {
sleep_ms(50);
}
}

} // namespace

// This whole implementation is very inefficient because it only reads / writes
Expand All @@ -56,7 +47,6 @@ namespace pw::sys_io {

Status ReadByte(std::byte* dest) {
LazyInitSysIo();
WaitForConnect();

while (true) {
int c = getchar_timeout_us(0);
Expand Down
1 change: 1 addition & 0 deletions pw_unit_test/py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ py_library(
srcs = [
"pw_unit_test/__init__.py",
"pw_unit_test/rpc.py",
"pw_unit_test/serial_test_runner.py",
"pw_unit_test/test_runner.py",
],
imports = ["."],
Expand Down
18 changes: 7 additions & 11 deletions targets/rp2040/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,18 @@ constraint_value(
constraint_setting = "@freertos//:freertos_config_setting",
)

# This is just a stub to silence warnings saying that pico_logging_test_main.cc
# is missing from the bazel build. There's no plans yet to do a Bazel build for
# the Pi Pico.
#
# TODO: b/260639642 - Support Pico in the Bazel build.
cc_library(
name = "pico_logging_test_main",
name = "unit_test_app",
testonly = True,
srcs = [
"pico_logging_test_main.cc",
"unit_test_app.cc",
],
tags = ["manual"],
deps = [
"//pw_unit_test",
"//pw_unit_test:logging_event_handler",
"//pw_status",
"//pw_system",
"//pw_unit_test:rpc_service",
],
alwayslink = True,
)

# This is just a stub to silence warnings saying that boot.cc and *.h files are
Expand All @@ -84,7 +81,6 @@ filegroup(
srcs = [
"config/pigweed_module_config.h",
"config/rp2040_hal_config.h",
"unit_test_app.cc",
],
)

Expand Down
98 changes: 98 additions & 0 deletions targets/rp2040/py/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_platform//platform_data:defs.bzl", "platform_data")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")

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

py_library(
name = "rp2040_utils",
srcs = [
"rp2040_utils/__init__.py",
"rp2040_utils/device_detector.py",
"rp2040_utils/unit_test_client.py",
"rp2040_utils/unit_test_runner.py",
"rp2040_utils/unit_test_server.py",
],
data = ["@libusb//:libusb_dynamic"],
imports = ["."],
tags = ["manual"],
deps = [
"//pw_cli/py:pw_cli",
"//pw_hdlc/py:pw_hdlc",
"//pw_log:log_proto_py_pb2",
"//pw_log/py:pw_log",
"//pw_log_rpc/py:pw_log_rpc",
"//pw_system/py:pw_system_lib",
"//pw_tokenizer:tokenizer_proto_py_pb2",
"//pw_tokenizer/py:pw_tokenizer",
"//pw_unit_test:unit_test_py_pb2",
"//pw_unit_test/py:pw_unit_test",
"@python_packages_pyusb//:pkg",
"@rules_python//python/runfiles",
],
)

py_binary(
name = "rpc_unit_test_runner",
srcs = [
"rp2040_utils/rpc_unit_test_runner.py",
],
data = [
"//third_party/probe-rs",
],
main = "rp2040_utils/rpc_unit_test_runner.py",
tags = ["manual"],
deps = [
":rp2040_utils",
],
)

py_binary(
name = "unit_test_server",
srcs = [
"rp2040_utils/unit_test_server.py",
],
data = [
":rpc_unit_test_runner",
"//pw_target_runner/go/cmd:server",
],
main = "rp2040_utils/unit_test_server.py",
tags = ["manual"],
deps = [
":rp2040_utils",
"@rules_python//python/runfiles",
],
)

py_binary(
name = "_unit_test_client",
srcs = [
"rp2040_utils/unit_test_client.py",
],
data = ["//pw_target_runner/go/cmd:client"],
main = "rp2040_utils/unit_test_client.py",
tags = ["manual"],
deps = [
":rp2040_utils",
"@rules_python//python/runfiles",
],
)

platform_data(
name = "unit_test_client",
platform = "@local_config_platform//:host",
target = ":_unit_test_client",
)
13 changes: 13 additions & 0 deletions targets/rp2040/py/rp2040_utils/device_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ def _custom_find_library(name: str) -> str | None:
if cipd_lib.is_dir():
search_paths.append(cipd_lib)

# libusb provided by Bazel
try:
# pylint: disable=import-outside-toplevel
from rules_python.python.runfiles import runfiles # type: ignore

r = runfiles.Create()
libusb_dir = os.path.dirname(
r.Rlocation(f'libusb/libusb-1.0{_LIB_SUFFIX}')
)
search_paths.append(Path(libusb_dir))
except ImportError:
pass

_LOG.debug('Potential shared library search paths:')
for path in search_paths:
_LOG.debug(path)
Expand Down
12 changes: 11 additions & 1 deletion targets/rp2040/py/rp2040_utils/unit_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@
# Load from this directory if rp2040_utils is not available.
import unit_test_server # type: ignore

_TARGET_CLIENT_COMMAND = 'pw_target_runner_client'
# If the script is being run through Bazel, our client is provided at a well
# known location in its runfiles.
try:
from rules_python.python.runfiles import runfiles # type: ignore

r = runfiles.Create()
_TARGET_CLIENT_COMMAND = r.Rlocation(
'pigweed/pw_target_runner/go/cmd/client_/client'
)
except ImportError:
_TARGET_CLIENT_COMMAND = 'pw_target_runner_client'


def parse_args():
Expand Down
20 changes: 17 additions & 3 deletions targets/rp2040/py/rp2040_utils/unit_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@

_LOG = logging.getLogger()

# If the script is being run through Bazel, our support binaries are provided
# at well known locations in its runfiles.
try:
from rules_python.python.runfiles import runfiles # type: ignore

r = runfiles.Create()
_PROBE_RS_COMMAND = r.Rlocation('pigweed/third_party/probe-rs/probe-rs')
_PICOTOOL_COMMAND = 'picotool'
except ImportError:
_PROBE_RS_COMMAND = 'probe-rs'
_PICOTOOL_COMMAND = 'picotool'


def parse_args():
"""Parses command-line arguments."""
Expand Down Expand Up @@ -153,7 +165,7 @@ def load_debugprobe_binary(self, binary: Path) -> bool:
)

download_cmd = (
'probe-rs',
_PROBE_RS_COMMAND,
'download',
'--probe',
probe,
Expand All @@ -176,8 +188,10 @@ def load_debugprobe_binary(self, binary: Path) -> bool:
_LOG.error('\n\n'.join(err))
return False

# `probe-rs download` leaves the device halted so it needs to be reset
# to run.
reset_cmd = (
'probe-rs',
_PROBE_RS_COMMAND,
'reset',
'--probe',
probe,
Expand Down Expand Up @@ -207,7 +221,7 @@ def load_picotool_binary(self, binary: Path) -> bool:
"""Flash a binary to this device using picotool, returning success or
failure."""
cmd = (
'picotool',
_PICOTOOL_COMMAND,
'load',
'-x',
str(binary),
Expand Down
17 changes: 15 additions & 2 deletions targets/rp2040/py/rp2040_utils/unit_test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,21 @@

DEFAULT_PORT = 34172

_TEST_RUNNER_COMMAND = 'rp2040_unit_test_runner'
_TEST_SERVER_COMMAND = 'pw_target_runner_server'
# If the script is being run through Bazel, our runner and server are provided
# at well known locations in the runfiles.
try:
from rules_python.python.runfiles import runfiles # type: ignore

r = runfiles.Create()
_TEST_RUNNER_COMMAND = r.Rlocation(
'pigweed/targets/rp2040/py/rpc_unit_test_runner'
)
_TEST_SERVER_COMMAND = r.Rlocation(
'pigweed/pw_target_runner/go/cmd/server_/server'
)
except ImportError:
_TEST_RUNNER_COMMAND = 'rp2040_unit_test_runner'
_TEST_SERVER_COMMAND = 'pw_target_runner_server'


def parse_args():
Expand Down

0 comments on commit 946880d

Please sign in to comment.