build.sh 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. mkit_import ini
  5. mkit_import facts
  6. __build1() {
  7. #
  8. # Process one skeleton $1 of type $3 (or guessed) to path $2
  9. #
  10. local srcpath=$1 # skeleton path
  11. local dstpath=$2 # destination meaty animal path
  12. local ftype=$3 # file/builder type
  13. test -n "$dstpath" || dstpath=${srcpath%.skel}
  14. test -n "$ftype" || ftype=$(__guess_ftype "$dstpath")
  15. debug_var srcpath dstpath ftype
  16. <"$srcpath" __build1_ftype "$ftype" >"$dstpath"
  17. __rec_built "$dstpath"
  18. }
  19. __build1_ftype() {
  20. #
  21. # Build a file of type $1; fom stdin to stdout
  22. #
  23. local ftype=$1 # file/builder type
  24. case $ftype in
  25. MKIT_COMMON) __expand_macros "macros" ;;
  26. rpmstuff) __expand_macros "macros" "rpmstuff:macros" ;;
  27. debstuff) __expand_macros "macros" "debstuff:macros" ;;
  28. *) die "unknown file type: $ftype" ;;
  29. esac
  30. }
  31. __expand_line() {
  32. #
  33. # Expand macro from $MacroMap in single line $1
  34. #
  35. # If macro value has multiple lines, repeat original line with
  36. # different substitution.
  37. #
  38. # E.g. if macro value is "foo\nbar" and macro name is __FOO__,
  39. # line `see: "__FOO__"` will expand to two lines: `see: "foo"`
  40. # and `see: "bar"`.
  41. #
  42. local line=$1 # line to process
  43. local mname # macro name
  44. local mvline # line of macro value
  45. for mname in "${!MacroMap[@]}"; do
  46. if ! test "${line//$mname}" == "$line"; then
  47. while IFS= read -r mvline; do
  48. echo "${line//$mname/$mvline}"
  49. done <<<"${MacroMap[$mname]}"
  50. return 0
  51. fi
  52. done
  53. echo "$line"
  54. return 1
  55. }
  56. __expand_macros() {
  57. #
  58. # Read stdin, expanding macros from sections $@
  59. #
  60. local section # each section to expand macros from
  61. local line # each line on stdin
  62. local mname # each macro name
  63. local -A MacroMap # macro value map
  64. MacroMap[__MKIT_PROJ_NAME__]=$(ini 1value project:name)
  65. MacroMap[__MKIT_PROJ_CODENAME__]=$(ini 1value project:codename)
  66. MacroMap[__MKIT_PROJ_LICENSE__]=$(ini 1value project:license)
  67. MacroMap[__MKIT_PROJ_PKGNAME__]=$(ini 1value project:pkgname)
  68. MacroMap[__MKIT_PROJ_TAGLINE__]=$(ini 1value project:tagline)
  69. MacroMap[__MKIT_PROJ_MAINTAINER__]=$(ini 1value project:maintainer)
  70. MacroMap[__MKIT_PROJ_VCS_BROWSER__]=$(ini 1value project:vcs_browser)
  71. MacroMap[__MKIT_PROJ_GIT_LASTHASH__]=$(__cached git_lasthash)
  72. MacroMap[__MKIT_PROJ_VERSION__]=$(__cached semver)
  73. MacroMap[__MKIT_SELF_VERSION__]=$MKIT_VERSION
  74. for section in "$@"; do
  75. for mname in $(ini lskeys "$section"); do
  76. MacroMap[$mname]=$(ini values "$section:$mname")
  77. done
  78. done
  79. debug_var MacroMap
  80. while IFS= read -r line; do
  81. __expand_line "$line"
  82. done
  83. }
  84. __guess_ftype() {
  85. #
  86. # Guess file type from destination path $1
  87. #
  88. local dstpath=$1 # destination path
  89. case $dstpath in
  90. *) echo MKIT_COMMON ;;
  91. esac
  92. }
  93. __qfs() {
  94. #
  95. # Quote for our sed scipt's RHS
  96. #
  97. sed '
  98. s:\\:\\\\:g
  99. s:|:\\|:g
  100. '
  101. }
  102. __cached() {
  103. #
  104. # Cached value $1 of function $1()
  105. #
  106. # In order to support git-less builds, some values might be cached
  107. # in $MKIT_LOCAL. This function gets file $1 from that cache (cache
  108. # hit) or re-creates it (cache miss), but prints its body in either
  109. # case.
  110. #
  111. # The command to re-create file is the same as the key (ie. no
  112. # arguments).
  113. #
  114. local name=$1
  115. __local_get "$name" && return 0
  116. "$name" | __local_putb "$name"
  117. __local_get "$name"
  118. }
  119. __local_putb() {
  120. #
  121. # Make file $1 in $MKIT_LOCAL from stdin and mark as built
  122. #
  123. local fpath=$1
  124. __local_put "$fpath" && __rec_built "$MKIT_LOCAL/$fpath"
  125. }
  126. __local_put() {
  127. #
  128. # Make file $1 in $MKIT_LOCAL from stdin
  129. #
  130. local fpath="$MKIT_LOCAL/$1"
  131. { mkdir -p "${fpath%/*}" && cat >"$fpath"; } \
  132. || die "cannot write to local cache: $fpath"
  133. }
  134. __local_get() {
  135. #
  136. # Read file $1 in $MKIT_LOCAL
  137. #
  138. local fpath="$MKIT_LOCAL/$1"
  139. cat "$fpath" 2>/dev/null
  140. }
  141. __rec_built() {
  142. #
  143. # Record file $1 for deletion on `clean`
  144. #
  145. local file=$1
  146. mkdir -p "$MKIT_LOCAL"
  147. echo "$file" >> "$MKIT_LOCAL/built.lst"
  148. }
  149. _mkit_data() {
  150. #
  151. # Build sampler showing all macro values
  152. #
  153. local macro
  154. local section
  155. local sections
  156. sections=()
  157. ini lskeys macros | grep -q . && sections=(macros)
  158. sections+=( $(ini lssect | grep ':macros$') )
  159. {
  160. echo "(builtin):"
  161. echo " x_MKIT_PROJ_NAME__ => '__MKIT_PROJ_NAME__'"
  162. echo " x_MKIT_PROJ_CODENAME__ => '__MKIT_PROJ_CODENAME__'"
  163. echo " x_MKIT_PROJ_LICENSE__ => '__MKIT_PROJ_LICENSE__'"
  164. echo " x_MKIT_PROJ_PKGNAME__ => '__MKIT_PROJ_PKGNAME__'"
  165. echo " x_MKIT_PROJ_TAGLINE__ => '__MKIT_PROJ_TAGLINE__'"
  166. echo " x_MKIT_PROJ_MAINTAINER__ => '__MKIT_PROJ_MAINTAINER__'"
  167. echo " x_MKIT_PROJ_VCS_BROWSER__ => '__MKIT_PROJ_VCS_BROWSER__'"
  168. echo " x_MKIT_PROJ_GIT_LASTHASH__ => '__MKIT_PROJ_GIT_LASTHASH__'"
  169. echo " x_MKIT_PROJ_VERSION__ => '__MKIT_PROJ_VERSION__'"
  170. echo " x_MKIT_SELF_VERSION__ => '__MKIT_SELF_VERSION__'"
  171. for section in "${sections[@]}"; do
  172. echo "$section:"
  173. for macro in $(ini lskeys "$section"); do
  174. echo " x${macro:1} => '$macro'"
  175. done
  176. done
  177. } \
  178. | __expand_macros "MKIT_BUILTIN" "${sections[@]}" \
  179. | sed '/^ x/ s|x|_|'
  180. }
  181. build() {
  182. #
  183. # Add meat to all skeletons
  184. #
  185. local srcpath # each source path
  186. find . -type f -name '*.skel' \
  187. | while read -r srcpath; do
  188. __build1 "$srcpath"
  189. done
  190. }
  191. clean() {
  192. #
  193. # Clean up tree after building
  194. #
  195. test -f "$MKIT_LOCAL/built.lst" && {
  196. <"$MKIT_LOCAL/built.lst" grep -v -e '\.\.' -e ^/ \
  197. | xargs -r rm -rf
  198. rm -f "$MKIT_LOCAL/built.lst"
  199. rmdir --ignore-fail-on-non-empty "$MKIT_LOCAL"
  200. }
  201. true
  202. }
  203. debstuff() {
  204. #
  205. # Build Debian stuff (eamed tarball, debian dir)
  206. #
  207. local version # package version
  208. local debian_skel # 'debian' folder skeleton
  209. local dfsrc # each source file from ^^
  210. local dftgt # each built packaging file
  211. version=$(__cached semver)
  212. # tarball - we should already have by means of 'dist'
  213. #
  214. mv "${MKIT_PROJ_PKGNAME}-$version.tar.gz" \
  215. "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz" \
  216. || die "could not rename tarball"
  217. __rec_built "${MKIT_PROJ_PKGNAME}_$version.orig.tar.gz"
  218. # read content of each mandatory file from debian_skel
  219. #
  220. debian_skel=$(ini 1value dist:debstuff)
  221. test -n "$debian_skel" || die "dist:debstuff not specified"
  222. test -d "$debian_skel" || die "debian directory template found: $debian_skel"
  223. mkdir -p debian/source
  224. find "$debian_skel" -type f \
  225. | while read -r dfsrc; do
  226. dftgt="debian/${dfsrc#$debian_skel}"
  227. mkdir -p "$(dirname "$dftgt")"
  228. __build1 "$dfsrc" "$dftgt" debstuff
  229. done
  230. __rec_built debian
  231. }
  232. dist() {
  233. #
  234. # Create distributable tarball
  235. #
  236. #FIXME: lacking Makefile skills, we do this step twice for
  237. # rpmstuff, hence -f hack for gzip
  238. #
  239. local version # tarball version
  240. local git_lasthash # last git commit hash
  241. local dirname # directory and tarball name
  242. version=$(semver)
  243. dirname=$MKIT_PROJ_PKGNAME-$version
  244. git_lasthash=$(git_lasthash)
  245. mkdir -p "$dirname"
  246. ini values "dist:tarball" | xargs -I DIST_ITEM cp -R DIST_ITEM "$dirname"
  247. mkdir -p "$dirname/.mkit"
  248. echo -n "$version" > "$dirname/.mkit/semver"
  249. echo -n "$git_lasthash" > "$dirname/.mkit/git_lasthash"
  250. tar -cf "$dirname.tar" "$dirname"
  251. gzip -f "$dirname.tar" # see above FIXME
  252. __rec_built "$dirname.tar.gz"
  253. rm -rf "$dirname"
  254. }
  255. rpmstuff() {
  256. #
  257. # Build specfile
  258. #
  259. local specname=$MKIT_PROJ_PKGNAME.spec # .spec filename
  260. local specsrc # source of specfile
  261. specsrc="$(ini 1value "dist:rpmstuff")"
  262. test -n "$specsrc" || die "dist:rpmstuff not specified"
  263. test -f "$specsrc" || die "specfile template not found: $specsrc"
  264. __build1 "$specsrc" "$specname" rpmstuff
  265. }