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