]> git.saurik.com Git - apt.git/blame_incremental - CMake/Translations.cmake
CMake: Translations: Build byproduct before output
[apt.git] / CMake / Translations.cmake
... / ...
CommitLineData
1# translations.cmake - Translations using APT's translation system.
2# Copyright (C) 2009, 2016 Julian Andres Klode <jak@debian.org>
3
4function(apt_add_translation_domain)
5 set(options)
6 set(oneValueArgs DOMAIN)
7 set(multiValueArgs TARGETS SCRIPTS EXCLUDE_LANGUAGES)
8 cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
9 # Build the list of source files of the target
10 set(files "")
11 set(abs_files "")
12 set(scripts "")
13 set(abs_scripts "")
14 set(targets ${NLS_TARGETS})
15 set(domain ${NLS_DOMAIN})
16 set(xgettext_params
17 --add-comments
18 --foreign
19 --package-name=${PROJECT_NAME}
20 --package-version=${PACKAGE_VERSION}
21 --msgid-bugs-address=${PACKAGE_MAIL}
22 )
23 foreach(source ${NLS_SCRIPTS})
24 path_join(file "${CMAKE_CURRENT_SOURCE_DIR}" "${source}")
25 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
26 list(APPEND scripts ${relfile})
27 list(APPEND abs_scripts ${file})
28 endforeach()
29 foreach(target ${targets})
30 get_target_property(source_dir ${target} SOURCE_DIR)
31 get_target_property(sources ${target} SOURCES)
32 foreach(source ${sources})
33 path_join(file "${source_dir}" "${source}")
34 file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR} ${file})
35 set(files ${files} ${relfile})
36 set(abs_files ${abs_files} ${file})
37 endforeach()
38
39 target_compile_definitions(${target} PRIVATE -DAPT_DOMAIN="${domain}")
40 endforeach()
41
42 if("${scripts}" STREQUAL "")
43 set(sh_pot "/dev/null")
44 else()
45 set(sh_pot ${CMAKE_CURRENT_BINARY_DIR}/${domain}.sh.pot)
46 # Create the template for this specific sub-domain
47 add_custom_command (OUTPUT ${sh_pot}
48 COMMAND xgettext ${xgettext_params} -L Shell
49 -o ${sh_pot} ${scripts}
50 DEPENDS ${abs_scripts}
51 VERBATIM
52 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
53 )
54 endif()
55
56
57 add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
58 COMMAND xgettext ${xgettext_params} -k_ -kN_
59 --keyword=P_:1,2
60 -o ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot ${files}
61 DEPENDS ${abs_files}
62 VERBATIM
63 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
64 )
65
66 # We are building a ${domain}.pot with a header for launchpad, but we also
67 # build a ${domain.pot}-tmp as a byproduct. The msgfmt command than depend
68 # on the byproduct while their target depends on the output, so that msgfmt
69 # does not have to be rerun if nothing in the template changed.
70 #
71 # Make sure the .pot-tmp has no line numbers, to avoid useless rebuilding
72 # of .mo files.
73 add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot
74 BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
75 COMMAND msgcomm --more-than=0 --omit-header --sort-by-file --add-location=file
76 ${sh_pot}
77 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
78 --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0
79 COMMAND msgcomm --more-than=0 --sort-by-file
80 ${sh_pot}
81 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
82 --output=${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot
83 COMMAND cmake -E copy_if_different
84 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp0
85 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
86 DEPENDS ${sh_pot}
87 ${CMAKE_CURRENT_BINARY_DIR}/${domain}.c.pot
88 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
89 )
90
91 # We need a target to depend on otherwise, the msgmerge might not get called
92 # with the make generator
93 add_custom_target(nls-${domain}-template DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot)
94
95 # Build .mo files
96 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
97 list(SORT translations)
98 foreach(file ${translations})
99 get_filename_component(langcode ${file} NAME_WE)
100 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
101 continue()
102 endif()
103 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/locale/${langcode}/LC_MESSAGES)
104 file(MAKE_DIRECTORY ${outdir})
105 # Command to merge and compile the messages. As explained in the custom
106 # command for msgcomm, this depends on byproduct to avoid reruns
107 add_custom_command(OUTPUT ${outdir}/${domain}.po
108 COMMAND msgmerge -qo ${outdir}/${domain}.po ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
109 DEPENDS ${file} ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot-tmp
110 )
111 add_custom_command(OUTPUT ${outdir}/${domain}.mo
112 COMMAND msgfmt --statistics -o ${outdir}/${domain}.mo ${outdir}/${domain}.po
113 DEPENDS ${outdir}/${domain}.po
114 )
115
116 set(mofiles ${mofiles} ${outdir}/${domain}.mo)
117 install(FILES ${outdir}/${domain}.mo
118 DESTINATION "${CMAKE_INSTALL_LOCALEDIR}/${langcode}/LC_MESSAGES")
119 endforeach(file ${translations})
120
121 add_custom_target(nls-${domain} ALL DEPENDS ${mofiles} nls-${domain}-template)
122endfunction()
123
124# Usage: apt_add_update_po(output domain [domain ...])
125function(apt_add_update_po)
126 set(options)
127 set(oneValueArgs TEMPLATE)
128 set(multiValueArgs DOMAINS EXCLUDE_LANGUAGES)
129 cmake_parse_arguments(NLS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
130 set(output ${CMAKE_CURRENT_SOURCE_DIR}/${NLS_TEMPLATE}.pot)
131 foreach(domain ${NLS_DOMAINS})
132 list(APPEND potfiles ${CMAKE_CURRENT_BINARY_DIR}/${domain}.pot)
133 endforeach()
134
135 get_filename_component(master_name ${output} NAME_WE)
136 add_custom_target(nls-${master_name}
137 COMMAND msgcomm --sort-by-file --add-location=file
138 --more-than=0 --output=${output}
139 ${potfiles}
140 DEPENDS ${potfiles})
141
142 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
143 if (NOT TARGET update-po)
144 add_custom_target(update-po)
145 endif()
146 foreach(translation ${translations})
147 get_filename_component(langcode ${translation} NAME_WE)
148 if ("${langcode}" IN_LIST NLS_EXCLUDE_LANGUAGES)
149 continue()
150 endif()
151 add_custom_target(update-po-${langcode}
152 COMMAND msgmerge -q --previous --update --backup=none ${translation} ${output}
153 DEPENDS nls-${master_name}
154 )
155 add_dependencies(update-po update-po-${langcode})
156 endforeach()
157 add_dependencies(update-po nls-${master_name})
158endfunction()
159
160function(apt_add_po_statistics excluded)
161 add_custom_target(statistics)
162 file(GLOB translations "${PROJECT_SOURCE_DIR}/po/*.po")
163 foreach(translation ${translations})
164 get_filename_component(langcode ${translation} NAME_WE)
165 if ("${langcode}" IN_LIST excluded)
166 add_custom_command(
167 TARGET statistics PRE_BUILD
168 COMMAND printf "%-6s " "${langcode}:"
169 COMMAND echo "ignored"
170 VERBATIM
171 )
172 continue()
173 endif()
174 add_custom_command(
175 TARGET statistics PRE_BUILD
176 COMMAND printf "%-6s " "${langcode}:"
177 COMMAND msgfmt --statistics -o /dev/null ${translation}
178 VERBATIM
179 )
180 endforeach()
181endfunction()