make.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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-integration
  55. test-docker-py
  56. cross
  57. )
  58. VERSION=${VERSION:-dev}
  59. ! BUILDTIME=$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')
  60. if [ "$DOCKER_GITCOMMIT" ]; then
  61. GITCOMMIT="$DOCKER_GITCOMMIT"
  62. elif command -v git &> /dev/null && [ -e .git ] && git rev-parse &> /dev/null; then
  63. GITCOMMIT=$(git rev-parse --short HEAD)
  64. if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
  65. GITCOMMIT="$GITCOMMIT-unsupported"
  66. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  67. echo "# GITCOMMIT = $GITCOMMIT"
  68. echo "# The version you are building is listed as unsupported because"
  69. echo "# there are some files in the git repository that are in an uncommitted state."
  70. echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
  71. echo "# Here is the current list:"
  72. git status --porcelain --untracked-files=no
  73. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  74. fi
  75. else
  76. echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
  77. echo >&2 ' Please either build with the .git directory accessible, or specify the'
  78. echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
  79. echo >&2 ' future accountability in diagnosing build issues. Thanks!'
  80. exit 1
  81. fi
  82. if [ "$AUTO_GOPATH" ]; then
  83. rm -rf .gopath
  84. mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
  85. ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
  86. export GOPATH="${PWD}/.gopath"
  87. fi
  88. if [ ! "$GOPATH" ]; then
  89. echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH'
  90. echo >&2 ' alternatively, set AUTO_GOPATH=1'
  91. exit 1
  92. fi
  93. if ${PKG_CONFIG} 'libsystemd >= 209' 2> /dev/null ; then
  94. DOCKER_BUILDTAGS+=" journald"
  95. elif ${PKG_CONFIG} 'libsystemd-journal' 2> /dev/null ; then
  96. DOCKER_BUILDTAGS+=" journald journald_compat"
  97. fi
  98. # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately
  99. if \
  100. command -v gcc &> /dev/null \
  101. && ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \
  102. ; then
  103. DOCKER_BUILDTAGS+=' btrfs_noversion'
  104. fi
  105. # test whether "libdevmapper.h" is new enough to support deferred remove
  106. # functionality.
  107. if \
  108. command -v gcc &> /dev/null \
  109. && ! ( echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -xc - -o /dev/null $(pkg-config --libs devmapper) &> /dev/null ) \
  110. ; then
  111. DOCKER_BUILDTAGS+=' libdm_no_deferred_remove'
  112. fi
  113. # Use these flags when compiling the tests and final binary
  114. IAMSTATIC='true'
  115. if [ -z "$DOCKER_DEBUG" ]; then
  116. LDFLAGS='-w'
  117. fi
  118. LDFLAGS_STATIC=''
  119. EXTLDFLAGS_STATIC='-static'
  120. # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
  121. # with options like -race.
  122. ORIG_BUILDFLAGS=( -tags "autogen netgo static_build $DOCKER_BUILDTAGS" -installsuffix netgo )
  123. # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
  124. # When $DOCKER_INCREMENTAL_BINARY is set in the environment, enable incremental
  125. # builds by installing dependent packages to the GOPATH.
  126. REBUILD_FLAG="-a"
  127. if [ "$DOCKER_INCREMENTAL_BINARY" == "1" ] || [ "$DOCKER_INCREMENTAL_BINARY" == "true" ]; then
  128. REBUILD_FLAG="-i"
  129. fi
  130. ORIG_BUILDFLAGS+=( $REBUILD_FLAG )
  131. BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
  132. # Test timeout.
  133. if [ "${DOCKER_ENGINE_GOARCH}" == "arm" ]; then
  134. : ${TIMEOUT:=10m}
  135. elif [ "${DOCKER_ENGINE_GOARCH}" == "windows" ]; then
  136. : ${TIMEOUT:=8m}
  137. else
  138. : ${TIMEOUT:=5m}
  139. fi
  140. LDFLAGS_STATIC_DOCKER="
  141. $LDFLAGS_STATIC
  142. -extldflags \"$EXTLDFLAGS_STATIC\"
  143. "
  144. if [ "$(uname -s)" = 'FreeBSD' ]; then
  145. # Tell cgo the compiler is Clang, not GCC
  146. # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
  147. export CC=clang
  148. # "-extld clang" is a workaround for
  149. # https://code.google.com/p/go/issues/detail?id=6845
  150. LDFLAGS="$LDFLAGS -extld clang"
  151. fi
  152. bundle() {
  153. local bundle="$1"; shift
  154. echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
  155. source "$SCRIPTDIR/make/$bundle" "$@"
  156. }
  157. main() {
  158. if [ -z "${KEEPBUNDLE-}" ]; then
  159. echo "Removing bundles/"
  160. rm -rf "bundles/*"
  161. echo
  162. fi
  163. mkdir -p bundles
  164. # Windows and symlinks don't get along well
  165. if [ "$(go env GOHOSTOS)" != 'windows' ]; then
  166. rm -f bundles/latest
  167. # preserve latest symlink for backward compatibility
  168. ln -sf . bundles/latest
  169. fi
  170. if [ $# -lt 1 ]; then
  171. bundles=(${DEFAULT_BUNDLES[@]})
  172. else
  173. bundles=($@)
  174. fi
  175. for bundle in ${bundles[@]}; do
  176. export DEST="bundles/$(basename "$bundle")"
  177. # Cygdrive paths don't play well with go build -o.
  178. if [[ "$(uname -s)" == CYGWIN* ]]; then
  179. export DEST="$(cygpath -mw "$DEST")"
  180. fi
  181. mkdir -p "$DEST"
  182. ABS_DEST="$(cd "$DEST" && pwd -P)"
  183. bundle "$bundle"
  184. echo
  185. done
  186. }
  187. main "$@"