From 967a9fb012d38da6a766ccf2df51f086f27754ec Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 16 Aug 2020 23:45:22 -0400 Subject: [PATCH] Fix building for musl libc --- CHANGELOG.md | 4 ++++ setuptools_rust/build.py | 16 ++++++++++++++-- setuptools_rust/utils.py | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fa16f23..e19be8cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + + - Fix building on Linux distributions that use musl (e.g. Alpine) out of the box. [#80](https://github.com/PyO3/setuptools-rust/pull/80) + ## 0.11.2 (2020-08-10) - Fix support for namespace packages. [#79](https://github.com/PyO3/setuptools-rust/pull/79) diff --git a/setuptools_rust/build.py b/setuptools_rust/build.py index 17701ac1..5e09d386 100644 --- a/setuptools_rust/build.py +++ b/setuptools_rust/build.py @@ -17,7 +17,9 @@ from subprocess import check_output from .extension import RustExtension -from .utils import Binding, Strip, cpython_feature, get_rust_version +from .utils import ( + Binding, Strip, cpython_feature, get_rust_version, get_rust_target_info +) class build_rust(Command): @@ -70,6 +72,8 @@ def finalize_options(self): def build_extension(self, ext): executable = ext.binding == Binding.Exec + rust_target_info = get_rust_target_info() + # Make sure that if pythonXX-sys is used, it builds against the current # executing python interpreter. bindir = os.path.dirname(sys.executable) @@ -85,6 +89,7 @@ def build_extension(self, ext): "PYO3_PYTHON": sys.executable, } ) + rustflags = "" # If we are on a 64-bit machine, but running a 32-bit Python, then # we'll target a 32-bit Rust build. @@ -162,12 +167,19 @@ def build_extension(self, ext): args.extend( ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"] ) + # Tell musl targets not to statically link libc. See + # https://github.com/rust-lang/rust/issues/59302 for details. + if b'target_env="musl"' in rust_target_info: + rustflags += " -C target-feature=-crt-static" if not quiet: print(" ".join(args), file=sys.stderr) if ext.native: - env["RUSTFLAGS"] = "-C target-cpu=native" + rustflags += " -C target-cpu=native" + + if rustflags: + env["RUSTFLAGS"] = rustflags # Execute cargo try: diff --git a/setuptools_rust/utils.py b/setuptools_rust/utils.py index 4d2252f4..c805e7c6 100644 --- a/setuptools_rust/utils.py +++ b/setuptools_rust/utils.py @@ -67,3 +67,8 @@ def get_rust_version(): raise DistutilsPlatformError("Can not find Rust compiler") except Exception as exc: raise DistutilsPlatformError("Can not get rustc version: %s" % str(exc)) + + +def get_rust_target_info(): + output = subprocess.check_output(["rustc", "--print", "cfg"]) + return output.splitlines()