Skip to content

Commit

Permalink
Set pw_command_launcher from build_example.py CLI (#23702)
Browse files Browse the repository at this point in the history
* Set pw_command_launcher from build_example.py CLI

This will allow effortless ccache setup like this:

./scripts/build/build_examples.py \
  --target linux-x64-light \
	--pw-command-launcher=ccache \
	build

* Do not use setter pattern for simple flag

* Keep all builder options in dedicated dataclass

* Fix unit test module import
  • Loading branch information
arkq authored and pull[bot] committed Mar 30, 2023
1 parent 821cdf6 commit 50b0efa
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
7 changes: 4 additions & 3 deletions scripts/build/build/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Sequence

from .targets import BUILD_TARGETS
from builders.builder import BuilderOptions


class BuildSteps(Enum):
Expand All @@ -24,8 +25,7 @@ def __init__(self, runner, repository_path: str, output_prefix: str):
self.output_prefix = output_prefix
self.completed_steps = set()

def SetupBuilders(self, targets: Sequence[str],
enable_flashbundle: bool):
def SetupBuilders(self, targets: Sequence[str], options: BuilderOptions):
"""
Configures internal builders for the given platform/board/app
combination.
Expand All @@ -35,7 +35,8 @@ def SetupBuilders(self, targets: Sequence[str],
for target in targets:
found = False
for choice in BUILD_TARGETS:
builder = choice.Create(target, self.runner, self.repository_path, self.output_prefix, enable_flashbundle)
builder = choice.Create(target, self.runner, self.repository_path,
self.output_prefix, options)
if builder:
self.builders.append(builder)
found = True
Expand Down
6 changes: 4 additions & 2 deletions scripts/build/build/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
from dataclasses import dataclass
from typing import Any, Dict, List, Iterable, Optional

from builders.builder import BuilderOptions


report_rejected_parts = True

Expand Down Expand Up @@ -330,7 +332,7 @@ def StringIntoTargetParts(self, value: str):
return _StringIntoParts(value, suffix, self.fixed_targets, self.modifiers)

def Create(self, name: str, runner, repository_path: str, output_prefix: str,
enable_flashbundle: bool):
builder_options: BuilderOptions):

parts = self.StringIntoTargetParts(name)

Expand All @@ -348,6 +350,6 @@ def Create(self, name: str, runner, repository_path: str, output_prefix: str,
builder.identifier = name
builder.output_dir = os.path.join(output_prefix, name)
builder.chip_dir = repository_path
builder.enable_flashbundle(enable_flashbundle)
builder.options = builder_options

return builder
12 changes: 4 additions & 8 deletions scripts/build/build/test_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
# limitations under the License.

import unittest
import sys
import os

try:
from build.target import *
except:
import sys
import os

sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from target import *
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from build.target import BuildTarget, TargetPart # noqa: E402


class FakeBuilder:
Expand Down
15 changes: 11 additions & 4 deletions scripts/build/build_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import coloredlogs

import build
from glob_matcher import GlobMatcher
from builders.builder import BuilderOptions
from runner import PrintOnlyRunner, ShellRunner

sys.path.append(os.path.abspath(os.path.dirname(__file__)))
Expand Down Expand Up @@ -107,10 +107,15 @@ def ValidateRepoPath(context, parameter, value):
default=False,
is_flag=True,
help='Skip timestaps in log output')
@click.option(
'--pw-command-launcher',
help=(
'Set pigweed command launcher. E.g.: "--pw-command-launcher=ccache" '
'for using ccache when building examples.'))
@click.pass_context
def main(context, log_level, target, repo,
out_prefix, clean, dry_run, dry_run_output, enable_flashbundle,
no_log_timestamps):
no_log_timestamps, pw_command_launcher):
# Ensures somewhat pretty logging of what is going on
log_fmt = '%(asctime)s %(levelname)-7s %(message)s'
if no_log_timestamps:
Expand All @@ -135,8 +140,10 @@ def main(context, log_level, target, repo,

context.obj = build.Context(
repository_path=repo, output_prefix=out_prefix, runner=runner)
context.obj.SetupBuilders(
targets=requested_targets, enable_flashbundle=enable_flashbundle)
context.obj.SetupBuilders(targets=requested_targets, options=BuilderOptions(
enable_flashbundle=enable_flashbundle,
pw_command_launcher=pw_command_launcher,
))

if clean:
context.obj.CleanOutputDirectories()
Expand Down
22 changes: 14 additions & 8 deletions scripts/build/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,33 @@
import shutil
import tarfile
from abc import ABC, abstractmethod
from dataclasses import dataclass


@dataclass
class BuilderOptions:
# Enable flashbundle generation stage
enable_flashbundle: bool = False
# Allow to wrap default build command
pw_command_launcher: str = None


class Builder(ABC):
"""Generic builder base class for CHIP.
Provides ability to boostrap and copy output artifacts and subclasses can use
a generic shell runner.
Provides ability to bootstrap and copy output artifacts and subclasses can
use a generic shell runner.
"""

def __init__(self, root, runner):
self.root = os.path.abspath(root)
self._runner = runner
self._enable_flashbundle = False

# Set post-init once actual build target is known
self.identifier = None
self.output_dir = None

def enable_flashbundle(self, enable_flashbundle: bool):
self._enable_flashbundle = enable_flashbundle
self.options = BuilderOptions()

@abstractmethod
def generate(self):
Expand Down Expand Up @@ -81,13 +87,13 @@ def flashbundle(self):

def outputs(self):
artifacts = self.build_outputs()
if self._enable_flashbundle:
if self.options.enable_flashbundle:
artifacts.update(self.flashbundle())
return artifacts

def build(self):
self._build()
if self._enable_flashbundle:
if self.options.enable_flashbundle:
self._generate_flashbundle()

def _Execute(self, cmdarray, title=None):
Expand Down
5 changes: 4 additions & 1 deletion scripts/build/builders/gn.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def generate(self):
'--root=%s' % self.root
]

extra_args = self.GnBuildArgs()
extra_args = []
if self.options.pw_command_launcher:
extra_args.append('pw_command_launcher="%s"' % self.options.pw_command_launcher)
extra_args.extend(self.GnBuildArgs() or [])
if extra_args:
cmd += ['--args=%s' % ' '.join(extra_args)]

Expand Down

0 comments on commit 50b0efa

Please sign in to comment.