make.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/bin/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. # (http://github.com/dotcloud/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 container image.
  21. #
  22. set -o pipefail
  23. # We're a nice, sexy, little shell script, and people might try to run us;
  24. # but really, they shouldn't. We want to be in a container!
  25. RESOLVCONF=$(readlink --canonicalize /etc/resolv.conf)
  26. grep -q "$RESOLVCONF" /proc/mounts || {
  27. echo >&2 "# WARNING! I don't seem to be running in a docker container."
  28. echo >&2 "# The result of this command might be an incorrect build, and will not be officially supported."
  29. echo >&2 "# Try this: 'make all'"
  30. }
  31. # List of bundles to create when no argument is passed
  32. DEFAULT_BUNDLES=(
  33. binary
  34. test
  35. test-integration
  36. dynbinary
  37. dyntest
  38. dyntest-integration
  39. cover
  40. cross
  41. tgz
  42. ubuntu
  43. )
  44. VERSION=$(cat ./VERSION)
  45. if [ -d .git ] && command -v git &> /dev/null; then
  46. GITCOMMIT=$(git rev-parse --short HEAD)
  47. if [ -n "$(git status --porcelain)" ]; then
  48. GITCOMMIT="$GITCOMMIT-dirty"
  49. fi
  50. elif [ "$DOCKER_GITCOMMIT" ]; then
  51. GITCOMMIT="$DOCKER_GITCOMMIT"
  52. else
  53. echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
  54. echo >&2 ' Please either build with the .git directory accessible, or specify the'
  55. echo >&2 ' exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
  56. echo >&2 ' future accountability in diagnosing build issues. Thanks!'
  57. exit 1
  58. fi
  59. # Use these flags when compiling the tests and final binary
  60. LDFLAGS='-X main.GITCOMMIT "'$GITCOMMIT'" -X main.VERSION "'$VERSION'" -w'
  61. LDFLAGS_STATIC='-X github.com/dotcloud/docker/utils.IAMSTATIC true -linkmode external -extldflags "-lpthread -static -Wl,--unresolved-symbols=ignore-in-object-files"'
  62. BUILDFLAGS='-tags netgo -a'
  63. HAVE_GO_TEST_COVER=
  64. if \
  65. go help testflag | grep -- -cover > /dev/null \
  66. && go tool -n cover > /dev/null 2>&1 \
  67. ; then
  68. HAVE_GO_TEST_COVER=1
  69. fi
  70. # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
  71. # You can use this to select certain tests to run, eg.
  72. #
  73. # TESTFLAGS='-run ^TestBuild$' ./hack/make.sh test
  74. #
  75. go_test_dir() {
  76. dir=$1
  77. testcover=()
  78. if [ "$HAVE_GO_TEST_COVER" ]; then
  79. # if our current go install has -cover, we want to use it :)
  80. mkdir -p "$DEST/coverprofiles"
  81. coverprofile="docker${dir#.}"
  82. coverprofile="$DEST/coverprofiles/${coverprofile//\//-}"
  83. testcover=( -cover -coverprofile "$coverprofile" )
  84. fi
  85. (
  86. set -x
  87. cd "$dir"
  88. go test ${testcover[@]} -ldflags "$LDFLAGS" $BUILDFLAGS $TESTFLAGS
  89. )
  90. }
  91. bundle() {
  92. bundlescript=$1
  93. bundle=$(basename $bundlescript)
  94. echo "---> Making bundle: $bundle (in bundles/$VERSION/$bundle)"
  95. mkdir -p bundles/$VERSION/$bundle
  96. source $bundlescript $(pwd)/bundles/$VERSION/$bundle
  97. }
  98. main() {
  99. # We want this to fail if the bundles already exist and cannot be removed.
  100. # This is to avoid mixing bundles from different versions of the code.
  101. mkdir -p bundles
  102. if [ -e "bundles/$VERSION" ]; then
  103. echo "bundles/$VERSION already exists. Removing."
  104. rm -fr bundles/$VERSION && mkdir bundles/$VERSION || exit 1
  105. echo
  106. fi
  107. SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  108. if [ $# -lt 1 ]; then
  109. bundles=(${DEFAULT_BUNDLES[@]})
  110. else
  111. bundles=($@)
  112. fi
  113. for bundle in ${bundles[@]}; do
  114. bundle $SCRIPTDIR/make/$bundle
  115. echo
  116. done
  117. }
  118. main "$@"