|
32 | 32 | from ..interpreterbase import SubProject |
33 | 33 | from ..compilers.rust import RustCompiler |
34 | 34 |
|
| 35 | + from typing_extensions import Literal |
| 36 | + |
35 | 37 | def _dependency_name(package_name: str, api: str, suffix: str = '-rs') -> str: |
36 | | - basename = package_name[:-len(suffix)] if package_name.endswith(suffix) else package_name |
| 38 | + basename = package_name[:-len(suffix)] if suffix and package_name.endswith(suffix) else package_name |
37 | 39 | return f'{basename}-{api}{suffix}' |
38 | 40 |
|
39 | 41 |
|
@@ -139,8 +141,19 @@ def _create_package(self, pkg: PackageState, build: builder.Builder, subdir: str |
139 | 141 | ast += self._create_meson_subdir(build) |
140 | 142 |
|
141 | 143 | if pkg.manifest.lib: |
142 | | - for crate_type in pkg.manifest.lib.crate_type: |
143 | | - ast.extend(self._create_lib(pkg, build, crate_type)) |
| 144 | + crate_type = pkg.manifest.lib.crate_type |
| 145 | + if 'dylib' in crate_type and 'cdylib' in crate_type: |
| 146 | + raise MesonException('Cannot build both dylib and cdylib due to file name conflict') |
| 147 | + if 'proc-macro' in crate_type: |
| 148 | + ast.extend(self._create_lib(pkg, build, 'proc-macro', shared=True)) |
| 149 | + if any(x in crate_type for x in ['lib', 'rlib', 'dylib']): |
| 150 | + ast.extend(self._create_lib(pkg, build, 'rust', |
| 151 | + static=('lib' in crate_type or 'rlib' in crate_type), |
| 152 | + shared='dylib' in crate_type)) |
| 153 | + if any(x in crate_type for x in ['staticlib', 'cdylib']): |
| 154 | + ast.extend(self._create_lib(pkg, build, 'c', |
| 155 | + static='staticlib' in crate_type, |
| 156 | + shared='cdylib' in crate_type)) |
144 | 157 |
|
145 | 158 | return ast |
146 | 159 |
|
@@ -560,7 +573,9 @@ def _create_meson_subdir(self, build: builder.Builder) -> T.List[mparser.BaseNod |
560 | 573 | build.block([build.function('subdir', [build.string('meson')])])) |
561 | 574 | ] |
562 | 575 |
|
563 | | - def _create_lib(self, pkg: PackageState, build: builder.Builder, crate_type: raw.CRATE_TYPE) -> T.List[mparser.BaseNode]: |
| 576 | + def _create_lib(self, pkg: PackageState, build: builder.Builder, |
| 577 | + lib_type: Literal['rust', 'c', 'proc-macro'], |
| 578 | + static: bool = False, shared: bool = False) -> T.List[mparser.BaseNode]: |
564 | 579 | dependencies: T.List[mparser.BaseNode] = [] |
565 | 580 | dependency_map: T.Dict[mparser.BaseNode, mparser.BaseNode] = {} |
566 | 581 | for name in pkg.required_deps: |
@@ -593,21 +608,19 @@ def _create_lib(self, pkg: PackageState, build: builder.Builder, crate_type: raw |
593 | 608 | 'rust_args': build.array(rust_args), |
594 | 609 | } |
595 | 610 |
|
596 | | - depname_suffix = '-rs' if crate_type in {'lib', 'rlib', 'proc-macro'} else f'-{crate_type}' |
| 611 | + depname_suffix = '' if lib_type == 'c' else '-rs' |
597 | 612 | depname = _dependency_name(pkg.manifest.package.name, pkg.manifest.package.api, depname_suffix) |
598 | 613 |
|
599 | 614 | lib: mparser.BaseNode |
600 | | - if crate_type == 'proc-macro': |
| 615 | + if lib_type == 'proc-macro': |
601 | 616 | lib = build.method('proc_macro', build.identifier('rust'), posargs, kwargs) |
602 | 617 | else: |
603 | | - if crate_type in {'lib', 'rlib', 'staticlib'}: |
604 | | - target_type = 'static_library' |
605 | | - elif crate_type in {'dylib', 'cdylib'}: |
606 | | - target_type = 'shared_library' |
| 618 | + if static and shared: |
| 619 | + target_type = 'both_libraries' |
607 | 620 | else: |
608 | | - raise MesonException(f'Unsupported crate type {crate_type}') |
609 | | - if crate_type in {'staticlib', 'cdylib'}: |
610 | | - kwargs['rust_abi'] = build.string('c') |
| 621 | + target_type = 'shared_library' if shared else 'static_library' |
| 622 | + |
| 623 | + kwargs['rust_abi'] = build.string(lib_type) |
611 | 624 | lib = build.function(target_type, posargs, kwargs) |
612 | 625 |
|
613 | 626 | features_args: T.List[mparser.BaseNode] = [] |
|
0 commit comments