Skip to content

Commit 967a9fb

Browse files
committed
Fix building for musl libc
1 parent fa2ee8e commit 967a9fb

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Fix building on Linux distributions that use musl (e.g. Alpine) out of the box. [#80](https://github.com/PyO3/setuptools-rust/pull/80)
6+
37
## 0.11.2 (2020-08-10)
48

59
- Fix support for namespace packages. [#79](https://github.com/PyO3/setuptools-rust/pull/79)

setuptools_rust/build.py

+14-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,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:

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)