Pascal Bach | d5f87e1 | 2014-12-12 15:59:17 +0100 | [diff] [blame] | 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 | |
| 44 | find_package(PkgConfig) |
| 45 | pkg_check_modules(PC_GLIB QUIET glib-2.0) |
| 46 | |
| 47 | find_library(GLIB_LIBRARIES |
| 48 | NAMES glib-2.0 |
| 49 | HINTS ${PC_GLIB_LIBDIR} |
| 50 | ${PC_GLIB_LIBRARY_DIRS} |
| 51 | ) |
| 52 | |
| 53 | # Files in glib's main include path may include glibconfig.h, which, |
| 54 | # for some odd reason, is normally in $LIBDIR/glib-2.0/include. |
| 55 | get_filename_component(_GLIB_LIBRARY_DIR ${GLIB_LIBRARIES} PATH) |
| 56 | find_path(GLIBCONFIG_INCLUDE_DIR |
| 57 | NAMES glibconfig.h |
| 58 | HINTS ${PC_LIBDIR} ${PC_LIBRARY_DIRS} ${_GLIB_LIBRARY_DIR} |
| 59 | ${PC_GLIB_INCLUDEDIR} ${PC_GLIB_INCLUDE_DIRS} |
| 60 | PATH_SUFFIXES glib-2.0/include |
| 61 | ) |
| 62 | |
| 63 | find_path(GLIB_INCLUDE_DIR |
| 64 | NAMES glib.h |
| 65 | HINTS ${PC_GLIB_INCLUDEDIR} |
| 66 | ${PC_GLIB_INCLUDE_DIRS} |
| 67 | PATH_SUFFIXES glib-2.0 |
| 68 | ) |
| 69 | |
| 70 | set(GLIB_INCLUDE_DIRS ${GLIB_INCLUDE_DIR} ${GLIBCONFIG_INCLUDE_DIR}) |
| 71 | |
| 72 | if(GLIBCONFIG_INCLUDE_DIR) |
| 73 | # Version detection |
| 74 | file(READ "${GLIBCONFIG_INCLUDE_DIR}/glibconfig.h" GLIBCONFIG_H_CONTENTS) |
| 75 | string(REGEX MATCH "#define GLIB_MAJOR_VERSION ([0-9]+)" _dummy "${GLIBCONFIG_H_CONTENTS}") |
| 76 | set(GLIB_VERSION_MAJOR "${CMAKE_MATCH_1}") |
| 77 | string(REGEX MATCH "#define GLIB_MINOR_VERSION ([0-9]+)" _dummy "${GLIBCONFIG_H_CONTENTS}") |
| 78 | set(GLIB_VERSION_MINOR "${CMAKE_MATCH_1}") |
| 79 | string(REGEX MATCH "#define GLIB_MICRO_VERSION ([0-9]+)" _dummy "${GLIBCONFIG_H_CONTENTS}") |
| 80 | set(GLIB_VERSION_MICRO "${CMAKE_MATCH_1}") |
| 81 | set(GLIB_VERSION "${GLIB_VERSION_MAJOR}.${GLIB_VERSION_MINOR}.${GLIB_VERSION_MICRO}") |
| 82 | endif() |
| 83 | |
| 84 | # Additional Glib components. We only look for libraries, as not all of them |
| 85 | # have corresponding headers and all headers are installed alongside the main |
| 86 | # glib ones. |
| 87 | foreach (_component ${GLIB_FIND_COMPONENTS}) |
| 88 | if (${_component} STREQUAL "gio") |
| 89 | find_library(GLIB_GIO_LIBRARIES NAMES gio-2.0 HINTS ${_GLIB_LIBRARY_DIR}) |
| 90 | set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GIO_LIBRARIES) |
| 91 | elseif (${_component} STREQUAL "gobject") |
| 92 | find_library(GLIB_GOBJECT_LIBRARIES NAMES gobject-2.0 HINTS ${_GLIB_LIBRARY_DIR}) |
| 93 | set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GOBJECT_LIBRARIES) |
| 94 | elseif (${_component} STREQUAL "gmodule") |
| 95 | find_library(GLIB_GMODULE_LIBRARIES NAMES gmodule-2.0 HINTS ${_GLIB_LIBRARY_DIR}) |
| 96 | set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GMODULE_LIBRARIES) |
| 97 | elseif (${_component} STREQUAL "gthread") |
| 98 | find_library(GLIB_GTHREAD_LIBRARIES NAMES gthread-2.0 HINTS ${_GLIB_LIBRARY_DIR}) |
| 99 | set(ADDITIONAL_REQUIRED_VARS ${ADDITIONAL_REQUIRED_VARS} GLIB_GTHREAD_LIBRARIES) |
| 100 | elseif (${_component} STREQUAL "gio-unix") |
| 101 | # gio-unix is compiled as part of the gio library, but the include paths |
| 102 | # are separate from the shared glib ones. Since this is currently only used |
| 103 | # by WebKitGTK+ we don't go to extraordinary measures beyond pkg-config. |
| 104 | pkg_check_modules(GIO_UNIX QUIET gio-unix-2.0) |
| 105 | endif () |
| 106 | endforeach () |
| 107 | |
| 108 | include(FindPackageHandleStandardArgs) |
| 109 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLIB REQUIRED_VARS GLIB_INCLUDE_DIRS GLIB_LIBRARIES ${ADDITIONAL_REQUIRED_VARS} |
| 110 | VERSION_VAR GLIB_VERSION) |
| 111 | |
| 112 | mark_as_advanced( |
| 113 | GLIBCONFIG_INCLUDE_DIR |
| 114 | GLIB_GIO_LIBRARIES |
| 115 | GLIB_GIO_UNIX_LIBRARIES |
| 116 | GLIB_GMODULE_LIBRARIES |
| 117 | GLIB_GOBJECT_LIBRARIES |
| 118 | GLIB_GTHREAD_LIBRARIES |
| 119 | GLIB_INCLUDE_DIR |
| 120 | GLIB_INCLUDE_DIRS |
| 121 | GLIB_LIBRARIES |
| 122 | ) |