FindGettext.cmake 7.9 KB

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