Support building a dynbinary

Cleanup dynbinary and binary builds

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-05-11 18:52:17 -04:00
parent c0cbb6580a
commit a787cbc93b
10 changed files with 101 additions and 25 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
.DS_Store
build
./build

View File

@ -2,11 +2,6 @@
# github.com/docker/cli
#
# build the CLI
.PHONY: build
build: clean
@./scripts/build/binary
# remove build artifacts
.PHONY: clean
clean:
@ -18,16 +13,24 @@ clean:
test:
@go test -tags daemon -v $(shell go list ./... | grep -v /vendor/)
# run linters
.PHONY: lint
lint:
@gometalinter --config gometalinter.json ./...
.PHONY: binary
binary:
@./scripts/build/binary
# build the CLI for multiple architectures
.PHONY: cross
cross: clean
cross:
@./scripts/build/cross
.PHONY: dynbinary
dynbinary:
@./scripts/build/dynbinary
# download dependencies (vendor/) listed in vendor.conf
.PHONY: vendor
vendor: vendor.conf

View File

@ -6,6 +6,7 @@
DEV_DOCKER_IMAGE_NAME = docker-cli-dev
LINTER_IMAGE_NAME = docker-cli-lint
CROSS_IMAGE_NAME = docker-cli-cross
MOUNTS = -v `pwd`:/go/src/github.com/docker/cli
# build docker image (dockerfiles/Dockerfile.build)
@ -18,6 +19,11 @@ build_docker_image:
build_linter_image:
@docker build -q -t $(LINTER_IMAGE_NAME) -f ./dockerfiles/Dockerfile.lint .
.PHONY: build_cross_image
build_cross_image:
@docker build -t $(CROSS_IMAGE_NAME) -f ./dockerfiles/Dockerfile.cross .
# build executable using a container
.PHONY: build
build: build_docker_image
@ -44,6 +50,8 @@ cross: build_docker_image
dev: build_docker_image
@docker run -ti $(MOUNTS) -v /var/run/docker.sock:/var/run/docker.sock $(DEV_DOCKER_IMAGE_NAME) ash
shell: dev
# run linters in a container
.PHONY: lint
lint: build_linter_image
@ -53,3 +61,6 @@ lint: build_linter_image
.PHONY: vendor
vendor: build_docker_image vendor.conf
@docker run -ti --rm $(MOUNTS) $(DEV_DOCKER_IMAGE_NAME) make vendor
dynbinary: build_cross_image
@docker run -ti --rm $(MOUNTS) $(CROSS_IMAGE_NAME) make dynbinary

View File

@ -1,7 +1,7 @@
FROM golang:1.8-alpine
RUN apk add -U git make
RUN apk add -U git make bash
RUN go get github.com/LK4D4/vndr && \
cp /go/bin/vndr /usr/bin && \

View File

@ -0,0 +1,19 @@
FROM golang:1.8.1
# allow replacing httpredir or deb mirror
ARG APT_MIRROR=deb.debian.org
RUN sed -ri "s/(httpredir|deb).debian.org/$APT_MIRROR/g" /etc/apt/sources.list
RUN apt-get update && apt-get install -y \
libltdl-dev \
;
# build-essential \
# binutils-mingw-w64 gcc-mingw-w64
RUN go get github.com/mitchellh/gox && \
cp /go/bin/gox /usr/bin && \
rm -rf /go/src/* /go/pkg/* /go/bin/*
WORKDIR /go/src/github.com/docker/cli

18
scripts/build/.variables Executable file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -eu
VERSION=${VERSION:-"unknown-version"}
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
BUILDTIME=${BUILDTIME:-$(date --utc --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')}
export LDFLAGS="\
-w \
-X github.com/docker/cli/cli.GitCommit=${GITCOMMIT} \
-X github.com/docker/cli/cli.BuildTime=${BUILDTIME} \
-X github.com/docker/cli/cli.Version=${VERSION} \
${LDFLAGS:-} \
"
export TARGET="build/docker-$(go env GOHOSTOS)-$(go env GOHOSTARCH)"
export SOURCE="github.com/docker/cli/cmd/docker"

View File

@ -1,5 +1,13 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
#
# Build a static binary for the host OS/ARCH
#
source ./scripts/build/ldflags
set -eu -o pipefail
go build -o ./build/docker --ldflags "${LDFLAGS}" github.com/docker/cli/cmd/docker
source ./scripts/build/.variables
export CGO_ENABLED=0
go build -o "${TARGET}" --ldflags "${LDFLAGS}" "${SOURCE}"
ln -sf "$(basename ${TARGET})" build/docker

View File

@ -1,8 +1,21 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
source ./scripts/build/ldflags
set -eu -o pipefail
source ./scripts/build/.variables
# Wow, gox doesn't like extra spaces between these strings...
CROSS_OSARCH="\
windows/amd64"
#darwin/amd64 \
#linux/amd64 \
#linux/arm \
# Currently broken
# linux/ppc64le \
gox -output build/docker-{{.OS}}-{{.Arch}} \
-osarch="linux/arm linux/amd64 darwin/amd64 windows/amd64" \
-osarch "${CROSS_OSARCH}" \
--ldflags "${LDFLAGS}" \
github.com/docker/cli/cmd/docker
${SOURCE}

13
scripts/build/dynbinary Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
#
# Build a dynamically linked binary for the host OS/ARCH
#
set -eu -o pipefail
source ./scripts/build/.variables
export CGO_ENABLED=1
go build -o "${TARGET}" -tags pkcs11 --ldflags "${LDFLAGS}" "${SOURCE}"
ln -sf "$(basename ${TARGET})" build/docker

View File

@ -1,9 +0,0 @@
#!/usr/bin/env bash
VERSION=${VERSION:-"unknown-version"}
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
BUILDTIME=${BUILDTIME:-$(date --utc --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')}
export LDFLAGS="-X github.com/docker/cli/cli.GitCommit=${GITCOMMIT} \
-X github.com/docker/cli/cli.BuildTime=${BUILDTIME} \
-X github.com/docker/cli/cli.Version=${VERSION} ${LDFLAGS}"