FindLibintl.cmake 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. include(CheckCSourceCompiles)
  2. include(CheckVariableExists)
  3. # Append custom gettext path to CMAKE_PREFIX_PATH
  4. # if installed via Mac Homebrew
  5. if (APPLE)
  6. find_program(HOMEBREW_PRG brew)
  7. if (EXISTS ${HOMEBREW_PRG})
  8. execute_process(COMMAND ${HOMEBREW_PRG} --prefix gettext
  9. OUTPUT_STRIP_TRAILING_WHITESPACE
  10. OUTPUT_VARIABLE HOMEBREW_GETTEXT_PREFIX)
  11. list(APPEND CMAKE_PREFIX_PATH "${HOMEBREW_GETTEXT_PREFIX}")
  12. endif()
  13. endif()
  14. find_path(LIBINTL_INCLUDE_DIR
  15. NAMES libintl.h
  16. PATH_SUFFIXES gettext
  17. )
  18. find_library(LIBINTL_LIBRARY
  19. NAMES intl libintl
  20. )
  21. if (LIBINTL_INCLUDE_DIR)
  22. list(APPEND CMAKE_REQUIRED_INCLUDES "${LIBINTL_INCLUDE_DIR}")
  23. endif()
  24. # On some systems (linux+glibc) libintl is passively available.
  25. # So only specify the library if one was found.
  26. if (LIBINTL_LIBRARY)
  27. list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBINTL_LIBRARY}")
  28. endif()
  29. if (MSVC)
  30. list(APPEND CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARY})
  31. endif()
  32. # On macOS, if libintl is a static library then we also need
  33. # to link libiconv and CoreFoundation.
  34. get_filename_component(LibIntl_EXT "${LIBINTL_LIBRARY}" EXT)
  35. if (APPLE AND (LibIntl_EXT STREQUAL ".a"))
  36. set(LibIntl_STATIC TRUE)
  37. find_library(CoreFoundation_FRAMEWORK CoreFoundation)
  38. list(APPEND CMAKE_REQUIRED_LIBRARIES "${ICONV_LIBRARY}" "${CoreFoundation_FRAMEWORK}")
  39. endif()
  40. check_c_source_compiles("
  41. #include <libintl.h>
  42. int main(int argc, char** argv) {
  43. gettext(\"foo\");
  44. ngettext(\"foo\", \"bar\", 1);
  45. bindtextdomain(\"foo\", \"bar\");
  46. bind_textdomain_codeset(\"foo\", \"bar\");
  47. textdomain(\"foo\");
  48. }" HAVE_WORKING_LIBINTL)
  49. if (MSVC)
  50. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES ${ICONV_LIBRARY})
  51. endif()
  52. if (LibIntl_STATIC)
  53. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "${ICONV_LIBRARY}" "${CoreFoundation_FRAMEWORK}")
  54. endif()
  55. if (LIBINTL_INCLUDE_DIR)
  56. list(REMOVE_ITEM CMAKE_REQUIRED_INCLUDES "${LIBINTL_INCLUDE_DIR}")
  57. endif()
  58. if (LIBINTL_LIBRARY)
  59. list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "${LIBINTL_LIBRARY}")
  60. endif()
  61. set(REQUIRED_VARIABLES LIBINTL_LIBRARY LIBINTL_INCLUDE_DIR)
  62. if (HAVE_WORKING_LIBINTL)
  63. # On some systems (linux+glibc) libintl is passively available.
  64. # If HAVE_WORKING_LIBINTL then we consider the requirement satisfied.
  65. unset(REQUIRED_VARIABLES)
  66. check_variable_exists(_nl_msg_cat_cntr HAVE_NL_MSG_CAT_CNTR)
  67. endif()
  68. find_package_handle_standard_args(Libintl DEFAULT_MSG
  69. ${REQUIRED_VARIABLES})
  70. mark_as_advanced(LIBINTL_LIBRARY LIBINTL_INCLUDE_DIR)
  71. add_library(libintl INTERFACE)
  72. target_include_directories(libintl SYSTEM BEFORE INTERFACE ${LIBINTL_INCLUDE_DIR})
  73. if (LIBINTL_LIBRARY)
  74. target_link_libraries(libintl INTERFACE ${LIBINTL_LIBRARY})
  75. endif()