make.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/usr/bin/env bash
  2. set -e
  3. # This script builds various binary artifacts from a checkout of the docker
  4. # source code.
  5. #
  6. # Requirements:
  7. # - The current directory should be a checkout of the docker source code
  8. # (https://github.com/docker/docker). Whatever version is checked out
  9. # will be built.
  10. # - The VERSION file, at the root of the repository, should exist, and
  11. # will be used as Docker binary version and package version.
  12. # - The hash of the git commit will also be included in the Docker binary,
  13. # with the suffix -unsupported if the repository isn't clean.
  14. # - The script is intended to be run inside the docker container specified
  15. # in the Dockerfile at the root of the source. In other words:
  16. # DO NOT CALL THIS SCRIPT DIRECTLY.
  17. # - The right way to call this script is to invoke "make" from
  18. # your checkout of the Docker repository.
  19. # the Makefile will do a "docker build -t docker ." and then
  20. # "docker run hack/make.sh" in the resulting image.
  21. #
  22. set -o pipefail
  23. export DOCKER_PKG='github.com/docker/docker'
  24. export SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  25. export MAKEDIR="$SCRIPTDIR/make"
  26. export PKG_CONFIG=${PKG_CONFIG:-pkg-config}
  27. # We're a nice, sexy, little shell script, and people might try to run us;
  28. # but really, they shouldn't. We want to be in a container!
  29. inContainer="AssumeSoInitially"
  30. if [ "$(go env GOHOSTOS)" = 'windows' ]; then
  31. if [ -z "$FROM_DOCKERFILE" ]; then
  32. unset inContainer
  33. fi
  34. else
  35. if [ "$PWD" != "/go/src/$DOCKER_PKG" ]; then
  36. unset inContainer
  37. fi
  38. fi
  39. if [ -z "$inContainer" ]; then
  40. {
  41. echo "# WARNING! I don't seem to be running in a Docker container."
  42. echo "# The result of this command might be an incorrect build, and will not be"
  43. echo "# officially supported."
  44. echo "#"
  45. echo "# Try this instead: make all"
  46. echo "#"
  47. } >&2
  48. fi
  49. echo
  50. # List of bundles to create when no argument is passed
  51. DEFAULT_BUNDLES=(
  52. binary-daemon
  53. dynbinary
  54. test-unit
  55. test-integration-cli
  56. test-docker-py
  57. cross
  58. tgz
  59. )
  60. VERSION=$(< ./VERSION)
  61. ! BUILDTIME=$(date --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')
  62. if [ "$DOCKER_GITCOMMIT" ]; then
  63. GITCOMMIT="$DOCKER_GITCOMMIT"
  64. elif command -v git &> /dev/null && [ -d .git ] && git rev-parse &> /dev/null; then
  65. GITCOMMIT=$(git rev-parse --short HEAD)
  66. if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
  67. GITCOMMIT="$GITCOMMIT-unsupported"
  68. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  69. echo "# GITCOMMIT = $GITCOMMIT"
  70. echo "# The version you are building is listed as unsupported because"
  71. echo "# there are some files in the git repository that are in an uncommitted state."
  72. echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
  73. echo "# Here is the current list:"
  74. git status --porcelain --untracked-files=no
  75. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  76. fi
  77. else
  78. echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
  79. echo >&2 ' Please either build with the .git directory accessible, or specify the'
  80. echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
  81. echo >&2 ' future accountability in diagnosing build issues. Thanks!'
  82. exit 1
  83. fi
  84. if [ "$AUTO_GOPATH" ]; then
  85. rm -rf .gopath
  86. mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
  87. ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
  88. export GOPATH="${PWD}/.gopath"
  89. if [ "$(go env GOOS)" = 'solaris' ]; then
  90. # sys/unix is installed outside the standard library on solaris
  91. # TODO need to allow for version change, need to get version from go
  92. export GO_VERSION=${GO_VERSION:-"1.8.1"}
  93. export GOPATH="${GOPATH}:/usr/lib/gocode/${GO_VERSION}"
  94. fi
  95. fi
  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. DOCKER_BUILDTAGS+=" daemon"
  102. if ${PKG_CONFIG} 'libsystemd >= 209' 2> /dev/null ; then
  103. DOCKER_BUILDTAGS+=" journald"
  104. elif ${PKG_CONFIG} 'libsystemd-journal' 2> /dev/null ; then
  105. DOCKER_BUILDTAGS+=" journald journald_compat"
  106. fi
  107. # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately
  108. if \
  109. command -v gcc &> /dev/null \
  110. && ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \
  111. ; then
  112. DOCKER_BUILDTAGS+=' btrfs_noversion'
  113. fi
  114. # test whether "libdevmapper.h" is new enough to support deferred remove
  115. # functionality.
  116. if \
  117. command -v gcc &> /dev/null \
  118. && ! ( echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -xc - -o /dev/null -ldevmapper &> /dev/null ) \
  119. ; then
  120. DOCKER_BUILDTAGS+=' libdm_no_deferred_remove'
  121. fi
  122. # Use these flags when compiling the tests and final binary
  123. IAMSTATIC='true'
  124. source "$SCRIPTDIR/make/.go-autogen"
  125. if [ -z "$DOCKER_DEBUG" ]; then
  126. LDFLAGS='-w'
  127. fi
  128. LDFLAGS_STATIC=''
  129. EXTLDFLAGS_STATIC='-static'
  130. # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
  131. # with options like -race.
  132. ORIG_BUILDFLAGS=( -tags "autogen netgo static_build $DOCKER_BUILDTAGS" -installsuffix netgo )
  133. # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
  134. # When $DOCKER_INCREMENTAL_BINARY is set in the environment, enable incremental
  135. # builds by installing dependent packages to the GOPATH.
  136. REBUILD_FLAG="-a"
  137. if [ "$DOCKER_INCREMENTAL_BINARY" == "1" ] || [ "$DOCKER_INCREMENTAL_BINARY" == "true" ]; then
  138. REBUILD_FLAG="-i"
  139. fi
  140. ORIG_BUILDFLAGS+=( $REBUILD_FLAG )
  141. BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
  142. # Test timeout.
  143. if [ "${DOCKER_ENGINE_GOARCH}" == "arm" ]; then
  144. : ${TIMEOUT:=10m}
  145. elif [ "${DOCKER_ENGINE_GOARCH}" == "windows" ]; then
  146. : ${TIMEOUT:=8m}
  147. else
  148. : ${TIMEOUT:=5m}
  149. fi
  150. LDFLAGS_STATIC_DOCKER="
  151. $LDFLAGS_STATIC
  152. -extldflags \"$EXTLDFLAGS_STATIC\"
  153. "
  154. if [ "$(uname -s)" = 'FreeBSD' ]; then
  155. # Tell cgo the compiler is Clang, not GCC
  156. # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
  157. export CC=clang
  158. # "-extld clang" is a workaround for
  159. # https://code.google.com/p/go/issues/detail?id=6845
  160. LDFLAGS="$LDFLAGS -extld clang"
  161. fi
  162. HAVE_GO_TEST_COVER=
  163. if \
  164. go help testflag | grep -- -cover > /dev/null \
  165. && go tool -n cover > /dev/null 2>&1 \
  166. ; then
  167. HAVE_GO_TEST_COVER=1
  168. fi
  169. # a helper to provide ".exe" when it's appropriate
  170. binary_extension() {
  171. if [ "$(go env GOOS)" = 'windows' ]; then
  172. echo -n '.exe'
  173. fi
  174. }
  175. hash_files() {
  176. while [ $# -gt 0 ]; do
  177. f="$1"
  178. shift
  179. dir="$(dirname "$f")"
  180. base="$(basename "$f")"
  181. for hashAlgo in md5 sha256; do
  182. if command -v "${hashAlgo}sum" &> /dev/null; then
  183. (
  184. # subshell and cd so that we get output files like:
  185. # $HASH docker-$VERSION
  186. # instead of:
  187. # $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
  188. cd "$dir"
  189. "${hashAlgo}sum" "$base" > "$base.$hashAlgo"
  190. )
  191. fi
  192. done
  193. done
  194. }
  195. bundle() {
  196. local bundle="$1"; shift
  197. echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
  198. source "$SCRIPTDIR/make/$bundle" "$@"
  199. }
  200. copy_binaries() {
  201. dir="$1"
  202. # Add nested executables to bundle dir so we have complete set of
  203. # them available, but only if the native OS/ARCH is the same as the
  204. # OS/ARCH of the build target
  205. if [ "$(go env GOOS)/$(go env GOARCH)" == "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
  206. if [ -x /usr/local/bin/docker-runc ]; then
  207. echo "Copying nested executables into $dir"
  208. for file in containerd containerd-shim containerd-ctr runc init proxy; do
  209. cp -f `which "docker-$file"` "$dir/"
  210. if [ "$2" == "hash" ]; then
  211. hash_files "$dir/docker-$file"
  212. fi
  213. done
  214. fi
  215. fi
  216. }
  217. install_binary() {
  218. file="$1"
  219. target="${DOCKER_MAKE_INSTALL_PREFIX:=/usr/local}/bin/"
  220. if [ "$(go env GOOS)" == "linux" ]; then
  221. echo "Installing $(basename $file) to ${target}"
  222. mkdir -p "$target"
  223. cp -f -L "$file" "$target"
  224. else
  225. echo "Install is only supported on linux"
  226. return 1
  227. fi
  228. }
  229. main() {
  230. # We want this to fail if the bundles already exist and cannot be removed.
  231. # This is to avoid mixing bundles from different versions of the code.
  232. mkdir -p bundles
  233. if [ -e "bundles/$VERSION" ] && [ -z "$KEEPBUNDLE" ]; then
  234. echo "bundles/$VERSION already exists. Removing."
  235. rm -fr "bundles/$VERSION" && mkdir "bundles/$VERSION" || exit 1
  236. echo
  237. fi
  238. if [ "$(go env GOHOSTOS)" != 'windows' ]; then
  239. # Windows and symlinks don't get along well
  240. rm -f bundles/latest
  241. ln -s "$VERSION" bundles/latest
  242. fi
  243. if [ $# -lt 1 ]; then
  244. bundles=(${DEFAULT_BUNDLES[@]})
  245. else
  246. bundles=($@)
  247. fi
  248. for bundle in ${bundles[@]}; do
  249. export DEST="bundles/$VERSION/$(basename "$bundle")"
  250. # Cygdrive paths don't play well with go build -o.
  251. if [[ "$(uname -s)" == CYGWIN* ]]; then
  252. export DEST="$(cygpath -mw "$DEST")"
  253. fi
  254. mkdir -p "$DEST"
  255. ABS_DEST="$(cd "$DEST" && pwd -P)"
  256. bundle "$bundle"
  257. echo
  258. done
  259. }
  260. main "$@"