FindGettext.cmake 8.2 KB

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