[GTK] [CMake] Improve the way we locate gobject-introspection
[WebKit-https.git] / Source / cmake / FindGObjectIntrospection.cmake
1 # - Try to find gobject-introspection
2 # Once done, this will define
3 #
4 #  INTROSPECTION_FOUND - system has gobject-introspection
5 #  INTROSPECTION_SCANNER - the gobject-introspection scanner, g-ir-scanner
6 #  INTROSPECTION_COMPILER - the gobject-introspection compiler, g-ir-compiler
7 #  INTROSPECTION_GENERATE - the gobject-introspection generate, g-ir-generate
8 #  INTROSPECTION_GIRDIR
9 #  INTROSPECTION_TYPELIBDIR
10 #  INTROSPECTION_CFLAGS
11 #  INTROSPECTION_LIBS
12 #
13 # Copyright (C) 2010, Pino Toscano, <pino@kde.org>
14 #
15 # Redistribution and use is allowed according to the terms of the BSD license.
16
17 macro(_GIR_GET_PKGCONFIG_VAR _outvar _varname _extra_args)
18     execute_process(
19         COMMAND ${PKG_CONFIG_EXECUTABLE} --variable=${_varname} ${_extra_args} gobject-introspection-1.0
20         OUTPUT_VARIABLE _result
21         RESULT_VARIABLE _null
22     )
23
24     if (_null)
25     else ()
26         string(REGEX REPLACE "[\r\n]" " " _result "${_result}")
27         string(REGEX REPLACE " +$" ""  _result "${_result}")
28         separate_arguments(_result)
29         set(${_outvar} ${_result} CACHE INTERNAL "")
30     endif ()
31 endmacro(_GIR_GET_PKGCONFIG_VAR)
32
33 find_package(PkgConfig)
34 if (PKG_CONFIG_FOUND)
35     if (PACKAGE_FIND_VERSION_COUNT GREATER 0)
36         set(_gir_version_cmp ">=${PACKAGE_FIND_VERSION}")
37     endif ()
38     pkg_check_modules(_pc_gir gobject-introspection-1.0${_gir_version_cmp})
39     if (_pc_gir_FOUND)
40         set(INTROSPECTION_FOUND TRUE)
41         _gir_get_pkgconfig_var(INTROSPECTION_SCANNER "g_ir_scanner" "")
42         _gir_get_pkgconfig_var(INTROSPECTION_COMPILER "g_ir_compiler" "")
43         _gir_get_pkgconfig_var(INTROSPECTION_GENERATE "g_ir_generate" "")
44         _gir_get_pkgconfig_var(INTROSPECTION_GIRDIR "girdir" "")
45         _gir_get_pkgconfig_var(INTROSPECTION_TYPELIBDIR "typelibdir" "")
46         _gir_get_pkgconfig_var(INTROSPECTION_INSTALL_GIRDIR "girdir" "--define-variable=prefix=${CMAKE_INSTALL_PREFIX}")
47         _gir_get_pkgconfig_var(INTROSPECTION_INSTALL_TYPELIBDIR "typelibdir" "--define-variable=prefix=${CMAKE_INSTALL_PREFIX}")
48         set(INTROSPECTION_CFLAGS "${_pc_gir_CFLAGS}")
49         set(INTROSPECTION_LIBS "${_pc_gir_LIBS}")
50     endif ()
51 endif ()
52
53 mark_as_advanced(
54     INTROSPECTION_SCANNER
55     INTROSPECTION_COMPILER
56     INTROSPECTION_GENERATE
57     INTROSPECTION_GIRDIR
58     INTROSPECTION_TYPELIBDIR
59     INTROSPECTION_CFLAGS
60     INTROSPECTION_LIBS
61     INTROSPECTION_INSTALL_GIRDIR
62     INTROSPECTION_INSTALL_TYPELIBDIR
63 )