Skip to content

Commit 5796abe

Browse files
authored
Use a custom volume for build cache inside of a container (#828)
Keep in-container Swift build directories away from the virtiofs `/workspace` share and use a named volume instead for the cache store
1 parent ff44a5b commit 5796abe

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

Makefile

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
# Build configuration variables
1616
BUILD_CONFIGURATION ?= debug
1717
WARNINGS_AS_ERRORS ?= true
18-
SWIFT_CONFIGURATION := $(if $(filter-out false,$(WARNINGS_AS_ERRORS)),-Xswiftc -warnings-as-errors) --disable-automatic-resolution
18+
19+
# Allow for a custom build cache directory
20+
# By default this is left unset, and swift uses the default directory as `./.build`
21+
# The `linux_run` target exports SCRATCH_ROOT inside of the container
22+
SCRATCH_ROOT ?=
23+
SCRATCH_PATH ?= $(if $(SCRATCH_ROOT),$(SCRATCH_ROOT)/build-containerization)
24+
SWIFT_SCRATCH_FLAGS := $(if $(SCRATCH_PATH),--scratch-path $(SCRATCH_PATH))
25+
SWIFT_CONFIGURATION := $(if $(filter-out false,$(WARNINGS_AS_ERRORS)),-Xswiftc -warnings-as-errors) --disable-automatic-resolution $(SWIFT_SCRATCH_FLAGS)
1926

2027
# Commonly used locations
2128
UNAME_S := $(shell uname -s)
@@ -48,7 +55,7 @@ SWIFT ?= swift
4855
endif
4956

5057
ROOT_DIR := $(shell git rev-parse --show-toplevel)
51-
BUILD_BIN_DIR = $(shell $(SWIFT) build -c $(BUILD_CONFIGURATION) --show-bin-path)
58+
BUILD_BIN_DIR = $(shell $(SWIFT) build -c $(BUILD_CONFIGURATION) $(SWIFT_SCRATCH_FLAGS) --show-bin-path)
5259
COV_DATA_DIR = $(shell $(SWIFT) test --show-coverage-path | xargs dirname)
5360
COV_REPORT_FILE = $(ROOT_DIR)/code-coverage-report
5461

@@ -68,6 +75,14 @@ SWIFT_SDK_URL := $(shell grep '^SWIFT_SDK_URL' vminitd/Makefile | head -1 | sed
6875
SWIFT_SDK_CHECKSUM := $(shell grep '^SWIFT_SDK_CHECKSUM' vminitd/Makefile | head -1 | sed 's/.*:= *//')
6976
LINUX_DEV_IMAGE := containerization-dev:$(SWIFT_VERSION)
7077

78+
# Use an alternative path (backed by a named volume) for the build cache
79+
# when building products inside of a container
80+
# LINUX_SCRATCH_ROOT is used for the build cache
81+
# LINUX_SHARED_CACHE is used for the dependency cache
82+
LINUX_BUILD_VOLUME := containerization-linux-build
83+
LINUX_SCRATCH_ROOT := /build
84+
LINUX_SHARED_CACHE := $(LINUX_SCRATCH_ROOT)/cache
85+
7186
# Literal `,` for use inside $(call ...) arguments — bare commas are
7287
# treated as the call's argument separator and split the value early.
7388
comma := ,
@@ -99,8 +114,15 @@ define linux_run
99114
$(MAKE) linux-image; \
100115
fi
101116
@mkdir -p $(ROOT_DIR)/.local/integration-cache
117+
@if ! container volume inspect $(LINUX_BUILD_VOLUME) > /dev/null 2>&1; then \
118+
echo "Creating Linux build volume $(LINUX_BUILD_VOLUME)..."; \
119+
container volume create $(LINUX_BUILD_VOLUME) > /dev/null; \
120+
fi
102121
@container run --rm $(2) --memory 16gb --cpus 8 \
122+
--env SCRATCH_ROOT=$(LINUX_SCRATCH_ROOT) \
123+
--env XDG_CACHE_HOME=$(LINUX_SHARED_CACHE) \
103124
-v $(ROOT_DIR):/workspace \
125+
-v $(LINUX_BUILD_VOLUME):$(LINUX_SCRATCH_ROOT) \
104126
-v $(ROOT_DIR)/.local/integration-cache:/root/.local/share/com.apple.containerization \
105127
-w /workspace $(LINUX_DEV_IMAGE) \
106128
bash -c "$(1)"
@@ -140,7 +162,7 @@ endif
140162

141163
.PHONY: linux-test
142164
linux-test:
143-
$(call linux_run,swift test $(SWIFT_CONFIGURATION))
165+
$(call linux_run,swift test $(SWIFT_CONFIGURATION) --scratch-path $(LINUX_SCRATCH_ROOT)/build-containerization)
144166

145167
.PHONY: build-cloud-hypervisor
146168
# Build cloud-hypervisor from the patched source at .local/cloud-hypervisor and

scripts/build-dist-x86_64.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,20 @@ if [ "${REBUILD_VIRTIOFSD:-0}" != "1" ] && [ -x "${DIST_DIR}/virtiofsd" ]; then
146146
NEED_VIRTIOFSD=0
147147
fi
148148

149+
SCRATCH_FLAGS=()
150+
if [ -n "${SCRATCH_ROOT:-}" ]; then
151+
SCRATCH_FLAGS=(--scratch-path "${SCRATCH_ROOT}/build-containerization")
152+
fi
153+
149154
echo "==> Cross-compiling cctl to x86_64-linux-musl"
150155
swift build -c release \
151156
--swift-sdk x86_64-swift-linux-musl \
152157
--product cctl \
153158
-Xswiftc -warnings-as-errors \
154159
-Xlinker -L"${CROSS_PREFIX}/lib" \
155-
--disable-automatic-resolution
156-
CCTL_X86_64_BIN="$(swift build -c release --swift-sdk x86_64-swift-linux-musl --show-bin-path)/cctl"
160+
--disable-automatic-resolution \
161+
"${SCRATCH_FLAGS[@]}"
162+
CCTL_X86_64_BIN="$(swift build -c release --swift-sdk x86_64-swift-linux-musl "${SCRATCH_FLAGS[@]}" --show-bin-path)/cctl"
157163
install -m 755 "${CCTL_X86_64_BIN}" "${DIST_DIR}/cctl"
158164

159165
if [ "${NEED_VMINITD}" = "1" ]; then

vminitd/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ endif
4040

4141
SWIFT_CONFIGURATION := $(SWIFT_SDK_FLAGS) $(SWIFT_WARNING_CONFIG) -Xlinker -s --disable-automatic-resolution
4242

43+
SCRATCH_ROOT ?=
44+
SCRATCH_PATH ?= $(if $(SCRATCH_ROOT),$(SCRATCH_ROOT)/build-vminitd-$(LIBC))
45+
SWIFT_CONFIGURATION += $(if $(SCRATCH_PATH),--scratch-path $(SCRATCH_PATH))
46+
4347
SWIFT_VERSION := 6.3.0
4448
SWIFT_SDK_URL := https://download.swift.org/swift-6.3-release/static-sdk/swift-6.3-RELEASE/swift-6.3-RELEASE_static-linux-0.1.0.artifactbundle.tar.gz
4549
SWIFT_SDK_CHECKSUM := d2078b69bdeb5c31202c10e9d8a11d6f66f82938b51a4b75f032ccb35c4c286c
@@ -98,4 +102,4 @@ clean:
98102
@echo Cleaning the vminitd build files...
99103
@rm -f ./bin/vminitd
100104
@rm -f ./bin/vmexec
101-
@rm -rf .build
105+
@rm -rf .build $(SCRATCH_PATH)

0 commit comments

Comments
 (0)