FindGettext.cmake 8.3 KB

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