Skip to content

Commit

Permalink
Adding --target flag to build CMake target by name (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStarch authored Nov 25, 2024
1 parent c81b089 commit 6bfd6aa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/fprime/fbuild/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from fprime.common.utils import confirm
from fprime.fbuild.builder import Build
from fprime.fbuild.target import Target
from fprime.fbuild.target import Target, DesignateTargetAction
from fprime.fbuild.types import BuildType


Expand Down Expand Up @@ -180,12 +180,23 @@ def add_target_parser(
if target.allows_pass_args()
else ""
)
parser.add_argument(
f"--{flag}",
action="store_true",
default=False,
help=f"{target.desc}{extra_help}",
)
# Target flag special handling
if flag == "target":
parser.add_argument(
f"--target",
type=str,
action=DesignateTargetAction,
default=None,
nargs=1,
help=f"{target.desc}",
)
else:
parser.add_argument(
f"--{flag}",
action="store_true",
default=False,
help=f"{target.desc}{extra_help}",
)
for flag, description in filter(
lambda flag_desc: flag_desc[0] not in flags, target.option_args()
):
Expand Down
38 changes: 38 additions & 0 deletions src/fprime/fbuild/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import functools
import itertools
from abc import ABC, abstractmethod
from argparse import Action
from enum import Enum
from pathlib import Path
from typing import Dict, List, Set, Tuple
Expand Down Expand Up @@ -341,6 +342,43 @@ def is_supported(self, builder: "Build", context: Path):
return self.build_target in build_target_names


class DesignateTargetAction(Action):
"""This class, when used as the action will set the target of all DesignatedBuildSystemTarget
argparse.Action actions can be used to handle custom behavior when parsing commands. This class will use the
--target flag to specify the build target on all known DesignatedBuildSystemTarget that have registered with this
class.
"""

_DESIGNEES = []

@classmethod
def register_designee(cls, designee):
"""Register designee to this action"""
cls._DESIGNEES.append(designee)

def __call__(self, parser, namespace, values, option_string=None):
"""Required __call__ function triggered by the parse"""
assert len(values) == 1, "Values object should contain 1 value"
for designee in self._DESIGNEES:
designee.set_target(values[0])
# The build target detection looks for true/false flags to be set. Mimic this by setting 'target' to True
setattr(namespace, "target", True)


class DesignatedBuildSystemTarget(BuildSystemTarget):
"""Invokes a designated target using the --target flag"""

def __init__(self, _, *args, **kwargs):
"""Constructor setting child targets"""
super().__init__(None, *args, **kwargs)
DesignateTargetAction.register_designee(self)

def set_target(self, target):
"""Set the target to build"""
self.build_target = target


class DelegatorTarget(Target):
"""Delegates to another target
Expand Down
21 changes: 19 additions & 2 deletions src/fprime/fbuild/target_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

from .gcovr import GcovrTarget
from .target import BuildSystemTarget, TargetScope
from .target import BuildSystemTarget, DesignatedBuildSystemTarget, TargetScope
from .types import BuildType

#### "build" targets for components, deployments, unittests for both normal and testing builds ####
Expand All @@ -27,12 +27,29 @@
BuildSystemTarget(
"all",
mnemonic="build",
desc="Build components, ports, UTs, and deployments for unittest build",
desc="Build all components, ports, UTs, and deployments for unittest build",
scope=TargetScope.GLOBAL,
flags={"all", "ut"},
build_type=BuildType.BUILD_TESTING,
)

DesignatedBuildSystemTarget(
"target",
mnemonic="build",
desc="Build a specific CMake target by name",
scope=TargetScope.GLOBAL,
flags={"target"},
)

DesignatedBuildSystemTarget(
"target",
mnemonic="build",
desc="Build a specific CMake target by name using the UT build",
scope=TargetScope.GLOBAL,
flags={"ut", "target"},
build_type=BuildType.BUILD_TESTING,
)

#### Check targets ####
check = BuildSystemTarget(
"check",
Expand Down

0 comments on commit 6bfd6aa

Please sign in to comment.