Skip to content

Commit 04d2043

Browse files
bonzinidcbaker
authored andcommitted
cargo: move library autodetection to Manifest.from_raw
It makes sense to make the lib argument optional, so do the same for all the others as well because optional arguments must all go together. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d3e055b commit 04d2043

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

mesonbuild/cargo/interpreter.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ def interpret(self, subdir: str) -> mparser.CodeBlockNode:
100100
ast += self._create_dependencies(pkg, build)
101101
ast += self._create_meson_subdir(build)
102102

103-
# Libs are always auto-discovered and there's no other way to handle them,
104-
# which is unfortunate for reproducability
105-
if os.path.exists(os.path.join(self.environment.source_dir, subdir, pkg.manifest.path, pkg.manifest.lib.path)):
103+
if pkg.manifest.lib:
106104
for crate_type in pkg.manifest.lib.crate_type:
107105
ast.extend(self._create_lib(pkg, build, crate_type))
108106

@@ -139,11 +137,12 @@ def _dep_package(self, dep: Dependency) -> PackageState:
139137
def _load_manifest(self, subdir: str) -> Manifest:
140138
manifest_ = self.manifests.get(subdir)
141139
if not manifest_:
142-
filename = os.path.join(self.environment.source_dir, subdir, 'Cargo.toml')
140+
path = os.path.join(self.environment.source_dir, subdir)
141+
filename = os.path.join(path, 'Cargo.toml')
143142
toml = load_toml(filename)
144143
if 'package' in toml:
145144
raw_manifest = T.cast('raw.Manifest', toml)
146-
manifest_ = Manifest.from_raw(raw_manifest)
145+
manifest_ = Manifest.from_raw(raw_manifest, path)
147146
self.manifests[subdir] = manifest_
148147
else:
149148
raise MesonException(f'{subdir}/Cargo.toml does not have [package] section')

mesonbuild/cargo/manifest.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,17 +341,17 @@ class Manifest:
341341
"""
342342

343343
package: Package
344-
dependencies: T.Dict[str, Dependency]
345-
dev_dependencies: T.Dict[str, Dependency]
346-
build_dependencies: T.Dict[str, Dependency]
344+
dependencies: T.Dict[str, Dependency] = dataclasses.field(default_factory=dict)
345+
dev_dependencies: T.Dict[str, Dependency] = dataclasses.field(default_factory=dict)
346+
build_dependencies: T.Dict[str, Dependency] = dataclasses.field(default_factory=dict)
347347
system_dependencies: T.Dict[str, SystemDependency] = dataclasses.field(init=False)
348-
lib: Library
349-
bin: T.List[Binary]
350-
test: T.List[Test]
351-
bench: T.List[Benchmark]
352-
example: T.List[Example]
353-
features: T.Dict[str, T.List[str]]
354-
target: T.Dict[str, T.Dict[str, Dependency]]
348+
lib: T.Optional[Library] = None
349+
bin: T.List[Binary] = dataclasses.field(default_factory=list)
350+
test: T.List[Test] = dataclasses.field(default_factory=list)
351+
bench: T.List[Benchmark] = dataclasses.field(default_factory=list)
352+
example: T.List[Example] = dataclasses.field(default_factory=list)
353+
features: T.Dict[str, T.List[str]] = dataclasses.field(default_factory=dict)
354+
target: T.Dict[str, T.Dict[str, Dependency]] = dataclasses.field(default_factory=dict)
355355

356356
path: str = ''
357357

@@ -361,12 +361,18 @@ def __post_init__(self) -> None:
361361

362362
@classmethod
363363
def from_raw(cls, raw: raw.Manifest, path: str = '') -> Self:
364+
# Libs are always auto-discovered and there's no other way to handle them,
365+
# which is unfortunate for reproducability
366+
pkg = Package.from_raw(raw['package'])
367+
if pkg.autolib and 'lib' not in raw and \
368+
os.path.exists(os.path.join(path, 'src/lib.rs')):
369+
raw['lib'] = {}
364370
return cls(
365-
package=Package.from_raw(raw['package']),
371+
package=pkg,
366372
dependencies={k: Dependency.from_raw(k, v) for k, v in raw.get('dependencies', {}).items()},
367373
dev_dependencies={k: Dependency.from_raw(k, v) for k, v in raw.get('dev-dependencies', {}).items()},
368374
build_dependencies={k: Dependency.from_raw(k, v) for k, v in raw.get('build-dependencies', {}).items()},
369-
lib=Library.from_raw(raw.get('lib', {}), raw['package']['name']),
375+
lib=Library.from_raw(raw['lib'], raw['package']['name']) if 'lib' in raw else None,
370376
bin=[Binary.from_raw(b) for b in raw.get('bin', {})],
371377
test=[Test.from_raw(b) for b in raw.get('test', {})],
372378
bench=[Benchmark.from_raw(b) for b in raw.get('bench', {})],

0 commit comments

Comments
 (0)