diff --git a/.gitignore b/.gitignore index 30944692f3..896ffb3ad8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .DS_Store -build +./build diff --git a/Makefile b/Makefile index 932568bf73..74e3e2d509 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/docker.Makefile b/docker.Makefile index e0d8643da7..d502ff2e73 100644 --- a/docker.Makefile +++ b/docker.Makefile @@ -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 diff --git a/dockerfiles/Dockerfile.build b/dockerfiles/Dockerfile.build index 13ee07f252..5d09e18c4e 100644 --- a/dockerfiles/Dockerfile.build +++ b/dockerfiles/Dockerfile.build @@ -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 && \ diff --git a/dockerfiles/Dockerfile.cross b/dockerfiles/Dockerfile.cross new file mode 100644 index 0000000000..2af3d24aef --- /dev/null +++ b/dockerfiles/Dockerfile.cross @@ -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 diff --git a/scripts/build/.variables b/scripts/build/.variables new file mode 100755 index 0000000000..12ca95e7d1 --- /dev/null +++ b/scripts/build/.variables @@ -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" diff --git a/scripts/build/binary b/scripts/build/binary index 9f6f472812..d4faac11b5 100755 --- a/scripts/build/binary +++ b/scripts/build/binary @@ -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 diff --git a/scripts/build/cross b/scripts/build/cross index 4e113943a4..62cb687aa7 100755 --- a/scripts/build/cross +++ b/scripts/build/cross @@ -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} diff --git a/scripts/build/dynbinary b/scripts/build/dynbinary new file mode 100755 index 0000000000..153a62fbdd --- /dev/null +++ b/scripts/build/dynbinary @@ -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 diff --git a/scripts/build/ldflags b/scripts/build/ldflags deleted file mode 100755 index a0c5e1f9e6..0000000000 --- a/scripts/build/ldflags +++ /dev/null @@ -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}"