]> git.draconx.ca Git - dxcommon.git/blob - m4/glib.m4
32d27848da5d2656480a197960557bf916f4c8ba
[dxcommon.git] / m4 / glib.m4
1 dnl Copyright © 2009, 2019 Nick Bowler
2 dnl
3 dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
4 dnl This is free software: you are free to do what the fuck you want to.
5 dnl There is NO WARRANTY, to the extent permitted by law.
6
7 dnl DX_LIB_GLIB2([min-version], [modules], [action-if-ok], [action-if-fail])
8 dnl
9 dnl Search for GLib-2 libraries, with the given minimum version.  The modules
10 dnl argument specifies which additional GLib libraries are required, which is
11 dnl a space-separated list of zero or more of the following values:
12 dnl
13 dnl   gthread
14 dnl   gobject
15 dnl   gio
16 dnl   gio-unix
17 dnl   gmodule
18 dnl
19 dnl Each detected module's flags are set in LIBxxx_CFLAGS and LIBxxx_LIBS,
20 dnl where xxx is the module's name in all capital letters and with hyphens
21 dnl converted to underscores (cf. AS_TR_SH).  The core GLib flags are set
22 dnl in LIBGLIB_CFLAGS and LIBGLIB_LIBS.  The combination of all selected
23 dnl and detected modules is set in GLIB_CFLAGS and GLIB_LIBS.  All of
24 dnl these variables are substituted as normal.
25 dnl
26 dnl If GLib and all selected modules are successfully found, action-if-ok
27 dnl is run.  Otherwise, action-if-fail is run.  If necessary, use the
28 dnl dx_cv_libxxx_lib_found cache variables to determine exactly which
29 dnl modules are not available.
30 AC_DEFUN([DX_LIB_GLIB2],
31 [GLIB_CFLAGS= GLIB_LIBS= _dx_lib_glib_search_ok=true
32 m4_define([_DX_LIB_GLIB2_TESTHEAD], [#include <glib.h>])dnl
33 _DX_LIB_GLIB2_VERSION_TEST(m4_split([$1], [\.]))dnl
34 m4_define([_DX_LIB_GLIB2_TESTMAIN],
35   [const char *v = glib_check_version(2,0,0);])dnl
36 m4_define([_DX_GLIB_MODULES], [glib])dnl
37 m4_append_uniq_w([_DX_GLIB_MODULES], [$2])dnl
38
39 m4_map_args_w(m4_defn([_DX_GLIB_MODULES]), [_DX_LIB_GLIB2_MODULE(], [)])
40 AC_SUBST([GLIB_CFLAGS])dnl
41 AC_SUBST([GLIB_LIBS])dnl
42 AS_IF([$_dx_lib_glib_search_ok], [$3], [m4_default([$4], [AC_MSG_FAILURE(
43 [GLib[]m4_ifnblank([$1], [ version $1 or newer]) is required.  The latest
44 version may be found at <https://www.gtk.org/>.
45 m4_newline([DX_LIB_USERFLAG_BLURB([libglib], [GLib])])
46 m4_newline([DX_LIB_PKGCONFIG_BLURB([glib], [GLib])])
47 ])])])])
48
49 AC_DEFUN([_DX_LIB_GLIB2_MODULE],
50 [DX_LIB_SETUP([lib$1],
51 [m4_if([$1], [glib],     [GLib],
52        [$1], [gio],      [GIO],
53        [$1], [gio-unix], [GIO-Unix],
54        [$1], [gthread],  [GThread],
55        [$1], [gobject],  [GObject],
56        [$1], [gmodule],  [GModule])])dnl
57 DX_LIB_PKGCONFIG_FLAGS([lib$1],
58   [m4_if([$1], [gmodule], [gmodule-no-export-2.0], [$1-2.0])])
59 AC_MSG_CHECKING([for lib$1])
60 DX_LIB_SEARCH_LINK([lib$1],
61   [AC_LANG_PROGRAM([_DX_LIB_GLIB2_TESTHEAD
62 m4_n(m4_if([$1], [gobject], [[#include <glib-object.h>]]))dnl
63 m4_n(m4_if([$1], [gio], [[#include <gio/gio.h>]]))dnl
64 m4_n(m4_if([$1], [gio-unix], [[#include <gio/gunixmounts.h>]]))dnl
65 m4_n(m4_if([$1], [gmodule], [[#include <gmodule.h>]]))],
66     [_DX_LIB_GLIB2_TESTMAIN
67 m4_if([$1], [glib], [],
68       [$1], [gobject], [g_type_init();
69 { GObject *obj = g_object_new(G_TYPE_OBJECT, NULL); }],
70       [$1], [gthread], [g_thread_init(NULL);
71 { GThread *thr = g_thread_self(); }],
72       [$1], [gio], [{ GFile *f = g_file_new_for_path("/"); }],
73       [$1], [gio-unix], [{ GUnixMountEntry *m = g_unix_mount_at("/", NULL); }],
74       [$1], [gmodule], [{ GModule *m = g_module_open(NULL, 0); }],
75       [DX_LIB_COMPILE_ERROR([unsupported module])])])],
76   [ [[$LIBGLIB_CFLAGS m4_if([$1], [glib], [], [m4_toupper([$lib$1_cflags])])],
77      [$LIBGLIB_LIBS m4_if([$1], [glib], [], [m4_toupper([$lib$1_libs])])]],
78     [[$AS_TR_SH([dx_cv_lib$1_pkg_cflags])],
79      [$AS_TR_SH([dx_cv_lib$1_pkg_libs])],
80      [test x"$AS_TR_SH([dx_cv_lib$1_pkg_found])" = x"yes"]],
81     m4_if([$1], [gthread], [[[$LIBGLIB_CFLAGS -pthread],
82           [-pthread -lgthread-2.0 -lglib-2.0]],])
83     [[$LIBGLIB_CFLAGS], [m4_if([$1], [glib], [], [-l$1-2.0]) -lglib-2.0]],
84   ])
85 AS_IF([test x"$AS_TR_SH([dx_cv_lib$1_lib_found])" = x"yes"],
86 [set -f
87 set x $AS_TR_SH([m4_toupper([lib$1_cflags])]); shift
88 for _dx_i
89 do
90   for _dx_j in $GLIB_CFLAGS; do
91     if test x"$_dx_i" = x"$_dx_j"; then
92       shift; continue 2
93     fi
94   done
95   set x ${1+"$[]@"} "$[]1"; shift; shift
96 done
97 GLIB_CFLAGS=${GLIB_CFLAGS:+"$GLIB_CFLAGS "}$[]*
98 set x $AS_TR_SH([m4_toupper([lib$1_libs])]); shift
99 for _dx_i
100 do
101   for _dx_j in $GLIB_LIBS; do
102     if test x"$_dx_i" = x"$_dx_j"; then
103       shift; continue 2
104     fi
105   done
106   set x ${1+"$[]@"} "$[]1"; shift; shift
107 done
108 GLIB_LIBS=$[]*${GLIB_LIBS:+" $GLIB_LIBS"}
109 set +f],
110   [_dx_lib_glib_search_ok=false])
111 ])
112
113 AC_DEFUN([_DX_LIB_GLIB2_VERSION_TEST], [m4_append([_DX_LIB_GLIB2_TESTHEAD], [
114 [#if !GLIB_CHECK_VERSION(]m4_default([$1],
115 [0]), m4_default([$2],
116 [0]), m4_default([$3],
117 [0]))
118 DX_LIB_COMPILE_ERROR([glib version insufficient])
119 #endif])])