Skip to content

Commit dca8b07

Browse files
committed
cargo: load a toplevel Cargo.lock
1 parent 042b4ac commit dca8b07

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Common `Cargo.lock` for all Cargo subprojects
2+
3+
Whenever Meson finds a `Cargo.lock` file in the toplevel directory
4+
of the project, it will use it to resolve the versions of Cargo
5+
subprojects in preference to per-subproject `Cargo.lock` files.
6+
Per-subproject lock files are only used if the invoking project
7+
did not have a `Cargo.lock` file itself.
8+
9+
If you wish to experiment with Cargo subprojects, it is recommended
10+
to use `cargo` to set up `Cargo.lock` and `Cargo.toml` files,
11+
encompassing all Rust targets, in the toplevel source directory.
12+
Cargo subprojects remain unstable and subject to change.

mesonbuild/interpreter/interpreter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ def __init__(
316316
def __getnewargs_ex__(self) -> T.Tuple[T.Tuple[object], T.Dict[str, object]]:
317317
raise MesonBugException('This class is unpicklable')
318318

319+
def load_root_cargo_lock_file(self) -> None:
320+
cargo_lock_filename = os.path.join(self.subdir, 'Cargo.lock')
321+
cargo_lock = os.path.join(self.source_root, cargo_lock_filename)
322+
if not os.path.isfile(cargo_lock):
323+
return
324+
from .. import cargo
325+
try:
326+
self.cargo = cargo.Interpreter(self.environment, self.subdir, self.subproject_dir)
327+
except cargo.TomlImplementationMissing as e:
328+
# error delayed to actual usage of a Cargo subproject
329+
mlog.warning(f'cannot load Cargo.lock: {e}', fatal=False)
330+
319331
def _redetect_machines(self) -> None:
320332
# Re-initialize machine descriptions. We can do a better job now because we
321333
# have the compilers needed to gain more knowledge, so wipe out old
@@ -1301,6 +1313,9 @@ def func_project(self, node: mparser.FunctionNode, args: T.Tuple[str, T.List[str
13011313
assert self.environment.wrap_resolver is not None, 'for mypy'
13021314
self.environment.wrap_resolver.load_and_merge(subprojects_dir, self.subproject)
13031315

1316+
if self.cargo is None:
1317+
self.load_root_cargo_lock_file()
1318+
13041319
self.build.projects[self.subproject] = proj_name
13051320
mlog.log('Project name:', mlog.bold(proj_name))
13061321
mlog.log('Project version:', mlog.bold(self.project_version))

0 commit comments

Comments
 (0)