Skip to content

Commit 26a55fa

Browse files
authored
Merge pull request #35 from PyO3/target_dir
Use cargo metadata instead of overwriting CARGO_TARGET_DIR
2 parents edf8c34 + 7d62bc9 commit 26a55fa

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

setuptools_rust/build.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import print_function, absolute_import
22
import glob
3+
import json
34
import os
45
import shutil
56
import sys
@@ -8,6 +9,7 @@
89
from distutils.errors import (
910
CompileError, DistutilsExecError, DistutilsFileError,
1011
DistutilsPlatformError, DistutilsSetupError)
12+
from subprocess import check_output
1113

1214
from .extension import RustExtension
1315
from .utils import Binding, Strip, cpython_feature, get_rust_version
@@ -59,20 +61,20 @@ def build_extension(self, ext):
5961
# executing python interpreter.
6062
bindir = os.path.dirname(sys.executable)
6163

62-
# Find where to put the temporary build files created by `cargo`
63-
targetdir = os.environ.get('CARGO_TARGET_DIR') \
64-
or os.path.join(self.build_temp, self.distribution.get_name())
65-
6664
env = os.environ.copy()
6765
env.update({
68-
'CARGO_TARGET_DIR': targetdir,
69-
7066
# disables rust's pkg-config seeking for specified packages,
7167
# which causes pythonXX-sys to fall back to detecting the
7268
# interpreter from the path.
7369
"PATH": os.path.join(bindir, os.environ.get("PATH", "")),
7470
})
7571

72+
# Find where to put the temporary build files created by `cargo`
73+
metadata_command = ["cargo", "metadata", "--manifest-path", ext.path, "--format-version", "1"]
74+
# The decoding is needed for python 3.5 compatibility
75+
metadata = json.loads(check_output(metadata_command).decode("utf-8"))
76+
target_dir = metadata["target_directory"]
77+
7678
if not os.path.exists(ext.path):
7779
raise DistutilsFileError(
7880
"Can not find rust extension project file: %s" % ext.path)
@@ -149,7 +151,7 @@ def build_extension(self, ext):
149151
suffix = "release"
150152

151153
# location of cargo compiled files
152-
artifactsdir = os.path.join(targetdir, suffix)
154+
artifactsdir = os.path.join(target_dir, suffix)
153155
dylib_paths = []
154156

155157
if executable:

0 commit comments

Comments
 (0)