Documentation.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. # Split up a string of the form DOCUMENT[.DOCUMENT][.LANGUAGE][.SECTION].EXTENSION
  26. #
  27. # There might be up to two parts in the document name. The language must be
  28. # a two char language code like de, or a 5 char code of the form de_DE.
  29. function(po4a_components doc lang sec ext translated_full_document)
  30. get_filename_component(name ${translated_full_document} NAME)
  31. string(REPLACE "." ";" name "${name}") # Make it a list
  32. list(GET name 0 _doc) # First element is always the document
  33. list(GET name 1 _lang) # Second *might* be a language
  34. list(GET name -2 _sec) # Second-last *might* be a section
  35. list(GET name -1 _ext) # Last element is always the file type
  36. # If the language code is neither a file type, nor a section, nor a language
  37. # assume it is part of the file name and use the next component as the lang.
  38. if(_lang AND NOT _lang MATCHES "^(xml|dbk|[0-9]|[a-z][a-z]|[a-z][a-z]_[A-Z][A-Z])$")
  39. set(_doc "${_doc}.${_lang}")
  40. list(GET name 2 _lang)
  41. endif()
  42. # If no language is present, we get a section; both not present => type
  43. if(_lang MATCHES "xml|dbk|[0-9]")
  44. set(_lang "")
  45. endif()
  46. if(NOT _sec MATCHES "^[0-9]$") # A (manpage) section must be a number
  47. set(_sec "")
  48. endif()
  49. set(${doc} ${_doc} PARENT_SCOPE)
  50. set(${lang} ${_lang} PARENT_SCOPE)
  51. set(${sec} ${_sec} PARENT_SCOPE)
  52. set(${ext} ${_ext} PARENT_SCOPE)
  53. endfunction()
  54. # Process one document
  55. function(po4a_one stamp_out out full_document language deps)
  56. path_join(full_path "${CMAKE_CURRENT_SOURCE_DIR}" "${full_document}")
  57. po4a_components(document _ section ext "${full_document}")
  58. # Calculate target file name
  59. set(dest "${language}/${document}.${language}")
  60. if(section)
  61. set(dest "${dest}.${section}")
  62. endif()
  63. # po4a might drop files not translated enough, so build a stamp file
  64. set(stamp ${CMAKE_CURRENT_BINARY_DIR}/${dest}.po4a-stamp)
  65. add_custom_command(
  66. OUTPUT ${stamp}
  67. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${language}
  68. COMMAND po4a --previous --no-backups
  69. --package-name='${PROJECT}-doc'
  70. --package-version='${PACKAGE_VERSION}'
  71. --msgid-bugs-address='${PACKAGE_MAIL}'
  72. --translate-only ${dest}.${ext}
  73. --srcdir ${CMAKE_CURRENT_SOURCE_DIR}
  74. --destdir ${CMAKE_CURRENT_BINARY_DIR}
  75. ${CMAKE_CURRENT_SOURCE_DIR}/po4a.conf
  76. COMMAND ${CMAKE_COMMAND} -E touch ${stamp}
  77. COMMENT "Generating ${dest}.${ext} (or dropping it)"
  78. DEPENDS ${full_document} ${deps} po/${language}.po
  79. )
  80. # Return result
  81. set(${stamp_out} ${stamp} PARENT_SCOPE)
  82. set(${out} ${CMAKE_CURRENT_BINARY_DIR}/${dest}.${ext} PARENT_SCOPE)
  83. endfunction()
  84. function(xsltproc_one)
  85. set(generated "")
  86. set(options HTML TEXT MANPAGE)
  87. set(oneValueArgs STAMP STAMP_OUT FULL_DOCUMENT)
  88. set(multiValueArgs INSTALL DEPENDS)
  89. cmake_parse_arguments(DOC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  90. po4a_components(document language section ext "${DOC_FULL_DOCUMENT}")
  91. # Default parameters
  92. set(params
  93. --nonet
  94. --xinclude
  95. --stringparam chunk.quietly yes
  96. --stringparam man.output.quietly yes
  97. --path ${PROJECT_SOURCE_DIR}/vendor/${CURRENT_VENDOR}/
  98. --path ${CMAKE_CURRENT_SOURCE_DIR}/
  99. )
  100. # Parameters if localized
  101. if(language)
  102. list(APPEND params -stringparam l10n.gentext.default.language ${language})
  103. endif()
  104. path_join(full_input_path ${CMAKE_CURRENT_SOURCE_DIR} ${DOC_FULL_DOCUMENT})
  105. if (DOC_MANPAGE)
  106. if (language)
  107. set(manpage_output "${CMAKE_CURRENT_BINARY_DIR}/${language}/${document}.${section}")
  108. else()
  109. set(manpage_output "${CMAKE_CURRENT_BINARY_DIR}/${document}.${section}")
  110. endif()
  111. set(manpage_stylesheet "${CMAKE_CURRENT_SOURCE_DIR}/manpage-style.xsl")
  112. install(FILES ${manpage_output}
  113. DESTINATION ${CMAKE_INSTALL_MANDIR}/${language}/man${section}
  114. OPTIONAL)
  115. endif()
  116. if (DOC_HTML)
  117. if (language)
  118. set(html_output "${CMAKE_CURRENT_BINARY_DIR}/${language}/${document}.${language}.html")
  119. else()
  120. set(html_output "${CMAKE_CURRENT_BINARY_DIR}/${document}.html")
  121. endif()
  122. set(html_params --stringparam base.dir ${html_output})
  123. set(html_stylesheet "${CMAKE_CURRENT_SOURCE_DIR}/docbook-html-style.xsl")
  124. install(DIRECTORY ${html_output}
  125. DESTINATION ${DOC_INSTALL}
  126. OPTIONAL)
  127. endif()
  128. if (DOC_TEXT)
  129. if (language)
  130. set(text_output "${CMAKE_CURRENT_BINARY_DIR}/${language}/${document}.${language}.text")
  131. else()
  132. set(text_output "${CMAKE_CURRENT_BINARY_DIR}/${document}.text")
  133. endif()
  134. set(text_params --stringparam base.dir ${text_output})
  135. set(text_stylesheet "${CMAKE_CURRENT_SOURCE_DIR}/docbook-text-style.xsl")
  136. file(RELATIVE_PATH text_output_relative ${CMAKE_CURRENT_BINARY_DIR} ${text_output})
  137. add_custom_command(OUTPUT ${text_output}.w3m-stamp
  138. COMMAND ${PROJECT_SOURCE_DIR}/CMake/run_if_exists.sh
  139. --stdout ${text_output}
  140. ${text_output}.html
  141. env LC_ALL=C.UTF-8 w3m -cols 78 -dump
  142. -o display_charset=UTF-8
  143. -no-graph -T text/html ${text_output}.html
  144. COMMAND ${CMAKE_COMMAND} -E touch ${text_output}.w3m-stamp
  145. COMMENT "Generating ${text_output_relative} (if not dropped by po4a)"
  146. DEPENDS "${text_output}.html.xsltproc-stamp"
  147. )
  148. list(APPEND generated ${text_output}.w3m-stamp)
  149. install(FILES ${text_output}
  150. DESTINATION ${DOC_INSTALL}
  151. OPTIONAL)
  152. set(text_output "${text_output}.html")
  153. endif()
  154. foreach(type in manpage html text)
  155. if (NOT ${type}_output)
  156. continue()
  157. endif()
  158. set(output ${${type}_output})
  159. set(stylesheet ${${type}_stylesheet})
  160. set(type_params ${${type}_params})
  161. file(RELATIVE_PATH output_relative ${CMAKE_CURRENT_BINARY_DIR} ${output})
  162. add_custom_command(OUTPUT ${output}.xsltproc-stamp
  163. COMMAND ${PROJECT_SOURCE_DIR}/CMake/run_if_exists.sh
  164. ${full_input_path}
  165. xsltproc ${params} ${type_params} -o ${output}
  166. ${stylesheet}
  167. ${full_input_path}
  168. COMMAND ${CMAKE_COMMAND} -E touch ${output}.xsltproc-stamp
  169. COMMENT "Generating ${output_relative} (if not dropped by po4a)"
  170. DEPENDS ${DOC_STAMP} ${DOC_DEPENDS})
  171. list(APPEND generated ${output}.xsltproc-stamp)
  172. endforeach()
  173. set(${DOC_STAMP_OUT} ${generated} PARENT_SCOPE)
  174. endfunction()
  175. # add_docbook(Name [ALL] [HTML] [TEXT] [MANPAGE]
  176. # [INSTALL install dir]
  177. # [DEPENDS depend ...]
  178. # [DOCUMENTS documents ...]
  179. # [LINGUAS lingua ...])
  180. #
  181. # Generate a target called name with all the documents being converted to
  182. # the chosen output formats and translated to the chosen languages using po4a.
  183. #
  184. # For the translation support, the po4a.conf must be written so that
  185. # translations for a document guide.xml are written to LANG/guide.LANG.xml,
  186. # and for a manual page man.5.xml to a file called LANG/man.LANG.5.xml.
  187. #
  188. # The guide and manual page names may also contain a second component separated
  189. # by a dot, it must however not be a valid language code.
  190. #
  191. # Note that po4a might chose not to generate a translated manual page for a
  192. # given language if the translation rate is not high enough. We deal with this
  193. # by creating stamp files.
  194. function(add_docbook target)
  195. set(generated "")
  196. set(options HTML TEXT MANPAGE ALL)
  197. set(multiValueArgs INSTALL DOCUMENTS LINGUAS DEPENDS)
  198. cmake_parse_arguments(DOC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  199. if (DOC_HTML)
  200. list(APPEND formats HTML)
  201. endif()
  202. if (DOC_TEXT)
  203. list(APPEND formats TEXT)
  204. endif()
  205. if (DOC_MANPAGE)
  206. list(APPEND formats MANPAGE)
  207. endif()
  208. foreach(document ${DOC_DOCUMENTS})
  209. foreach(lang ${DOC_LINGUAS})
  210. po4a_one(po4a_stamp po4a_out ${document} "${lang}" "${DOC_DEPENDS}")
  211. xsltproc_one(STAMP_OUT xslt_stamp
  212. STAMP ${po4a_stamp}
  213. FULL_DOCUMENT ${po4a_out}
  214. INSTALL ${DOC_INSTALL}
  215. ${formats})
  216. list(APPEND stamps ${xslt_stamp})
  217. endforeach()
  218. xsltproc_one(STAMP_OUT xslt_stamp
  219. STAMP ${document}
  220. FULL_DOCUMENT ${document}
  221. INSTALL ${DOC_INSTALL}
  222. ${formats})
  223. list(APPEND stamps ${xslt_stamp})
  224. endforeach()
  225. if (DOC_ALL)
  226. add_custom_target(${target} ALL DEPENDS ${stamps})
  227. else()
  228. add_custom_target(${target} DEPENDS ${stamps})
  229. endif()
  230. endfunction()
  231. # Add an update-po4a target
  232. function(add_update_po4a target pot header)
  233. set(WRITE_HEADER "")
  234. if (header)
  235. set(WRITE_HEADER
  236. COMMAND sed -n "/^\#$/,$p" ${pot} > ${pot}.headerfree
  237. COMMAND cat ${header} ${pot}.headerfree > ${pot}
  238. COMMAND rm ${pot}.headerfree
  239. )
  240. endif()
  241. add_custom_target(${target}
  242. COMMAND po4a --previous --no-backups --force --no-translations
  243. --msgmerge-opt --add-location=file
  244. --porefs noline,wrap
  245. --package-name=${PROJECT_NAME}-doc --package-version=${PACKAGE_VERSION}
  246. --msgid-bugs-address=${PACKAGE_MAIL} po4a.conf
  247. ${WRITE_HEADER}
  248. VERBATIM
  249. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  250. )
  251. endfunction()