.binary 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env bash
  2. set -e
  3. # a helper to provide ".exe" when it's appropriate
  4. binary_extension() {
  5. if [ "$(go env GOOS)" = 'windows' ]; then
  6. echo -n '.exe'
  7. fi
  8. }
  9. BINARY_NAME="$BINARY_SHORT_NAME-$VERSION"
  10. BINARY_EXTENSION="$(binary_extension)"
  11. BINARY_FULLNAME="$BINARY_NAME$BINARY_EXTENSION"
  12. source "${MAKEDIR}/.go-autogen"
  13. hash_files() {
  14. while [ $# -gt 0 ]; do
  15. f="$1"
  16. shift
  17. dir="$(dirname "$f")"
  18. base="$(basename "$f")"
  19. for hashAlgo in md5 sha256; do
  20. if command -v "${hashAlgo}sum" &> /dev/null; then
  21. (
  22. # subshell and cd so that we get output files like:
  23. # $HASH docker-$VERSION
  24. # instead of:
  25. # $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
  26. cd "$dir"
  27. "${hashAlgo}sum" "$base" > "$base.$hashAlgo"
  28. )
  29. fi
  30. done
  31. done
  32. }
  33. (
  34. export GOGC=${DOCKER_BUILD_GOGC:-1000}
  35. if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
  36. # must be cross-compiling!
  37. case "$(go env GOOS)/$(go env GOARCH)" in
  38. windows/amd64)
  39. export CC="${CC:-x86_64-w64-mingw32-gcc}"
  40. export CGO_ENABLED=1
  41. ;;
  42. linux/arm)
  43. case "${GOARM}" in
  44. 5 | "")
  45. export CC="${CC:-arm-linux-gnueabi-gcc}"
  46. export CGO_ENABLED=1
  47. ;;
  48. 7)
  49. export CC="${CC:-arm-linux-gnueabihf-gcc}"
  50. export CGO_ENABLED=1
  51. ;;
  52. esac
  53. ;;
  54. linux/arm64)
  55. export CC="${CC:-aarch64-linux-gnu-gcc}"
  56. export CGO_ENABLED=1
  57. ;;
  58. linux/amd64)
  59. export CC="${CC:-x86_64-linux-gnu-gcc}"
  60. export CGO_ENABLED=1
  61. ;;
  62. linux/ppc64le)
  63. export CC="${CC:-powerpc64le-linux-gnu-gcc}"
  64. export CGO_ENABLED=1
  65. ;;
  66. linux/s390x)
  67. export CC="${CC:-s390x-linux-gnu-gcc}"
  68. export CGO_ENABLED=1
  69. ;;
  70. esac
  71. fi
  72. # -buildmode=pie is not supported on Windows and Linux on mips, riscv64 and ppc64be.
  73. # https://github.com/golang/go/blob/77aa209b386a184e7f4b44938f2a05a1b5c5a3cf/src/cmd/internal/sys/supported.go#L89-L99
  74. case "$(go env GOOS)/$(go env GOARCH)" in
  75. windows/* | linux/mips* | linux/riscv* | linux/ppc64) ;;
  76. # TODO remove windows in Go 1.15+: https://github.com/golang/go/commit/95f382139043059a2a0780ba577b53893408f7e4
  77. # TODO remove riscv64 in Go 1.16+: https://github.com/golang/go/commit/8eb846fd37eb7bded8a1cf6932be2c59069863e5
  78. *)
  79. BUILDFLAGS+=("-buildmode=pie")
  80. ;;
  81. esac
  82. echo "Building: $DEST/$BINARY_FULLNAME"
  83. echo "GOOS=\"${GOOS}\" GOARCH=\"${GOARCH}\" GOARM=\"${GOARM}\""
  84. go build \
  85. -o "$DEST/$BINARY_FULLNAME" \
  86. "${BUILDFLAGS[@]}" \
  87. -ldflags "
  88. $LDFLAGS
  89. $LDFLAGS_STATIC_DOCKER
  90. $DOCKER_LDFLAGS
  91. " \
  92. ${GO_PACKAGE}
  93. )
  94. echo "Created binary: $DEST/$BINARY_FULLNAME"
  95. ln -sf "$BINARY_FULLNAME" "$DEST/$BINARY_SHORT_NAME$BINARY_EXTENSION"
  96. hash_files "$DEST/$BINARY_FULLNAME"