build_release.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #!/bin/bash
  2. # set -x #enable debug
  3. SCRIPTPATH=$(readlink -f $0)
  4. SCRIPTNAME=$(basename $SCRIPTPATH)
  5. SCRIPTDIR=$(dirname $SCRIPTPATH)
  6. git="git"
  7. HAS_GIT=true
  8. HEADER_DIR="$SCRIPTDIR/header"
  9. DOCU_DIR="$SCRIPTDIR/doc"
  10. INC_MAJOR=false
  11. INC_MINOR=false
  12. INC_BUGFIX=false
  13. ALLZIPDIR="$SCRIPTDIR/TextSuite-all"
  14. if [ -z "$FIRST_VERSION" ]; then
  15. FIRST_VERSION="0.0.0.0"
  16. fi
  17. function log()
  18. {
  19. echo "$@" >&2
  20. }
  21. function cleaupAndExit()
  22. {
  23. if $HAS_GIT && [[ -n "$TAGNAME" ]] && [[ $1 -ne 0 ]]; then
  24. log "remove git tag $TAGNAME"
  25. $git tag -d $TAGNAME
  26. fi
  27. exit $1
  28. }
  29. function printHelp()
  30. {
  31. printf "script to build libShaderFile release
  32. Usage:
  33. $SCRIPTNAME [parameter]
  34. [] optional parameters
  35. () required parameters
  36. Parameter:
  37. --major | -m
  38. increment major version and reset the minor version before creating git tag
  39. --minor | -n
  40. increment major version and reset the bugfix version before creating git tag
  41. --bufgix | -b
  42. increment bugfix version before creating git tag
  43. --help | -h | -?
  44. print this help
  45. "
  46. }
  47. function getLastVersion()
  48. {
  49. POS="HEAD"
  50. TAG=$($git describe --tags --abbrev=0 $POS 2>/dev/null)
  51. RET=$?
  52. while [[ $RET -eq 0 ]]; do
  53. if [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  54. echo $(echo $TAG | egrep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
  55. return 0
  56. fi
  57. POS=$TAG"^"
  58. TAG=$($git describe --tags --abbrev=0 $POS 2>/dev/null)
  59. RET=$?
  60. done
  61. return 1
  62. }
  63. function incVersion()
  64. {
  65. read -r -a PARTS <<< $(echo "$1" | tr "." " ")
  66. if $INC_MAJOR; then
  67. ((PARTS[0]++))
  68. PARTS[1]=0
  69. fi
  70. if $INC_MINOR; then
  71. ((PARTS[1]++))
  72. PARTS[2]=0
  73. fi
  74. if $INC_BUGFIX; then
  75. ((PARTS[2]++))
  76. fi
  77. ((PARTS[3]++))
  78. local tmp="${PARTS[@]}"
  79. echo "${tmp// /.}"
  80. }
  81. function addGitTag()
  82. {
  83. local INC_VERSION=true
  84. log "create git version tag"
  85. CURRENT=$(getLastVersion)
  86. if [ $? -ne 0 ]; then
  87. CURRENT="$FIRST_VERSION"
  88. INC_VERSION=false
  89. fi
  90. local tmp="v$CURRENT"
  91. LINES=$($git log --pretty=oneline $tmp..HEAD)
  92. if [ $? -eq 0 ]; then
  93. DIFF=$(echo $LINES | wc -l)
  94. if [ $DIFF -eq 0 ] || [ -z "$LINES" ]; then
  95. log "current commit already has a version tag: $tmp"
  96. TAGNAME=$tmp
  97. return 0
  98. fi
  99. fi
  100. if $INC_VERSION; then
  101. NEWVERSION=$(incVersion $CURRENT)
  102. else
  103. NEWVERSION="$CURRENT"
  104. fi
  105. log "current version: $CURRENT"
  106. log "new version: $NEWVERSION"
  107. local tmp="v$NEWVERSION"
  108. $git tag $tmp
  109. if [[ $? -ne 0 ]]; then
  110. log "unable to create version tag: exit"
  111. cleaupAndExit 100
  112. fi
  113. TAGNAME=$tmp
  114. HASH="$(git rev-parse HEAD)"
  115. printf "const Version = 'v$NEWVERSION $HASH';" > inc/utsTextSuiteVersion.inc
  116. return 0
  117. }
  118. function BuildConfig()
  119. {
  120. CONFIG=$1
  121. TARGET=$2
  122. FILEEXT=$3
  123. DBGEXT=".dbg"
  124. printf "\n== build project $CONFIG ==\n"
  125. lazbuild --build-mode=$CONFIG --build-all --verbose $SCRIPTDIR/libTextSuite.lpi
  126. if [ $? -ne 0 ]; then
  127. echo "build failed! exit."
  128. cleaupAndExit 1
  129. fi
  130. BINDIR="$SCRIPTDIR/bin"
  131. ZIPDIR="$SCRIPTDIR/TextSuite-$TARGET"
  132. ZIPPATH="$SCRIPTDIR/TextSuite-$TARGET.zip"
  133. if [ -n "$TAGNAME" ]; then
  134. mkdir -p "$SCRIPTDIR/$TAGNAME/"
  135. ZIPPATH="$SCRIPTDIR/$TAGNAME/TextSuite-$TARGET.zip"
  136. fi
  137. TARGETDIR="$BINDIR/$TARGET"
  138. BINNAME="$TARGETDIR/libtextsuite$FILEEXT"
  139. DBGNAME="$TARGETDIR/libtextsuite$DBGEXT"
  140. if [ ! -f $BINNAME ]; then
  141. echo "file not found: $BINNAME"
  142. cleaupAndExit 2
  143. fi
  144. # extract debug infos
  145. cp "$BINNAME" "$DBGNAME" || { log "unable to copy binary to *.dbg"; cleaupAndExit 3; }
  146. objcopy --only-keep-debug "$DBGNAME" || { log "unable to strip $DBGNAME"; cleaupAndExit 3; }
  147. objcopy --strip-debug --strip-unneeded "$BINNAME" || { log "unable to copy binary to $BINNAME"; cleaupAndExit 3; }
  148. objcopy --add-gnu-debuglink "$DBGNAME" "$BINNAME" || { log "unable to create debug link"; cleaupAndExit 3; }
  149. if [ ! -f $DBGNAME ]; then
  150. echo "file not found: $DBGNAME"
  151. cleaupAndExit 2
  152. fi
  153. # copy header and docu
  154. mkdir -p "$ZIPDIR/bin"
  155. cp -r "$TARGETDIR" "$ZIPDIR/bin" || { log "unable to copy binaries"; cleaupAndExit 3; }
  156. cp -r "$HEADER_DIR" "$ZIPDIR" || { log "unable to copy header files"; cleaupAndExit 4; }
  157. cp -r "$DOCU_DIR" "$ZIPDIR/docu" || { log "unable to copy documentation files"; cleaupAndExit 5; }
  158. pushd "$ZIPDIR"
  159. rm -rf "$ZIPPATH"
  160. zip -r "$ZIPPATH" "." || { log "unable to create zip archive"; cleaupAndExit 6; }
  161. popd
  162. mkdir -p "$ALLZIPDIR"
  163. echo "mv $ZIPDIR/* $ALLZIPDIR/"
  164. cp -rf $ZIPDIR/* $ALLZIPDIR/ || { log "unable to copy binaries to all directory"; cleaupAndExit 3; }
  165. rm -rf "$ZIPDIR"
  166. }
  167. while [[ $# -gt 0 ]]; do
  168. case $1 in
  169. "--major" | "-m")
  170. INC_MAJOR=true
  171. ;;
  172. "--minor" | "-n")
  173. INC_MINOR=true
  174. ;;
  175. "--bufgix" | "-b")
  176. INC_BUGFIX=true
  177. ;;
  178. "--help" | "-h" | "-?")
  179. printHelp
  180. cleaupAndExit 0
  181. ;;
  182. *)
  183. echo "invalid parameter: $1"
  184. echo "use --help to get further information"
  185. ;;
  186. esac
  187. shift
  188. done
  189. if $HAS_GIT; then
  190. GIT_CHANGED=$($git status --untracked-files=all --verbose --porcelain)
  191. if [[ -n "$GIT_CHANGED" ]]; then
  192. log "git has uncommited changes. please commit before building a release"
  193. cleaupAndExit 101
  194. fi
  195. addGitTag
  196. fi
  197. BuildConfig "Win32Release" "i386-win32" ".dll"
  198. BuildConfig "Win64Release" "x86_64-win64" ".dll"
  199. BuildConfig "Linux32Release" "i386-linux" ".so"
  200. BuildConfig "Linux64Release" "x86_64-linux" ".so"
  201. ALLZIPPATH="$SCRIPTDIR/TextSuite-all.zip"
  202. if [ -n "$TAGNAME" ]; then
  203. mkdir -p "$SCRIPTDIR/$TAGNAME/"
  204. ALLZIPPATH="$SCRIPTDIR/$TAGNAME/TextSuite-all.zip"
  205. fi
  206. pushd "$ALLZIPDIR"
  207. rm -rf "$ALLZIPPATH"
  208. zip -r "$ALLZIPPATH" "." || { log "unable to create zip archive"; cleaupAndExit 6; }
  209. popd
  210. rm -rf "$ALLZIPDIR"
  211. cleaupAndExit 0