FindLibIntl.cmake 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # - Try to find libintl
  2. # Once done, this will define
  3. #
  4. # LibIntl_FOUND - system has libintl
  5. # LibIntl_INCLUDE_DIRS - the libintl include directories
  6. # LibIntl_LIBRARIES - link these to use libintl
  7. include(CheckCSourceCompiles)
  8. include(CheckVariableExists)
  9. include(LibFindMacros)
  10. # Append custom gettext path to CMAKE_PREFIX_PATH
  11. # if installed via Mac Hombrew
  12. if (CMAKE_HOST_APPLE)
  13. find_program(HOMEBREW_PROG brew)
  14. if (EXISTS ${HOMEBREW_PROG})
  15. execute_process(COMMAND ${HOMEBREW_PROG} --prefix gettext
  16. OUTPUT_STRIP_TRAILING_WHITESPACE
  17. OUTPUT_VARIABLE HOMEBREW_GETTEXT_PREFIX)
  18. list(APPEND CMAKE_PREFIX_PATH "${HOMEBREW_GETTEXT_PREFIX}")
  19. endif()
  20. endif()
  21. find_path(LibIntl_INCLUDE_DIR
  22. NAMES libintl.h
  23. PATH_SUFFIXES gettext
  24. )
  25. find_library(LibIntl_LIBRARY
  26. NAMES intl libintl
  27. )
  28. if (LibIntl_INCLUDE_DIR)
  29. list(APPEND CMAKE_REQUIRED_INCLUDES "${LibIntl_INCLUDE_DIR}")
  30. endif()
  31. # On some systems (linux+glibc) libintl is passively available.
  32. # So only specify the library if one was found.
  33. if (LibIntl_LIBRARY)
  34. list(APPEND CMAKE_REQUIRED_LIBRARIES "${LibIntl_LIBRARY}")
  35. endif()
  36. if (MSVC)
  37. list(APPEND CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARY})
  38. endif()
  39. # On macOS, if libintl is a static library then we also need
  40. # to link libiconv and CoreFoundation.
  41. get_filename_component(LibIntl_EXT "${LibIntl_LIBRARY}" EXT)
  42. if (APPLE AND (LibIntl_EXT STREQUAL ".a"))
  43. set(LibIntl_STATIC TRUE)
  44. find_library(CoreFoundation_FRAMEWORK CoreFoundation)
  45. list(APPEND CMAKE_REQUIRED_LIBRARIES "${ICONV_LIBRARY}" "${CoreFoundation_FRAMEWORK}")
  46. endif()
  47. check_c_source_compiles("
  48. #include <libintl.h>
  49. int main(int argc, char** argv) {
  50. gettext(\"foo\");
  51. ngettext(\"foo\", \"bar\", 1);
  52. bindtextdomain(\"foo\", \"bar\");
  53. bind_textdomain_codeset(\"foo\", \"bar\");
  54. textdomain(\"foo\");
  55. }" HAVE_WORKING_LIBINTL)
  56. if (MSVC)
  57. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARY})
  58. endif()
  59. if (LibIntl_STATIC)
  60. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "${ICONV_LIBRARY}" "${CoreFoundation_FRAMEWORK}")
  61. endif()
  62. if (LibIntl_INCLUDE_DIR)
  63. list(REMOVE_ITEM CMAKE_REQUIRED_INCLUDES "${LibIntl_INCLUDE_DIR}")
  64. endif()
  65. if (LibIntl_LIBRARY)
  66. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "${LibIntl_LIBRARY}")
  67. endif()
  68. if (HAVE_WORKING_LIBINTL)
  69. # On some systems (linux+glibc) libintl is passively available.
  70. # If HAVE_WORKING_LIBINTL then we consider the requirement satisfied.
  71. # Unset REQUIRED so that libfind_process(LibIntl) can proceed.
  72. if(LibIntl_FIND_REQUIRED)
  73. unset(LibIntl_FIND_REQUIRED)
  74. endif()
  75. set(LibIntl_FIND_QUIETLY ON)
  76. check_variable_exists(_nl_msg_cat_cntr HAVE_NL_MSG_CAT_CNTR)
  77. endif()
  78. set(LibIntl_PROCESS_INCLUDES LibIntl_INCLUDE_DIR)
  79. set(LibIntl_PROCESS_LIBS LibIntl_LIBRARY)
  80. libfind_process(LibIntl)