build.sh 8.5 KB

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