Skip to content

Commit

Permalink
fixup cross path
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jul 24, 2022
1 parent 0763f07 commit 09d526e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
16 changes: 13 additions & 3 deletions setuptools_rust/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def build_extension(
f"unable to find executable '{name}' in '{artifacts_dir}'"
)
else:
package_id = ext._metadata(quiet=quiet)["resolve"]["root"]
package_id = ext.metadata(quiet=quiet)["resolve"]["root"]
# Find artifact from cargo messages
artifacts = tuple(
_find_cargo_artifacts(
Expand All @@ -282,8 +282,18 @@ def build_extension(
f"Rust build failed; expected only one build artifact but found {artifacts}"
)

artifact_path = artifacts[0]

# Cross returns the path from inside the dockerfile, replace this
# with the local target dir.
if os.environ.get("CARGO") == "cross":
local_target_dir = ext._metadata(cargo="cargo", quiet=quiet)[
"target_dir"
]
artifact_path = artifact_path.replace(target_dir, local_target_dir)

# guaranteed to be just one element after checks above
dylib_paths.append(_BuiltModule(ext.name, artifacts[0]))
dylib_paths.append(_BuiltModule(ext.name, artifact_path))
return dylib_paths

def install_extension(
Expand Down Expand Up @@ -681,7 +691,7 @@ def _base_cargo_target_dir(ext: RustExtension, *, quiet: bool) -> str:
If --target is passed to cargo in the command line, the target directory
will have the target appended as a child.
"""
target_directory = ext._metadata(quiet=quiet)["target_directory"]
target_directory = ext.metadata(quiet=quiet)["target_directory"]
assert isinstance(
target_directory, str
), "expected cargo metadata to contain a string target directory"
Expand Down
72 changes: 37 additions & 35 deletions setuptools_rust/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from distutils.errors import DistutilsSetupError
from enum import IntEnum, auto
from functools import lru_cache
from typing import Any, Dict, List, NewType, Optional, Sequence, Union

from semantic_version import SimpleSpec
Expand Down Expand Up @@ -156,8 +157,6 @@ def __init__(
self.optional = optional
self.py_limited_api = py_limited_api

self._cargo_metadata: Optional[_CargoMetadata] = None

if native:
warnings.warn(
"`native` is deprecated, set RUSTFLAGS=-Ctarget-cpu=native instead.",
Expand Down Expand Up @@ -231,42 +230,45 @@ def install_script(self, module_name: str, exe_path: str) -> None:
with open(file, "w") as f:
f.write(_SCRIPT_TEMPLATE.format(executable=repr(executable)))

def _metadata(self, *, quiet: bool) -> "_CargoMetadata":
def metadata(self, *, quiet: bool) -> "CargoMetadata":
"""Returns cargo metadata for this extension package.
Cached - will only execute cargo on first invocation.
"""
if self._cargo_metadata is None:
metadata_command = [
os.environ.get("CARGO", "cargo"),
"metadata",
"--manifest-path",
self.path,
"--format-version",
"1",
]
if self.cargo_manifest_args:
metadata_command.extend(self.cargo_manifest_args)

try:
# If quiet, capture stderr and only show it on exceptions
# If not quiet, let stderr be inherited
stderr = subprocess.PIPE if quiet else None
payload = subprocess.check_output(
metadata_command, stderr=stderr, encoding="latin-1"
)
except subprocess.CalledProcessError as e:
raise DistutilsSetupError(format_called_process_error(e))
try:
self._cargo_metadata = json.loads(payload)
except json.decoder.JSONDecodeError as e:
raise DistutilsSetupError(
f"""
Error parsing output of cargo metadata as json; received:
{payload}
"""
) from e
return self._cargo_metadata

return self._metadata(os.environ.get("CARGO", "cargo"), quiet)

@lru_cache()
def _metadata(self, cargo: str, quiet: bool) -> "CargoMetadata":
metadata_command = [
cargo,
"metadata",
"--manifest-path",
self.path,
"--format-version",
"1",
]
if self.cargo_manifest_args:
metadata_command.extend(self.cargo_manifest_args)

try:
# If quiet, capture stderr and only show it on exceptions
# If not quiet, let stderr be inherited
stderr = subprocess.PIPE if quiet else None
payload = subprocess.check_output(
metadata_command, stderr=stderr, encoding="latin-1"
)
except subprocess.CalledProcessError as e:
raise DistutilsSetupError(format_called_process_error(e))
try:
return json.loads(payload)
except json.decoder.JSONDecodeError as e:
raise DistutilsSetupError(
f"""
Error parsing output of cargo metadata as json; received:
{payload}
"""
) from e

def _uses_exec_binding(self) -> bool:
return self.binding == Binding.Exec
Expand Down Expand Up @@ -332,7 +334,7 @@ def entry_points(self) -> List[str]:
return []


_CargoMetadata = NewType("_CargoMetadata", Dict[str, Any])
CargoMetadata = NewType("CargoMetadata", Dict[str, Any])


def _script_name(executable: str) -> str:
Expand Down

0 comments on commit 09d526e

Please sign in to comment.