Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix building for musl libc (alpine linux) #80

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 14 additions & 2 deletions setuptools_rust/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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"
alex marked this conversation as resolved.
Show resolved Hide resolved

if not quiet:
print(" ".join(args), file=sys.stderr)
alex marked this conversation as resolved.
Show resolved Hide resolved

if ext.native:
env["RUSTFLAGS"] = "-C target-cpu=native"
rustflags += " -C target-cpu=native"
alex marked this conversation as resolved.
Show resolved Hide resolved

if rustflags:
env["RUSTFLAGS"] = rustflags

# Execute cargo
try:
Expand Down
5 changes: 5 additions & 0 deletions setuptools_rust/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()