Simple Makefile target helper https://pagure.io/mkit

build.sh 6.6KB

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