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