make.sh 8.1 KB

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