Documentation.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. # po4a/docbook documentation support for CMake
  2. # - see documentation of add_docbook()
  3. #
  4. # Copyright (C) 2016 Julian Andres Klode <jak@debian.org>.
  5. #
  6. # Permission is hereby granted, free of charge, to any person
  7. # obtaining a copy of this software and associated documentation files
  8. # (the "Software"), to deal in the Software without restriction,
  9. # including without limitation the rights to use, copy, modify, merge,
  10. # publish, distribute, sublicense, and/or sell copies of the Software,
  11. # and to permit persons to whom the Software is furnished to do so,
  12. # subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be
  15. # included in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. find_path(DOCBOOK_XSL manpages/docbook.xsl
  26. # Debian
  27. /usr/share/xml/docbook/stylesheet/docbook-xsl
  28. /usr/share/xml/docbook/stylesheet/nwalsh
  29. # OpenSUSE
  30. /usr/share/xml/docbook/stylesheet/nwalsh/current
  31. # Arch
  32. /usr/share/xml/docbook/xsl-stylesheets
  33. # Fedora
  34. /usr/share/sgml/docbook/xsl-stylesheets
  35. # Fink
  36. ${CMAKE_INSTALL_PREFIX}/share/xml/xsl/docbook-xsl
  37. # FreeBSD
  38. ${CMAKE_INSTALL_PREFIX}/share/xsl/docbook/
  39. NO_DEFAULT_PATH)
  40. if(NOT DOCBOOK_XSL)
  41. message(FATAL_ERROR "Could not find docbook xsl")
  42. endif()
  43. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docbook-text-style.xsl.cmake.in
  44. ${CMAKE_CURRENT_BINARY_DIR}/docbook-text-style.xsl)
  45. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docbook-html-style.xsl.cmake.in
  46. ${CMAKE_CURRENT_BINARY_DIR}/docbook-html-style.xsl)
  47. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/manpage-style.xsl.cmake.in
  48. ${CMAKE_CURRENT_BINARY_DIR}/manpage-style.xsl)
  49. # Split up a string of the form DOCUMENT[.DOCUMENT][.LANGUAGE][.SECTION].EXTENSION
  50. #
  51. # There might be up to two parts in the document name. The language must be
  52. # a two char language code like de, or a 5 char code of the form de_DE.
  53. function(po4a_components doc lang sec ext translated_full_document)
  54. get_filename_component(name ${translated_full_document} NAME)
  55. string(REPLACE "." ";" name "${name}") # Make it a list
  56. list(GET name 0 _doc) # First element is always the document
  57. list(GET name 1 _lang) # Second *might* be a language
  58. list(GET name -2 _sec) # Second-last *might* be a section
  59. list(GET name -1 _ext) # Last element is always the file type
  60. # If the language code is neither a file type, nor a section, nor a language
  61. # assume it is part of the file name and use the next component as the lang.
  62. if(_lang AND NOT _lang MATCHES "^(xml|dbk|[0-9]|[a-z][a-z]|[a-z][a-z]_[A-Z][A-Z])$")
  63. set(_doc "${_doc}.${_lang}")
  64. list(GET name 2 _lang)
  65. endif()
  66. # If no language is present, we get a section; both not present => type
  67. if(_lang MATCHES "xml|dbk|[0-9]")
  68. set(_lang "")
  69. endif()
  70. if(NOT _sec MATCHES "^[0-9]$") # A (manpage) section must be a number
  71. set(_sec "")
  72. endif()
  73. set(${doc} ${_doc} PARENT_SCOPE)
  74. set(${lang} ${_lang} PARENT_SCOPE)
  75. set(${sec} ${_sec} PARENT_SCOPE)
  76. set(${ext} ${_ext} PARENT_SCOPE)
  77. endfunction()
  78. # Process one document
  79. function(po4a_one stamp_out out full_document language deps)
  80. path_join(full_path "${CMAKE_CURRENT_SOURCE_DIR}" "${full_document}")
  81. po4a_components(document _ section ext "${full_document}")
  82. # Calculate target file name
  83. set(dest "${language}/${document}.${language}")
  84. if(section)
  85. set(dest "${dest}.${section}")
  86. endif()
  87. # po4a might drop files not translated enough, so build a stamp file
  88. set(stamp ${CMAKE_CURRENT_BINARY_DIR}/${dest}.po4a-stamp)
  89. add_custom_command(
  90. OUTPUT ${stamp}
  91. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${language}
  92. COMMAND po4a --previous --no-backups
  93. --package-name='${PROJECT_NAME}-doc'
  94. --package-version='${PACKAGE_VERSION}'
  95. --msgid-bugs-address='${PACKAGE_MAIL}'
  96. --translate-only ${dest}.${ext}
  97. --srcdir ${CMAKE_CURRENT_SOURCE_DIR}
  98. --destdir ${CMAKE_CURRENT_BINARY_DIR}
  99. ${CMAKE_CURRENT_SOURCE_DIR}/po4a.conf
  100. COMMAND ${CMAKE_COMMAND} -E touch ${stamp}
  101. COMMENT "Generating ${dest}.${ext} (or dropping it)"
  102. DEPENDS ${full_document} ${deps} po/${language}.po
  103. )
  104. # Return result
  105. set(${stamp_out} ${stamp} PARENT_SCOPE)
  106. set(${out} ${CMAKE_CURRENT_BINARY_DIR}/${dest}.${ext} PARENT_SCOPE)
  107. endfunction()
  108. function(xsltproc_one)
  109. set(generated "")
  110. set(options HTML TEXT MANPAGE)
  111. set(oneValueArgs STAMP STAMP_OUT FULL_DOCUMENT)
  112. set(multiValueArgs INSTALL DEPENDS)
  113. cmake_parse_arguments(DOC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  114. po4a_components(document language section ext "${DOC_FULL_DOCUMENT}")
  115. # Default parameters
  116. set(params
  117. --nonet
  118. --xinclude
  119. --stringparam chunk.quietly yes
  120. --stringparam man.output.quietly yes
  121. --path ${PROJECT_SOURCE_DIR}/vendor/${CURRENT_VENDOR}/
  122. --path ${CMAKE_CURRENT_SOURCE_DIR}/
  123. )
  124. # Parameters if localized
  125. if(language)
  126. list(APPEND params -stringparam l10n.gentext.default.language ${language})
  127. endif()
  128. path_join(full_input_path ${CMAKE_CURRENT_SOURCE_DIR} ${DOC_FULL_DOCUMENT})
  129. if (DOC_MANPAGE)
  130. if (language)
  131. set(manpage_output "${CMAKE_CURRENT_BINARY_DIR}/${language}/${document}.${section}")
  132. else()
  133. set(manpage_output "${CMAKE_CURRENT_BINARY_DIR}/${document}.${section}")
  134. endif()
  135. set(manpage_stylesheet "${CMAKE_CURRENT_BINARY_DIR}/manpage-style.xsl")
  136. set(manpage_params)
  137. install(FILES ${manpage_output}
  138. DESTINATION ${CMAKE_INSTALL_MANDIR}/${language}/man${section}
  139. OPTIONAL)
  140. endif()
  141. if (DOC_HTML)
  142. if (language)
  143. set(html_output "${CMAKE_CURRENT_BINARY_DIR}/${language}/${document}.${language}.html")
  144. else()
  145. set(html_output "${CMAKE_CURRENT_BINARY_DIR}/${document}.html")
  146. endif()
  147. set(html_params --stringparam base.dir ${html_output})
  148. set(html_stylesheet "${CMAKE_CURRENT_BINARY_DIR}/docbook-html-style.xsl")
  149. install(DIRECTORY ${html_output}
  150. DESTINATION ${DOC_INSTALL}
  151. OPTIONAL)
  152. endif()
  153. if (DOC_TEXT)
  154. if (language)
  155. set(text_output "${CMAKE_CURRENT_BINARY_DIR}/${language}/${document}.${language}.text")
  156. else()
  157. set(text_output "${CMAKE_CURRENT_BINARY_DIR}/${document}.text")
  158. endif()
  159. set(text_params --stringparam base.dir ${text_output})
  160. set(text_stylesheet "${CMAKE_CURRENT_BINARY_DIR}/docbook-text-style.xsl")
  161. file(RELATIVE_PATH text_output_relative ${CMAKE_CURRENT_BINARY_DIR} ${text_output})
  162. add_custom_command(OUTPUT ${text_output}.w3m-stamp
  163. COMMAND ${PROJECT_SOURCE_DIR}/CMake/run_if_exists.sh
  164. --stdout ${text_output}
  165. ${text_output}.html
  166. env LC_ALL=C.UTF-8 w3m -cols 78 -dump
  167. -o display_charset=UTF-8
  168. -no-graph -T text/html ${text_output}.html
  169. COMMAND ${CMAKE_COMMAND} -E touch ${text_output}.w3m-stamp
  170. COMMENT "Generating ${text_output_relative} (if not dropped by po4a)"
  171. DEPENDS "${text_output}.html.xsltproc-stamp"
  172. )
  173. list(APPEND generated ${text_output}.w3m-stamp)
  174. install(FILES ${text_output}
  175. DESTINATION ${DOC_INSTALL}
  176. OPTIONAL)
  177. set(text_output "${text_output}.html")
  178. endif()
  179. foreach(type in manpage html text)
  180. if (NOT ${type}_output)
  181. continue()
  182. endif()
  183. set(output ${${type}_output})
  184. set(stylesheet ${${type}_stylesheet})
  185. set(type_params ${${type}_params})
  186. file(RELATIVE_PATH output_relative ${CMAKE_CURRENT_BINARY_DIR} ${output})
  187. add_custom_command(OUTPUT ${output}.xsltproc-stamp
  188. COMMAND ${PROJECT_SOURCE_DIR}/CMake/run_if_exists.sh
  189. ${full_input_path}
  190. xsltproc ${params} ${type_params} -o ${output}
  191. ${stylesheet}
  192. ${full_input_path}
  193. COMMAND ${CMAKE_COMMAND} -E touch ${output}.xsltproc-stamp
  194. COMMENT "Generating ${output_relative} (if not dropped by po4a)"
  195. DEPENDS ${DOC_STAMP} ${DOC_DEPENDS})
  196. list(APPEND generated ${output}.xsltproc-stamp)
  197. endforeach()
  198. set(${DOC_STAMP_OUT} ${generated} PARENT_SCOPE)
  199. endfunction()
  200. # add_docbook(Name [ALL] [HTML] [TEXT] [MANPAGE]
  201. # [INSTALL install dir]
  202. # [DEPENDS depend ...]
  203. # [DOCUMENTS documents ...]
  204. # [LINGUAS lingua ...])
  205. #
  206. # Generate a target called name with all the documents being converted to
  207. # the chosen output formats and translated to the chosen languages using po4a.
  208. #
  209. # For the translation support, the po4a.conf must be written so that
  210. # translations for a document guide.xml are written to LANG/guide.LANG.xml,
  211. # and for a manual page man.5.xml to a file called LANG/man.LANG.5.xml.
  212. #
  213. # The guide and manual page names may also contain a second component separated
  214. # by a dot, it must however not be a valid language code.
  215. #
  216. # Note that po4a might chose not to generate a translated manual page for a
  217. # given language if the translation rate is not high enough. We deal with this
  218. # by creating stamp files.
  219. function(add_docbook target)
  220. set(generated "")
  221. set(options HTML TEXT MANPAGE ALL)
  222. set(oneValueArgs)
  223. set(multiValueArgs INSTALL DOCUMENTS LINGUAS DEPENDS)
  224. cmake_parse_arguments(DOC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  225. if (DOC_HTML)
  226. list(APPEND formats HTML)
  227. endif()
  228. if (DOC_TEXT)
  229. list(APPEND formats TEXT)
  230. endif()
  231. if (DOC_MANPAGE)
  232. list(APPEND formats MANPAGE)
  233. endif()
  234. foreach(document ${DOC_DOCUMENTS})
  235. foreach(lang ${DOC_LINGUAS})
  236. po4a_one(po4a_stamp po4a_out ${document} "${lang}" "${DOC_DEPENDS}")
  237. xsltproc_one(STAMP_OUT xslt_stamp
  238. STAMP ${po4a_stamp}
  239. FULL_DOCUMENT ${po4a_out}
  240. INSTALL ${DOC_INSTALL}
  241. ${formats})
  242. list(APPEND stamps ${xslt_stamp})
  243. endforeach()
  244. xsltproc_one(STAMP_OUT xslt_stamp
  245. STAMP ${document}
  246. FULL_DOCUMENT ${document}
  247. INSTALL ${DOC_INSTALL}
  248. ${formats})
  249. list(APPEND stamps ${xslt_stamp})
  250. endforeach()
  251. if (DOC_ALL)
  252. add_custom_target(${target} ALL DEPENDS ${stamps})
  253. else()
  254. add_custom_target(${target} DEPENDS ${stamps})
  255. endif()
  256. endfunction()
  257. # Add an update-po4a target
  258. function(add_update_po4a target pot header)
  259. set(WRITE_HEADER "")
  260. if (header)
  261. set(WRITE_HEADER
  262. COMMAND sed -n "/^\#$/,$p" ${pot} > ${pot}.headerfree
  263. COMMAND cat ${header} ${pot}.headerfree > ${pot}
  264. COMMAND rm ${pot}.headerfree
  265. )
  266. endif()
  267. add_custom_target(${target}
  268. COMMAND po4a --previous --no-backups --force --no-translations
  269. --msgmerge-opt --add-location=file
  270. --porefs noline,wrap
  271. --package-name=${PROJECT_NAME}-doc --package-version=${PACKAGE_VERSION}
  272. --msgid-bugs-address=${PACKAGE_MAIL} po4a.conf
  273. ${WRITE_HEADER}
  274. VERBATIM
  275. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  276. )
  277. endfunction()