1515# Build configuration variables
1616BUILD_CONFIGURATION ?= debug
1717WARNINGS_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
2128UNAME_S := $(shell uname -s)
@@ -48,7 +55,7 @@ SWIFT ?= swift
4855endif
4956
5057ROOT_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)
5259COV_DATA_DIR = $(shell $(SWIFT ) test --show-coverage-path | xargs dirname)
5360COV_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
6875SWIFT_SDK_CHECKSUM := $(shell grep '^SWIFT_SDK_CHECKSUM' vminitd/Makefile | head -1 | sed 's/.* := * //')
6976LINUX_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.
7388comma := ,
@@ -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
142164linux-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
0 commit comments