From d61ba7d6cd1a17ad319ba912fe04b040578e2704 Mon Sep 17 00:00:00 2001 From: Danny Canter Date: Wed, 8 Apr 2026 17:46:33 -0700 Subject: [PATCH] makefile: Add goal to build the project in a linux container 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. --- .github/workflows/linux-build.yml | 19 +++++++++++++- Makefile | 43 +++++++++++++++++++++++++++++++ images/linux-dev/Dockerfile | 27 +++++++++++++++++++ vminitd/.devcontainer/Dockerfile | 2 +- vminitd/Makefile | 7 ++++- 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 images/linux-dev/Dockerfile diff --git a/.github/workflows/linux-build.yml b/.github/workflows/linux-build.yml index e7c7318c..230bafdf 100644 --- a/.github/workflows/linux-build.yml +++ b/.github/workflows/linux-build.yml @@ -12,11 +12,28 @@ on: - release/* jobs: + swift-version: + name: Determine Swift version + runs-on: ubuntu-24.04 + outputs: + image: ${{ steps.version.outputs.image }} + steps: + - name: Checkout .swift-version + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6 + with: + sparse-checkout: .swift-version + sparse-checkout-cone-mode: false + + - name: Read Swift version + id: version + run: echo "image=swift:$(cat .swift-version)-noble" >> "$GITHUB_OUTPUT" + build: name: Linux compile check + needs: swift-version timeout-minutes: 30 runs-on: ubuntu-24.04 - container: swift:6.3-noble + container: ${{ needs.swift-version.outputs.image }} steps: - name: Checkout repository diff --git a/Makefile b/Makefile index 3792c762..b2f61687 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,27 @@ LIBARCHIVE_LOCAL_DIR := workdir/libarchive KATA_BINARY_PACKAGE := https://github.com/kata-containers/kata-containers/releases/download/3.17.0/kata-static-3.17.0-arm64.tar.xz +SWIFT_VERSION := $(shell cat $(ROOT_DIR)/.swift-version) +SWIFT_SDK_URL := $(shell grep '^SWIFT_SDK_URL' vminitd/Makefile | head -1 | sed 's/.*:= *//') +SWIFT_SDK_CHECKSUM := $(shell grep '^SWIFT_SDK_CHECKSUM' vminitd/Makefile | head -1 | sed 's/.*:= *//') +LINUX_DEV_IMAGE := containerization-dev:$(SWIFT_VERSION) + +# Run a command inside a Linux dev container. +# Requires 'container' (https://github.com/apple/container). +# Automatically builds the dev image if it doesn't exist. +define linux_run + @if ! command -v container > /dev/null 2>&1; then \ + echo "Error: 'container' CLI not found. Install from https://github.com/apple/container"; \ + exit 1; \ + fi + @if ! container image list -q 2>/dev/null | grep -q "$(LINUX_DEV_IMAGE)"; then \ + echo "Building Linux dev container image..."; \ + $(MAKE) linux-image; \ + fi + @container run --memory 8gb --cpus 4 -v $(ROOT_DIR):/workspace -w /workspace $(LINUX_DEV_IMAGE) \ + bash -c "$(1)" +endef + include Protobuf.Makefile .DEFAULT_GOAL := all @@ -48,6 +69,28 @@ else @echo "No additional dependencies required on $(UNAME_S)" endif +ifeq ($(UNAME_S),Darwin) +.PHONY: linux-image +linux-image: + container build \ + --progress plain \ + -f images/linux-dev/Dockerfile \ + --build-arg SWIFT_VERSION=$(SWIFT_VERSION) \ + --build-arg SWIFT_SDK_URL=$(SWIFT_SDK_URL) \ + --build-arg SWIFT_SDK_CHECKSUM=$(SWIFT_SDK_CHECKSUM) \ + -t $(LINUX_DEV_IMAGE) \ + . + +.PHONY: linux-build +linux-build: LIBC ?= musl +linux-build: +ifeq ($(LIBC),all) + $(call linux_run,make containerization && make -C vminitd LIBC=glibc && make -C vminitd LIBC=musl) +else + $(call linux_run,make containerization && make -C vminitd LIBC=$(LIBC)) +endif +endif + .PHONY: all all: containerization all: init diff --git a/images/linux-dev/Dockerfile b/images/linux-dev/Dockerfile new file mode 100644 index 00000000..afde9e95 --- /dev/null +++ b/images/linux-dev/Dockerfile @@ -0,0 +1,27 @@ +# Copyright © 2026 Apple Inc. and the Containerization project authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ARG SWIFT_VERSION=6.3 +FROM swift:${SWIFT_VERSION}-noble + +RUN apt-get update \ + && apt-get install -y make libarchive-dev libbz2-dev liblzma-dev libssl-dev \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +ARG SWIFT_SDK_URL +ARG SWIFT_SDK_CHECKSUM +RUN if [ -n "$SWIFT_SDK_URL" ]; then \ + swift sdk install "$SWIFT_SDK_URL" --checksum "$SWIFT_SDK_CHECKSUM"; \ + fi diff --git a/vminitd/.devcontainer/Dockerfile b/vminitd/.devcontainer/Dockerfile index 92cf57c0..cfc40ad0 100644 --- a/vminitd/.devcontainer/Dockerfile +++ b/vminitd/.devcontainer/Dockerfile @@ -1,4 +1,4 @@ -FROM swift:6.2 +FROM swift:6.3-noble RUN apt-get update \ && apt-get install make \ diff --git a/vminitd/Makefile b/vminitd/Makefile index 505bff15..a7825b99 100644 --- a/vminitd/Makefile +++ b/vminitd/Makefile @@ -27,7 +27,12 @@ else MUSL_ARCH := x86_64 endif -SWIFT_CONFIGURATION := --swift-sdk $(MUSL_ARCH)-swift-linux-musl $(SWIFT_WARNING_CONFIG) -Xlinker -s --disable-automatic-resolution +LIBC ?= musl +ifeq ($(LIBC),musl) +SWIFT_SDK_FLAGS := --swift-sdk $(MUSL_ARCH)-swift-linux-musl +endif + +SWIFT_CONFIGURATION := $(SWIFT_SDK_FLAGS) $(SWIFT_WARNING_CONFIG) -Xlinker -s --disable-automatic-resolution SWIFT_VERSION := 6.3 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