make.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. # (http://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 -dirty if the repository isn't clean.
  14. # - The script is intented 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. # We're a nice, sexy, little shell script, and people might try to run us;
  25. # but really, they shouldn't. We want to be in a container!
  26. if [ "$(pwd)" != "/go/src/$DOCKER_PKG" ] || [ -z "$DOCKER_CROSSPLATFORMS" ]; then
  27. {
  28. echo "# WARNING! I don't seem to be running in the Docker container."
  29. echo "# The result of this command might be an incorrect build, and will not be"
  30. echo "# officially supported."
  31. echo "#"
  32. echo "# Try this instead: make all"
  33. echo "#"
  34. } >&2
  35. fi
  36. echo
  37. # List of bundles to create when no argument is passed
  38. DEFAULT_BUNDLES=(
  39. validate-dco
  40. validate-gofmt
  41. binary
  42. test-unit
  43. test-integration
  44. test-integration-cli
  45. test-docker-py
  46. dynbinary
  47. dyntest-unit
  48. dyntest-integration
  49. cover
  50. cross
  51. tgz
  52. ubuntu
  53. )
  54. VERSION=$(cat ./VERSION)
  55. if command -v git &> /dev/null && git rev-parse &> /dev/null; then
  56. GITCOMMIT=$(git rev-parse --short HEAD)
  57. if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
  58. GITCOMMIT="$GITCOMMIT-dirty"
  59. fi
  60. elif [ "$DOCKER_GITCOMMIT" ]; then
  61. GITCOMMIT="$DOCKER_GITCOMMIT"
  62. else
  63. echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
  64. echo >&2 ' Please either build with the .git directory accessible, or specify the'
  65. echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
  66. echo >&2 ' future accountability in diagnosing build issues. Thanks!'
  67. exit 1
  68. fi
  69. if [ "$AUTO_GOPATH" ]; then
  70. rm -rf .gopath
  71. mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
  72. ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
  73. export GOPATH="$(pwd)/.gopath:$(pwd)/vendor"
  74. fi
  75. if [ ! "$GOPATH" ]; then
  76. echo >&2 'error: missing GOPATH; please see http://golang.org/doc/code.html#GOPATH'
  77. echo >&2 ' alternatively, set AUTO_GOPATH=1'
  78. exit 1
  79. fi
  80. if [ -z "$DOCKER_CLIENTONLY" ]; then
  81. DOCKER_BUILDTAGS+=" daemon"
  82. fi
  83. # Use these flags when compiling the tests and final binary
  84. LDFLAGS='
  85. -X '$DOCKER_PKG'/dockerversion.GITCOMMIT "'$GITCOMMIT'"
  86. -X '$DOCKER_PKG'/dockerversion.VERSION "'$VERSION'"
  87. '
  88. if [ -z "$DEBUG" ]; then
  89. LDFLAGS="-w $LDFLAGS"
  90. fi
  91. LDFLAGS_STATIC='-linkmode external'
  92. # Cgo -H windows is incompatible with -linkmode external.
  93. if [ "$(go env GOOS)" == 'windows' ]; then
  94. LDFLAGS_STATIC=''
  95. fi
  96. EXTLDFLAGS_STATIC='-static'
  97. # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
  98. # with options like -race.
  99. ORIG_BUILDFLAGS=( -a -tags "netgo static_build $DOCKER_BUILDTAGS" )
  100. BUILDFLAGS=( $BUILDFLAGS "${ORIG_BUILDFLAGS[@]}" )
  101. # Test timeout.
  102. : ${TIMEOUT:=30m}
  103. TESTFLAGS+=" -test.timeout=${TIMEOUT}"
  104. # A few more flags that are specific just to building a completely-static binary (see hack/make/binary)
  105. # PLEASE do not use these anywhere else.
  106. EXTLDFLAGS_STATIC_DOCKER="$EXTLDFLAGS_STATIC -lpthread -Wl,--unresolved-symbols=ignore-in-object-files"
  107. LDFLAGS_STATIC_DOCKER="
  108. $LDFLAGS_STATIC
  109. -X $DOCKER_PKG/dockerversion.IAMSTATIC true
  110. -extldflags \"$EXTLDFLAGS_STATIC_DOCKER\"
  111. "
  112. if [ "$(uname -s)" = 'FreeBSD' ]; then
  113. # Tell cgo the compiler is Clang, not GCC
  114. # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
  115. export CC=clang
  116. # "-extld clang" is a workaround for
  117. # https://code.google.com/p/go/issues/detail?id=6845
  118. LDFLAGS="$LDFLAGS -extld clang"
  119. fi
  120. # If sqlite3.h doesn't exist under /usr/include,
  121. # check /usr/local/include also just in case
  122. # (e.g. FreeBSD Ports installs it under the directory)
  123. if [ ! -e /usr/include/sqlite3.h ] && [ -e /usr/local/include/sqlite3.h ]; then
  124. export CGO_CFLAGS='-I/usr/local/include'
  125. export CGO_LDFLAGS='-L/usr/local/lib'
  126. fi
  127. HAVE_GO_TEST_COVER=
  128. if \
  129. go help testflag | grep -- -cover > /dev/null \
  130. && go tool -n cover > /dev/null 2>&1 \
  131. ; then
  132. HAVE_GO_TEST_COVER=1
  133. fi
  134. # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
  135. # You can use this to select certain tests to run, eg.
  136. #
  137. # TESTFLAGS='-run ^TestBuild$' ./hack/make.sh test
  138. #
  139. go_test_dir() {
  140. dir=$1
  141. coverpkg=$2
  142. testcover=()
  143. if [ "$HAVE_GO_TEST_COVER" ]; then
  144. # if our current go install has -cover, we want to use it :)
  145. mkdir -p "$DEST/coverprofiles"
  146. coverprofile="docker${dir#.}"
  147. coverprofile="$DEST/coverprofiles/${coverprofile//\//-}"
  148. testcover=( -cover -coverprofile "$coverprofile" $coverpkg )
  149. fi
  150. (
  151. export DEST
  152. echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
  153. cd "$dir"
  154. go test ${testcover[@]} -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS
  155. )
  156. }
  157. # a helper to provide ".exe" when it's appropriate
  158. binary_extension() {
  159. if [ "$(go env GOOS)" = 'windows' ]; then
  160. echo -n '.exe'
  161. fi
  162. }
  163. # This helper function walks the current directory looking for directories
  164. # holding certain files ($1 parameter), and prints their paths on standard
  165. # output, one per line.
  166. find_dirs() {
  167. find . -not \( \
  168. \( \
  169. -path './vendor/*' \
  170. -o -path './integration/*' \
  171. -o -path './integration-cli/*' \
  172. -o -path './contrib/*' \
  173. -o -path './pkg/mflag/example/*' \
  174. -o -path './.git/*' \
  175. -o -path './bundles/*' \
  176. -o -path './docs/*' \
  177. -o -path './pkg/libcontainer/nsinit/*' \
  178. \) \
  179. -prune \
  180. \) -name "$1" -print0 | xargs -0n1 dirname | sort -u
  181. }
  182. hash_files() {
  183. while [ $# -gt 0 ]; do
  184. f="$1"
  185. shift
  186. dir="$(dirname "$f")"
  187. base="$(basename "$f")"
  188. for hashAlgo in md5 sha256; do
  189. if command -v "${hashAlgo}sum" &> /dev/null; then
  190. (
  191. # subshell and cd so that we get output files like:
  192. # $HASH docker-$VERSION
  193. # instead of:
  194. # $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
  195. cd "$dir"
  196. "${hashAlgo}sum" "$base" > "$base.$hashAlgo"
  197. )
  198. fi
  199. done
  200. done
  201. }
  202. bundle() {
  203. bundlescript=$1
  204. bundle=$(basename $bundlescript)
  205. echo "---> Making bundle: $bundle (in bundles/$VERSION/$bundle)"
  206. mkdir -p bundles/$VERSION/$bundle
  207. source "$bundlescript" "$(pwd)/bundles/$VERSION/$bundle"
  208. }
  209. main() {
  210. # We want this to fail if the bundles already exist and cannot be removed.
  211. # This is to avoid mixing bundles from different versions of the code.
  212. mkdir -p bundles
  213. if [ -e "bundles/$VERSION" ]; then
  214. echo "bundles/$VERSION already exists. Removing."
  215. rm -fr bundles/$VERSION && mkdir bundles/$VERSION || exit 1
  216. echo
  217. fi
  218. SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  219. if [ $# -lt 1 ]; then
  220. bundles=(${DEFAULT_BUNDLES[@]})
  221. else
  222. bundles=($@)
  223. fi
  224. for bundle in ${bundles[@]}; do
  225. bundle $SCRIPTDIR/make/$bundle
  226. echo
  227. done
  228. }
  229. main "$@"