Skip to content

Commit cb1d94c

Browse files
Merge pull request #109 from cevich/fix_build_target
Improve Makefile + Caching in CI
2 parents 52af3b1 + 57786f8 commit cb1d94c

File tree

4 files changed

+90
-39
lines changed

4 files changed

+90
-39
lines changed

.cirrus.yml

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ env:
1111
# Location where source repo. will be cloned
1212
CIRRUS_WORKING_DIR: "/var/tmp/netavark"
1313
# Rust package cache also lives here
14-
CARGO_HOME: "/usr/local/cargo"
14+
CARGO_HOME: "/var/cache/cargo"
15+
# Rust compiler output lives here (see Makefile)
16+
CARGO_TARGET_DIR: "$CIRRUS_WORKING_DIR/targets"
1517
# Save a little typing (path relative to $CIRRUS_WORKING_DIR)
1618
SCRIPT_BASE: "./contrib/cirrus"
1719
FEDORA_NAME: "fedora-35"
@@ -46,35 +48,40 @@ build_task:
4648
# okay, as the cache still provides a benefit vs constantly hitting the
4749
# RPM repos. Use of this cache depends on dnf arguments passed in setup.sh
4850
folder: "/var/cache/dnf"
49-
# This is significant, Cirrus-CI will automatically store separate
50-
# caches for branches & PRs. We use the branch-name here mainly to
51-
# distinguish PR-level caches in order to properly support backport-PRs
52-
# to release branches. Otherwise all PRs & branches will share caches
53-
# with other PRs and branches (for a given $DEST_BRANCH value).
5451
fingerprint_key: "pkg_${DEST_BRANCH}"
5552
reupload_on_changes: true # required since fingerprint_key is defined
5653
cargo_cache: &cargo_cache
57-
# Populating this cache requires both setup.sh and `make all` to run
58-
# an actual build. It takes about 10-minutes when starting from scratch,
59-
# so caching this folder is important.
54+
# Populating this cache depends on execution of setup.sh, and runner.sh
55+
# to builds of all release, debug, plus unit-tests.
6056
folder: "$CARGO_HOME"
61-
# This cache is only about 100mb, but involves compiling. Make it
62-
# available across all PR's and Branches (separately) based on the
63-
# $DEST_BRANCH value.
64-
fingerprint_key: "cargo_${DEST_BRANCH}"
57+
# Cirrus-CI will automatically store separate caches for branches vs PRs.
58+
# We use the branch-name here mainly to distinguish PR-level caches in
59+
# order to properly support backport-PRs to release branches. Otherwise
60+
# all PRs & branches will share caches with other PRs and branches
61+
# for a given $DEST_BRANCH and vX value. Adjust vX if cache schema
62+
# changes.
63+
fingerprint_key: "cargo_v1_${DEST_BRANCH}"
64+
# Required to be set explicitly since fingerprint_key is also set
65+
reupload_on_changes: true
66+
targets_cache: &targets_cache
67+
# Similar to cargo_cache, but holds the actual compiled artifacts. This must
68+
# be scoped similar to bin_cache to avoid binary pollution across cache
69+
# contexts. For example, two PRs that happen to coincidentally change
70+
# and use cache. Adjust vX if cache schema changes.
71+
folder: "$CARGO_TARGET_DIR"
72+
fingerprint_key: "targets_v1_${CIRRUS_BUILD_ID}" # Cache only within same build
6573
reupload_on_changes: true
6674
bin_cache: &bin_cache
67-
# This isn't very significant, it simply prevents rebuilding the
68-
# binaries for every subsequent task - Saving about 3-5 minutes.
75+
# This simply prevents rebuilding bin/netavark for every subsequent task.
6976
folder: "$CIRRUS_WORKING_DIR/bin"
70-
# This is likely to change frequently, keep cache limited to ONLY this
71-
# specific CI run (a.k.a. "build").
72-
fingerprint_key: "bin_${CIRRUS_BUILD_ID}" # Cache only within the same build
77+
# Avoid binary pollution by scoping this to only this specific build.
78+
# Adjust vX if cache schema changes.
79+
fingerprint_key: "bin_v1_${CIRRUS_BUILD_ID}" # Cache only within same build
7380
reupload_on_changes: true
7481
setup_script: &setup "$SCRIPT_BASE/setup.sh"
7582
upload_caches: [ "pkg" ] # Upload now, in case of main_script failure
7683
main_script: &main "$SCRIPT_BASE/runner.sh $CIRRUS_TASK_NAME"
77-
upload_caches: [ "cargo", "bin" ]
84+
upload_caches: [ "cargo", "targets", "bin" ]
7885

7986

8087
validate_task:
@@ -90,6 +97,9 @@ validate_task:
9097
cargo_cache: &ro_cargo_cache
9198
<<: *cargo_cache
9299
reupload_on_changes: false
100+
targets_cache: &ro_targets_cache
101+
<<: *targets_cache
102+
reupload_on_changes: false
93103
bin_cache: &ro_bin_cache
94104
<<: *bin_cache
95105
reupload_on_changes: false
@@ -103,6 +113,7 @@ unit_task:
103113
- "validate"
104114
pkg_cache: *ro_pkg_cache # TODO: Remove when packages included in static VM images
105115
cargo_cache: *ro_cargo_cache
116+
targets_cache: *ro_targets_cache
106117
bin_cache: *ro_bin_cache
107118
setup_script: *setup
108119
main_script: *main
@@ -114,6 +125,7 @@ integration_task:
114125
- "unit"
115126
pkg_cache: *ro_pkg_cache # TODO: Remove when packages included in static VM images
116127
cargo_cache: *ro_cargo_cache
128+
targets_cache: *ro_targets_cache
117129
bin_cache: *ro_bin_cache
118130
setup_script: *setup
119131
main_script: *main
@@ -137,7 +149,7 @@ meta_task:
137149
GCPJSON: ENCRYPTED[e7e6e13b98eb34f480a12412a048e3fb78a02239c229659e136b7a27e2ab25a5bbb61ab6016e322cb6f777fa2c9f9520]
138150
GCPNAME: ENCRYPTED[f3fc6da8fe283ef506d7b18467a81153ea8e18b1d3cd76e79dcd6f566f20fdd3651522432d3d232f4d69eeb1502d1f6b]
139151
GCPPROJECT: libpod-218412
140-
clone_script: &noop /bin/true # source not needed
152+
clone_script: &noop mkdir -p $CIRRUS_WORKING_DIR # source not needed
141153
script: /usr/local/bin/entrypoint.sh
142154

143155

@@ -148,11 +160,17 @@ success_task:
148160
- "build"
149161
- "validate"
150162
- "unit"
151-
# - "integration"
163+
- "integration"
152164
- "meta"
153-
container:
154-
image: quay.io/libpod/alpine:latest
155165
env:
156166
CIRRUS_SHELL: "/bin/sh"
157167
clone_script: *noop
158-
script: *noop
168+
bin_cache: *ro_bin_cache
169+
# The paths used for uploaded artifacts are relative here and in Cirrus
170+
script:
171+
- mv bin/* ./
172+
- rm -rf bin
173+
# Upload tested binary for consumption downstream
174+
# https://cirrus-ci.org/guide/writing-tasks/#artifacts-instruction
175+
binary_artifacts:
176+
path: ./*netavark*

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
bin/
2-
target/
2+
targets/
33
*.swp
44
netavark.1

Makefile

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# TODO: Make this more configurable
1+
# This Makefile is intended for developer convenience. For the most part
2+
# all the targets here simply wrap calls to the `cargo` tool. Therefore,
3+
# most targets must be marked 'PHONY' to prevent `make` getting in the way
4+
#
25
#prog :=xnixperms
36

47
DESTDIR ?=
@@ -8,25 +11,46 @@ LIBEXECPODMAN ?= ${LIBEXECDIR}/podman
811

912
SELINUXOPT ?= $(shell test -x /usr/sbin/selinuxenabled && selinuxenabled && echo -Z)
1013

14+
# Set this to any non-empty string to enable unoptimized
15+
# build w/ debugging features.
1116
debug ?=
1217

18+
# All complication artifacts, including dependencies and intermediates
19+
# will be stored here, for all architectures. Use a non-default name
20+
# since the (default) 'target' is used/referenced ambiguously in many
21+
# places in the tool-chain (including 'make' itself).
22+
CARGO_TARGET_DIR ?= targets
23+
export CARGO_TARGET_DIR # 'cargo' is sensitive to this env. var. value.
24+
1325
ifdef debug
1426
$(info debug is $(debug))
27+
# These affect both $(CARGO_TARGET_DIR) layout and contents
28+
# Ref: https://doc.rust-lang.org/cargo/guide/build-cache.html
1529
release :=
16-
target :=debug
17-
extension :=debug
30+
profile :=debug
1831
else
1932
release :=--release
20-
target :=release
21-
extension :=
33+
profile :=release
2234
endif
2335

24-
build:
25-
cargo build $(release) --target-dir bin
26-
cp bin/$(target)/netavark bin/netavark
36+
.PHONY: all
37+
all: build docs
38+
39+
bin:
40+
mkdir -p $@
41+
42+
$(CARGO_TARGET_DIR):
43+
mkdir -p $@
2744

45+
.PHONY: build
46+
build: bin $(CARGO_TARGET_DIR)
47+
cargo build $(release)
48+
cp $(CARGO_TARGET_DIR)/$(profile)/netavark bin/netavark$(if $(debug),.debug,)
49+
50+
.PHONY: clean
2851
clean:
2952
rm -rf bin
53+
if [[ "$(CARGO_TARGET_DIR)" == "targets" ]]; then rm -rf targets; fi
3054
$(MAKE) -C docs clean
3155

3256
.PHONY: docs
@@ -46,20 +70,25 @@ uninstall:
4670
.PHONY: test
4771
test: unit integration
4872

73+
# Used by CI to compile the unit tests but not run them
74+
.PHONY: build_unit
75+
build_unit: $(CARGO_TARGET_DIR)
76+
cargo test --no-run
77+
4978
.PHONY: unit
50-
unit:
79+
unit: $(CARGO_TARGET_DIR)
5180
cargo test
5281

5382
.PHONY: integration
54-
integration:
83+
integration: $(CARGO_TARGET_DIR)
5584
# needs to be run as root or with podman unshare --rootless-netns
5685
bats test/
5786

58-
validate:
87+
.PHONY: validate
88+
validate: $(CARGO_TARGET_DIR)
5989
cargo fmt --all -- --check
6090
cargo clippy -p netavark -- -D warnings
6191

62-
all: build docs
63-
92+
.PHONY: help
6493
help:
6594
@echo "usage: make $(prog) [debug=1]"

contrib/cirrus/runner.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ _run_noarg() {
1717
}
1818

1919
_run_build() {
20-
make all
20+
# Assume we're on a fast VM, compile everything needed by the
21+
# rest of CI since subsequent tasks may have limited resources.
22+
make all debug=1
23+
make build_unit # reuses some debug binaries
24+
make all # optimized/non-debug binaries
2125
}
2226

2327
_run_validate() {

0 commit comments

Comments
 (0)