prebuild.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/bin/bash
  2. # _needgen is a helper function to tell if we need to generate files for msgp, codecgen.
  3. _needgen() {
  4. local a="$1"
  5. zneedgen=0
  6. if [[ ! -e "$a" ]]
  7. then
  8. zneedgen=1
  9. echo 1
  10. return 0
  11. fi
  12. for i in `ls -1 *.go.tmpl gen.go values_test.go`
  13. do
  14. if [[ "$a" -ot "$i" ]]
  15. then
  16. zneedgen=1
  17. echo 1
  18. return 0
  19. fi
  20. done
  21. echo 0
  22. }
  23. # _build generates fast-path.go and gen-helper.go.
  24. #
  25. # It is needed because there is some dependency between the generated code
  26. # and the other classes. Consequently, we have to totally remove the
  27. # generated files and put stubs in place, before calling "go run" again
  28. # to recreate them.
  29. _build() {
  30. if ! [[ "${zforce}" == "1" ||
  31. "1" == $( _needgen "fast-path.generated.go" ) ||
  32. "1" == $( _needgen "gen-helper.generated.go" ) ||
  33. "1" == $( _needgen "gen.generated.go" ) ||
  34. 1 == 0 ]]
  35. then
  36. return 0
  37. fi
  38. # echo "Running prebuild"
  39. if [ "${zbak}" == "1" ]
  40. then
  41. # echo "Backing up old generated files"
  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 "safe${_gg}" ] && mv safe${_gg} safe${_gg}__${_zts}.bak
  47. # [ -e "unsafe${_gg}" ] && mv unsafe${_gg} unsafe${_gg}__${_zts}.bak
  48. else
  49. rm -f fast-path.generated.go gen.generated.go gen-helper.generated.go \
  50. *safe.generated.go *_generated_test.go *.generated_ffjson_expose.go
  51. fi
  52. cat > gen.generated.go <<EOF
  53. // Copyright (c) 2012-2015 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. EOF
  68. cat > gen-from-tmpl.codec.generated.go <<EOF
  69. package codec
  70. import "io"
  71. func GenInternalGoFile(r io.Reader, w io.Writer, safe bool) error {
  72. return genInternalGoFile(r, w, safe)
  73. }
  74. EOF
  75. cat > gen-from-tmpl.generated.go <<EOF
  76. //+build ignore
  77. package main
  78. //import "flag"
  79. import "ugorji.net/codec"
  80. import "os"
  81. func run(fnameIn, fnameOut string, safe bool) {
  82. fin, err := os.Open(fnameIn)
  83. if err != nil { panic(err) }
  84. defer fin.Close()
  85. fout, err := os.Create(fnameOut)
  86. if err != nil { panic(err) }
  87. defer fout.Close()
  88. err = codec.GenInternalGoFile(fin, fout, safe)
  89. if err != nil { panic(err) }
  90. }
  91. func main() {
  92. // do not make safe/unsafe variants.
  93. // Instead, depend on escape analysis, and place string creation and usage appropriately.
  94. // run("unsafe.go.tmpl", "safe.generated.go", true)
  95. // run("unsafe.go.tmpl", "unsafe.generated.go", false)
  96. run("fast-path.go.tmpl", "fast-path.generated.go", false)
  97. run("gen-helper.go.tmpl", "gen-helper.generated.go", false)
  98. }
  99. EOF
  100. go run -tags=notfastpath gen-from-tmpl.generated.go && \
  101. rm -f gen-from-tmpl.*generated.go
  102. }
  103. _codegenerators() {
  104. if [[ $zforce == "1" ||
  105. "1" == $( _needgen "values_codecgen${zsfx}" ) ||
  106. "1" == $( _needgen "values_msgp${zsfx}" ) ||
  107. "1" == $( _needgen "values_ffjson${zsfx}" ) ||
  108. 1 == 0 ]]
  109. then
  110. # codecgen creates some temporary files in the directory (main, pkg).
  111. # Consequently, we should start msgp and ffjson first, and also put a small time latency before
  112. # starting codecgen.
  113. # Without this, ffjson chokes on one of the temporary files from codecgen.
  114. if [[ $zexternal == "1" ]]
  115. then
  116. echo "ffjson ... " && \
  117. ffjson -w values_ffjson${zsfx} $zfin &
  118. zzzIdFF=$!
  119. echo "msgp ... " && \
  120. msgp -tests=false -o=values_msgp${zsfx} -file=$zfin &
  121. zzzIdMsgp=$!
  122. sleep 1 # give ffjson and msgp some buffer time. see note above.
  123. fi
  124. echo "codecgen - !unsafe ... " && \
  125. codecgen -rt codecgen -t 'x,codecgen,!unsafe' -o values_codecgen${zsfx} -d 19780 $zfin &
  126. zzzIdC=$!
  127. echo "codecgen - unsafe ... " && \
  128. codecgen -u -rt codecgen -t 'x,codecgen,unsafe' -o values_codecgen_unsafe${zsfx} -d 19781 $zfin &
  129. zzzIdCU=$!
  130. wait $zzzIdC $zzzIdCU $zzzIdMsgp $zzzIdFF && \
  131. # remove (M|Unm)arshalJSON implementations, so they don't conflict with encoding/json bench \
  132. if [[ $zexternal == "1" ]]
  133. then
  134. sed -i 's+ MarshalJSON(+ _MarshalJSON(+g' values_ffjson${zsfx} && \
  135. sed -i 's+ UnmarshalJSON(+ _UnmarshalJSON(+g' values_ffjson${zsfx}
  136. fi && \
  137. echo "generators done!" && \
  138. true
  139. fi
  140. }
  141. # _init reads the arguments and sets up the flags
  142. _init() {
  143. OPTIND=1
  144. while getopts "fbx" flag
  145. do
  146. case "x$flag" in
  147. 'xf') zforce=1;;
  148. 'xb') zbak=1;;
  149. 'xx') zexternal=1;;
  150. *) echo "prebuild.sh accepts [-fb] only"; return 1;;
  151. esac
  152. done
  153. shift $((OPTIND-1))
  154. OPTIND=1
  155. }
  156. # main script.
  157. # First ensure that this is being run from the basedir (i.e. dirname of script is .)
  158. if [ "." = `dirname $0` ]
  159. then
  160. zmydir=`pwd`
  161. zfin="test_values.generated.go"
  162. zsfx="_generated_test.go"
  163. # rm -f *_generated_test.go
  164. rm -f codecgen-*.go && \
  165. _init "$@" && \
  166. _build && \
  167. cp $zmydir/values_test.go $zmydir/$zfin && \
  168. _codegenerators && \
  169. echo prebuild done successfully
  170. rm -f $zmydir/$zfin
  171. else
  172. echo "Script must be run from the directory it resides in"
  173. fi