From 0a561195ce13fd08c09d939019aa4ff1b501edf8 Mon Sep 17 00:00:00 2001 From: Calvin Leung Huang Date: Fri, 24 Jun 2016 02:21:19 -0400 Subject: [PATCH] Static linking! --- Makefile | 2 +- build/Dockerfile | 7 +++++-- build/build.sh | 15 ++++++++++++--- build/configure.sh | 8 ++++---- build/install-libgit2.sh | 13 ++++++++++--- build/install-libssh2.sh | 26 ++++++++++++++++++++++++++ build/install-openssl.sh | 21 +++++++++++++++++++++ 7 files changed, 79 insertions(+), 13 deletions(-) create mode 100755 build/install-libssh2.sh create mode 100755 build/install-openssl.sh diff --git a/Makefile b/Makefile index 60c6489..34a12a6 100644 --- a/Makefile +++ b/Makefile @@ -40,5 +40,5 @@ remove-dangling: run-build-image: @echo " ===> Building..." - @docker run --rm --name git2consul-builder -v $(CURDIR):/app -v $(CURDIR)/build/bin:/build/bin cimpress/git2consul-builder + @docker run --rm --name git2consul-builder -v $(CURDIR):/app -v $(CURDIR)/build/bin:/build/bin --entrypoint /app/build/build.sh cimpress/git2consul-builder .PHONY: run-build-image diff --git a/build/Dockerfile b/build/Dockerfile index 41704ef..14d7a35 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,7 +1,10 @@ FROM alpine:edge MAINTAINER Calvin Leung Huang -RUN apk --no-cache --no-progress add ca-certificates git libssh2 openssl \ +RUN echo "@testing http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories + +RUN apk update +RUN apk --no-cache --no-progress add ca-certificates git go gcc musl-dev make cmake http-parser@testing perl \ && rm -rf /var/cache/apk/* COPY . /build @@ -9,4 +12,4 @@ WORKDIR /app RUN /build/configure.sh -ENTRYPOINT ["/build/build.sh"] +ENTRYPOINT ["/app/build/build.sh"] diff --git a/build/build.sh b/build/build.sh index 102b9d2..2893f86 100755 --- a/build/build.sh +++ b/build/build.sh @@ -6,10 +6,19 @@ set -e export GOPATH=/tmp/go export PATH=${PATH}:${GOPATH}/bin export BUILDPATH=${GOPATH}/src/github.com/Cimpress-MCP/go-git2consul -export PKG_CONFIG_PATH="/usr/lib/pkgconfig/:/usr/local/lib/pkgconfig/" +export PKG_CONFIG_PATH="/usr/lib/pkgconfig:/usr/local/lib/pkgconfig" +export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/tmp/libgit2/install/lib/pkgconfig:/tmp/openssl/install/lib/pkgconfig:/tmp/libssh2/build/src" + +FLAGS=$(pkg-config --static --libs --cflags libssh2 libgit2) || exit 1 +export CGO_LDFLAGS="/tmp/libgit2/build/libgit2.a /tmp/openssl/libcrypto.a /tmp/openssl/libssl.a /tmp/libssh2/build/src/libssh2.a -L/tmp/libgit2/build ${FLAGS}" +export CGO_CFLAGS="-I/tmp/libgit2/include" + +# Get git commit information +GIT_COMMIT=$(git rev-parse HEAD) +GIT_DIRTY=$(test -n "`git status --porcelain`" && echo "+CHANGES" || true) # Build git2consul cd ${BUILDPATH} -go get -v -GOOS=linux GOARCH=amd64 go build -o /build/bin/git2consul.linux.amd64 . +go get -v -d +GOOS=linux GOARCH=amd64 go build -ldflags "-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -v -linkmode=external -extldflags '-static'" -o /build/bin/git2consul.linux.amd64 . # GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build -o /build/bin/git2consul.darwin.amd64 . diff --git a/build/configure.sh b/build/configure.sh index 0f7d9da..ae6f622 100755 --- a/build/configure.sh +++ b/build/configure.sh @@ -7,11 +7,11 @@ export GOPATH=/tmp/go export PATH=${PATH}:${GOPATH}/bin export BUILDPATH=${GOPATH}/src/github.com/Cimpress-MCP/go-git2consul export PKG_CONFIG_PATH="/usr/lib/pkgconfig/:/usr/local/lib/pkgconfig/" +export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/tmp/libgit2/install/lib/pkgconfig:/tmp/openssl/install/lib/pkgconfig:/tmp/libssh2/build/src" -# Install build deps -apk --no-cache --no-progress --virtual build-deps add go gcc musl-dev make cmake openssl-dev libssh2-dev - -# Install libgit2 +# Install libraries +/build/install-openssl.sh +/build/install-libssh2.sh /build/install-libgit2.sh # Set up go environment diff --git a/build/install-libgit2.sh b/build/install-libgit2.sh index 3966ac4..d8f84a4 100755 --- a/build/install-libgit2.sh +++ b/build/install-libgit2.sh @@ -1,19 +1,26 @@ #!/bin/sh set -x -set -e # Set temp environment vars export LIBGIT2REPO=https://github.com/libgit2/libgit2.git export LIBGIT2BRANCH=v0.24.0 export LIBGIT2PATH=/tmp/libgit2 +export PKG_CONFIG_PATH="/usr/lib/pkgconfig/:/usr/local/lib/pkgconfig/" +export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/tmp/libgit2/install/lib/pkgconfig:/tmp/openssl/install/lib/pkgconfig:/tmp/libssh2/build/src" # Compile & Install libgit2 (v0.23) git clone -b ${LIBGIT2BRANCH} --depth 1 -- ${LIBGIT2REPO} ${LIBGIT2PATH} mkdir -p ${LIBGIT2PATH}/build cd ${LIBGIT2PATH}/build -cmake -DBUILD_CLAR=OFF .. +cmake -DTHREADSAFE=ON \ + -DBUILD_CLAR=OFF \ + -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_C_FLAGS=-fPIC \ + -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ + -DCMAKE_INSTALL_PREFIX=../install \ + .. cmake --build . --target install # Cleanup -rm -r ${LIBGIT2PATH} +# rm -r ${LIBGIT2PATH} diff --git a/build/install-libssh2.sh b/build/install-libssh2.sh new file mode 100755 index 0000000..e29139a --- /dev/null +++ b/build/install-libssh2.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -x + +# Set temp environment vars +export REPO=https://github.com/libssh2/libssh2 +export BRANCH=libssh2-1.7.0 +export REPO_PATH=/tmp/libssh2 +export PKG_CONFIG_PATH="/usr/lib/pkgconfig/:/usr/local/lib/pkgconfig/" +export PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/tmp/libgit2/install/lib/pkgconfig:/tmp/openssl/install/lib/pkgconfig:/tmp/libssh2/build/src" + +# Compile & Install libgit2 (v0.23) +git clone -b ${BRANCH} --depth 1 -- ${REPO} ${REPO_PATH} + +mkdir -p ${REPO_PATH}/build +cd ${REPO_PATH}/build +cmake -DTHREADSAFE=ON \ + -DBUILD_CLAR=OFF \ + -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_C_FLAGS=-fPIC \ + -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ + -DCMAKE_INSTALL_PREFIX=../install \ + .. +cmake --build . --target install + +# Cleanup +# rm -r ${LIBGIT2PATH} diff --git a/build/install-openssl.sh b/build/install-openssl.sh new file mode 100755 index 0000000..f9989f7 --- /dev/null +++ b/build/install-openssl.sh @@ -0,0 +1,21 @@ +#!/bin/sh +set -x +set -e + +# Set temp environment vars +export REPO=https://github.com/openssl/openssl.git +export BRANCH=OpenSSL_1_0_2h +export BUILD_PATH=/tmp/openssl + +# Compile & Install libgit2 (v0.23) +git clone -b ${BRANCH} --depth 1 -- ${REPO} ${BUILD_PATH} + +mkdir -p ${BUILD_PATH}/install/lib +cd ${BUILD_PATH} +./config threads no-shared --prefix=${BUILD_PATH}/install -fPIC -DOPENSSL_PIC && +make depend && +make && +make install + +# Cleanup +# rm -r ${LIBGIT2PATH}