make.sh 11 KB

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