Skip to content

Commit 73a2b5f

Browse files
committed
Fix building for musl libc
1 parent de4489d commit 73a2b5f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

setuptools_rust/build.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
from subprocess import check_output
1818

1919
from .extension import RustExtension
20-
from .utils import Binding, Strip, cpython_feature, get_rust_version
20+
from .utils import (
21+
Binding, Strip, cpython_feature, get_rust_version, get_rust_target_info
22+
)
2123

2224

2325
class build_rust(Command):
@@ -70,6 +72,8 @@ def finalize_options(self):
7072
def build_extension(self, ext):
7173
executable = ext.binding == Binding.Exec
7274

75+
rust_target_info = get_rust_target_info()
76+
7377
# Make sure that if pythonXX-sys is used, it builds against the current
7478
# executing python interpreter.
7579
bindir = os.path.dirname(sys.executable)
@@ -85,6 +89,7 @@ def build_extension(self, ext):
8589
"PYO3_PYTHON": sys.executable,
8690
}
8791
)
92+
rustflags = ""
8893

8994
# If we are on a 64-bit machine, but running a 32-bit Python, then
9095
# we'll target a 32-bit Rust build.
@@ -162,12 +167,17 @@ def build_extension(self, ext):
162167
args.extend(
163168
["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
164169
)
170+
if b'target_env="musl"' in rust_target_info:
171+
rustflags += " -C target-feature=-crt-static"
165172

166173
if not quiet:
167174
print(" ".join(args), file=sys.stderr)
168175

169176
if ext.native:
170-
env["RUSTFLAGS"] = "-C target-cpu=native"
177+
rustflags += " -C target-cpu=native"
178+
179+
if rustflags:
180+
env["RUSTFLAGS"] = rustflags
171181

172182
# Execute cargo
173183
try:

setuptools_rust/utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ def get_rust_version():
6767
raise DistutilsPlatformError("Can not find Rust compiler")
6868
except Exception as exc:
6969
raise DistutilsPlatformError("Can not get rustc version: %s" % str(exc))
70+
71+
72+
def get_rust_target_info():
73+
output = subprocess.check_output(["rustc", "--print", "cfg"])
74+
return output.splitlines()

0 commit comments

Comments
 (0)