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
This PR:
- Includes the distro information (from `/etc/os-release`) in the cache
id default value. This avoids the scenario of reusing the cache entries
across potentially incompatibles shared libraries.
- Includes a `+INIT` UDC used for initialize the build environment for
the rest UDCs to be invoked.
- Exports the cache id to the build environment as
`$CARGO_HOME_CACHE_ID`
---------
Co-authored-by: Vlad A. Ionescu <446771+vladaionescu@users.noreply.github.com>
# INIT stores the configuration required for the other UDCs 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: $CARGO_HOME_CACHE_ID
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
+
# - sweep_days (4): +CARGO calls use cargo-sweep to clean build artifacts that haven't been accessed for this number of days.
7
+
INIT:
8
+
COMMAND
9
+
RUN if [ -f /earthly/cfg/cache_id ]; then \
10
+
echo "+INIT has already been called in this build environment";\
RUN echo "$keep_fingerprints">/earthly/cfg/keep_fingerprints
26
+
27
+
#sweep_days
28
+
ARG sweep_days=4
29
+
RUN echo "$sweep_days">/earthly/cfg/sweep_days
30
+
3
31
# CARGO runs the cargo command "cargo $args".
4
-
# This UDC should be thread safe. Parallel builds of targets using it should be free of race conditions.
32
+
# This UDC is thread safe. Parallel builds of targets calling this UDC should be free of race conditions.
33
+
# Notice that in order to run this UDC, +INIT must be called first.
5
34
# Arguments:
6
35
# - args: Cargo subcommand and its arguments. Required.
7
-
# - keep_fingerprints (false): Do not remove source packages fingerprints. Use only when source packages have been COPYed with --keep-ts option.
8
-
# - sweep_days (4): The UDC uses cargo-sweep to clean build artifacts that haven't been accessed for this number of days.
9
36
# - output: Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers).
10
37
# 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.
11
38
# For example --output="release/[^\./]+" would keep all the files in /target/release that don't have any extension.
# 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.
82
+
# This way, if BK garbage-collects them, the build is not broken.
83
+
ENV CARGO_INSTALL_ROOT=$ORIGINAL_CARGO_HOME
84
+
# 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
85
+
# ($CARGO_HOME/.package-cache has to be in the cache so Cargo can properly synchronize parallel access to $CARGO_HOME resources).
# RUN_WITH_CACHE runs the passed command with the CARGO caches mounted.
86
-
# Arguments:
87
-
# - command (required): Command to run, can be any expression.
88
-
#
89
-
# This implementation is not expected to significantly change. Prefer using the `CARGO` UDC if you can, so you can get future improvements transparently.
# 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.
100
-
# This way, if BK garbage-collects them, the build is not broken
101
-
ENV CARGO_INSTALL_ROOT=$ORIGINAL_CARGO_HOME
102
-
# We need $CARGO_HOME/.package-cache within the earthly shared-mount-cache, so cargo can properly synchronize parallel access to $CARGO_HOME resources.
Copy file name to clipboardExpand all lines: rust/README.md
+62-25Lines changed: 62 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,40 +2,55 @@
2
2
3
3
Earthly's official collection of rust [UDCs](https://docs.earthly.dev/docs/guides/udc).
4
4
5
-
## +CARGO
6
-
7
-
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.
8
-
9
-
### Usage
10
-
11
5
First, import the UDC up in your Earthfile:
12
6
```earthfile
13
-
VERSION 0.7
7
+
VERSION --global-cache 0.7
14
8
IMPORT github.com/earthly/lib/rust:<version/commit> AS rust
15
9
```
10
+
> :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.
16
11
17
-
Then, just use it in your own targets and UDCs:
18
-
```earthfile
19
-
DO rust+CARGO ...
20
-
```
12
+
## +INIT
21
13
22
-
### Thread safety
23
-
This UDC should be thread safe. Parallel builds of targets using it should be free of race conditions.
14
+
This UDC stores the configuration required by the other UDCs in the build environment filesystem, and installs required dependencies.
24
15
25
-
### Arguments
16
+
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.
26
17
27
-
#### `args`
28
-
Cargo subcommand and its arguments. Required.
18
+
### Usage
29
19
30
-
#### `keep_fingerprints (false)`
31
-
Do not remove source packages fingerprints. Use only when source packages have been `COPY`ed with `--keep-ts` option.
20
+
Call once per build environment:
21
+
```earthfile
22
+
DO rust+INIT ...
23
+
```
32
24
33
-
Cargo caches compilations of packages in `target` folder based on their last modification timestamps.
25
+
### Arguments
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`.
34
28
29
+
#### `keep_fingerprints (false)`
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.
31
+
Cargo caches compilations of packages in `target` folder based on their last modification timestamps.
35
32
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.
36
33
37
34
#### `sweep_days (4)`
38
-
The UDC uses [cargo-sweep](https://github.com/holmgr/cargo-sweep) to clean build artifacts that haven't been accessed for this number of days.
35
+
`+CARGO` calls use cargo-sweep to clean build artifacts that haven't been accessed for this number of days.
36
+
37
+
## +CARGO
38
+
39
+
This UDC runs the cargo command `cargo $args` caching the contents of `$CARGO_HOME` and `target` for future builds of the same calling target.
40
+
41
+
Notice that in order to run this UDC, [+INIT](#init) must be called first.
42
+
43
+
### Usage
44
+
45
+
After calling `+INIT`, use it to wrap cargo commands:
46
+
47
+
```earthfile
48
+
DO rust+CARGO ...
49
+
```
50
+
### Arguments
51
+
52
+
#### `args`
53
+
Cargo subcommand and its arguments. Required.
39
54
40
55
#### `output`
41
56
Regex to match the files within the target folder to be copied from the cache to the caller filesystem (image layers).
@@ -44,7 +59,27 @@ Use this argument when you want to `SAVE ARTIFACT` from the target folder (mount
44
59
45
60
For example `--output="release/[^\./]+"` would keep all the files in `/target/release` that don't have any extension.
46
61
47
-
### Examples:
62
+
### Thread safety
63
+
This UDC is thread safe. Parallel builds of targets calling this UDC should be free of race conditions.
64
+
65
+
## +RUN_WITH_CACHE
66
+
67
+
`+RUN_WITH_CACHE` runs the passed command with the CARGO caches mounted.
68
+
69
+
Notice that in order to run this UDC, [+INIT](#init) must be called first.
70
+
71
+
### Arguments
72
+
#### `command (required)`
73
+
Command to run, can be any expression.
74
+
75
+
### Example
76
+
Show `$CARGO_HOME` cached-entries size:
77
+
78
+
```earthfile
79
+
DO rust-udc+RUN_WITH_CACHE --command "du \$CARGO_HOME"
80
+
```
81
+
82
+
## Complete example
48
83
49
84
Suppose the following project:
50
85
```
@@ -66,25 +101,27 @@ Suppose the following project:
66
101
The Earthfile would look like:
67
102
68
103
```earthfile
69
-
VERSION 0.7
70
-
ARG --global debian=bookworm
104
+
VERSION --global-cache 0.7
71
105
72
106
# Importing UDC definition from default branch (in a real case, specify version or commit to guarantee immutability)
0 commit comments