build.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. godep go build $* -ldflags "$ldflags" ./cmd/syncthing
  21. godep go build $* -ldflags "$ldflags" ./cmd/discosrv
  22. }
  23. assets() {
  24. check
  25. godep go run cmd/genassets/main.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. go vet ./...
  42. godep go test -cpu=1,2,4 ./...
  43. }
  44. sign() {
  45. if git describe --exact-match 2>/dev/null >/dev/null ; then
  46. # HEAD is a tag
  47. id=BCE524C7
  48. if gpg --list-keys "$id" >/dev/null 2>&1 ; then
  49. gpg -ab -u "$id" "$1"
  50. fi
  51. fi
  52. }
  53. tarDist() {
  54. name="$1"
  55. rm -rf "$name"
  56. mkdir -p "$name"
  57. cp syncthing "${distFiles[@]}" "$name"
  58. sign "$name/syncthing"
  59. tar zcvf "$name.tar.gz" "$name"
  60. rm -rf "$name"
  61. }
  62. zipDist() {
  63. name="$1"
  64. rm -rf "$name"
  65. mkdir -p "$name"
  66. for f in "${distFiles[@]}" ; do
  67. GOARCH="" GOOS="" go run cmd/todos/main.go < "$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/...
  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. xdr() {
  85. for f in discover/packets files/leveldb protocol/message ; do
  86. go run cmd/genxdr/main.go -- "${f}.go" > "${f}_xdr.go"
  87. done
  88. }
  89. transifex() {
  90. pushd gui
  91. go run ../cmd/transifexdl/main.go
  92. popd
  93. }
  94. case "$1" in
  95. "")
  96. shift
  97. export GOBIN=$(pwd)/bin
  98. godep go install $* -ldflags "$ldflags" ./cmd/...
  99. ;;
  100. race)
  101. build -race
  102. ;;
  103. guidev)
  104. echo "Syncthing is already built for GUI developments. Try:"
  105. echo " STGUIASSETS=~/someDir/gui syncthing"
  106. ;;
  107. test)
  108. test
  109. ;;
  110. test-cov)
  111. test-cov
  112. ;;
  113. tar)
  114. rm -f *.tar.gz *.zip
  115. test || exit 1
  116. assets
  117. build
  118. eval $(go env)
  119. name="syncthing-$GOOS-$GOARCH-$version"
  120. tarDist "$name"
  121. ;;
  122. all)
  123. rm -f *.tar.gz *.zip
  124. test || exit 1
  125. assets
  126. for os in darwin-amd64 linux-386 linux-amd64 freebsd-amd64 windows-amd64 windows-386 solaris-amd64 ; do
  127. export GOOS=${os%-*}
  128. export GOARCH=${os#*-}
  129. build
  130. name="syncthing-$os-$version"
  131. case $GOOS in
  132. windows)
  133. zipDist "$name"
  134. rm -f syncthing.exe
  135. ;;
  136. *)
  137. tarDist "$name"
  138. rm -f syncthing
  139. ;;
  140. esac
  141. done
  142. export GOOS=linux
  143. export GOARCH=arm
  144. origldflags="$ldflags"
  145. export GOARM=7
  146. ldflags="$origldflags -X main.GoArchExtra v7"
  147. build
  148. tarDist "syncthing-linux-armv7-$version"
  149. export GOARM=6
  150. ldflags="$origldflags -X main.GoArchExtra v6"
  151. build
  152. tarDist "syncthing-linux-armv6-$version"
  153. export GOARM=5
  154. ldflags="$origldflags -X main.GoArchExtra v5"
  155. build
  156. tarDist "syncthing-linux-armv5-$version"
  157. ;;
  158. upload)
  159. tag=$(git describe)
  160. shopt -s nullglob
  161. for f in *.tar.gz *.zip *.asc ; do
  162. relup calmh/syncthing "$tag" "$f"
  163. done
  164. ;;
  165. deps)
  166. deps
  167. ;;
  168. assets)
  169. assets
  170. ;;
  171. setup)
  172. setup
  173. ;;
  174. xdr)
  175. xdr
  176. ;;
  177. transifex)
  178. transifex
  179. ;;
  180. *)
  181. echo "Unknown build parameter $1"
  182. ;;
  183. esac