Skip to content

Commit

Permalink
build: Add dummy base class for CustomTarget and CustomTargetIndex
Browse files Browse the repository at this point in the history
CustomTarget and CustomTargetIndex often have to be special cased
because they are not subclass of BuildTarget. It's easier to introduce a
dummy base class.
  • Loading branch information
xclaesse committed Nov 8, 2023
1 parent b3aae77 commit 9bb8235
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,21 +1276,21 @@ def get_extra_args(self, language: str) -> T.List[str]:
return self.extra_args[language]

@lru_cache(maxsize=None)
def get_dependencies(self) -> OrderedSet[Target]:
def get_dependencies(self) -> OrderedSet[BuildTargetTypes]:
# Get all targets needed for linking. This includes all link_with and
# link_whole targets, and also all dependencies of static libraries
# recursively. The algorithm here is closely related to what we do in
# get_internal_static_libraries(): Installed static libraries include
# objects from all their dependencies already.
result: OrderedSet[Target] = OrderedSet()
result: OrderedSet[BuildTargetTypes] = OrderedSet()
for t in itertools.chain(self.link_targets, self.link_whole_targets):
if t not in result:
result.add(t)
if isinstance(t, StaticLibrary):
t.get_dependencies_recurse(result)
return result

def get_dependencies_recurse(self, result: OrderedSet[Target], include_internals: bool = True) -> None:
def get_dependencies_recurse(self, result: OrderedSet[BuildTargetTypes], include_internals: bool = True) -> None:
# self is always a static library because we don't need to pull dependencies
# of shared libraries. If self is installed (not internal) it already
# include objects extracted from all its internal dependencies so we can
Expand All @@ -1299,7 +1299,7 @@ def get_dependencies_recurse(self, result: OrderedSet[Target], include_internals
for t in self.link_targets:
if t in result:
continue
if isinstance(t, SharedLibrary) and t.rust_crate_type == 'proc-macro':
if t.rust_crate_type == 'proc-macro':
continue
if include_internals or not t.is_internal():
result.add(t)
Expand Down Expand Up @@ -1394,7 +1394,7 @@ def get_external_deps(self) -> T.List[dependencies.Dependency]:
def is_internal(self) -> bool:
return False

def link(self, targets):
def link(self, targets: T.List[BuildTargetTypes]):
for t in targets:
if not isinstance(t, (Target, CustomTargetIndex)):
if isinstance(t, dependencies.ExternalLibrary):
Expand All @@ -1420,7 +1420,7 @@ def link(self, targets):
self.check_can_link_together(t)
self.link_targets.append(t)

def link_whole(self, targets, promoted: bool = False):
def link_whole(self, targets: T.List[BuildTargetTypes], promoted: bool = False):
for t in targets:
if isinstance(t, (CustomTarget, CustomTargetIndex)):
if not t.is_linkable_target():
Expand Down Expand Up @@ -2527,7 +2527,26 @@ def flatten_command(self, cmd: T.Sequence[T.Union[str, File, programs.ExternalPr
raise InvalidArguments(f'Argument {c!r} in "command" is invalid')
return final_cmd

class CustomTarget(Target, CommandBase):
class CustomTargetBase:
''' Base class for CustomTarget and CustomTargetIndex
This base class can be used to provide a dummy implementation of some
private methods to avoid repeating `isinstance(t, BuildTarget)` when dealing
with custom targets.
'''

rust_crate_type = ''

def get_dependencies_recurse(self, result: OrderedSet[BuildTargetTypes], include_internals: bool = True) -> None:
pass

def get_internal_static_libraries(self) -> OrderedSet[BuildTargetTypes]:
return OrderedSet()

def get_internal_static_libraries_recurse(self, result: OrderedSet[BuildTargetTypes]) -> None:
pass

class CustomTarget(Target, CustomTargetBase, CommandBase):

typename = 'custom'

Expand Down Expand Up @@ -2904,7 +2923,7 @@ def get_default_install_dir(self) -> T.Tuple[str, str]:
return self.environment.get_jar_dir(), '{jardir}'

@dataclass(eq=False)
class CustomTargetIndex(HoldableObject):
class CustomTargetIndex(CustomTargetBase, HoldableObject):

"""A special opaque object returned by indexing a CustomTarget. This object
exists in Meson, but acts as a proxy in the backends, making targets depend
Expand Down

0 comments on commit 9bb8235

Please sign in to comment.