build.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/bin/bash
  2. # Run all the different permutations of all the tests and other things
  3. # This helps ensure that nothing gets broken.
  4. _tests() {
  5. local vet="" # TODO: make it off
  6. local gover=$( go version | cut -f 3 -d ' ' )
  7. # note that codecgen requires fastpath, so you cannot do "codecgen notfastpath"
  8. local a=( "" "safe" "notfastpath" "notfastpath safe" "codecgen" "codecgen safe" )
  9. for i in "${a[@]}"
  10. do
  11. echo ">>>> TAGS: $i"
  12. local i2=${i:-default}
  13. case $gover in
  14. go1.[0-6]*) go vet -printfuncs "errorf" "$@" &&
  15. go test ${zargs[*]} -vet "$vet" -tags "$i" "$@" ;;
  16. *) go vet -printfuncs "errorf" "$@" &&
  17. go test ${zargs[*]} -vet "$vet" -tags "alltests $i" -run "Suite" -coverprofile "${i2// /-}.cov.out" "$@" ;;
  18. esac
  19. if [[ "$?" != 0 ]]; then return 1; fi
  20. done
  21. echo "++++++++ TEST SUITES ALL PASSED ++++++++"
  22. }
  23. # is a generation needed?
  24. _ng() {
  25. local a="$1"
  26. if [[ ! -e "$a" ]]; then echo 1; return; fi
  27. for i in `ls -1 *.go.tmpl gen.go values_test.go`
  28. do
  29. if [[ "$a" -ot "$i" ]]; then echo 1; return; fi
  30. done
  31. }
  32. _prependbt() {
  33. cat > ${2} <<EOF
  34. // +build generated
  35. EOF
  36. cat ${1} >> ${2}
  37. rm -f ${1}
  38. }
  39. # _build generates fast-path.go and gen-helper.go.
  40. _build() {
  41. if ! [[ "${zforce}" || $(_ng "fast-path.generated.go") || $(_ng "gen-helper.generated.go") || $(_ng "gen.generated.go") ]]; then return 0; fi
  42. if [ "${zbak}" ]; then
  43. _zts=`date '+%m%d%Y_%H%M%S'`
  44. _gg=".generated.go"
  45. [ -e "gen-helper${_gg}" ] && mv gen-helper${_gg} gen-helper${_gg}__${_zts}.bak
  46. [ -e "fast-path${_gg}" ] && mv fast-path${_gg} fast-path${_gg}__${_zts}.bak
  47. [ -e "gen${_gg}" ] && mv gen${_gg} gen${_gg}__${_zts}.bak
  48. fi
  49. rm -f gen-helper.generated.go fast-path.generated.go gen.generated.go \
  50. *safe.generated.go *_generated_test.go *.generated_ffjson_expose.go
  51. cat > gen.generated.go <<EOF
  52. // +build codecgen.exec
  53. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  54. // Use of this source code is governed by a MIT license found in the LICENSE file.
  55. package codec
  56. // DO NOT EDIT. THIS FILE IS AUTO-GENERATED FROM gen-dec-(map|array).go.tmpl
  57. const genDecMapTmpl = \`
  58. EOF
  59. cat >> gen.generated.go < gen-dec-map.go.tmpl
  60. cat >> gen.generated.go <<EOF
  61. \`
  62. const genDecListTmpl = \`
  63. EOF
  64. cat >> gen.generated.go < gen-dec-array.go.tmpl
  65. cat >> gen.generated.go <<EOF
  66. \`
  67. const genEncChanTmpl = \`
  68. EOF
  69. cat >> gen.generated.go < gen-enc-chan.go.tmpl
  70. cat >> gen.generated.go <<EOF
  71. \`
  72. EOF
  73. cat > gen-from-tmpl.codec.generated.go <<EOF
  74. package codec
  75. import "io"
  76. func GenInternalGoFile(r io.Reader, w io.Writer) error {
  77. return genInternalGoFile(r, w)
  78. }
  79. EOF
  80. cat > gen-from-tmpl.generated.go <<EOF
  81. //+build ignore
  82. package main
  83. import "${zpkg}"
  84. import "os"
  85. func run(fnameIn, fnameOut string) {
  86. println("____ " + fnameIn + " --> " + fnameOut + " ______")
  87. fin, err := os.Open(fnameIn)
  88. if err != nil { panic(err) }
  89. defer fin.Close()
  90. fout, err := os.Create(fnameOut)
  91. if err != nil { panic(err) }
  92. defer fout.Close()
  93. err = codec.GenInternalGoFile(fin, fout)
  94. if err != nil { panic(err) }
  95. }
  96. func main() {
  97. run("fast-path.go.tmpl", "fast-path.generated.go")
  98. run("gen-helper.go.tmpl", "gen-helper.generated.go")
  99. run("mammoth-test.go.tmpl", "mammoth_generated_test.go")
  100. run("mammoth2-test.go.tmpl", "mammoth2_generated_test.go")
  101. // run("sort-slice.go.tmpl", "sort-slice.generated.go")
  102. }
  103. EOF
  104. sed -e 's+// __DO_NOT_REMOVE__NEEDED_FOR_REPLACING__IMPORT_PATH__FOR_CODEC_BENCH__+import . "github.com/ugorji/go/codec"+' \
  105. shared_test.go > bench/shared_test.go
  106. # explicitly return 0 if this passes, else return 1
  107. go run -tags "prebuild" prebuild.go || return 1
  108. go run -tags "notfastpath safe codecgen.exec" gen-from-tmpl.generated.go || return 1
  109. rm -f gen-from-tmpl.*generated.go
  110. return 0
  111. }
  112. _codegenerators() {
  113. local c5="_generated_test.go"
  114. local c7="$PWD/codecgen"
  115. local c8="$c7/__codecgen"
  116. local c9="codecgen-scratch.go"
  117. if ! [[ $zforce || $(_ng "values_codecgen${c5}") ]]; then return 0; fi
  118. # Note: ensure you run the codecgen for this codebase/directory i.e. ./codecgen/codecgen
  119. true &&
  120. echo "codecgen ... " &&
  121. if [[ $zforce || ! -f "$c8" || "$c7/gen.go" -nt "$c8" ]]; then
  122. echo "rebuilding codecgen ... " && ( cd codecgen && go build -o $c8 ${zargs[*]} . )
  123. fi &&
  124. $c8 -rt codecgen -t 'codecgen generated' -o values_codecgen${c5} -d 19780 $zfin $zfin2 &&
  125. cp mammoth2_generated_test.go $c9 &&
  126. $c8 -t 'codecgen,!notfastpath generated,!notfastpath' -o mammoth2_codecgen${c5} -d 19781 mammoth2_generated_test.go &&
  127. rm -f $c9 &&
  128. echo "generators done!"
  129. }
  130. _prebuild() {
  131. echo "prebuild: zforce: $zforce"
  132. local d="$PWD"
  133. local zfin="test_values.generated.go"
  134. local zfin2="test_values_flex.generated.go"
  135. local zpkg="github.com/ugorji/go/codec"
  136. # zpkg=${d##*/src/}
  137. # zgobase=${d%%/src/*}
  138. # rm -f *_generated_test.go
  139. rm -f codecgen-*.go &&
  140. _build &&
  141. cp $d/values_test.go $d/$zfin &&
  142. cp $d/values_flex_test.go $d/$zfin2 &&
  143. _codegenerators &&
  144. if [[ "$(type -t _codegenerators_external )" = "function" ]]; then _codegenerators_external ; fi &&
  145. if [[ $zforce ]]; then go install ${zargs[*]} .; fi &&
  146. echo "prebuild done successfully"
  147. rm -f $d/$zfin $d/$zfin2
  148. # unset zfin zfin2 zpkg
  149. }
  150. _make() {
  151. local makeforce=${zforce}
  152. zforce=1
  153. (cd codecgen && go install ${zargs[*]} .) && _prebuild && go install ${zargs[*]} .
  154. zforce=${makeforce}
  155. }
  156. _clean() {
  157. rm -f gen-from-tmpl.*generated.go \
  158. codecgen-*.go \
  159. test_values.generated.go test_values_flex.generated.go
  160. }
  161. _release() {
  162. local reply
  163. read -p "Pre-release validation takes a few minutes and MUST be run from within GOPATH/src. Confirm y/n? " -n 1 -r reply
  164. echo
  165. if [[ ! $reply =~ ^[Yy]$ ]]; then return 1; fi
  166. # expects GOROOT, GOROOT_BOOTSTRAP to have been set.
  167. if [[ -z "${GOROOT// }" || -z "${GOROOT_BOOTSTRAP// }" ]]; then return 1; fi
  168. # (cd $GOROOT && git checkout -f master && git pull && git reset --hard)
  169. (cd $GOROOT && git pull)
  170. local f=`pwd`/make.release.out
  171. cat > $f <<EOF
  172. ========== `date` ===========
  173. EOF
  174. # # go 1.6 and below kept giving memory errors on Mac OS X during SDK build or go run execution,
  175. # # that is fine, as we only explicitly test the last 3 releases and tip (2 years).
  176. local makeforce=${zforce}
  177. zforce=1
  178. for i in 1.10 1.11 1.12 master
  179. do
  180. echo "*********** $i ***********" >>$f
  181. if [[ "$i" != "master" ]]; then i="release-branch.go$i"; fi
  182. (false ||
  183. (echo "===== BUILDING GO SDK for branch: $i ... =====" &&
  184. cd $GOROOT &&
  185. git checkout -f $i && git reset --hard && git clean -f . &&
  186. cd src && ./make.bash >>$f 2>&1 && sleep 1 ) ) &&
  187. echo "===== GO SDK BUILD DONE =====" &&
  188. _prebuild &&
  189. echo "===== PREBUILD DONE with exit: $? =====" &&
  190. _tests "$@"
  191. if [[ "$?" != 0 ]]; then return 1; fi
  192. done
  193. zforce=${makeforce}
  194. echo "++++++++ RELEASE TEST SUITES ALL PASSED ++++++++"
  195. }
  196. _usage() {
  197. cat <<EOF
  198. primary usage: $0
  199. -[tmpfxnld] -> [tests, make, prebuild (force) (external), inlining diagnostics, mid-stack inlining, race detector]
  200. -v -> verbose
  201. EOF
  202. if [[ "$(type -t _usage_run)" = "function" ]]; then _usage_run ; fi
  203. }
  204. _main() {
  205. if [[ -z "$1" ]]; then _usage; return 1; fi
  206. local x
  207. local zforce
  208. local zargs=()
  209. local zverbose=()
  210. local zbenchflags
  211. # unset zforce
  212. zargs=()
  213. zbenchflags=""
  214. OPTIND=1
  215. while getopts ":ctmnrgpfvlzdb:" flag
  216. do
  217. case "x$flag" in
  218. 'xf') zforce=1 ;;
  219. 'xv') zverbose+=(1) ;;
  220. 'xl') zargs+=("-gcflags"); zargs+=("-l=4") ;;
  221. 'xn') zargs+=("-gcflags"); zargs+=("-m=2") ;;
  222. 'xd') zargs+=("-race") ;;
  223. 'xb') x='b'; zbenchflags=${OPTARG} ;;
  224. x\?) _usage; return 1 ;;
  225. *) x=$flag ;;
  226. esac
  227. done
  228. shift $((OPTIND-1))
  229. # echo ">>>> _main: extra args: $@"
  230. case "x$x" in
  231. 'xt') _tests "$@" ;;
  232. 'xm') _make "$@" ;;
  233. 'xr') _release "$@" ;;
  234. 'xg') _go ;;
  235. 'xp') _prebuild "$@" ;;
  236. 'xc') _clean "$@" ;;
  237. 'xz') _analyze "$@" ;;
  238. 'xb') _bench "$@" ;;
  239. esac
  240. # unset zforce zargs zbenchflags
  241. }
  242. [ "." = `dirname $0` ] && _main "$@"