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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/usr/bin/env 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 = --build-env "${build_env:-development}" --source "$basedir" --statusdir "$statusdir" --domain "$domain"
  18. INPUTDIR = $input
  19. OUTPUTDIR = $output
  20. STATUSDIR = $statusdir
  21. LANGUAGES = $languages
  22. # -----------------------------------------------------------------------------
  23. # Build .html files from .xhtml sources
  24. # -----------------------------------------------------------------------------
  25. # All .xhtml source files
  26. HTML_SRC_FILES := \$(shell find "\$(INPUTDIR)" \
  27. -name '*.??.xhtml' \
  28. -not -path '\$(INPUTDIR)/.git/*' \
  29. )
  30. # All basenames of .xhtml source files (without .<lang>.xhtml ending)
  31. # Note: \$(sort ...) is used to remove duplicates
  32. HTML_SRC_BASES := \$(sort \$(basename \$(basename \$(HTML_SRC_FILES))))
  33. # All directories containing .xhtml source files
  34. HTML_SRC_DIRS := \$(sort \$(dir \$(HTML_SRC_BASES)))
  35. # The same as above, but moved to the output directory
  36. HTML_DST_BASES := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(HTML_SRC_BASES))
  37. # List of .<lang>.html files to build
  38. HTML_DST_FILES := \$(foreach base,\$(HTML_DST_BASES),\$(foreach lang,\$(LANGUAGES),\$(base).\$(lang).html))
  39. # .xmllist file used to build a html file
  40. XMLLIST_DEP = \$(wildcard \$(INPUTDIR)/\$(dir \$*).\$(notdir \$*).xmllist)
  41. # .xsl file used to build a html file
  42. XSL_DEP = \$(firstword \$(wildcard \$(INPUTDIR)/\$*.xsl) \$(INPUTDIR)/\$(dir \$*).default.xsl)
  43. all: \$(HTML_DST_FILES)
  44. EOF
  45. for lang in ${languages}; do
  46. cat<<EOF
  47. \$(filter %.${lang}.html,\$(HTML_DST_FILES)): \$(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
  48. echo "* Building \$*.${lang}.html"
  49. \${PROCESSOR} \${PROCFLAGS} process_file "\$(INPUTDIR)/\$*.${lang}.xhtml" > "\$@"
  50. EOF
  51. done
  52. cat <<EOF
  53. # -----------------------------------------------------------------------------
  54. # Create index.* symlinks
  55. # -----------------------------------------------------------------------------
  56. # All .xhtml source files with the same name as their parent directory
  57. INDEX_SRC_FILES := \$(wildcard \$(foreach directory,\$(HTML_SRC_DIRS),\$(directory)\$(notdir \$(directory:/=)).??.xhtml))
  58. # All basenames of .xhtml source files with the same name as their parent
  59. # directory
  60. INDEX_SRC_BASES := \$(sort \$(basename \$(basename \$(INDEX_SRC_FILES))))
  61. # All directories containing .xhtml source files with the same name as their
  62. # parent directory (that is, all directories in which index files should be
  63. # created)
  64. INDEX_SRC_DIRS := \$(dir \$(INDEX_SRC_BASES))
  65. # The same as above, but moved to the output directory
  66. INDEX_DST_DIRS := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(INDEX_SRC_DIRS))
  67. # List of index.<lang>.html symlinks to create
  68. INDEX_DST_LINKS := \$(foreach base,\$(INDEX_DST_DIRS),\$(foreach lang,\$(LANGUAGES),\$(base)index.\$(lang).html))
  69. all: \$(INDEX_DST_LINKS)
  70. EOF
  71. for lang in ${languages}; do
  72. cat<<EOF
  73. \$(filter %/index.${lang}.html,\$(INDEX_DST_LINKS)): \$(OUTPUTDIR)/%/index.${lang}.html:
  74. echo "* Creating symlink \$*/index.${lang}.html"
  75. ln -sf "\$(notdir \$*).${lang}.html" "\$@"
  76. EOF
  77. done
  78. cat <<EOF
  79. # -----------------------------------------------------------------------------
  80. # Create symlinks from file.<lang>.html to file.html.<lang>
  81. # -----------------------------------------------------------------------------
  82. # List of .html.<lang> symlinks to create
  83. HTML_DST_LINKS := \$(foreach base,\$(HTML_DST_BASES) \$(addsuffix index,\$(INDEX_DST_DIRS)),\$(foreach lang,\$(LANGUAGES),\$(base).html.\$(lang)))
  84. all: \$(HTML_DST_LINKS)
  85. EOF
  86. for lang in ${languages}; do
  87. cat<<EOF
  88. \$(OUTPUTDIR)/%.html.${lang}:
  89. echo "* Creating symlink \$*.html.${lang}"
  90. ln -sf "\$(notdir \$*).${lang}.html" "\$@"
  91. EOF
  92. done
  93. cat <<EOF
  94. # -----------------------------------------------------------------------------
  95. # Build .rss files from .xhtml sources
  96. # -----------------------------------------------------------------------------
  97. # All .rss.xsl scripts which can create .rss output
  98. RSS_SRC_SCRIPTS := \$(shell find "\$(INPUTDIR)" \
  99. -name '*.rss.xsl' \
  100. -not -path '\$(INPUTDIR)/.git/*' \
  101. )
  102. # All basenames of .xhtml source files from which .rss files should be built
  103. RSS_SRC_BASES := \$(sort \$(basename \$(basename \$(RSS_SRC_SCRIPTS))))
  104. # The same as above, but moved to the output directory
  105. RSS_DST_BASES := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(RSS_SRC_BASES))
  106. # List of .<lang>.rss files to build
  107. RSS_DST_FILES := \$(foreach base,\$(RSS_DST_BASES),\$(foreach lang,\$(LANGUAGES),\$(base).\$(lang).rss))
  108. all: \$(RSS_DST_FILES)
  109. EOF
  110. for lang in ${languages}; do
  111. cat<<EOF
  112. \$(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
  113. echo "* Building \$*.${lang}.rss"
  114. \${PROCESSOR} \${PROCFLAGS} process_file "\$(INPUTDIR)/\$*.${lang}.xhtml" "\$(INPUTDIR)/\$*.rss.xsl" > "\$@"
  115. EOF
  116. done
  117. cat <<EOF
  118. # -----------------------------------------------------------------------------
  119. # Build .ics files from .xhtml sources
  120. # -----------------------------------------------------------------------------
  121. # All .ics.xsl scripts which can create .ics output
  122. ICS_SRC_SCRIPTS := \$(shell find "\$(INPUTDIR)" \
  123. -name '*.ics.xsl' \
  124. -not -path '\$(INPUTDIR)/.git/*' \
  125. )
  126. # All basenames of .xhtml source files from which .ics files should be built
  127. ICS_SRC_BASES := \$(sort \$(basename \$(basename \$(ICS_SRC_SCRIPTS))))
  128. # The same as above, but moved to the output directory
  129. ICS_DST_BASES := \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(ICS_SRC_BASES))
  130. # List of .<lang>.ics files to build
  131. ICS_DST_FILES := \$(foreach base,\$(ICS_DST_BASES),\$(foreach lang,\$(LANGUAGES),\$(base).\$(lang).ics))
  132. all: \$(ICS_DST_FILES)
  133. EOF
  134. for lang in ${languages}; do
  135. cat<<EOF
  136. \$(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
  137. echo "* Building \$*.${lang}.ics"
  138. \${PROCESSOR} \${PROCFLAGS} process_file "\$(INPUTDIR)/\$*.${lang}.xhtml" "\$(INPUTDIR)/\$*.ics.xsl" > "\$@"
  139. EOF
  140. done
  141. cat <<EOF
  142. # -----------------------------------------------------------------------------
  143. # Copy images, docments etc
  144. # -----------------------------------------------------------------------------
  145. # All files which should just be copied over
  146. COPY_SRC_FILES := \$(shell find "\$(INPUTDIR)" -type f \
  147. -not -path '\$(INPUTDIR)/.git/*' \
  148. -not -path '\$(INPUTDIR)/build/*' \
  149. -not -path '\$(INPUTDIR)/tools/*' \
  150. -not -name '.drone.yml' \
  151. -not -name '.gitignore' \
  152. -not -name 'README*' \
  153. -not -name 'Makefile' \
  154. -not -name '*.sources' \
  155. -not -name "*.xmllist" \
  156. -not -name '*.xhtml' \
  157. -not -name '*.xml' \
  158. -not -name '*.xsl' \
  159. -not -name '*.outdated-translations' \
  160. )
  161. # The same as above, but moved to the output directory
  162. COPY_DST_FILES := \$(sort \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/%,\$(COPY_SRC_FILES)))
  163. all: \$(COPY_DST_FILES)
  164. \$(COPY_DST_FILES): \$(OUTPUTDIR)/%: \$(INPUTDIR)/%
  165. echo "* Copying file \$*"
  166. cp "\$<" "\$@"
  167. # -----------------------------------------------------------------------------
  168. # Copy .xhtml files to "source" directory in target directory tree
  169. # -----------------------------------------------------------------------------
  170. SOURCE_DST_FILES := \$(sort \$(patsubst \$(INPUTDIR)/%,\$(OUTPUTDIR)/source/%,\$(HTML_SRC_FILES)))
  171. all: \$(SOURCE_DST_FILES)
  172. \$(SOURCE_DST_FILES): \$(OUTPUTDIR)/source/%: \$(INPUTDIR)/%
  173. echo "* Copying source \$*"
  174. cp "\$<" "\$@"
  175. # -----------------------------------------------------------------------------
  176. # Clean up excess files in target directory
  177. # -----------------------------------------------------------------------------
  178. ALL_DST := \$(HTML_DST_FILES) \$(INDEX_DST_LINKS) \$(HTML_DST_LINKS) \$(RSS_DST_FILES) \$(ICS_DST_FILES) \$(COPY_DST_FILES) \$(SOURCE_DST_FILES)
  179. .PHONY: clean
  180. all: clean
  181. clean:
  182. # Write all destination filenames into "manifest" file, one per line
  183. \$(file >\$(STATUSDIR)/manifest)
  184. \$(foreach filename,\$(ALL_DST),\$(file >>\$(STATUSDIR)/manifest,\$(filename)))
  185. sort "\$(STATUSDIR)/manifest" > "\$(STATUSDIR)/manifest.sorted"
  186. find -L "\$(OUTPUTDIR)" -type f \\
  187. | sort \\
  188. | diff - "\$(STATUSDIR)/manifest.sorted" \\
  189. | sed -rn 's;^< ;;p' \\
  190. | while read file; do echo "* Deleting \$\${file}"; rm "\$\${file}"; done
  191. # -----------------------------------------------------------------------------
  192. EOF
  193. }