make.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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
  56. test-docker-py
  57. cross
  58. tgz
  59. )
  60. VERSION=${VERSION:-$(< ./VERSION)}
  61. ! BUILDTIME=$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" --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 && [ -e .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. if ${PKG_CONFIG} 'libsystemd >= 209' 2> /dev/null ; then
  102. DOCKER_BUILDTAGS+=" journald"
  103. elif ${PKG_CONFIG} 'libsystemd-journal' 2> /dev/null ; then
  104. DOCKER_BUILDTAGS+=" journald journald_compat"
  105. fi
  106. # test whether "btrfs/version.h" exists and apply btrfs_noversion appropriately
  107. if \
  108. command -v gcc &> /dev/null \
  109. && ! gcc -E - -o /dev/null &> /dev/null <<<'#include <btrfs/version.h>' \
  110. ; then
  111. DOCKER_BUILDTAGS+=' btrfs_noversion'
  112. fi
  113. # test whether "libdevmapper.h" is new enough to support deferred remove
  114. # functionality.
  115. if \
  116. command -v gcc &> /dev/null \
  117. && ! ( echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }'| gcc -xc - -o /dev/null $(pkg-config --libs devmapper) &> /dev/null ) \
  118. ; then
  119. DOCKER_BUILDTAGS+=' libdm_no_deferred_remove'
  120. fi
  121. # Use these flags when compiling the tests and final binary
  122. IAMSTATIC='true'
  123. if [ -z "$DOCKER_DEBUG" ]; then
  124. LDFLAGS='-w'
  125. fi
  126. LDFLAGS_STATIC=''
  127. EXTLDFLAGS_STATIC='-static'
  128. # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
  129. # with options like -race.
  130. ORIG_BUILDFLAGS=( -tags "autogen netgo static_build $DOCKER_BUILDTAGS" -installsuffix netgo )
  131. # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
  132. # When $DOCKER_INCREMENTAL_BINARY is set in the environment, enable incremental
  133. # builds by installing dependent packages to the GOPATH.
  134. REBUILD_FLAG="-a"
  135. if [ "$DOCKER_INCREMENTAL_BINARY" == "1" ] || [ "$DOCKER_INCREMENTAL_BINARY" == "true" ]; then
  136. REBUILD_FLAG="-i"
  137. fi
  138. ORIG_BUILDFLAGS+=( $REBUILD_FLAG )
  139. BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
  140. # Test timeout.
  141. if [ "${DOCKER_ENGINE_GOARCH}" == "arm" ]; then
  142. : ${TIMEOUT:=10m}
  143. elif [ "${DOCKER_ENGINE_GOARCH}" == "windows" ]; then
  144. : ${TIMEOUT:=8m}
  145. else
  146. : ${TIMEOUT:=5m}
  147. fi
  148. LDFLAGS_STATIC_DOCKER="
  149. $LDFLAGS_STATIC
  150. -extldflags \"$EXTLDFLAGS_STATIC\"
  151. "
  152. if [ "$(uname -s)" = 'FreeBSD' ]; then
  153. # Tell cgo the compiler is Clang, not GCC
  154. # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
  155. export CC=clang
  156. # "-extld clang" is a workaround for
  157. # https://code.google.com/p/go/issues/detail?id=6845
  158. LDFLAGS="$LDFLAGS -extld clang"
  159. fi
  160. bundle() {
  161. local bundle="$1"; shift
  162. echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
  163. source "$SCRIPTDIR/make/$bundle" "$@"
  164. }
  165. main() {
  166. if [ -z "${KEEPBUNDLE-}" ]; then
  167. echo "Removing bundles/"
  168. rm -rf "bundles/*"
  169. echo
  170. fi
  171. mkdir -p bundles
  172. # Windows and symlinks don't get along well
  173. if [ "$(go env GOHOSTOS)" != 'windows' ]; then
  174. rm -f bundles/latest
  175. # preserve latest symlink for backward compatibility
  176. ln -sf . bundles/latest
  177. fi
  178. if [ $# -lt 1 ]; then
  179. bundles=(${DEFAULT_BUNDLES[@]})
  180. else
  181. bundles=($@)
  182. fi
  183. for bundle in ${bundles[@]}; do
  184. export DEST="bundles/$(basename "$bundle")"
  185. # Cygdrive paths don't play well with go build -o.
  186. if [[ "$(uname -s)" == CYGWIN* ]]; then
  187. export DEST="$(cygpath -mw "$DEST")"
  188. fi
  189. mkdir -p "$DEST"
  190. ABS_DEST="$(cd "$DEST" && pwd -P)"
  191. bundle "$bundle"
  192. echo
  193. done
  194. }
  195. main "$@"