]> git.lyx.org Git - lyx.git/blob - po/CMakeLists.txt
e690e40472fd40ee5aaadbc9ef2576251b7b1a39
[lyx.git] / po / CMakeLists.txt
1 # This file is part of LyX, the document processor.
2 # Licence details can be found in the file COPYING.
3 #
4 # Copyright (c) 2008-2011 Peter Kümmel, <syntheticpp@gmx.net>
5 # Copyright (c) 2008-2011 Kornel Benko, <Kornel.Benko@berlin.de>
6 #
7
8 project(po)
9
10 include_directories(${TOP_SRC_DIR}/po)
11
12 SET(_lyxname ${PACKAGE})
13 SET(_py_sources)
14
15 # Idea stolen from http://public.kitware.com/pipermail/cmake/2011-January/041666.html
16
17 macro(add_gettext_python  _par _dir)
18   set(_sources)
19   foreach(_arg ${ARGN})
20     file(GLOB _s1 ${TOP_SRC_DIR}/${_dir}/${_arg})
21     list(SORT _s1)
22     list(APPEND _sources ${_s1})
23   endforeach(_arg)
24   if (${_par} MATCHES "layouttranslations")
25     SET(_dst "${CMAKE_CURRENT_BINARY_DIR}/${_par}")
26   else ()
27     SET(_dst "${CMAKE_CURRENT_BINARY_DIR}/${_par}_l10n.pot")
28   endif()
29   SET(_tmp_src_files "${CMAKE_CURRENT_BINARY_DIR}/${_par}_tmp_files")
30   SET(_src_files "${CMAKE_CURRENT_BINARY_DIR}/${_par}_files")
31   file(WRITE ${_tmp_src_files} "")
32   foreach(_s ${_sources})
33     FILE(APPEND ${_tmp_src_files} "${_s}\n")
34   endforeach()
35   if(${_par} MATCHES "^\(.*\)_[0-9]+$")
36     set(_par1 ${CMAKE_MATCH_1})
37   else()
38     set(_par1 ${_par})
39   endif()
40   ADD_CUSTOM_COMMAND(
41     OUTPUT ${_src_files}
42     COMMAND ${CMAKE_COMMAND} -E copy_if_different ${_tmp_src_files} ${_src_files}
43     DEPENDS ${_tmp_src_files}
44   )
45   if (${_par} MATCHES "layouttranslations")
46     # lyx_pot.py requires the module "polib" if using target layouttranslations1,
47     # so we have to check for it
48     find_python_module("polib" REQUIRED)
49     ADD_CUSTOM_COMMAND(
50       OUTPUT "${_dst}"
51       PRE_BUILD
52       COMMAND  ${CMAKE_COMMAND}
53         "-DTOP_SRC_DIR=${TOP_SRC_DIR}"
54         "-DDestFile=${_dst}"
55         "-DLYX_PYTHON_EXECUTABLE=${LYX_PYTHON_EXECUTABLE}"
56         "-DParType=${_par1}"
57         "-DSources=${_src_files}"
58         -P "${TOP_SRC_DIR}/po/CreateLayoutTranslations.cmake"
59       DEPENDS ${_sources} ${_src_files} "${TOP_SRC_DIR}/lib/layouttranslations"
60       COMMENT "${LYX_PYTHON_EXECUTABLE} ${TOP_SRC_DIR}/po/lyx_pot.py -b ${TOP_SRC_DIR} -o  ${_dst} -t ${_par1} --src_file=${_src_files}"
61       )
62     ADD_CUSTOM_TARGET(layouttranslations1 DEPENDS "${_dst}")
63         set_target_properties(layouttranslations1 PROPERTIES FOLDER "i18n")
64     if (LYX_DATA_SUBDIR STREQUAL "")
65       install(FILES "${TOP_SRC_DIR}/lib/layouttranslations" DESTINATION .)
66     else()
67       install(FILES "${TOP_SRC_DIR}/lib/layouttranslations" DESTINATION ${LYX_DATA_SUBDIR})
68     endif()
69   else()
70     ADD_CUSTOM_COMMAND(
71       OUTPUT "${_dst}"
72       PRE_BUILD
73       COMMAND ${LYX_PYTHON_EXECUTABLE}
74       ARGS "${TOP_SRC_DIR}/po/lyx_pot.py" -b "${TOP_SRC_DIR}" -o "${_dst}" -t ${_par1} "--src_file=${_src_files}"
75       DEPENDS ${_sources} ${_src_files}
76       COMMENT "${LYX_PYTHON_EXECUTABLE} ${TOP_SRC_DIR}/po/lyx_pot.py -b ${TOP_SRC_DIR} -o  ${_dst} -t ${_par1} --src_file=${_src_files}"
77       )
78     LIST(APPEND _py_sources "${_dst}")
79   endif()
80   SET_SOURCE_FILES_PROPERTIES("${_dst}" GENERATED)
81   SET_SOURCE_FILES_PROPERTIES("${_src_files}" GENERATED)
82 endmacro(add_gettext_python)
83
84 SET_SOURCE_FILES_PROPERTIES("${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.cat.pot" GENERATED)
85 SET_SOURCE_FILES_PROPERTIES("${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.fmt.pot" GENERATED)
86 SET_SOURCE_FILES_PROPERTIES("${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.pot" GENERATED)
87
88 add_gettext_python(qt4 src/frontends/qt4/ui *.ui)
89 add_gettext_python(layouts lib/layouts *.layout *.inc *.module)
90 add_gettext_python(languages lib languages)
91 add_gettext_python(latexfonts lib latexfonts)
92 add_gettext_python(encodings lib encodings)
93 add_gettext_python(ui lib/ui *.ui *.inc)
94 add_gettext_python(external lib external_templates)
95 add_gettext_python(formats lib configure.py)
96 add_gettext_python(layouttranslations lib/layouts *.layout *.inc *.module)
97
98 ADD_CUSTOM_COMMAND(
99     OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.cat.pot"
100     COMMAND ${LYX_PYTHON_EXECUTABLE}
101     ARGS   "${TOP_CMAKE_PATH}/po/cat.py" ${_py_sources} > "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.cat_tmp.pot"
102     COMMAND ${CMAKE_COMMAND} -E copy_if_different
103       "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.cat_tmp.pot" "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.cat.pot"
104     DEPENDS ${_py_sources}
105     )
106
107 ADD_CUSTOM_COMMAND(
108     OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.fmt.pot"
109     COMMAND ${GETTEXT_MSGUNIQ_EXECUTABLE}
110     ARGS -o "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.fmt_tmp.pot" "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.cat.pot"
111     COMMAND ${CMAKE_COMMAND} -E copy_if_different
112       "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.fmt_tmp.pot" "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.fmt.pot"
113     DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.cat.pot"
114     )
115
116 SET(_potfiles_dep)
117
118 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in.tmp "")
119 file(GLOB_RECURSE _tmp_potfiles RELATIVE ${TOP_SRC_DIR}/ ${TOP_SRC_DIR}/src/*.h  ${TOP_SRC_DIR}/src/*.cpp  ${TOP_SRC_DIR}/src/*.cpp.in)
120 list(SORT _tmp_potfiles)
121 # list(REMOVE_ITEM _tmp_potfiles src/support/Package.cpp)
122
123 foreach(_f ${_tmp_potfiles})
124   # we don't need to select only relevant files because the outcome of xgettext
125   # will stay the same.
126   # Relevant files are files containing the string '_(".*")'
127   LIST(APPEND _potfiles_dep "${TOP_SRC_DIR}/${_f}")
128   file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in.tmp "${_f}\n")
129 endforeach(_f)
130
131 execute_process(
132   COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in.tmp" "${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in"
133   )
134
135 ADD_CUSTOM_COMMAND(
136     OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.pot"
137     COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.fmt.pot" "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.pot"
138     COMMAND ${GETTEXT_XGETTEXT_EXECUTABLE}
139       --from-code=UTF-8
140       --default-domain=${_lyxname} --directory=${TOP_SRC_DIR}
141       --add-comments=TRANSLATORS:
142       --language=C++ --join-existing --keyword=_ --keyword=N_ --keyword=B_ --keyword=qt_
143       --files-from=${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in
144       --copyright-holder="LyX Developers" --msgid-bugs-address=lyx-devel@lists.lyx.org
145       -o "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.pot"
146     DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.fmt.pot" ${_potfiles_dep}
147     )
148
149 #
150 # We have to copy the po-files first,
151 # otherwise they would be modified (inside the macro GETTEXT_CREATE_TRANSLATIONS())
152 # This makes the "git pull" on po-files work again
153 file(GLOB LYX_BASE_PO_FILES RELATIVE ${TOP_SRC_DIR}/po ${TOP_SRC_DIR}/po/*.po)
154 list(SORT LYX_BASE_PO_FILES)
155 set(LYX_PO_FILES)
156 SET(LYX_UPDATED_PO_FILES)
157 SET(LYX_DEST_PO ${TOP_SRC_DIR}/po)
158
159 set(CONFIGURED_LANGUAGES)               # from file LINGUAS
160 set(INSTALLED_LANGUAGES)                # from names of existing po-files
161
162 file(STRINGS "${TOP_SRC_DIR}/po/LINGUAS" _lang_lines)
163 foreach(_c_l ${_lang_lines})
164   if(NOT _c_l MATCHES "^#.*")
165     string(REPLACE " " ";" _c_l ${_c_l}) #Split ${_c_l}
166     list(APPEND CONFIGURED_LANGUAGES ${_c_l})
167   endif()
168 endforeach(_c_l)
169
170 foreach(_pofile ${LYX_BASE_PO_FILES})
171   string(REGEX REPLACE "^(.*)\\.po$" "\\1" _inst_lng ${_pofile})
172   list(FIND CONFIGURED_LANGUAGES ${_inst_lng} _lng_idx)
173   if(_lng_idx GREATER -1)
174     list(APPEND INSTALLED_LANGUAGES ${_inst_lng})
175   endif()
176   add_custom_command(
177     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_pofile}
178     COMMAND ${CMAKE_COMMAND} -E copy ${TOP_SRC_DIR}/po/${_pofile} ${CMAKE_CURRENT_BINARY_DIR}/${_pofile}
179     DEPENDS ${TOP_SRC_DIR}/po/${_pofile}
180     )
181   list(APPEND LYX_PO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${_pofile})
182 endforeach(_pofile)
183
184 FIND_PROGRAM(LYX_GITVERSION git)
185 set(LYX_UPDATED_GMO_FILES)
186 foreach(_gmo_base ${INSTALLED_LANGUAGES})
187   set(_gmo ${_gmo_base}.gmo)
188   set(_tobeupdated "${CMAKE_CURRENT_BINARY_DIR}/${_gmo}.tobeupdated")
189   list(APPEND LYX_UPDATED_GMO_FILES "${_tobeupdated}")
190   if(LYX_GITVERSION)
191     add_custom_command(
192       OUTPUT "${_tobeupdated}"
193           COMMAND "${LYX_GITVERSION}" "status" "--porcelain" "${_gmo_base}.po" ">" "${_tobeupdated}"
194           COMMAND ${LYX_PYTHON_EXECUTABLE} "${TOP_CMAKE_PATH}/po/update-gmo.py" 
195                 "${_tobeupdated}"
196                 "${CMAKE_CURRENT_BINARY_DIR}/${_gmo}"
197                 "${LYX_DEST_PO}/${_gmo}"
198           WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
199       DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_gmo}"
200       )
201   else()
202     add_custom_command(
203       OUTPUT "${_tobeupdated}"
204       COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_BINARY_DIR}/${_gmo}" "${LYX_DEST_PO}/${_gmo}"
205       COMMAND ${CMAKE_COMMAND} -E touch "${_tobeupdated}"
206       DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_gmo}"
207     )
208   endif()
209 endforeach()
210
211 set(LYX_UPDATED_PO_FILES)
212 foreach(_pofile ${LYX_BASE_PO_FILES})
213   if(WIN32)
214     add_custom_command(
215       OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_pofile}.copied
216       COMMAND ${LYX_PYTHON_EXECUTABLE}
217         ARGS   "${TOP_CMAKE_PATH}/po/unix2dos.py" "${CMAKE_CURRENT_BINARY_DIR}/${_pofile}"
218       COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/${_pofile} ${LYX_DEST_PO}/${_pofile}
219       COMMAND ${CMAKE_COMMAND} -E touch ${_pofile}.copied
220       DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_pofile}
221       )
222   else()
223     add_custom_command(
224       OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_pofile}.copied
225       COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/${_pofile} ${LYX_DEST_PO}/${_pofile}
226       COMMAND ${CMAKE_COMMAND} -E touch ${_pofile}.copied
227       DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_pofile}
228       )
229   endif()
230   list(APPEND LYX_UPDATED_PO_FILES ${CMAKE_CURRENT_BINARY_DIR}/${_pofile}.copied)
231 endforeach(_pofile)
232
233 if(LYX_CPACK OR LYX_INSTALL)
234   set(_addALLOption ALL)
235 endif()
236 GETTEXT_CREATE_TRANSLATIONS(${CMAKE_CURRENT_BINARY_DIR}/${_lyxname}.pot ${_addALLOption} ${LYX_PO_FILES})
237
238 # Create a new target to remerge po files
239 # ALL omitted here, to prevent from automatic creation
240 ADD_CUSTOM_TARGET(update-gmo DEPENDS ${LYX_UPDATED_GMO_FILES})
241 ADD_CUSTOM_TARGET(update-po DEPENDS ${LYX_UPDATED_PO_FILES} ${LYX_UPDATED_GMO_FILES})
242 set_target_properties(update-po update-gmo PROPERTIES FOLDER "i18n")
243 add_dependencies(update-po translations)
244