1717from subprocess import check_output
1818
1919from .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
2325class 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,19 @@ def build_extension(self, ext):
162167 args .extend (
163168 ["-C" , "link-arg=-undefined" , "-C" , "link-arg=dynamic_lookup" ]
164169 )
170+ # Tell musl targets not to statically link libc. See
171+ # https://github.com/rust-lang/rust/issues/59302 for details.
172+ if b'target_env="musl"' in rust_target_info :
173+ rustflags += " -C target-feature=-crt-static"
165174
166175 if not quiet :
167176 print (" " .join (args ), file = sys .stderr )
168177
169178 if ext .native :
170- env ["RUSTFLAGS" ] = "-C target-cpu=native"
179+ rustflags += " -C target-cpu=native"
180+
181+ if rustflags :
182+ env ["RUSTFLAGS" ] = rustflags
171183
172184 # Execute cargo
173185 try :
0 commit comments