hack/make.sh
b1953bab
 #!/usr/bin/env bash
83a2e92d
 set -e
89ee5242
 
d9f76993
 # This script builds various binary artifacts from a checkout of the docker
 # source code.
89ee5242
 #
 # Requirements:
d9f76993
 # - The current directory should be a checkout of the docker source code
723d4338
 #   (https://github.com/docker/docker). Whatever version is checked out
d9f76993
 #   will be built.
 # - The VERSION file, at the root of the repository, should exist, and
 #   will be used as Docker binary version and package version.
 # - The hash of the git commit will also be included in the Docker binary,
3e298c59
 #   with the suffix -unsupported if the repository isn't clean.
927b334e
 # - The script is intended to be run inside the docker container specified
d9f76993
 #   in the Dockerfile at the root of the source. In other words:
 #   DO NOT CALL THIS SCRIPT DIRECTLY.
83d81758
 # - The right way to call this script is to invoke "make" from
b3d5e952
 #   your checkout of the Docker repository.
2a1181f4
 #   the Makefile will do a "docker build -t docker ." and then
a0505edc
 #   "docker run hack/make.sh" in the resulting image.
aa3de0b8
 #
89ee5242
 
83a2e92d
 set -o pipefail
89ee5242
 
3a14eb06
 export DOCKER_PKG='github.com/docker/docker'
23afce5f
 export SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6533cb97
 export MAKEDIR="$SCRIPTDIR/make"
20bf00df
 export PKG_CONFIG=${PKG_CONFIG:-pkg-config}
3a14eb06
 
477fe484
 : ${TEST_REPEAT:=0}
 
749a7d0e
 # We're a nice, sexy, little shell script, and people might try to run us;
 # but really, they shouldn't. We want to be in a container!
4dbd612d
 inContainer="AssumeSoInitially"
 if [ "$(go env GOHOSTOS)" = 'windows' ]; then
5601fc85
 	if [ -z "$FROM_DOCKERFILE" ]; then
4dbd612d
 		unset inContainer
 	fi
 else
 	if [ "$PWD" != "/go/src/$DOCKER_PKG" ] || [ -z "$DOCKER_CROSSPLATFORMS" ]; then
 		unset inContainer
 	fi
 fi
 
0c7201ee
 if [ -z "$inContainer" ]; then
b994c131
 	{
4dbd612d
 		echo "# WARNING! I don't seem to be running in a Docker container."
b994c131
 		echo "# The result of this command might be an incorrect build, and will not be"
4dbd612d
 		echo "# officially supported."
b994c131
 		echo "#"
 		echo "# Try this instead: make all"
 		echo "#"
 	} >&2
 fi
 
 echo
749a7d0e
 
3d39336a
 # List of bundles to create when no argument is passed
 DEFAULT_BUNDLES=(
81370b5b
 	validate-dco
9bc771af
 	validate-default-seccomp
81370b5b
 	validate-gofmt
bc8b8e03
 	validate-lint
9465272c
 	validate-pkg
ecccfa82
 	validate-test
d245a8a7
 	validate-toml
3280ce65
 	validate-vet
3a138570
 
9e7651db
 	binary-client
 	binary-daemon
0d47b74a
 	dynbinary
3a138570
 
a39f3c92
 	test-unit
2543912e
 	test-integration-cli
100267de
 	test-docker-py
3a138570
 
59dc2876
 	cover
62a81370
 	cross
b3f5973f
 	tgz
3d39336a
 )
 
4d53a195
 VERSION=$(< ./VERSION)
355ad330
 if command -v git &> /dev/null && [ -d .git ] && git rev-parse &> /dev/null; then
efd0e13c
 	GITCOMMIT=$(git rev-parse --short HEAD)
0a040645
 	if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
3e298c59
 		GITCOMMIT="$GITCOMMIT-unsupported"
181d2725
 		echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
 		echo "# GITCOMMIT = $GITCOMMIT"
 		echo "# The version you are building is listed as unsupported because"
 		echo "# there are some files in the git repository that are in an uncommited state."
 		echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
 		echo "# Here is the current list:"
 		git status --porcelain --untracked-files=no
 		echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
efd0e13c
 	fi
86d8758e
 	! BUILDTIME=$(date --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/') &> /dev/null
c10311bc
 	if [ -z $BUILDTIME ]; then
 		# If using bash 3.1 which doesn't support --rfc-3389, eg Windows CI
 		BUILDTIME=$(date -u)
 	fi
efd0e13c
 elif [ "$DOCKER_GITCOMMIT" ]; then
 	GITCOMMIT="$DOCKER_GITCOMMIT"
 else
 	echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
 	echo >&2 '  Please either build with the .git directory accessible, or specify the'
 	echo >&2 '  exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
 	echo >&2 '  future accountability in diagnosing build issues.  Thanks!'
 	exit 1
5b630d43
 fi
89ee5242
 
d3d85d38
 if [ "$AUTO_GOPATH" ]; then
 	rm -rf .gopath
3a14eb06
 	mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
 	ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
6f812a4e
 	export GOPATH="${PWD}/.gopath:${PWD}/vendor"
86d8758e
 
 	if [ "$(go env GOOS)" = 'solaris' ]; then
 		# sys/unix is installed outside the standard library on solaris
 		# TODO need to allow for version change, need to get version from go
fa82c0aa
 		export GOPATH="${GOPATH}:/usr/lib/gocode/1.6.2"
86d8758e
 	fi
d3d85d38
 fi
 
 if [ ! "$GOPATH" ]; then
723d4338
 	echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH'
d3d85d38
 	echo >&2 '  alternatively, set AUTO_GOPATH=1'
 	exit 1
 fi
 
557c7cb8
 if [ "$DOCKER_EXPERIMENTAL" ]; then
ca6722f1
 	echo >&2 '# WARNING! DOCKER_EXPERIMENTAL is set: building experimental features'
 	echo >&2
8d18e6b3
 	DOCKER_BUILDTAGS+=" experimental"
ca6722f1
 fi
 
8983d429
 DOCKER_BUILDTAGS+=" daemon"
20bf00df
 if ${PKG_CONFIG} 'libsystemd >= 209' 2> /dev/null ; then
9e7651db
 	DOCKER_BUILDTAGS+=" journald"
20bf00df
 elif ${PKG_CONFIG} 'libsystemd-journal' 2> /dev/null ; then
9e7651db
 	DOCKER_BUILDTAGS+=" journald journald_compat"
1b95590d
 fi
 
3761955e
 # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately
 if \
 	command -v gcc &> /dev/null \
7f608c39
 	&& ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \
3761955e
 ; then
 	DOCKER_BUILDTAGS+=' btrfs_noversion'
 fi
 
b3e29926
 # test whether "libdevmapper.h" is new enough to support deferred remove
 # functionality.
 if \
 	command -v gcc &> /dev/null \
e2076453
 	&& ! ( echo -e  '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -xc - -o /dev/null -ldevmapper &> /dev/null ) \
b3e29926
 ; then
        DOCKER_BUILDTAGS+=' libdm_no_deferred_remove'
 fi
 
b187cc40
 # Use these flags when compiling the tests and final binary
bce9ed0e
 
6871b9b1
 IAMSTATIC='true'
23afce5f
 source "$SCRIPTDIR/make/.go-autogen"
a121ac85
 if [ -z "$DOCKER_DEBUG" ]; then
 	LDFLAGS='-w'
 fi
bce9ed0e
 
927e1e98
 LDFLAGS_STATIC=''
be344cf0
 EXTLDFLAGS_STATIC='-static'
aa129b35
 # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
 # with options like -race.
1445e4db
 ORIG_BUILDFLAGS=( -tags "autogen netgo static_build sqlite_omit_load_extension $DOCKER_BUILDTAGS" -installsuffix netgo )
232d59ba
 # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
1445e4db
 
 # When $DOCKER_INCREMENTAL_BINARY is set in the environment, enable incremental
 # builds by installing dependent packages to the GOPATH.
 REBUILD_FLAG="-a"
 if [ "$DOCKER_INCREMENTAL_BINARY" ]; then
 	REBUILD_FLAG="-i"
 fi
 ORIG_BUILDFLAGS+=( $REBUILD_FLAG )
 
aa129b35
 BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
 # Test timeout.
acb42c5f
 
 if [ "${DOCKER_ENGINE_GOARCH}" == "arm" ]; then
11d3f709
 	: ${TIMEOUT:=10m}
6a1ae187
 elif [ "${DOCKER_ENGINE_GOARCH}" == "windows" ]; then
11d3f709
 	: ${TIMEOUT:=8m}
acb42c5f
 else
11d3f709
 	: ${TIMEOUT:=5m}
acb42c5f
 fi
 
be344cf0
 LDFLAGS_STATIC_DOCKER="
 	$LDFLAGS_STATIC
8845eb43
 	-extldflags \"$EXTLDFLAGS_STATIC\"
be344cf0
 "
 
18bea249
 if [ "$(uname -s)" = 'FreeBSD' ]; then
 	# Tell cgo the compiler is Clang, not GCC
 	# https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
 	export CC=clang
 
 	# "-extld clang" is a workaround for
 	# https://code.google.com/p/go/issues/detail?id=6845
 	LDFLAGS="$LDFLAGS -extld clang"
 fi
 
92ea101b
 # If sqlite3.h doesn't exist under /usr/include,
 # check /usr/local/include also just in case
 # (e.g. FreeBSD Ports installs it under the directory)
 if [ ! -e /usr/include/sqlite3.h ] && [ -e /usr/local/include/sqlite3.h ]; then
 	export CGO_CFLAGS='-I/usr/local/include'
 	export CGO_LDFLAGS='-L/usr/local/lib'
 fi
 
59dc2876
 HAVE_GO_TEST_COVER=
eddda577
 if \
ad80da33
 	go help testflag | grep -- -cover > /dev/null \
eddda577
 	&& go tool -n cover > /dev/null 2>&1 \
 ; then
59dc2876
 	HAVE_GO_TEST_COVER=1
 fi
 
dcfc4ada
 # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
 # You can use this to select certain tests to run, eg.
 #
987e2216
 #     TESTFLAGS='-test.run ^TestBuild$' ./hack/make.sh test-unit
 #
 # For integration-cli test, we use [gocheck](https://labix.org/gocheck), if you want
 # to run certain tests on your local host, you should run with command:
 #
 #     TESTFLAGS='-check.f DockerSuite.TestBuild*' ./hack/make.sh binary test-integration-cli
dcfc4ada
 #
 go_test_dir() {
 	dir=$1
b3d5e952
 	coverpkg=$2
59dc2876
 	testcover=()
477fe484
 	testcoverprofile=()
 	testbinary="$DEST/test.main"
59dc2876
 	if [ "$HAVE_GO_TEST_COVER" ]; then
 		# if our current go install has -cover, we want to use it :)
 		mkdir -p "$DEST/coverprofiles"
 		coverprofile="docker${dir#.}"
ac338836
 		coverprofile="$ABS_DEST/coverprofiles/${coverprofile//\//-}"
477fe484
 		testcover=( -test.cover )
 		testcoverprofile=( -test.coverprofile "$coverprofile" $coverpkg )
59dc2876
 	fi
dcfc4ada
 	(
3a14eb06
 		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
dcfc4ada
 		cd "$dir"
ac338836
 		export DEST="$ABS_DEST" # we're in a subshell, so this is safe -- our integration-cli tests need DEST, and "cd" screws it up
477fe484
 		go test -c -o "$testbinary" ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}"
 		i=0
 		while ((++i)); do
 			test_env "$testbinary" ${testcoverprofile[@]} $TESTFLAGS
 			if [ $i -gt "$TEST_REPEAT" ]; then
 				break
 			fi
 			echo "Repeating test ($i)"
 		done
dcfc4ada
 	)
 }
ed345fb1
 test_env() {
 	# use "env -i" to tightly control the environment variables that bleed into the tests
 	env -i \
 		DEST="$DEST" \
f4a1e3db
 		DOCKER_TLS_VERIFY="$DOCKER_TEST_TLS_VERIFY" \
 		DOCKER_CERT_PATH="$DOCKER_TEST_CERT_PATH" \
d12f4bab
 		DOCKER_ENGINE_GOARCH="$DOCKER_ENGINE_GOARCH" \
ed345fb1
 		DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \
44de5fec
 		DOCKER_USERLANDPROXY="$DOCKER_USERLANDPROXY" \
ed345fb1
 		DOCKER_HOST="$DOCKER_HOST" \
930b27a8
 		DOCKER_REMAP_ROOT="$DOCKER_REMAP_ROOT" \
ac120567
 		DOCKER_REMOTE_DAEMON="$DOCKER_REMOTE_DAEMON" \
ed345fb1
 		GOPATH="$GOPATH" \
11d3f709
 		GOTRACEBACK=all \
ac338836
 		HOME="$ABS_DEST/fake-HOME" \
ed345fb1
 		PATH="$PATH" \
fba44a7c
 		TEMP="$TEMP" \
ed345fb1
 		"$@"
 }
dcfc4ada
 
d43f0b9f
 # a helper to provide ".exe" when it's appropriate
 binary_extension() {
 	if [ "$(go env GOOS)" = 'windows' ]; then
 		echo -n '.exe'
 	fi
 }
 
6b46a091
 hash_files() {
 	while [ $# -gt 0 ]; do
 		f="$1"
 		shift
 		dir="$(dirname "$f")"
 		base="$(basename "$f")"
 		for hashAlgo in md5 sha256; do
 			if command -v "${hashAlgo}sum" &> /dev/null; then
 				(
 					# subshell and cd so that we get output files like:
 					#   $HASH docker-$VERSION
 					# instead of:
 					#   $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
 					cd "$dir"
 					"${hashAlgo}sum" "$base" > "$base.$hashAlgo"
 				)
 			fi
 		done
 	done
 }
 
3d39336a
 bundle() {
ac338836
 	local bundle="$1"; shift
 	echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
 	source "$SCRIPTDIR/make/$bundle" "$@"
89ee5242
 }
 
78568f2e
 copy_containerd() {
2164018d
 	dir="$1"
 	# Add nested executables to bundle dir so we have complete set of
 	# them available, but only if the native OS/ARCH is the same as the
 	# OS/ARCH of the build target
 	if [ "$(go env GOOS)/$(go env GOARCH)" == "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
 		if [ -x /usr/local/bin/docker-runc ]; then
 			echo "Copying nested executables into $dir"
 			for file in containerd containerd-shim containerd-ctr runc; do
45fb8033
 				cp `which "docker-$file"` "$dir/"
2164018d
 				if [ "$2" == "hash" ]; then
 					hash_files "$dir/docker-$file"
 				fi
 			done
009399dc
 		fi
2164018d
 	fi
78568f2e
 }
 
9bb54f89
 install_binary() {
 	file="$1"
 	target="${DOCKER_MAKE_INSTALL_PREFIX:=/usr/local}/bin/"
 	if [ "$(go env GOOS)" == "linux" ]; then
 		echo "Installing $(basename $file) to ${target}"
 		cp -L "$file" "$target"
 	else
 		echo "Install is only supported on linux"
 		return 1
 	fi
 }
 
 
89ee5242
 main() {
aa3de0b8
 	# We want this to fail if the bundles already exist and cannot be removed.
3d39336a
 	# This is to avoid mixing bundles from different versions of the code.
 	mkdir -p bundles
a15b67b1
 	if [ -e "bundles/$VERSION" ] && [ -z "$KEEPBUNDLE" ]; then
3d39336a
 		echo "bundles/$VERSION already exists. Removing."
ac20568b
 		rm -fr "bundles/$VERSION" && mkdir "bundles/$VERSION" || exit 1
aa3de0b8
 		echo
3d39336a
 	fi
d4275348
 
 	if [ "$(go env GOHOSTOS)" != 'windows' ]; then
 		# Windows and symlinks don't get along well
9e00e340
 
f228fb09
 		rm -f bundles/latest
 		ln -s "$VERSION" bundles/latest
d4275348
 	fi
 
3d39336a
 	if [ $# -lt 1 ]; then
85956c70
 		bundles=(${DEFAULT_BUNDLES[@]})
3d39336a
 	else
 		bundles=($@)
 	fi
 	for bundle in ${bundles[@]}; do
ac338836
 		export DEST="bundles/$VERSION/$(basename "$bundle")"
 		# Cygdrive paths don't play well with go build -o.
 		if [[ "$(uname -s)" == CYGWIN* ]]; then
 			export DEST="$(cygpath -mw "$DEST")"
 		fi
 		mkdir -p "$DEST"
 		ABS_DEST="$(cd "$DEST" && pwd -P)"
 		bundle "$bundle"
aa3de0b8
 		echo
3d39336a
 	done
89ee5242
 }
 
3d39336a
 main "$@"