github.com/damirazo/docker@v1.9.0/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-lint 50 validate-pkg 51 validate-test 52 validate-toml 53 validate-vet 54 55 binary 56 57 test-unit 58 test-integration-cli 59 test-docker-py 60 61 dynbinary 62 63 cover 64 cross 65 tgz 66 ubuntu 67 ) 68 69 VERSION=$(< ./VERSION) 70 if command -v git &> /dev/null && git rev-parse &> /dev/null; then 71 GITCOMMIT=$(git rev-parse --short HEAD) 72 if [ -n "$(git status --porcelain --untracked-files=no)" ]; then 73 GITCOMMIT="$GITCOMMIT-dirty" 74 fi 75 BUILDTIME=$(date -u) 76 elif [ "$DOCKER_GITCOMMIT" ]; then 77 GITCOMMIT="$DOCKER_GITCOMMIT" 78 else 79 echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified' 80 echo >&2 ' Please either build with the .git directory accessible, or specify the' 81 echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for' 82 echo >&2 ' future accountability in diagnosing build issues. Thanks!' 83 exit 1 84 fi 85 86 if [ "$AUTO_GOPATH" ]; then 87 rm -rf .gopath 88 mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")" 89 ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}" 90 export GOPATH="${PWD}/.gopath:${PWD}/vendor" 91 fi 92 93 if [ ! "$GOPATH" ]; then 94 echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH' 95 echo >&2 ' alternatively, set AUTO_GOPATH=1' 96 exit 1 97 fi 98 99 if [ "$DOCKER_EXPERIMENTAL" ] || [ "$DOCKER_REMAP_ROOT" ]; then 100 echo >&2 '# WARNING! DOCKER_EXPERIMENTAL is set: building experimental features' 101 echo >&2 102 DOCKER_BUILDTAGS+=" experimental" 103 fi 104 105 if [ -z "$DOCKER_CLIENTONLY" ]; then 106 DOCKER_BUILDTAGS+=" daemon" 107 if pkg-config libsystemd-journal 2> /dev/null ; then 108 DOCKER_BUILDTAGS+=" journald" 109 fi 110 fi 111 112 if [ "$DOCKER_EXECDRIVER" = 'lxc' ]; then 113 DOCKER_BUILDTAGS+=' test_no_exec' 114 fi 115 116 # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately 117 if \ 118 command -v gcc &> /dev/null \ 119 && ! gcc -E - &> /dev/null <<<'#include <btrfs/version.h>' \ 120 ; then 121 DOCKER_BUILDTAGS+=' btrfs_noversion' 122 fi 123 124 # test whether "libdevmapper.h" is new enough to support deferred remove 125 # functionality. 126 if \ 127 command -v gcc &> /dev/null \ 128 && ! ( echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -ldevmapper -xc - &> /dev/null ) \ 129 ; then 130 DOCKER_BUILDTAGS+=' libdm_no_deferred_remove' 131 fi 132 133 # Use these flags when compiling the tests and final binary 134 135 IAMSTATIC='true' 136 source "$SCRIPTDIR/make/.go-autogen" 137 if [ -z "$DOCKER_DEBUG" ]; then 138 LDFLAGS='-w' 139 fi 140 141 LDFLAGS_STATIC='-linkmode external' 142 # Cgo -H windows is incompatible with -linkmode external. 143 if [ "$(go env GOOS)" == 'windows' ]; then 144 LDFLAGS_STATIC='' 145 fi 146 EXTLDFLAGS_STATIC='-static' 147 # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build 148 # with options like -race. 149 ORIG_BUILDFLAGS=( -a -tags "netgo static_build sqlite_omit_load_extension $DOCKER_BUILDTAGS" -installsuffix netgo ) 150 # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here 151 BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" ) 152 # Test timeout. 153 : ${TIMEOUT:=60m} 154 TESTFLAGS+=" -test.timeout=${TIMEOUT}" 155 156 LDFLAGS_STATIC_DOCKER=" 157 $LDFLAGS_STATIC 158 -extldflags \"$EXTLDFLAGS_STATIC\" 159 " 160 161 if [ "$(uname -s)" = 'FreeBSD' ]; then 162 # Tell cgo the compiler is Clang, not GCC 163 # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752 164 export CC=clang 165 166 # "-extld clang" is a workaround for 167 # https://code.google.com/p/go/issues/detail?id=6845 168 LDFLAGS="$LDFLAGS -extld clang" 169 fi 170 171 # If sqlite3.h doesn't exist under /usr/include, 172 # check /usr/local/include also just in case 173 # (e.g. FreeBSD Ports installs it under the directory) 174 if [ ! -e /usr/include/sqlite3.h ] && [ -e /usr/local/include/sqlite3.h ]; then 175 export CGO_CFLAGS='-I/usr/local/include' 176 export CGO_LDFLAGS='-L/usr/local/lib' 177 fi 178 179 HAVE_GO_TEST_COVER= 180 if \ 181 go help testflag | grep -- -cover > /dev/null \ 182 && go tool -n cover > /dev/null 2>&1 \ 183 ; then 184 HAVE_GO_TEST_COVER=1 185 fi 186 187 # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'. 188 # You can use this to select certain tests to run, eg. 189 # 190 # TESTFLAGS='-test.run ^TestBuild$' ./hack/make.sh test-unit 191 # 192 # For integration-cli test, we use [gocheck](https://labix.org/gocheck), if you want 193 # to run certain tests on your local host, you should run with command: 194 # 195 # TESTFLAGS='-check.f DockerSuite.TestBuild*' ./hack/make.sh binary test-integration-cli 196 # 197 go_test_dir() { 198 dir=$1 199 coverpkg=$2 200 testcover=() 201 if [ "$HAVE_GO_TEST_COVER" ]; then 202 # if our current go install has -cover, we want to use it :) 203 mkdir -p "$DEST/coverprofiles" 204 coverprofile="docker${dir#.}" 205 coverprofile="$ABS_DEST/coverprofiles/${coverprofile//\//-}" 206 testcover=( -cover -coverprofile "$coverprofile" $coverpkg ) 207 fi 208 ( 209 echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}" 210 cd "$dir" 211 export DEST="$ABS_DEST" # we're in a subshell, so this is safe -- our integration-cli tests need DEST, and "cd" screws it up 212 test_env go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS 213 ) 214 } 215 test_env() { 216 # use "env -i" to tightly control the environment variables that bleed into the tests 217 env -i \ 218 DEST="$DEST" \ 219 DOCKER_EXECDRIVER="$DOCKER_EXECDRIVER" \ 220 DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \ 221 DOCKER_USERLANDPROXY="$DOCKER_USERLANDPROXY" \ 222 DOCKER_HOST="$DOCKER_HOST" \ 223 DOCKER_REMAP_ROOT="$DOCKER_REMAP_ROOT" \ 224 DOCKER_REMOTE_DAEMON="$DOCKER_REMOTE_DAEMON" \ 225 GOPATH="$GOPATH" \ 226 HOME="$ABS_DEST/fake-HOME" \ 227 PATH="$PATH" \ 228 TEMP="$TEMP" \ 229 TEST_DOCKERINIT_PATH="$TEST_DOCKERINIT_PATH" \ 230 "$@" 231 } 232 233 # a helper to provide ".exe" when it's appropriate 234 binary_extension() { 235 if [ "$(go env GOOS)" = 'windows' ]; then 236 echo -n '.exe' 237 fi 238 } 239 240 # This helper function walks the current directory looking for directories 241 # holding certain files ($1 parameter), and prints their paths on standard 242 # output, one per line. 243 find_dirs() { 244 find . -not \( \ 245 \( \ 246 -path './vendor/*' \ 247 -o -path './integration-cli/*' \ 248 -o -path './contrib/*' \ 249 -o -path './pkg/mflag/example/*' \ 250 -o -path './.git/*' \ 251 -o -path './bundles/*' \ 252 -o -path './docs/*' \ 253 -o -path './pkg/libcontainer/nsinit/*' \ 254 \) \ 255 -prune \ 256 \) -name "$1" -print0 | xargs -0n1 dirname | sort -u 257 } 258 259 hash_files() { 260 while [ $# -gt 0 ]; do 261 f="$1" 262 shift 263 dir="$(dirname "$f")" 264 base="$(basename "$f")" 265 for hashAlgo in md5 sha256; do 266 if command -v "${hashAlgo}sum" &> /dev/null; then 267 ( 268 # subshell and cd so that we get output files like: 269 # $HASH docker-$VERSION 270 # instead of: 271 # $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION 272 cd "$dir" 273 "${hashAlgo}sum" "$base" > "$base.$hashAlgo" 274 ) 275 fi 276 done 277 done 278 } 279 280 bundle() { 281 local bundle="$1"; shift 282 echo "---> Making bundle: $(basename "$bundle") (in $DEST)" 283 source "$SCRIPTDIR/make/$bundle" "$@" 284 } 285 286 main() { 287 # We want this to fail if the bundles already exist and cannot be removed. 288 # This is to avoid mixing bundles from different versions of the code. 289 mkdir -p bundles 290 if [ -e "bundles/$VERSION" ]; then 291 echo "bundles/$VERSION already exists. Removing." 292 rm -fr "bundles/$VERSION" && mkdir "bundles/$VERSION" || exit 1 293 echo 294 fi 295 296 if [ "$(go env GOHOSTOS)" != 'windows' ]; then 297 # Windows and symlinks don't get along well 298 299 rm -f bundles/latest 300 ln -s "$VERSION" bundles/latest 301 fi 302 303 if [ $# -lt 1 ]; then 304 bundles=(${DEFAULT_BUNDLES[@]}) 305 else 306 bundles=($@) 307 fi 308 for bundle in ${bundles[@]}; do 309 export DEST="bundles/$VERSION/$(basename "$bundle")" 310 # Cygdrive paths don't play well with go build -o. 311 if [[ "$(uname -s)" == CYGWIN* ]]; then 312 export DEST="$(cygpath -mw "$DEST")" 313 fi 314 mkdir -p "$DEST" 315 ABS_DEST="$(cd "$DEST" && pwd -P)" 316 bundle "$bundle" 317 echo 318 done 319 } 320 321 main "$@"