Skip to content

Commit

Permalink
Fix building for musl libc
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Aug 17, 2020
1 parent fa2ee8e commit 967a9fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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"

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:
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()

0 comments on commit 967a9fb

Please sign in to comment.