github.com/endocode/docker@v1.4.2-0.20160113120958-46eb4700391e/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 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-lint 50 validate-pkg 51 validate-test 52 validate-toml 53 validate-vet 54 validate-vendor 55 56 binary 57 dynbinary 58 59 test-unit 60 test-integration-cli 61 test-docker-py 62 63 cover 64 cross 65 tgz 66 ) 67 68 VERSION=$(< ./VERSION) 69 if command -v git &> /dev/null && git rev-parse &> /dev/null; then 70 GITCOMMIT=$(git rev-parse --short HEAD) 71 if [ -n "$(git status --porcelain --untracked-files=no)" ]; then 72 GITCOMMIT="$GITCOMMIT-unsupported" 73 fi 74 ! BUILDTIME=$(date --rfc-3339 ns | sed -e 's/ /T/') &> /dev/null 75 if [ -z $BUILDTIME ]; then 76 # If using bash 3.1 which doesn't support --rfc-3389, eg Windows CI 77 BUILDTIME=$(date -u) 78 fi 79 elif [ "$DOCKER_GITCOMMIT" ]; then 80 GITCOMMIT="$DOCKER_GITCOMMIT" 81 else 82 echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified' 83 echo >&2 ' Please either build with the .git directory accessible, or specify the' 84 echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for' 85 echo >&2 ' future accountability in diagnosing build issues. Thanks!' 86 exit 1 87 fi 88 89 if [ "$AUTO_GOPATH" ]; then 90 rm -rf .gopath 91 mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")" 92 ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}" 93 export GOPATH="${PWD}/.gopath:${PWD}/vendor" 94 fi 95 96 if [ ! "$GOPATH" ]; then 97 echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH' 98 echo >&2 ' alternatively, set AUTO_GOPATH=1' 99 exit 1 100 fi 101 102 if [ "$DOCKER_EXPERIMENTAL" ]; then 103 echo >&2 '# WARNING! DOCKER_EXPERIMENTAL is set: building experimental features' 104 echo >&2 105 DOCKER_BUILDTAGS+=" experimental pkcs11" 106 fi 107 108 if [ -z "$DOCKER_CLIENTONLY" ]; then 109 DOCKER_BUILDTAGS+=" daemon" 110 if pkg-config libsystemd-journal 2> /dev/null ; then 111 DOCKER_BUILDTAGS+=" journald" 112 fi 113 fi 114 115 # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately 116 if \ 117 command -v gcc &> /dev/null \ 118 && ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \ 119 ; then 120 DOCKER_BUILDTAGS+=' btrfs_noversion' 121 fi 122 123 # test whether "libdevmapper.h" is new enough to support deferred remove 124 # functionality. 125 if \ 126 command -v gcc &> /dev/null \ 127 && ! ( echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -ldevmapper -xc - -o /dev/null &> /dev/null ) \ 128 ; then 129 DOCKER_BUILDTAGS+=' libdm_no_deferred_remove' 130 fi 131 132 # Use these flags when compiling the tests and final binary 133 134 IAMSTATIC='true' 135 source "$SCRIPTDIR/make/.go-autogen" 136 if [ -z "$DOCKER_DEBUG" ]; then 137 LDFLAGS='-w' 138 fi 139 140 LDFLAGS_STATIC='' 141 EXTLDFLAGS_STATIC='-static' 142 # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build 143 # with options like -race. 144 ORIG_BUILDFLAGS=( -a -tags "autogen netgo static_build sqlite_omit_load_extension $DOCKER_BUILDTAGS" -installsuffix netgo ) 145 # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here 146 BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" ) 147 # Test timeout. 148 : ${TIMEOUT:=120m} 149 TESTFLAGS+=" -test.timeout=${TIMEOUT}" 150 151 LDFLAGS_STATIC_DOCKER=" 152 $LDFLAGS_STATIC 153 -extldflags \"$EXTLDFLAGS_STATIC\" 154 " 155 156 if [ "$(uname -s)" = 'FreeBSD' ]; then 157 # Tell cgo the compiler is Clang, not GCC 158 # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752 159 export CC=clang 160 161 # "-extld clang" is a workaround for 162 # https://code.google.com/p/go/issues/detail?id=6845 163 LDFLAGS="$LDFLAGS -extld clang" 164 fi 165 166 # If sqlite3.h doesn't exist under /usr/include, 167 # check /usr/local/include also just in case 168 # (e.g. FreeBSD Ports installs it under the directory) 169 if [ ! -e /usr/include/sqlite3.h ] && [ -e /usr/local/include/sqlite3.h ]; then 170 export CGO_CFLAGS='-I/usr/local/include' 171 export CGO_LDFLAGS='-L/usr/local/lib' 172 fi 173 174 HAVE_GO_TEST_COVER= 175 if \ 176 go help testflag | grep -- -cover > /dev/null \ 177 && go tool -n cover > /dev/null 2>&1 \ 178 ; then 179 HAVE_GO_TEST_COVER=1 180 fi 181 182 # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'. 183 # You can use this to select certain tests to run, eg. 184 # 185 # TESTFLAGS='-test.run ^TestBuild$' ./hack/make.sh test-unit 186 # 187 # For integration-cli test, we use [gocheck](https://labix.org/gocheck), if you want 188 # to run certain tests on your local host, you should run with command: 189 # 190 # TESTFLAGS='-check.f DockerSuite.TestBuild*' ./hack/make.sh binary test-integration-cli 191 # 192 go_test_dir() { 193 dir=$1 194 coverpkg=$2 195 testcover=() 196 if [ "$HAVE_GO_TEST_COVER" ]; then 197 # if our current go install has -cover, we want to use it :) 198 mkdir -p "$DEST/coverprofiles" 199 coverprofile="docker${dir#.}" 200 coverprofile="$ABS_DEST/coverprofiles/${coverprofile//\//-}" 201 testcover=( -cover -coverprofile "$coverprofile" $coverpkg ) 202 fi 203 ( 204 echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}" 205 cd "$dir" 206 export DEST="$ABS_DEST" # we're in a subshell, so this is safe -- our integration-cli tests need DEST, and "cd" screws it up 207 test_env go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS 208 ) 209 } 210 test_env() { 211 # use "env -i" to tightly control the environment variables that bleed into the tests 212 env -i \ 213 DEST="$DEST" \ 214 DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \ 215 DOCKER_USERLANDPROXY="$DOCKER_USERLANDPROXY" \ 216 DOCKER_HOST="$DOCKER_HOST" \ 217 DOCKER_REMAP_ROOT="$DOCKER_REMAP_ROOT" \ 218 DOCKER_REMOTE_DAEMON="$DOCKER_REMOTE_DAEMON" \ 219 GOPATH="$GOPATH" \ 220 HOME="$ABS_DEST/fake-HOME" \ 221 PATH="$PATH" \ 222 TEMP="$TEMP" \ 223 TEST_DOCKERINIT_PATH="$TEST_DOCKERINIT_PATH" \ 224 "$@" 225 } 226 227 # a helper to provide ".exe" when it's appropriate 228 binary_extension() { 229 if [ "$(go env GOOS)" = 'windows' ]; then 230 echo -n '.exe' 231 fi 232 } 233 234 hash_files() { 235 while [ $# -gt 0 ]; do 236 f="$1" 237 shift 238 dir="$(dirname "$f")" 239 base="$(basename "$f")" 240 for hashAlgo in md5 sha256; do 241 if command -v "${hashAlgo}sum" &> /dev/null; then 242 ( 243 # subshell and cd so that we get output files like: 244 # $HASH docker-$VERSION 245 # instead of: 246 # $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION 247 cd "$dir" 248 "${hashAlgo}sum" "$base" > "$base.$hashAlgo" 249 ) 250 fi 251 done 252 done 253 } 254 255 bundle() { 256 local bundle="$1"; shift 257 echo "---> Making bundle: $(basename "$bundle") (in $DEST)" 258 source "$SCRIPTDIR/make/$bundle" "$@" 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 "$@"