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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 -name .git -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"/*) printf '${INPUTDIR}/%s\n' "${each#${input}/}" ;;
  18. "$output"/*) printf '${OUTPUTDIR}/%s\n' "${each#${output}/}" ;;
  19. *) printf %s\\n "$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 ':X; $bY; N; bX; :Y;
  28. s;[ #];\\&;g; s;\n; ;g'
  29. }
  30. xhtml_maker(){
  31. # generate make rules for building html files out of xhtml
  32. # account for included xml files and xsl rules
  33. shortname="$input/$1"
  34. outpath="\${OUTPUTDIR}/${2}"
  35. outpath="${outpath%/*}"
  36. textsen="$(get_textsfile "en")"
  37. menufile="$basedir/tools/menu-global.xml"
  38. filedir="${shortname%/*}"
  39. shortbase="${shortname##*/}"
  40. processor="$(get_processor "$shortname")"
  41. [ -f "$shortname".rss.xsl ] && bool_rss=true || bool_rss=false
  42. [ -f "$shortname".ics.xsl ] && bool_ics=true || bool_ics=false
  43. olang="$(echo "${shortname}".[a-z][a-z].xhtml "${shortname}".[e]n.xhtml |sed -rn 's;^.*\.([a-z]{2})\.xhtml.*$;\1;p')"
  44. if [ "${shortbase}" = "${filedir##*/}" ] && \
  45. [ ! -f "${filedir}/index.${olang}.xhtml" ]; then
  46. bool_indexname=true
  47. else
  48. bool_indexname=false
  49. fi
  50. [ -f "${shortname}.sources" ] && sourcesfile="${shortname}.sources" || unset sourcesfile
  51. # For speed considerations: avoid all disk I/O in this loop
  52. for lang in $(get_languages); do
  53. infile="${shortname}.${lang}.xhtml"
  54. depfile="${shortname}.*.xhtml"
  55. infile="$(mio "$infile")"
  56. outbase="${shortbase}.${lang}.html"
  57. outfile="${outpath}/${outbase}"
  58. outlink="${outpath}/${shortbase}.html.$lang"
  59. rssfile="${outpath}/${shortbase}.${lang}.rss"
  60. icsfile="${outpath}/${shortbase}.${lang}.ics"
  61. textsfile="$(get_textsfile "$lang")"
  62. fundraisingfile="$(get_fundraisingfile "$lang")"
  63. # $bool_sourceinc && sourceglobs="${filedir#./}/._._${shortbase}.${lang}.refglobs" || unset sourceglobs
  64. cat <<MakeEND
  65. all: $(mes "$outfile" "$outlink")
  66. $(mes "$outfile"): $(mes "$depfile" "$processor" "$textsen" "$textsfile" "$fundraisingfile" "$menufile" "$sourcesfile")
  67. \${PROCESSOR} \${PROCFLAGS} process_file "${infile}" "$(mio "$processor")" "$olang" >"$outfile"
  68. $(mes "$outlink"):
  69. ln -sf "${outbase}" "${outlink}"
  70. MakeEND
  71. $bool_rss && cat<<MakeEND
  72. all: $(mes "$rssfile")
  73. $(mes "$rssfile"): $(mes "$depfile" "${shortname}.rss.xsl" "$textsen" "$textsfile" "$fundraisingfile" "$menufile" "$sourcesfile")
  74. \${PROCESSOR} \${PROCFLAGS} process_file "${infile}" "$(mio "${shortname}.rss.xsl")" "$olang" >"$rssfile"
  75. MakeEND
  76. $bool_ics && cat<<MakeEND
  77. all: $(mes "$icsfile")
  78. $(mes "$icsfile"): $(mes "$depfile" "${shortname}.ics.xsl" "$textsen" "$textsfile" "$fundraisingfile" "$menufile" "$sourcesfile")
  79. \${PROCESSOR} \${PROCFLAGS} process_file "${infile}" "$(mio "${shortname}.ics.xsl")" "$olang" >"$icsfile"
  80. MakeEND
  81. $bool_indexname && cat <<MakeEND
  82. all: $(mes "$outpath/index.${lang}.html" "$outpath/index.html.$lang")
  83. $(mes "$outpath/index.${lang}.html"):
  84. ln -sf "$outbase" "$outpath/index.${lang}.html"
  85. $(mes "$outpath/index.html.$lang"):
  86. ln -sf "$outbase" "$outpath/index.html.$lang"
  87. MakeEND
  88. done
  89. }
  90. xhtml_makers(){
  91. # generate make rules concerning all .xhtml files in source tree
  92. sourcefind -name '*.[a-z][a-z].xhtml' \
  93. | sed -r 's;\.[a-z][a-z]\.xhtml$;;' \
  94. | sort -u \
  95. | while read shortpath; do
  96. xhtml_maker "$shortpath" "${shortpath}"
  97. done
  98. }
  99. xhtml_additions(){
  100. printf "%s\n" "$@" \
  101. | sed -rn 's;\.[a-z][a-z]\.xhtml$;;p' \
  102. | sort -u \
  103. | xargs realpath \
  104. | while read addition; do
  105. xhtml_maker "${addition#$input/}" "${addition#$input/}"
  106. done
  107. }
  108. copy_maker(){
  109. # generate make rule for copying a plain file
  110. infile="\${INPUTDIR}/$1"
  111. outfile="\${OUTPUTDIR}/$2"
  112. cat <<MakeEND
  113. all: $(mes "$outfile")
  114. $(mes "$outfile"): $(mes "$infile")
  115. cp "$infile" "$outfile"
  116. MakeEND
  117. }
  118. copy_makers(){
  119. # generate copy rules for entire input tree
  120. sourcefind \! -name 'Makefile' \! -name '*.sourceglobs' \! -name '*.refglobs' \
  121. \! -name '*.sources' \! -name '*.xhtml' \! -name '*.xml' \
  122. \! -name '*.xsl' \! -name 'tagmap' \! -name '*.langglob' \
  123. | while read filepath; do
  124. copy_maker "$filepath" "$filepath"
  125. done
  126. }
  127. copy_additions(){
  128. printf "%s\n" "$@" \
  129. | egrep -v '.+(\.sources|\.sourceglobs|\.refglobs|\.xhtml|\.xml|\.xsl|/Makefile|/)$' \
  130. | xargs realpath \
  131. | while read addition; do
  132. copy_maker "${addition#$input/}" "${addition#$input/}"
  133. done
  134. }
  135. xslt_dependencies(){
  136. # list referenced xsl files for a given xsl file
  137. # *not* recursive since Make will handle recursive
  138. # dependency resolution
  139. file="$1"
  140. cat "$file" \
  141. | tr '\n\t\r' ' ' \
  142. | sed -r 's;(<xsl:(include|import)[^>]*>);\n\1\n;g' \
  143. | sed -nr '/<xsl:(include|import)[^>]*>/s;^.*href *= *"([^"]*)".*$;\1;gp'
  144. }
  145. xslt_maker(){
  146. # find external references in a xsl file and generate
  147. # Make dependencies accordingly
  148. file="$input/$1"
  149. dir="${file%/*}"
  150. deps="$( xslt_dependencies "$file" |xargs -I'{}' realpath "$dir/{}" )"
  151. cat <<MakeEND
  152. $(mes "$file"): $(mes $deps)
  153. touch "$(mio "$file")"
  154. MakeEND
  155. }
  156. xslt_makers(){
  157. # generate make dependencies for all .xsl files in input tree
  158. sourcefind -name '*.xsl' \
  159. | while read filepath; do
  160. xslt_maker "$filepath"
  161. done
  162. }
  163. xslt_additions(){
  164. printf "%s\n" "$@" \
  165. | egrep '.+\.xsl$' \
  166. | xargs realpath \
  167. | while read addition; do
  168. xslt_maker "${addition#$input/}"
  169. done
  170. }
  171. copy_sources(){
  172. # generate rules to copy all .xhtml files in the input to
  173. # the public source directory
  174. sourcefind -name '*.xhtml' \
  175. | while read filepath; do
  176. copy_maker "$filepath" "source/$filepath"
  177. done
  178. }
  179. copy_sourceadditions(){
  180. printf "%s\n" "$@" \
  181. | egrep '.+\.xhtml$' \
  182. | xargs realpath \
  183. | while read addition; do
  184. copy_maker "${addition#$input/}" "source/${addition#$input/}"
  185. done
  186. }
  187. tree_maker(){
  188. # walk through file tree and issue Make rules according to file type
  189. input="$(realpath "$1")"
  190. output="$(realpath "$2")"
  191. shift 2
  192. cache_textsfile
  193. cache_fundraising
  194. cat <<MakeHead
  195. .PHONY: all
  196. PROCESSOR = "$basedir/build/process_file.sh"
  197. PGLOBBER = "$basedir/build/source_globber.sh"
  198. PROCFLAGS = --source "$basedir" --statusdir "$statusdir" --domain "$domain"
  199. INPUTDIR = $input
  200. OUTPUTDIR = $output
  201. # cannot store find results in variable because it will result in too many arguments for the shell
  202. # \${INPUTDIR}/tagmap: \$(shell find "$basedir" -name '*.[a-z][a-z].xml')
  203. # find "$basedir" -name '*.[a-z][a-z].xml' |xargs \${PGLOBBER} \${PROCFLAGS} map_tags >\${INPUTDIR}/tagmap
  204. MakeHead
  205. forcelog Make_globs; Make_globs="$(logname Make_globs)"
  206. forcelog Make_xslt; Make_xslt="$(logname Make_xslt)"
  207. forcelog Make_copy; Make_copy="$(logname Make_copy)"
  208. forcelog Make_sourcecopy; Make_sourcecopy="$(logname Make_sourcecopy)"
  209. Make_xhtml="$(logname Make_xhtml)"
  210. trap "trap - 0 2 3 6 9 15; killall \"${0##*/}\"" 0 2 3 6 9 15
  211. [ "$regen_xslt" = false -a -s "$Make_xslt" ] && \
  212. xslt_additions "$@" >>"$Make_xslt" \
  213. || xslt_makers >"$Make_xslt" &
  214. [ "$regen_copy" = false -a -s "$Make_copy" ] && \
  215. copy_additions "$@" >>"$Make_copy" \
  216. || copy_makers >"$Make_copy" &
  217. [ "$regen_xhtml" = false -a -s "$Make_sourcecopy" ] && \
  218. copy_sourceadditions "$@" >>"$Make_sourcecopy" \
  219. || copy_sources >"$Make_sourcecopy" &
  220. if [ "$regen_xhtml" = false -a -s "$Make_xhtml" ]; then
  221. cat "$Make_xhtml"
  222. xhtml_additions "$@" |tee -a "$Make_xhtml"
  223. else
  224. xhtml_makers |tee "$Make_xhtml"
  225. fi
  226. wait
  227. trap - 0 2 3 6 9 15
  228. cat "$Make_xslt" "$Make_copy" "$Make_sourcecopy"
  229. }