Skip to content

Commit

Permalink
feat: build darwin amd64/arm64 Docker images (#200)
Browse files Browse the repository at this point in the history
* feat: buid darwing amd64/arm64 Docker images

* Update go/darwin-arm64/Makefile

* Update go/darwin/Makefile

* fix: typo

* feat: build multiarch Docker image

* fix: use production Docker images

* fix: typo

* fix: copy changes to darwinw-arm64

* fix: remove arg
  • Loading branch information
kuisathaverat authored May 30, 2022
1 parent 4792481 commit 831fe5e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*.o
.idea
.vagrant
.vscode
_obj
Dockerfile
.status.*
*.bck
CHANGELOG.md
npcap/lib/*.exe

30 changes: 18 additions & 12 deletions go/darwin-arm64/Dockerfile.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
ARG REPOSITORY
ARG VERSION
ARG TAG_EXTENSION=''
FROM docker.elastic.co/beats-dev/golang-crossbuild:llvm-apple${TAG_EXTENSION} AS BUILD_LLVM_APPLE

ARG REPOSITORY
ARG VERSION
ARG TAG_EXTENSION=''
{{- if or (eq .DEBIAN_VERSION "10") (eq .DEBIAN_VERSION "11")}}
FROM --platform=linux/amd64 docker.elastic.co/beats-dev/golang-crossbuild:llvm-apple-debian{{ .DEBIAN_VERSION }}-amd64 AS build-llvm-apple-amd64
FROM --platform=linux/arm64 docker.elastic.co/beats-dev/golang-crossbuild:llvm-apple-debian{{ .DEBIAN_VERSION }}-arm64 AS build-llvm-apple-arm64
# workaround to https://github.com/moby/moby/issues/34482
ARG TARGETARCH
ARG BUILDARCH
FROM build-llvm-apple-${TARGETARCH} as build-llvm-apple
ARG TARGETARCH
ARG BUILDARCH
RUN echo "Building ${TARGETARCH} on a ${BUILDARCH}"
{{- end }}
FROM ${REPOSITORY}/golang-crossbuild:${VERSION}-base${TAG_EXTENSION}

{{- if and (ne .DEBIAN_VERSION "10") (ne .DEBIAN_VERSION "11")}}
RUN echo "This Docker image will work only with Debian >10" && exit 1
{{- end }}

RUN \
apt-get -o Acquire::Check-Valid-Until=false update \
&& apt-get install -qq -y --no-install-recommends --allow-unauthenticated \
Expand All @@ -19,22 +29,18 @@ RUN \
uuid-dev \
&& rm -rf /var/lib/apt/lists/*

{{- if or (eq .DEBIAN_VERSION "10") (eq .DEBIAN_VERSION "11")}}
COPY --from=BUILD_LLVM_APPLE /llvm-apple-Linux.tar.gz /tmp/llvm-apple-Linux.tar.gz
COPY --from=build-llvm-apple /llvm-apple-Linux.tar.gz /tmp/llvm-apple-Linux.tar.gz
RUN tar -xzf /tmp/llvm-apple-Linux.tar.gz --strip 1 -C /usr/local \
&& rm /tmp/llvm-apple-Linux.tar.gz
{{- else }}
RUN echo "This Docker image will work only with Debian >10" && exit 1
{{- end }}

ARG OSXCROSS_PATH=/usr/local/osxcross
COPY --from=BUILD_LLVM_APPLE /osxcross.tar.gz /tmp/osxcross.tar.gz
COPY --from=build-llvm-apple /osxcross.tar.gz /tmp/osxcross.tar.gz
RUN tar -xzf /tmp/osxcross.tar.gz -C / \
&& rm /tmp/osxcross.tar.gz

ENV PATH $PATH:$OSXCROSS_PATH/bin
# Add osxcross libraries to the library PATH
ENV LD_LIBRARY_PATH ${OSXCROSS_PATH}/lib:$LD_LIBRARY_PATH
ENV LD_LIBRARY_PATH $OSXCROSS_PATH/lib:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH

COPY rootfs /

Expand Down
9 changes: 9 additions & 0 deletions go/darwin-arm64/Makefile
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
include ../Makefile.common

ifeq ($(DOCKER_MULTIARCH),1)
DOCKER_CMD := $(SELF_DIR)/../.ci/scripts/buildx.sh

push:
@echo "Already done by buildx (.ci/scripts/buildx.sh)."
atomic-push:
@echo "Already done by buildx (.ci/scripts/buildx.sh)."
endif
47 changes: 32 additions & 15 deletions go/darwin/Dockerfile.tmpl
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{{- if or (eq .DEBIAN_VERSION "10") (eq .DEBIAN_VERSION "11")}}
ARG REPOSITORY
ARG VERSION
ARG TAG_EXTENSION=''
FROM docker.elastic.co/beats-dev/golang-crossbuild:llvm-apple${TAG_EXTENSION} AS BUILD_LLVM_APPLE
{{- end }}

ARG REPOSITORY
ARG VERSION
ARG TAG_EXTENSION=''
{{- if or (eq .DEBIAN_VERSION "10") (eq .DEBIAN_VERSION "11")}}
FROM --platform=linux/amd64 docker.elastic.co/beats-dev/golang-crossbuild:llvm-apple-debian{{ .DEBIAN_VERSION }}-amd64 AS build-llvm-apple-amd64
FROM --platform=linux/arm64 docker.elastic.co/beats-dev/golang-crossbuild:llvm-apple-debian{{ .DEBIAN_VERSION }}-arm64 AS build-llvm-apple-arm64
# workaround to https://github.com/moby/moby/issues/34482
ARG TARGETARCH
ARG BUILDARCH
FROM build-llvm-apple-${TARGETARCH} as build-llvm-apple
ARG TARGETARCH
ARG BUILDARCH
RUN echo "Building ${TARGETARCH} on a ${BUILDARCH}"
{{- end }}
FROM ${REPOSITORY}/golang-crossbuild:${VERSION}-base${TAG_EXTENSION}

RUN \
Expand All @@ -29,7 +34,7 @@ RUN \

{{- if or (eq .DEBIAN_VERSION "10") (eq .DEBIAN_VERSION "11")}}
ARG OSXCROSS_PATH=/usr/local/osxcross
COPY --from=BUILD_LLVM_APPLE /osxcross.tar.gz /tmp/osxcross.tar.gz
COPY --from=build-llvm-apple /osxcross.tar.gz /tmp/osxcross.tar.gz
RUN tar -xzf /tmp/osxcross.tar.gz -C / \
&& rm /tmp/osxcross.tar.gz
{{- else }}
Expand All @@ -51,23 +56,35 @@ RUN \

ENV PATH $PATH:$OSXCROSS_PATH/bin
# Add osxcross libraries to the library PATH
ENV LD_LIBRARY_PATH ${OSXCROSS_PATH}/lib:$LD_LIBRARY_PATH
ENV LD_LIBRARY_PATH $OSXCROSS_PATH/lib:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH

COPY rootfs /

# Basic test
RUN cd / \
&& o64-clang helloWorld.c -o helloWorld \
&& file helloWorld \
&& file helloWorld | grep -c 'Mach-O 64-bit x86_64'
&& o64-clang helloWorld.c -o helloWorld.x86_64 \
&& file helloWorld.x86_64 \
&& file helloWorld.x86_64 | grep -c 'Mach-O 64-bit x86_64'

{{- if or (eq .DEBIAN_VERSION "10") (eq .DEBIAN_VERSION "11")}}
RUN cd / \
&& oa64-clang helloWorld.c -o helloWorld.arm64 \
&& file helloWorld.arm64 \
&& file helloWorld.arm64 | grep -c 'Mach-O 64-bit arm64'

RUN cd / \
&& lipo -create -output helloWorld.universal helloWorld.x86_64 helloWorld.arm64 \
&& file helloWorld.universal | grep 'Mach-O universal binary' \
&& rm helloWorld.*
{{- end }}

# MacOSX10.14 SDK does not have 32bits compiler
{{- if and (ne .DEBIAN_VERSION "10") (ne .DEBIAN_VERSION "11")}}
RUN cd / \
&& o32-clang helloWorld.c -o helloWorld \
&& file helloWorld \
&& file helloWorld | grep -c 'Mach-O i386' \
&& rm helloWorld.c helloWorld
&& o32-clang helloWorld.c -o helloWorld.i368 \
&& file helloWorld.i368 \
&& file helloWorld.i368 | grep -c 'Mach-O i386' \
&& rm helloWorld.*
{{- end }}

# Build-time metadata as defined at http://label-schema.org.
Expand Down
9 changes: 9 additions & 0 deletions go/darwin/Makefile
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
include ../Makefile.common

ifeq ($(DOCKER_MULTIARCH),1)
DOCKER_CMD := $(SELF_DIR)/../.ci/scripts/buildx.sh

push:
@echo "Already done by buildx (.ci/scripts/buildx.sh)."
atomic-push:
@echo "Already done by buildx (.ci/scripts/buildx.sh)."
endif

0 comments on commit 831fe5e

Please sign in to comment.