另客网go项目公用的代码库

build.sh 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 ||
  110. $(_ng "values_codecgen${zsfx}") ]]; then return 0; fi
  111. # Note: ensure you run the codecgen for this codebase (using $zgobase/bin/codecgen)
  112. local c9="codecgen-scratch.go"
  113. true &&
  114. echo "codecgen ... " &&
  115. $zgobase/bin/codecgen -rt codecgen -t 'codecgen generated' -o values_codecgen${zsfx} -d 19780 $zfin $zfin2 &&
  116. cp mammoth2_generated_test.go $c9 &&
  117. $zgobase/bin/codecgen -t '!notfastpath' -o mammoth2_codecgen${zsfx} -d 19781 mammoth2_generated_test.go &&
  118. rm -f $c9 &&
  119. echo "generators done!"
  120. }
  121. _prebuild() {
  122. echo "prebuild: zforce: $zforce , zexternal: $zexternal"
  123. zmydir=`pwd`
  124. zfin="test_values.generated.go"
  125. zfin2="test_values_flex.generated.go"
  126. zsfx="_generated_test.go"
  127. # zpkg="ugorji.net/codec"
  128. zpkg=${zmydir##*/src/}
  129. zgobase=${zmydir%%/src/*}
  130. # rm -f *_generated_test.go
  131. rm -f codecgen-*.go &&
  132. _build &&
  133. cp $zmydir/values_test.go $zmydir/$zfin &&
  134. cp $zmydir/values_flex_test.go $zmydir/$zfin2 &&
  135. _codegenerators &&
  136. if [[ "$(type -t _codegenerators_external )" = "function" ]]; then _codegenerators_external ; fi &&
  137. if [[ $zforce ]]; then go install ${zargs[*]} .; fi &&
  138. echo "prebuild done successfully"
  139. rm -f $zmydir/$zfin $zmydir/$zfin2
  140. }
  141. _make() {
  142. zforce=1
  143. zexternal=1
  144. ( cd codecgen && go install ${zargs[*]} . ) && _prebuild && go install ${zargs[*]} .
  145. unset zforce zexternal
  146. }
  147. _clean() {
  148. rm -f gen-from-tmpl.*generated.go \
  149. codecgen-*.go \
  150. test_values.generated.go test_values_flex.generated.go
  151. }
  152. _usage() {
  153. cat <<EOF
  154. primary usage: $0
  155. -[tmpfxnld] -> [tests, make, prebuild (force) (external), inlining diagnostics, mid-stack inlining, race detector]
  156. -v -> verbose
  157. EOF
  158. if [[ "$(type -t _usage_run)" = "function" ]]; then _usage_run ; fi
  159. }
  160. _main() {
  161. if [[ -z "$1" ]]; then _usage; return 1; fi
  162. local x
  163. unset zforce zexternal
  164. zargs=()
  165. zbenchflags=""
  166. OPTIND=1
  167. while getopts ":ctmnrgupfvxlzdb:" flag
  168. do
  169. case "x$flag" in
  170. 'xf') zforce=1 ;;
  171. 'xx') zexternal=1 ;;
  172. 'xv') zverbose=1 ;;
  173. 'xl') zargs+=("-gcflags"); zargs+=("-l=4") ;;
  174. 'xn') zargs+=("-gcflags"); zargs+=("-m=2") ;;
  175. 'xd') zargs+=("-race") ;;
  176. 'xb') x='b'; zbenchflags=${OPTARG} ;;
  177. x\?) _usage; return 1 ;;
  178. *) x=$flag ;;
  179. esac
  180. done
  181. shift $((OPTIND-1))
  182. # echo ">>>> _main: extra args: $@"
  183. case "x$x" in
  184. 'xt') _tests "$@" ;;
  185. 'xm') _make "$@" ;;
  186. 'xr') _release "$@" ;;
  187. 'xg') _go ;;
  188. 'xu') _githubupdate ;;
  189. 'xp') _prebuild "$@" ;;
  190. 'xc') _clean "$@" ;;
  191. 'xz') _analyze "$@" ;;
  192. 'xb') _bench "$@" ;;
  193. esac
  194. unset zforce zexternal
  195. }
  196. [ "." = `dirname $0` ] && _main "$@"