hack/make.sh
3d39336a
 #!/bin/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
 #   (http://github.com/dotcloud/docker). Whatever version is checked out
 #   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,
a8059059
 #   with the suffix -dirty if the repository isn't clean.
d14058bc
 # - The script is intented 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
 #   your checkout of the Docker repository. 
 #   the Makefile will so a "docker build -t docker ." and then
aa3de0b8
 #   "docker run hack/make.sh" in the resulting container image.
 #
89ee5242
 
83a2e92d
 set -o pipefail
89ee5242
 
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!
 RESOLVCONF=$(readlink --canonicalize /etc/resolv.conf)
 grep -q "$RESOLVCONF" /proc/mounts || {
ccefe478
 	echo >&2 "# WARNING! I don't seem to be running in a docker container."
 	echo >&2 "# The result of this command might be an incorrect build, and will not be officially supported."
83d81758
 	echo >&2 "# Try this: 'make all'"
749a7d0e
 }
 
3d39336a
 # List of bundles to create when no argument is passed
 DEFAULT_BUNDLES=(
 	binary
85956c70
 	test
f0879a1e
 	test-integration
21161dbd
 	dynbinary
 	dyntest
f0879a1e
 	dyntest-integration
59dc2876
 	cover
62a81370
 	cross
b3f5973f
 	tgz
3d39336a
 	ubuntu
 )
 
d9f76993
 VERSION=$(cat ./VERSION)
efd0e13c
 if [ -d .git ] && command -v git &> /dev/null; then
 	GITCOMMIT=$(git rev-parse --short HEAD)
 	if [ -n "$(git status --porcelain)" ]; then
 		GITCOMMIT="$GITCOMMIT-dirty"
 	fi
 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
 
b187cc40
 # Use these flags when compiling the tests and final binary
d7897508
 LDFLAGS='-X main.GITCOMMIT "'$GITCOMMIT'" -X main.VERSION "'$VERSION'" -w'
21161dbd
 LDFLAGS_STATIC='-X github.com/dotcloud/docker/utils.IAMSTATIC true -linkmode external -extldflags "-lpthread -static -Wl,--unresolved-symbols=ignore-in-object-files"'
fbac8125
 BUILDFLAGS='-tags netgo -a'
b187cc40
 
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.
 #
 #   TESTFLAGS='-run ^TestBuild$' ./hack/make.sh test
 #
 go_test_dir() {
 	dir=$1
59dc2876
 	testcover=()
 	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#.}"
 		coverprofile="$DEST/coverprofiles/${coverprofile//\//-}"
 		testcover=( -cover -coverprofile "$coverprofile" )
 	fi
dcfc4ada
 	(
 		set -x
 		cd "$dir"
59dc2876
 		go test ${testcover[@]} -ldflags "$LDFLAGS" $BUILDFLAGS $TESTFLAGS
dcfc4ada
 	)
 }
 
3d39336a
 bundle() {
 	bundlescript=$1
 	bundle=$(basename $bundlescript)
aa3de0b8
 	echo "---> Making bundle: $bundle (in bundles/$VERSION/$bundle)"
3d39336a
 	mkdir -p bundles/$VERSION/$bundle
 	source $bundlescript $(pwd)/bundles/$VERSION/$bundle
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
 	if [ -e "bundles/$VERSION" ]; then
 		echo "bundles/$VERSION already exists. Removing."
 		rm -fr bundles/$VERSION && mkdir bundles/$VERSION || exit 1
aa3de0b8
 		echo
3d39336a
 	fi
 	SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 	if [ $# -lt 1 ]; then
85956c70
 		bundles=(${DEFAULT_BUNDLES[@]})
3d39336a
 	else
 		bundles=($@)
 	fi
 	for bundle in ${bundles[@]}; do
 		bundle $SCRIPTDIR/make/$bundle
aa3de0b8
 		echo
3d39336a
 	done
89ee5242
 }
 
3d39336a
 main "$@"