Skip to content

Commit

Permalink
Dockerfile: WIP cross-compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
rvolosatovs committed Mar 31, 2022
1 parent 4b458a8 commit 754bd62
Showing 1 changed file with 120 additions and 62 deletions.
182 changes: 120 additions & 62 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,93 @@ ARG SWIFT_VERSION
ARG NODE_VERSION
ARG GOTEMPLATE_VERSION

FROM alpine:${ALPINE_VERSION} as protoc_builder
FROM --platform=$BUILDPLATFORM alpine:${ALPINE_VERSION} as protoc_cross_builder
RUN apk add --no-cache build-base curl automake autoconf libtool git zlib-dev linux-headers cmake ninja

RUN mkdir -p /out

ARG GOOGLE_API_VERSION
RUN mkdir -p /googleapis
RUN curl -sSL https://api.github.com/repos/googleapis/googleapis/tarball/${GOOGLE_API_VERSION} | tar xz --strip 1 -C /googleapis
WORKDIR /googleapis
RUN install -D ./google/api/annotations.proto /out/usr/include/google/api/annotations.proto
RUN install -D ./google/api/field_behavior.proto /out/usr/include/google/api/field_behavior.proto
RUN install -D ./google/api/http.proto /out/usr/include/google/api/http.proto
RUN install -D ./google/api/httpbody.proto /out/usr/include/google/api/httpbody.proto

ARG GRPC_VERSION
RUN git clone --recursive --depth=1 -b v${GRPC_VERSION} https://github.com/grpc/grpc.git /grpc && \
ln -s /grpc/third_party/protobuf /protobuf && \
mkdir -p /grpc/cmake/build && \
cd /grpc/cmake/build && \
cmake \
RUN git clone --recursive --depth=1 -b v${GRPC_VERSION} https://github.com/grpc/grpc.git /grpc
RUN ln -s /grpc/third_party/protobuf /protobuf
RUN mkdir -p /grpc/cmake/build
RUN cd /grpc/cmake/build
RUN cmake \
-GNinja \
-DBUILD_SHARED_LIBS=ON \
-DCMAKE_INSTALL_PREFIX=/usr \
-DCMAKE_BUILD_TYPE=Release \
-DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
../.. && \
cmake --build . --target plugins && \
cmake --build . --target install && \
DESTDIR=/out cmake --build . --target install
../..
RUN cmake --build . --target plugins
RUN cmake --build . --target install
ARG BUILDPLATFORM TARGETARCH
RUN <<EOF
if [ ${TARGETARCH} != ${BUILDPLATFORM} ]; then
case ${TARGETARCH} in \
"amd64") C_TARGET=amd64 ;; \
"arm64") C_TARGET=aarch64 ;; \
*) echo "ERROR: Machine arch ${TARGETARCH} not supported." ;; \
esac && \
cat > /tmp/toolchain.cmake <<EOT
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR ${C_TARGET})
set(CMAKE_STAGING_PREFIX /tmp/stage)
set(CMAKE_C_COMPILER /usr/bin/${C_TARGET}-linux-gnu-gcc-6)
set(CMAKE_CXX_COMPILER /usr/bin/${C_TARGET}-linux-gnu-g++-6)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
EOT
cd ..
mkdir -p 'build-cross'
cd 'build-cross'
cmake \
-GNinja \
-DBUILD_SHARED_LIBS=ON \
-DCMAKE_TOOLCHAIN_FILE=/tmp/toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=/usr \
-DCMAKE_BUILD_TYPE=Release \
-DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
../..
DESTDIR=/out cmake --build . --target install
else
DESTDIR=/out cmake --build . --target install
fi;
EOF

FROM alpine:${ALPINE_VERSION} as protoc_builder
RUN apk add --no-cache build-base curl automake autoconf libtool zlib-dev linux-headers

RUN mkdir -p /out
COPY --from=protoc_cross_builder /out/ /

ARG PROTOBUF_C_VERSION
RUN mkdir -p /protobuf-c && \
curl -sSL https://api.github.com/repos/protobuf-c/protobuf-c/tarball/v${PROTOBUF_C_VERSION} | tar xz --strip 1 -C /protobuf-c && \
cd /protobuf-c && \
export LD_LIBRARY_PATH=/usr/lib && \
RUN mkdir -p /protobuf-c
RUN curl -sSL https://api.github.com/repos/protobuf-c/protobuf-c/tarball/v${PROTOBUF_C_VERSION} | tar xz --strip 1 -C /protobuf-c
WORKDIR /protobuf-c
RUN export LD_LIBRARY_PATH=/usr/lib && \
export PKG_CONFIG_PATH=/usr/lib/pkgconfig && \
./autogen.sh && \
./configure --prefix=/usr && \
make && make install DESTDIR=/out

ARG GRPC_JAVA_VERSION
RUN mkdir -p /grpc-java && \
curl -sSL https://api.github.com/repos/grpc/grpc-java/tarball/v${GRPC_JAVA_VERSION} | tar xz --strip 1 -C /grpc-java && \
cd /grpc-java && \
g++ \
RUN curl -sSL https://api.github.com/repos/grpc/grpc-java/tarball/v${GRPC_JAVA_VERSION} | tar xz --strip 1 -C /grpc-java
WORKDIR /grpc-java
RUN g++ \
-I. -I/usr/include \
compiler/src/java_plugin/cpp/*.cpp \
-L/usr/lib \
Expand All @@ -52,51 +102,64 @@ RUN mkdir -p /grpc-java && \

ARG GRPC_WEB_VERSION
RUN mkdir -p /grpc-web && \
curl -sSL https://api.github.com/repos/grpc/grpc-web/tarball/${GRPC_WEB_VERSION} | tar xz --strip 1 -C /grpc-web && \
cd /grpc-web && \
make install-plugin && \
install -Ds /usr/local/bin/protoc-gen-grpc-web /out/usr/bin/protoc-gen-grpc-web
RUN curl -sSL https://api.github.com/repos/grpc/grpc-web/tarball/${GRPC_WEB_VERSION} | tar xz --strip 1 -C /grpc-web
WORKDIR /grpc-web
RUN make install-plugin
RUN install -Ds /usr/local/bin/protoc-gen-grpc-web /out/usr/bin/protoc-gen-grpc-web


FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as go_builder
RUN apk add --no-cache build-base curl git
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as protoc_gen_doc_builder
RUN apk add --no-cache build-base curl

RUN mkdir -p ${GOPATH}/src/github.com/pseudomuto/protoc-gen-doc
ARG PROTOC_GEN_DOC_VERSION
RUN mkdir -p ${GOPATH}/src/github.com/pseudomuto/protoc-gen-doc && \
curl -sSL https://api.github.com/repos/pseudomuto/protoc-gen-doc/tarball/v${PROTOC_GEN_DOC_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/pseudomuto/protoc-gen-doc && \
cd ${GOPATH}/src/github.com/pseudomuto/protoc-gen-doc && \
go build -ldflags '-w -s' -o /protoc-gen-doc-out/protoc-gen-doc ./cmd/protoc-gen-doc && \
install -Ds /protoc-gen-doc-out/protoc-gen-doc /out/usr/bin/protoc-gen-doc
RUN curl -sSL https://api.github.com/repos/pseudomuto/protoc-gen-doc/tarball/v${PROTOC_GEN_DOC_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/pseudomuto/protoc-gen-doc
RUN cd ${GOPATH}/src/github.com/pseudomuto/protoc-gen-doc
ARG TARGETOS TARGETARCH
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /protoc-gen-doc-out/protoc-gen-doc ./cmd/protoc-gen-doc
RUN install -Ds /protoc-gen-doc-out/protoc-gen-doc /out/usr/bin/protoc-gen-doc

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as protoc_gen_go_grpc_builder
RUN apk add --no-cache build-base curl

RUN mkdir -p ${GOPATH}/src/github.com/grpc/grpc-go
ARG PROTOC_GEN_GO_GRPC_VERSION
RUN mkdir -p ${GOPATH}/src/github.com/grpc/grpc-go && \
curl -sSL https://api.github.com/repos/grpc/grpc-go/tarball/v${PROTOC_GEN_GO_GRPC_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/grpc/grpc-go &&\
cd ${GOPATH}/src/github.com/grpc/grpc-go/cmd/protoc-gen-go-grpc && \
go build -ldflags '-w -s' -o /golang-protobuf-out/protoc-gen-go-grpc . && \
install -Ds /golang-protobuf-out/protoc-gen-go-grpc /out/usr/bin/protoc-gen-go-grpc
RUN curl -sSL https://api.github.com/repos/grpc/grpc-go/tarball/v${PROTOC_GEN_GO_GRPC_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/grpc/grpc-go
RUN cd ${GOPATH}/src/github.com/grpc/grpc-go/cmd/protoc-gen-go-grpc
ARG TARGETOS TARGETARCH
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /golang-protobuf-out/protoc-gen-go-grpc .
RUN install -Ds /golang-protobuf-out/protoc-gen-go-grpc /out/usr/bin/protoc-gen-go-grpc

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as protoc_gen_go_builder
RUN apk add --no-cache build-base curl

RUN mkdir -p ${GOPATH}/src/google.golang.org/protobuf
ARG PROTOC_GEN_GO_VERSION
RUN mkdir -p ${GOPATH}/src/google.golang.org/protobuf && \
curl -sSL https://api.github.com/repos/protocolbuffers/protobuf-go/tarball/v${PROTOC_GEN_GO_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/google.golang.org/protobuf &&\
cd ${GOPATH}/src/google.golang.org/protobuf && \
go build -ldflags '-w -s' -o /golang-protobuf-out/protoc-gen-go ./cmd/protoc-gen-go && \
install -Ds /golang-protobuf-out/protoc-gen-go /out/usr/bin/protoc-gen-go
RUN curl -sSL https://api.github.com/repos/protocolbuffers/protobuf-go/tarball/v${PROTOC_GEN_GO_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/google.golang.org/protobuf
RUN cd ${GOPATH}/src/google.golang.org/protobuf
ARG TARGETOS TARGETARCH
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /golang-protobuf-out/protoc-gen-go ./cmd/protoc-gen-go
RUN install -Ds /golang-protobuf-out/protoc-gen-go /out/usr/bin/protoc-gen-go

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as protoc_gen_gogo_builder
RUN apk add --no-cache build-base curl

RUN mkdir -p ${GOPATH}/src/github.com/gogo/protobuf
ARG PROTOC_GEN_GOGO_VERSION
RUN mkdir -p ${GOPATH}/src/github.com/gogo/protobuf && \
curl -sSL https://api.github.com/repos/gogo/protobuf/tarball/v${PROTOC_GEN_GOGO_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/gogo/protobuf &&\
cd ${GOPATH}/src/github.com/gogo/protobuf && \
go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gofast ./protoc-gen-gofast && \
go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogo ./protoc-gen-gogo && \
go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogofast ./protoc-gen-gogofast && \
go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogofaster ./protoc-gen-gogofaster && \
go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogoslick ./protoc-gen-gogoslick && \
go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogotypes ./protoc-gen-gogotypes && \
go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gostring ./protoc-gen-gostring && \
install -D $(find /gogo-protobuf-out -name 'protoc-gen-*') -t /out/usr/bin && \
mkdir -p /out/usr/include/github.com/gogo/protobuf/protobuf/google/protobuf && \
install -D $(find ./protobuf/google/protobuf -name '*.proto') -t /out/usr/include/github.com/gogo/protobuf/protobuf/google/protobuf && \
install -D ./gogoproto/gogo.proto /out/usr/include/github.com/gogo/protobuf/gogoproto/gogo.proto
RUN curl -sSL https://api.github.com/repos/gogo/protobuf/tarball/v${PROTOC_GEN_GOGO_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/gogo/protobuf
RUN cd ${GOPATH}/src/github.com/gogo/protobuf
ARG TARGETOS TARGETARCH
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gofast ./protoc-gen-gofast
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogo ./protoc-gen-gogo
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogofast ./protoc-gen-gogofast
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogofaster ./protoc-gen-gogofaster
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogoslick ./protoc-gen-gogoslick
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gogotypes ./protoc-gen-gogotypes
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags '-w -s' -o /gogo-protobuf-out/protoc-gen-gostring ./protoc-gen-gostring
RUN install -D $(find /gogo-protobuf-out -name 'protoc-gen-*') -t /out/usr/bin
RUN mkdir -p /out/usr/include/github.com/gogo/protobuf/protobuf/google/protobuf
RUN install -D $(find ./protobuf/google/protobuf -name '*.proto') -t /out/usr/include/github.com/gogo/protobuf/protobuf/google/protobuf
RUN install -D ./gogoproto/gogo.proto /out/usr/include/github.com/gogo/protobuf/gogoproto/gogo.proto

ARG PROTOC_GEN_GOVALIDATORS_VERSION
RUN mkdir -p ${GOPATH}/src/github.com/mwitkow/go-proto-validators && \
Expand Down Expand Up @@ -144,15 +207,6 @@ RUN mkdir -p ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway && \
mkdir -p /out/usr/include/protoc-gen-openapiv2/options && \
install -D $(find ./protoc-gen-openapiv2/options -name '*.proto') -t /out/usr/include/protoc-gen-openapiv2/options

ARG GOOGLE_API_VERSION
RUN mkdir -p ${GOPATH}/src/github.com/googleapis/googleapis && \
curl -sSL https://api.github.com/repos/googleapis/googleapis/tarball/${GOOGLE_API_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/googleapis/googleapis && \
cd ${GOPATH}/src/github.com/googleapis/googleapis && \
install -D ./google/api/annotations.proto /out/usr/include/google/api/annotations.proto && \
install -D ./google/api/field_behavior.proto /out/usr/include/google/api/field_behavior.proto && \
install -D ./google/api/http.proto /out/usr/include/google/api/http.proto && \
install -D ./google/api/httpbody.proto /out/usr/include/google/api/httpbody.proto

ARG JSONSCHEMA_VERSION
RUN mkdir -p ${GOPATH}/src/github.com/chrusty/protoc-gen-jsonschema && \
curl -sSL https://api.github.com/repos/chrusty/protoc-gen-jsonschema/tarball/${JSONSCHEMA_VERSION} | tar xz --strip 1 -C ${GOPATH}/src/github.com/chrusty/protoc-gen-jsonschema && \
Expand Down Expand Up @@ -246,8 +300,12 @@ ARG TARGETARCH
RUN mkdir -p /upx && curl -sSL https://github.com/upx/upx/releases/download/v${UPX_VERSION}/upx-${UPX_VERSION}-${TARGETARCH}_linux.tar.xz | tar xJ --strip 1 -C /upx && \
install -D /upx/upx /usr/local/bin/upx

COPY --from=protoc_cross_builder /out/ /out/
COPY --from=protoc_builder /out/ /out/
COPY --from=go_builder /out/ /out/
COPY --from=protoc_gen_doc_builder /out/ /out/
COPY --from=protoc_gen_go_builder /out/ /out/
COPY --from=protoc_gen_go_grpc_builder /out/ /out/
COPY --from=protoc_gen_gogo_builder /out/ /out/
COPY --from=rust_builder /out/ /out/
COPY --from=swift_builder /protoc-gen-swift /out/protoc-gen-swift
COPY --from=dart_builder /out/ /out/
Expand Down

0 comments on commit 754bd62

Please sign in to comment.