earthly / lib

Mozilla Public License 2.0
7 stars 11 forks source link

document how to run cross #42

Closed glehmann closed 6 months ago

glehmann commented 7 months ago

I've seen cross mentioned in the documentation, so I thought it could be easy to run it. Not that much so far :-)

Here is what I tried to do:

VERSION --global-cache 0.7
IMPORT github.com/earthly/lib/rust AS rust

rust:
    FROM rust:alpine
    RUN apk add --no-cache musl-dev findutils
    DO rust+INIT --keep_fingerprints=true

cross:
    FROM +rust
    DO rust+CARGO --args="install cross"
    RUN apk add docker jq
    ARG --required target
    DO rust+SET_CACHE_MOUNTS_ENV
    COPY --keep-ts . ./
    WITH DOCKER
        RUN --mount=$EARTHLY_RUST_CARGO_HOME_CACHE \
            --mount=$EARTHLY_RUST_TARGET_CACHE \
            cross build --target $target --release
    END
    DO rust+COPY_OUTPUT --output="[^\./]+/release/[^\./]+"

but cross fails with this error:

              +cross | build=cross target=i686-unknown-linux-musl
              +cross | --> WITH DOCKER RUN --privileged cross build --target $target --release
              +cross | Starting dockerd with data root /var/earthly/dind/f93b09b85d482e3c2c2a9ba24dac0885b16ada45cadeaaa5667074ec0e54ffda/tmp.HMgOjE
              +cross | error: DEPRECATED: future versions of rustup will require --force-non-host to install a non-host toolchain as the default.
              +cross | warning: toolchain '1.75.0-x86_64-unknown-linux-gnu' may not be able to run on this system.
              +cross | warning: If you meant to build software to target that platform, perhaps try `rustup target add x86_64-unknown-linux-gnu` instead?
              +cross | info: syncing channel updates for '1.75.0-x86_64-unknown-linux-gnu'
              +cross | info: latest update on 2023-12-28, rust version 1.75.0 (82e1608df 2023-12-21)
              +cross | info: downloading component 'cargo'
              +cross | info: downloading component 'rust-std'
              +cross | info: downloading component 'rustc'
              +cross | info: installing component 'cargo'
              +cross | info: installing component 'rust-std'
              +cross | info: installing component 'rustc'

              +cross |   1.75.0-x86_64-unknown-linux-gnu installed - (error reading rustc version)

              +cross | error: rustup is not installed at '/tmp/earthly/.cargo'
              +cross | Error: 
              +cross |    0: couldn't install toolchain `1.75.0-x86_64-unknown-linux-gnu`
              +cross |    1: `rustup toolchain add 1.75.0-x86_64-unknown-linux-gnu --profile minimal` failed with exit status: 1
              +cross | earthly debugger | Command /bin/sh -c 'cross build --target $target --release' failed with exit code 1
$ earthly --version
earthly version v0.8.3 70916968c9b1cbc764c4a4d4d137eb9921e97a1f linux/amd64; Ubuntu 23.10 (Mantic Minotaur)

Is there a example with cross available somewhere?

glehmann commented 7 months ago

hey @joepio , I see in #34 that your using cross and earthly with much more success than I do. Would you have a configuration that you could share as an example, or a link to the Earthfile of your project if the sources are public?

joepio commented 7 months ago

Yes! https://github.com/atomicdata-dev/atomic-server/blob/develop/Earthfile

glehmann commented 7 months ago

@joepio thanks a lot, I've been able to make it work with your example :-)

for the reference, the problem disappeared when I switched from the alpine to the slim (debian) variant of the rust image.

That was not all though, I had to clean the target/release directory before each run — somehow some stuff generated during the build was making the next builds fail: https://github.com/glehmann/yage/blob/8446fe3722fb782e6f0aa2b8dfcbd3d00617e3bc/Earthfile#L43

Also I found quite useful to:

that's my take on using cross and earthly together. I hope it will be useful for others :-)

idelvall commented 6 months ago

Hi guys, thanks for pushing on this! I will go through your examples and update the documentation accordingly

idelvall commented 6 months ago

Hi guys, I was able to improve a little @glehmann solution. This is what I did:

It's taking more time than expected for me to ship this though the library in a clean way and without losing parallelization, but I didn't want to hold it any longer.

Please let me know your thoughts!

glehmann commented 6 months ago

rust+CROSS works great, thanks :+1: