buildall.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh -e
  2. # buildall.sh $SRC_DIR $DST_DIR $SUFFIX
  3. #
  4. # Builds Elvish binaries for all supported platforms, using the code in $SRC_DIR
  5. # and building $DST_DIR/$GOOS-$GOARCH/elvish-$SUFFIX for each supported
  6. # combination of $GOOS and $GOARCH.
  7. #
  8. # It also creates an archive for each binary file, and puts it in the same
  9. # directory. For GOOS=windows, the archive is a .zip file. For all other GOOS,
  10. # the archive is a .tar.gz file.
  11. #
  12. # If the sha256sum command is available, this script also creates a sha256sum
  13. # file for each binary and archive file, and puts it in the same directory.
  14. #
  15. # The value of the ELVISH_BUILD_VARIANT environment variable will be used to
  16. # override src.elv.sh/pkg/buildinfo.BuildVariant.
  17. #
  18. # This script is not whitespace-correct; avoid whitespaces in directory names.
  19. if test $# != 3; then
  20. # Output the comment block above, stripping any leading "#" or "# "
  21. sed < $0 -En '
  22. /^# /,/^$/{
  23. /^$/q
  24. s/^# ?//
  25. p
  26. }'
  27. exit 1
  28. fi
  29. SRC_DIR=$1
  30. DST_DIR=$2
  31. SUFFIX=$3
  32. export GOOS GOARCH GOFLAGS
  33. export CGO_ENABLED=0
  34. main() {
  35. buildarch amd64 linux darwin freebsd openbsd netbsd windows
  36. buildarch 386 linux windows
  37. buildarch arm64 linux darwin
  38. }
  39. # buildarch $arch $os...
  40. #
  41. # Builds one GOARCH, multiple GOOS.
  42. buildarch() {
  43. local GOARCH=$1 GOOS
  44. shift
  45. for GOOS in $@; do
  46. buildone
  47. done
  48. }
  49. # buildone
  50. #
  51. # Builds one GOARCH and one GOOS.
  52. #
  53. # Uses: $GOARCH $GOOS $DST_DIR
  54. buildone() {
  55. local BIN_DIR=$DST_DIR/$GOOS-$GOARCH
  56. mkdir -p $BIN_DIR
  57. local STEM=elvish-$SUFFIX
  58. if test $GOOS = windows; then
  59. local BIN=$STEM.exe
  60. local ARCHIVE=$STEM.zip
  61. else
  62. local BIN=$STEM
  63. local ARCHIVE=$STEM.tar.gz
  64. fi
  65. if go env GOOS GOARCH | egrep -qx '(windows .*|linux (amd64|arm64))'; then
  66. local GOFLAGS=-buildmode=pie
  67. fi
  68. printf '%s' "Building for $GOOS-$GOARCH... "
  69. go build \
  70. -trimpath \
  71. -ldflags "-X src.elv.sh/pkg/buildinfo.BuildVariant=$ELVISH_BUILD_VARIANT" \
  72. -o $BIN_DIR/$BIN $SRC_DIR/cmd/elvish || {
  73. echo "Failed"
  74. return
  75. }
  76. (
  77. cd $BIN_DIR
  78. if test $GOOS = windows; then
  79. zip -q $ARCHIVE $BIN
  80. else
  81. tar cfz $ARCHIVE $BIN
  82. fi
  83. echo "Done"
  84. if which sha256sum > /dev/null; then
  85. sha256sum $BIN > $BIN.sha256sum
  86. sha256sum $ARCHIVE > $ARCHIVE.sha256sum
  87. fi
  88. )
  89. }
  90. main