2 # This module can find the GTK2 widget libraries and several of its other
3 # optional components like gtkmm, glade, and glademm.
5 # NOTE: If you intend to use version checking, CMake 2.6.2 or later is
8 # Specify one or more of the following components
9 # as you call this find module. See example below.
16 # The following variables will be defined for your use
18 # GTK2_FOUND - Were all of your specified components found?
19 # GTK2_INCLUDE_DIRS - All include directories
20 # GTK2_LIBRARIES - All libraries
22 # GTK2_VERSION - The version of GTK2 found (x.y.z)
23 # GTK2_MAJOR_VERSION - The major version of GTK2
24 # GTK2_MINOR_VERSION - The minor version of GTK2
25 # GTK2_PATCH_VERSION - The patch version of GTK2
27 # Optional variables you can define prior to calling this module:
29 # GTK2_DEBUG - Enables verbose debugging of the module
30 # GTK2_SKIP_MARK_AS_ADVANCED - Disable marking cache variables as advanced
35 # Call find_package() once, here are some examples to pick from:
37 # Require GTK 2.6 or later
38 # find_package(GTK2 2.6 REQUIRED gtk)
40 # Require GTK 2.10 or later and Glade
41 # find_package(GTK2 2.10 REQUIRED gtk glade)
43 # Search for GTK/GTKMM 2.8 or later
44 # find_package(GTK2 2.8 COMPONENTS gtk gtkmm)
47 # include_directories(${GTK2_INCLUDE_DIRS})
48 # add_executable(mygui mygui.cc)
49 # target_link_libraries(mygui ${GTK2_LIBRARIES})
54 # Copyright (c) 2008-2009
55 # Philip Lowman <philip@yhbt.com>
57 # Version 0.7 (3/22/09)
58 # * Checked into CMake CVS
59 # * Added versioning support
60 # * Module now defaults to searching for GTK if COMPONENTS not specified.
61 # * Added HKCU prior to HKLM registry key and GTKMM specific environment
62 # variable as per mailing list discussion.
63 # * Added lib64 to include search path and a few other search paths where GTK
64 # may be installed on Unix systems.
65 # * Switched to lowercase CMake commands
66 # * Prefaced internal variables with _GTK2 to prevent collision
67 # * Changed internal macros to functions
68 # * Enhanced documentation
69 # Version 0.6 (1/8/08)
70 # Added GTK2_SKIP_MARK_AS_ADVANCED option
71 # Version 0.5 (12/19/08)
72 # Second release to cmake mailing list
74 # Redistribution and use is allowed according to the terms of the BSD license.
75 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
78 #=============================================================
80 # Internal function to parse the version number in gtkversion.h
81 # _OUT_major = Major version number
82 # _OUT_minor = Minor version number
83 # _OUT_micro = Micro version number
84 # _gtkversion_hdr = Header file to parse
85 #=============================================================
86 function(_GTK2_GET_VERSION _OUT_major _OUT_minor _OUT_micro _gtkversion_hdr)
87 file(READ ${_gtkversion_hdr} _contents)
89 string(REGEX REPLACE ".*#define GTK_MAJOR_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_major} "${_contents}")
90 string(REGEX REPLACE ".*#define GTK_MINOR_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_minor} "${_contents}")
91 string(REGEX REPLACE ".*#define GTK_MICRO_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_micro} "${_contents}")
93 if(NOT ${_OUT_major} MATCHES "[0-9]+")
94 message(FATAL_ERROR "Version parsing failed for GTK2_MAJOR_VERSION!")
96 if(NOT ${_OUT_minor} MATCHES "[0-9]+")
97 message(FATAL_ERROR "Version parsing failed for GTK2_MINOR_VERSION!")
99 if(NOT ${_OUT_micro} MATCHES "[0-9]+")
100 message(FATAL_ERROR "Version parsing failed for GTK2_MICRO_VERSION!")
103 set(${_OUT_major} ${${_OUT_major}} PARENT_SCOPE)
104 set(${_OUT_minor} ${${_OUT_minor}} PARENT_SCOPE)
105 set(${_OUT_micro} ${${_OUT_micro}} PARENT_SCOPE)
107 message(FATAL_ERROR "Include file ${_gtkversion_hdr} does not exist")
111 #=============================================================
112 # _GTK2_FIND_INCLUDE_DIR
113 # Internal function to find the GTK include directories
114 # _var = variable to set
115 # _hdr = header file to look for
116 #=============================================================
117 function(_GTK2_FIND_INCLUDE_DIR _var _hdr)
120 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
121 "_GTK2_FIND_INCLUDE_DIR( ${_var} ${_hdr} )")
144 foreach(_d ${_relatives})
145 list(APPEND _suffixes ${_d})
146 list(APPEND _suffixes ${_d}/include) # for /usr/lib/gtk-2.0/include
150 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
151 "include suffixes = ${_suffixes}")
154 find_path(${_var} ${_hdr}
164 $ENV{GTKMM_BASEPATH}/include
165 $ENV{GTKMM_BASEPATH}/lib
166 [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/include
167 [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib
168 [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/include
169 [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib
175 set(GTK2_INCLUDE_DIRS ${GTK2_INCLUDE_DIRS} ${${_var}} PARENT_SCOPE)
176 if(NOT GTK2_SKIP_MARK_AS_ADVANCED)
177 mark_as_advanced(${_var})
181 endfunction(_GTK2_FIND_INCLUDE_DIR)
183 #=============================================================
185 # Internal function to find libraries packaged with GTK2
186 # _var = library variable to create
187 #=============================================================
188 function(_GTK2_FIND_LIBRARY _var _lib _expand_vc _append_version)
191 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
192 "_GTK2_FIND_LIBRARY( ${_var} ${_lib} ${_expand_vc} ${_append_version} )")
195 # Not GTK versions per se but the versions encoded into Windows
196 # import libraries (GtkMM 2.14.1 has a gtkmm-vc80-2_4.lib for example)
197 # Also the MSVC libraries use _ for . (this is handled below)
198 set(_versions 2.20 2.18 2.16 2.14 2.12
199 2.10 2.8 2.6 2.4 2.2 2.0
200 1.20 1.18 1.16 1.14 1.12
201 1.10 1.8 1.6 1.4 1.2 1.0)
206 set(_library ${_lib})
209 # Add vc80/vc90 midfixes
211 set(_library ${_library}-vc80)
212 set(_library_d ${_library}-d)
214 set(_library ${_library}-vc90)
215 set(_library_d ${_library}-d)
220 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
221 "After midfix addition = ${_library} and ${_library_d}")
227 foreach(_ver ${_versions})
228 list(APPEND _lib_list "${_library}-${_ver}")
229 list(APPEND _libd_list "${_library_d}-${_ver}")
232 set(_lib_list ${_library})
233 set(_libd_list ${_library_d})
237 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
238 "library list = ${_lib_list} and library debug list = ${_libd_list}")
241 # For some silly reason the MSVC libraries use _ instead of .
242 # in the version fields
243 if(_expand_vc AND MSVC)
244 set(_no_dots_lib_list)
245 set(_no_dots_libd_list)
246 foreach(_l ${_lib_list})
247 string(REPLACE "." "_" _no_dots_library ${_l})
248 list(APPEND _no_dots_lib_list ${_no_dots_library})
251 set(_no_dots_libsd_list)
252 foreach(_l ${_libd_list})
253 string(REPLACE "." "_" _no_dots_libraryd ${_l})
254 list(APPEND _no_dots_libd_list ${_no_dots_libraryd})
257 # Copy list back to original names
258 set(_lib_list ${_no_dots_lib_list})
259 set(_libd_list ${_no_dots_libd_list})
263 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
264 "While searching for ${_var}, our proposed library list is ${_lib_list}")
274 $ENV{GTKMM_BASEPATH}/lib
275 [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib
276 [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib
279 if(_expand_vc AND MSVC)
281 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
282 "While searching for ${_var}_DEBUG our proposed library list is ${_libd_list}")
285 find_library(${_var}_DEBUG
288 $ENV{GTKMM_BASEPATH}/lib
289 [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib
290 [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib
293 if(${_var} AND ${_var}_DEBUG)
294 if(NOT GTK2_SKIP_MARK_AS_ADVANCED)
295 mark_as_advanced(${_var}_DEBUG)
297 set(GTK2_LIBRARIES ${GTK2_LIBRARIES} optimized ${${_var}} debug ${${_var}_DEBUG})
298 set(GTK2_LIBRARIES ${GTK2_LIBRARIES} PARENT_SCOPE)
301 if(NOT GTK2_SKIP_MARK_AS_ADVANCED)
302 mark_as_advanced(${_var})
304 set(GTK2_LIBRARIES ${GTK2_LIBRARIES} ${${_var}})
305 set(GTK2_LIBRARIES ${GTK2_LIBRARIES} PARENT_SCOPE)
306 # Set debug to release
307 set(${_var}_DEBUG ${${_var}})
308 set(${_var}_DEBUG ${${_var}} PARENT_SCOPE)
310 endfunction(_GTK2_FIND_LIBRARY)
312 #=============================================================
319 set(GTK2_INCLUDE_DIRS)
322 if(NOT GTK2_FIND_COMPONENTS)
323 # Assume they only want GTK
324 set(GTK2_FIND_COMPONENTS gtk)
328 # If specified, enforce version number
330 if(GTK2_FIND_VERSION)
331 cmake_minimum_required(VERSION 2.6.2)
332 set(GTK2_FAILED_VERSION_CHECK true)
334 message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
335 "Searching for version ${GTK2_FIND_VERSION}")
337 _GTK2_FIND_INCLUDE_DIR(GTK2_GTK_INCLUDE_DIR gtk/gtk.h)
338 if(GTK2_GTK_INCLUDE_DIR)
339 _GTK2_GET_VERSION(GTK2_MAJOR_VERSION
342 ${GTK2_GTK_INCLUDE_DIR}/gtk/gtkversion.h)
344 ${GTK2_MAJOR_VERSION}.${GTK2_MINOR_VERSION}.${GTK2_PATCH_VERSION})
345 if(GTK2_FIND_VERSION_EXACT)
346 if(GTK2_VERSION VERSION_EQUAL GTK2_FIND_VERSION)
347 set(GTK2_FAILED_VERSION_CHECK false)
350 if(GTK2_VERSION VERSION_EQUAL GTK2_FIND_VERSION OR
351 GTK2_VERSION VERSION_GREATER GTK2_FIND_VERSION)
352 set(GTK2_FAILED_VERSION_CHECK false)
356 # If we can't find the GTK include dir, we can't do version checking
357 if(GTK2_FIND_REQUIRED AND NOT GTK2_FIND_QUIETLY)
358 message(FATAL_ERROR "Could not find GTK2 include directory")
363 if(GTK2_FAILED_VERSION_CHECK)
364 if(GTK2_FIND_REQUIRED AND NOT GTK2_FIND_QUIETLY)
365 if(GTK2_FIND_VERSION_EXACT)
366 message(FATAL_ERROR "GTK2 version check failed. Version ${GTK2_VERSION} was found, version ${GTK2_FIND_VERSION} is needed exactly.")
368 message(FATAL_ERROR "GTK2 version check failed. Version ${GTK2_VERSION} was found, at least version ${GTK2_FIND_VERSION} is required")
372 # If the version check fails, exit out of the module here
378 # Find all components
381 foreach(_GTK2_component ${GTK2_FIND_COMPONENTS})
382 if(_GTK2_component STREQUAL "gtk")
384 _GTK2_FIND_INCLUDE_DIR(GTK2_GLIB_INCLUDE_DIR glib.h)
385 _GTK2_FIND_INCLUDE_DIR(GTK2_GLIBCONFIG_INCLUDE_DIR glibconfig.h)
386 _GTK2_FIND_LIBRARY (GTK2_GLIB_LIBRARY glib false true)
388 _GTK2_FIND_INCLUDE_DIR(GTK2_GDK_INCLUDE_DIR gdk/gdk.h)
389 _GTK2_FIND_INCLUDE_DIR(GTK2_GDKCONFIG_INCLUDE_DIR gdkconfig.h)
390 _GTK2_FIND_LIBRARY (GTK2_GDK_LIBRARY gdk-x11 false true)
391 _GTK2_FIND_LIBRARY (GTK2_GDK_LIBRARY gdk-win32 false true)
393 _GTK2_FIND_INCLUDE_DIR(GTK2_GTK_INCLUDE_DIR gtk/gtk.h)
394 _GTK2_FIND_LIBRARY (GTK2_GTK_LIBRARY gtk-x11 false true)
395 _GTK2_FIND_LIBRARY (GTK2_GTK_LIBRARY gtk-win32 false true)
397 _GTK2_FIND_INCLUDE_DIR(GTK2_CAIRO_INCLUDE_DIR cairo.h)
398 _GTK2_FIND_LIBRARY (GTK2_CAIRO_LIBRARY cairo false false)
400 _GTK2_FIND_INCLUDE_DIR(GTK2_PANGO_INCLUDE_DIR pango/pango.h)
401 _GTK2_FIND_LIBRARY (GTK2_PANGO_LIBRARY pango false true)
403 _GTK2_FIND_INCLUDE_DIR(GTK2_ATK_INCLUDE_DIR atk/atk.h)
404 _GTK2_FIND_LIBRARY (GTK2_ATK_LIBRARY atk false true)
406 #elseif(_GTK2_component STREQUAL "gdk_pixbuf")
407 #_GTK2_FIND_INCLUDE_DIR(GTK2_GDKPIXBUF_INCLUDE_DIR gdk-pixbuf/gdk-pixbuf.h)
408 #_GTK2_FIND_LIBRARY (GTK2_GDKPIXBUF_LIBRARY gdk_pixbuf false true)
410 elseif(_GTK2_component STREQUAL "gtkmm")
412 _GTK2_FIND_INCLUDE_DIR(GTK2_GLIBMM_INCLUDE_DIR glibmm.h)
413 _GTK2_FIND_INCLUDE_DIR(GTK2_GLIBMMCONFIG_INCLUDE_DIR glibmmconfig.h)
414 _GTK2_FIND_LIBRARY (GTK2_GLIBMM_LIBRARY glibmm true true)
416 _GTK2_FIND_INCLUDE_DIR(GTK2_GDKMM_INCLUDE_DIR gdkmm.h)
417 _GTK2_FIND_INCLUDE_DIR(GTK2_GDKMMCONFIG_INCLUDE_DIR gdkmmconfig.h)
418 _GTK2_FIND_LIBRARY (GTK2_GDKMM_LIBRARY gdkmm true true)
420 _GTK2_FIND_INCLUDE_DIR(GTK2_GTKMM_INCLUDE_DIR gtkmm.h)
421 _GTK2_FIND_INCLUDE_DIR(GTK2_GTKMMCONFIG_INCLUDE_DIR gtkmmconfig.h)
422 _GTK2_FIND_LIBRARY (GTK2_GTKMM_LIBRARY gtkmm true true)
424 _GTK2_FIND_INCLUDE_DIR(GTK2_CAIROMM_INCLUDE_DIR cairomm/cairomm.h)
425 _GTK2_FIND_LIBRARY (GTK2_CAIROMM_LIBRARY cairomm true true)
427 _GTK2_FIND_INCLUDE_DIR(GTK2_PANGOMM_INCLUDE_DIR pangomm.h)
428 _GTK2_FIND_LIBRARY (GTK2_PANGOMM_LIBRARY pangomm true true)
430 _GTK2_FIND_INCLUDE_DIR(GTK2_SIGC++_INCLUDE_DIR sigc++/sigc++.h)
431 _GTK2_FIND_INCLUDE_DIR(GTK2_SIGC++CONFIG_INCLUDE_DIR sigc++config.h)
432 _GTK2_FIND_LIBRARY (GTK2_SIGC++_LIBRARY sigc true true)
434 _GTK2_FIND_INCLUDE_DIR(GTK2_GIOMM_INCLUDE_DIR giomm.h)
435 _GTK2_FIND_INCLUDE_DIR(GTK2_GIOMMCONFIG_INCLUDE_DIR giommconfig.h)
436 _GTK2_FIND_LIBRARY (GTK2_GIOMM_LIBRARY giomm true true)
438 _GTK2_FIND_INCLUDE_DIR(GTK2_ATKMM_INCLUDE_DIR atkmm.h)
439 _GTK2_FIND_LIBRARY (GTK2_ATKMM_LIBRARY atkmm true true)
441 elseif(_GTK2_component STREQUAL "glade")
443 _GTK2_FIND_INCLUDE_DIR(GTK2_GLADE_INCLUDE_DIR glade/glade.h)
444 _GTK2_FIND_LIBRARY (GTK2_GLADE_LIBRARY glade false true)
446 elseif(_GTK2_component STREQUAL "glademm")
448 _GTK2_FIND_INCLUDE_DIR(GTK2_GLADEMM_INCLUDE_DIR libglademm.h)
449 _GTK2_FIND_INCLUDE_DIR(GTK2_GLADEMMCONFIG_INCLUDE_DIR libglademmconfig.h)
450 _GTK2_FIND_LIBRARY (GTK2_GLADEMM_LIBRARY glademm true true)
453 message(FATAL_ERROR "Unknown GTK2 component ${_component}")
458 # Solve for the GTK2 version if we haven't already
460 if(NOT GTK2_FIND_VERSION AND GTK2_GTK_INCLUDE_DIR)
461 _GTK2_GET_VERSION(GTK2_MAJOR_VERSION
464 ${GTK2_GTK_INCLUDE_DIR}/gtk/gtkversion.h)
465 set(GTK2_VERSION ${GTK2_MAJOR_VERSION}.${GTK2_MINOR_VERSION}.${GTK2_PATCH_VERSION})
469 # Try to enforce components
472 set(_GTK2_did_we_find_everything true) # This gets set to GTK2_FOUND
474 include(FindPackageHandleStandardArgs)
476 foreach(_GTK2_component ${GTK2_FIND_COMPONENTS})
477 string(TOUPPER ${_GTK2_component} _COMPONENT_UPPER)
479 if(_GTK2_component STREQUAL "gtk")
480 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "Some or all of the gtk libraries were not found."
484 GTK2_GLIB_INCLUDE_DIR
485 GTK2_GLIBCONFIG_INCLUDE_DIR
489 GTK2_GDKCONFIG_INCLUDE_DIR
492 elseif(_GTK2_component STREQUAL "gtkmm")
493 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "Some or all of the gtkmm libraries were not found."
495 GTK2_GTKMM_INCLUDE_DIR
496 GTK2_GTKMMCONFIG_INCLUDE_DIR
498 GTK2_GLIBMM_INCLUDE_DIR
499 GTK2_GLIBMMCONFIG_INCLUDE_DIR
502 GTK2_GDKMM_INCLUDE_DIR
503 GTK2_GDKMMCONFIG_INCLUDE_DIR
506 elseif(_GTK2_component STREQUAL "glade")
507 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "The glade library was not found."
509 GTK2_GLADE_INCLUDE_DIR
511 elseif(_GTK2_component STREQUAL "glademm")
512 FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "The glademm library was not found."
514 GTK2_GLADEMM_INCLUDE_DIR
515 GTK2_GLADEMMCONFIG_INCLUDE_DIR
519 if(NOT GTK2_${_COMPONENT_UPPER}_FOUND)
520 set(_GTK2_did_we_find_everything false)
524 if(_GTK2_did_we_find_everything AND NOT GTK2_VERSION_CHECK_FAILED)
527 # Unset our variables.
528 set(GTK2_FOUND false)
530 set(GTK2_VERSION_MAJOR)
531 set(GTK2_VERSION_MINOR)
532 set(GTK2_VERSION_PATCH)
533 set(GTK2_INCLUDE_DIRS)
537 if(GTK2_INCLUDE_DIRS)
538 list(REMOVE_DUPLICATES GTK2_INCLUDE_DIRS)