Skip to content

Commit

Permalink
Add x86_64-unknown-linux-musl target (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez authored Nov 3, 2023
1 parent b91df38 commit 71359f6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
[target.x86_64-unknown-linux-musl]
rustflags = [
"-C", "target-feature=+crt-static",
"-C", "link-arg=-s",
"-C", "link-arg=-lc",
"-C", "link-arg=-lgcc",
"-L", "native=/usr/local/x86_64-linux-musl-target/lib",
"-l", "static=stdc++",
]

[target.aarch64-unknown-linux-musl]
rustflags = [
"-C", "target-feature=+crt-static",
Expand Down
3 changes: 3 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[target.x86_64-unknown-linux-musl]
dockerfile="cross/Dockerfile.x86_64-unknown-linux-musl"

[target.aarch64-unknown-linux-musl]
dockerfile="cross/Dockerfile.aarch64-unknown-linux-musl"

Expand Down
24 changes: 22 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
.PHONY: dist

# Compile the binaries for all targets.
build: build-aarch64-unknown-linux-musl \
build: \
build-x86_64-unknown-linux-musl \
build-aarch64-unknown-linux-musl \
build-armv5te-unknown-linux-musleabi \
build-armv7-unknown-linux-musleabihf \
build-mips-unknown-linux-musl \
build-mipsel-unknown-linux-musl

build-x86_64-unknown-linux-musl:
cross build --target x86_64-unknown-linux-musl --release

build-aarch64-unknown-linux-musl:
cross build --target aarch64-unknown-linux-musl --release

Expand All @@ -23,12 +28,16 @@ build-mipsel-unknown-linux-musl:
cross build --target mipsel-unknown-linux-musl --release --no-default-features --features semtech_udp

# Build distributable binaries for all targets.
dist: dist-aarch64-unknown-linux-musl \
dist: \
dist-x86_64-unknown-linux-musl \
dist-aarch64-unknown-linux-musl \
dist-armv5te-unknown-linux-musleabi \
dist-armv7-unknown-linux-musleabihf \
dist-mips-unknown-linux-musl \
dist-mipsel-unknown-linux-musl

dist-x86_64-unknown-linux-musl: build-x86_64-unknown-linux-musl package-x86_64-unknown-linux-musl

dist-aarch64-unknown-linux-musl: build-aarch64-unknown-linux-musl package-aarch64-unknown-linux-musl

dist-armv5te-unknown-linux-musleabi: build-armv5te-unknown-linux-musleabi package-armv5te-unknown-linux-musleabi
Expand All @@ -40,6 +49,17 @@ dist-mips-unknown-linux-musl: build-mips-unknown-linux-musl package-mips-unknown
dist-mipsel-unknown-linux-musl: build-mipsel-unknown-linux-musl package-mipsel-unknown-linux-musl

# Package the compiled binaries
package-x86_64-unknown-linux-musl:
$(eval PKG_VERSION := $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version'))
mkdir -p dist

# .tar.gz
tar -czvf dist/chirpstack-mqtt-forwarder_$(PKG_VERSION)_amd64.tar.gz -C target/x86_64-unknown-linux-musl/release chirpstack-mqtt-forwarder

# .deb
cargo deb --target x86_64-unknown-linux-musl --no-build --no-strip
cp target/x86_64-unknown-linux-musl/debian/*.deb ./dist

package-aarch64-unknown-linux-musl:
$(eval PKG_VERSION := $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version'))
mkdir -p dist
Expand Down
28 changes: 28 additions & 0 deletions cross/Dockerfile.x86_64-unknown-linux-musl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM ghcr.io/cross-rs/x86_64-unknown-linux-musl:latest

ENV OPENSSL_VERSION=3.1.2
ENV OPENSSL_TARGET=linux-x86_64
ENV MUSL_PREFIX=x86_64-linux-musl

RUN apt-get update && \
apt-get --assume-yes install \
protobuf-compiler \
libprotobuf-dev \
libssl-dev

RUN echo "Building OpenSSL" && \
cd /tmp && \
curl -fLO "https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz" && \
tar xvzf "openssl-$OPENSSL_VERSION.tar.gz" && cd "openssl-$OPENSSL_VERSION" && \
env CC=$MUSL_PREFIX-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/$MUSL_PREFIX-target -DOPENSSL_NO_SECURE_MEMORY $OPENSSL_TARGET && \
env C_INCLUDE_PATH=/usr/local/$MUSL_PREFIX/include/ make depend && \
env C_INCLUDE_PATH=/usr/local/$MUSL_PREFIX/include/ make && \
make install_sw && \
rm -r /tmp/*

RUN echo "Copy libstdc++" && \
mkdir -p /usr/local/$MUSL_PREFIX-target/lib && \
cp /usr/local/$MUSL_PREFIX/lib/libstdc++* /usr/local/$MUSL_PREFIX-target/lib

ENV PKG_CONFIG_PATH=/usr/local/$MUSL_PREFIX-target/lib/pkgconfig
ENV OPENSSL_DIR=/usr/local/$MUSL_PREFIX-target

0 comments on commit 71359f6

Please sign in to comment.