Skip to content

Commit

Permalink
add .as_static and .as_shared methods to deps
Browse files Browse the repository at this point in the history
  • Loading branch information
bruchar1 committed Sep 5, 2024
1 parent 6f34c43 commit f455e58
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/markdown/Builtin-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fails.

#### Details for `default_both_libraries`

Since `1.4.0`, you can select the default type of library selected when using
Since `1.5.0`, you can select the default type of library selected when using
a `both_libraries` object. This can be either 'shared' (default value, compatible
with previous meson versions), 'static', or 'auto'. With auto, the value from
`default_library` option is used, unless it is 'both', in which case 'shared'
Expand Down
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.
2 changes: 1 addition & 1 deletion docs/yaml/functions/alias_target.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: |
are built. Dependencies can be any build target. Since 0.60.0, this includes
[[@run_tgt]].
*Since 1.4.0* passing a [[@both_libs]] object builds both shared and
*Since 1.5.0* passing a [[@both_libs]] object builds both shared and
static libraries.
posargs:
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.5.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.5.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

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
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.5.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.5.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
30 changes: 28 additions & 2 deletions test cases/common/273 both libraries/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(
'test both libraries',
'c',
meson_version: '>= 1.3.99',
meson_version: '>= 1.5.0',
)

expected = 0
Expand Down 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 f455e58

Please sign in to comment.