123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/bin/bash
  2. . "$MKIT_DIR/include/ini.sh" || die "cannot import ini.sh"
  3. build() {
  4. #
  5. # Add meat to all skeletons
  6. #
  7. local srcpath
  8. find src -type f -name '*.skel' \
  9. | while read srcpath;
  10. do
  11. build1 "$srcpath"
  12. done
  13. }
  14. build1() {
  15. #
  16. # Process one skeleton
  17. #
  18. local srcpath="$1"
  19. local dstpath="$2"
  20. local ftype="$3"
  21. test -n "$dstpath" || dstpath=${srcpath%.skel}
  22. test -n "$ftype" || ftype=$(guess_ftype "$dstpath")
  23. debug_var srcpath dstpath ftype
  24. <"$srcpath" build1_ftype "$ftype" >"$dstpath"
  25. mkdir -p "$MKIT_LOCAL"
  26. echo "$dstpath" >> "$MKIT_LOCAL/built.lst"
  27. }
  28. guess_ftype() {
  29. #
  30. # Guess file type from destination path $1
  31. #
  32. local dstpath="$1"
  33. case $dstpath in
  34. *.md) echo markdown ;;
  35. *) echo MKIT_COMMON ;;
  36. esac
  37. }
  38. build1_ftype() {
  39. #
  40. # Build a file of type $1
  41. #
  42. local ftype="$1"
  43. case $ftype in
  44. MKIT_COMMON) expand_variables "vars" ;;
  45. markdown) expand_includes | expand_variables "vars" ;;
  46. rpmstuff) expand_variables "vars" "rpmstuff:vars" ;;
  47. *) die "unknown file type: $ftype" ;;
  48. esac
  49. }
  50. build_manpages() {
  51. local manfile mdfile
  52. if command -v ronn >/dev/null;
  53. then
  54. ini lskeys "files:man" \
  55. | while read manfile;
  56. do
  57. mdfile="$manfile.md"
  58. ronn -r "$mdfile"
  59. mkdir -p "$MKIT_LOCAL"
  60. echo "$manfile" >> "$MKIT_LOCAL/built.lst"
  61. done
  62. else
  63. echo "ronn is not installed"
  64. return 1
  65. fi
  66. }
  67. clean() {
  68. #
  69. # Clean up tree after building
  70. #
  71. test -f "$MKIT_LOCAL/built.lst" && {
  72. <"$MKIT_LOCAL/built.lst" xargs -r rm -f
  73. rm -f "$MKIT_LOCAL/built.lst"
  74. rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL"
  75. }
  76. true
  77. }
  78. dist() {
  79. #
  80. # Create distributable tarball
  81. #
  82. #FIXME: lacking Makefile skills, we do this step twice fot
  83. # rpmstuff, hence -f hack for gzip
  84. #
  85. local version=$(get_version)
  86. local dirname=$MKIT_PKGNAME-$version
  87. mkdir -p "$dirname"
  88. ini values "lists:dist" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
  89. sed -i -e "s/^VERSION = .*/VERSION = $version/" "$dirname/config.mk"
  90. tar -cf "$dirname.tar" "$dirname"
  91. gzip -f "$dirname.tar" # see above FIXME
  92. mkdir -p "$MKIT_LOCAL"
  93. echo "$dirname.tar.gz" >> "$MKIT_LOCAL/built.lst"
  94. rm -rf "$dirname"
  95. }
  96. rpmstuff() {
  97. #
  98. # Build specfile
  99. #
  100. local specname="$(ini 1value ENV:PKGNAME).spec"
  101. local specsrc="$(ini 1value "rpmstuff:spec_skel")"
  102. test -n "$specsrc" || die "rpmstuff:spec_skel not specified"
  103. test -f "$specsrc" || die "specfile template not found: $specsrc"
  104. build1 "$specsrc" "$specname"
  105. }
  106. expand_includes() {
  107. #
  108. # Expand include directives
  109. #
  110. # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
  111. #
  112. perl -we '
  113. use strict;
  114. my $text;
  115. while (<>) {
  116. chomp;
  117. if (m/<!-- include4: (\S+) -->/) {
  118. open my $fh, $1 or warn "cannot find: $1";
  119. my $text = do { local($/); <$fh> };
  120. close $fh;
  121. $text =~ s/^(.)/ $1/gm;
  122. chomp $text;
  123. print "$text\n";
  124. } else {
  125. print "$_\n";
  126. }
  127. }
  128. '
  129. }
  130. expand_variables() {
  131. #
  132. # Expand variables from sections $@
  133. #
  134. local script=$(mktemp --tmpdir mkit-tmp.XXXXXXXXXX)
  135. local section varname varvalue
  136. for section in "$@";
  137. do
  138. debug_var section
  139. ini lskeys "$section" \
  140. | while read varname;
  141. do
  142. varvalue="$(ini 1value "$section:$varname" | sed -e 's/\$/\\$/' )"
  143. echo "s|$varname|$varvalue|;" >> "$script"
  144. debug_var varname varvalue
  145. done
  146. done
  147. echo "s|__CODENAME__|$CODENAME|;" >> "$script"
  148. echo "s|__VERSION__|$(get_version)|;" >> "$script"
  149. perl -wp "$script" || die "expand_variables failed"
  150. rm "$script"
  151. }
  152. get_version() {
  153. #
  154. # Build semver version string with build metadata
  155. #
  156. # Build version string from available info using following
  157. # logic:
  158. #
  159. # 1. use VERSION (from config.mk)
  160. # 2. if we are in git, override the version with last tag
  161. # 3. if set, add PRERELEASE (from config.mk) as pre-release ID
  162. # (afer dash)
  163. # 4. if we are at a later commit than the last tag, add branch
  164. # name and commit sha1 to build metadata (after plus sign)
  165. # 5. if the tree is "dirty", i.e. has uncommited changes,
  166. # add "dirty" to build metadata
  167. #
  168. # The version is compatible with SemVer 2.0.0.
  169. #
  170. # Examples:
  171. #
  172. # myprog v1.0.7 # all clear
  173. # myprog v1.0.7-alpha # PRERELEASE="alpha"
  174. # myprog v1.0.7-alpha+g1aef811.master # ^^ + some commits after
  175. # myprog v1.0.7-alpha+gf14fc4f.api2 # ^^ + on a feature branch
  176. # myprog v1.0.7-alpha+gf14fc4f.api2.dirty # ^^ + tree edited
  177. # myprog v1.0.7-alpha+dirty # tag OK but tree edited
  178. # myprog v1.0.7+dirty # ^^ but no pre-release id
  179. #
  180. # Note that versions with "dirty" should be perceived as kind of
  181. # dangerous outside developer's own machine. Versions with sha1 are
  182. # safer but must not be released.
  183. #
  184. # I have considered decorating the git commit refs to make them
  185. # sort of sortable (e.g. "r1.g1aef811"), but on second thought,
  186. # I don't think it's good idea to give *any* semantics to meta-data
  187. # at all. First, there is no rule that r1<r2<r3; a commit can be
  188. # removing what other just added and one change can be split to
  189. # multiple commits. Also, the whole thing breaks anyway once you
  190. # rebase your branch (no, it's not a sin). The sole purpose of
  191. # meta-data is to *identify* the code, and provide safe path back
  192. # to tree; commit refs are already perfect for that.
  193. #
  194. # FIXME: Using PRERELEASE for release IDs may not be compatible with
  195. # release strategy implemented in release.sh
  196. #
  197. local version=$VERSION
  198. local prerl=$PRERELEASE
  199. grep ":" <<<"$prerl" && warn "colon in PRERELEASE may corrupt version data: $prerl"
  200. if git rev-parse HEAD >&/dev/null;
  201. then # we are in git repo... so we can get smart
  202. local latest_tag=$(
  203. git log --format="%D" \
  204. | sed 's/,/\n/g' \
  205. | sed 's/^[[:blank:]]*//; ' \
  206. | grep -E '^tag: v[[:digit:]]+\.' \
  207. | cut -d' ' -f2 \
  208. | head -1
  209. )
  210. if ! git describe --tags --exact-match HEAD >&/dev/null;
  211. then # we are at a later commit than the last tag
  212. local sha=g$(git log -1 --pretty=format:%h HEAD)
  213. local curbranch=$(git rev-parse --abbrev-ref HEAD)
  214. local commit="$curbranch.$sha"
  215. fi
  216. if test "$(git diff --shortstat 2>/dev/null)" != "";
  217. then # the tree is "dirty", i.e. has been edited
  218. local dirty=dirty
  219. fi
  220. test -n "$latest_tag" && version=${latest_tag:1}
  221. local suffix=""
  222. case "$commit:$dirty" in
  223. :) suffix="" ;;
  224. :dirty) suffix="+$dirty" ;;
  225. *:) suffix="+$commit" ;;
  226. *:dirty) suffix="+$commit.$dirty" ;;
  227. esac
  228. test -b "$prerl" && suffix="-$prerl$suffix"
  229. version="$version$suffix"
  230. fi
  231. echo "$version"
  232. }