Skip to content

Commit

Permalink
Convert llvm to multi-platform
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Anh Duong <[email protected]>
  • Loading branch information
vietanhduong committed Dec 27, 2023
1 parent 499cf04 commit 36a7dbe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 69 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ go.work
/.vscode

.DS_Store

# The following files get created during image builds
.buildx
.buildx_builder
32 changes: 21 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand 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)
9 changes: 5 additions & 4 deletions docker/compiler/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
19 changes: 4 additions & 15 deletions docker/llvm/Dockerfile
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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="[email protected]"
# Squash image
COPY --from=rootfs / /
39 changes: 0 additions & 39 deletions docker/llvm/wrapper.sh

This file was deleted.

0 comments on commit 36a7dbe

Please sign in to comment.