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