prebuild.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 *safe.generated.go *_generated_test.go
  50. fi
  51. cat > gen.generated.go <<EOF
  52. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  53. // Use of this source code is governed by a BSD-style 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. EOF
  67. # All functions, variables which must exist are put in this file.
  68. # This way, build works before we generate the right things.
  69. cat > fast-path.generated.go <<EOF
  70. package codec
  71. import "reflect"
  72. // func GenBytesToStringRO(b []byte) string { return string(b) }
  73. func fastpathDecodeTypeSwitch(iv interface{}, d *Decoder) bool { return false }
  74. func fastpathEncodeTypeSwitch(iv interface{}, e *Encoder) bool { return false }
  75. type fastpathE struct {
  76. rtid uintptr
  77. rt reflect.Type
  78. encfn func(encFnInfo, reflect.Value)
  79. decfn func(decFnInfo, reflect.Value)
  80. }
  81. type fastpathA [0]fastpathE
  82. func (x fastpathA) index(rtid uintptr) int { return -1 }
  83. var fastpathAV fastpathA
  84. EOF
  85. cat > gen-from-tmpl.generated.go <<EOF
  86. //+build ignore
  87. package main
  88. //import "flag"
  89. import "ugorji.net/codec"
  90. import "os"
  91. func run(fnameIn, fnameOut string, safe bool) {
  92. fin, err := os.Open(fnameIn)
  93. if err != nil { panic(err) }
  94. defer fin.Close()
  95. fout, err := os.Create(fnameOut)
  96. if err != nil { panic(err) }
  97. defer fout.Close()
  98. codec.GenInternalGoFile(fin, fout, safe)
  99. }
  100. func main() {
  101. // do not make safe/unsafe variants.
  102. // Instead, depend on escape analysis, and place string creation and usage appropriately.
  103. // run("unsafe.go.tmpl", "safe.generated.go", true)
  104. // run("unsafe.go.tmpl", "unsafe.generated.go", false)
  105. run("fast-path.go.tmpl", "fast-path.generated.go", false)
  106. run("gen-helper.go.tmpl", "gen-helper.generated.go", false)
  107. }
  108. EOF
  109. go run gen-from-tmpl.generated.go && \
  110. rm -f gen-from-tmpl.generated.go
  111. }
  112. _msgp_and_codecgen() {
  113. if [[ $zforce == "1" ||
  114. "1" == $( _needgen "values_msgp${zsfx}" )
  115. || "1" == $( _needgen "values_codecgen${zsfx}" ) ]]
  116. then
  117. msgp -tests=false -pkg=codec -o=values_msgp${zsfx} -file=$zfin && \
  118. codecgen -rt codecgen -t 'x,codecgen,!unsafe' -o values_codecgen${zsfx} $zfin && \
  119. codecgen -u -rt codecgen -t 'x,codecgen,unsafe' -o values_codecgen_unsafe${zsfx} $zfin
  120. fi
  121. }
  122. # _init reads the arguments and sets up the flags
  123. _init() {
  124. OPTIND=1
  125. while getopts "fb" flag
  126. do
  127. case "x$flag" in
  128. 'xf') zforce=1;;
  129. 'xb') zbak=1;;
  130. *) echo "prebuild.sh accepts [-fb] only"; return 1;;
  131. esac
  132. done
  133. shift $((OPTIND-1))
  134. OPTIND=1
  135. }
  136. # main script.
  137. # First ensure that this is being run from the basedir (i.e. dirname of script is .)
  138. if [ "." = `dirname $0` ]
  139. then
  140. zmydir=`pwd`
  141. zfin="test_values.generated.go"
  142. zsfx="_generated_test.go"
  143. # rm -f *_generated_test.go
  144. rm -f codecgen-*.go && \
  145. _init "$@" && \
  146. _build && \
  147. cp $zmydir/values_test.go $zmydir/$zfin && \
  148. _msgp_and_codecgen && \
  149. echo prebuild done successfully
  150. rm -f $zmydir/$zfin
  151. else
  152. echo "Script must be run from the directory it resides in"
  153. fi