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