summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2025-10-26 09:15:54 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2025-12-22 12:01:05 +0100
commit254d7e1c48f3a2d0837439f07628cfd54e47367b (patch)
tree7c5deb63b2c3ae70db943d450d123e60645c9f78
parent2a5370fa85397473e98a09779fc4c7a56e048959 (diff)
downloadmeson-254d7e1c48f3a2d0837439f07628cfd54e47367b.tar.gz
rust: add to_system_dependency
Move the logic for system dependencies outside Cargo.interpreter and into the rust module, so that it can be reused by the workspace object. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r--docs/markdown/Rust-module.md15
-rw-r--r--mesonbuild/build.py8
-rw-r--r--mesonbuild/compilers/rust.py8
-rw-r--r--mesonbuild/modules/rust.py17
4 files changed, 44 insertions, 4 deletions
diff --git a/docs/markdown/Rust-module.md b/docs/markdown/Rust-module.md
index b1edb793c..37ba59d8e 100644
--- a/docs/markdown/Rust-module.md
+++ b/docs/markdown/Rust-module.md
@@ -172,6 +172,21 @@ Only a subset of [[shared_library]] keyword arguments are allowed:
- link_with
- override_options
+### to_system_dependency()
+
+*Since 1.11.0*
+
+```meson
+rustmod.to_system_dependency(dep[, name])
+```
+
+Create and return an internal dependency that wraps `dep` and
+defines `cfg(system_deps_have_NAME)`. This is compatible with
+how the `system-deps` crate reports the availability of a system
+dependency to Rust code.
+
+If omitted, the name defaults to the name of the dependency.
+
### workspace()
Basic usage:
diff --git a/mesonbuild/build.py b/mesonbuild/build.py
index f46bd616f..2ae47a858 100644
--- a/mesonbuild/build.py
+++ b/mesonbuild/build.py
@@ -1437,10 +1437,10 @@ class BuildTarget(Target):
self.link_whole_targets.extend(dep.whole_libraries)
if dep.get_compile_args() or dep.get_link_args():
# Those parts that are external.
- extpart = dependencies.InternalDependency(dep.version,
- compile_args=dep.get_compile_args(),
- link_args=dep.get_link_args(),
- name=dep.name)
+ extpart = type(dep)(dep.version,
+ compile_args=dep.get_compile_args(),
+ link_args=dep.get_link_args(),
+ name=dep.name)
self.external_deps.append(extpart)
# Deps of deps.
self.add_deps(dep.ext_deps)
diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py
index ab0706d26..7ad7f34e9 100644
--- a/mesonbuild/compilers/rust.py
+++ b/mesonbuild/compilers/rust.py
@@ -11,6 +11,7 @@ import re
import typing as T
from .. import options
+from ..dependencies import InternalDependency
from ..mesonlib import EnvironmentException, MesonException, Popen_safe_logged, version_compare
from ..linkers.linkers import VisualStudioLikeLinkerMixin
from ..options import OptionKey
@@ -73,6 +74,11 @@ def rustc_link_args(args: T.List[str]) -> T.List[str]:
rustc_args.append(f'link-arg={arg}')
return rustc_args
+
+class RustSystemDependency(InternalDependency):
+ pass
+
+
class RustCompiler(Compiler):
# rustc doesn't invoke the compiler itself, it doesn't need a LINKER_PREFIX
@@ -323,6 +329,8 @@ class RustCompiler(Compiler):
return opts
def get_dependency_compile_args(self, dep: 'Dependency') -> T.List[str]:
+ if isinstance(dep, RustSystemDependency):
+ return dep.get_compile_args()
# Rust doesn't have dependency compile arguments so simply return
# nothing here. Dependencies are linked and all required metadata is
# provided by the linker flags.
diff --git a/mesonbuild/modules/rust.py b/mesonbuild/modules/rust.py
index 0ea1c8f7e..5ba2a5f17 100644
--- a/mesonbuild/modules/rust.py
+++ b/mesonbuild/modules/rust.py
@@ -15,6 +15,7 @@ from .. import mesonlib, mlog
from ..build import (BothLibraries, BuildTarget, CustomTargetIndex, Executable, ExtractedObjects, GeneratedList,
CustomTarget, InvalidArguments, Jar, StructuredSources, SharedLibrary, StaticLibrary)
from ..compilers.compilers import are_asserts_disabled_for_subproject, lang_suffixes
+from ..compilers.rust import RustSystemDependency
from ..dependencies import Dependency
from ..interpreter.type_checking import (
DEPENDENCIES_KW, LINK_WITH_KW, LINK_WHOLE_KW, SHARED_LIB_KWS, TEST_KWS, TEST_KWS_NO_ARGS,
@@ -259,6 +260,7 @@ class RustModule(ExtensionModule):
'doctest': self.doctest,
'bindgen': self.bindgen,
'proc_macro': self.proc_macro,
+ 'to_system_dependency': self.to_system_dependency,
'workspace': self.workspace,
})
@@ -657,6 +659,21 @@ class RustModule(ExtensionModule):
target = state._interpreter.build_target(state.current_node, args, kwargs, SharedLibrary)
return target
+ @FeatureNew('rust.to_system_dependency', '1.11.0')
+ @typed_pos_args('rust.to_system_dependency', Dependency, optargs=[str])
+ @noKwargs
+ def to_system_dependency(self, state: ModuleState, args: T.Tuple[Dependency, T.Optional[str]], kwargs: TYPE_kwargs) -> Dependency:
+ dep, depname = args
+ if not dep.found():
+ return dep
+ if not depname:
+ if not dep.name:
+ raise MesonException("rust.to_system_dependency() called with an unnamed dependency and no explicit name")
+ depname = dep.name
+ depname = re.sub(r'[^a-zA-Z0-9]', '_', depname)
+ rust_args = ['--cfg', f'system_deps_have_{depname}']
+ return RustSystemDependency(dep.version, compile_args=rust_args, ext_deps=[dep], name=dep.name)
+
@FeatureNew('rust.workspace', '1.11.0')
@noPosargs
@typed_kwargs(