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