From 36a7dbe8e0e3f6af7dee73215f9e095c9601130d Mon Sep 17 00:00:00 2001 From: Viet Anh Duong Date: Wed, 27 Dec 2023 11:25:24 +0000 Subject: [PATCH] Convert llvm to multi-platform Signed-off-by: Viet Anh Duong --- .gitignore | 4 ++++ Makefile | 32 ++++++++++++++++++++----------- docker/compiler/Dockerfile | 9 +++++---- docker/llvm/Dockerfile | 19 ++++--------------- docker/llvm/wrapper.sh | 39 -------------------------------------- 5 files changed, 34 insertions(+), 69 deletions(-) delete mode 100755 docker/llvm/wrapper.sh diff --git a/.gitignore b/.gitignore index 09ee176..d846a7e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ go.work /.vscode .DS_Store + +# The following files get created during image builds +.buildx +.buildx_builder diff --git a/Makefile b/Makefile index 2d5fa7e..bfe83f0 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ SHELL = /usr/bin/env bash DOCKER ?= docker GO ?= go CLANG ?= $$(which clang) +PLATFORMS = linux/amd64,linux/arm64 + LLVM_VERSION = 15.0.7 @@ -11,6 +13,12 @@ COMPILER_IMAGE = $(IMAGE_REPO)/wbpf-compiler CURRENT_SHORT_COMMIT = $$(git rev-parse --short HEAD) +PUSH ?= false +DOCKER_OUTPUT = "type=image" +ifeq ($(PUSH), true) +DOCKER_OUTPUT = "type=registry,push=true" +endif + .PHONY: build-examples build-examples: make -C examples CC=$(CLANG) BUILD_BPF=1 build-all @@ -24,21 +32,23 @@ else endif ## DOCKER -.PHONY: push-llvm -push-llvm: build-llvm - $(DOCKER) push $(LLVM_IMAGE):$(LLVM_VERSION) +buildx_builder: + docker buildx create --platform $(PLATFORMS) --buildkitd-flags '--debug' --name $@ .PHONY: build-llvm -build-llvm: +build-llvm: buildx_builder $(DOCKER) buildx build ./docker/llvm \ --build-arg LLVM_VERSION=$(LLVM_VERSION) \ - -t $(LLVM_IMAGE):$(LLVM_VERSION) + --platform=$(PLATFORMS) \ + --output=$(DOCKER_OUTPUT) \ + --builder="$<" \ + --tag $(LLVM_IMAGE):$(LLVM_VERSION) -.PHONY: push-compiler -push-compiler: build-compiler - $(DOCKER) push $(COMPILER_IMAGE):$(CURRENT_SHORT_COMMIT) .PHONY: build-compiler -build-compiler: - $(DOCKER) buildx build . -f ./docker/compiler/Dockerfile \ - -t $(COMPILER_IMAGE):$(CURRENT_SHORT_COMMIT) +build-compiler: buildx_builder + $(DOCKER) buildx build . --file ./docker/compiler/Dockerfile \ + --platform=$(PLATFORMS) \ + --output=$(DOCKER_OUTPUT) \ + --builder="$<" \ + --tag $(COMPILER_IMAGE):$(CURRENT_SHORT_COMMIT) diff --git a/docker/compiler/Dockerfile b/docker/compiler/Dockerfile index ef61c7a..7c5b580 100644 --- a/docker/compiler/Dockerfile +++ b/docker/compiler/Dockerfile @@ -1,11 +1,12 @@ -FROM golang:1.21 as builder +FROM --platform=${TARGETPLATFORM} golang:1.21 as builder WORKDIR /src - COPY . . -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o ./dist/compiler ./cmd/compiler +ARG TARGETOS +ARG TARGETARCH -FROM ghcr.io/vietanhduong/wbpf-llvm:15.0.7 +RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -o ./dist/compiler ./cmd/compiler +FROM --platform=${TARGETPLATFORM} ghcr.io/vietanhduong/wbpf-llvm:15.0.7 COPY --from=builder /src/dist/compiler /usr/local/bin diff --git a/docker/llvm/Dockerfile b/docker/llvm/Dockerfile index 40b26ad..b3d1811 100644 --- a/docker/llvm/Dockerfile +++ b/docker/llvm/Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:22.04 as builder +FROM --platform=linux/amd64 ubuntu:22.04 as builder ARG LLVM_VERSION=15.0.7 ENV LLVM_VERSION=${LLVM_VERSION} @@ -11,22 +11,11 @@ RUN /build_llvm_native.sh RUN /build_llvm_cross_aarch64.sh FROM ubuntu:22.04 as rootfs - -# Copy amd64 binary -COPY --from=builder /out/linux/amd64/bin/clang /usr/local/bin/clang-amd64 -COPY --from=builder /out/linux/amd64/bin/llc /usr/local/bin/llc-amd64 -COPY --from=builder /out/linux/amd64/bin/llvm-objcopy /usr/local/bin/llvm-objcopy-amd64 - -# Copy amd64 binary -COPY --from=builder /out/linux/arm64/bin/clang /usr/local/bin/clang-arm64 -COPY --from=builder /out/linux/arm64/bin/llc /usr/local/bin/llc-arm64 -COPY --from=builder /out/linux/arm64/bin/llvm-objcopy /usr/local/bin/llvm-objcopy-arm64 - -COPY wrapper.sh /usr/local/bin/clang -COPY wrapper.sh /usr/local/bin/llc -COPY wrapper.sh /usr/local/bin/llvm-objcopy +ARG TARGETPLATFORM +COPY --from=builder /out/${TARGETPLATFORM}/bin /usr/local/bin FROM scratch LABEL org.opencontainers.image.source=https://github.com/vietanhduong/wbpf LABEL maintainer="vietanhs0817@gmail.com" +# Squash image COPY --from=rootfs / / diff --git a/docker/llvm/wrapper.sh b/docker/llvm/wrapper.sh deleted file mode 100755 index b4cef3a..0000000 --- a/docker/llvm/wrapper.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash - -BINARY="" -case "$(basename $0)" in -clang) - BINARY=clang - ;; -llc) - BINARY=llc - ;; -llvm-objcopy) - BINARY=llvm-objcopy - ;; -*) - echo "Unsupported binary: $(basename $0)" - exit 1 - ;; -esac - -if [[ -z "$TARGETARCH" ]]; then - TARGETARCH=$(uname -m) - case $TARGETARCH in - i386 | x86_64) - BINARY="$BINARY-amd64" - ;; - aarch64) - BINARY="$BINARY-arm64" - ;; - *) - echo "Unsupported architecture: $TARGETARCH" - exit 1 - ;; - esac -else - # For CI or docker build while we can select the architecture - BINARY="$BINARY-$TARGETARCH" -fi - -exec "$BINARY" "$@"