Skip to content

Commit 2df2a48

Browse files
committed
modules: rust: add package.shared_module method
1 parent b91d68e commit 2df2a48

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

docs/markdown/Rust-module.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,16 @@ Accepts all keyword arguments from [[shared_library]] and [[static_library]].
394394
`rust_abi` must match the crate types and is mandatory if more than one
395395
ABI is exposed by the crate.
396396

397+
#### package.shared_module()
398+
399+
```meson
400+
lib = pkg.shared_module(...)
401+
```
402+
403+
Builds the `cdylib` for a workspace package as a shared module.
404+
405+
Accepts all keyword arguments from [[shared_module]].
406+
397407
#### package.proc_macro()
398408

399409
```meson

mesonbuild/modules/rust.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
from . import ExtensionModule, ModuleReturnValue, ModuleInfo, ModuleObject
1414
from .. import mesonlib, mlog
1515
from ..build import (BothLibraries, BuildTarget, CustomTargetIndex, Executable, ExtractedObjects, GeneratedList,
16-
CustomTarget, InvalidArguments, Jar, StructuredSources, SharedLibrary, StaticLibrary)
16+
CustomTarget, InvalidArguments, Jar, StructuredSources, SharedLibrary, StaticLibrary,
17+
SharedModule)
1718
from ..compilers.compilers import are_asserts_disabled_for_subproject, lang_suffixes
1819
from ..compilers.rust import RustSystemDependency
1920
from ..dependencies import Dependency
2021
from ..interpreter.type_checking import (
2122
DEPENDENCIES_KW, LINK_WITH_KW, LINK_WHOLE_KW, SHARED_LIB_KWS, TEST_KWS, TEST_KWS_NO_ARGS,
2223
OUTPUT_KW, INCLUDE_DIRECTORIES, SOURCES_VARARGS, NoneType, in_set_validator,
23-
EXECUTABLE_KWS, LIBRARY_KWS, _BASE_LANG_KW
24+
EXECUTABLE_KWS, LIBRARY_KWS, SHARED_MOD_KWS, _BASE_LANG_KW
2425
)
2526
from ..interpreterbase import ContainerTypeInfo, InterpreterException, KwargInfo, typed_kwargs, typed_pos_args, noKwargs, noPosargs, permittedKwargs
2627
from ..interpreter.interpreterobjects import Doctest
@@ -247,6 +248,7 @@ def __init__(self, rust_ws: RustWorkspace, package: cargo.PackageState) -> None:
247248
'dependencies': self.dependencies_method,
248249
'library': self.library_method,
249250
'proc_macro': self.proc_macro_method,
251+
'shared_module': self.shared_module_method,
250252
'executable': self.executable_method,
251253
})
252254

@@ -313,7 +315,8 @@ def merge_kw_args(self, state: ModuleState, kwargs: T.Union[RustPackageExecutabl
313315
def _library_method(self, state: ModuleState, args: T.Tuple[
314316
T.Optional[T.Union[str, StructuredSources]],
315317
T.Optional[StructuredSources]], kwargs: RustPackageLibrary,
316-
static: bool, shared: bool) -> T.Union[BothLibraries, SharedLibrary, StaticLibrary]:
318+
static: bool, shared: bool,
319+
shared_mod: bool = False) -> T.Union[BothLibraries, SharedLibrary, StaticLibrary]:
317320
tgt_args = self.validate_pos_args('package.library', args)
318321
if not self.package.manifest.lib:
319322
raise MesonException("no [lib] section in Cargo package")
@@ -333,6 +336,11 @@ def _library_method(self, state: ModuleState, args: T.Tuple[
333336
lib_args: T.Tuple[str, SourcesVarargsType] = (tgt_name, [sources])
334337
self.merge_kw_args(state, kwargs)
335338

339+
if shared_mod:
340+
return state._interpreter.build_target(state.current_node, lib_args,
341+
T.cast('_kwargs.SharedModule', kwargs),
342+
SharedModule)
343+
336344
if static and shared:
337345
return state._interpreter.build_both_libraries(state.current_node, lib_args, kwargs)
338346
elif shared:
@@ -400,6 +408,28 @@ def proc_macro_method(self, state: 'ModuleState', args: T.Tuple[
400408
raise MesonException("not a procedural macro crate")
401409
return self._proc_macro_method(state, args, kwargs)
402410

411+
@typed_pos_args('package.shared_module', optargs=[(str, StructuredSources), StructuredSources])
412+
@typed_kwargs(
413+
'package.shared_module',
414+
*SHARED_MOD_KWS,
415+
DEPENDENCIES_KW,
416+
LINK_WITH_KW,
417+
LINK_WHOLE_KW,
418+
_BASE_LANG_KW.evolve(name='rust_args'),
419+
)
420+
def shared_module_method(self, state: 'ModuleState', args: T.Tuple[
421+
T.Optional[T.Union[str, StructuredSources]],
422+
T.Optional[StructuredSources]], kwargs: RustPackageLibrary) -> SharedModule:
423+
if not self.package.manifest.lib:
424+
raise MesonException("no [lib] section in Cargo package")
425+
if 'cdylib' not in self.package.manifest.lib.crate_type:
426+
raise MesonException("not a cdylib crate")
427+
428+
kwargs['rust_abi'] = None
429+
kwargs['rust_crate_type'] = 'cdylib'
430+
result = self._library_method(state, args, kwargs, shared=True, static=False, shared_mod=True)
431+
return T.cast('SharedModule', result)
432+
403433
@typed_pos_args('package.executable', optargs=[(str, StructuredSources), StructuredSources])
404434
@typed_kwargs(
405435
'package.executable',

0 commit comments

Comments
 (0)