github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/hack/make.sh (about)

     1  #!/usr/bin/env bash
     2  set -e
     3  
     4  # This script builds various binary artifacts from a checkout of the docker
     5  # source code.
     6  #
     7  # Requirements:
     8  # - The current directory should be a checkout of the docker source code
     9  #   (https://github.com/docker/docker). Whatever version is checked out
    10  #   will be built.
    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 -dirty 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.
    22  #
    23  
    24  set -o pipefail
    25  
    26  export DOCKER_PKG='github.com/docker/docker'
    27  export SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    28  export MAKEDIR="$SCRIPTDIR/make"
    29  
    30  # We're a nice, sexy, little shell script, and people might try to run us;
    31  # but really, they shouldn't. We want to be in a container!
    32  if [ "$PWD" != "/go/src/$DOCKER_PKG" ] || [ -z "$DOCKER_CROSSPLATFORMS" ]; then
    33  	{
    34  		echo "# WARNING! I don't seem to be running in the Docker container."
    35  		echo "# The result of this command might be an incorrect build, and will not be"
    36  		echo "#   officially supported."
    37  		echo "#"
    38  		echo "# Try this instead: make all"
    39  		echo "#"
    40  	} >&2
    41  fi
    42  
    43  echo
    44  
    45  # List of bundles to create when no argument is passed
    46  DEFAULT_BUNDLES=(
    47  	validate-dco
    48  	validate-gofmt
    49  	validate-lint
    50  	validate-pkg
    51  	validate-test
    52  	validate-toml
    53  	validate-vet
    54  
    55  	binary
    56  	dynbinary
    57  
    58  	test-unit
    59  	test-integration-cli
    60  	test-docker-py
    61  
    62  	cover
    63  	cross
    64  	tgz
    65  )
    66  
    67  VERSION=$(< ./VERSION)
    68  if command -v git &> /dev/null && git rev-parse &> /dev/null; then
    69  	GITCOMMIT=$(git rev-parse --short HEAD)
    70  	if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
    71  		GITCOMMIT="$GITCOMMIT-dirty"
    72  	fi
    73  	! BUILDTIME=$(date --rfc-3339 ns | sed -e 's/ /T/') &> /dev/null
    74  	if [ -z $BUILDTIME ]; then
    75  		# If using bash 3.1 which doesn't support --rfc-3389, eg Windows CI
    76  		BUILDTIME=$(date -u)
    77  	fi
    78  elif [ "$DOCKER_GITCOMMIT" ]; then
    79  	GITCOMMIT="$DOCKER_GITCOMMIT"
    80  else
    81  	echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
    82  	echo >&2 '  Please either build with the .git directory accessible, or specify the'
    83  	echo >&2 '  exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
    84  	echo >&2 '  future accountability in diagnosing build issues.  Thanks!'
    85  	exit 1
    86  fi
    87  
    88  if [ "$AUTO_GOPATH" ]; then
    89  	rm -rf .gopath
    90  	mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
    91  	ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
    92  	export GOPATH="${PWD}/.gopath:${PWD}/vendor"
    93  fi
    94  
    95  if [ ! "$GOPATH" ]; then
    96  	echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH'
    97  	echo >&2 '  alternatively, set AUTO_GOPATH=1'
    98  	exit 1
    99  fi
   100  
   101  if [ "$DOCKER_EXPERIMENTAL" ] || [ "$DOCKER_REMAP_ROOT" ]; then
   102  	echo >&2 '# WARNING! DOCKER_EXPERIMENTAL is set: building experimental features'
   103  	echo >&2
   104  	DOCKER_BUILDTAGS+=" experimental pkcs11"
   105  fi
   106  
   107  if [ -z "$DOCKER_CLIENTONLY" ]; then
   108  	DOCKER_BUILDTAGS+=" daemon"
   109  	if pkg-config libsystemd-journal 2> /dev/null ; then
   110  		DOCKER_BUILDTAGS+=" journald"
   111  	fi
   112  fi
   113  
   114  # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately
   115  if \
   116  	command -v gcc &> /dev/null \
   117  	&& ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \
   118  ; then
   119  	DOCKER_BUILDTAGS+=' btrfs_noversion'
   120  fi
   121  
   122  # test whether "libdevmapper.h" is new enough to support deferred remove
   123  # functionality.
   124  if \
   125  	command -v gcc &> /dev/null \
   126  	&& ! ( echo -e  '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -ldevmapper -xc - -o /dev/null &> /dev/null ) \
   127  ; then
   128         DOCKER_BUILDTAGS+=' libdm_no_deferred_remove'
   129  fi
   130  
   131  # Use these flags when compiling the tests and final binary
   132  
   133  IAMSTATIC='true'
   134  source "$SCRIPTDIR/make/.go-autogen"
   135  if [ -z "$DOCKER_DEBUG" ]; then
   136  	LDFLAGS='-w'
   137  fi
   138  
   139  LDFLAGS_STATIC=''
   140  EXTLDFLAGS_STATIC='-static'
   141  # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
   142  # with options like -race.
   143  ORIG_BUILDFLAGS=( -a -tags "autogen netgo static_build sqlite_omit_load_extension $DOCKER_BUILDTAGS" -installsuffix netgo )
   144  # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
   145  BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
   146  # Test timeout.
   147  : ${TIMEOUT:=120m}
   148  TESTFLAGS+=" -test.timeout=${TIMEOUT}"
   149  
   150  LDFLAGS_STATIC_DOCKER="
   151  	$LDFLAGS_STATIC
   152  	-extldflags \"$EXTLDFLAGS_STATIC\"
   153  "
   154  
   155  if [ "$(uname -s)" = 'FreeBSD' ]; then
   156  	# Tell cgo the compiler is Clang, not GCC
   157  	# https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
   158  	export CC=clang
   159  
   160  	# "-extld clang" is a workaround for
   161  	# https://code.google.com/p/go/issues/detail?id=6845
   162  	LDFLAGS="$LDFLAGS -extld clang"
   163  fi
   164  
   165  # If sqlite3.h doesn't exist under /usr/include,
   166  # check /usr/local/include also just in case
   167  # (e.g. FreeBSD Ports installs it under the directory)
   168  if [ ! -e /usr/include/sqlite3.h ] && [ -e /usr/local/include/sqlite3.h ]; then
   169  	export CGO_CFLAGS='-I/usr/local/include'
   170  	export CGO_LDFLAGS='-L/usr/local/lib'
   171  fi
   172  
   173  HAVE_GO_TEST_COVER=
   174  if \
   175  	go help testflag | grep -- -cover > /dev/null \
   176  	&& go tool -n cover > /dev/null 2>&1 \
   177  ; then
   178  	HAVE_GO_TEST_COVER=1
   179  fi
   180  
   181  # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
   182  # You can use this to select certain tests to run, eg.
   183  #
   184  #     TESTFLAGS='-test.run ^TestBuild$' ./hack/make.sh test-unit
   185  #
   186  # For integration-cli test, we use [gocheck](https://labix.org/gocheck), if you want
   187  # to run certain tests on your local host, you should run with command:
   188  #
   189  #     TESTFLAGS='-check.f DockerSuite.TestBuild*' ./hack/make.sh binary test-integration-cli
   190  #
   191  go_test_dir() {
   192  	dir=$1
   193  	coverpkg=$2
   194  	testcover=()
   195  	if [ "$HAVE_GO_TEST_COVER" ]; then
   196  		# if our current go install has -cover, we want to use it :)
   197  		mkdir -p "$DEST/coverprofiles"
   198  		coverprofile="docker${dir#.}"
   199  		coverprofile="$ABS_DEST/coverprofiles/${coverprofile//\//-}"
   200  		testcover=( -cover -coverprofile "$coverprofile" $coverpkg )
   201  	fi
   202  	(
   203  		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
   204  		cd "$dir"
   205  		export DEST="$ABS_DEST" # we're in a subshell, so this is safe -- our integration-cli tests need DEST, and "cd" screws it up
   206  		test_env go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS
   207  	)
   208  }
   209  test_env() {
   210  	# use "env -i" to tightly control the environment variables that bleed into the tests
   211  	env -i \
   212  		DEST="$DEST" \
   213  		DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \
   214  		DOCKER_USERLANDPROXY="$DOCKER_USERLANDPROXY" \
   215  		DOCKER_HOST="$DOCKER_HOST" \
   216  		DOCKER_REMAP_ROOT="$DOCKER_REMAP_ROOT" \
   217  		DOCKER_REMOTE_DAEMON="$DOCKER_REMOTE_DAEMON" \
   218  		GOPATH="$GOPATH" \
   219  		HOME="$ABS_DEST/fake-HOME" \
   220  		PATH="$PATH" \
   221  		TEMP="$TEMP" \
   222  		TEST_DOCKERINIT_PATH="$TEST_DOCKERINIT_PATH" \
   223  		"$@"
   224  }
   225  
   226  # a helper to provide ".exe" when it's appropriate
   227  binary_extension() {
   228  	if [ "$(go env GOOS)" = 'windows' ]; then
   229  		echo -n '.exe'
   230  	fi
   231  }
   232  
   233  hash_files() {
   234  	while [ $# -gt 0 ]; do
   235  		f="$1"
   236  		shift
   237  		dir="$(dirname "$f")"
   238  		base="$(basename "$f")"
   239  		for hashAlgo in md5 sha256; do
   240  			if command -v "${hashAlgo}sum" &> /dev/null; then
   241  				(
   242  					# subshell and cd so that we get output files like:
   243  					#   $HASH docker-$VERSION
   244  					# instead of:
   245  					#   $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
   246  					cd "$dir"
   247  					"${hashAlgo}sum" "$base" > "$base.$hashAlgo"
   248  				)
   249  			fi
   250  		done
   251  	done
   252  }
   253  
   254  bundle() {
   255  	local bundle="$1"; shift
   256  	echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
   257  	source "$SCRIPTDIR/make/$bundle" "$@"
   258  }
   259  
   260  main() {
   261  	# We want this to fail if the bundles already exist and cannot be removed.
   262  	# This is to avoid mixing bundles from different versions of the code.
   263  	mkdir -p bundles
   264  	if [ -e "bundles/$VERSION" ] && [ -z "$KEEPBUNDLE" ]; then
   265  		echo "bundles/$VERSION already exists. Removing."
   266  		rm -fr "bundles/$VERSION" && mkdir "bundles/$VERSION" || exit 1
   267  		echo
   268  	fi
   269  
   270  	if [ "$(go env GOHOSTOS)" != 'windows' ]; then
   271  		# Windows and symlinks don't get along well
   272  
   273  		rm -f bundles/latest
   274  		ln -s "$VERSION" bundles/latest
   275  	fi
   276  
   277  	if [ $# -lt 1 ]; then
   278  		bundles=(${DEFAULT_BUNDLES[@]})
   279  	else
   280  		bundles=($@)
   281  	fi
   282  	for bundle in ${bundles[@]}; do
   283  		export DEST="bundles/$VERSION/$(basename "$bundle")"
   284  		# Cygdrive paths don't play well with go build -o.
   285  		if [[ "$(uname -s)" == CYGWIN* ]]; then
   286  			export DEST="$(cygpath -mw "$DEST")"
   287  		fi
   288  		mkdir -p "$DEST"
   289  		ABS_DEST="$(cd "$DEST" && pwd -P)"
   290  		bundle "$bundle"
   291  		echo
   292  	done
   293  }
   294  
   295  main "$@"