build.sh 8.1 KB

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