build.sh 3.4 KB

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