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