You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exposes cache ids as ARGs in +RUN_WITH_CACHE (#32)
Exposes cache ids as ARGs in `+RUN_WITH_CACHE`, this enables running
different commands on the cache mounts of choice.
Additionally:
- Stores INIT config as ENV entries, simplifying previous approach
- Adds new section "Mount caches and parallelization" to the
documentation
Copy file name to clipboardExpand all lines: rust/Earthfile
+25-25Lines changed: 25 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
VERSION--global-cache 0.7
2
2
3
-
# INIT installs some required dependencies and stores in the filesystem the configuration that will be available for the rest of functions.
4
-
# - 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
3
+
# INIT stores the configuration required for the other functions in the filesystem, and installs required dependencies.
4
+
# - cache_id: Overrides default ID of the global $CARGO_HOME cache. Its value is exported to the build environment under the entry: $EARTHLY_CARGO_HOME_CACHE_ID
5
5
# - 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.
6
6
# - sweep_days (4): +CARGO uses cargo-sweep to clean build artifacts that haven't been accessed for this number of days.
7
7
INIT:
8
8
COMMAND
9
-
RUN if [ -f /tmp/earthly/cfg/cache_id ]; then \
9
+
RUN if [ -n"$EARTHLY_CARGO_HOME_CACHE_ID" ]; then \
10
10
echo "+INIT has already been called in this build environment";\
RUN echo "$keep_fingerprints">/tmp/earthly/cfg/keep_fingerprints
29
+
ENV EARTHLY_KEEP_FINGERPRINTS=$keep_fingerprints
32
30
33
-
#sweep_days
31
+
# $EARTHLY_SWEEP_DAYS
34
32
ARG sweep_days=4
35
-
RUN echo "$sweep_days">/tmp/earthly/cfg/sweep_days
33
+
ENV EARTHLY_SWEEP_DAYS=$sweep_days
36
34
37
35
# CARGO runs the cargo command "cargo $args".
38
36
# This function is thread safe. Parallel builds of targets calling this function should be free of race conditions.
39
37
# Notice that in order to run this function, +INIT must be called first.
40
38
# Arguments:
41
39
# - args: Cargo subcommand and its arguments. Required.
42
-
# - output: Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers).
40
+
# - output: Regex matching output artifacts files to be copied to ./target folder in the caller filesystem (image layers).
43
41
# 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.
44
42
# For example --output="release/[^\./]+" would keep all the files in /target/release that don't have any extension.
# RUN_WITH_CACHE runs the passed command with the CARGO caches mounted.
76
-
# Notice that in order to run this function, +INIT must be called first.
72
+
# Notice that in order to run this function, +INIT must be called first. This function exports the target cache mount ID under the env entry: $TARGET_CACHE_ID.
77
73
# Arguments:
78
74
# - command (required): Command to run, can be any expression.
75
+
# - cargo_home_cache_id: ID of the cargo home cache mount. By default: $CARGO_HOME_CACHE_ID as exported by +INIT
76
+
# - target_cache_id: ID of the target cache mount. By default: ${CARGO_HOME_CACHE_ID}#${EARTHLY_TARGET_NAME}
# Make sure that crates installed though this function are stored in the original cargo home, and not in the cargo home within the mount cache.
88
+
# Make sure that crates installed through this function are stored in the original cargo home, and not in the cargo home within the mount cache.
90
89
# This way, if BK garbage-collects them, the build is not broken.
91
90
ENV CARGO_INSTALL_ROOT=$ORIGINAL_CARGO_HOME
92
91
# 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
93
92
# ($CARGO_HOME/.package-cache has to be in the cache so Cargo can properly synchronize parallel access to $CARGO_HOME resources).
Copy file name to clipboardExpand all lines: rust/README.md
+36-7Lines changed: 36 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ IMPORT github.com/earthly/lib/rust:<version/commit> AS rust
13
13
14
14
This function stores the configuration required by the other functions in the build environment filesystem, and installs required dependencies.
15
15
16
-
It must be called once per build environment, to avoid passing repetitive arguments to the functions called after it, and to install required dependencies before the source files are copied from the build context.
16
+
It must be called once per build environment, to avoid passing repetitive arguments to the functions called after it, and to install required dependencies before the source files are copied from the build context.
17
17
18
18
### Usage
19
19
@@ -24,7 +24,7 @@ DO rust+INIT ...
24
24
25
25
### Arguments
26
26
#### `cache_id`
27
-
Overrides default ID of the global `$CARGO_HOME` cache. Its value is exported to the build environment under the entry: `$CARGO_HOME_CACHE_ID`.
27
+
Overrides default ID of the global `$CARGO_HOME` cache. Its value is exported to the build environment under the entry: `$EARTHLY_CARGO_HOME_CACHE_ID`.
28
28
29
29
#### `keep_fingerprints (false)`
30
30
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.
@@ -36,7 +36,7 @@ By default, this function removes the fingerprints of the packages found in the
36
36
37
37
## +CARGO
38
38
39
-
This function runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME` and `target` for future builds of the same calling target.
39
+
This function runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME` and `target` for future builds of the same calling target. See #mount-caches-and-parallelization below for more details.
40
40
41
41
Notice that in order to run this function, [+INIT](#init) must be called first.
42
42
@@ -53,9 +53,9 @@ DO rust+CARGO ...
53
53
Cargo subcommand and its arguments. Required.
54
54
55
55
#### `output`
56
-
Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers).
56
+
Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers).
57
57
58
-
Use this argument when you want to `SAVE ARTIFACT` from the target folder (mounted cache), always trying to minimize the total size of the copied fileset.
58
+
Use this argument when you want to `SAVE ARTIFACT` from the target folder (mounted cache), always trying to minimize the total size of the copied fileset.
59
59
60
60
For example `--output="release/[^\./]+"` would keep all the files in `/target/release` that don't have any extension.
61
61
@@ -66,12 +66,18 @@ This function is thread safe. Parallel builds of targets calling this function s
66
66
67
67
`+RUN_WITH_CACHE` runs the passed command with the CARGO caches mounted.
68
68
69
-
Notice that in order to run this function, [+INIT](#init) must be called first.
69
+
Notice that in order to run this function, [+INIT](#init) must be called first. This function exports the target cache mount ID under the env entry: `$TARGET_CACHE_ID`.
70
70
71
71
### Arguments
72
-
#### `command (required)`
72
+
#### `command (required)`
73
73
Command to run, can be any expression.
74
74
75
+
#### `cargo_home_cache_id`
76
+
ID of the cargo home cache mount. By default: `$CARGO_HOME_CACHE_ID` as exported by `+INIT`
77
+
78
+
#### `target_cache_id`
79
+
ID of the target cache mount. By default: `${CARGO_HOME_CACHE_ID}#${EARTHLY_TARGET_NAME}`
80
+
75
81
### Example
76
82
Show `$CARGO_HOME` cached-entries size:
77
83
@@ -148,4 +154,27 @@ lint:
148
154
check-dependencies:
149
155
FROM +source
150
156
DO rust+CARGO --args="deny --all-features check --deny warnings bans license sources"
157
+
158
+
# all runs all other targets in parallel
159
+
all:
160
+
BUILD +lint
161
+
BUILD +build
162
+
BUILD +test
163
+
BUILD +fmt
164
+
BUILD +check-dependencies
151
165
```
166
+
167
+
## Mount caches and parallelization
168
+
169
+
This library uses several mount caches per tuple of `{project, os_release}`:
170
+
- One cache mount for `$CARGO_HOME`, shared across all target builds without any locking involved.
171
+
- A family of locked cache mounts for `$CARGO_TARGET_DIR`. One per target.
172
+
173
+
Notice that:
174
+
- the previous targets builds might belong to one or multiple Earthly builds.
175
+
- builds will only be blocked by concurrent ones of the same target
176
+
177
+
For example, running `earthly +all` in the previous example will:
178
+
- run all targets (`+lint,+build,+test,+fmt,+check-dependencies`) in parallel without any blocking involved
179
+
- use a common cache mount for `$CARGO_HOME`
180
+
- use one individual `$CARGO_TARGET_DIR` cache mount per target
0 commit comments