Source files of fsfe.org, pdfreaders.org, freeyourandroid.org, ilovefs.org, drm.info, and test.fsfe.org. Contribute: https://fsfe.org/contribute/web/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

makerules.sh 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #!/bin/sh
  2. inc_makerules=true
  3. [ -z "$inc_misc" ] && . "$basedir/build/misc.sh"
  4. [ -z "$inc_translations" ] && . "$basedir/build/translations.sh"
  5. [ -z "$inc_filenames" ] && . "$basedir/build/filenames.sh"
  6. [ -z "$inc_fundraising" ] && . "$basedir/build/fundraising.sh"
  7. [ -z "$inc_languages" ] && . "$basedir/build/languages.sh"
  8. [ -z "$inc_sources" ] && . "$basedir/build/sources.sh"
  9. sourcefind() {
  10. find "$input" -name .svn -prune -o -type f "$@" -printf '%P\n'
  11. }
  12. mio(){
  13. # make input/output abstraction, produce reusable makefiles
  14. # by replacing in and out pathes with make variables.
  15. for each in "$@"; do
  16. case "$each" in
  17. "$input"/*) echo "\${INPUTDIR}/${each#$input/}" ;;
  18. "$output"/*) echo "\${OUTPUTDIR}/${each#$output/}" ;;
  19. *) echo "$each" ;;
  20. esac
  21. done
  22. }
  23. mes(){
  24. # make escape... escape a filename for make syntax
  25. # possibly not complete
  26. mio "$@" \
  27. | sed -r 's;([ #]);\\\1;g' \
  28. | tr '\n' ' '
  29. }
  30. glob_maker(){
  31. # issue make rules for preglobbed sources files
  32. sourcesfile="$1"
  33. filedir="\${INPUTDIR}/${sourcesfile}"
  34. filedir="${filedir%/*}"
  35. shortbase="${sourcesfile##*/}"
  36. shortbase="${shortbase%.sources}"
  37. for lang in $(get_languages); do
  38. globfile="${filedir}/._._${shortbase}.${lang}.sourceglobs"
  39. refglobs="${filedir}/._._${shortbase}.${lang}.refglobs"
  40. cat <<MakeEND
  41. $(mes "$globfile"): $(mes "\${INPUTDIR}/tagmap" "\${INPUTDIR}/$sourcesfile")
  42. \${PGLOBBER} \${PROCFLAGS} lang_sources "\${INPUTDIR}/$sourcesfile" "$lang" >"$globfile"
  43. $(mes "$refglobs"): $(mes "$globfile")
  44. \${PGLOBBER} \${PROCFLAGS} cast_refglobs "$globfile" "$refglobs"
  45. MakeEND
  46. done
  47. }
  48. glob_makers(){
  49. # generate make rules for globbing all .sources files
  50. # within input tree
  51. sourcefind -name '*.sources' \
  52. | while read filepath; do
  53. glob_maker "$filepath"
  54. done
  55. }
  56. glob_additions(){
  57. printf "%s\n" "$@" \
  58. | egrep '.+\.sources$' \
  59. | xargs realpath \
  60. | while read addition; do
  61. glob_maker "${addition#$input/}"
  62. done
  63. }
  64. xhtml_maker(){
  65. # generate make rules for building html files out of xhtml
  66. # account for included xml files and xsl rules
  67. shortname="$input/$1"
  68. outpath="\${OUTPUTDIR}/${2}"
  69. outpath="${outpath%/*}"
  70. textsen="$(get_textsfile "en")"
  71. menufile="$basedir/tools/menu-global.xml"
  72. filedir="${shortname%/*}"
  73. shortbase="${shortname##*/}"
  74. processor="$(get_processor "$shortname")"
  75. langglob="$filedir/._._${shortbase}.langglob"
  76. cast_langglob "$shortname" "$langglob"
  77. [ -f "$shortname".rss.xsl ] && bool_rss=true || bool_rss=false
  78. [ -f "$shortname".ics.xsl ] && bool_ics=true || bool_ics=false
  79. olang="$(echo "${shortname}".[a-z][a-z].xhtml "${shortname}".[e]n.xhtml |sed -rn 's;^.*\.([a-z]{2})\.xhtml.*$;\1;p')"
  80. if [ "${shortbase}" = "${filedir##*/}" ] && \
  81. [ ! -f "${filedir}/index.${olang}.xhtml" ]; then
  82. bool_indexname=true
  83. else
  84. bool_indexname=false
  85. fi
  86. [ -f "${shortname}.sources" ] && bool_sourceinc=true || bool_sourceinc=false
  87. # For speed considerations: avoid all disk I/O in this loop
  88. for lang in $(get_languages); do
  89. infile="${shortname}.${lang}.xhtml"
  90. [ -e "$infile" ] && depfile="$infile" || depfile="${shortname}.${olang}.xhtml"
  91. infile="$(mio "$infile")"
  92. outbase="${shortbase}.${lang}.html"
  93. outfile="${outpath}/${outbase}"
  94. outlink="${outpath}/${shortbase}.html.$lang"
  95. rssfile="${outpath}/${shortbase}.${lang}.rss"
  96. icsfile="${outpath}/${shortbase}.${lang}.ics"
  97. textsfile="$(get_textsfile "$lang")"
  98. fundraisingfile="$(get_fundraisingfile "$lang")"
  99. $bool_sourceinc && sourceglobs="${filedir#./}/._._${shortbase}.${lang}.refglobs" || unset sourceglobs
  100. cat <<MakeEND
  101. all: $(mes "$outfile" "$outlink")
  102. $(mes "$outfile"): $(mes "$depfile" "$processor" "$textsen" "$textsfile" "$fundraisingfile" "$menufile" "$sourceglobs" "$langglob")
  103. \${PROCESSOR} \${PROCFLAGS} process_file "${infile}" "$(mio "$processor")" "$olang" >"$outfile"
  104. $(mes "$outlink"):
  105. ln -sf "${outbase}" "${outlink}"
  106. MakeEND
  107. $bool_rss && cat<<MakeEND
  108. all: $(mes "$rssfile")
  109. $(mes "$rssfile"): $(mes "$depfile" "${shortname}.rss.xsl" "$textsen" "$textsfile" "$fundraisingfile" "$menufile" "$sourceglobs")
  110. \${PROCESSOR} \${PROCFLAGS} process_file "${infile}" "$(mio "${shortname}.rss.xsl")" "$olang" >"$rssfile"
  111. MakeEND
  112. $bool_ics && cat<<MakeEND
  113. all: $(mes "$icsfile")
  114. $(mes "$icsfile"): $(mes "$depfile" "${shortname}.ics.xsl" "$textsen" "$textsfile" "$fundraisingfile" "$menufile" "$sourceglobs")
  115. \${PROCESSOR} \${PROCFLAGS} process_file "${infile}" "$(mio "${shortname}.ics.xsl")" "$olang" >"$icsfile"
  116. MakeEND
  117. $bool_indexname && cat <<MakeEND
  118. all: $(mes "$outpath/index.${lang}.html" "$outpath/index.html.$lang")
  119. $(mes "$outpath/index.${lang}.html"):
  120. ln -sf "$outbase" "$outpath/index.${lang}.html"
  121. $(mes "$outpath/index.html.$lang"):
  122. ln -sf "$outbase" "$outpath/index.html.$lang"
  123. MakeEND
  124. done
  125. }
  126. xhtml_makers(){
  127. # generate make rules concerning all .xhtml files in source tree
  128. sourcefind -name '*.[a-z][a-z].xhtml' \
  129. | sed -r 's;\.[a-z][a-z]\.xhtml$;;' \
  130. | sort -u \
  131. | while read shortpath; do
  132. xhtml_maker "$shortpath" "${shortpath}"
  133. done
  134. }
  135. xhtml_additions(){
  136. printf "%s\n" "$@" \
  137. | sed -rn 's;\.[a-z][a-z]\.xhtml$;;p' \
  138. | sort -u \
  139. | xargs realpath \
  140. | while read addition; do
  141. xhtml_maker "${addition#$input/}" "${addition#$input/}"
  142. done
  143. }
  144. copy_maker(){
  145. # generate make rule for copying a plain file
  146. infile="\${INPUTDIR}/$1"
  147. outfile="\${OUTPUTDIR}/$2"
  148. cat <<MakeEND
  149. all: $(mes "$outfile")
  150. $(mes "$outfile"): $(mes "$infile")
  151. cp "$infile" "$outfile"
  152. MakeEND
  153. }
  154. copy_makers(){
  155. # generate copy rules for entire input tree
  156. sourcefind \! -name 'Makefile' \! -name '*.sourceglobs' \! -name '*.refglobs' \
  157. \! -name '*.sources' \! -name '*.xhtml' \! -name '*.xml' \
  158. \! -name '*.xsl' \! -name 'tagmap' \! -name '*.langglob' \
  159. | while read filepath; do
  160. copy_maker "$filepath" "$filepath"
  161. done
  162. }
  163. copy_additions(){
  164. printf "%s\n" "$@" \
  165. | egrep -v '.+(\.sources|\.sourceglobs|\.refglobs|\.xhtml|\.xml|\.xsl|/Makefile|/)$' \
  166. | xargs realpath \
  167. | while read addition; do
  168. copy_maker "${addition#$input/}" "${addition#$input/}"
  169. done
  170. }
  171. xslt_dependencies(){
  172. # list referenced xsl files for a given xsl file
  173. # *not* recursive since Make will handle recursive
  174. # dependency resolution
  175. file="$1"
  176. cat "$file" \
  177. | tr '\n\t' ' ' \
  178. | sed -r 's;(<xsl:(include|import)[^>]*>);\n\1\n;g' \
  179. | sed -nr '/<xsl:(include|import)[^>]*>/s;^.*href *= *"([^"]*)".*$;\1;gp'
  180. }
  181. xslt_maker(){
  182. # find external references in a xsl file and generate
  183. # Make dependencies accordingly
  184. file="$input/$1"
  185. dir="${file%/*}"
  186. deps="$( xslt_dependencies "$file" |xargs -I'{}' realpath "$dir/{}" )"
  187. cat <<MakeEND
  188. $(mes "$file"): $(mes $deps)
  189. touch "$(mio "$file")"
  190. MakeEND
  191. }
  192. xslt_makers(){
  193. # generate make dependencies for all .xsl files in input tree
  194. sourcefind -name '*.xsl' \
  195. | while read filepath; do
  196. xslt_maker "$filepath"
  197. done
  198. }
  199. xslt_additions(){
  200. printf "%s\n" "$@" \
  201. | egrep '.+\.xsl$' \
  202. | xargs realpath \
  203. | while read addition; do
  204. xslt_maker "${addition#$input/}"
  205. done
  206. }
  207. copy_sources(){
  208. # generate rules to copy all .xhtml files in the input to
  209. # the public source directory
  210. sourcefind -name '*.xhtml' \
  211. | while read filepath; do
  212. copy_maker "$filepath" "source/$filepath"
  213. done
  214. }
  215. copy_sourceadditions(){
  216. printf "%s\n" "$@" \
  217. | egrep '.+\.xhtml$' \
  218. | xargs realpath \
  219. | while read addition; do
  220. copy_maker "${addition#$input/}" "source/${addition#$input/}"
  221. done
  222. }
  223. tree_maker(){
  224. # walk through file tree and issue Make rules according to file type
  225. input="$(realpath "$1")"
  226. output="$(realpath "$2")"
  227. shift 2
  228. cache_textsfile
  229. cache_fundraising
  230. cat <<MakeHead
  231. .PHONY: all
  232. PROCESSOR = "$basedir/build/process_file.sh"
  233. PGLOBBER = "$basedir/build/source_globber.sh"
  234. PROCFLAGS = --source "$basedir" --statusdir "$statusdir" --domain "$domain"
  235. INPUTDIR = $input
  236. OUTPUTDIR = $output
  237. # cannot store find results in variable because it will result in too many arguments for the shell
  238. \${INPUTDIR}/tagmap: \$(shell find "$basedir" -name '*.[a-z][a-z].xml')
  239. find "$basedir" -name '*.[a-z][a-z].xml' |xargs \${PGLOBBER} \${PROCFLAGS} map_tags >\${INPUTDIR}/tagmap
  240. MakeHead
  241. forcelog Make_globs; Make_globs="$(logname Make_globs)"
  242. forcelog Make_xslt; Make_xslt="$(logname Make_xslt)"
  243. forcelog Make_copy; Make_copy="$(logname Make_copy)"
  244. forcelog Make_sourcecopy; Make_sourcecopy="$(logname Make_sourcecopy)"
  245. Make_xhtml="$(logname Make_xhtml)"
  246. trap "trap - 0 2 3 6 9 15; killall \"${0##*/}\"" 0 2 3 6 9 15
  247. [ "$regen_globs" = false -a -s "$Make_globs" ] && \
  248. glob_additions "$@" >>"$Make_globs" \
  249. || glob_makers >"$Make_globs" &
  250. [ "$regen_xslt" = false -a -s "$Make_xslt" ] && \
  251. xslt_additions "$@" >>"$Make_xslt" \
  252. || xslt_makers >"$Make_xslt" &
  253. [ "$regen_copy" = false -a -s "$Make_copy" ] && \
  254. copy_additions "$@" >>"$Make_copy" \
  255. || copy_makers >"$Make_copy" &
  256. [ "$regen_xhtml" = false -a -s "$Make_sourcecopy" ] && \
  257. copy_sourceadditions "$@" >>"$Make_sourcecopy" \
  258. || copy_sources >"$Make_sourcecopy" &
  259. if [ "$regen_xhtml" = false -a -s "$Make_xhtml" ]; then
  260. cat "$Make_xhtml"
  261. xhtml_additions "$@" |tee -a "$Make_xhtml"
  262. else
  263. xhtml_makers |tee "$Make_xhtml"
  264. fi
  265. wait
  266. trap - 0 2 3 6 9 15
  267. cat "$Make_globs" "$Make_xslt" "$Make_copy" "$Make_sourcecopy"
  268. }