FindGTK2.cmake 30.5 KB
Newer Older
Philip Lowman's avatar
Philip Lowman committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# - FindGTK2.cmake
# This module can find the GTK2 widget libraries and several of its other
# optional components like gtkmm, glade, and glademm.
#
# NOTE: If you intend to use version checking, CMake 2.6.2 or later is
#       required.
#
# Specify one or more of the following components
# as you call this find module. See example below.
#
#   gtk
#   gtkmm
#   glade
#   glademm
#
# The following variables will be defined for your use
#
#   GTK2_FOUND - Were all of your specified components found?
#   GTK2_INCLUDE_DIRS - All include directories
#   GTK2_LIBRARIES - All libraries
21
#   GTK2_DEFINITIONS - Additional compiler flags
Philip Lowman's avatar
Philip Lowman committed
22
23
24
25
26
27
28
29
30
#
#   GTK2_VERSION - The version of GTK2 found (x.y.z)
#   GTK2_MAJOR_VERSION - The major version of GTK2
#   GTK2_MINOR_VERSION - The minor version of GTK2
#   GTK2_PATCH_VERSION - The patch version of GTK2
#
# Optional variables you can define prior to calling this module:
#
#   GTK2_DEBUG - Enables verbose debugging of the module
31
32
#   GTK2_ADDITIONAL_SUFFIXES - Allows defining additional directories to
#                              search for include files
Philip Lowman's avatar
Philip Lowman committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#
#=================
# Example Usage:
#
#   Call find_package() once, here are some examples to pick from:
#
#   Require GTK 2.6 or later
#       find_package(GTK2 2.6 REQUIRED gtk)
#
#   Require GTK 2.10 or later and Glade
#       find_package(GTK2 2.10 REQUIRED gtk glade)
#
#   Search for GTK/GTKMM 2.8 or later
#       find_package(GTK2 2.8 COMPONENTS gtk gtkmm)
#
#   if(GTK2_FOUND)
#      include_directories(${GTK2_INCLUDE_DIRS})
#      add_executable(mygui mygui.cc)
#      target_link_libraries(mygui ${GTK2_LIBRARIES})
#   endif()
#
54

55
56
#=============================================================================
# Copyright 2009 Kitware, Inc.
57
# Copyright 2008-2012 Philip Lowman <philip@yhbt.com>
58
59
60
61
62
63
64
65
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
66
# (To distribute this file outside of CMake, substitute the full
67
68
#  License text for the above reference.)

69
70
71
72
73
74
75
76
77
# Version 1.5 (UNRELEASED) (CMake 2.8.12)
#   * 14236: Detect gthread library
#            Detect pangocairo on windows
#            Detect pangocairo with gtk module instead of with gtkmm
#   * 14259: Use vc100 libraries with MSVC11
#   * 14260: Export a GTK2_DEFINITIONS variable to set /vd2 when appropriate
#            (i.e. MSVC)
#   * Use the optimized/debug syntax for _LIBRARY and _LIBRARIES variables when
#     appropriate. A new set of _RELEASE variables was also added.
78
79
#   * Remove GTK2_SKIP_MARK_AS_ADVANCED option, as now the variables are
#     marked as advanced by SelectLibraryConfigurations
80
#   * Detect gmodule, pangoft2 and pangoxft libraries
81
#   * Create targets for each library.
82
83
84
85
# Version 1.4 (10/4/2012) (CMake 2.8.10)
#   * 12596: Missing paths for FindGTK2 on NetBSD
#   * 12049: Fixed detection of GTK include files in the lib folder on
#            multiarch systems.
86
87
88
# Version 1.3 (11/9/2010) (CMake 2.8.4)
#   * 11429: Add support for detecting GTK2 built with Visual Studio 10.
#            Thanks to Vincent Levesque for the patch.
89
90
91
92
# Version 1.2 (8/30/2010) (CMake 2.8.3)
#   * Merge patch for detecting gdk-pixbuf library (split off
#     from core GTK in 2.21).  Thanks to Vincent Untz for the patch
#     and Ricardo Cruz for the heads up.
93
94
95
# Version 1.1 (8/19/2010) (CMake 2.8.3)
#   * Add support for detecting GTK2 under macports (thanks to Gary Kramlich)
# Version 1.0 (8/12/2010) (CMake 2.8.3)
96
97
98
99
100
101
102
103
#   * Add support for detecting new pangommconfig.h header file
#     (Thanks to Sune Vuorela & the Debian Project for the patch)
#   * Add support for detecting fontconfig.h header
#   * Call find_package(Freetype) since it's required
#   * Add support for allowing users to add additional library directories
#     via the GTK2_ADDITIONAL_SUFFIXES variable (kind of a future-kludge in
#     case the GTK developers change versions on any of the directories in the
#     future).
104
105
106
# Version 0.8 (1/4/2010)
#   * Get module working under MacOSX fink by adding /sw/include, /sw/lib
#     to PATHS and the gobject library
Philip Lowman's avatar
Philip Lowman committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Version 0.7 (3/22/09)
#   * Checked into CMake CVS
#   * Added versioning support
#   * Module now defaults to searching for GTK if COMPONENTS not specified.
#   * Added HKCU prior to HKLM registry key and GTKMM specific environment
#      variable as per mailing list discussion.
#   * Added lib64 to include search path and a few other search paths where GTK
#      may be installed on Unix systems.
#   * Switched to lowercase CMake commands
#   * Prefaced internal variables with _GTK2 to prevent collision
#   * Changed internal macros to functions
#   * Enhanced documentation
# Version 0.6 (1/8/08)
#   Added GTK2_SKIP_MARK_AS_ADVANCED option
# Version 0.5 (12/19/08)
#   Second release to cmake mailing list

#=============================================================
# _GTK2_GET_VERSION
# Internal function to parse the version number in gtkversion.h
#   _OUT_major = Major version number
#   _OUT_minor = Minor version number
#   _OUT_micro = Micro version number
#   _gtkversion_hdr = Header file to parse
#=============================================================
132
133
134

include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)

Philip Lowman's avatar
Philip Lowman committed
135
function(_GTK2_GET_VERSION _OUT_major _OUT_minor _OUT_micro _gtkversion_hdr)
136
    file(STRINGS ${_gtkversion_hdr} _contents REGEX "#define GTK_M[A-Z]+_VERSION[ \t]+")
Philip Lowman's avatar
Philip Lowman committed
137
138
139
140
    if(_contents)
        string(REGEX REPLACE ".*#define GTK_MAJOR_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_major} "${_contents}")
        string(REGEX REPLACE ".*#define GTK_MINOR_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_minor} "${_contents}")
        string(REGEX REPLACE ".*#define GTK_MICRO_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_micro} "${_contents}")
141

Philip Lowman's avatar
Philip Lowman committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        if(NOT ${_OUT_major} MATCHES "[0-9]+")
            message(FATAL_ERROR "Version parsing failed for GTK2_MAJOR_VERSION!")
        endif()
        if(NOT ${_OUT_minor} MATCHES "[0-9]+")
            message(FATAL_ERROR "Version parsing failed for GTK2_MINOR_VERSION!")
        endif()
        if(NOT ${_OUT_micro} MATCHES "[0-9]+")
            message(FATAL_ERROR "Version parsing failed for GTK2_MICRO_VERSION!")
        endif()

        set(${_OUT_major} ${${_OUT_major}} PARENT_SCOPE)
        set(${_OUT_minor} ${${_OUT_minor}} PARENT_SCOPE)
        set(${_OUT_micro} ${${_OUT_micro}} PARENT_SCOPE)
    else()
        message(FATAL_ERROR "Include file ${_gtkversion_hdr} does not exist")
    endif()
endfunction()

#=============================================================
# _GTK2_FIND_INCLUDE_DIR
# Internal function to find the GTK include directories
163
#   _var = variable to set (_INCLUDE_DIR is appended)
Philip Lowman's avatar
Philip Lowman committed
164
165
166
167
168
169
170
171
172
#   _hdr = header file to look for
#=============================================================
function(_GTK2_FIND_INCLUDE_DIR _var _hdr)

    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "_GTK2_FIND_INCLUDE_DIR( ${_var} ${_hdr} )")
    endif()

173
    set(_gtk_packages
174
175
        # If these ever change, things will break.
        ${GTK2_ADDITIONAL_SUFFIXES}
Philip Lowman's avatar
Philip Lowman committed
176
177
178
179
180
181
        glibmm-2.4
        glib-2.0
        atk-1.0
        atkmm-1.6
        cairo
        cairomm-1.0
182
        gdk-pixbuf-2.0
Philip Lowman's avatar
Philip Lowman committed
183
184
185
186
187
188
189
190
191
192
193
        gdkmm-2.4
        giomm-2.4
        gtk-2.0
        gtkmm-2.4
        libglade-2.0
        libglademm-2.4
        pango-1.0
        pangomm-1.4
        sigc++-2.0
    )

194
195
196
197
198
199
200
201
202
    #
    # NOTE: The following suffixes cause searching for header files in both of
    # these directories:
    #         /usr/include/<pkg>
    #         /usr/lib/<pkg>/include
    #

    set(_suffixes)
    foreach(_d ${_gtk_packages})
Philip Lowman's avatar
Philip Lowman committed
203
204
205
206
207
208
209
210
211
        list(APPEND _suffixes ${_d})
        list(APPEND _suffixes ${_d}/include) # for /usr/lib/gtk-2.0/include
    endforeach()

    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
                       "include suffixes = ${_suffixes}")
    endif()

212
213
214
215
216
217
    if(CMAKE_LIBRARY_ARCHITECTURE)
      set(_gtk2_arch_dir /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE})
      if(GTK2_DEBUG)
        message(STATUS "Adding ${_gtk2_arch_dir} to search path for multiarch support")
      endif()
    endif()
218
    find_path(GTK2_${_var}_INCLUDE_DIR ${_hdr}
Philip Lowman's avatar
Philip Lowman committed
219
        PATHS
220
            ${_gtk2_arch_dir}
Philip Lowman's avatar
Philip Lowman committed
221
            /usr/local/lib64
222
            /usr/local/lib
Philip Lowman's avatar
Philip Lowman committed
223
            /usr/lib64
224
225
226
227
228
229
230
231
232
            /usr/lib
            /opt/gnome/include
            /opt/gnome/lib
            /opt/openwin/include
            /usr/openwin/lib
            /sw/include
            /sw/lib
            /opt/local/include
            /opt/local/lib
233
234
            /usr/pkg/lib
            /usr/pkg/include/glib
235
236
237
238
239
240
            $ENV{GTKMM_BASEPATH}/include
            $ENV{GTKMM_BASEPATH}/lib
            [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/include
            [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib
            [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/include
            [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib
Philip Lowman's avatar
Philip Lowman committed
241
242
243
        PATH_SUFFIXES
            ${_suffixes}
    )
244
    mark_as_advanced(GTK2_${_var}_INCLUDE_DIR)
Philip Lowman's avatar
Philip Lowman committed
245

246
247
    if(GTK2_${_var}_INCLUDE_DIR)
        set(GTK2_INCLUDE_DIRS ${GTK2_INCLUDE_DIRS} ${GTK2_${_var}_INCLUDE_DIR} PARENT_SCOPE)
Philip Lowman's avatar
Philip Lowman committed
248
249
    endif()

250
endfunction()
Philip Lowman's avatar
Philip Lowman committed
251
252
253
254

#=============================================================
# _GTK2_FIND_LIBRARY
# Internal function to find libraries packaged with GTK2
255
#   _var = library variable to create (_LIBRARY is appended)
Philip Lowman's avatar
Philip Lowman committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#=============================================================
function(_GTK2_FIND_LIBRARY _var _lib _expand_vc _append_version)

    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "_GTK2_FIND_LIBRARY( ${_var} ${_lib} ${_expand_vc} ${_append_version} )")
    endif()

    # Not GTK versions per se but the versions encoded into Windows
    # import libraries (GtkMM 2.14.1 has a gtkmm-vc80-2_4.lib for example)
    # Also the MSVC libraries use _ for . (this is handled below)
    set(_versions 2.20 2.18 2.16 2.14 2.12
                  2.10  2.8  2.6  2.4  2.2 2.0
                  1.20 1.18 1.16 1.14 1.12
                  1.10  1.8  1.6  1.4  1.2 1.0)

    set(_library)
    set(_library_d)

    set(_library ${_lib})

277
278
    if(_expand_vc AND MSVC)
        # Add vc80/vc90/vc100 midfixes
Philip Lowman's avatar
Philip Lowman committed
279
280
281
282
        if(MSVC80)
            set(_library   ${_library}-vc80)
        elseif(MSVC90)
            set(_library   ${_library}-vc90)
283
284
        elseif(MSVC10)
            set(_library ${_library}-vc100)
285
286
287
        elseif(MSVC11)
            # Up to gtkmm-win 2.22.0-2 there are no vc110 libraries but vc100 can be used
            set(_library ${_library}-vc100)
Philip Lowman's avatar
Philip Lowman committed
288
        endif()
289
        set(_library_d ${_library}-d)
Philip Lowman's avatar
Philip Lowman committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    endif()

    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
                       "After midfix addition = ${_library} and ${_library_d}")
    endif()

    set(_lib_list)
    set(_libd_list)
    if(_append_version)
        foreach(_ver ${_versions})
            list(APPEND _lib_list  "${_library}-${_ver}")
            list(APPEND _libd_list "${_library_d}-${_ver}")
        endforeach()
    else()
        set(_lib_list ${_library})
        set(_libd_list ${_library_d})
    endif()
308

Philip Lowman's avatar
Philip Lowman committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
                       "library list = ${_lib_list} and library debug list = ${_libd_list}")
    endif()

    # For some silly reason the MSVC libraries use _ instead of .
    # in the version fields
    if(_expand_vc AND MSVC)
        set(_no_dots_lib_list)
        set(_no_dots_libd_list)
        foreach(_l ${_lib_list})
            string(REPLACE "." "_" _no_dots_library ${_l})
            list(APPEND _no_dots_lib_list ${_no_dots_library})
        endforeach()
        # And for debug
        set(_no_dots_libsd_list)
        foreach(_l ${_libd_list})
            string(REPLACE "." "_" _no_dots_libraryd ${_l})
            list(APPEND _no_dots_libd_list ${_no_dots_libraryd})
        endforeach()

        # Copy list back to original names
        set(_lib_list ${_no_dots_lib_list})
        set(_libd_list ${_no_dots_libd_list})
    endif()

    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
337
                       "While searching for GTK2_${_var}_LIBRARY, our proposed library list is ${_lib_list}")
Philip Lowman's avatar
Philip Lowman committed
338
339
    endif()

340
    find_library(GTK2_${_var}_LIBRARY_RELEASE
Philip Lowman's avatar
Philip Lowman committed
341
342
343
344
        NAMES ${_lib_list}
        PATHS
            /opt/gnome/lib
            /usr/openwin/lib
345
            /sw/lib
Philip Lowman's avatar
Philip Lowman committed
346
347
348
349
350
351
352
353
            $ENV{GTKMM_BASEPATH}/lib
            [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib
            [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib
        )

    if(_expand_vc AND MSVC)
        if(GTK2_DEBUG)
            message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
354
                           "While searching for GTK2_${_var}_LIBRARY_DEBUG our proposed library list is ${_libd_list}")
Philip Lowman's avatar
Philip Lowman committed
355
356
        endif()

357
        find_library(GTK2_${_var}_LIBRARY_DEBUG
Philip Lowman's avatar
Philip Lowman committed
358
359
360
361
362
363
            NAMES ${_libd_list}
            PATHS
            $ENV{GTKMM_BASEPATH}/lib
            [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib
            [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib
        )
364
    endif()
Philip Lowman's avatar
Philip Lowman committed
365

366
    select_library_configurations(GTK2_${_var})
367

368
369
    set(GTK2_${_var}_LIBRARY ${GTK2_${_var}_LIBRARY} PARENT_SCOPE)
    set(GTK2_${_var}_FOUND ${GTK2_${_var}_FOUND} PARENT_SCOPE)
370

371
    set(GTK2_LIBRARIES ${GTK2_LIBRARIES} ${GTK2_${_var}_LIBRARY})
372
373
374
375
    set(GTK2_LIBRARIES ${GTK2_LIBRARIES} PARENT_SCOPE)

    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
376
                       "GTK2_${_var}_LIBRARY_RELEASE = \"${GTK2_${_var}_LIBRARY_RELEASE}\"")
377
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
378
                       "GTK2_${_var}_LIBRARY_DEBUG   = \"${GTK2_${_var}_LIBRARY_DEBUG}\"")
379
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
380
                       "GTK2_${_var}_LIBRARY         = \"${GTK2_${_var}_LIBRARY}\"")
381
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}]     "
382
                       "GTK2_${_var}_FOUND           = \"${GTK2_${_var}_FOUND}\"")
383
384
    endif()

385
endfunction()
Philip Lowman's avatar
Philip Lowman committed
386

387
388
389
390
391
392
393
394
395
396
397
#=============================================================
# _GTK2_ADJUST_LIB_VARS
# Internal function to create targets for GTK2
#   _var = target to create (GTK_ is removed)
#=============================================================
function(_GTK2_ADJUST_LIB_VARS _var)
    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "_GTK2_ADJUST_LIB_VARS( ${_var} )")
    endif()

398
    string(TOLOWER "${_var}" _basename)
399

400
    if(GTK2_${_var}_FOUND AND NOT TARGET GTK2::${_basename})
401
402
        add_library(GTK2::${_basename} UNKNOWN IMPORTED)

403
        if(GTK2_${_var}_LIBRARY_RELEASE)
404
            set_property(TARGET GTK2::${_basename} APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
405
            set_property(TARGET GTK2::${_basename}        PROPERTY IMPORTED_LOCATION_RELEASE "${GTK2_${_var}_LIBRARY_RELEASE}" )
406
407
        endif()

408
        if(GTK2_${_var}_LIBRARY_DEBUG)
409
            set_property(TARGET GTK2::${_basename} APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
410
            set_property(TARGET GTK2::${_basename}        PROPERTY IMPORTED_LOCATION_DEBUG "${GTK2_${_var}_LIBRARY_DEBUG}" )
411
412
        endif()

413
414
        if(GTK2_${_var}_INCLUDE_DIR)
            set_property(TARGET GTK2::${_basename} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${GTK2_${_var}_INCLUDE_DIR}")
415
416
        endif()

417
418
        if(GTK2_${_var}CONFIG_INCLUDE_DIR)
            set_property(TARGET GTK2::${_basename} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${GTK2_${_var}CONFIG_INCLUDE_DIR}")
419
420
421
422
423
        endif()

        set_property(TARGET GTK2::${_basename} PROPERTY INTERFACE_COMPILE_DEFINITIONS "${GTK2_DEFINITIONS}")

        if(GTK2_USE_IMPORTED_TARGETS)
424
            set(GTK2_${_var}_LIBRARY GTK2::${_basename} PARENT_SCOPE)
425
426
427
428
429
430
431
432
433
434
        endif()
    endif()
endfunction()

function(_GTK2_ADD_TARGET_DEPENDS_INTERNAL _var _property)
    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "_GTK2_ADD_TARGET_DEPENDS_INTERNAL( ${_var} ${_property} )")
    endif()

435
    string(TOLOWER "${_var}" _basename)
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

    if (TARGET GTK2::${_basename})
        foreach(_depend ${ARGN})
            set(_valid_depends)
            if (TARGET GTK2::${_depend})
                list(APPEND _valid_depends GTK2::${_depend})
            endif()
            if (_valid_depends)
                set_property(TARGET GTK2::${_basename} APPEND PROPERTY ${_property} "${_valid_depends}")
            endif()
            set(_valid_depends)
        endforeach()
    endif()
endfunction()

function(_GTK2_ADD_TARGET_DEPENDS _var)
    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "_GTK2_ADD_TARGET_DEPENDS( ${_var} )")
    endif()

457
    string(TOLOWER "${_var}" _basename)
458
459
460
461
462
463
464
465

    get_target_property(_configs GTK2::${_basename} IMPORTED_CONFIGURATIONS)
    _GTK2_ADD_TARGET_DEPENDS_INTERNAL(${_var} INTERFACE_LINK_LIBRARIES ${ARGN})
    foreach(_config ${_configs})
        _GTK2_ADD_TARGET_DEPENDS_INTERNAL(${_var} IMPORTED_LINK_INTERFACE_LIBRARIES_${_config} ${ARGN})
    endforeach()
endfunction()

466
function(_GTK2_ADD_TARGET_INCLUDE_DIRS _var)
467
468
469
470
471
472
473
    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "_GTK2_ADD_TARGET_INCLUDE_DIRS( ${_var} )")
    endif()

    string(TOLOWER "${_var}" _basename)

474
475
476
477
478
479
    if(TARGET GTK2::${_basename})
        foreach(_include ${ARGN})
            set_property(TARGET GTK2::${_basename} APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${_include}")
        endforeach()
    endif()
endfunction()
480

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
function(_GTK2_ADD_TARGET_LIBRARIES _var)
    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "_GTK2_ADD_TARGET_LIBRARIES( ${_var} )")
    endif()

    string(TOLOWER "${_var}" _basename)

    if(TARGET GTK2::${_basename})
        set_property(TARGET GTK2::${_basename} APPEND PROPERTY INTERFACE_LINK_LIBRARIES "${ARGN}")
        foreach(_library ${ARGN})
            set_property(TARGET GTK2::${_basename} APPEND PROPERTY IMPORTED_LINK_INTERFACE_LIBRARIES_$ "${_library}")
        endforeach()
    endif()
endfunction()

Philip Lowman's avatar
Philip Lowman committed
497
498
499
500
501
502
503
504
505
#=============================================================

#
# main()
#

set(GTK2_FOUND)
set(GTK2_INCLUDE_DIRS)
set(GTK2_LIBRARIES)
506
set(GTK2_DEFINITIONS)
Philip Lowman's avatar
Philip Lowman committed
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522

if(NOT GTK2_FIND_COMPONENTS)
    # Assume they only want GTK
    set(GTK2_FIND_COMPONENTS gtk)
endif()

#
# If specified, enforce version number
#
if(GTK2_FIND_VERSION)
    cmake_minimum_required(VERSION 2.6.2)
    set(GTK2_FAILED_VERSION_CHECK true)
    if(GTK2_DEBUG)
        message(STATUS "[FindGTK2.cmake:${CMAKE_CURRENT_LIST_LINE}] "
                       "Searching for version ${GTK2_FIND_VERSION}")
    endif()
523
    _GTK2_FIND_INCLUDE_DIR(GTK gtk/gtk.h)
Philip Lowman's avatar
Philip Lowman committed
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
    if(GTK2_GTK_INCLUDE_DIR)
        _GTK2_GET_VERSION(GTK2_MAJOR_VERSION
                          GTK2_MINOR_VERSION
                          GTK2_PATCH_VERSION
                          ${GTK2_GTK_INCLUDE_DIR}/gtk/gtkversion.h)
        set(GTK2_VERSION
            ${GTK2_MAJOR_VERSION}.${GTK2_MINOR_VERSION}.${GTK2_PATCH_VERSION})
        if(GTK2_FIND_VERSION_EXACT)
            if(GTK2_VERSION VERSION_EQUAL GTK2_FIND_VERSION)
                set(GTK2_FAILED_VERSION_CHECK false)
            endif()
        else()
            if(GTK2_VERSION VERSION_EQUAL   GTK2_FIND_VERSION OR
               GTK2_VERSION VERSION_GREATER GTK2_FIND_VERSION)
                set(GTK2_FAILED_VERSION_CHECK false)
            endif()
        endif()
    else()
        # If we can't find the GTK include dir, we can't do version checking
        if(GTK2_FIND_REQUIRED AND NOT GTK2_FIND_QUIETLY)
            message(FATAL_ERROR "Could not find GTK2 include directory")
        endif()
        return()
    endif()

    if(GTK2_FAILED_VERSION_CHECK)
        if(GTK2_FIND_REQUIRED AND NOT GTK2_FIND_QUIETLY)
            if(GTK2_FIND_VERSION_EXACT)
                message(FATAL_ERROR "GTK2 version check failed.  Version ${GTK2_VERSION} was found, version ${GTK2_FIND_VERSION} is needed exactly.")
            else()
                message(FATAL_ERROR "GTK2 version check failed.  Version ${GTK2_VERSION} was found, at least version ${GTK2_FIND_VERSION} is required")
            endif()
556
557
        endif()

Philip Lowman's avatar
Philip Lowman committed
558
559
560
561
562
        # If the version check fails, exit out of the module here
        return()
    endif()
endif()

563
564
565
566
567
568
569
570
571
572
573
574
575
576
#
# On MSVC, according to https://wiki.gnome.org/gtkmm/MSWindows, the /vd2 flag needs to be
# passed to the compiler in order to use gtkmm
#
if(MSVC)
    foreach(_GTK2_component ${GTK2_FIND_COMPONENTS})
        if(_GTK2_component STREQUAL "gtkmm")
            set(GTK2_DEFINITIONS "/vd2")
        elseif(_GTK2_component STREQUAL "glademm")
            set(GTK2_DEFINITIONS "/vd2")
        endif()
    endforeach()
endif()

Philip Lowman's avatar
Philip Lowman committed
577
578
579
580
#
# Find all components
#

581
find_package(Freetype QUIET)
582
583
584
list(APPEND GTK2_INCLUDE_DIRS ${FREETYPE_INCLUDE_DIRS})
list(APPEND GTK2_LIBRARIES ${FREETYPE_LIBRARIES})

Philip Lowman's avatar
Philip Lowman committed
585
586
foreach(_GTK2_component ${GTK2_FIND_COMPONENTS})
    if(_GTK2_component STREQUAL "gtk")
587
        _GTK2_FIND_INCLUDE_DIR(GTK gtk/gtk.h)
588
        if(UNIX)
589
            _GTK2_FIND_LIBRARY    (GTK gtk-x11 false true)
590
        else()
591
            _GTK2_FIND_LIBRARY    (GTK gtk-win32 false true)
592
        endif()
593
        _GTK2_ADJUST_LIB_VARS (GTK)
Philip Lowman's avatar
Philip Lowman committed
594

595
596
        _GTK2_FIND_INCLUDE_DIR(GDK gdk/gdk.h)
        _GTK2_FIND_INCLUDE_DIR(GDKCONFIG gdkconfig.h)
597
        if(UNIX)
598
            _GTK2_FIND_LIBRARY    (GDK gdk-x11 false true)
599
        else()
600
            _GTK2_FIND_LIBRARY    (GDK gdk-win32 false true)
601
        endif()
602
        _GTK2_ADJUST_LIB_VARS (GDK)
603

604
605
606
        _GTK2_FIND_INCLUDE_DIR(CAIRO cairo.h)
        _GTK2_FIND_LIBRARY    (CAIRO cairo false false)
        _GTK2_ADJUST_LIB_VARS (CAIRO)
Philip Lowman's avatar
Philip Lowman committed
607

608
        _GTK2_FIND_INCLUDE_DIR(FONTCONFIG fontconfig/fontconfig.h)
609

610
611
612
        _GTK2_FIND_INCLUDE_DIR(PANGO pango/pango.h)
        _GTK2_FIND_LIBRARY    (PANGO pango false true)
        _GTK2_ADJUST_LIB_VARS (PANGO)
Philip Lowman's avatar
Philip Lowman committed
613

614
615
        _GTK2_FIND_LIBRARY    (PANGOCAIRO pangocairo false true)
        _GTK2_ADJUST_LIB_VARS (PANGOCAIRO)
616

617
618
        _GTK2_FIND_LIBRARY    (PANGOFT2 pangoft2 false true)
        _GTK2_ADJUST_LIB_VARS (PANGOFT2)
619

620
621
        _GTK2_FIND_LIBRARY    (PANGOXFT pangoxft false true)
        _GTK2_ADJUST_LIB_VARS (PANGOXFT)
622

623
624
625
        _GTK2_FIND_INCLUDE_DIR(GDK_PIXBUF gdk-pixbuf/gdk-pixbuf.h)
        _GTK2_FIND_LIBRARY    (GDK_PIXBUF gdk_pixbuf false true)
        _GTK2_ADJUST_LIB_VARS (GDK_PIXBUF)
626

627
628
        _GTK2_FIND_LIBRARY    (GTHREAD gthread false true)
        _GTK2_ADJUST_LIB_VARS (GTHREAD)
629

630
631
        _GTK2_FIND_LIBRARY    (GMODULE gmodule false true)
        _GTK2_ADJUST_LIB_VARS (MODULE)
632

633
634
        _GTK2_FIND_LIBRARY    (GIO gio false true)
        _GTK2_ADJUST_LIB_VARS (GIO)
635

636
637
638
        _GTK2_FIND_INCLUDE_DIR(ATK atk/atk.h)
        _GTK2_FIND_LIBRARY    (ATK atk false true)
        _GTK2_ADJUST_LIB_VARS (ATK)
Philip Lowman's avatar
Philip Lowman committed
639

640
641
642
        _GTK2_FIND_INCLUDE_DIR(GOBJECT glib-object.h)
        _GTK2_FIND_LIBRARY    (GOBJECT gobject false true)
        _GTK2_ADJUST_LIB_VARS (GOBJECT)
Philip Lowman's avatar
Philip Lowman committed
643

644
645
646
647
        _GTK2_FIND_INCLUDE_DIR(GLIB glib.h)
        _GTK2_FIND_INCLUDE_DIR(GLIBCONFIG glibconfig.h)
        _GTK2_FIND_LIBRARY    (GLIB glib false true)
        _GTK2_ADJUST_LIB_VARS (GLIB)
648

649
650
651
652
653
654
655
656
        #_GTK2_ADD_TARGET_DEPENDS(GOBJECT glib)
        _GTK2_ADD_TARGET_DEPENDS(GOBJECT glib)
        _GTK2_ADD_TARGET_DEPENDS(ATK gobject glib)
        _GTK2_ADD_TARGET_DEPENDS(GIO gobject glib)
        _GTK2_ADD_TARGET_DEPENDS(GTHREAD glib)
        _GTK2_ADD_TARGET_DEPENDS(GMODULE glib)
        _GTK2_ADD_TARGET_DEPENDS(GDK_PIXBUF gobject glib)
        _GTK2_ADD_TARGET_INCLUDE_DIRS(CAIRO ${FREETYPE_INCLUDE_DIRS} ${GTK2_FONTCONFIG_INCLUDE_DIRS})
657
        _GTK2_ADD_TARGET_LIBRARIES(CAIRO ${FREETYPE_LIBRARIES})
658
659
660
661
        _GTK2_ADD_TARGET_DEPENDS(PANGO gobject glib)
        _GTK2_ADD_TARGET_DEPENDS(PANGOCAIRO pango cairo gobject glib)
        _GTK2_ADD_TARGET_DEPENDS(GDK pangocairo pango cairo gdk_pixbuf gobject glib)
        _GTK2_ADD_TARGET_DEPENDS(GTK gdk atk gio pangoft2 pangocairo pango cairo gdk_pixbuf gthread gobject glib)
Philip Lowman's avatar
Philip Lowman committed
662

663
    elseif(_GTK2_component STREQUAL "gtkmm")
Philip Lowman's avatar
Philip Lowman committed
664

665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
        _GTK2_FIND_INCLUDE_DIR(GTKMM gtkmm.h)
        _GTK2_FIND_INCLUDE_DIR(GTKMMCONFIG gtkmmconfig.h)
        _GTK2_FIND_LIBRARY    (GTKMM gtkmm true true)
        _GTK2_ADJUST_LIB_VARS (GTKMM)

        _GTK2_FIND_INCLUDE_DIR(GDKMM gdkmm.h)
        _GTK2_FIND_INCLUDE_DIR(GDKMMCONFIG gdkmmconfig.h)
        _GTK2_FIND_LIBRARY    (GDKMM gdkmm true true)
        _GTK2_ADJUST_LIB_VARS (GDKMM)

        _GTK2_FIND_INCLUDE_DIR(PANGOMM pangomm.h)
        _GTK2_FIND_INCLUDE_DIR(PANGOMMCONFIG pangommconfig.h)
        _GTK2_FIND_LIBRARY    (PANGOMM pangomm true true)
        _GTK2_ADJUST_LIB_VARS (PANGOMM)

        _GTK2_FIND_INCLUDE_DIR(CAIROMM cairomm/cairomm.h)
        _GTK2_FIND_INCLUDE_DIR(CAIROMMCONFIG cairommconfig.h)
        _GTK2_FIND_LIBRARY    (CAIROMM cairomm true true)
        _GTK2_ADJUST_LIB_VARS (CAIROMM)

        _GTK2_FIND_INCLUDE_DIR(GIOMM giomm.h)
        _GTK2_FIND_INCLUDE_DIR(GIOMMCONFIG giommconfig.h)
        _GTK2_FIND_LIBRARY    (GIOMM giomm true true)
        _GTK2_ADJUST_LIB_VARS (GIOMM)

        _GTK2_FIND_INCLUDE_DIR(ATKMM atkmm.h)
        _GTK2_FIND_LIBRARY    (ATKMM atkmm true true)
        _GTK2_ADJUST_LIB_VARS (ATKMM)

        _GTK2_FIND_INCLUDE_DIR(GLIBMM glibmm.h)
        _GTK2_FIND_INCLUDE_DIR(GLIBMMCONFIG glibmmconfig.h)
        _GTK2_FIND_LIBRARY    (GLIBMM glibmm true true)
        _GTK2_ADJUST_LIB_VARS (GLIBMM)

        _GTK2_FIND_INCLUDE_DIR(SIGC++ sigc++/sigc++.h)
        _GTK2_FIND_INCLUDE_DIR(SIGC++CONFIG sigc++config.h)
        _GTK2_FIND_LIBRARY    (SIGC++ sigc true true)
        _GTK2_ADJUST_LIB_VARS (SIGC++)

        _GTK2_ADD_TARGET_DEPENDS(GLIBMM gobject sigc glib)
        _GTK2_ADD_TARGET_DEPENDS(GIOMM gio glibmm gobject sigc glib)
        _GTK2_ADD_TARGET_DEPENDS(ATKMM atk glibmm gobject sigc glib)
        _GTK2_ADD_TARGET_DEPENDS(CAIROMM cairo sigc++)
        _GTK2_ADD_TARGET_DEPENDS(PANGOMM glibmm cairomm pangocairo sigc++ pango cairo gobject glib)
        _GTK2_ADD_TARGET_DEPENDS(GDKMM giomm pangomm gtk glibmm cairomm sigc++ gdk atk gio pangoft2 pangocairo gdk_pixbuf cairo pango gobject glib)
        _GTK2_ADD_TARGET_INCLUDE_DIRS(GDKMM ${FREETYPE_INCLUDE_DIRS} ${GTK2_FONTCONFIG_INCLUDE_DIRS})
711
        _GTK2_ADD_TARGET_LIBRARIES(GDKMM ${FREETYPE_LIBRARIES})
712
713
        _GTK2_ADD_TARGET_DEPENDS(GTKMM atkmm gdkmm giomm pangomm gtk glibmm cairomm sigc++ gdk atk gio pangoft2 pangocairo gdk_pixbuf cairo pango gthread gobject glib)
        _GTK2_ADD_TARGET_INCLUDE_DIRS(GTKMM ${FREETYPE_INCLUDE_DIRS} ${GTK2_FONTCONFIG_INCLUDE_DIRS})
714
        _GTK2_ADD_TARGET_LIBRARIES(GTKMM ${FREETYPE_LIBRARIES})
715

Philip Lowman's avatar
Philip Lowman committed
716
717
    elseif(_GTK2_component STREQUAL "glade")

718
719
720
721
722
        _GTK2_FIND_INCLUDE_DIR(GLADE glade/glade.h)
        _GTK2_FIND_LIBRARY    (GLADE glade false true)
        _GTK2_ADJUST_LIB_VARS (GLADE)
        _GTK2_ADD_TARGET_DEPENDS(GLADE gtk gdk atk gio pangoft2 pangocairo gdk_pixbuf cairo pango gobject glib)
        _GTK2_ADD_TARGET_INCLUDE_DIRS(GLADE ${FREETYPE_INCLUDE_DIRS} ${GTK2_FONTCONFIG_INCLUDE_DIRS})
723
        _GTK2_ADD_TARGET_LIBRARIES(GLADE ${FREETYPE_LIBRARIES})
724

Philip Lowman's avatar
Philip Lowman committed
725
726
    elseif(_GTK2_component STREQUAL "glademm")

727
728
729
730
731
732
        _GTK2_FIND_INCLUDE_DIR(GLADEMM libglademm.h)
        _GTK2_FIND_INCLUDE_DIR(GLADEMMCONFIG libglademmconfig.h)
        _GTK2_FIND_LIBRARY    (GLADEMM glademm true true)
        _GTK2_ADJUST_LIB_VARS (GLADEMM)
        _GTK2_ADD_TARGET_DEPENDS(GLADEMM gtkmm glade atkmm gdkmm giomm pangomm glibmm cairomm sigc++ gtk gdk atk gio pangoft pangocairo gdk_pixbuf cairo pango gthread gobject glib)
        _GTK2_ADD_TARGET_INCLUDE_DIRS(GLADEMM ${FREETYPE_INCLUDE_DIRS} ${GTK2_FONTCONFIG_INCLUDE_DIRS})
733
        _GTK2_ADD_TARGET_LIBRARIES(GLADEMM ${FREETYPE_LIBRARIES})
Philip Lowman's avatar
Philip Lowman committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756

    else()
        message(FATAL_ERROR "Unknown GTK2 component ${_component}")
    endif()
endforeach()

#
# Solve for the GTK2 version if we haven't already
#
if(NOT GTK2_FIND_VERSION AND GTK2_GTK_INCLUDE_DIR)
    _GTK2_GET_VERSION(GTK2_MAJOR_VERSION
                      GTK2_MINOR_VERSION
                      GTK2_PATCH_VERSION
                      ${GTK2_GTK_INCLUDE_DIR}/gtk/gtkversion.h)
    set(GTK2_VERSION ${GTK2_MAJOR_VERSION}.${GTK2_MINOR_VERSION}.${GTK2_PATCH_VERSION})
endif()

#
# Try to enforce components
#

set(_GTK2_did_we_find_everything true)  # This gets set to GTK2_FOUND

757
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
Philip Lowman's avatar
Philip Lowman committed
758
759
760
761

foreach(_GTK2_component ${GTK2_FIND_COMPONENTS})
    string(TOUPPER ${_GTK2_component} _COMPONENT_UPPER)

762
763
    set(GTK2_${_COMPONENT_UPPER}_FIND_QUIETLY ${GTK2_FIND_QUIETLY})

Philip Lowman's avatar
Philip Lowman committed
764
765
766
767
768
769
770
771
    if(_GTK2_component STREQUAL "gtk")
        FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "Some or all of the gtk libraries were not found."
            GTK2_GTK_LIBRARY
            GTK2_GTK_INCLUDE_DIR

            GTK2_GDK_INCLUDE_DIR
            GTK2_GDKCONFIG_INCLUDE_DIR
            GTK2_GDK_LIBRARY
772
773
774
775

            GTK2_GLIB_INCLUDE_DIR
            GTK2_GLIBCONFIG_INCLUDE_DIR
            GTK2_GLIB_LIBRARY
Philip Lowman's avatar
Philip Lowman committed
776
777
778
779
780
781
782
        )
    elseif(_GTK2_component STREQUAL "gtkmm")
        FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "Some or all of the gtkmm libraries were not found."
            GTK2_GTKMM_LIBRARY
            GTK2_GTKMM_INCLUDE_DIR
            GTK2_GTKMMCONFIG_INCLUDE_DIR

783
784
785
786
            GTK2_GDKMM_INCLUDE_DIR
            GTK2_GDKMMCONFIG_INCLUDE_DIR
            GTK2_GDKMM_LIBRARY

Philip Lowman's avatar
Philip Lowman committed
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
            GTK2_GLIBMM_INCLUDE_DIR
            GTK2_GLIBMMCONFIG_INCLUDE_DIR
            GTK2_GLIBMM_LIBRARY
        )
    elseif(_GTK2_component STREQUAL "glade")
        FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "The glade library was not found."
            GTK2_GLADE_LIBRARY
            GTK2_GLADE_INCLUDE_DIR
        )
    elseif(_GTK2_component STREQUAL "glademm")
        FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK2_${_COMPONENT_UPPER} "The glademm library was not found."
            GTK2_GLADEMM_LIBRARY
            GTK2_GLADEMM_INCLUDE_DIR
            GTK2_GLADEMMCONFIG_INCLUDE_DIR
        )
    endif()

    if(NOT GTK2_${_COMPONENT_UPPER}_FOUND)
        set(_GTK2_did_we_find_everything false)
    endif()
endforeach()

if(_GTK2_did_we_find_everything AND NOT GTK2_VERSION_CHECK_FAILED)
    set(GTK2_FOUND true)
else()
    # Unset our variables.
    set(GTK2_FOUND false)
    set(GTK2_VERSION)
    set(GTK2_VERSION_MAJOR)
    set(GTK2_VERSION_MINOR)
    set(GTK2_VERSION_PATCH)
    set(GTK2_INCLUDE_DIRS)
    set(GTK2_LIBRARIES)
820
    set(GTK2_DEFINITIONS)
Philip Lowman's avatar
Philip Lowman committed
821
822
823
824
825
826
endif()

if(GTK2_INCLUDE_DIRS)
   list(REMOVE_DUPLICATES GTK2_INCLUDE_DIRS)
endif()