make.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. echo
  28. # List of bundles to create when no argument is passed
  29. DEFAULT_BUNDLES=(
  30. binary-daemon
  31. dynbinary
  32. test-integration
  33. test-docker-py
  34. )
  35. VERSION=${VERSION:-dev}
  36. case "$VERSION" in
  37. refs/tags/v*) VERSION=${VERSION#refs/tags/v} ;;
  38. refs/tags/*) VERSION=${VERSION#refs/tags/} ;;
  39. refs/heads/*) VERSION=$(echo "${VERSION#refs/heads/}" | sed -r 's#/+#-#g') ;;
  40. refs/pull/*) VERSION=pr-$(echo "$VERSION" | grep -o '[0-9]\+') ;;
  41. esac
  42. ! BUILDTIME=$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')
  43. if [ "$DOCKER_GITCOMMIT" ]; then
  44. GITCOMMIT="$DOCKER_GITCOMMIT"
  45. elif command -v git &> /dev/null && [ -e .git ] && git rev-parse &> /dev/null; then
  46. GITCOMMIT=$(git rev-parse HEAD)
  47. if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
  48. GITCOMMIT="$GITCOMMIT-unsupported"
  49. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  50. echo "# GITCOMMIT = $GITCOMMIT"
  51. echo "# The version you are building is listed as unsupported because"
  52. echo "# there are some files in the git repository that are in an uncommitted state."
  53. echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
  54. echo "# Here is the current list:"
  55. git status --porcelain --untracked-files=no
  56. echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  57. fi
  58. else
  59. echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
  60. echo >&2 ' Please either build with the .git directory accessible, or specify the'
  61. echo >&2 ' exact commit hash you are building using DOCKER_GITCOMMIT for future'
  62. echo >&2 ' accountability in diagnosing build issues. Thanks!'
  63. exit 1
  64. fi
  65. if [ "$AUTO_GOPATH" ]; then
  66. rm -rf .gopath
  67. mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
  68. ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
  69. export GOPATH="${PWD}/.gopath"
  70. fi
  71. if [ ! "$GOPATH" ]; then
  72. echo >&2 'error: missing GOPATH; please see https://pkg.go.dev/cmd/go#hdr-GOPATH_environment_variable'
  73. echo >&2 ' alternatively, set AUTO_GOPATH=1'
  74. exit 1
  75. fi
  76. if ${PKG_CONFIG} 'libsystemd' 2> /dev/null; then
  77. DOCKER_BUILDTAGS+=" journald"
  78. fi
  79. # Use these flags when compiling the tests and final binary
  80. if [ -z "$DOCKER_DEBUG" ]; then
  81. LDFLAGS='-w'
  82. fi
  83. BUILDFLAGS=(${BUILDFLAGS} -tags "netgo osusergo static_build $DOCKER_BUILDTAGS")
  84. LDFLAGS_STATIC="-extldflags -static"
  85. if [ "$(uname -s)" = 'FreeBSD' ]; then
  86. # Tell cgo the compiler is Clang, not GCC
  87. # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
  88. export CC=clang
  89. # "-extld clang" is a workaround for
  90. # https://code.google.com/p/go/issues/detail?id=6845
  91. LDFLAGS="$LDFLAGS -extld clang"
  92. fi
  93. bundle() {
  94. local bundle="$1"
  95. shift
  96. echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
  97. source "$SCRIPTDIR/make/$bundle" "$@"
  98. }
  99. main() {
  100. bundle_dir="bundles"
  101. if [ -n "${PREFIX}" ]; then
  102. bundle_dir="${PREFIX}/${bundle_dir}"
  103. fi
  104. if [ -z "${KEEPBUNDLE-}" ]; then
  105. echo "Removing ${bundle_dir}/"
  106. rm -rf "${bundle_dir}"/*
  107. echo
  108. fi
  109. mkdir -p "${bundle_dir}"
  110. if [ $# -lt 1 ]; then
  111. bundles=(${DEFAULT_BUNDLES[@]})
  112. else
  113. bundles=($@)
  114. fi
  115. for bundle in ${bundles[@]}; do
  116. export DEST="${bundle_dir}/$(basename "$bundle")"
  117. # Cygdrive paths don't play well with go build -o.
  118. if [[ "$(uname -s)" == CYGWIN* ]]; then
  119. export DEST="$(cygpath -mw "$DEST")"
  120. fi
  121. mkdir -p "$DEST"
  122. ABS_DEST="$(cd "$DEST" && pwd -P)"
  123. bundle "$bundle"
  124. echo
  125. done
  126. }
  127. main "$@"