github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/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  # We're a nice, sexy, little shell script, and people might try to run us;
    32  # but really, they shouldn't. We want to be in a container!
    33  inContainer="AssumeSoInitially"
    34  if [ "$(go env GOHOSTOS)" = 'windows' ]; then
    35  	if [ -z "$FROM_DOCKERFILE" ]; then
    36  		unset inContainer
    37  	fi
    38  else
    39  	if [ "$PWD" != "/go/src/$DOCKER_PKG" ]; then
    40  		unset inContainer
    41  	fi
    42  fi
    43  
    44  if [ -z "$inContainer" ]; then
    45  	{
    46  		echo "# WARNING! I don't seem to be running in a Docker container."
    47  		echo "# The result of this command might be an incorrect build, and will not be"
    48  		echo "# officially supported."
    49  		echo "#"
    50  		echo "# Try this instead: make all"
    51  		echo "#"
    52  	} >&2
    53  fi
    54  
    55  echo
    56  
    57  # List of bundles to create when no argument is passed
    58  DEFAULT_BUNDLES=(
    59  	binary-daemon
    60  	dynbinary
    61  
    62  	test-unit
    63  	test-integration-cli
    64  	test-docker-py
    65  
    66  	cross
    67  	tgz
    68  )
    69  
    70  VERSION=$(< ./VERSION)
    71  ! BUILDTIME=$(date --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')
    72  if [ "$DOCKER_GITCOMMIT" ]; then
    73  	GITCOMMIT="$DOCKER_GITCOMMIT"
    74  elif command -v git &> /dev/null && [ -d .git ] && git rev-parse &> /dev/null; then
    75  	GITCOMMIT=$(git rev-parse --short HEAD)
    76  	if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
    77  		GITCOMMIT="$GITCOMMIT-unsupported"
    78  		echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    79  		echo "# GITCOMMIT = $GITCOMMIT"
    80  		echo "# The version you are building is listed as unsupported because"
    81  		echo "# there are some files in the git repository that are in an uncommitted state."
    82  		echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
    83  		echo "# Here is the current list:"
    84  		git status --porcelain --untracked-files=no
    85  		echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    86  	fi
    87  else
    88  	echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
    89  	echo >&2 '  Please either build with the .git directory accessible, or specify the'
    90  	echo >&2 '  exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
    91  	echo >&2 '  future accountability in diagnosing build issues.  Thanks!'
    92  	exit 1
    93  fi
    94  
    95  if [ "$AUTO_GOPATH" ]; then
    96  	rm -rf .gopath
    97  	mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
    98  	ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
    99  	export GOPATH="${PWD}/.gopath"
   100  
   101  	if [ "$(go env GOOS)" = 'solaris' ]; then
   102  		# sys/unix is installed outside the standard library on solaris
   103  		# TODO need to allow for version change, need to get version from go
   104  		export GO_VERSION=${GO_VERSION:-"1.8.1"}
   105  		export GOPATH="${GOPATH}:/usr/lib/gocode/${GO_VERSION}"
   106  	fi
   107  fi
   108  
   109  if [ ! "$GOPATH" ]; then
   110  	echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH'
   111  	echo >&2 '  alternatively, set AUTO_GOPATH=1'
   112  	exit 1
   113  fi
   114  
   115  DOCKER_BUILDTAGS+=" daemon"
   116  if ${PKG_CONFIG} 'libsystemd >= 209' 2> /dev/null ; then
   117  	DOCKER_BUILDTAGS+=" journald"
   118  elif ${PKG_CONFIG} 'libsystemd-journal' 2> /dev/null ; then
   119  	DOCKER_BUILDTAGS+=" journald journald_compat"
   120  fi
   121  
   122  # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately
   123  if \
   124  	command -v gcc &> /dev/null \
   125  	&& ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \
   126  ; then
   127  	DOCKER_BUILDTAGS+=' btrfs_noversion'
   128  fi
   129  
   130  # test whether "libdevmapper.h" is new enough to support deferred remove
   131  # functionality.
   132  if \
   133  	command -v gcc &> /dev/null \
   134  	&& ! ( echo -e  '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -xc - -o /dev/null -ldevmapper &> /dev/null ) \
   135  ; then
   136         DOCKER_BUILDTAGS+=' libdm_no_deferred_remove'
   137  fi
   138  
   139  # Use these flags when compiling the tests and final binary
   140  
   141  IAMSTATIC='true'
   142  source "$SCRIPTDIR/make/.go-autogen"
   143  if [ -z "$DOCKER_DEBUG" ]; then
   144  	LDFLAGS='-w'
   145  fi
   146  
   147  LDFLAGS_STATIC=''
   148  EXTLDFLAGS_STATIC='-static'
   149  # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
   150  # with options like -race.
   151  ORIG_BUILDFLAGS=( -tags "autogen netgo static_build $DOCKER_BUILDTAGS" -installsuffix netgo )
   152  # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
   153  
   154  # When $DOCKER_INCREMENTAL_BINARY is set in the environment, enable incremental
   155  # builds by installing dependent packages to the GOPATH.
   156  REBUILD_FLAG="-a"
   157  if [ "$DOCKER_INCREMENTAL_BINARY" == "1" ] || [ "$DOCKER_INCREMENTAL_BINARY" == "true" ]; then
   158  	REBUILD_FLAG="-i"
   159  fi
   160  ORIG_BUILDFLAGS+=( $REBUILD_FLAG )
   161  
   162  BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
   163  # Test timeout.
   164  
   165  if [ "${DOCKER_ENGINE_GOARCH}" == "arm" ]; then
   166  	: ${TIMEOUT:=10m}
   167  elif [ "${DOCKER_ENGINE_GOARCH}" == "windows" ]; then
   168  	: ${TIMEOUT:=8m}
   169  else
   170  	: ${TIMEOUT:=5m}
   171  fi
   172  
   173  LDFLAGS_STATIC_DOCKER="
   174  	$LDFLAGS_STATIC
   175  	-extldflags \"$EXTLDFLAGS_STATIC\"
   176  "
   177  
   178  if [ "$(uname -s)" = 'FreeBSD' ]; then
   179  	# Tell cgo the compiler is Clang, not GCC
   180  	# https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
   181  	export CC=clang
   182  
   183  	# "-extld clang" is a workaround for
   184  	# https://code.google.com/p/go/issues/detail?id=6845
   185  	LDFLAGS="$LDFLAGS -extld clang"
   186  fi
   187  
   188  HAVE_GO_TEST_COVER=
   189  if \
   190  	go help testflag | grep -- -cover > /dev/null \
   191  	&& go tool -n cover > /dev/null 2>&1 \
   192  ; then
   193  	HAVE_GO_TEST_COVER=1
   194  fi
   195  
   196  # a helper to provide ".exe" when it's appropriate
   197  binary_extension() {
   198  	if [ "$(go env GOOS)" = 'windows' ]; then
   199  		echo -n '.exe'
   200  	fi
   201  }
   202  
   203  hash_files() {
   204  	while [ $# -gt 0 ]; do
   205  		f="$1"
   206  		shift
   207  		dir="$(dirname "$f")"
   208  		base="$(basename "$f")"
   209  		for hashAlgo in md5 sha256; do
   210  			if command -v "${hashAlgo}sum" &> /dev/null; then
   211  				(
   212  					# subshell and cd so that we get output files like:
   213  					#   $HASH docker-$VERSION
   214  					# instead of:
   215  					#   $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
   216  					cd "$dir"
   217  					"${hashAlgo}sum" "$base" > "$base.$hashAlgo"
   218  				)
   219  			fi
   220  		done
   221  	done
   222  }
   223  
   224  bundle() {
   225  	local bundle="$1"; shift
   226  	echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
   227  	source "$SCRIPTDIR/make/$bundle" "$@"
   228  }
   229  
   230  copy_binaries() {
   231  	dir="$1"
   232  	# Add nested executables to bundle dir so we have complete set of
   233  	# them available, but only if the native OS/ARCH is the same as the
   234  	# OS/ARCH of the build target
   235  	if [ "$(go env GOOS)/$(go env GOARCH)" == "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
   236  		if [ -x /usr/local/bin/docker-runc ]; then
   237  			echo "Copying nested executables into $dir"
   238  			for file in containerd containerd-shim containerd-ctr runc init proxy; do
   239  				cp -f `which "docker-$file"` "$dir/"
   240  				if [ "$2" == "hash" ]; then
   241  					hash_files "$dir/docker-$file"
   242  				fi
   243  			done
   244  		fi
   245  	fi
   246  }
   247  
   248  install_binary() {
   249  	file="$1"
   250  	target="${DOCKER_MAKE_INSTALL_PREFIX:=/usr/local}/bin/"
   251  	if [ "$(go env GOOS)" == "linux" ]; then
   252  		echo "Installing $(basename $file) to ${target}"
   253  		mkdir -p "$target"
   254  		cp -f -L "$file" "$target"
   255  	else
   256  		echo "Install is only supported on linux"
   257  		return 1
   258  	fi
   259  }
   260  
   261  main() {
   262  	# We want this to fail if the bundles already exist and cannot be removed.
   263  	# This is to avoid mixing bundles from different versions of the code.
   264  	mkdir -p bundles
   265  	if [ -e "bundles/$VERSION" ] && [ -z "$KEEPBUNDLE" ]; then
   266  		echo "bundles/$VERSION already exists. Removing."
   267  		rm -fr "bundles/$VERSION" && mkdir "bundles/$VERSION" || exit 1
   268  		echo
   269  	fi
   270  
   271  	if [ "$(go env GOHOSTOS)" != 'windows' ]; then
   272  		# Windows and symlinks don't get along well
   273  
   274  		rm -f bundles/latest
   275  		ln -s "$VERSION" bundles/latest
   276  	fi
   277  
   278  	if [ $# -lt 1 ]; then
   279  		bundles=(${DEFAULT_BUNDLES[@]})
   280  	else
   281  		bundles=($@)
   282  	fi
   283  	for bundle in ${bundles[@]}; do
   284  		export DEST="bundles/$VERSION/$(basename "$bundle")"
   285  		# Cygdrive paths don't play well with go build -o.
   286  		if [[ "$(uname -s)" == CYGWIN* ]]; then
   287  			export DEST="$(cygpath -mw "$DEST")"
   288  		fi
   289  		mkdir -p "$DEST"
   290  		ABS_DEST="$(cd "$DEST" && pwd -P)"
   291  		bundle "$bundle"
   292  		echo
   293  	done
   294  }
   295  
   296  main "$@"