| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright © 2022-2024 Intel Corporation |
| 3 |
|
| 4 |
"""Interpreter for converting Cargo Toml definitions to Meson AST |
| 5 |
|
| 6 |
There are some notable limits here. We don't even try to convert something with |
| 7 |
a build.rs: there's so few limits on what Cargo allows a build.rs (basically |
| 8 |
none), and no good way for us to convert them. In that case, an actual meson |
| 9 |
port will be required. |
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
import dataclasses |
| 14 |
import functools |
| 15 |
import itertools |
| 16 |
import os |
| 17 |
import pathlib |
| 18 |
import collections |
| 19 |
import urllib.parse |
| 20 |
import typing as T |
| 21 |
from pathlib import PurePath |
| 22 |
|
| 23 |
from . import builder, version |
| 24 |
from .cfg import eval_cfg |
| 25 |
from .toml import load_toml |
| 26 |
from .manifest import ( |
| 27 |
Manifest, CargoLock, CargoLockPackage, Workspace, fixup_meson_varname, |
| 28 |
validate_patch, |
| 29 |
) |
| 30 |
from ..mesonlib import ( |
| 31 |
is_parent_path, lazy_property, MesonException, MachineChoice, |
| 32 |
PerMachine, unique_list, SubProject, |
| 33 |
) |
| 34 |
from .. import coredata, mlog |
| 35 |
from ..wrap.wrap import PackageDefinition |
| 36 |
|
| 37 |
if T.TYPE_CHECKING: |
| 38 |
from . import raw |
| 39 |
from .. import mparser |
| 40 |
from typing_extensions import Literal |
| 41 |
|
| 42 |
from .manifest import Dependency |
| 43 |
from ..environment import Environment |
| 44 |
from ..compilers.rust import RustCompiler |
| 45 |
|
| 46 |
RUST_ABI = Literal['rust', 'c', 'proc-macro'] |
| 47 |
|
| 48 |
def _dependency_name(package_name: str, api: str, suffix: str = '-rs') -> str: |
| 49 |
basename = package_name[:-len(suffix)] if suffix and package_name.endswith(suffix) else package_name |
| 50 |
return f'{basename}-{api}{suffix}' |
| 51 |
|
| 52 |
|
| 53 |
def _extra_args_varname() -> str: |
| 54 |
return 'extra_args' |
| 55 |
|
| 56 |
|
| 57 |
def _extra_deps_varname() -> str: |
| 58 |
return 'extra_deps' |
| 59 |
|
| 60 |
|
| 61 |
@dataclasses.dataclass |
| 62 |
class PackageConfiguration: |
| 63 |
"""Configuration for a package during dependency resolution.""" |
| 64 |
for_machine: MachineChoice |
| 65 |
features: T.Set[str] = dataclasses.field(default_factory=set) |
| 66 |
required_deps: T.Set[str] = dataclasses.field(default_factory=set) |
| 67 |
optional_deps_features: T.Dict[str, T.Set[str]] = dataclasses.field(default_factory=lambda: collections.defaultdict(set)) |
| 68 |
# Cache of resolved dependency packages |
| 69 |
dep_packages: T.Dict[PackageKey, PackageState] = dataclasses.field(default_factory=dict) |
| 70 |
|
| 71 |
def get_features_args(self) -> T.List[str]: |
| 72 |
"""Get feature configuration arguments.""" |
| 73 |
args: T.List[str] = [] |
| 74 |
for feature in sorted(self.features): |
| 75 |
args.extend(['--cfg', f'feature="{feature}"']) |
| 76 |
return args |
| 77 |
|
| 78 |
def get_dependency_map(self, manifest: Manifest) -> T.Dict[str, str]: |
| 79 |
"""Get the rust dependency mapping for this package configuration.""" |
| 80 |
dependency_map: T.Dict[str, str] = {} |
| 81 |
for name in sorted(self.required_deps): |
| 82 |
dep = manifest.dependencies[name] |
| 83 |
dep_key = PackageKey(dep.package, dep.api) |
| 84 |
dep_pkg = self.dep_packages[dep_key] |
| 85 |
dep_lib_name = dep_pkg.library_name(self.for_machine) |
| 86 |
dep_crate_name = name if name != dep.package else dep_pkg.manifest.lib.name |
| 87 |
dependency_map[dep_lib_name] = dep_crate_name |
| 88 |
return dependency_map |
| 89 |
|
| 90 |
|
| 91 |
@dataclasses.dataclass |
| 92 |
class PackageState: |
| 93 |
manifest: Manifest |
| 94 |
downloaded: bool = False |
| 95 |
# If this package is member of a workspace. |
| 96 |
ws_subdir: T.Optional[str] = None |
| 97 |
ws_member: T.Optional[str] = None |
| 98 |
# Per-machine configuration state |
| 99 |
cfg: PerMachine[T.Optional[PackageConfiguration]] = dataclasses.field( |
| 100 |
default_factory=lambda: PerMachine(None, None) |
| 101 |
) |
| 102 |
# Subproject name as known to the wrap resolver (may differ from the |
| 103 |
# meson dep name for git sources, where the wrap is named after the |
| 104 |
# git directory rather than the crate name + api version). |
| 105 |
subproject_name: T.Optional[str] = None |
| 106 |
|
| 107 |
@lazy_property |
| 108 |
def path(self) -> T.Optional[str]: |
| 109 |
if not self.ws_subdir: |
| 110 |
return None |
| 111 |
return os.path.normpath(os.path.join(self.ws_subdir, self.ws_member)) |
| 112 |
|
| 113 |
def library_name(self, machine: MachineChoice = MachineChoice.HOST, lib_type: RUST_ABI = 'rust') -> str: |
| 114 |
# Add the API version to the library name to avoid conflicts when multiple |
| 115 |
# versions of the same crate are used. The Ninja backend removed everything |
| 116 |
# after the + to form the crate name. |
| 117 |
name = fixup_meson_varname(self.manifest.package.name) |
| 118 |
suffix = '+build' if machine == MachineChoice.BUILD else '' |
| 119 |
if lib_type == 'c': |
| 120 |
return name |
| 121 |
return f'{name}+{self.manifest.package.api.replace(".", "_")}{suffix}' |
| 122 |
|
| 123 |
def has_both_machines(self) -> bool: |
| 124 |
return bool(self.cfg.host and self.cfg.build) |
| 125 |
|
| 126 |
def get_env_dict(self, environment: Environment, subdir: str) -> T.Dict[str, str]: |
| 127 |
"""Get environment variables for this package.""" |
| 128 |
# Common variables for build.rs and crates |
| 129 |
# https://doc.rust-lang.org/cargo/reference/environment-variables.html |
| 130 |
# OUT_DIR is the directory where build.rs generate files. In our case, |
| 131 |
# it's the directory where meson/meson.build places generated files. |
| 132 |
out_dir = os.path.join(environment.build_dir, subdir, 'meson') |
| 133 |
os.makedirs(out_dir, exist_ok=True) |
| 134 |
version_arr = self.manifest.package.version.split('.') |
| 135 |
version_arr += [''] * (4 - len(version_arr)) |
| 136 |
|
| 137 |
return { |
| 138 |
'OUT_DIR': out_dir, |
| 139 |
'CARGO_MANIFEST_DIR': os.path.join(environment.source_dir, subdir), |
| 140 |
'CARGO_MANIFEST_PATH': os.path.join(environment.source_dir, subdir, 'Cargo.toml'), |
| 141 |
'CARGO_PKG_VERSION': self.manifest.package.version, |
| 142 |
'CARGO_PKG_VERSION_MAJOR': version_arr[0], |
| 143 |
'CARGO_PKG_VERSION_MINOR': version_arr[1], |
| 144 |
'CARGO_PKG_VERSION_PATCH': version_arr[2], |
| 145 |
'CARGO_PKG_VERSION_PRE': version_arr[3], |
| 146 |
'CARGO_PKG_AUTHORS': ','.join(self.manifest.package.authors), |
| 147 |
'CARGO_PKG_NAME': self.manifest.package.name, |
| 148 |
# FIXME: description can contain newlines which breaks ninja. |
| 149 |
#'CARGO_PKG_DESCRIPTION': self.manifest.package.description or '', |
| 150 |
'CARGO_PKG_HOMEPAGE': self.manifest.package.homepage or '', |
| 151 |
'CARGO_PKG_REPOSITORY': self.manifest.package.repository or '', |
| 152 |
'CARGO_PKG_LICENSE': self.manifest.package.license or '', |
| 153 |
'CARGO_PKG_LICENSE_FILE': self.manifest.package.license_file or '', |
| 154 |
'CARGO_PKG_RUST_VERSION': self.manifest.package.rust_version or '', |
| 155 |
'CARGO_PKG_README': self.manifest.package.readme or '', |
| 156 |
'CARGO_CRATE_NAME': fixup_meson_varname(self.manifest.package.name), |
| 157 |
} |
| 158 |
|
| 159 |
def get_lint_args(self, rustc: RustCompiler) -> T.List[str]: |
| 160 |
"""Get lint arguments for this package.""" |
| 161 |
args: T.List[str] = [] |
| 162 |
has_check_cfg = rustc.has_check_cfg |
| 163 |
|
| 164 |
for lint in self.manifest.lints: |
| 165 |
args.extend(lint.to_arguments(has_check_cfg)) |
| 166 |
|
| 167 |
if has_check_cfg: |
| 168 |
args.extend(['--check-cfg', 'cfg(docsrs)', |
| 169 |
'--check-cfg', 'cfg(test)']) |
| 170 |
for feature in self.manifest.features: |
| 171 |
if feature != 'default': |
| 172 |
args.append('--check-cfg') |
| 173 |
args.append(f'cfg(feature,values("{feature}"))') |
| 174 |
for name in self.manifest.system_dependencies: |
| 175 |
args.append('--check-cfg') |
| 176 |
args.append(f'cfg(system_deps_have_{fixup_meson_varname(name)})') |
| 177 |
|
| 178 |
return args |
| 179 |
|
| 180 |
def get_env_args(self, rustc: RustCompiler, environment: Environment, subdir: str) -> T.List[str]: |
| 181 |
"""Get environment variable arguments for rustc.""" |
| 182 |
enable_env_set_args = rustc.enable_env_set_args() |
| 183 |
if enable_env_set_args is None: |
| 184 |
return [] |
| 185 |
|
| 186 |
env_dict = self.get_env_dict(environment, subdir) |
| 187 |
env_args = list(enable_env_set_args) |
| 188 |
for k, v in env_dict.items(): |
| 189 |
env_args.extend(['--env-set', f'{k}={v}']) |
| 190 |
return env_args |
| 191 |
|
| 192 |
def get_rustc_args(self, environment: Environment, subdir: str, machine: MachineChoice) -> T.List[str]: |
| 193 |
"""Get rustc arguments for this package.""" |
| 194 |
if not environment.is_cross_build(): |
| 195 |
machine = MachineChoice.HOST |
| 196 |
|
| 197 |
rustc = T.cast('RustCompiler', environment.coredata.compilers[machine]['rust']) |
| 198 |
cfg = self.cfg[machine] |
| 199 |
|
| 200 |
args: T.List[str] = [] |
| 201 |
args.extend(self.get_lint_args(rustc)) |
| 202 |
args.extend(cfg.get_features_args()) |
| 203 |
args.extend(self.get_env_args(rustc, environment, subdir)) |
| 204 |
return args |
| 205 |
|
| 206 |
def supported_abis(self) -> T.Set[RUST_ABI]: |
| 207 |
"""Return which ABIs are exposed by the package's crate_types.""" |
| 208 |
crate_types = self.manifest.lib.crate_type |
| 209 |
abis: T.Set[RUST_ABI] = set() |
| 210 |
if any(ct in {'lib', 'rlib', 'dylib'} for ct in crate_types): |
| 211 |
abis.add('rust') |
| 212 |
if any(ct in {'staticlib', 'cdylib'} for ct in crate_types): |
| 213 |
abis.add('c') |
| 214 |
if 'proc-macro' in crate_types: |
| 215 |
abis.add('proc-macro') |
| 216 |
return abis |
| 217 |
|
| 218 |
def get_subproject_name(self) -> SubProject: |
| 219 |
if self.subproject_name is not None: |
| 220 |
return SubProject(self.subproject_name) |
| 221 |
dep = _dependency_name(self.manifest.package.name, self.manifest.package.api) |
| 222 |
return SubProject(dep) |
| 223 |
|
| 224 |
def abi_resolve_default(self, rust_abi: T.Optional[RUST_ABI]) -> RUST_ABI: |
| 225 |
supported_abis = self.supported_abis() |
| 226 |
if rust_abi is None: |
| 227 |
if len(supported_abis) > 1: |
| 228 |
raise MesonException(f'Package {self.manifest.package.name} support more than one ABI') |
| 229 |
return next(iter(supported_abis)) |
| 230 |
else: |
| 231 |
if rust_abi not in supported_abis: |
| 232 |
raise MesonException(f'Package {self.manifest.package.name} does not support ABI {rust_abi}') |
| 233 |
return rust_abi |
| 234 |
|
| 235 |
def abi_has_shared(self, rust_abi: RUST_ABI) -> bool: |
| 236 |
if rust_abi == 'proc-macro': |
| 237 |
return True |
| 238 |
return ('cdylib' if rust_abi == 'c' else 'dylib') in self.manifest.lib.crate_type |
| 239 |
|
| 240 |
def abi_has_static(self, rust_abi: RUST_ABI) -> bool: |
| 241 |
if rust_abi == 'proc-macro': |
| 242 |
return False |
| 243 |
crate_type = self.manifest.lib.crate_type |
| 244 |
if rust_abi == 'c': |
| 245 |
return 'staticlib' in crate_type |
| 246 |
return 'lib' in crate_type or 'rlib' in crate_type |
| 247 |
|
| 248 |
def get_dependency_name(self, rust_abi: T.Optional[RUST_ABI]) -> str: |
| 249 |
"""Get the dependency name for a package with the given ABI.""" |
| 250 |
rust_abi = self.abi_resolve_default(rust_abi) |
| 251 |
package_name = self.manifest.package.name |
| 252 |
api = self.manifest.package.api |
| 253 |
|
| 254 |
if rust_abi in {'rust', 'proc-macro'}: |
| 255 |
return _dependency_name(package_name, api) |
| 256 |
elif rust_abi == 'c': |
| 257 |
return _dependency_name(package_name, api, '') |
| 258 |
else: |
| 259 |
raise MesonException(f'Unknown rust_abi: {rust_abi}') |
| 260 |
|
| 261 |
def get_rust_dependency_name(self) -> str: |
| 262 |
"""Get the dependency name for a package with the rust or proc-macro ABI.""" |
| 263 |
supported_abis = self.supported_abis() |
| 264 |
package_name = self.manifest.package.name |
| 265 |
if 'rust' in supported_abis or 'proc-macro' in supported_abis: |
| 266 |
return _dependency_name(package_name, self.manifest.package.api) |
| 267 |
raise MesonException(f'Package {package_name} does not support rust or proc-macro ABI') |
| 268 |
|
| 269 |
@dataclasses.dataclass(frozen=True) |
| 270 |
class PackageKey: |
| 271 |
package_name: str |
| 272 |
api: str |
| 273 |
|
| 274 |
|
| 275 |
@dataclasses.dataclass |
| 276 |
class WorkspaceState: |
| 277 |
workspace: Workspace |
| 278 |
subdir: str |
| 279 |
downloaded: bool = False |
| 280 |
# member path -> PackageState, for all members of this workspace |
| 281 |
packages: T.Dict[str, PackageState] = dataclasses.field(default_factory=dict) |
| 282 |
# package name to member path, for all members of this workspace |
| 283 |
packages_to_member: T.Dict[str, str] = dataclasses.field(default_factory=dict) |
| 284 |
# member paths that are required to be built |
| 285 |
required_members: T.List[str] = dataclasses.field(default_factory=list) |
| 286 |
|
| 287 |
|
| 288 |
class Interpreter: |
| 289 |
_features: T.Optional[T.List[str]] = None |
| 290 |
|
| 291 |
def __init__(self, env: Environment, subdir: str, subprojects_dir: str) -> None: |
| 292 |
self.environment = env |
| 293 |
self.subprojects_dir = subprojects_dir |
| 294 |
# Map Cargo.toml's subdir to loaded manifest. |
| 295 |
self.manifests: T.Dict[str, T.Union[Manifest, Workspace]] = {} |
| 296 |
# Map of cargo package (name + api) to its state |
| 297 |
self.packages: T.Dict[PackageKey, PackageState] = {} |
| 298 |
# Map subdir to workspace |
| 299 |
self.workspaces: T.Dict[str, WorkspaceState] = {} |
| 300 |
# Files that should trigger a reconfigure if modified |
| 301 |
self.build_def_files: T.List[str] = [] |
| 302 |
# Cargo packages |
| 303 |
filename = os.path.join(self.environment.get_source_dir(), subdir, 'Cargo.lock') |
| 304 |
subprojects_dir = os.path.join(self.environment.get_source_dir(), subdir, subprojects_dir) |
| 305 |
self.cargolock = load_cargo_lock(filename, subprojects_dir) |
| 306 |
if self.cargolock: |
| 307 |
self.environment.wrap_resolver.merge_wraps(self.cargolock.wraps) |
| 308 |
self.build_def_files.append(filename) |
| 309 |
|
| 310 |
@property |
| 311 |
def is_cross(self) -> bool: |
| 312 |
return self.environment.is_cross_build() |
| 313 |
|
| 314 |
@property |
| 315 |
def features(self) -> T.List[str]: |
| 316 |
"""Get the features list. Once read, it cannot be modified.""" |
| 317 |
if self._features is None: |
| 318 |
self._features = ['default'] |
| 319 |
return self._features |
| 320 |
|
| 321 |
@features.setter |
| 322 |
def features(self, value: T.List[str]) -> None: |
| 323 |
"""Set the features list. Can only be set before first read.""" |
| 324 |
value_unique = sorted(unique_list(value)) |
| 325 |
if self._features is not None and value_unique != self._features: |
| 326 |
raise MesonException("Cannot modify features after they have been selected or used") |
| 327 |
self._features = value_unique |
| 328 |
|
| 329 |
def get_build_def_files(self) -> T.List[str]: |
| 330 |
return self.build_def_files |
| 331 |
|
| 332 |
def load_workspace(self, subdir: str, extra_members: T.Optional[T.List[str]]) -> WorkspaceState: |
| 333 |
"""Load the root Cargo.toml package and prepare it with features and dependencies.""" |
| 334 |
subdir = os.path.normpath(subdir) |
| 335 |
manifest, cached = self._load_manifest(subdir) |
| 336 |
ws = self._get_workspace(manifest, subdir, extra_members, False) |
| 337 |
if not cached: |
| 338 |
# [patch] only takes effect in the top-level Cargo.toml |
| 339 |
for warning in validate_patch(ws.workspace.patch, ws.packages_to_member): |
| 340 |
mlog.warning(warning) |
| 341 |
if ws.workspace.profile: |
| 342 |
mlog.warning('[profile] entries are not implemented yet') |
| 343 |
|
| 344 |
self._prepare_entry_point(ws) |
| 345 |
return ws |
| 346 |
|
| 347 |
def _prepare_entry_point(self, ws: WorkspaceState) -> None: |
| 348 |
pkgs = [self._require_workspace_member(ws, m) for m in ws.workspace.default_members] |
| 349 |
for pkg in pkgs: |
| 350 |
for machine in pkg.manifest.machines_from(MachineChoice.HOST, bin=True, is_cross=self.is_cross): |
| 351 |
self._prepare_package(pkg, machine) |
| 352 |
for feature in self.features: |
| 353 |
self._enable_feature(pkg, feature, machine) |
| 354 |
|
| 355 |
def load_package(self, ws: WorkspaceState, package_name: T.Optional[str]) -> PackageState: |
| 356 |
if package_name is None: |
| 357 |
if not ws.workspace.root_package: |
| 358 |
raise MesonException('no root package in workspace') |
| 359 |
path = '.' |
| 360 |
else: |
| 361 |
try: |
| 362 |
path = ws.packages_to_member[package_name] |
| 363 |
except KeyError: |
| 364 |
raise MesonException(f'workspace member "{package_name}" not found') |
| 365 |
|
| 366 |
if is_parent_path(self.subprojects_dir, path): |
| 367 |
raise MesonException('argument to package() cannot be a subproject') |
| 368 |
return ws.packages[path] |
| 369 |
|
| 370 |
def interpret(self, subdir: str, project_root: T.Optional[str] = None) -> mparser.CodeBlockNode: |
| 371 |
filename = os.path.join(self.environment.source_dir, subdir, 'Cargo.toml') |
| 372 |
build = builder.Builder(filename) |
| 373 |
if project_root: |
| 374 |
# this is a subdir() |
| 375 |
manifest, _ = self._load_manifest(subdir) |
| 376 |
assert isinstance(manifest, Manifest) |
| 377 |
return self.interpret_package(manifest, build, subdir, project_root) |
| 378 |
else: |
| 379 |
ws = self.load_workspace(subdir, None) |
| 380 |
return self.interpret_workspace(ws, build, subdir) |
| 381 |
|
| 382 |
def interpret_package(self, manifest: Manifest, build: builder.Builder, subdir: str, project_root: str) -> mparser.CodeBlockNode: |
| 383 |
# Build an AST for this package |
| 384 |
ws = self.workspaces[project_root] |
| 385 |
member = ws.packages_to_member[manifest.package.name] |
| 386 |
pkg = ws.packages[member] |
| 387 |
ast = self._create_package(pkg, build, subdir) |
| 388 |
return build.block(ast) |
| 389 |
|
| 390 |
def _create_package(self, pkg: PackageState, build: builder.Builder, subdir: str) -> T.List[mparser.BaseNode]: |
| 391 |
# proc_macro automatically adds native: true; passing "native: true" |
| 392 |
# to cargo_ws.package() is only needed to query the features and |
| 393 |
# pass them to meson/meson.build. |
| 394 |
native = pkg.manifest.lib.crate_type == ['proc-macro'] |
| 395 |
ast: T.List[mparser.BaseNode] = [ |
| 396 |
build.assign(build.method('package', build.identifier('cargo_ws'), |
| 397 |
[build.string(pkg.manifest.package.name)], |
| 398 |
{'native': build.bool(native)}), |
| 399 |
'pkg_obj'), |
| 400 |
build.assign(build.method('features', build.identifier('pkg_obj')), 'features'), |
| 401 |
build.function('message', [ |
| 402 |
build.string('Enabled features:'), |
| 403 |
build.identifier('features'), |
| 404 |
]), |
| 405 |
] |
| 406 |
ast += self._create_meson_subdir(build) |
| 407 |
|
| 408 |
if pkg.manifest.lib: |
| 409 |
crate_type = pkg.manifest.lib.crate_type |
| 410 |
if 'dylib' in crate_type and 'cdylib' in crate_type: |
| 411 |
raise MesonException('Cannot build both dylib and cdylib due to file name conflict') |
| 412 |
for abi in pkg.supported_abis(): |
| 413 |
ast.extend(self._create_lib(pkg, build, subdir, abi)) |
| 414 |
|
| 415 |
return ast |
| 416 |
|
| 417 |
def interpret_workspace(self, ws: WorkspaceState, build: builder.Builder, subdir: str) -> mparser.CodeBlockNode: |
| 418 |
name = os.path.basename(subdir) |
| 419 |
subprojects_dir = os.path.join(subdir, 'subprojects') |
| 420 |
self.environment.wrap_resolver.load_and_merge(subprojects_dir, SubProject(name)) |
| 421 |
ast: T.List[mparser.BaseNode] = [] |
| 422 |
|
| 423 |
# Call subdir() for each required member of the workspace. The order is |
| 424 |
# important, if a member depends on another member, that member must be |
| 425 |
# processed first. |
| 426 |
processed_members: T.Dict[str, PackageState] = {} |
| 427 |
|
| 428 |
def _process_member(member: str) -> None: |
| 429 |
if member in processed_members: |
| 430 |
return |
| 431 |
pkg = ws.packages[member] |
| 432 |
# Process dependencies for all configured machines |
| 433 |
found = False |
| 434 |
for machine in MachineChoice: |
| 435 |
cfg = pkg.cfg[machine] |
| 436 |
if not cfg: |
| 437 |
continue |
| 438 |
for depname in cfg.required_deps: |
| 439 |
dep = pkg.manifest.dependencies[depname] |
| 440 |
if dep.path: |
| 441 |
dep_member = os.path.normpath(os.path.join(pkg.ws_member, dep.path)) |
| 442 |
_process_member(dep_member) |
| 443 |
found = True |
| 444 |
if not found: |
| 445 |
raise MesonException(f'Package {pkg.manifest.package.name!r} is not enabled for this build ' |
| 446 |
'configuration. Maybe you forgot to enable a Cargo feature, or to check ' |
| 447 |
'a Meson option?') |
| 448 |
if member == '.': |
| 449 |
ast.extend(self._create_package(pkg, build, subdir)) |
| 450 |
elif is_parent_path(self.subprojects_dir, member): |
| 451 |
depname = _dependency_name(pkg.manifest.package.name, pkg.manifest.package.api) |
| 452 |
ast.append(build.function('subproject', [build.string(depname)])) |
| 453 |
else: |
| 454 |
ast.append(build.function('subdir', [build.string(member)])) |
| 455 |
processed_members[member] = pkg |
| 456 |
|
| 457 |
for member in ws.required_members: |
| 458 |
_process_member(member) |
| 459 |
ast = self._create_project(name, processed_members.get('.'), build) + ast |
| 460 |
return build.block(ast) |
| 461 |
|
| 462 |
def _load_workspace_member(self, ws: WorkspaceState, m: str) -> None: |
| 463 |
m = os.path.normpath(m) |
| 464 |
if m in ws.packages: |
| 465 |
return |
| 466 |
# Load member's manifest |
| 467 |
m_subdir = os.path.join(ws.subdir, m) |
| 468 |
manifest_, _ = self._load_manifest(m_subdir, ws.workspace, m) |
| 469 |
assert isinstance(manifest_, Manifest) |
| 470 |
self._add_workspace_member(manifest_, ws, m) |
| 471 |
|
| 472 |
def _add_workspace_member(self, manifest_: Manifest, ws: WorkspaceState, m: str) -> None: |
| 473 |
key = PackageKey(manifest_.package.name, manifest_.package.api) |
| 474 |
ws.packages_to_member[manifest_.package.name] = m |
| 475 |
if key in self.packages: |
| 476 |
ws.packages[m] = self.packages[key] |
| 477 |
self._require_workspace_member(ws, m) |
| 478 |
else: |
| 479 |
ws.packages[m] = PackageState(manifest_, ws_subdir=ws.subdir, ws_member=m, downloaded=ws.downloaded) |
| 480 |
|
| 481 |
def _get_workspace(self, manifest: T.Union[Workspace, Manifest], subdir: str, extra_members: T.Optional[T.List[str]], downloaded: bool) -> WorkspaceState: |
| 482 |
ws = self.workspaces.get(subdir) |
| 483 |
if ws: |
| 484 |
return ws |
| 485 |
workspace = manifest if isinstance(manifest, Workspace) else \ |
| 486 |
Workspace(root_package=manifest, members=['.'], default_members=['.']) |
| 487 |
ws = WorkspaceState(workspace, subdir, downloaded=downloaded) |
| 488 |
if workspace.root_package: |
| 489 |
self._add_workspace_member(workspace.root_package, ws, '.') |
| 490 |
|
| 491 |
if extra_members is not None: |
| 492 |
for m in extra_members: |
| 493 |
m = PurePath(m).as_posix() |
| 494 |
if m not in workspace.members: |
| 495 |
l = ', '.join(sorted(list(workspace.members))) |
| 496 |
raise MesonException(f'{m} is not a workspace member for {subdir}/Cargo.toml (valid members are {l})') |
| 497 |
if m not in workspace.default_members: |
| 498 |
workspace.default_members.append(m) |
| 499 |
for m in workspace.members: |
| 500 |
self._load_workspace_member(ws, m) |
| 501 |
self.workspaces[subdir] = ws |
| 502 |
return ws |
| 503 |
|
| 504 |
def _record_package(self, pkg: PackageState) -> None: |
| 505 |
key = PackageKey(pkg.manifest.package.name, pkg.manifest.package.api) |
| 506 |
if key not in self.packages: |
| 507 |
self.packages[key] = pkg |
| 508 |
|
| 509 |
def _require_workspace_member(self, ws: WorkspaceState, member: str) -> PackageState: |
| 510 |
member = os.path.normpath(member) |
| 511 |
pkg = ws.packages[member] |
| 512 |
if member not in ws.required_members: |
| 513 |
self._record_package(pkg) |
| 514 |
ws.required_members.append(member) |
| 515 |
return pkg |
| 516 |
|
| 517 |
def _fetch_package(self, package_name: str, api: str) -> PackageState: |
| 518 |
key = PackageKey(package_name, api) |
| 519 |
pkg = self.packages.get(key) |
| 520 |
if pkg: |
| 521 |
return pkg |
| 522 |
return self._fetch_package_from_provider(package_name, api) |
| 523 |
|
| 524 |
def _resolve_package(self, package_name: str, accepts_version: T.Callable[[str], bool]) -> \ |
| 525 |
T.Optional[CargoLockPackage]: |
| 526 |
"""From all available versions from Cargo.lock, pick the most recent |
| 527 |
satisfying the constraints and return it.""" |
| 528 |
if not self.cargolock: |
| 529 |
return None |
| 530 |
|
| 531 |
for cargo_pkg in self.cargolock.named(package_name): |
| 532 |
if accepts_version(cargo_pkg.version): |
| 533 |
return cargo_pkg |
| 534 |
return None |
| 535 |
|
| 536 |
def resolve_package(self, package_name: str, api: str) -> T.Optional[PackageState]: |
| 537 |
cargo_pkg = self._resolve_package(package_name, version.cargo_parse(api)) |
| 538 |
if not cargo_pkg: |
| 539 |
return None |
| 540 |
api = version.api(cargo_pkg.version) |
| 541 |
return self._fetch_package(package_name, api) |
| 542 |
|
| 543 |
def _fetch_package_from_provider(self, package_name: str, api: str) -> PackageState: |
| 544 |
meson_depname = _dependency_name(package_name, api) |
| 545 |
subp_name, _ = self.environment.wrap_resolver.find_dep_provider(meson_depname) |
| 546 |
if subp_name is None: |
| 547 |
if self.cargolock is None: |
| 548 |
raise MesonException(f'Dependency {meson_depname!r} not found in any wrap files.') |
| 549 |
# If Cargo.lock has a different version, this could be a resolution |
| 550 |
# bug, but maybe also a version mismatch? I am not sure yet... |
| 551 |
similar_deps = [pkg.subproject |
| 552 |
for pkg in self.cargolock.named(package_name)] |
| 553 |
if similar_deps: |
| 554 |
similar_msg = f'Cargo.lock provides: {", ".join(similar_deps)}.' |
| 555 |
else: |
| 556 |
similar_msg = 'Cargo.lock does not contain this crate name.' |
| 557 |
raise MesonException(f'Dependency {meson_depname!r} not found in any wrap files or Cargo.lock; {similar_msg} This could be a Meson bug, please report it.') |
| 558 |
|
| 559 |
return self._fetch_package_from_subproject(package_name, subp_name) |
| 560 |
|
| 561 |
def _fetch_package_from_subproject(self, package_name: str, subp_name: str) -> PackageState: |
| 562 |
subdir, _ = self.environment.wrap_resolver.resolve(subp_name) |
| 563 |
subprojects_dir = os.path.join(subdir, 'subprojects') |
| 564 |
self.environment.wrap_resolver.load_and_merge(subprojects_dir, SubProject(subp_name)) |
| 565 |
manifest, _ = self._load_manifest(subdir) |
| 566 |
downloaded = \ |
| 567 |
subp_name in self.environment.wrap_resolver.wraps and \ |
| 568 |
self.environment.wrap_resolver.wraps[subp_name].type is not None |
| 569 |
|
| 570 |
ws = self._get_workspace(manifest, subdir, None, downloaded=downloaded) |
| 571 |
member = ws.packages_to_member[package_name] |
| 572 |
pkg = self._require_workspace_member(ws, member) |
| 573 |
pkg.subproject_name = subp_name |
| 574 |
return pkg |
| 575 |
|
| 576 |
def _prepare_package(self, pkg: PackageState, machine: MachineChoice) -> None: |
| 577 |
key = PackageKey(pkg.manifest.package.name, pkg.manifest.package.api) |
| 578 |
assert key in self.packages |
| 579 |
if pkg.cfg[machine] is not None: |
| 580 |
return # Already prepared for this machine |
| 581 |
|
| 582 |
pkg.cfg[machine] = PackageConfiguration(for_machine=machine) |
| 583 |
# Merge target-specific dependencies that are enabled for this machine |
| 584 |
target_cfgs = self._get_cfgs(machine) |
| 585 |
for condition, dependencies in pkg.manifest.target.items(): |
| 586 |
if eval_cfg(condition, target_cfgs): |
| 587 |
pkg.manifest.dependencies.update(dependencies) |
| 588 |
|
| 589 |
# If you specify the optional dependency with the dep: prefix anywhere in the [features] |
| 590 |
# table, that disables the implicit feature. |
| 591 |
deps = set(feature[4:] |
| 592 |
for feature in itertools.chain.from_iterable(pkg.manifest.features.values()) |
| 593 |
if feature.startswith('dep:')) |
| 594 |
for name, dep in itertools.chain(pkg.manifest.dependencies.items(), |
| 595 |
pkg.manifest.dev_dependencies.items(), |
| 596 |
pkg.manifest.build_dependencies.items()): |
| 597 |
if dep.optional and name not in deps: |
| 598 |
pkg.manifest.features.setdefault(name, []) |
| 599 |
pkg.manifest.features[name].append(f'dep:{name}') |
| 600 |
deps.add(name) |
| 601 |
|
| 602 |
# Fetch required dependencies recursively for this machine |
| 603 |
for depname, dep in pkg.manifest.dependencies.items(): |
| 604 |
if not dep.optional: |
| 605 |
self._add_dependency(pkg, depname, machine) |
| 606 |
|
| 607 |
def _dep_package(self, pkg: PackageState, dep: Dependency, cfg: PackageConfiguration) -> PackageState: |
| 608 |
if dep.path: |
| 609 |
ws = self.workspaces[pkg.ws_subdir] |
| 610 |
dep_member = os.path.normpath(os.path.join(pkg.ws_member, dep.path)) |
| 611 |
if is_parent_path(self.subprojects_dir, dep_member): |
| 612 |
if len(pathlib.PurePath(dep_member).parts) != 2: |
| 613 |
raise MesonException('found "{self.subprojects_dir}" in path but it is not a valid subproject path') |
| 614 |
self._load_workspace_member(ws, dep_member) |
| 615 |
dep_pkg = self._require_workspace_member(ws, dep_member) |
| 616 |
elif dep.git: |
| 617 |
_, _, directory = _parse_git_url(dep.git, dep.branch) |
| 618 |
dep_pkg = self._fetch_package_from_subproject(dep.package, directory) |
| 619 |
else: |
| 620 |
cargo_pkg = self._resolve_package(dep.package, dep.accepts_version) |
| 621 |
if cargo_pkg: |
| 622 |
dep.update_version(f'={cargo_pkg.version}') |
| 623 |
dep_pkg = self._fetch_package(dep.package, dep.api) |
| 624 |
|
| 625 |
if not dep.version: |
| 626 |
dep.update_version(f'={dep_pkg.manifest.package.version}') |
| 627 |
|
| 628 |
dep_key = PackageKey(dep.package, dep.api) |
| 629 |
cfg.dep_packages.setdefault(dep_key, dep_pkg) |
| 630 |
assert cfg.dep_packages[dep_key] == dep_pkg |
| 631 |
return dep_pkg |
| 632 |
|
| 633 |
def _load_manifest(self, subdir: str, workspace: T.Optional[Workspace] = None, member_path: str = '') -> T.Tuple[T.Union[Manifest, Workspace], bool]: |
| 634 |
manifest_ = self.manifests.get(subdir) |
| 635 |
if manifest_: |
| 636 |
return manifest_, True |
| 637 |
path = os.path.join(self.environment.source_dir, subdir) |
| 638 |
filename = os.path.join(path, 'Cargo.toml') |
| 639 |
try: |
| 640 |
raw_manifest = T.cast('raw.Manifest', load_toml(filename)) |
| 641 |
except OSError as e: |
| 642 |
raise MesonException(f'could not load {subdir}/Cargo.toml: {e}') |
| 643 |
|
| 644 |
self.build_def_files.append(filename) |
| 645 |
if 'workspace' in raw_manifest: |
| 646 |
manifest_ = Workspace.from_raw(raw_manifest, path) |
| 647 |
elif 'package' in raw_manifest: |
| 648 |
manifest_ = Manifest.from_raw(raw_manifest, path, workspace, member_path) |
| 649 |
else: |
| 650 |
raise MesonException(f'{subdir}/Cargo.toml does not have [package] or [workspace] section') |
| 651 |
self.manifests[subdir] = manifest_ |
| 652 |
return manifest_, False |
| 653 |
|
| 654 |
def _add_dependency(self, pkg: PackageState, depname: str, machine: MachineChoice) -> None: |
| 655 |
cfg = pkg.cfg[machine] |
| 656 |
if depname in cfg.required_deps: |
| 657 |
return |
| 658 |
dep = pkg.manifest.dependencies.get(depname) |
| 659 |
if not dep: |
| 660 |
# It could be build/dev/target dependency. Just ignore it. |
| 661 |
return |
| 662 |
cfg.required_deps.add(depname) |
| 663 |
dep_pkg = self._dep_package(pkg, dep, cfg) |
| 664 |
# Use machines_from() to determine which machines the dependency needs |
| 665 |
for dep_machine in dep_pkg.manifest.machines_from(machine, self.is_cross): |
| 666 |
self._prepare_package(dep_pkg, dep_machine) |
| 667 |
if dep.default_features: |
| 668 |
self._enable_feature(dep_pkg, 'default', dep_machine) |
| 669 |
for f in dep.features: |
| 670 |
self._enable_feature(dep_pkg, f, dep_machine) |
| 671 |
for f in cfg.optional_deps_features[depname]: |
| 672 |
self._enable_feature(dep_pkg, f, dep_machine) |
| 673 |
|
| 674 |
def _enable_feature(self, pkg: PackageState, feature: str, machine: MachineChoice) -> None: |
| 675 |
cfg = pkg.cfg[machine] |
| 676 |
if feature in cfg.features: |
| 677 |
return |
| 678 |
cfg.features.add(feature) |
| 679 |
# Recurse on extra features and dependencies this feature pulls. |
| 680 |
# https://doc.rust-lang.org/cargo/reference/features.html#the-features-section |
| 681 |
for f in pkg.manifest.features.get(feature, []): |
| 682 |
if '/' in f: |
| 683 |
depname, dep_f = f.split('/', 1) |
| 684 |
if depname[-1] == '?': |
| 685 |
depname = depname[:-1] |
| 686 |
else: |
| 687 |
self._add_dependency(pkg, depname, machine) |
| 688 |
if depname in cfg.required_deps: |
| 689 |
dep = pkg.manifest.dependencies[depname] |
| 690 |
dep_pkg = self._dep_package(pkg, dep, cfg) |
| 691 |
# Use machines_from() to determine which machines the dependency needs |
| 692 |
for dep_machine in dep_pkg.manifest.machines_from(machine, self.is_cross): |
| 693 |
self._enable_feature(dep_pkg, dep_f, dep_machine) |
| 694 |
else: |
| 695 |
# This feature will be enabled only if that dependency |
| 696 |
# is later added. |
| 697 |
cfg.optional_deps_features[depname].add(dep_f) |
| 698 |
elif f.startswith('dep:'): |
| 699 |
self._add_dependency(pkg, f[4:], machine) |
| 700 |
else: |
| 701 |
self._enable_feature(pkg, f, machine) |
| 702 |
|
| 703 |
def has_check_cfg(self, machine: MachineChoice) -> bool: |
| 704 |
if not self.environment.is_cross_build(): |
| 705 |
machine = MachineChoice.HOST |
| 706 |
rustc = T.cast('RustCompiler', self.environment.coredata.compilers[machine]['rust']) |
| 707 |
return rustc.has_check_cfg |
| 708 |
|
| 709 |
@functools.lru_cache(maxsize=None) |
| 710 |
def _get_cfgs(self, machine: MachineChoice) -> T.Dict[str, str]: |
| 711 |
if not self.environment.is_cross_build(): |
| 712 |
machine = MachineChoice.HOST |
| 713 |
rustc = T.cast('RustCompiler', self.environment.coredata.compilers[machine]['rust']) |
| 714 |
cfgs = rustc.get_cfgs().copy() |
| 715 |
rustflags = self.environment.coredata.get_external_args(machine, 'rust') |
| 716 |
rustflags_i = iter(rustflags) |
| 717 |
for i in rustflags_i: |
| 718 |
if i == '--cfg': |
| 719 |
cfgs.append(next(rustflags_i)) |
| 720 |
return dict(self._split_cfg(i) for i in cfgs) |
| 721 |
|
| 722 |
@staticmethod |
| 723 |
def _split_cfg(cfg: str) -> T.Tuple[str, str]: |
| 724 |
pair = cfg.split('=', maxsplit=1) |
| 725 |
value = pair[1] if len(pair) > 1 else '' |
| 726 |
if value and value[0] == '"': |
| 727 |
value = value[1:-1] |
| 728 |
return pair[0], value |
| 729 |
|
| 730 |
def _create_project(self, name: str, pkg: T.Optional[PackageState], build: builder.Builder) -> T.List[mparser.BaseNode]: |
| 731 |
"""Create the project() function call |
| 732 |
|
| 733 |
:param pkg: The package to generate from |
| 734 |
:param build: The AST builder |
| 735 |
:return: a list nodes |
| 736 |
""" |
| 737 |
args: T.List[mparser.BaseNode] = [ |
| 738 |
build.string(name), |
| 739 |
build.string('rust'), |
| 740 |
] |
| 741 |
kwargs: T.Dict[str, mparser.BaseNode] = { |
| 742 |
# Always assume that the generated meson is using the latest features |
| 743 |
# This will warn when when we generate deprecated code, which is helpful |
| 744 |
# for the upkeep of the module |
| 745 |
'meson_version': build.string(f'>= {coredata.stable_version}'), |
| 746 |
} |
| 747 |
if pkg: |
| 748 |
default_options: T.Dict[str, mparser.BaseNode] = {} |
| 749 |
if pkg.downloaded: |
| 750 |
default_options['warning_level'] = build.string('0') |
| 751 |
|
| 752 |
kwargs.update({ |
| 753 |
'version': build.string(pkg.manifest.package.version), |
| 754 |
'default_options': build.dict({build.string(k): v for k, v in default_options.items()}), |
| 755 |
}) |
| 756 |
if pkg.manifest.package.license: |
| 757 |
kwargs['license'] = build.string(pkg.manifest.package.license) |
| 758 |
elif pkg.manifest.package.license_file: |
| 759 |
kwargs['license_files'] = build.string(pkg.manifest.package.license_file) |
| 760 |
|
| 761 |
# project(...) |
| 762 |
# rust = import('rust') |
| 763 |
# cargo_ws = rust.workspace() |
| 764 |
return [ |
| 765 |
build.function('project', args, kwargs), |
| 766 |
build.assign(build.function('import', [build.string('rust')]), |
| 767 |
'rust'), |
| 768 |
build.assign(build.method('workspace', build.identifier('rust'), []), |
| 769 |
'cargo_ws') |
| 770 |
] |
| 771 |
|
| 772 |
def _create_meson_subdir(self, build: builder.Builder) -> T.List[mparser.BaseNode]: |
| 773 |
# Allow Cargo subprojects to add extra Rust args in meson/meson.build file. |
| 774 |
# This is used to replace build.rs logic. |
| 775 |
|
| 776 |
# extra_args = [] |
| 777 |
# extra_deps = [] |
| 778 |
# fs = import('fs') |
| 779 |
# if fs.is_dir('meson') |
| 780 |
# subdir('meson') |
| 781 |
# endif |
| 782 |
return [ |
| 783 |
build.assign(build.array([]), _extra_args_varname()), |
| 784 |
build.assign(build.array([]), _extra_deps_varname()), |
| 785 |
build.assign(build.function('import', [build.string('fs')]), 'fs'), |
| 786 |
build.if_(build.method('is_dir', build.identifier('fs'), [build.string('meson')]), |
| 787 |
build.block([build.function('subdir', [build.string('meson')])])) |
| 788 |
] |
| 789 |
|
| 790 |
def _create_lib(self, pkg: PackageState, build: builder.Builder, subdir: str, |
| 791 |
lib_type: RUST_ABI) -> T.List[mparser.BaseNode]: |
| 792 |
machine = MachineChoice.BUILD if lib_type == 'proc-macro' else MachineChoice.HOST |
| 793 |
posargs: T.List[mparser.BaseNode] = [ |
| 794 |
build.string(pkg.library_name(machine, lib_type)), |
| 795 |
] |
| 796 |
|
| 797 |
kwargs: T.Dict[str, mparser.BaseNode] = { |
| 798 |
'dependencies': build.identifier(_extra_deps_varname()), |
| 799 |
'rust_args': build.identifier(_extra_args_varname()), |
| 800 |
} |
| 801 |
|
| 802 |
if lib_type == 'proc-macro': |
| 803 |
lib = build.method('proc_macro', build.identifier('pkg_obj'), posargs, kwargs) |
| 804 |
else: |
| 805 |
kwargs['rust_abi'] = build.string(lib_type) |
| 806 |
lib = build.method('library', build.identifier('pkg_obj'), posargs, kwargs) |
| 807 |
|
| 808 |
# lib = xxx_library() |
| 809 |
# dep = declare_dependency() |
| 810 |
# meson.override_dependency() |
| 811 |
return [ |
| 812 |
build.assign(lib, 'lib'), |
| 813 |
build.assign( |
| 814 |
build.function( |
| 815 |
'declare_dependency', |
| 816 |
kw={ |
| 817 |
'link_with': build.identifier('lib'), |
| 818 |
'variables': build.dict({ |
| 819 |
build.string('features'): build.method('join', build.string(','), |
| 820 |
[build.identifier('features')]), |
| 821 |
}), |
| 822 |
'version': build.method('version', build.identifier('pkg_obj')), |
| 823 |
}, |
| 824 |
), |
| 825 |
'dep' |
| 826 |
), |
| 827 |
build.method( |
| 828 |
'override_dependency', |
| 829 |
build.identifier('pkg_obj'), |
| 830 |
[build.identifier('dep')], |
| 831 |
{'rust_abi': build.string(lib_type)} |
| 832 |
), |
| 833 |
] |
| 834 |
|
| 835 |
|
| 836 |
def _parse_git_url(url: str, branch: T.Optional[str] = None) -> T.Tuple[str, str, str]: |
| 837 |
if url.startswith('git+'): |
| 838 |
url = url[4:] |
| 839 |
parts = urllib.parse.urlparse(url) |
| 840 |
query = urllib.parse.parse_qs(parts.query) |
| 841 |
query_branch = query['branch'][0] if 'branch' in query else '' |
| 842 |
branch = branch or query_branch |
| 843 |
revision = parts.fragment or branch |
| 844 |
directory = PurePath(parts.path).name |
| 845 |
if directory.endswith('.git'): |
| 846 |
directory = directory[:-4] |
| 847 |
if branch: |
| 848 |
branch_encoded = branch.replace('/', '-') |
| 849 |
directory += f'-{branch_encoded}' |
| 850 |
url = urllib.parse.urlunparse(parts._replace(params='', query='', fragment='')) |
| 851 |
return url, revision, directory |
| 852 |
|
| 853 |
|
| 854 |
def load_cargo_lock(filename: str, subproject_dir: str) -> T.Optional[CargoLock]: |
| 855 |
""" Convert Cargo.lock into a list of wraps """ |
| 856 |
|
| 857 |
# Map directory -> PackageDefinition, to avoid duplicates. Multiple packages |
| 858 |
# can have the same source URL, in that case we have a single wrap that |
| 859 |
# provides multiple dependency names. |
| 860 |
if os.path.exists(filename): |
| 861 |
toml = load_toml(filename) |
| 862 |
raw_cargolock = T.cast('raw.CargoLock', toml) |
| 863 |
cargolock = CargoLock.from_raw(raw_cargolock) |
| 864 |
packagefiles_dir = os.path.join(subproject_dir, 'packagefiles') |
| 865 |
wraps: T.Dict[str, PackageDefinition] = {} |
| 866 |
for package in cargolock.package: |
| 867 |
meson_depname = _dependency_name(package.name, version.api(package.version)) |
| 868 |
if package.source is None: |
| 869 |
# This is project's package, or one of its workspace members. |
| 870 |
continue |
| 871 |
elif package.source == 'registry+https://github.com/rust-lang/crates.io-index': |
| 872 |
checksum = package.checksum |
| 873 |
if checksum is None: |
| 874 |
checksum = cargolock.metadata[f'checksum {package.name} {package.version} ({package.source})'] |
| 875 |
url = f'https://static.crates.io/crates/{package.name}/{package.version}/download' |
| 876 |
directory = f'{package.name}-{package.version}' |
| 877 |
name = SubProject(meson_depname) |
| 878 |
wrap_type = 'file' |
| 879 |
cfg = { |
| 880 |
'directory': directory, |
| 881 |
'source_url': url, |
| 882 |
'source_filename': f'{directory}.tar.gz', |
| 883 |
'source_hash': checksum, |
| 884 |
'method': 'cargo', |
| 885 |
} |
| 886 |
elif package.source.startswith('git+'): |
| 887 |
url, revision, directory = _parse_git_url(package.source) |
| 888 |
name = SubProject(directory) |
| 889 |
wrap_type = 'git' |
| 890 |
cfg = { |
| 891 |
'url': url, |
| 892 |
'revision': revision, |
| 893 |
'method': 'cargo', |
| 894 |
} |
| 895 |
else: |
| 896 |
mlog.warning(f'Unsupported source URL in {filename}: {package.source}') |
| 897 |
continue |
| 898 |
if os.path.isdir(os.path.join(packagefiles_dir, name)): |
| 899 |
cfg['patch_directory'] = name |
| 900 |
if directory not in wraps: |
| 901 |
wraps[directory] = PackageDefinition.from_values(name, subproject_dir, wrap_type, cfg) |
| 902 |
wraps[directory].add_provided_dep(meson_depname) |
| 903 |
cargolock.wraps = {w.name: w for w in wraps.values()} |
| 904 |
return cargolock |
| 905 |
return None |