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