From f1f3a913660841258482299e4753b1fd6a630fe6 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 00:50:02 +0100 Subject: [PATCH 01/20] add +INIT and $OS_RELEASE to cache id --- rust/Earthfile | 142 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 33 deletions(-) diff --git a/rust/Earthfile b/rust/Earthfile index c6fc2ea..1f1812a 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -1,54 +1,130 @@ -VERSION 0.7 +VERSION --global-cache 0.7 + +# INIT stores the configuration required for the other UDCs in the filesystem, and installs required dependencies. +# - cache_id: Overrides default ID of the global $CARGO_HOME cache. Its value is exported to the build environment under the entry: $CARGO_HOME_CACHE_ID +# - keep_fingerprints (false): Instructs the following +CARGO calls to don't remove the Cargo fingerprints of the source packages. Use only when source packages have been COPYed with --keep-ts option. +# - sweep_days (4): +CARGO calls use cargo-sweep to clean build artifacts that haven't been accessed for this number of days. +INIT: + COMMAND + DO +INSTALL_CARGO_SWEEP + RUN mkdir -p /earthly/cfg + + # cache_id + ARG EARTHLY_TARGET_PROJECT_NO_TAG + ARG OS_RELEASE=$(md5sum /etc/os-release | cut -d ' ' -f 1) + ARG cache_id="${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-cargo-cache" + RUN echo "$cache_id">/earthly/cfg/cache_id + ENV CARGO_HOME_CACHE_ID=$cache_id + + #keep_fingerprints + ARG keep_fingerprints=false + RUN echo "$keep_fingerprints">/earthly/cfg/keep_fingerprints + + #sweep_days + ARG sweep_days=4 + RUN echo "$sweep_days">/earthly/cfg/sweep_days # CARGO runs the cargo command "cargo $args". -# This UDC should be thread safe. Parallel builds of targets using it should be free of race conditions. +# This UDC is thread safe. Parallel builds of targets calling this UDC should be free of race conditions. # Arguments: -# - args: Cargo subcommand and its arguments. Required -# - keep_fingerprints (false): Do not remove source packages fingerprints. Use only when source packages have been COPYed with --keep-ts option +# - args: Cargo subcommand and its arguments. Required. # - output: Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers). # Use this argument when you want to SAVE an ARTIFACT from the target folder (mounted cache), always trying to minimize the total size of the copied fileset. -# For example --output="release/[^\./]+" would keep all the files in /target/release that don't have any extension +# For example --output="release/[^\./]+" would keep all the files in /target/release that don't have any extension. CARGO: COMMAND ARG --required args - ARG keep_fingerprints=false + ARG keep_fingerprints=$(cat /earthly/cfg/keep_fingerprints) + ARG sweep_days=$(cat /earthly/cfg/sweep_days) ARG output IF [ "$keep_fingerprints" = "false" ] DO +REMOVE_SOURCE_FINGERPRINTS END - RUN --mount=type=cache,mode=0777,sharing=shared,target=$CARGO_HOME/registry \ - --mount=type=cache,mode=0777,sharing=shared,target=$CARGO_HOME/git \ - --mount=type=cache,mode=0777,target=target \ - cargo $args; \ - if [ -n "$output" ]; then \ - mkdir /earthly_lib_rust_temp; \ - cd target; \ - find . -type f -regextype posix-egrep -regex "./$output" -exec cp --parents \{\} /earthly_lib_rust_temp \; ; \ - fi + DO +RUN_WITH_CACHE --command="set -e; + echo \"Running cargo $args\" ; + cargo $args; + if [ -n \"$output\" ]; then + echo \"Copying output files\" ; + mkdir -p /earthly_lib_rust_temp; + cd target; + find . -type f -regextype posix-egrep -regex \"./$output\" -exec cp --parents \{\} /earthly_lib_rust_temp \; ; + cd ..; + fi; + echo \"Running cargo sweep -r -t $sweep_days\" ; + cargo sweep -r -t $sweep_days; + echo \"Running cargo sweep -r -i\" ; + cargo sweep -r -i;" IF [ "$output" != "" ] RUN mkdir -p target; \ mv /earthly_lib_rust_temp/* target 2>/dev/null || echo "no files found within ./target matching the provided output regexp" ; END +INSTALL_CARGO_SWEEP: + COMMAND + RUN if [ ! -f $CARGO_HOME/bin/cargo-sweep ]; then \ + echo "Installing cargo sweep" ; \ + cargo install cargo-sweep --root $CARGO_HOME; \ + fi; + # REMOVE_SOURCE_FINGERPRINTS removes the Cargo fingerprint folders of the source packages. -# This guarantees Cargo compiles the packages when COPY commands of the source folders have a static timestamp (see --keep-ts) +# This guarantees Cargo compiles the packages when COPY commands of the source folders have a static timestamp (see --keep-ts). REMOVE_SOURCE_FINGERPRINTS: COMMAND - COPY +get-stoml/stoml /tmp/stoml - ARG source_libs=$(find . -name Cargo.toml -exec bash -c '/tmp/stoml {} package.name; printf "\n"' \\;) - RUN --mount=type=cache,mode=0777,sharing=shared,target=target \ - find target -name .fingerprint; \ - fingerprint_folders=$(find target -name .fingerprint) ; \ - for fingerprint_folder in $fingerprint_folders; do \ - cd $fingerprint_folder; \ - for source_lib in $source_libs; do \ - find . -maxdepth 1 -regex "\./$source_lib-[^-]+" -exec rm -rf {} \; ;\ - done \ - done - -# get-stoml gets the portable stoml binary -get-stoml: + COPY +get-tomljson/tomljson /tmp/tomljson + COPY +get-jq/jq /tmp/jq + DO +RUN_WITH_CACHE --command="set -e; + source_libs=\$(find . -name Cargo.toml -exec bash -c '/tmp/tomljson {} | /tmp/jq -r .package.name; printf \"\\n\"' \\;) ; + fingerprint_folders=\$(find target -name .fingerprint) ; + echo \"deleting fingerprints:\"; + for fingerprint_folder in \$fingerprint_folders; do + cd \$fingerprint_folder; + for source_lib in \$source_libs; do + find . -maxdepth 1 -regex \"\./\$source_lib-[^-]+\" -exec bash -c 'readlink -f {}; rm -rf {}' \; ; + done + done" + +# get-tomljson gets the portable tomljson binary. +get-tomljson: + FROM alpine:3.18.3 + ARG USERARCH + ARG version=2.1.0 + RUN wget -O tomljson.tar.xz https://github.com/pelletier/go-toml/releases/download/v${version}/tomljson_${version}_linux_${USERARCH}.tar.xz; \ + tar -xf tomljson.tar.xz; \ + chmod +x tomljson + SAVE ARTIFACT tomljson + +# get-jq gets the portable jq binary. +get-jq: FROM alpine:3.18.3 - RUN wget -O stoml https://github.com/freshautomations/stoml/releases/download/v0.7.1/stoml_linux_amd64; \ - chmod +x stoml - SAVE ARTIFACT ./stoml stoml + ARG USERARCH + ARG version=1.7 + RUN wget -O jq https://github.com/jqlang/jq/releases/download/jq-${version}/jq-linux-${USERARCH}; \ + chmod +x jq + SAVE ARTIFACT jq + +# RUN_WITH_CACHE runs the passed command with the CARGO caches mounted. +# Arguments: +# - command (required): Command to run, can be any expression. +# +# This implementation is not expected to change significantly. Prefer using the `CARGO` UDC if you can, so you can get future improvements transparently. +RUN_WITH_CACHE: + COMMAND + ARG --required command + ARG cache_id = $(cat /earthly/cfg/cache_id) + # Save to restore at the end. + ARG ORIGINAL_CARGO_HOME=$CARGO_HOME + ARG ORIGINAL_CARGO_INSTALL_ROOT=$CARGO_INSTALL_ROOT + # Make sure that crates installed though this UDC are stored in the original cargo home, and not in the cargo home within the mount cache. + # This way, if BK garbage-collects them, the build is not broken. + ENV CARGO_INSTALL_ROOT=$ORIGINAL_CARGO_HOME + # We change $CARGO_HOME while keeping $ORIGINAL_CARGO_HOME/bin directory in the path. This way, the Cargo binary is still accessible and the whole $CARGO_HOME is within the global cache + # ($CARGO_HOME/.package-cache has to be in the cache so Cargo can properly synchronize parallel access to $CARGO_HOME resources). + ENV CARGO_HOME="/earthly/.cargo" + RUN --mount=type=cache,mode=0777,id=$cache_id,sharing=shared,target=/$CARGO_HOME \ + --mount=type=cache,mode=0777,target=target \ + set -e; \ + mkdir -p $CARGO_HOME; \ + printf "Running:\n $command\n"; \ + eval $command + ENV CARGO_HOME=$ORIGINAL_CARGO_HOME + ENV CARGO_INSTALL_ROOT=$ORIGINAL_CARGO_INSTALL_ROOT \ No newline at end of file From e986c307d526d2d7ab41a4a80cef73a1f58d9fc6 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 01:21:19 +0100 Subject: [PATCH 02/20] add doc changes --- rust/README.md | 53 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/rust/README.md b/rust/README.md index 844becd..e1f25ad 100644 --- a/rust/README.md +++ b/rust/README.md @@ -2,37 +2,51 @@ Earthly's official collection of rust [UDCs](https://docs.earthly.dev/docs/guides/udc). -## +CARGO +First, import the UDC up in your Earthfile: +```earthfile +IMPORT github.com/earthly/lib/rust: AS rust +``` -This UDC runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME/registry`, `$CARGO_HOME/git` and `target` for future builds of the same calling target. +## +INIT + +This UDC stores the configuration required for the other UDCs in the filesystem, and installs required dependencies. +It is meant to be called once per build environment, with the goal of avoiding passing repetitive arguments following UDCs called after it, and to install required dependencies before the source files are copied from the build context. ### Usage -First, import the UDC up in your Earthfile: +Call once per build environment: ```earthfile -IMPORT github.com/earthly/lib/rust: AS rust +DO rust+INIT ... ``` -Then, just use it in your own targets and UDCs: +### Arguments +#### `cache_id` +Overrides default ID of the global `$CARGO_HOME` cache. Its value is exported to the build environment under the entry: `$CARGO_HOME_CACHE_ID`. + +#### `keep_fingerprints (false)` +Instructs the following `+CARGO` calls to don't remove the Cargo fingerprints of the source packages. Use only when source packages have been COPYed with `--keep-ts `option. +Cargo caches compilations of packages in `target` folder based on their last modification timestamps. +By default, this UDC removes the fingerprints of the packages found in the source code, to force their recompilation and work even when the Earthly `COPY` commands used overwrote the timestamps. + +#### `sweep_days (4)` +`+CARGO` calls use cargo-sweep to clean build artifacts that haven't been accessed for this number of days. + +## +CARGO + +This UDC runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME/registry`, `$CARGO_HOME/git` and `target` for future builds of the same calling target. + +### Usage + +After calling `+INIT`, use it to wrap cargo commands: + ```earthfile DO rust+CARGO ... ``` - -### Thread safety -This UDC should be thread safe. Parallel builds of targets using it should be free of race conditions. - ### Arguments #### `args` Cargo subcommand and its arguments. Required. -#### `keep_fingerprints (false)` -Do not remove source packages fingerprints. Use only when source packages have been `COPY`ed with `--keep-ts` option. - -Cargo caches compilations of packages in `target` folder based on their last modification timestamps. - -By default, this UDC removes the fingerprints of the packages found in the source code, to force their recompilation and work even when the Earthly `COPY` commands used overwrote the timestamps. - #### `output` Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers). @@ -40,7 +54,10 @@ Use this argument when you want to `SAVE ARTIFACT` from the target folder (mount For example `--output="release/[^\./]+"` would keep all the files in `/target/release` that don't have any extension. -### Examples: +### Thread safety +This UDC should is thread safe. Parallel builds of targets calling this UDC should be free of race conditions. + +## Examples: Suppose the following project: ``` @@ -75,6 +92,8 @@ install: RUN cargo install --locked cargo-deny RUN rustup component add clippy RUN rustup component add rustfmt + # Call +INIT before copying the source file to avoid installing depencies every time source code changes + DO rust+INIT source: FROM +install From cbd302d4f9099abdfcb03c0b5d2dd4012a923f43 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 01:23:12 +0100 Subject: [PATCH 03/20] add doc changes --- rust/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rust/README.md b/rust/README.md index e1f25ad..37621f7 100644 --- a/rust/README.md +++ b/rust/README.md @@ -9,8 +9,9 @@ IMPORT github.com/earthly/lib/rust: AS rust ## +INIT -This UDC stores the configuration required for the other UDCs in the filesystem, and installs required dependencies. -It is meant to be called once per build environment, with the goal of avoiding passing repetitive arguments following UDCs called after it, and to install required dependencies before the source files are copied from the build context. +This UDC stores the configuration required by the other UDCs in the build environment filesystem, and installs required dependencies. + +It is meant to be called once per build environment, to avoid passing repetitive arguments to the following UDCs called after it, and to install required dependencies before the source files are copied from the build context. ### Usage From 0341ed08d6e52fa0bae5f9c27f47aad2a15bcf30 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 01:24:09 +0100 Subject: [PATCH 04/20] add doc changes --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 37621f7..8aecbe7 100644 --- a/rust/README.md +++ b/rust/README.md @@ -11,7 +11,7 @@ IMPORT github.com/earthly/lib/rust: AS rust This UDC stores the configuration required by the other UDCs in the build environment filesystem, and installs required dependencies. -It is meant to be called once per build environment, to avoid passing repetitive arguments to the following UDCs called after it, and to install required dependencies before the source files are copied from the build context. +It is meant to be called once per build environment, to avoid passing repetitive arguments to the UDCs called after it, and to install required dependencies before the source files are copied from the build context. ### Usage From 9a6ad37cb20e60fddd37978b2b23981b9ca2954e Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 01:25:39 +0100 Subject: [PATCH 05/20] add doc changes --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 8aecbe7..fab667b 100644 --- a/rust/README.md +++ b/rust/README.md @@ -34,7 +34,7 @@ By default, this UDC removes the fingerprints of the packages found in the sourc ## +CARGO -This UDC runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME/registry`, `$CARGO_HOME/git` and `target` for future builds of the same calling target. +This UDC runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME` and `target` for future builds of the same calling target. ### Usage From 12e1db67fba6292b17504736cb4da52310e151c5 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 01:31:12 +0100 Subject: [PATCH 06/20] add doc changes --- rust/README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rust/README.md b/rust/README.md index fab667b..69a9fc0 100644 --- a/rust/README.md +++ b/rust/README.md @@ -93,14 +93,15 @@ install: RUN cargo install --locked cargo-deny RUN rustup component add clippy RUN rustup component add rustfmt - # Call +INIT before copying the source file to avoid installing depencies every time source code changes - DO rust+INIT + # Call +INIT before copying the source file to avoid installing depencies every time source code changes. + # This parametrization will be used in future calls to UDCs of the library + DO rust+INIT --keep_fingerprints=true source: FROM +install COPY --keep-ts Cargo.toml Cargo.lock ./ COPY --keep-ts deny.toml ./ - COPY --dir package1 package2 ./ + COPY --keep-ts --dir package1 package2 ./ # build builds with the Cargo release profile build: From 1e5e15a93a8c4e455a4e0e3aa46a557018b88735 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 01:39:34 +0100 Subject: [PATCH 07/20] reorder --- rust/Earthfile | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rust/Earthfile b/rust/Earthfile index 1f1812a..4c8272b 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -59,13 +59,6 @@ CARGO: mv /earthly_lib_rust_temp/* target 2>/dev/null || echo "no files found within ./target matching the provided output regexp" ; END -INSTALL_CARGO_SWEEP: - COMMAND - RUN if [ ! -f $CARGO_HOME/bin/cargo-sweep ]; then \ - echo "Installing cargo sweep" ; \ - cargo install cargo-sweep --root $CARGO_HOME; \ - fi; - # REMOVE_SOURCE_FINGERPRINTS removes the Cargo fingerprint folders of the source packages. # This guarantees Cargo compiles the packages when COPY commands of the source folders have a static timestamp (see --keep-ts). REMOVE_SOURCE_FINGERPRINTS: @@ -102,6 +95,14 @@ get-jq: chmod +x jq SAVE ARTIFACT jq +# INSTALL_CARGO_SWEEP installs cargo-sweep if it doesn't exist already. +INSTALL_CARGO_SWEEP: + COMMAND + RUN if [ ! -f $CARGO_HOME/bin/cargo-sweep ]; then \ + echo "Installing cargo sweep" ; \ + cargo install cargo-sweep --root $CARGO_HOME; \ + fi; + # RUN_WITH_CACHE runs the passed command with the CARGO caches mounted. # Arguments: # - command (required): Command to run, can be any expression. From b91ab1d3e7904efb9473cd4fe55d873be9eee0df Mon Sep 17 00:00:00 2001 From: Ignacio del Valle Alles Date: Tue, 14 Nov 2023 11:38:50 +0100 Subject: [PATCH 08/20] Update rust/README.md Co-authored-by: Vlad A. Ionescu <446771+vladaionescu@users.noreply.github.com> --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 5bed8cc..7b78bfd 100644 --- a/rust/README.md +++ b/rust/README.md @@ -57,7 +57,7 @@ Use this argument when you want to `SAVE ARTIFACT` from the target folder (mount For example `--output="release/[^\./]+"` would keep all the files in `/target/release` that don't have any extension. ### Thread safety -This UDC should is thread safe. Parallel builds of targets calling this UDC should be free of race conditions. +This UDC is thread safe. Parallel builds of targets calling this UDC should be free of race conditions. ## Examples: From 7dbc57fe62c34cdd561d358b89bb87d286a71012 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 12:46:41 +0100 Subject: [PATCH 09/20] address feedback --- rust/Earthfile | 99 +++++++++++++++++++++++++++----------------------- rust/README.md | 2 +- 2 files changed, 55 insertions(+), 46 deletions(-) diff --git a/rust/Earthfile b/rust/Earthfile index 4c8272b..a020ab7 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -6,6 +6,10 @@ VERSION --global-cache 0.7 # - sweep_days (4): +CARGO calls use cargo-sweep to clean build artifacts that haven't been accessed for this number of days. INIT: COMMAND + RUN if [ -f /earthly/cfg/cache_id ]; then \ + echo "+INIT has already been called in this build environment" ; \ + exit 1; \ + fi DO +INSTALL_CARGO_SWEEP RUN mkdir -p /earthly/cfg @@ -24,6 +28,13 @@ INIT: ARG sweep_days=4 RUN echo "$sweep_days">/earthly/cfg/sweep_days +CHECK_INITED: + COMMAND + RUN if [ ! -f /earthly/cfg/cache_id ]; then \ + echo "+INIT has not been called yet in this build environment" ; \ + exit 1; \ + fi; + # CARGO runs the cargo command "cargo $args". # This UDC is thread safe. Parallel builds of targets calling this UDC should be free of race conditions. # Arguments: @@ -33,6 +44,7 @@ INIT: # For example --output="release/[^\./]+" would keep all the files in /target/release that don't have any extension. CARGO: COMMAND + DO +CHECK_INITED ARG --required args ARG keep_fingerprints=$(cat /earthly/cfg/keep_fingerprints) ARG sweep_days=$(cat /earthly/cfg/sweep_days) @@ -59,50 +71,6 @@ CARGO: mv /earthly_lib_rust_temp/* target 2>/dev/null || echo "no files found within ./target matching the provided output regexp" ; END -# REMOVE_SOURCE_FINGERPRINTS removes the Cargo fingerprint folders of the source packages. -# This guarantees Cargo compiles the packages when COPY commands of the source folders have a static timestamp (see --keep-ts). -REMOVE_SOURCE_FINGERPRINTS: - COMMAND - COPY +get-tomljson/tomljson /tmp/tomljson - COPY +get-jq/jq /tmp/jq - DO +RUN_WITH_CACHE --command="set -e; - source_libs=\$(find . -name Cargo.toml -exec bash -c '/tmp/tomljson {} | /tmp/jq -r .package.name; printf \"\\n\"' \\;) ; - fingerprint_folders=\$(find target -name .fingerprint) ; - echo \"deleting fingerprints:\"; - for fingerprint_folder in \$fingerprint_folders; do - cd \$fingerprint_folder; - for source_lib in \$source_libs; do - find . -maxdepth 1 -regex \"\./\$source_lib-[^-]+\" -exec bash -c 'readlink -f {}; rm -rf {}' \; ; - done - done" - -# get-tomljson gets the portable tomljson binary. -get-tomljson: - FROM alpine:3.18.3 - ARG USERARCH - ARG version=2.1.0 - RUN wget -O tomljson.tar.xz https://github.com/pelletier/go-toml/releases/download/v${version}/tomljson_${version}_linux_${USERARCH}.tar.xz; \ - tar -xf tomljson.tar.xz; \ - chmod +x tomljson - SAVE ARTIFACT tomljson - -# get-jq gets the portable jq binary. -get-jq: - FROM alpine:3.18.3 - ARG USERARCH - ARG version=1.7 - RUN wget -O jq https://github.com/jqlang/jq/releases/download/jq-${version}/jq-linux-${USERARCH}; \ - chmod +x jq - SAVE ARTIFACT jq - -# INSTALL_CARGO_SWEEP installs cargo-sweep if it doesn't exist already. -INSTALL_CARGO_SWEEP: - COMMAND - RUN if [ ! -f $CARGO_HOME/bin/cargo-sweep ]; then \ - echo "Installing cargo sweep" ; \ - cargo install cargo-sweep --root $CARGO_HOME; \ - fi; - # RUN_WITH_CACHE runs the passed command with the CARGO caches mounted. # Arguments: # - command (required): Command to run, can be any expression. @@ -110,6 +78,7 @@ INSTALL_CARGO_SWEEP: # This implementation is not expected to change significantly. Prefer using the `CARGO` UDC if you can, so you can get future improvements transparently. RUN_WITH_CACHE: COMMAND + DO +CHECK_INITED ARG --required command ARG cache_id = $(cat /earthly/cfg/cache_id) # Save to restore at the end. @@ -128,4 +97,44 @@ RUN_WITH_CACHE: printf "Running:\n $command\n"; \ eval $command ENV CARGO_HOME=$ORIGINAL_CARGO_HOME - ENV CARGO_INSTALL_ROOT=$ORIGINAL_CARGO_INSTALL_ROOT \ No newline at end of file + ENV CARGO_INSTALL_ROOT=$ORIGINAL_CARGO_INSTALL_ROOT + +get-tomljson: + FROM alpine:3.18.3 + ARG USERARCH + ARG version=2.1.0 + RUN wget -O tomljson.tar.xz https://github.com/pelletier/go-toml/releases/download/v${version}/tomljson_${version}_linux_${USERARCH}.tar.xz && \ + tar -xf tomljson.tar.xz; \ + chmod +x tomljson + SAVE ARTIFACT tomljson + +get-jq: + FROM alpine:3.18.3 + ARG USERARCH + ARG version=1.7 + RUN wget -O jq https://github.com/jqlang/jq/releases/download/jq-${version}/jq-linux-${USERARCH} && \ + chmod +x jq + SAVE ARTIFACT jq + +INSTALL_CARGO_SWEEP: + COMMAND + RUN if [ ! -f $CARGO_HOME/bin/cargo-sweep ]; then \ + echo "Installing cargo sweep" ; \ + cargo install cargo-sweep --root $CARGO_HOME; \ + fi; + +REMOVE_SOURCE_FINGERPRINTS: + COMMAND + DO +CHECK_INITED + COPY +get-tomljson/tomljson /tmp/tomljson + COPY +get-jq/jq /tmp/jq + DO +RUN_WITH_CACHE --command="set -e; + source_libs=\$(find . -name Cargo.toml -exec bash -c '/tmp/tomljson {} | /tmp/jq -r .package.name; printf \"\\n\"' \\;) ; + fingerprint_folders=\$(find target -name .fingerprint) ; + echo \"deleting fingerprints:\"; + for fingerprint_folder in \$fingerprint_folders; do + cd \$fingerprint_folder; + for source_lib in \$source_libs; do + find . -maxdepth 1 -regex \"\./\$source_lib-[^-]+\" -exec bash -c 'readlink -f {}; rm -rf {}' \; ; + done + done" \ No newline at end of file diff --git a/rust/README.md b/rust/README.md index 7b78bfd..1544a72 100644 --- a/rust/README.md +++ b/rust/README.md @@ -12,7 +12,7 @@ IMPORT github.com/earthly/lib/rust: AS rust This UDC stores the configuration required by the other UDCs in the build environment filesystem, and installs required dependencies. -It is meant to be called once per build environment, to avoid passing repetitive arguments to the UDCs called after it, and to install required dependencies before the source files are copied from the build context. +It must be called once per build environment, to avoid passing repetitive arguments to the UDCs called after it, and to install required dependencies before the source files are copied from the build context. ### Usage From c884ebb17f7852e9aebfd290f38e664dc6725583 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 12:58:16 +0100 Subject: [PATCH 10/20] Point out +INIT must be called first --- rust/Earthfile | 19 ++++++++++--------- rust/README.md | 2 ++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/rust/Earthfile b/rust/Earthfile index a020ab7..3bd4aaa 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -28,15 +28,9 @@ INIT: ARG sweep_days=4 RUN echo "$sweep_days">/earthly/cfg/sweep_days -CHECK_INITED: - COMMAND - RUN if [ ! -f /earthly/cfg/cache_id ]; then \ - echo "+INIT has not been called yet in this build environment" ; \ - exit 1; \ - fi; - # CARGO runs the cargo command "cargo $args". # This UDC is thread safe. Parallel builds of targets calling this UDC should be free of race conditions. +# Notice that in order to run this UDC, +INIT must be called first. # Arguments: # - args: Cargo subcommand and its arguments. Required. # - output: Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers). @@ -72,10 +66,10 @@ CARGO: END # RUN_WITH_CACHE runs the passed command with the CARGO caches mounted. +# Notice that in order to run this UDC, +INIT must be called first. # Arguments: # - command (required): Command to run, can be any expression. # -# This implementation is not expected to change significantly. Prefer using the `CARGO` UDC if you can, so you can get future improvements transparently. RUN_WITH_CACHE: COMMAND DO +CHECK_INITED @@ -137,4 +131,11 @@ REMOVE_SOURCE_FINGERPRINTS: for source_lib in \$source_libs; do find . -maxdepth 1 -regex \"\./\$source_lib-[^-]+\" -exec bash -c 'readlink -f {}; rm -rf {}' \; ; done - done" \ No newline at end of file + done" + +CHECK_INITED: + COMMAND + RUN if [ ! -f /earthly/cfg/cache_id ]; then \ + echo "+INIT has not been called yet in this build environment" ; \ + exit 1; \ + fi; \ No newline at end of file diff --git a/rust/README.md b/rust/README.md index 1544a72..06b0d5f 100644 --- a/rust/README.md +++ b/rust/README.md @@ -37,6 +37,8 @@ By default, this UDC removes the fingerprints of the packages found in the sourc This UDC runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME` and `target` for future builds of the same calling target. +Notice that in order to run this UDC, [+INIT](#init) must be called first. + ### Usage After calling `+INIT`, use it to wrap cargo commands: From 61ab7e843d166b858d3fe16b86ce7a4963277546 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 13:12:30 +0100 Subject: [PATCH 11/20] Add RUN_WITH_CACHE doc --- rust/README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rust/README.md b/rust/README.md index 06b0d5f..0906d67 100644 --- a/rust/README.md +++ b/rust/README.md @@ -61,6 +61,20 @@ For example `--output="release/[^\./]+"` would keep all the files in `/target/re ### Thread safety This UDC is thread safe. Parallel builds of targets calling this UDC should be free of race conditions. +## +RUN_WITH_CACHE + +`+RUN_WITH_CACHE` runs the passed command with the CARGO caches mounted. + +Notice that in order to run this UDC, +INIT must be called first. + +### Arguments +#### `command (required)` +Command to run, can be any expression. For example: + +```earthfile + DO rust-udc+RUN_WITH_CACHE --command "du \$CARGO_HOME" +``` + ## Examples: Suppose the following project: From 07dc3983627bad90fc2ba1d3ac1b8f4aa77dc661 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 13:14:55 +0100 Subject: [PATCH 12/20] Add RUN_WITH_CACHE doc --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 0906d67..eadb7f5 100644 --- a/rust/README.md +++ b/rust/README.md @@ -65,7 +65,7 @@ This UDC is thread safe. Parallel builds of targets calling this UDC should be f `+RUN_WITH_CACHE` runs the passed command with the CARGO caches mounted. -Notice that in order to run this UDC, +INIT must be called first. +Notice that in order to run this UDC, [+INIT](#init) must be called first. ### Arguments #### `command (required)` From 3e94680e7e6f559e3bb1f973cf6ee9bc4ba52c39 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 18:42:37 +0100 Subject: [PATCH 13/20] fix cache path --- rust/Earthfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/Earthfile b/rust/Earthfile index 3bd4aaa..750025a 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -84,7 +84,7 @@ RUN_WITH_CACHE: # We change $CARGO_HOME while keeping $ORIGINAL_CARGO_HOME/bin directory in the path. This way, the Cargo binary is still accessible and the whole $CARGO_HOME is within the global cache # ($CARGO_HOME/.package-cache has to be in the cache so Cargo can properly synchronize parallel access to $CARGO_HOME resources). ENV CARGO_HOME="/earthly/.cargo" - RUN --mount=type=cache,mode=0777,id=$cache_id,sharing=shared,target=/$CARGO_HOME \ + RUN --mount=type=cache,mode=0777,id=$cache_id,sharing=shared,target=$CARGO_HOME \ --mount=type=cache,mode=0777,target=target \ set -e; \ mkdir -p $CARGO_HOME; \ From 0cc685c02799460c6f50cb12252f1bb8cf53d1f7 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 18:43:02 +0100 Subject: [PATCH 14/20] add feature flags client-side --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index eadb7f5..81b3376 100644 --- a/rust/README.md +++ b/rust/README.md @@ -4,7 +4,7 @@ Earthly's official collection of rust [UDCs](https://docs.earthly.dev/docs/guide First, import the UDC up in your Earthfile: ```earthfile -VERSION 0.7 +VERSION --global-cache 0.7 IMPORT github.com/earthly/lib/rust: AS rust ``` From 8a7a8e25c36b8ebea2d85ecf97825fecc6608494 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 19:24:05 +0100 Subject: [PATCH 15/20] remove colon --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 81b3376..791922f 100644 --- a/rust/README.md +++ b/rust/README.md @@ -75,7 +75,7 @@ Command to run, can be any expression. For example: DO rust-udc+RUN_WITH_CACHE --command "du \$CARGO_HOME" ``` -## Examples: +## Examples Suppose the following project: ``` From 8206f3970552b879d28c163697b459c7790d4aa7 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 19:28:06 +0100 Subject: [PATCH 16/20] add warning --- rust/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/README.md b/rust/README.md index 791922f..792860a 100644 --- a/rust/README.md +++ b/rust/README.md @@ -7,6 +7,7 @@ First, import the UDC up in your Earthfile: VERSION --global-cache 0.7 IMPORT github.com/earthly/lib/rust: AS rust ``` +> :warning: Due to [this issue](https://github.com/earthly/earthly/issues/3490) `-global-cache` is required. ## +INIT From bb2d68d8eb4fd37245173878c2b6a254ac229faa Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 19:30:36 +0100 Subject: [PATCH 17/20] add warning --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 792860a..7fa1f2a 100644 --- a/rust/README.md +++ b/rust/README.md @@ -7,7 +7,7 @@ First, import the UDC up in your Earthfile: VERSION --global-cache 0.7 IMPORT github.com/earthly/lib/rust: AS rust ``` -> :warning: Due to [this issue](https://github.com/earthly/earthly/issues/3490) `-global-cache` is required. +> :warning: Due to [this issue](https://github.com/earthly/earthly/issues/3490), make sure to enable `--global-cache` in the calling Earthfile, as shown above. ## +INIT From 0e83f43c66b49bd56610b865139fae8759295db0 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 19:33:51 +0100 Subject: [PATCH 18/20] imrpove doc --- rust/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 7fa1f2a..09f7ec6 100644 --- a/rust/README.md +++ b/rust/README.md @@ -70,7 +70,10 @@ Notice that in order to run this UDC, [+INIT](#init) must be called first. ### Arguments #### `command (required)` -Command to run, can be any expression. For example: +Command to run, can be any expression. + +### Example +Show `$CARGO_HOME` cached-entries size: ```earthfile DO rust-udc+RUN_WITH_CACHE --command "du \$CARGO_HOME" From e3b00ba02f5ef2ad1703aaaaf89b7332df078de5 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 19:36:00 +0100 Subject: [PATCH 19/20] fix example --- rust/README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rust/README.md b/rust/README.md index 09f7ec6..125e742 100644 --- a/rust/README.md +++ b/rust/README.md @@ -79,7 +79,7 @@ Show `$CARGO_HOME` cached-entries size: DO rust-udc+RUN_WITH_CACHE --command "du \$CARGO_HOME" ``` -## Examples +## Complete example Suppose the following project: ``` @@ -101,14 +101,13 @@ Suppose the following project: The Earthfile would look like: ```earthfile -VERSION 0.7 -ARG --global debian=bookworm +VERSION --global-cache 0.7 # Importing UDC definition from default branch (in a real case, specify version or commit to guarantee immutability) IMPORT github.com/earthly/lib/rust AS rust install: - FROM rust:1.73.0-$debian + FROM rust:1.73.0-bookworm RUN apt-get update -qq RUN apt-get install --no-install-recommends -qq autoconf autotools-dev libtool-bin clang cmake bsdmainutils RUN cargo install --locked cargo-deny From f58cb332490d8bc1c286b94f3d1bcf1d702122f4 Mon Sep 17 00:00:00 2001 From: nacho Date: Tue, 14 Nov 2023 19:37:32 +0100 Subject: [PATCH 20/20] remove indentation --- rust/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/README.md b/rust/README.md index 125e742..346bd21 100644 --- a/rust/README.md +++ b/rust/README.md @@ -76,7 +76,7 @@ Command to run, can be any expression. Show `$CARGO_HOME` cached-entries size: ```earthfile - DO rust-udc+RUN_WITH_CACHE --command "du \$CARGO_HOME" +DO rust-udc+RUN_WITH_CACHE --command "du \$CARGO_HOME" ``` ## Complete example