make.sh 9.2 KB

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