make.sh 4.6 KB

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