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