Debian Codegraph @

cmake/packages/FindGLIB.cmake

raw
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 <[email protected]>
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 # Version detection
73 if(EXISTS "${GLIBCONFIG_INCLUDE_DIR}/glibconfig.h")
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 )
123
124 if(GLIB_FOUND AND NOT TARGET GLIB::GLIB)
125 add_library(GLIB::GLIB INTERFACE IMPORTED)
126 set_property(TARGET GLIB::GLIB PROPERTY
127 INTERFACE_INCLUDE_DIRECTORIES "${GLIB_INCLUDE_DIRS}")
128 set_property(TARGET GLIB::GLIB PROPERTY
129 INTERFACE_LINK_LIBRARIES "${GLIB_LIBRARIES}")
130 if(DEFINED GLIB_GIO_LIBRARIES)
131 set_property(TARGET GLIB::GLIB APPEND PROPERTY
132 INTERFACE_LINK_LIBRARIES "${GLIB_GIO_LIBRARIES}")
133 endif()
134 if(DEFINED GLIB_GOBJECT_LIBRARIES)
135 set_property(TARGET GLIB::GLIB APPEND PROPERTY
136 INTERFACE_LINK_LIBRARIES "${GLIB_GOBJECT_LIBRARIES}")
137 endif()
138 if(DEFINED GLIB_GMODULE_LIBRARIES)
139 set_property(TARGET GLIB::GLIB APPEND PROPERTY
140 INTERFACE_LINK_LIBRARIES "${GLIB_GMODULE_LIBRARIES}")
141 endif()
142 if(DEFINED GLIB_GTHREAD_LIBRARIES)
143 set_property(TARGET GLIB::GLIB APPEND PROPERTY
144 INTERFACE_LINK_LIBRARIES "${GLIB_GTHREAD_LIBRARIES}")
145 endif()
146 if(DEFINED GLIB_GIO_UNIX_LIBRARIES)
147 set_property(TARGET GLIB::GLIB APPEND PROPERTY
148 INTERFACE_LINK_LIBRARIES "${GLIB_GIO_UNIX_LIBRARIES}")
149 endif()
150 endif()