build.sh 6.0KB

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