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