Use same Docker image for compiling Rust programs for Linux (musl libc) & macOS (osxcross).
Until
v1.42.0
of this project, one old OpenSSL releasev1.0.2
was used.
Now, sincev1.43.x
or greater, OpenSSLv1.1.1
(LTS) is used which is supported until2023-09-11
.
View more at https://www.openssl.org/policies/releasestrat.html.
This is a Linux Docker image based on ekidd/rust-musl-builder but using debian:buster-slim. It contains essential tools for compile Rust projects such as Linux static binaries via musl-libc / musl-gcc (x86_64-unknown-linux-musl
) and macOS binaries (x86_64-apple-darwin
) via osxcross just using the same Linux image.
By default the working directory is /root/src
.
docker run --rm \
--volume "${PWD}/sample":/root/src \
--workdir /root/src \
joseluisq/rust-linux-darwin-builder:1.53.0 \
sh -c "cargo build --release"
docker run --rm \
--volume "${PWD}/sample":/root/src \
--workdir /root/src \
joseluisq/rust-linux-darwin-builder:1.53.0 \
sh -c "cargo build --release --target x86_64-apple-darwin"
You can also use the image as a base for your own Dockerfile:
FROM joseluisq/rust-linux-darwin-builder:1.53.0
You can also use o32-clang(++) and o64-clang(++) like a normal compiler.
Notes:
- The current 11.1 SDK does not support i386 anymore. Use <= 10.13 SDK if you rely on i386 support.
- The current 11.1 SDK does not support libstdc++ anymore. Use <= 10.13 SDK if you rely on libstdc++ support.
Examples:
Example usage:
Example 1: CC=o32-clang ./configure --host=i386-apple-darwin20.2
Example 2: CC=i386-apple-darwin20.2-clang ./configure --host=i386-apple-darwin20.2
Example 3: o64-clang -Wall test.c -o test
Example 4: x86_64-apple-darwin20.2-strip -x test
!!! Use aarch64-apple-darwin20.2-* instead of arm64-* when dealing with Automake !!!
!!! CC=aarch64-apple-darwin20.2-clang ./configure --host=aarch64-apple-darwin20.2 !!!
!!! CC="aarch64-apple-darwin20.2-clang -arch arm64e" ./configure --host=aarch64-apple-darwin20.2 !!!
Below a simple example using a Makefile
for cross-compiling a Rust app.
Notes:
- A hello world app is used.
- A custom directory is used below as working directory instead of
/root/src
.
Create a Makefile:
compile:
@docker run --rm -it \
-v $(PWD):/drone/src \
-w /drone/src \
joseluisq/rust-linux-darwin-builder:1.45.2 \
make cross-compile
.PHONY: compile
cross-compile:
@echo
@echo "1. Cross compiling example..."
@rustc -vV
@echo
@echo "2. Compiling application (linux-musl x86_64)..."
@cargo build --manifest-path=tests/hello-world/Cargo.toml --release --target x86_64-unknown-linux-musl
@du -sh tests/hello-world/target/x86_64-unknown-linux-musl/release/helloworld
@echo
@echo "3. Compiling application (apple-darwin x86_64)..."
@cargo build --manifest-path=tests/hello-world/Cargo.toml --release --target x86_64-apple-darwin
@du -sh tests/hello-world/target/x86_64-apple-darwin/release/helloworld
.PHONY: cross-compile
Just run the makefile compile
target, then you will see two release binaries x86_64-unknown-linux-musl
and x86_64-apple-darwin
).
make compile
# 1. Cross compiling example...
# rustc 1.45.2 (d3fb005a3 2020-07-31)
# binary: rustc
# commit-hash: d3fb005a39e62501b8b0b356166e515ae24e2e54
# commit-date: 2020-07-31
# host: x86_64-unknown-linux-gnu
# release: 1.45.2
# LLVM version: 10.0
# 2. Compiling application (linux-musl x86_64)...
# Finished release [optimized] target(s) in 0.01s
# 1.2M tests/hello-world/target/x86_64-unknown-linux-musl/release/helloworld
# 3. Compiling application (apple-darwin x86_64)...
# Finished release [optimized] target(s) in 0.01s
# 240K tests/hello-world/target/x86_64-apple-darwin/release/helloworld
If some of your crates require C bindings and you run into compilation or linking errors, try to use Clang for C/C++ builds.
For example to cross compile to Macos:
CC=o64-clang \
CXX=o64-clang++ \
cargo build --target x86_64-apple-darwin
For more details take a look at Cross compiling Rust from Linux to macOS by James Waples.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in current work by you, as defined in the Apache-2.0 license, shall be dual licensed as described below, without any additional terms or conditions.
Feel free to send some Pull request or issue.
This work is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).
© 2019-present Jose Quintana