Skip to content

Commit

Permalink
alias_target with both_libs builds both
Browse files Browse the repository at this point in the history
  • Loading branch information
bruchar1 committed Sep 5, 2024
1 parent 84aa8f4 commit 1682604
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 6 deletions.
5 changes: 5 additions & 0 deletions docs/markdown/snippets/alias_target_of_both_libraries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## `alias_target` of `both_libraries`

Previously, when passing a [[@both_libs]] object to [[alias_target]], the alias
would only point to the shared library. It now points to both the static and the
shared library.
8 changes: 8 additions & 0 deletions docs/markdown/snippets/dep_as_shared_as_static.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## New `as_static` and `as_shared` methods on internal dependencies

[[@dep]] object returned by [[declare_dependency]] now has `.as_static()` and
`.as_shared()` methods, to convert to a dependency that prefers the `static`
or the `shared` version of the linked [[@both_libs]] target.

When the same dependency is used without those methods, the
`default_both_libraries` option determines which version is used.
3 changes: 3 additions & 0 deletions docs/yaml/functions/alias_target.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ description: |
are built. Dependencies can be any build target. Since 0.60.0, this includes
[[@run_tgt]].
*Since 1.6.0* passing a [[@both_libs]] object builds both shared and
static libraries.
posargs:
target_name:
type: str
Expand Down
25 changes: 25 additions & 0 deletions docs/yaml/objects/dep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,28 @@ methods:
pkgconfig_define:
type: list[str]
description: See [[dep.get_pkgconfig_variable]]

- name: as_static
returns: dep
since: 1.6.0
description: |
Only for dependencies created with [[declare_dependency]],
returns a copy of the dependency object that prefer the `static` version
of [[both_libraries]].
kwargs:
recursive:
type: bool
description: If true, this is recursively applied to dependencies

- name: as_shared
returns: dep
since: 1.6.0
description: |
Only for dependencies created with [[declare_dependency]],
returns a copy of the dependency object that prefer the `shared` version
of [[both_libraries]].
kwargs:
recursive:
type: bool
description: If true, this is recursively applied to dependencies

13 changes: 11 additions & 2 deletions mesonbuild/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2889,14 +2889,23 @@ class AliasTarget(RunTarget):

typename = 'alias'

def __init__(self, name: str, dependencies: T.Sequence['Target'],
def __init__(self, name: str, dependencies: T.Sequence[T.Union[Target, BothLibraries]],
subdir: str, subproject: str, environment: environment.Environment):
super().__init__(name, [], dependencies, subdir, subproject, environment)
super().__init__(name, [], list(self._deps_generator(dependencies)), subdir, subproject, environment)

def __repr__(self):
repr_str = "<{0} {1}>"
return repr_str.format(self.__class__.__name__, self.get_id())

@staticmethod
def _deps_generator(dependencies: T.Sequence[T.Union[Target, BothLibraries]]) -> T.Generator[Target, None, None]:
for dep in dependencies:
if isinstance(dep, BothLibraries):
yield dep.shared
yield dep.static
else:
yield dep

class Jar(BuildTarget):
known_kwargs = known_jar_kwargs

Expand Down
18 changes: 18 additions & 0 deletions mesonbuild/dependencies/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,24 @@ def generate_link_whole_dependency(self) -> Dependency:
new_dep.libraries = []
return new_dep

def get_as_static(self, recursive: bool) -> Dependency:
from ..build import BothLibraries

new_dep = copy.copy(self)
new_dep.libraries = [lib.static if isinstance(lib, BothLibraries) else lib for lib in self.libraries]
if recursive:
new_dep.ext_deps = [dep.get_as_static(True) if isinstance(dep, InternalDependency) else dep for dep in self.ext_deps]
return new_dep

def get_as_shared(self, recursive: bool) -> Dependency:
from ..build import BothLibraries

new_dep = copy.copy(self)
new_dep.libraries = [lib.shared if isinstance(lib, BothLibraries) else lib for lib in self.libraries]
if recursive:
new_dep.ext_deps = [dep.get_as_shared(True) if isinstance(dep, InternalDependency) else dep for dep in self.ext_deps]
return new_dep

class HasNativeKwarg:
def __init__(self, kwargs: T.Dict[str, T.Any]):
self.for_machine = self.get_for_machine_from_kwargs(kwargs)
Expand Down
7 changes: 4 additions & 3 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,10 +2165,11 @@ def func_run_target(self, node: mparser.FunctionNode, args: T.Tuple[str],
return tg

@FeatureNew('alias_target', '0.52.0')
@typed_pos_args('alias_target', str, varargs=build.Target, min_varargs=1)
@typed_pos_args('alias_target', str, varargs=(build.Target, build.BothLibraries), min_varargs=1)
@noKwargs
def func_alias_target(self, node: mparser.BaseNode, args: T.Tuple[str, T.List[build.Target]],
kwargs: 'TYPE_kwargs') -> build.AliasTarget:
@noSecondLevelHolderResolving
def func_alias_target(self, node: mparser.BaseNode, args: T.Tuple[str, T.List[T.Union[build.Target, build.BothLibraries]]],
kwargs: TYPE_kwargs) -> build.AliasTarget:
name, deps = args
if any(isinstance(d, build.RunTarget) for d in deps):
FeatureNew.single_use('alias_target that depends on run_targets', '0.60.0', self.subproject)
Expand Down
28 changes: 28 additions & 0 deletions mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class EnvironmentSeparatorKW(TypedDict):

separator: str

class InternalDependencyAsKW(TypedDict):

recursive: bool

_ERROR_MSG_KW: KwargInfo[T.Optional[str]] = KwargInfo('error_message', (str, NoneType))


Expand Down Expand Up @@ -462,6 +466,8 @@ def __init__(self, dep: Dependency, interpreter: 'Interpreter'):
'include_type': self.include_type_method,
'as_system': self.as_system_method,
'as_link_whole': self.as_link_whole_method,
'as_static': self.as_static_method,
'as_shared': self.as_shared_method,
})

def found(self) -> bool:
Expand Down Expand Up @@ -580,6 +586,28 @@ def as_link_whole_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> D
new_dep = self.held_object.generate_link_whole_dependency()
return new_dep

@FeatureNew('dependency.as_static', '1.6.0')
@noPosargs
@typed_kwargs(
'dependency.as_static',
KwargInfo('recursive', bool, default=False),
)
def as_static_method(self, args: T.List[TYPE_var], kwargs: InternalDependencyAsKW) -> Dependency:
if not isinstance(self.held_object, InternalDependency):
raise InterpreterException('as_static method is only supported on declare_dependency() objects')
return self.held_object.get_as_static(kwargs['recursive'])

@FeatureNew('dependency.as_shared', '1.6.0')
@noPosargs
@typed_kwargs(
'dependency.as_shared',
KwargInfo('recursive', bool, default=False),
)
def as_shared_method(self, args: T.List[TYPE_var], kwargs: InternalDependencyAsKW) -> Dependency:
if not isinstance(self.held_object, InternalDependency):
raise InterpreterException('as_shared method is only supported on declare_dependency() objects')
return self.held_object.get_as_shared(kwargs['recursive'])

_EXTPROG = T.TypeVar('_EXTPROG', bound=ExternalProgram)

class _ExternalProgramHolder(ObjectHolder[_EXTPROG]):
Expand Down
1 change: 1 addition & 0 deletions test cases/common/178 bothlibraries/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ project('both libraries linking test', 'c', 'cpp')

both_libs = both_libraries('mylib', 'libfile.c')
dep = declare_dependency(link_with: both_libs)
alias_target('alias', both_libs)
exe_shared = executable('prog-shared', 'main.c', link_with : both_libs.get_shared_lib())
exe_static = executable('prog-static', 'main.c',
c_args : ['-DSTATIC_COMPILATION'],
Expand Down
28 changes: 27 additions & 1 deletion test cases/common/273 both libraries/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,37 @@ test('test both libs', main)
if get_option('default_library') == 'both' and get_option('default_both_libraries') == 'auto'
# With those options, even if the both_libraries defaults to 'shared',
# 'static' version is used when linking to the static part of another both_libraries.

if get_option('use_dep')
main_static_deps = [with_library_dep.as_static(recursive: true)]
main_static_links = []
else
main_static_deps = []
main_static_links = [with_library.get_static_lib()]
endif
main_static = executable(
'main_static',
files('src/main.c'),
c_args: [f'-DEXPECTED=0'],
link_with: with_library.get_static_lib(),
link_with: main_static_links,
dependencies: main_static_deps,
)
test('test static', main_static)


if get_option('use_dep')
main_shared_deps = [with_library_dep.as_shared(recursive: true)]
main_shared_links = []
else
main_shared_deps = []
main_shared_links = [with_library.get_shared_lib()]
endif
main_shared = executable(
'main_shared',
files('src/main.c'),
c_args: [f'-DEXPECTED=2'],
link_with: main_shared_links,
dependencies: main_shared_deps,
)
test('test shared', main_shared)
endif

0 comments on commit 1682604

Please sign in to comment.