4 # This script builds various binary artifacts from a checkout of the docker
8 # - The current directory should be a checkout of the docker source code
9 # (https://github.com/docker/docker). Whatever version is checked out
11 # - The VERSION file, at the root of the repository, should exist, and
12 # will be used as Docker binary version and package version.
13 # - The hash of the git commit will also be included in the Docker binary,
14 # with the suffix -unsupported if the repository isn't clean.
15 # - The script is intended to be run inside the docker container specified
16 # in the Dockerfile at the root of the source. In other words:
17 # DO NOT CALL THIS SCRIPT DIRECTLY.
18 # - The right way to call this script is to invoke "make" from
19 # your checkout of the Docker repository.
20 # the Makefile will do a "docker build -t docker ." and then
21 # "docker run hack/make.sh" in the resulting image.
26 export DOCKER_PKG='github.com/docker/docker'
27 export SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
28 export MAKEDIR="$SCRIPTDIR/make"
29 export PKG_CONFIG=${PKG_CONFIG:-pkg-config}
31 # We're a nice, sexy, little shell script, and people might try to run us;
32 # but really, they shouldn't. We want to be in a container!
33 inContainer="AssumeSoInitially"
34 if [ "$(go env GOHOSTOS)" = 'windows' ]; then
35 if [ -z "$FROM_DOCKERFILE" ]; then
39 if [ "$PWD" != "/go/src/$DOCKER_PKG" ]; then
44 if [ -z "$inContainer" ]; then
46 echo "# WARNING! I don't seem to be running in a Docker container."
47 echo "# The result of this command might be an incorrect build, and will not be"
48 echo "# officially supported."
50 echo "# Try this instead: make all"
57 # List of bundles to create when no argument is passed
72 VERSION=$(< ./VERSION)
73 ! BUILDTIME=$(date --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')
74 if [ "$DOCKER_GITCOMMIT" ]; then
75 GITCOMMIT="$DOCKER_GITCOMMIT"
76 elif command -v git &> /dev/null && [ -d .git ] && git rev-parse &> /dev/null; then
77 GITCOMMIT=$(git rev-parse --short HEAD)
78 if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
79 GITCOMMIT="$GITCOMMIT-unsupported"
80 echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
81 echo "# GITCOMMIT = $GITCOMMIT"
82 echo "# The version you are building is listed as unsupported because"
83 echo "# there are some files in the git repository that are in an uncommitted state."
84 echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
85 echo "# Here is the current list:"
86 git status --porcelain --untracked-files=no
87 echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
90 echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
91 echo >&2 ' Please either build with the .git directory accessible, or specify the'
92 echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
93 echo >&2 ' future accountability in diagnosing build issues. Thanks!'
97 if [ "$AUTO_GOPATH" ]; then
99 mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
100 ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
101 export GOPATH="${PWD}/.gopath"
103 if [ "$(go env GOOS)" = 'solaris' ]; then
104 # sys/unix is installed outside the standard library on solaris
105 # TODO need to allow for version change, need to get version from go
106 export GO_VERSION=${GO_VERSION:-"1.8.1"}
107 export GOPATH="${GOPATH}:/usr/lib/gocode/${GO_VERSION}"
111 if [ ! "$GOPATH" ]; then
112 echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH'
113 echo >&2 ' alternatively, set AUTO_GOPATH=1'
117 if ${PKG_CONFIG} 'libsystemd >= 209' 2> /dev/null ; then
118 DOCKER_BUILDTAGS+=" journald"
119 elif ${PKG_CONFIG} 'libsystemd-journal' 2> /dev/null ; then
120 DOCKER_BUILDTAGS+=" journald journald_compat"
123 # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately
125 command -v gcc &> /dev/null \
126 && ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \
128 DOCKER_BUILDTAGS+=' btrfs_noversion'
131 # test whether "libdevmapper.h" is new enough to support deferred remove
134 command -v gcc &> /dev/null \
135 && ! ( echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -xc - -o /dev/null -ldevmapper &> /dev/null ) \
137 DOCKER_BUILDTAGS+=' libdm_no_deferred_remove'
140 # Use these flags when compiling the tests and final binary
143 if [ -z "$DOCKER_DEBUG" ]; then
148 EXTLDFLAGS_STATIC='-static'
149 # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
150 # with options like -race.
151 ORIG_BUILDFLAGS=( -tags "autogen netgo static_build $DOCKER_BUILDTAGS" -installsuffix netgo )
152 # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
154 # When $DOCKER_INCREMENTAL_BINARY is set in the environment, enable incremental
155 # builds by installing dependent packages to the GOPATH.
157 if [ "$DOCKER_INCREMENTAL_BINARY" == "1" ] || [ "$DOCKER_INCREMENTAL_BINARY" == "true" ]; then
160 ORIG_BUILDFLAGS+=( $REBUILD_FLAG )
162 BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
165 if [ "${DOCKER_ENGINE_GOARCH}" == "arm" ]; then
167 elif [ "${DOCKER_ENGINE_GOARCH}" == "windows" ]; then
173 LDFLAGS_STATIC_DOCKER="
175 -extldflags \"$EXTLDFLAGS_STATIC\"
178 if [ "$(uname -s)" = 'FreeBSD' ]; then
179 # Tell cgo the compiler is Clang, not GCC
180 # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
183 # "-extld clang" is a workaround for
184 # https://code.google.com/p/go/issues/detail?id=6845
185 LDFLAGS="$LDFLAGS -extld clang"
190 go help testflag | grep -- -cover > /dev/null \
191 && go tool -n cover > /dev/null 2>&1 \
196 # a helper to provide ".exe" when it's appropriate
198 if [ "$(go env GOOS)" = 'windows' ]; then
204 while [ $# -gt 0 ]; do
207 dir="$(dirname "$f")"
208 base="$(basename "$f")"
209 for hashAlgo in md5 sha256; do
210 if command -v "${hashAlgo}sum" &> /dev/null; then
212 # subshell and cd so that we get output files like:
213 # $HASH docker-$VERSION
215 # $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
217 "${hashAlgo}sum" "$base" > "$base.$hashAlgo"
225 local bundle="$1"; shift
226 echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
227 source "$SCRIPTDIR/make/$bundle" "$@"
232 # Add nested executables to bundle dir so we have complete set of
233 # them available, but only if the native OS/ARCH is the same as the
234 # OS/ARCH of the build target
235 if [ "$(go env GOOS)/$(go env GOARCH)" == "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
236 if [ -x /usr/local/bin/docker-runc ]; then
237 echo "Copying nested executables into $dir"
238 for file in containerd containerd-shim containerd-ctr runc init proxy; do
239 cp -f `which "docker-$file"` "$dir/"
240 if [ "$2" == "hash" ]; then
241 hash_files "$dir/docker-$file"
250 target="${DOCKER_MAKE_INSTALL_PREFIX:=/usr/local}/bin/"
251 if [ "$(go env GOOS)" == "linux" ]; then
252 echo "Installing $(basename $file) to ${target}"
254 cp -f -L "$file" "$target"
256 echo "Install is only supported on linux"
262 # We want this to fail if the bundles already exist and cannot be removed.
263 # This is to avoid mixing bundles from different versions of the code.
265 if [ -e "bundles/$VERSION" ] && [ -z "$KEEPBUNDLE" ]; then
266 echo "bundles/$VERSION already exists. Removing."
267 rm -fr "bundles/$VERSION" && mkdir "bundles/$VERSION" || exit 1
271 if [ "$(go env GOHOSTOS)" != 'windows' ]; then
272 # Windows and symlinks don't get along well
275 ln -s "$VERSION" bundles/latest
278 if [ $# -lt 1 ]; then
279 bundles=(${DEFAULT_BUNDLES[@]})
283 for bundle in ${bundles[@]}; do
284 export DEST="bundles/$VERSION/$(basename "$bundle")"
285 # Cygdrive paths don't play well with go build -o.
286 if [[ "$(uname -s)" == CYGWIN* ]]; then
287 export DEST="$(cygpath -mw "$DEST")"
290 ABS_DEST="$(cd "$DEST" && pwd -P)"