make.sh 8.7 KB

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