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

cross-compile cargo plugins #3

Closed
wants to merge 1 commit into from
Closed

cross-compile cargo plugins #3

wants to merge 1 commit into from

Conversation

deitch
Copy link
Contributor

@deitch deitch commented Aug 18, 2024

Most of the Dockerfile for building an image is just installation, but there is some compilation, specifically cargo plugins. Those are fairly quick when building for local platform, but can be really slow when emulating.

This uses multistage builds with cross-compilation, and then copies them over to the final image.

@deitch
Copy link
Contributor Author

deitch commented Aug 18, 2024

Unfortunately, the cross-compilation fails. Maybe @rucoder can figure it out?

@deitch deitch mentioned this pull request Aug 18, 2024
7 tasks
@rucoder
Copy link
Contributor

rucoder commented Aug 18, 2024

Unfortunately, the cross-compilation fails. Maybe @rucoder can figure it out?

@deitch I tested cross compilation for applications, but I guess the same should be done to build eve-rust itself #4

@deitch
Copy link
Contributor Author

deitch commented Aug 18, 2024

Yeah, see if you can do that, figure out why this build fails? Without this, it builds fine, but building on the runners is really slow. A few minutes for amd64 on amd64, 20-25 mins for arm64 on amd64 or vice-versa.

@rucoder
Copy link
Contributor

rucoder commented Aug 18, 2024

Yeah, see if you can do that, figure out why this build fails? Without this, it builds fine, but building on the runners is really slow. A few minutes for amd64 on amd64, 20-25 mins for arm64 on amd64 or vice-versa.

yes, I have a fix, but we need this PR first

@rucoder
Copy link
Contributor

rucoder commented Aug 18, 2024

Yeah, see if you can do that, figure out why this build fails? Without this, it builds fine, but building on the runners is really slow. A few minutes for amd64 on amd64, 20-25 mins for arm64 on amd64 or vice-versa.

yes, I have a fix, but we need this PR first

basically I do this

ARG RUST_VERSION=1.80.1
FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-alpine3.20 AS tools-host
ARG BUILDPLATFORM
ARG TARGETARCH

ENV TARGETS="x86_64-unknown-linux-musl aarch64-unknown-linux-musl x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu riscv64gc-unknown-linux-gnu"
RUN rustup target add ${TARGETS}
RUN apk add musl-dev linux-headers make clang mold

FROM tools-host AS target-amd64
ENV CARGO_BUILD_TARGET="x86_64-unknown-linux-musl"

FROM tools-host AS target-arm64
ENV CARGO_BUILD_TARGET="aarch64-unknown-linux-musl"

FROM tools-host AS target-riscv64
ENV CARGO_BUILD_TARGET="riscv64gc-unknown-linux-gnu"

FROM target-$TARGETARCH AS tools
RUN echo "Cargo target: $CARGO_BUILD_TARGET"

ADD config.toml /usr/local/cargo/
# CARGO_BUILD_TARGET is respected by cargo install and other cargo commands
RUN cargo install --root /cargo-cross [email protected] [email protected]

FROM rust:${RUST_VERSION}-alpine3.20
ENV TARGETS="x86_64-unknown-linux-musl aarch64-unknown-linux-musl x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu riscv64gc-unknown-linux-gnu"
RUN rustup target add ${TARGETS}

# needed for cargo-chef and cargo-sbom, as well as many other compilations
RUN apk add musl-dev linux-headers make clang mold

# copy the cargo plugins from the tools stage
COPY --from=tools /cargo-cross /usr/local/cargo
# we define target specific rustc flags for cross-compilation
ADD config.toml /usr/local/cargo/

and we need $CARGO_ROOT/config.toml

[target.aarch64-unknown-linux-musl]
linker = "/usr/bin/clang"
rustflags = [
    "-C",
    "link-arg=--ld-path=/usr/bin/mold",
    "-C",
    "link-arg=--target=aarch64-unknown-linux-musl",
]

[target.x86_64-unknown-linux-musl]
linker = "/usr/bin/clang"
rustflags = [
    "-C",
    "link-arg=--ld-path=/usr/bin/mold",
    "-C",
    "link-arg=--target=x86_64-unknown-linux-musl",
]

# FIXME: riscv64 is not yet available in rust:1.80.1-alpine3.20
# but both clang and mold support it. Alos musl-dev is available in Alpine 3.20
# [target.riscv64gc-unknown-linux-gnu]
# linker = "/usr/bin/clang"
# rustflags = [
#     "-C",
#     "link-arg=--ld-path=/usr/bin/mold",
#     "-C",
#     "link-arg=--target=riscv64-unknown-linux-musl",
# ]

but I need to validate that architectures of all tools are correct. Building test apps now

Signed-off-by: Avi Deitcher <[email protected]>
@rucoder
Copy link
Contributor

rucoder commented Aug 18, 2024

@deitch 6m 21s only now with my new PR. it i based on my previouse for apps, so we can merge the new one only

@deitch deitch closed this Aug 18, 2024
@deitch deitch deleted the cross-compile branch August 18, 2024 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants