github.com/jaegerpicker/docker@v0.7.7-0.20150325003727-22dba32b4dab/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 # (http://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 28 # We're a nice, sexy, little shell script, and people might try to run us; 29 # but really, they shouldn't. We want to be in a container! 30 if [ "$(pwd)" != "/go/src/$DOCKER_PKG" ] || [ -z "$DOCKER_CROSSPLATFORMS" ]; then 31 { 32 echo "# WARNING! I don't seem to be running in the Docker container." 33 echo "# The result of this command might be an incorrect build, and will not be" 34 echo "# officially supported." 35 echo "#" 36 echo "# Try this instead: make all" 37 echo "#" 38 } >&2 39 fi 40 41 echo 42 43 # List of bundles to create when no argument is passed 44 DEFAULT_BUNDLES=( 45 validate-dco 46 validate-gofmt 47 validate-toml 48 validate-spaces 49 50 binary 51 52 test-unit 53 test-integration-cli 54 test-docker-py 55 56 dynbinary 57 test-integration 58 59 cover 60 cross 61 tgz 62 ubuntu 63 ) 64 65 VERSION=$(cat ./VERSION) 66 if command -v git &> /dev/null && git rev-parse &> /dev/null; then 67 GITCOMMIT=$(git rev-parse --short HEAD) 68 if [ -n "$(git status --porcelain --untracked-files=no)" ]; then 69 GITCOMMIT="$GITCOMMIT-dirty" 70 fi 71 elif [ "$DOCKER_GITCOMMIT" ]; then 72 GITCOMMIT="$DOCKER_GITCOMMIT" 73 else 74 echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified' 75 echo >&2 ' Please either build with the .git directory accessible, or specify the' 76 echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for' 77 echo >&2 ' future accountability in diagnosing build issues. Thanks!' 78 exit 1 79 fi 80 81 if [ "$AUTO_GOPATH" ]; then 82 rm -rf .gopath 83 mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")" 84 ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}" 85 export GOPATH="$(pwd)/.gopath:$(pwd)/vendor" 86 fi 87 88 if [ ! "$GOPATH" ]; then 89 echo >&2 'error: missing GOPATH; please see http://golang.org/doc/code.html#GOPATH' 90 echo >&2 ' alternatively, set AUTO_GOPATH=1' 91 exit 1 92 fi 93 94 if [ -z "$DOCKER_CLIENTONLY" ]; then 95 DOCKER_BUILDTAGS+=" daemon" 96 fi 97 98 if [ "$DOCKER_EXECDRIVER" = 'lxc' ]; then 99 DOCKER_BUILDTAGS+=' test_no_exec' 100 fi 101 102 # Use these flags when compiling the tests and final binary 103 104 IAMSTATIC='true' 105 source "$(dirname "$BASH_SOURCE")/make/.go-autogen" 106 LDFLAGS='-w' 107 108 LDFLAGS_STATIC='-linkmode external' 109 # Cgo -H windows is incompatible with -linkmode external. 110 if [ "$(go env GOOS)" == 'windows' ]; then 111 LDFLAGS_STATIC='' 112 fi 113 EXTLDFLAGS_STATIC='-static' 114 # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build 115 # with options like -race. 116 ORIG_BUILDFLAGS=( -a -tags "netgo static_build $DOCKER_BUILDTAGS" -installsuffix netgo ) 117 # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here 118 BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" ) 119 # Test timeout. 120 : ${TIMEOUT:=30m} 121 TESTFLAGS+=" -test.timeout=${TIMEOUT}" 122 123 # A few more flags that are specific just to building a completely-static binary (see hack/make/binary) 124 # PLEASE do not use these anywhere else. 125 EXTLDFLAGS_STATIC_DOCKER="$EXTLDFLAGS_STATIC -lpthread -Wl,--unresolved-symbols=ignore-in-object-files" 126 LDFLAGS_STATIC_DOCKER=" 127 $LDFLAGS_STATIC 128 -extldflags \"$EXTLDFLAGS_STATIC_DOCKER\" 129 " 130 131 if [ "$(uname -s)" = 'FreeBSD' ]; then 132 # Tell cgo the compiler is Clang, not GCC 133 # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752 134 export CC=clang 135 136 # "-extld clang" is a workaround for 137 # https://code.google.com/p/go/issues/detail?id=6845 138 LDFLAGS="$LDFLAGS -extld clang" 139 fi 140 141 # If sqlite3.h doesn't exist under /usr/include, 142 # check /usr/local/include also just in case 143 # (e.g. FreeBSD Ports installs it under the directory) 144 if [ ! -e /usr/include/sqlite3.h ] && [ -e /usr/local/include/sqlite3.h ]; then 145 export CGO_CFLAGS='-I/usr/local/include' 146 export CGO_LDFLAGS='-L/usr/local/lib' 147 fi 148 149 HAVE_GO_TEST_COVER= 150 if \ 151 go help testflag | grep -- -cover > /dev/null \ 152 && go tool -n cover > /dev/null 2>&1 \ 153 ; then 154 HAVE_GO_TEST_COVER=1 155 fi 156 157 # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'. 158 # You can use this to select certain tests to run, eg. 159 # 160 # TESTFLAGS='-run ^TestBuild$' ./hack/make.sh test 161 # 162 go_test_dir() { 163 dir=$1 164 coverpkg=$2 165 testcover=() 166 if [ "$HAVE_GO_TEST_COVER" ]; then 167 # if our current go install has -cover, we want to use it :) 168 mkdir -p "$DEST/coverprofiles" 169 coverprofile="docker${dir#.}" 170 coverprofile="$DEST/coverprofiles/${coverprofile//\//-}" 171 testcover=( -cover -coverprofile "$coverprofile" $coverpkg ) 172 fi 173 ( 174 export DEST 175 echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}" 176 cd "$dir" 177 test_env go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS 178 ) 179 } 180 test_env() { 181 # use "env -i" to tightly control the environment variables that bleed into the tests 182 env -i \ 183 DEST="$DEST" \ 184 DOCKER_EXECDRIVER="$DOCKER_EXECDRIVER" \ 185 DOCKER_GRAPHDRIVER="$DOCKER_GRAPHDRIVER" \ 186 DOCKER_HOST="$DOCKER_HOST" \ 187 GOPATH="$GOPATH" \ 188 HOME="$DEST/fake-HOME" \ 189 PATH="$PATH" \ 190 TEST_DOCKERINIT_PATH="$TEST_DOCKERINIT_PATH" \ 191 "$@" 192 } 193 194 # a helper to provide ".exe" when it's appropriate 195 binary_extension() { 196 if [ "$(go env GOOS)" = 'windows' ]; then 197 echo -n '.exe' 198 fi 199 } 200 201 # This helper function walks the current directory looking for directories 202 # holding certain files ($1 parameter), and prints their paths on standard 203 # output, one per line. 204 find_dirs() { 205 find . -not \( \ 206 \( \ 207 -path './vendor/*' \ 208 -o -path './integration/*' \ 209 -o -path './integration-cli/*' \ 210 -o -path './contrib/*' \ 211 -o -path './pkg/mflag/example/*' \ 212 -o -path './.git/*' \ 213 -o -path './bundles/*' \ 214 -o -path './docs/*' \ 215 -o -path './pkg/libcontainer/nsinit/*' \ 216 \) \ 217 -prune \ 218 \) -name "$1" -print0 | xargs -0n1 dirname | sort -u 219 } 220 221 hash_files() { 222 while [ $# -gt 0 ]; do 223 f="$1" 224 shift 225 dir="$(dirname "$f")" 226 base="$(basename "$f")" 227 for hashAlgo in md5 sha256; do 228 if command -v "${hashAlgo}sum" &> /dev/null; then 229 ( 230 # subshell and cd so that we get output files like: 231 # $HASH docker-$VERSION 232 # instead of: 233 # $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION 234 cd "$dir" 235 "${hashAlgo}sum" "$base" > "$base.$hashAlgo" 236 ) 237 fi 238 done 239 done 240 } 241 242 bundle() { 243 bundlescript=$1 244 bundle=$(basename $bundlescript) 245 echo "---> Making bundle: $bundle (in bundles/$VERSION/$bundle)" 246 mkdir -p bundles/$VERSION/$bundle 247 source "$bundlescript" "$(pwd)/bundles/$VERSION/$bundle" 248 } 249 250 main() { 251 # We want this to fail if the bundles already exist and cannot be removed. 252 # This is to avoid mixing bundles from different versions of the code. 253 mkdir -p bundles 254 if [ -e "bundles/$VERSION" ]; then 255 echo "bundles/$VERSION already exists. Removing." 256 rm -fr bundles/$VERSION && mkdir bundles/$VERSION || exit 1 257 echo 258 fi 259 SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 260 if [ $# -lt 1 ]; then 261 bundles=(${DEFAULT_BUNDLES[@]}) 262 else 263 bundles=($@) 264 fi 265 for bundle in ${bundles[@]}; do 266 bundle $SCRIPTDIR/make/$bundle 267 echo 268 done 269 } 270 271 main "$@"