FindGettext.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #.rst:
  2. # FindGettext
  3. # -----------
  4. #
  5. # Find GNU gettext tools
  6. #
  7. # This module looks for the GNU gettext tools. This module defines the
  8. # following values:
  9. #
  10. # ::
  11. #
  12. # GETTEXT_MSGMERGE_EXECUTABLE: the full path to the msgmerge tool.
  13. # GETTEXT_MSGFMT_EXECUTABLE: the full path to the msgfmt tool.
  14. # GETTEXT_FOUND: True if gettext has been found.
  15. # GETTEXT_VERSION_STRING: the version of gettext found (since CMake 2.8.8)
  16. #
  17. #
  18. #
  19. # Additionally it provides the following macros:
  20. #
  21. # GETTEXT_CREATE_TRANSLATIONS ( outputFile [ALL] file1 ... fileN )
  22. #
  23. # ::
  24. #
  25. # This will create a target "translations" which will convert the
  26. # given input po files into the binary output mo file. If the
  27. # ALL option is used, the translations will also be created when
  28. # building the default target.
  29. #
  30. # GETTEXT_PROCESS_POT_FILE( <potfile> [ALL] [INSTALL_DESTINATION <destdir>]
  31. # LANGUAGES <lang1> <lang2> ... )
  32. #
  33. # ::
  34. #
  35. # Process the given pot file to mo files.
  36. # If INSTALL_DESTINATION is given then automatically install rules will
  37. # be created, the language subdirectory will be taken into account
  38. # (by default use share/locale/).
  39. # If ALL is specified, the pot file is processed when building the all traget.
  40. # It creates a custom target "potfile".
  41. #
  42. # GETTEXT_PROCESS_PO_FILES( <lang> [ALL] [INSTALL_DESTINATION <dir>]
  43. # PO_FILES <po1> <po2> ... )
  44. #
  45. # ::
  46. #
  47. # Process the given po files to mo files for the given language.
  48. # If INSTALL_DESTINATION is given then automatically install rules will
  49. # be created, the language subdirectory will be taken into account
  50. # (by default use share/locale/).
  51. # If ALL is specified, the po files are processed when building the all traget.
  52. # It creates a custom target "pofiles".
  53. #
  54. # .. note::
  55. # If you wish to use the Gettext library (libintl), use :module:`FindIntl`.
  56. #=============================================================================
  57. # Copyright 2007-2009 Kitware, Inc.
  58. # Copyright 2007 Alexander Neundorf <neundorf@kde.org>
  59. #
  60. # Distributed under the OSI-approved BSD License (the "License");
  61. # see accompanying file Copyright.txt for details.
  62. #
  63. # This software is distributed WITHOUT ANY WARRANTY; without even the
  64. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  65. # See the License for more information.
  66. #=============================================================================
  67. # (To distribute this file outside of CMake, substitute the full
  68. # License text for the above reference.)
  69. find_program(GETTEXT_MSGMERGE_EXECUTABLE msgmerge)
  70. find_program(GETTEXT_MSGFMT_EXECUTABLE msgfmt)
  71. if(GETTEXT_MSGMERGE_EXECUTABLE)
  72. execute_process(COMMAND ${GETTEXT_MSGMERGE_EXECUTABLE} --version
  73. OUTPUT_VARIABLE gettext_version
  74. ERROR_QUIET
  75. OUTPUT_STRIP_TRAILING_WHITESPACE)
  76. if (gettext_version MATCHES "^msgmerge \\([^\\)]*\\) ([0-9\\.]+[^ \n]*)")
  77. set(GETTEXT_VERSION_STRING "${CMAKE_MATCH_1}")
  78. endif()
  79. unset(gettext_version)
  80. endif()
  81. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  82. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Gettext
  83. REQUIRED_VARS GETTEXT_MSGMERGE_EXECUTABLE GETTEXT_MSGFMT_EXECUTABLE
  84. VERSION_VAR GETTEXT_VERSION_STRING)
  85. include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
  86. function(_GETTEXT_GET_UNIQUE_TARGET_NAME _name _unique_name)
  87. set(propertyName "_GETTEXT_UNIQUE_COUNTER_${_name}")
  88. get_property(currentCounter GLOBAL PROPERTY "${propertyName}")
  89. if(NOT currentCounter)
  90. set(currentCounter 1)
  91. endif()
  92. set(${_unique_name} "${_name}_${currentCounter}" PARENT_SCOPE)
  93. math(EXPR currentCounter "${currentCounter} + 1")
  94. set_property(GLOBAL PROPERTY ${propertyName} ${currentCounter} )
  95. endfunction()
  96. macro(GETTEXT_CREATE_TRANSLATIONS _potFile _firstPoFileArg)
  97. # make it a real variable, so we can modify it here
  98. set(_firstPoFile "${_firstPoFileArg}")
  99. set(_gmoFiles)
  100. get_filename_component(_potName ${_potFile} NAME)
  101. string(REGEX REPLACE "^(.+)(\\.[^.]+)$" "\\1" _potBasename ${_potName})
  102. get_filename_component(_absPotFile ${_potFile} ABSOLUTE)
  103. set(_addToAll)
  104. if(${_firstPoFile} STREQUAL "ALL")
  105. set(_addToAll "ALL")
  106. set(_firstPoFile)
  107. endif()
  108. foreach (_currentPoFile ${_firstPoFile} ${ARGN})
  109. get_filename_component(_absFile ${_currentPoFile} ABSOLUTE)
  110. get_filename_component(_abs_PATH ${_absFile} PATH)
  111. get_filename_component(_lang ${_absFile} NAME_WE)
  112. set(_gmoFile ${CMAKE_CURRENT_BINARY_DIR}/${_lang}.gmo)
  113. add_custom_command(
  114. OUTPUT ${_gmoFile}
  115. COMMAND ${GETTEXT_MSGMERGE_EXECUTABLE} --quiet --update --backup=none -s ${_absFile} ${_absPotFile}
  116. COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${_gmoFile} ${_absFile}
  117. DEPENDS ${_absPotFile} ${_absFile}
  118. )
  119. install(FILES ${_gmoFile} DESTINATION share/locale/${_lang}/LC_MESSAGES RENAME ${_potBasename}.mo)
  120. set(_gmoFiles ${_gmoFiles} ${_gmoFile})
  121. endforeach ()
  122. if(NOT TARGET translations)
  123. add_custom_target(translations)
  124. endif()
  125. _GETTEXT_GET_UNIQUE_TARGET_NAME(translations uniqueTargetName)
  126. add_custom_target(${uniqueTargetName} ${_addToAll} DEPENDS ${_gmoFiles})
  127. add_dependencies(translations ${uniqueTargetName})
  128. endmacro()
  129. function(GETTEXT_PROCESS_POT_FILE _potFile)
  130. set(_gmoFiles)
  131. set(_options ALL)
  132. set(_oneValueArgs INSTALL_DESTINATION)
  133. set(_multiValueArgs LANGUAGES)
  134. CMAKE_PARSE_ARGUMENTS(_parsedArguments "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  135. get_filename_component(_potName ${_potFile} NAME)
  136. string(REGEX REPLACE "^(.+)(\\.[^.]+)$" "\\1" _potBasename ${_potName})
  137. get_filename_component(_absPotFile ${_potFile} ABSOLUTE)
  138. foreach (_lang ${_parsedArguments_LANGUAGES})
  139. set(_poFile "${CMAKE_CURRENT_BINARY_DIR}/${_lang}.po")
  140. set(_gmoFile "${CMAKE_CURRENT_BINARY_DIR}/${_lang}.gmo")
  141. add_custom_command(
  142. OUTPUT "${_poFile}"
  143. COMMAND ${GETTEXT_MSGMERGE_EXECUTABLE} --quiet --update --backup=none -s ${_poFile} ${_absPotFile}
  144. DEPENDS ${_absPotFile}
  145. )
  146. add_custom_command(
  147. OUTPUT "${_gmoFile}"
  148. COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${_gmoFile} ${_poFile}
  149. DEPENDS ${_absPotFile} ${_poFile}
  150. )
  151. if(_parsedArguments_INSTALL_DESTINATION)
  152. install(FILES ${_gmoFile} DESTINATION ${_parsedArguments_INSTALL_DESTINATION}/${_lang}/LC_MESSAGES RENAME ${_potBasename}.mo)
  153. endif()
  154. list(APPEND _gmoFiles ${_gmoFile})
  155. endforeach ()
  156. if(NOT TARGET potfiles)
  157. add_custom_target(potfiles)
  158. endif()
  159. _GETTEXT_GET_UNIQUE_TARGET_NAME( potfiles uniqueTargetName)
  160. if(_parsedArguments_ALL)
  161. add_custom_target(${uniqueTargetName} ALL DEPENDS ${_gmoFiles})
  162. else()
  163. add_custom_target(${uniqueTargetName} DEPENDS ${_gmoFiles})
  164. endif()
  165. add_dependencies(potfiles ${uniqueTargetName})
  166. endfunction()
  167. function(GETTEXT_PROCESS_PO_FILES _lang)
  168. set(_options ALL)
  169. set(_oneValueArgs INSTALL_DESTINATION)
  170. set(_multiValueArgs PO_FILES)
  171. set(_gmoFiles)
  172. CMAKE_PARSE_ARGUMENTS(_parsedArguments "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  173. foreach(_current_PO_FILE ${_parsedArguments_PO_FILES})
  174. get_filename_component(_name ${_current_PO_FILE} NAME)
  175. string(REGEX REPLACE "^(.+)(\\.[^.]+)$" "\\1" _basename ${_name})
  176. set(_gmoFile ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.gmo)
  177. add_custom_command(OUTPUT ${_gmoFile}
  178. COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${_gmoFile} ${_current_PO_FILE}
  179. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  180. DEPENDS ${_current_PO_FILE}
  181. )
  182. if(_parsedArguments_INSTALL_DESTINATION)
  183. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${_basename}.gmo DESTINATION ${_parsedArguments_INSTALL_DESTINATION}/${_lang}/LC_MESSAGES/ RENAME ${_basename}.mo)
  184. endif()
  185. list(APPEND _gmoFiles ${_gmoFile})
  186. endforeach()
  187. if(NOT TARGET pofiles)
  188. add_custom_target(pofiles)
  189. endif()
  190. _GETTEXT_GET_UNIQUE_TARGET_NAME( pofiles uniqueTargetName)
  191. if(_parsedArguments_ALL)
  192. add_custom_target(${uniqueTargetName} ALL DEPENDS ${_gmoFiles})
  193. else()
  194. add_custom_target(${uniqueTargetName} DEPENDS ${_gmoFiles})
  195. endif()
  196. add_dependencies(pofiles ${uniqueTargetName})
  197. endfunction()