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 de4489d commit 73a2b5f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 12 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,17 @@ def build_extension(self, ext):
args.extend(
["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
)
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 73a2b5f

Please sign in to comment.