FindGLIB.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # - Try to find Glib and its components (gio, gobject etc)
  2. # Once done, this will define
  3. #
  4. # GLIB_FOUND - system has Glib
  5. # GLIB_INCLUDE_DIRS - the Glib include directories
  6. # GLIB_LIBRARIES - link these to use Glib
  7. #
  8. # Optionally, the COMPONENTS keyword can be passed to find_package()
  9. # and Glib components can be looked for. Currently, the following
  10. # components can be used, and they define the following variables if
  11. # found:
  12. #
  13. # gio: GLIB_GIO_LIBRARIES
  14. # gobject: GLIB_GOBJECT_LIBRARIES
  15. # gmodule: GLIB_GMODULE_LIBRARIES
  16. # gthread: GLIB_GTHREAD_LIBRARIES
  17. #
  18. # Note that the respective _INCLUDE_DIR variables are not set, since
  19. # all headers are in the same directory as GLIB_INCLUDE_DIRS.
  20. #
  21. # Copyright (C) 2012 Raphael Kubo da Costa <rakuco@webkit.org>
  22. #
  23. # Redistribution and use in source and binary forms, with or without
  24. # modification, are permitted provided that the following conditions
  25. # are met:
  26. # 1. Redistributions of source code must retain the above copyright
  27. # notice, this list of conditions and the following disclaimer.
  28. # 2. Redistributions in binary form must reproduce the above copyright
  29. # notice, this list of conditions and the following disclaimer in the
  30. # documentation and/or other materials provided with the distribution.
  31. #
  32. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``AS
  33. # IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS
  36. # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  37. # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  38. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  39. # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  40. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  41. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. find_package(PkgConfig)
  44. pkg_check_modules(PC_GLIB QUIET glib-2.0)
  45. find_library(GLIB_LIBRARIES
  46. NAMES glib-2.0
  47. HINTS ${PC_GLIB_LIBDIR}
  48. ${PC_GLIB_LIBRARY_DIRS}
  49. )
  50. # Files in glib's main include path may include glibconfig.h, which,
  51. # for some odd reason, is normally in $LIBDIR/glib-2.0/include.
  52. get_filename_component(_GLIB_LIBRARY_DIR ${GLIB_LIBRARIES} PATH)
  53. find_path(GLIBCONFIG_INCLUDE_DIR
  54. NAMES glibconfig.h
  55. HINTS ${PC_LIBDIR} ${PC_LIBRARY_DIRS} ${_GLIB_LIBRARY_DIR}
  56. PATH_SUFFIXES glib-2.0/include
  57. )
  58. find_path(GLIB_INCLUDE_DIR
  59. NAMES glib.h
  60. HINTS ${PC_GLIB_INCLUDEDIR}
  61. ${PC_GLIB_INCLUDE_DIRS}
  62. PATH_SUFFIXES glib-2.0
  63. )
  64. set(GLIB_INCLUDE_DIRS ${GLIB_INCLUDE_DIR} ${GLIBCONFIG_INCLUDE_DIR})
  65. # Version detection
  66. file(READ "${GLIBCONFIG_INCLUDE_DIR}/glibconfig.h" GLIBCONFIG_H_CONTENTS)
  67. string(REGEX MATCH "#define GLIB_MAJOR_VERSION ([0-9]+)" _dummy "${GLIBCONFIG_H_CONTENTS}")
  68. set(GLIB_VERSION_MAJOR "${CMAKE_MATCH_1}")
  69. string(REGEX MATCH "#define GLIB_MINOR_VERSION ([0-9]+)" _dummy "${GLIBCONFIG_H_CONTENTS}")
  70. set(GLIB_VERSION_MINOR "${CMAKE_MATCH_1}")
  71. string(REGEX MATCH "#define GLIB_MICRO_VERSION ([0-9]+)" _dummy "${GLIBCONFIG_H_CONTENTS}")
  72. set(GLIB_VERSION_MICRO "${CMAKE_MATCH_1}")
  73. set(GLIB_VERSION "${GLIB_VERSION_MAJOR}.${GLIB_VERSION_MINOR}.${GLIB_VERSION_MICRO}")
  74. # Additional Glib components. We only look for libraries, as not all of them
  75. # have corresponding headers and all headers are installed alongside the main
  76. # glib ones.
  77. foreach (_component ${GLIB_FIND_COMPONENTS})
  78. if (${_component} STREQUAL "gio")
  79. find_library(GLIB_GIO_LIBRARIES NAMES gio-2.0 HINTS ${_GLIB_LIBRARY_DIR})
  80. set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GIO_LIBRARIES)
  81. elseif (${_component} STREQUAL "gobject")
  82. find_library(GLIB_GOBJECT_LIBRARIES NAMES gobject-2.0 HINTS ${_GLIB_LIBRARY_DIR})
  83. set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GOBJECT_LIBRARIES)
  84. elseif (${_component} STREQUAL "gmodule")
  85. find_library(GLIB_GMODULE_LIBRARIES NAMES gmodule-2.0 HINTS ${_GLIB_LIBRARY_DIR})
  86. set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GMODULE_LIBRARIES)
  87. elseif (${_component} STREQUAL "gthread")
  88. find_library(GLIB_GTHREAD_LIBRARIES NAMES gthread-2.0 HINTS ${_GLIB_LIBRARY_DIR})
  89. set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GTHREAD_LIBRARIES)
  90. endif ()
  91. endforeach ()
  92. include(FindPackageHandleStandardArgs)
  93. FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLIB REQUIRED_VARS GLIB_INCLUDE_DIRS GLIB_LIBRARIES ${ADDITIONAL_REQUIRED_VARS}
  94. VERSION_VAR GLIB_VERSION)