build.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env bash
  2. export COPYFILE_DISABLE=true
  3. export GO386=387 # Don't use SSE on 32 bit builds
  4. distFiles=(README.md LICENSE CONTRIBUTORS) # apart from the binary itself
  5. version=$(git describe --always --dirty)
  6. date=$(git show -s --format=%ct)
  7. user=$(whoami)
  8. host=$(hostname)
  9. host=${host%%.*}
  10. ldflags="-w -X main.Version $version -X main.BuildStamp $date -X main.BuildUser $user -X main.BuildHost $host"
  11. check() {
  12. if ! command -v godep >/dev/null ; then
  13. echo "Error: no godep. Try \"$0 setup\"."
  14. exit 1
  15. fi
  16. }
  17. build() {
  18. check
  19. go vet ./...
  20. godep go build $* -ldflags "$ldflags" ./cmd/syncthing
  21. }
  22. assets() {
  23. check
  24. godep go run cmd/assets/assets.go gui > auto/gui.files.go
  25. }
  26. test() {
  27. check
  28. godep go test -cpu=1,2,4 ./...
  29. }
  30. sign() {
  31. if git describe --exact-match 2>/dev/null >/dev/null ; then
  32. # HEAD is a tag
  33. id=BCE524C7
  34. if gpg --list-keys "$id" >/dev/null 2>&1 ; then
  35. gpg -ab -u "$id" "$1"
  36. fi
  37. fi
  38. }
  39. tarDist() {
  40. name="$1"
  41. rm -rf "$name"
  42. mkdir -p "$name"
  43. cp syncthing "${distFiles[@]}" "$name"
  44. sign "$name/syncthing"
  45. tar zcvf "$name.tar.gz" "$name"
  46. rm -rf "$name"
  47. }
  48. zipDist() {
  49. name="$1"
  50. rm -rf "$name"
  51. mkdir -p "$name"
  52. for f in "${distFiles[@]}" ; do
  53. sed 's/$/ /' < "$f" > "$name/$f.txt"
  54. done
  55. cp syncthing.exe "$name"
  56. sign "$name/syncthing.exe"
  57. zip -r "$name.zip" "$name"
  58. rm -rf "$name"
  59. }
  60. deps() {
  61. check
  62. godep save ./cmd/syncthing ./cmd/assets ./discover/cmd/discosrv
  63. }
  64. setup() {
  65. echo Installing godep...
  66. go get -u github.com/tools/godep
  67. echo Installing go vet...
  68. go get -u code.google.com/p/go.tools/cmd/vet
  69. }
  70. case "$1" in
  71. "")
  72. shift
  73. build $*
  74. ;;
  75. race)
  76. build -race
  77. ;;
  78. guidev)
  79. echo "Syncthing is already built for GUI developments. Try:"
  80. echo " STGUIASSETS=~/someDir/gui syncthing"
  81. ;;
  82. test)
  83. test
  84. ;;
  85. tar)
  86. rm -f *.tar.gz *.zip
  87. test || exit 1
  88. assets
  89. build
  90. eval $(go env)
  91. name="syncthing-$GOOS-$GOARCH-$version"
  92. tarDist "$name"
  93. ;;
  94. all)
  95. rm -f *.tar.gz *.zip
  96. test || exit 1
  97. assets
  98. godep go build ./discover/cmd/discosrv
  99. godep go build ./cmd/stpidx
  100. godep go build ./cmd/stcli
  101. for os in darwin-amd64 linux-386 linux-amd64 freebsd-amd64 windows-amd64 windows-386 ; do
  102. export GOOS=${os%-*}
  103. export GOARCH=${os#*-}
  104. build
  105. name="syncthing-$os-$version"
  106. case $GOOS in
  107. windows)
  108. zipDist "$name"
  109. rm -f syncthing.exe
  110. ;;
  111. *)
  112. tarDist "$name"
  113. rm -f syncthing
  114. ;;
  115. esac
  116. done
  117. export GOOS=linux
  118. export GOARCH=arm
  119. origldflags="$ldflags"
  120. export GOARM=7
  121. ldflags="$origldflags -X main.GoArchExtra v7"
  122. build
  123. tarDist "syncthing-linux-armv7-$version"
  124. export GOARM=6
  125. ldflags="$origldflags -X main.GoArchExtra v6"
  126. build
  127. tarDist "syncthing-linux-armv6-$version"
  128. export GOARM=5
  129. ldflags="$origldflags -X main.GoArchExtra v5"
  130. build
  131. tarDist "syncthing-linux-armv5-$version"
  132. ;;
  133. upload)
  134. tag=$(git describe)
  135. shopt -s nullglob
  136. for f in *.tar.gz *.zip *.asc ; do
  137. relup calmh/syncthing "$tag" "$f"
  138. done
  139. ;;
  140. deps)
  141. deps
  142. ;;
  143. assets)
  144. assets
  145. ;;
  146. setup)
  147. setup
  148. ;;
  149. *)
  150. echo "Unknown build parameter $1"
  151. ;;
  152. esac