Skip to content

Commit aec1496

Browse files
committed
Use cargo metadata instead of overwriting CARGO_TARGET_DIR
This hugely improves performance
1 parent ec8d8ab commit aec1496

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

setuptools_rust/build.py

Lines changed: 8 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,19 @@ 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+
metadata = json.loads(check_output(metadata_command))
75+
target_dir = metadata["target_directory"]
76+
7677
if not os.path.exists(ext.path):
7778
raise DistutilsFileError(
7879
"Can not find rust extension project file: %s" % ext.path)
@@ -149,7 +150,7 @@ def build_extension(self, ext):
149150
suffix = "release"
150151

151152
# location of cargo compiled files
152-
artifactsdir = os.path.join(targetdir, suffix)
153+
artifactsdir = os.path.join(target_dir, suffix)
153154
dylib_paths = []
154155

155156
if executable:

0 commit comments

Comments
 (0)