| 40 | |
| 41 | |
| 42 | def cargo_build(manifest_dir: Path) -> Path: |
| 43 | assert not (manifest_dir / "__init__.py").exists() |
| 44 | manifest_file = manifest_dir / "Cargo.toml" |
| 45 | assert manifest_file.exists() |
| 46 | profile = environ.get(PROFILE_ENV_VAR, DEFAULT_PROFILE) |
| 47 | quiet = environ.get(QUIET_ENV_VAR, "0").lower() in ("1", "true", "yes") |
| 48 | features = environ.get(FEATURES_ENV_VAR) |
| 49 | args = [ |
| 50 | "cargo", |
| 51 | "--locked", |
| 52 | "build", |
| 53 | "--lib", |
| 54 | "--message-format=json-render-diagnostics", |
| 55 | f"--profile={profile}", |
| 56 | ] |
| 57 | if quiet: |
| 58 | args += ["--quiet"] |
| 59 | if features: |
| 60 | args += ["--features", features] |
| 61 | cargo = subprocess.run( |
| 62 | args, |
| 63 | stdin=subprocess.DEVNULL, |
| 64 | stdout=subprocess.PIPE, |
| 65 | cwd=manifest_dir, |
| 66 | text=True, |
| 67 | check=True, |
| 68 | ) |
| 69 | module_candidates = [] |
| 70 | for line in cargo.stdout.splitlines(): |
| 71 | data = json.loads(line) |
| 72 | if data["reason"] != "compiler-artifact": |
| 73 | continue |
| 74 | if data["target"]["name"] != RUST_CRATE: |
| 75 | continue |
| 76 | for filename in data["filenames"]: |
| 77 | path = Path(filename) |
| 78 | if path.suffix not in EXTENSION_SUFFIXES: |
| 79 | continue |
| 80 | module_candidates.append(path) |
| 81 | assert len(module_candidates) == 1 |
| 82 | return module_candidates[0] |
| 83 | |
| 84 | |
| 85 | sys.meta_path.append(MagicCargoFinder()) |