Skip to content

Commit

Permalink
pw_package: Use bazel to build picotool package
Browse files Browse the repository at this point in the history
This ensures that the version of picotool being built
matches the version specified in pigweed's MODULE.bazel
and avoids the need to separately install the PICO_SDK
and libusb.

Change-Id: Iffb1f0d547301e165499e451e7f6948d7bce35a5
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/229431
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Anthony DiGirolamo <[email protected]>
Commit-Queue: Taylor Cramer <[email protected]>
  • Loading branch information
cramertj authored and CQ Bot Account committed Aug 14, 2024
1 parent 14b586f commit c9d5bef
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions pw_package/py/pw_package/packages/picotool.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
# the License.
"""Install and check status of picotool."""

from contextlib import contextmanager
import logging
import os
import pathlib
from pathlib import Path
import shutil
import subprocess
Expand All @@ -28,14 +26,10 @@
_LOG = logging.getLogger(__package__)


@contextmanager
def change_working_dir(directory: Path):
original_dir = Path.cwd()
try:
os.chdir(directory)
yield directory
finally:
os.chdir(original_dir)
def force_copy(source: Path, destination: Path):
_LOG.info('Copy %s -> %s', source, destination)
destination.unlink(missing_ok=True)
shutil.copy(source, destination)


class Picotool(pw_package.package_manager.Package):
Expand All @@ -44,49 +38,40 @@ class Picotool(pw_package.package_manager.Package):
def __init__(self, *args, **kwargs):
super().__init__(*args, name='picotool', **kwargs)

self._pico_tool_repo = pw_package.git_repo.GitRepo(
name='picotool',
url=(
'https://pigweed.googlesource.com/third_party/'
'github/raspberrypi/picotool.git'
),
commit='f6fe6b7c321a2def8950d2a440335dfba19e2eab',
)

def install(self, path: Path) -> None:
self._pico_tool_repo.install(path)

env = os.environ.copy()
env['PICO_SDK_PATH'] = str(path.parent.absolute() / 'pico_sdk')
bootstrap_env_path = Path(env.get('_PW_ACTUAL_ENVIRONMENT_ROOT', ''))

commands = (
('cmake', '-S', './', '-B', 'out/', '-G', 'Ninja'),
('ninja', '-C', 'out'),
)
def log_and_run(command: Sequence[str], **kwargs):
_LOG.info('==> %s', ' '.join(command))
return subprocess.run(
command,
env=env,
check=True,
**kwargs,
)

with change_working_dir(path) as _picotool_repo:
for command in commands:
_LOG.info('==> %s', ' '.join(command))
subprocess.run(
command,
env=env,
check=True,
)
log_and_run(('bazel', 'build', '@picotool'))
build_path = log_and_run(
('bazel', 'cquery', '@picotool', '--output=files'),
capture_output=True,
text=True,
).stdout.strip()

picotool_bin = path / 'out' / 'picotool'
force_copy(build_path, picotool_bin)

_LOG.info('Done! picotool binary located at:')
_LOG.info(picotool_bin)

bootstrap_env_path = Path(env.get('_PW_ACTUAL_ENVIRONMENT_ROOT', ''))
if bootstrap_env_path.is_dir() and picotool_bin.is_file():
bin_path = (
bootstrap_env_path / 'cipd' / 'packages' / 'pigweed' / 'bin'
)
destination_path = bin_path / picotool_bin.name
_LOG.info('Copy %s -> %s', picotool_bin, destination_path)
shutil.copy(picotool_bin, destination_path)
force_copy(build_path, destination_path)

def info(self, path: pathlib.Path) -> Sequence[str]:
def info(self, path: Path) -> Sequence[str]:
return (f'{self.name} installed in: {path}',)


Expand Down

0 comments on commit c9d5bef

Please sign in to comment.