Skip to content

Commit b4d1741

Browse files
committed
cargo: autodetect patch directory
Many crates that have a build.rs will usually require an overlay with the translated meson/meson.build file. To avoid having to create a .wrap file, when parsing Cargo.lock autodetect a directory in subproject/packagefiles/ and add it as the patch_directory. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent ad0ec84 commit b4d1741

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

docs/markdown/Wrap-dependency-system-manual.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,13 @@ Some naming conventions need to be respected:
364364
- The `features` variable is pre-defined and contains the list of features enabled
365365
on this crate.
366366

367-
Since *1.5.0* Cargo wraps can also be provided with `Cargo.lock` file at the root
368-
of (sub)project source tree. Meson will automatically load that file and convert
369-
it into a series of wraps definitions.
367+
Since *1.5.0* Cargo wraps can also be provided with `Cargo.lock` file at the
368+
root of (sub)project source tree. Meson will automatically load that file, looking
369+
for crates found on `crates.io` or in a git repository, and will convert those
370+
crates into a series of wraps definitions. Since *1.11.0* the overlay directory
371+
(`patch_directory`) is automatically detected, using the same directory as the
372+
dependency name for `crates.io` URLs, and the final component of the URL
373+
(possibly with the `.git` suffix removed) for "git" URLs.
370374

371375
Since *1.10.0* Workspace Cargo.toml are supported. For the time being it is
372376
recommended to regroup all Cargo dependencies inside a single workspace invoked

mesonbuild/cargo/interpreter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ def load_cargo_lock(filename: str, subproject_dir: str) -> T.Optional[CargoLock]
819819
toml = load_toml(filename)
820820
raw_cargolock = T.cast('raw.CargoLock', toml)
821821
cargolock = CargoLock.from_raw(raw_cargolock)
822+
packagefiles_dir = os.path.join(subproject_dir, 'packagefiles')
822823
wraps: T.Dict[str, PackageDefinition] = {}
823824
for package in cargolock.package:
824825
meson_depname = _dependency_name(package.name, version.api(package.version))
@@ -832,6 +833,7 @@ def load_cargo_lock(filename: str, subproject_dir: str) -> T.Optional[CargoLock]
832833
url = f'https://crates.io/api/v1/crates/{package.name}/{package.version}/download'
833834
directory = f'{package.name}-{package.version}'
834835
name = meson_depname
836+
wrap_type = 'file'
835837
cfg = {
836838
'directory': directory,
837839
'source_url': url,
@@ -842,17 +844,19 @@ def load_cargo_lock(filename: str, subproject_dir: str) -> T.Optional[CargoLock]
842844
elif package.source.startswith('git+'):
843845
url, revision, directory = _parse_git_url(package.source)
844846
name = directory
847+
wrap_type = 'git'
845848
cfg = {
846849
'url': url,
847850
'revision': revision,
848851
'method': 'cargo',
849852
}
850-
wraps[directory].add_provided_dep(meson_depname)
851853
else:
852854
mlog.warning(f'Unsupported source URL in {filename}: {package.source}')
853855
continue
856+
if os.path.isdir(os.path.join(packagefiles_dir, name)):
857+
cfg['patch_directory'] = name
854858
if directory not in wraps:
855-
wraps[directory] = PackageDefinition.from_values(name, subproject_dir, 'file', cfg)
859+
wraps[directory] = PackageDefinition.from_values(name, subproject_dir, wrap_type, cfg)
856860
wraps[directory].add_provided_dep(meson_depname)
857861
cargolock.wraps = {w.name: w for w in wraps.values()}
858862
return cargolock

0 commit comments

Comments
 (0)