make.sh 9.2 KB

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