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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/bin/bash
  2. inc_makerules=true
  3. [ -z "$inc_languages" ] && . "$basedir/build/languages.sh"
  4. tree_maker(){
  5. # walk through file tree and issue Make rules according to file type
  6. input="$(realpath "$1")"
  7. output="$(realpath "$2")"
  8. languages=$(get_languages)
  9. cat <<EOF
  10. # -----------------------------------------------------------------------------
  11. # Makefile for FSFE website build, phase 2
  12. # -----------------------------------------------------------------------------
  13. .PHONY: all
  14. .DELETE_ON_ERROR:
  15. .SECONDEXPANSION:
  16. PROCESSOR = "$basedir/build/process_file.sh"
  17. PROCFLAGS = --source "$basedir" --statusdir "$statusdir" --domain "$domain"
  18. INPUTDIR = $input
  19. OUTPUTDIR = $output
  20. STATUSDIR = $statusdir
  21. LANGUAGES = $languages
  22. # -----------------------------------------------------------------------------
  23. # Touch all XSL files depending on a newer other XSL file
  24. # -----------------------------------------------------------------------------
  25. EOF
  26. # Generate make dependencies for all .xsl files in input tree
  27. find "${input}" -name '*.xsl' -not -name '.default.xsl' -printf '%P\n' \
  28. | while read xsl_file; do
  29. prerequisites=$(echo $(
  30. cat "${input}/${xsl_file}" \
  31. | tr '\n\t\r' ' ' \
  32. | sed -r 's;(<xsl:(include|import)[^>]*>);\n\1\n;g' \
  33. | sed -nr '/<xsl:(include|import)[^>]*>/s;^.*href *= *"([^"]*)".*$;\1;gp' \
  34. | xargs -I'{}' realpath "${input}/$(dirname ${xsl_file})/{}" \
  35. | sed -r "s;^${input};\$(INPUTDIR);"
  36. ))
  37. if [ -n "${prerequisites}" ]; then
  38. echo "all: \$(INPUTDIR)/${xsl_file}"
  39. echo "\$(INPUTDIR)/${xsl_file}: ${prerequisites}"
  40. echo ""
  41. fi
  42. done
  43. cat <<EOF
  44. %.xsl:
  45. @echo "* Touching \$*"
  46. @touch \$@
  47. # -----------------------------------------------------------------------------
  48. # Build .html files from .xhtml sources
  49. # -----------------------------------------------------------------------------
  50. # All .xhtml source files
  51. HTML_SRC_FILES := \$(shell find \$(INPUTDIR) -name '*.??.xhtml')
  52. # All basenames of .xhtml source files (without .<lang>.xhtml ending)
  53. # Note: \$(sort ...) is used to remove duplicates
  54. HTML_SRC_BASES := \$(sort \$(basename \$(basename \$(HTML_SRC_FILES))))
  55. # All directories containing .xhtml source files
  56. HTML_SRC_DIRS := \$(sort \$(dir \$(HTML_SRC_BASES)))
  57. # The same as above, but moved to the output directory
  58. HTML_DST_BASES := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(HTML_SRC_BASES))
  59. # List of .<lang>.html files to build
  60. HTML_DST_FILES := \$(foreach base,\$(HTML_DST_BASES),\$(foreach lang,\$(LANGUAGES),\$(base).\$(lang).html))
  61. # .xmllist file used to build a html file
  62. XMLLIST_DEP = \$(wildcard \$(INPUTDIR)/\$(dir \$*).\$(notdir \$*).xmllist)
  63. # .xsl file used to build a html file
  64. XSL_DEP = \$(firstword \$(wildcard \$(INPUTDIR)/\$*.xsl) \$(INPUTDIR)/\$(dir \$*).default.xsl)
  65. all: \$(HTML_DST_FILES)
  66. EOF
  67. for lang in ${languages}; do
  68. cat<<EOF
  69. \$(OUTPUTDIR)/%.${lang}.html: \$(INPUTDIR)/%.*.xhtml \$\$(XMLLIST_DEP) \$\$(XSL_DEP) \$(INPUTDIR)/tools/menu-global.xml \$(INPUTDIR)/tools/.texts-${lang}.xml \$(INPUTDIR)/tools/texts-en.xml \$(INPUTDIR)/.fundraising.${lang}.xml \$(INPUTDIR)/fundraising.en.xml
  70. @echo "* Building \$*.${lang}.html"
  71. @\${PROCESSOR} \${PROCFLAGS} process_file \$(INPUTDIR)/\$*.${lang}.xhtml > \$@
  72. EOF
  73. done
  74. cat <<EOF
  75. # -----------------------------------------------------------------------------
  76. # Create index.* symlinks
  77. # -----------------------------------------------------------------------------
  78. # All .xhtml source files with the same name as their parent directory
  79. INDEX_SRC_FILES := \$(wildcard \$(foreach directory,\$(HTML_SRC_DIRS),\$(directory)\$(notdir \$(directory:/=)).??.xhtml))
  80. # All basenames of .xhtml source files with the same name as their parent
  81. # directory
  82. INDEX_SRC_BASES := \$(sort \$(basename \$(basename \$(INDEX_SRC_FILES))))
  83. # All directories containing .xhtml source files with the same name as their
  84. # parent directory (that is, all directories in which index files should be
  85. # created)
  86. INDEX_SRC_DIRS := \$(dir \$(INDEX_SRC_BASES))
  87. # The same as above, but moved to the output directory
  88. INDEX_DST_DIRS := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(INDEX_SRC_DIRS))
  89. # List of index.<lang>.html symlinks to create
  90. INDEX_DST_LINKS := \$(foreach base,\$(INDEX_DST_DIRS),\$(foreach lang,\$(LANGUAGES),\$(base)index.\$(lang).html))
  91. all: \$(INDEX_DST_LINKS)
  92. EOF
  93. for lang in ${languages}; do
  94. cat<<EOF
  95. \$(OUTPUTDIR)/%/index.${lang}.html:
  96. @echo "* Creating symlink \$*/index.${lang}.html"
  97. @ln -sf \$(notdir \$*).${lang}.html \$@
  98. EOF
  99. done
  100. cat <<EOF
  101. # -----------------------------------------------------------------------------
  102. # Create symlinks from file.<lang>.html to file.html.<lang>
  103. # -----------------------------------------------------------------------------
  104. # List of .html.<lang> symlinks to create
  105. HTML_DST_LINKS := \$(foreach base,\$(HTML_DST_BASES) \$(addsuffix index,\$(INDEX_DST_DIRS)),\$(foreach lang,\$(LANGUAGES),\$(base).html.\$(lang)))
  106. all: \$(HTML_DST_LINKS)
  107. EOF
  108. for lang in ${languages}; do
  109. cat<<EOF
  110. \$(OUTPUTDIR)/%.html.${lang}:
  111. @echo "* Creating symlink \$*.html.${lang}"
  112. @ln -sf \$(notdir \$*).${lang}.html \$@
  113. EOF
  114. done
  115. cat <<EOF
  116. # -----------------------------------------------------------------------------
  117. # Build .rss files from .xhtml sources
  118. # -----------------------------------------------------------------------------
  119. # All .rss.xsl scripts which can create .rss output
  120. RSS_SRC_SCRIPTS := \$(shell find \$(INPUTDIR) -name '*.rss.xsl')
  121. # All basenames of .xhtml source files from which .rss files should be built
  122. RSS_SRC_BASES := \$(sort \$(basename \$(basename \$(RSS_SRC_SCRIPTS))))
  123. # The same as above, but moved to the output directory
  124. RSS_DST_BASES := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(RSS_SRC_BASES))
  125. # List of .<lang>.rss files to build
  126. RSS_DST_FILES := \$(foreach base,\$(RSS_DST_BASES),\$(foreach lang,\$(LANGUAGES),\$(base).\$(lang).rss))
  127. all: \$(RSS_DST_FILES)
  128. EOF
  129. for lang in ${languages}; do
  130. cat<<EOF
  131. \$(OUTPUTDIR)/%.${lang}.rss: \$(INPUTDIR)/%.*.xhtml \$\$(XMLLIST_DEP) \$(INPUTDIR)/%.rss.xsl \$(INPUTDIR)/tools/menu-global.xml \$(INPUTDIR)/tools/.texts-${lang}.xml \$(INPUTDIR)/tools/texts-en.xml \$(INPUTDIR)/.fundraising.${lang}.xml \$(INPUTDIR)/fundraising.en.xml
  132. @echo "* Building \$*.${lang}.rss"
  133. @\${PROCESSOR} \${PROCFLAGS} process_file \$(INPUTDIR)/\$*.${lang}.xhtml \$(INPUTDIR)/\$*.rss.xsl > \$@
  134. EOF
  135. done
  136. cat <<EOF
  137. # -----------------------------------------------------------------------------
  138. # Build .ics files from .xhtml sources
  139. # -----------------------------------------------------------------------------
  140. # All .ics.xsl scripts which can create .ics output
  141. ICS_SRC_SCRIPTS := \$(shell find \$(INPUTDIR) -name '*.ics.xsl')
  142. # All basenames of .xhtml source files from which .ics files should be built
  143. ICS_SRC_BASES := \$(sort \$(basename \$(basename \$(ICS_SRC_SCRIPTS))))
  144. # The same as above, but moved to the output directory
  145. ICS_DST_BASES := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(ICS_SRC_BASES))
  146. # List of .<lang>.ics files to build
  147. ICS_DST_FILES := \$(foreach base,\$(ICS_DST_BASES),\$(foreach lang,\$(LANGUAGES),\$(base).\$(lang).ics))
  148. all: \$(ICS_DST_FILES)
  149. EOF
  150. for lang in ${languages}; do
  151. cat<<EOF
  152. \$(OUTPUTDIR)/%.${lang}.ics: \$(INPUTDIR)/%.*.xhtml \$\$(XMLLIST_DEP) \$(INPUTDIR)/%.ics.xsl \$(INPUTDIR)/tools/menu-global.xml \$(INPUTDIR)/tools/.texts-${lang}.xml \$(INPUTDIR)/tools/texts-en.xml \$(INPUTDIR)/.fundraising.${lang}.xml \$(INPUTDIR)/fundraising.en.xml
  153. @echo "* Building \$*.${lang}.ics"
  154. @\${PROCESSOR} \${PROCFLAGS} process_file \$(INPUTDIR)/\$*.${lang}.xhtml \$(INPUTDIR)/\$*.ics.xsl > \$@
  155. EOF
  156. done
  157. cat <<EOF
  158. # -----------------------------------------------------------------------------
  159. # Copy images, docments etc
  160. # -----------------------------------------------------------------------------
  161. # All files which should just be copied over
  162. COPY_SRC_FILES := \$(shell find \$(INPUTDIR) -type f \
  163. -not -path '\$(INPUTDIR)/.git/*' \
  164. -not -path '\$(INPUTDIR)/build/*' \
  165. -not -path '\$(INPUTDIR)/tools/*' \
  166. -not -name '.drone.yml' \
  167. -not -name '.gitignore' \
  168. -not -name 'README*' \
  169. -not -name 'Makefile' \
  170. -not -name '*.sources' \
  171. -not -name "*.xmllist" \
  172. -not -name '*.xhtml' \
  173. -not -name '*.xml' \
  174. -not -name '*.xsl' \
  175. )
  176. # The same as above, but moved to the output directory
  177. COPY_DST_FILES := \$(sort \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(COPY_SRC_FILES)))
  178. all: \$(COPY_DST_FILES)
  179. \$(COPY_DST_FILES): \$(OUTPUTDIR)/%: \$(INPUTDIR)/%
  180. @echo "* Copying file \$*"
  181. @cp \$< \$@
  182. # -----------------------------------------------------------------------------
  183. # Copy .xhtml files to "source" directory in target directory tree
  184. # -----------------------------------------------------------------------------
  185. SOURCE_DST_FILES := \$(sort \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/source/%,\$(HTML_SRC_FILES)))
  186. all: \$(SOURCE_DST_FILES)
  187. \$(SOURCE_DST_FILES): \$(OUTPUTDIR)/source/%: \$(INPUTDIR)/%
  188. @echo "* Copying source \$*"
  189. @cp \$< \$@
  190. # -----------------------------------------------------------------------------
  191. # Clean up excess files in target directory
  192. # -----------------------------------------------------------------------------
  193. ALL_DST := \$(HTML_DST_FILES) \$(INDEX_DST_LINKS) \$(HTML_DST_LINKS) \$(RSS_DST_FILES) \$(ICS_DST_FILES) \$(COPY_DST_FILES) \$(SOURCE_DST_FILES)
  194. .PHONY: clean
  195. all: clean
  196. clean:
  197. @# Write all destination filenames into "manifest" file, one per line
  198. \$(file >\$(STATUSDIR)/manifest)
  199. \$(foreach filename,\$(ALL_DST),\$(file >>\$(STATUSDIR)/manifest,\$(filename)))
  200. @sort \$(STATUSDIR)/manifest > \$(STATUSDIR)/manifest.sorted
  201. @find -L \$(OUTPUTDIR) -type f \\
  202. | sort \\
  203. | diff - \$(STATUSDIR)/manifest.sorted \\
  204. | sed -rn 's;^< ;;p' \\
  205. | while read file; do echo "* Deleting \$\${file}"; rm "\$\${file}"; done
  206. # -----------------------------------------------------------------------------
  207. EOF
  208. }