|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import re
|
| 4 | +import subprocess |
3 | 5 | from distutils.errors import DistutilsSetupError
|
4 | 6 | from enum import IntEnum, auto
|
5 | 7 | from typing import Dict, List, Optional, Union
|
6 | 8 |
|
7 |
| -import tomli |
8 | 9 | from semantic_version import SimpleSpec
|
9 | 10 | from typing_extensions import Literal
|
10 | 11 |
|
@@ -146,23 +147,23 @@ def __init__(
|
146 | 147 |
|
147 | 148 | def get_lib_name(self) -> str:
|
148 | 149 | """Parse Cargo.toml to get the name of the shared library."""
|
149 |
| - with open(self.path, "rb") as f: |
150 |
| - cfg = tomli.load(f) |
151 |
| - name = cfg.get("lib", {}).get("name") |
152 |
| - if name is None: |
153 |
| - name = cfg.get("package", {}).get("name") |
154 |
| - if name is None: |
155 |
| - raise Exception( |
156 |
| - "Can not parse library name from Cargo.toml. " |
157 |
| - "Cargo.toml missing value for 'name' key " |
158 |
| - "in both the [package] section and the [lib] section" |
| 150 | + data = json.loads( |
| 151 | + subprocess.check_output( |
| 152 | + [ |
| 153 | + "cargo", |
| 154 | + "metadata", |
| 155 | + "--manifest-path", |
| 156 | + self.path, |
| 157 | + "--format-version", |
| 158 | + "1", |
| 159 | + ] |
159 | 160 | )
|
160 |
| - if not isinstance(name, str): |
161 |
| - raise Exception( |
162 |
| - f"Expected string for Rust library name in Cargo.toml, got {name}" |
163 |
| - ) |
164 |
| - name = re.sub(r"[./\\-]", "_", name) |
165 |
| - return name |
| 161 | + ) |
| 162 | + root_key = data["resolve"]["root"] |
| 163 | + [pkg] = [p for p in data["packages"] if p["id"] == root_key] |
| 164 | + name = pkg["targets"][0]["name"] |
| 165 | + assert isinstance(name, str) |
| 166 | + return re.sub(r"[./\\-]", "_", name) |
166 | 167 |
|
167 | 168 | def get_rust_version(self) -> Optional[SimpleSpec]: # type: ignore[no-any-unimported]
|
168 | 169 | if self.rust_version is None:
|
|
0 commit comments