FindGettext.cmake 7.8 KB

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