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

Use Cross-Compile #2806

Closed
wants to merge 8 commits into from
Closed
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
60 changes: 3 additions & 57 deletions .woodpecker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pipeline:
settings:
repo: dessalines/lemmy
dockerfile: docker/Dockerfile.multiarch
platforms: linux/amd64
platforms: linux/amd64,arm64
build_args: RUST_RELEASE_MODE=release
username:
from_secret: docker_username
Expand All @@ -148,76 +148,22 @@ pipeline:
when:
event:
- cron
#platform: linux/amd64

publish_release_docker_image_amd:
publish_release_docker_image:
image: woodpeckerci/plugin-docker-buildx
settings:
repo: dessalines/lemmy
dockerfile: docker/Dockerfile.multiarch
platforms: linux/amd64
platforms: linux/amd64,linux/arm64
build_args: RUST_RELEASE_MODE=release
username:
from_secret: docker_username
password:
from_secret: docker_password
# add_host: github.com:140.82.112.3,static.crates.io:18.154.227.73,crates.io:108.138.64.68,dl-cdn.alpinelinux.org:146.75.30.133
auto_tag: true
# auto_tag_suffix: linux-amd64
when:
event: tag
#platform: linux/amd64

publish_release_docker_image_arm:
image: woodpeckerci/plugin-docker-buildx
settings:
repo: dessalines/lemmy
dockerfile: docker/Dockerfile.multiarch
platforms: linux/arm64
build_args: RUST_RELEASE_MODE=release
username:
from_secret: docker_username
password:
from_secret: docker_password
# add_host: github.com:140.82.112.3,static.crates.io:18.154.227.73,crates.io:108.138.64.68,dl-cdn.alpinelinux.org:146.75.30.133
auto_tag: true
# auto_tag_suffix: linux-arm64
when:
event: tag
#platform: linux/arm64

#publish_release_docker_manifest:
# image: plugins/manifest
# settings:
# username:
# from_secret: docker_username
# password:
# from_secret: docker_password
# target: "dessalines/lemmy:${CI_COMMIT_TAG}"
# template: "dessalines/lemmy:${CI_COMMIT_TAG}-OS-ARCH"
# platforms:
# - linux/amd64
# - linux/arm64
# ignore_missing: true
# when:
# event: tag

#publish_latest_release_docker_manifest:
# image: plugins/manifest
# settings:
# username:
# from_secret: docker_username
# password:
# from_secret: docker_password
# target: "dessalines/lemmy:latest"
# template: "dessalines/lemmy:${CI_COMMIT_TAG}-OS-ARCH"
# platforms:
# - linux/amd64
# - linux/arm64
# ignore_missing: true
# when:
# event: tag
# #platform: linux/amd64

# using https://github.com/pksunkara/cargo-workspaces
publish_to_crates_io:
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ debug = 0
embed-pictrs = ["pict-rs"]
console = ["console-subscriber", "opentelemetry", "opentelemetry-otlp", "tracing-opentelemetry", "reqwest-tracing/opentelemetry_0_16"]
default = []
# To do cross-compiling for a different architecture, we need vendored feature enabled. See https://docs.rs/openssl/#vendored for more.
vendored_openssl = ["lemmy_utils/vendored_openssl"]

[workspace]
members = [
Expand Down
5 changes: 5 additions & 0 deletions crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ tokio = { workspace = true, features = ["macros"] }

[build-dependencies]
rosetta-build = "0.1.2"

[features]
# To do cross-compiling for a different architecture
vendored_openssl = ["openssl/vendored"]

26 changes: 22 additions & 4 deletions docker/Dockerfile.multiarch
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# FIXME: use "--platform=$BUILDPLATFORM" and solve openssl cross-compile issue
FROM rust:1.67.0-alpine as builder
FROM --platform=$BUILDPLATFORM rust:1.67.0-alpine as builder

# Install build dependencies
RUN apk add --no-cache git openssl-dev libpq-dev musl-dev
Expand All @@ -23,13 +22,32 @@ RUN case $TARGETARCH in \
esac \
&& echo "RUSTARCH=$RUSTARCH" >> .buildenv

RUN \
# If $TARGETPLATFORM is different from $BUILDPLATFORM
if [ true ]; then \
# Install rust toolchain
source .buildenv; \
rustup toolchain install $(rustc --version | cut -d ' ' -f2)-${RUSTARCH}-unknown-linux-musl; \
# Install necessary packages for cross-compiling OpenSSL
apk add --no-cache perl make gcc && \
# Set the FEATUREFLAG environment variable with the specified value
echo "FEATUREFLAG='--features vendored_openssl'" >> .buildenv; \
# We need to compile with clang, see https://github.com/briansmith/ring/issues/1414
# Install necessary packages to cross-compiling ring with clang
apk add --no-cache llvm clang qemu-aarch64 && \
echo "export CC_aarch64_unknown_linux_musl=clang" >> .buildenv; \
echo "export AR_aarch64_unknown_linux_musl=llvm-ar" >> .buildenv; \
6543 marked this conversation as resolved.
Show resolved Hide resolved
echo "export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS='-Clink-self-contained=yes -Clinker=rust-lld'" >> .buildenv; \
echo "export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUNNER='qemu-aarch64 -L /usr/aarch64-alpine-linux-musl'" >> .buildenv; \
fi

# Debug mode build
RUN --mount=type=cache,target=/app/target \
if [ "$RUST_RELEASE_MODE" = "debug" ]; then \
source .buildenv \
&& echo "pub const VERSION: &str = \"$(git describe --tag)\";" > "crates/utils/src/version.rs" \
&& rustup target add ${RUSTARCH}-unknown-linux-musl \
&& cargo build --target ${RUSTARCH}-unknown-linux-musl \
&& cargo build --target ${RUSTARCH}-unknown-linux-musl ${FEATUREFLAG} \
&& cp ./target/${RUSTARCH}-unknown-linux-musl/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
fi

Expand All @@ -38,7 +56,7 @@ RUN \
if [ "$RUST_RELEASE_MODE" = "release" ]; then \
source .buildenv \
&& rustup target add ${RUSTARCH}-unknown-linux-musl \
&& cargo build --target ${RUSTARCH}-unknown-linux-musl --release \
&& cargo build --target ${RUSTARCH}-unknown-linux-musl ${FEATUREFLAG} --release \
&& cp ./target/${RUSTARCH}-unknown-linux-musl/${RUST_RELEASE_MODE}/lemmy_server /app/lemmy_server; \
fi

Expand Down