Skip to content

Commit 5244308

Browse files
committed
build: Add dummy base class for CustomTarget and CustomTargetIndex
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.
1 parent c6e6ade commit 5244308

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

mesonbuild/build.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,21 +1276,21 @@ def get_extra_args(self, language: str) -> T.List[str]:
12761276
return self.extra_args[language]
12771277

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

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

1397-
def link(self, targets):
1397+
def link(self, targets: T.List[BuildTargetTypes]):
13981398
for t in targets:
13991399
if not isinstance(t, (Target, CustomTargetIndex)):
14001400
if isinstance(t, dependencies.ExternalLibrary):
@@ -1420,7 +1420,7 @@ def link(self, targets):
14201420
self.check_can_link_together(t)
14211421
self.link_targets.append(t)
14221422

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

2530-
class CustomTarget(Target, CommandBase):
2530+
class CustomTargetBase:
2531+
''' Base class for CustomTarget and CustomTargetIndex
2532+
2533+
This base class can be used to provide a dummy implementation of some
2534+
private methods to avoid repeating `isinstance(t, BuildTarget)` when dealing
2535+
with custom targets.
2536+
'''
2537+
2538+
rust_crate_type = ''
2539+
2540+
def get_dependencies_recurse(self, result: OrderedSet[BuildTargetTypes], include_internals: bool = True) -> None:
2541+
pass
2542+
2543+
def get_internal_static_libraries(self) -> OrderedSet[BuildTargetTypes]:
2544+
return OrderedSet()
2545+
2546+
def get_internal_static_libraries_recurse(self, result: OrderedSet[BuildTargetTypes]) -> None:
2547+
pass
2548+
2549+
class CustomTarget(Target, CustomTargetBase, CommandBase):
25312550

25322551
typename = 'custom'
25332552

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

29062925
@dataclass(eq=False)
2907-
class CustomTargetIndex(HoldableObject):
2926+
class CustomTargetIndex(CustomTargetBase, HoldableObject):
29082927

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

0 commit comments

Comments
 (0)