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