build.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. sed -e 's+// __DO_NOT_REMOVE__NEEDED_FOR_REPLACING__IMPORT_PATH__FOR_CODEC_BENCH__+import . "github.com/ugorji/go/codec"+' \
  103. shared_test.go > bench/shared_test.go
  104. # explicitly return 0 if this passes, else return 1
  105. go run -tags "notfastpath safe codecgen.exec" gen-from-tmpl.generated.go &&
  106. rm -f gen-from-tmpl.*generated.go &&
  107. return 0
  108. return 1
  109. }
  110. _codegenerators() {
  111. local c5="_generated_test.go"
  112. local c7="$PWD/codecgen"
  113. local c8="$c7/__codecgen"
  114. local c9="codecgen-scratch.go"
  115. if ! [[ $zforce || $(_ng "values_codecgen${c5}") ]]; then return 0; fi
  116. # Note: ensure you run the codecgen for this codebase/directory i.e. ./codecgen/codecgen
  117. true &&
  118. echo "codecgen ... " &&
  119. if [[ $zforce || ! -f "$c8" || "$c7/gen.go" -nt "$c8" ]]; then
  120. echo "rebuilding codecgen ... " && ( cd codecgen && go build -o $c8 ${zargs[*]} . )
  121. fi &&
  122. $c8 -rt codecgen -t 'codecgen generated' -o values_codecgen${c5} -d 19780 $zfin $zfin2 &&
  123. cp mammoth2_generated_test.go $c9 &&
  124. $c8 -t '!notfastpath' -o mammoth2_codecgen${c5} -d 19781 mammoth2_generated_test.go &&
  125. rm -f $c9 &&
  126. echo "generators done!"
  127. }
  128. _prebuild() {
  129. echo "prebuild: zforce: $zforce"
  130. local d="$PWD"
  131. zfin="test_values.generated.go"
  132. zfin2="test_values_flex.generated.go"
  133. zpkg="github.com/ugorji/go/codec"
  134. # zpkg=${d##*/src/}
  135. # zgobase=${d%%/src/*}
  136. # rm -f *_generated_test.go
  137. rm -f codecgen-*.go &&
  138. _build &&
  139. cp $d/values_test.go $d/$zfin &&
  140. cp $d/values_flex_test.go $d/$zfin2 &&
  141. _codegenerators &&
  142. if [[ "$(type -t _codegenerators_external )" = "function" ]]; then _codegenerators_external ; fi &&
  143. if [[ $zforce ]]; then go install ${zargs[*]} .; fi &&
  144. echo "prebuild done successfully"
  145. rm -f $d/$zfin $d/$zfin2
  146. unset zfin zfin2 zpkg
  147. }
  148. _make() {
  149. zforce=1
  150. (cd codecgen && go install ${zargs[*]} .) && _prebuild && go install ${zargs[*]} .
  151. unset zforce
  152. }
  153. _clean() {
  154. rm -f gen-from-tmpl.*generated.go \
  155. codecgen-*.go \
  156. test_values.generated.go test_values_flex.generated.go
  157. }
  158. _release() {
  159. local reply
  160. read -p "Pre-release validation takes a few minutes and MUST be run from within GOPATH/src. Confirm y/n? " -n 1 -r reply
  161. echo
  162. if [[ ! $reply =~ ^[Yy]$ ]]; then return 1; fi
  163. # expects GOROOT, GOROOT_BOOTSTRAP to have been set.
  164. if [[ -z "${GOROOT// }" || -z "${GOROOT_BOOTSTRAP// }" ]]; then return 1; fi
  165. # (cd $GOROOT && git checkout -f master && git pull && git reset --hard)
  166. (cd $GOROOT && git pull)
  167. local f=`pwd`/make.release.out
  168. cat > $f <<EOF
  169. ========== `date` ===========
  170. EOF
  171. # # go 1.6 and below kept giving memory errors on Mac OS X during SDK build or go run execution,
  172. # # that is fine, as we only explicitly test the last 3 releases and tip (2 years).
  173. zforce=1
  174. for i in 1.10 1.11 1.12 master
  175. do
  176. echo "*********** $i ***********" >>$f
  177. if [[ "$i" != "master" ]]; then i="release-branch.go$i"; fi
  178. (false ||
  179. (echo "===== BUILDING GO SDK for branch: $i ... =====" &&
  180. cd $GOROOT &&
  181. git checkout -f $i && git reset --hard && git clean -f . &&
  182. cd src && ./make.bash >>$f 2>&1 && sleep 1 ) ) &&
  183. echo "===== GO SDK BUILD DONE =====" &&
  184. _prebuild &&
  185. echo "===== PREBUILD DONE with exit: $? =====" &&
  186. _tests "$@"
  187. if [[ "$?" != 0 ]]; then return 1; fi
  188. done
  189. unset zforce
  190. echo "++++++++ RELEASE TEST SUITES ALL PASSED ++++++++"
  191. }
  192. _usage() {
  193. cat <<EOF
  194. primary usage: $0
  195. -[tmpfxnld] -> [tests, make, prebuild (force) (external), inlining diagnostics, mid-stack inlining, race detector]
  196. -v -> verbose
  197. EOF
  198. if [[ "$(type -t _usage_run)" = "function" ]]; then _usage_run ; fi
  199. }
  200. _main() {
  201. if [[ -z "$1" ]]; then _usage; return 1; fi
  202. local x
  203. unset zforce
  204. zargs=()
  205. zbenchflags=""
  206. OPTIND=1
  207. while getopts ":ctmnrgpfvlzdb:" flag
  208. do
  209. case "x$flag" in
  210. 'xf') zforce=1 ;;
  211. 'xv') zverbose=1 ;;
  212. 'xl') zargs+=("-gcflags"); zargs+=("-l=4") ;;
  213. 'xn') zargs+=("-gcflags"); zargs+=("-m=2") ;;
  214. 'xd') zargs+=("-race") ;;
  215. 'xb') x='b'; zbenchflags=${OPTARG} ;;
  216. x\?) _usage; return 1 ;;
  217. *) x=$flag ;;
  218. esac
  219. done
  220. shift $((OPTIND-1))
  221. # echo ">>>> _main: extra args: $@"
  222. case "x$x" in
  223. 'xt') _tests "$@" ;;
  224. 'xm') _make "$@" ;;
  225. 'xr') _release "$@" ;;
  226. 'xg') _go ;;
  227. 'xp') _prebuild "$@" ;;
  228. 'xc') _clean "$@" ;;
  229. 'xz') _analyze "$@" ;;
  230. 'xb') _bench "$@" ;;
  231. esac
  232. unset zforce zargs zbenchflags
  233. }
  234. [ "." = `dirname $0` ] && _main "$@"