From 4d6a38074275224c99d0a90d470124e571961c5f Mon Sep 17 00:00:00 2001 From: Tarak Ben Youssef <50252200+tarakby@users.noreply.github.com> Date: Fri, 16 Feb 2024 10:00:31 -0600 Subject: [PATCH] Merge pull request #548 from onflow/tarak/fix-build Specify C compiler in build cross-compilation --- cmd/emulator/Dockerfile | 12 +++++++- cmd/emulator/Dockerfile.relic | 55 ----------------------------------- 2 files changed, 11 insertions(+), 56 deletions(-) delete mode 100644 cmd/emulator/Dockerfile.relic diff --git a/cmd/emulator/Dockerfile b/cmd/emulator/Dockerfile index f378819b..0ed9f8cf 100644 --- a/cmd/emulator/Dockerfile +++ b/cmd/emulator/Dockerfile @@ -12,15 +12,25 @@ WORKDIR /app # add the pubkey of github.com to knownhosts, so ssh-agent doesn't bark RUN mkdir -p /root/.ssh && ssh-keyscan -t rsa github.com >> /root/.ssh/known_hosts RUN git config --global 'url.ssh://git@github.com/.insteadOf' https://github.com/ +RUN apt-get update && apt-get -y install apt-utils gcc-aarch64-linux-gnu COPY . . ARG TARGETOS ARG TARGETARCH +ENV GO111MODULE=on \ + CGO_ENABLED=1 \ + CGO_FLAGS="-O2 -D__BLST_PORTABLE__" \ + GOOS=$TARGETOS \ + GOARCH=$TARGETARCH RUN --mount=type=ssh \ --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ - GO111MODULE=on CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \ + if [ "$TARGETARCH" = "arm64" ] ; then \ + export CC=aarch64-linux-gnu-gcc; \ + elif [ "$TARGETARCH" = "amd64" ] ; then \ + export CC=x86_64-linux-gnu-gcc; \ + fi; \ go build -ldflags "-extldflags -static" -o ./app ./cmd/emulator ## Add the binary to a fresh distroless image diff --git a/cmd/emulator/Dockerfile.relic b/cmd/emulator/Dockerfile.relic deleted file mode 100644 index 62bfe8dc..00000000 --- a/cmd/emulator/Dockerfile.relic +++ /dev/null @@ -1,55 +0,0 @@ -# NOTE: Must be run in the context of the repo's root directory - -## (1) Build Relic first to maximize caching -FROM golang:1.13-alpine3.10 AS build-relic - -RUN apk update && apk add \ - build-base \ - cmake \ - bash - -RUN mkdir /build -WORKDIR /build - -# Copy over *only* files necessary for Relic -COPY crypto/relic ./relic -COPY crypto/relic_build.sh . - -# Build Relic (this places build artifacts in /build/relic/build) -# NOTE: The Relic build script uses Bash-specific features, so we explicitly run -# it with bash rather than the default shell. -RUN bash ./relic_build.sh - -## (2) Build the app binary -FROM golang:1.13-alpine3.10 AS build-app - -RUN apk update && apk add build-base - -# Build the app binary in /app -RUN mkdir /app -WORKDIR /app - -COPY go.mod . -COPY go.sum . -RUN go mod download - -COPY . . -# Copy over Relic build artifacts -COPY --from=build-relic /build/relic/build ./crypto/relic/build - -RUN GO111MODULE=on CGO_ENABLED=1 GOOOS=linux GOARCH=amd64 go build -o ./app ./cmd/flow - -## (3) Add the binary to a fresh Alpine image -FROM alpine:3.10 - -COPY --from=build-app /app/app /bin/app - -# Expose GRPC and HTTP ports -EXPOSE 8080 -EXPOSE 3569 - -# Run the CLI binary as the entrypoint -ENTRYPOINT ["/bin/app"] -# These arguments are separated from the entrypoint to simplify running other -# commands with this image. -CMD ["emulator", "start", "--init", "--persist"]