Skip to content

Commit 6f7e7f9

Browse files
authored
makefile: Add goal to build the project in a linux container (#674)
Today our CI builds all of the linux code we have with musl and libc to ensure everything is silky smooth. We should have a way to do the same locally. This adds a new macOS only goal that spins up a container (requires `container` to be installed) that builds an image with our deps (libarchive and compression libs) and then builds the project. It supports supplying whatever libc (musl or glibc) to verify they both work.
1 parent 3a84f98 commit 6f7e7f9

5 files changed

Lines changed: 95 additions & 3 deletions

File tree

.github/workflows/linux-build.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,28 @@ on:
1212
- release/*
1313

1414
jobs:
15+
swift-version:
16+
name: Determine Swift version
17+
runs-on: ubuntu-24.04
18+
outputs:
19+
image: ${{ steps.version.outputs.image }}
20+
steps:
21+
- name: Checkout .swift-version
22+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
23+
with:
24+
sparse-checkout: .swift-version
25+
sparse-checkout-cone-mode: false
26+
27+
- name: Read Swift version
28+
id: version
29+
run: echo "image=swift:$(cat .swift-version)-noble" >> "$GITHUB_OUTPUT"
30+
1531
build:
1632
name: Linux compile check
33+
needs: swift-version
1734
timeout-minutes: 30
1835
runs-on: ubuntu-24.04
19-
container: swift:6.3-noble
36+
container: ${{ needs.swift-version.outputs.image }}
2037

2138
steps:
2239
- name: Checkout repository

Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ LIBARCHIVE_LOCAL_DIR := workdir/libarchive
3737

3838
KATA_BINARY_PACKAGE := https://github.com/kata-containers/kata-containers/releases/download/3.17.0/kata-static-3.17.0-arm64.tar.xz
3939

40+
SWIFT_VERSION := $(shell cat $(ROOT_DIR)/.swift-version)
41+
SWIFT_SDK_URL := $(shell grep '^SWIFT_SDK_URL' vminitd/Makefile | head -1 | sed 's/.*:= *//')
42+
SWIFT_SDK_CHECKSUM := $(shell grep '^SWIFT_SDK_CHECKSUM' vminitd/Makefile | head -1 | sed 's/.*:= *//')
43+
LINUX_DEV_IMAGE := containerization-dev:$(SWIFT_VERSION)
44+
45+
# Run a command inside a Linux dev container.
46+
# Requires 'container' (https://github.com/apple/container).
47+
# Automatically builds the dev image if it doesn't exist.
48+
define linux_run
49+
@if ! command -v container > /dev/null 2>&1; then \
50+
echo "Error: 'container' CLI not found. Install from https://github.com/apple/container"; \
51+
exit 1; \
52+
fi
53+
@if ! container image list -q 2>/dev/null | grep -q "$(LINUX_DEV_IMAGE)"; then \
54+
echo "Building Linux dev container image..."; \
55+
$(MAKE) linux-image; \
56+
fi
57+
@container run --memory 8gb --cpus 4 -v $(ROOT_DIR):/workspace -w /workspace $(LINUX_DEV_IMAGE) \
58+
bash -c "$(1)"
59+
endef
60+
4061
include Protobuf.Makefile
4162
.DEFAULT_GOAL := all
4263

@@ -48,6 +69,28 @@ else
4869
@echo "No additional dependencies required on $(UNAME_S)"
4970
endif
5071

72+
ifeq ($(UNAME_S),Darwin)
73+
.PHONY: linux-image
74+
linux-image:
75+
container build \
76+
--progress plain \
77+
-f images/linux-dev/Dockerfile \
78+
--build-arg SWIFT_VERSION=$(SWIFT_VERSION) \
79+
--build-arg SWIFT_SDK_URL=$(SWIFT_SDK_URL) \
80+
--build-arg SWIFT_SDK_CHECKSUM=$(SWIFT_SDK_CHECKSUM) \
81+
-t $(LINUX_DEV_IMAGE) \
82+
.
83+
84+
.PHONY: linux-build
85+
linux-build: LIBC ?= musl
86+
linux-build:
87+
ifeq ($(LIBC),all)
88+
$(call linux_run,make containerization && make -C vminitd LIBC=glibc && make -C vminitd LIBC=musl)
89+
else
90+
$(call linux_run,make containerization && make -C vminitd LIBC=$(LIBC))
91+
endif
92+
endif
93+
5194
.PHONY: all
5295
all: containerization
5396
all: init

images/linux-dev/Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright © 2026 Apple Inc. and the Containerization project authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
ARG SWIFT_VERSION=6.3
16+
FROM swift:${SWIFT_VERSION}-noble
17+
18+
RUN apt-get update \
19+
&& apt-get install -y make libarchive-dev libbz2-dev liblzma-dev libssl-dev \
20+
&& apt-get clean \
21+
&& rm -rf /var/lib/apt/lists/*
22+
23+
ARG SWIFT_SDK_URL
24+
ARG SWIFT_SDK_CHECKSUM
25+
RUN if [ -n "$SWIFT_SDK_URL" ]; then \
26+
swift sdk install "$SWIFT_SDK_URL" --checksum "$SWIFT_SDK_CHECKSUM"; \
27+
fi

vminitd/.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM swift:6.2
1+
FROM swift:6.3-noble
22

33
RUN apt-get update \
44
&& apt-get install make \

vminitd/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ else
2727
MUSL_ARCH := x86_64
2828
endif
2929

30-
SWIFT_CONFIGURATION := --swift-sdk $(MUSL_ARCH)-swift-linux-musl $(SWIFT_WARNING_CONFIG) -Xlinker -s --disable-automatic-resolution
30+
LIBC ?= musl
31+
ifeq ($(LIBC),musl)
32+
SWIFT_SDK_FLAGS := --swift-sdk $(MUSL_ARCH)-swift-linux-musl
33+
endif
34+
35+
SWIFT_CONFIGURATION := $(SWIFT_SDK_FLAGS) $(SWIFT_WARNING_CONFIG) -Xlinker -s --disable-automatic-resolution
3136

3237
SWIFT_VERSION := 6.3
3338
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

0 commit comments

Comments
 (0)