make.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. echo
  28. # List of bundles to create when no argument is passed
  29. DEFAULT_BUNDLES=(
  30. binary-daemon
  31. dynbinary
  32. test-integration
  33. test-docker-py
  34. cross
  35. )
  36. VERSION=${VERSION:-dev}
  37. ! BUILDTIME=$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')
  38. if [ "$DOCKER_GITCOMMIT" ]; then
  39. GITCOMMIT="$DOCKER_GITCOMMIT"
  40. elif command -v git &> /dev/null && [ -e .git ] && git rev-parse &> /dev/null; then
  41. GITCOMMIT=$(git rev-parse --short HEAD)
  42. if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
  43. GITCOMMIT="$GITCOMMIT-unsupported"
  44. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  45. echo "# GITCOMMIT = $GITCOMMIT"
  46. echo "# The version you are building is listed as unsupported because"
  47. echo "# there are some files in the git repository that are in an uncommitted state."
  48. echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
  49. echo "# Here is the current list:"
  50. git status --porcelain --untracked-files=no
  51. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  52. fi
  53. else
  54. echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
  55. echo >&2 ' Please either build with the .git directory accessible, or specify the'
  56. echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
  57. echo >&2 ' future accountability in diagnosing build issues. Thanks!'
  58. exit 1
  59. fi
  60. if [ "$AUTO_GOPATH" ]; then
  61. rm -rf .gopath
  62. mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
  63. ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
  64. export GOPATH="${PWD}/.gopath"
  65. fi
  66. if [ ! "$GOPATH" ]; then
  67. echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH'
  68. echo >&2 ' alternatively, set AUTO_GOPATH=1'
  69. exit 1
  70. fi
  71. # Adds $1_$2 to DOCKER_BUILDTAGS unless it already
  72. # contains a word starting from $1_
  73. add_buildtag() {
  74. [[ " $DOCKER_BUILDTAGS" == *" $1_"* ]] || DOCKER_BUILDTAGS+=" $1_$2"
  75. }
  76. if ${PKG_CONFIG} 'libsystemd >= 209' 2> /dev/null; then
  77. DOCKER_BUILDTAGS+=" journald"
  78. elif ${PKG_CONFIG} 'libsystemd-journal' 2> /dev/null; then
  79. DOCKER_BUILDTAGS+=" journald journald_compat"
  80. fi
  81. # test whether "libdevmapper.h" is new enough to support deferred remove
  82. # functionality. We favour libdm_dlsym_deferred_remove over
  83. # libdm_no_deferred_remove in dynamic cases because the binary could be shipped
  84. # with a newer libdevmapper than the one it was built with.
  85. if
  86. command -v gcc &> /dev/null \
  87. && ! (echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }' | gcc -xc - -o /dev/null $(pkg-config --libs devmapper) &> /dev/null) \
  88. ;
  89. then
  90. add_buildtag libdm dlsym_deferred_remove
  91. fi
  92. # Use these flags when compiling the tests and final binary
  93. IAMSTATIC='true'
  94. if [ -z "$DOCKER_DEBUG" ]; then
  95. LDFLAGS='-w'
  96. fi
  97. LDFLAGS_STATIC=''
  98. EXTLDFLAGS_STATIC='-static'
  99. # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
  100. # with options like -race.
  101. ORIG_BUILDFLAGS=(-tags "netgo osusergo static_build $DOCKER_BUILDTAGS" -installsuffix netgo)
  102. # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
  103. BUILDFLAGS=(${BUILDFLAGS} "${ORIG_BUILDFLAGS[@]}")
  104. LDFLAGS_STATIC_DOCKER="
  105. $LDFLAGS_STATIC
  106. -extldflags \"$EXTLDFLAGS_STATIC\"
  107. "
  108. if [ "$(uname -s)" = 'FreeBSD' ]; then
  109. # Tell cgo the compiler is Clang, not GCC
  110. # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
  111. export CC=clang
  112. # "-extld clang" is a workaround for
  113. # https://code.google.com/p/go/issues/detail?id=6845
  114. LDFLAGS="$LDFLAGS -extld clang"
  115. fi
  116. bundle() {
  117. local bundle="$1"
  118. shift
  119. echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
  120. source "$SCRIPTDIR/make/$bundle" "$@"
  121. }
  122. main() {
  123. bundle_dir="bundles"
  124. if [ -n "${PREFIX}" ]; then
  125. bundle_dir="${PREFIX}/${bundle_dir}"
  126. fi
  127. if [ -z "${KEEPBUNDLE-}" ]; then
  128. echo "Removing ${bundle_dir}/"
  129. rm -rf "${bundle_dir}"/*
  130. echo
  131. fi
  132. mkdir -p "${bundle_dir}"
  133. if [ $# -lt 1 ]; then
  134. bundles=(${DEFAULT_BUNDLES[@]})
  135. else
  136. bundles=($@)
  137. fi
  138. for bundle in ${bundles[@]}; do
  139. export DEST="${bundle_dir}/$(basename "$bundle")"
  140. # Cygdrive paths don't play well with go build -o.
  141. if [[ "$(uname -s)" == CYGWIN* ]]; then
  142. export DEST="$(cygpath -mw "$DEST")"
  143. fi
  144. mkdir -p "$DEST"
  145. ABS_DEST="$(cd "$DEST" && pwd -P)"
  146. bundle "$bundle"
  147. echo
  148. done
  149. }
  150. main "$@"