]> git.draconx.ca Git - dxcommon.git/blob - m4/libhelper.m4
Add libpng detection macro.
[dxcommon.git] / m4 / libhelper.m4
1 dnl Copyright © 2014 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_SETUP(env-base, [human-name])
8 dnl
9 dnl Helper to setup a library test.  Defines two user variables FOO_CFLAGS
10 dnl and FOO_LIBS, where FOO is replaced with env-base in uppercase.  The
11 dnl human-name is the name of the library used in help text, by default the
12 dnl same as env-base.
13 AC_DEFUN([DX_LIB_SETUP], [m4_do(
14         [AC_ARG_VAR(m4_toupper([$1])[_CFLAGS],
15                 [C compiler flags for ]m4_default([$2], [$1]))],
16         [AC_ARG_VAR(m4_toupper([$1])[_LIBS],
17                 [linker flags for ]m4_default([$2], [$1]))])])
18
19 dnl DX_LIB_PKGCONFIG_FLAGS(env-base, [library-name])
20 dnl
21 dnl Helper for querying a library's CFLAGS and LIBS values from the
22 dnl pkg-config database.  The results are stored in the cache variables
23 dnl dx_cv_foo_pkg_cflags and dx_cv_foo_pkg_libs, where foo is replaced with
24 dnl env-base in lowercase.  If not specified, the library name defaults to
25 dnl env-base.
26 AC_DEFUN([DX_LIB_PKGCONFIG_FLAGS],
27         [_DX_LIB_PKGCONFIG_FLAGS(m4_tolower([$1]),
28                 [m4_default([$2], [$1])])])
29
30 dnl Internal helper macro for the above.
31 AC_DEFUN([_DX_LIB_PKGCONFIG_FLAGS], [AS_IF([test x"$PKG_CONFIG" != x""],
32         [AC_CACHE_CHECK([pkg-config database for $2], [dx_cv_$1_pkg_found],
33                 [m4_do([dx_cv_$1_pkg_found=yes],
34                         [m4_newline([DX_PKG_CONFIG([dx_cv_$1_pkg_cflags],
35                                 [--cflags "$2"], [],
36                                 [dx_cv_$1_pkg_found=no])])],
37                         [m4_newline([DX_PKG_CONFIG([dx_cv_$1_pkg_libs],
38                                 [--libs "$2"], [],
39                                 [dx_cv_$1_pkg_found=no])])])])])])
40
41 dnl DX_LIB_SEARCH(env-base, test, test-matrix)
42 dnl
43 dnl Helper to search for the correct compiler/linker flags for a particular
44 dnl library.  The test-matrix is an ordered, quoted list of quoted lists.
45 dnl Each entry in the test matrix has the following form:
46 dnl
47 dnl    [cflags, libs, shell-condition]
48 dnl
49 dnl For each item, the specified cflags and libs are temporarily appended to
50 dnl CFLAGS and LIBS, respectively, then the given test is run.  The test
51 dnl argument must be an m4 macro which takes one argument (action-if-ok).
52 dnl This macro shall expand to shell code which executes action-if-ok if and
53 dnl only if the test was successful.  The test macro is expanded only once.
54 dnl
55 dnl If a test is successful, dx_cv_foo_lib_found is set to "yes", where foo is
56 dnl replaced with env-base in lowercase.  Subsequent tests are not tried.
57 dnl The cflags and libs values from a successful test are stored in FOO_CFLAGS
58 dnl and FOO_LIBS, respectively, where FOO is replaced with env-base in
59 dnl uppercase.  These values are AC_SUBST-ed.  If no test was successful,
60 dnl dx_cv_foo_lib_found is set to "no".
61 dnl
62 dnl The result ("yes" or "no") are printed so this macro should be preceded
63 dnl by a call to AC_MSG_CHECKING.
64 AC_DEFUN([DX_LIB_SEARCH], [m4_divert_push([KILL])
65
66 m4_pushdef([_DX_CVNAME], m4_tolower([dx_cv_$1_lib_])[$][1])
67 m4_pushdef([_DX_FNNAME], m4_tolower([dx_fn_$1_lib_])[$][1])
68
69 m4_divert([INIT_PREPARE])dnl
70 # Helper function to test whether $1 works.
71 _DX_FNNAME([test]) () {
72 CFLAGS="$_dx_save_cflags $[]1"
73 LIBS="$_dx_save_libs $[]2"
74 m4_apply([$2], [dnl
75         _DX_CVNAME([found])=yes
76         _DX_CVNAME([cflags])=$[]1
77         _DX_CVNAME([libs])=$[]2])
78 }
79
80 m4_divert_pop()dnl
81 AC_CACHE_VAL([_DX_CVNAME([found])], [dnl
82 _DX_CVNAME([found])=no
83 _dx_save_cflags=$CFLAGS
84 _dx_save_libs=$LIBS
85 m4_map_sep([_DX_LIB_TEST], [m4_newline], [$3])
86 CFLAGS=$_dx_save_cflags
87 LIBS=$_dx_save_libs])dnl
88
89 AC_MSG_RESULT([$_DX_CVNAME([found])])
90 AS_IF([test x"$_DX_CVNAME([found])" = x"yes"], [dnl
91         AC_SUBST(m4_toupper([$1_CFLAGS]), [$_DX_CVNAME([cflags])])dnl
92         AC_SUBST(m4_toupper([$1_LIBS]), [$_DX_CVNAME([libs])])])
93 m4_popdef([_DX_CVNAME], [_DX_FNNAME])])
94
95 dnl Internal helper macro for the above.
96 AC_DEFUN([_DX_LIB_TEST],
97         [AS_IF([m4_do([test x"$_DX_CVNAME([found])" != x"yes"],
98                 [m4_ifnblank([$3], [ && $3])])],
99         [_DX_FNNAME([test]) "$1" "$2"])])
100
101 dnl DX_LIB_SEARCH_LINK(env-base, test-program, test-matrix)
102 dnl
103 dnl Convenience wrapper around DX_LIB_SEARCH which implements the test
104 dnl function by attempting to linking the provided test program.
105 AC_DEFUN([DX_LIB_SEARCH_LINK],
106         [DX_LIB_SEARCH([$1], [m4_curry([AC_LINK_IFELSE], [$2])], [$3])])
107
108 dnl DX_LIB_USERFLAG_BLURB(env-base, [human-name])
109 dnl
110 dnl Expand to text explaining the FOO_CFLAGS and FOO_LIBS environment
111 dnl variables, used in error messages, where FOO is replaced with env-base
112 dnl in uppercase.  The human-name is the name of the library, by default the
113 dnl same as env-base.
114 AC_DEFUN([DX_LIB_USERFLAG_BLURB],
115         [_DX_LIB_USERFLAG_BLURB([m4_toupper([$1])], [m4_default([$2], [$1])])])
116
117 dnl Internal helper macro for the above.
118 AC_DEFUN([_DX_LIB_USERFLAG_BLURB], [dnl
119 If $2 is installed but was not detected by this configure script,
120 consider adjusting $1_CFLAGS and/or $1_LIBS as necessary.])
121
122 dnl DX_LIB_USERFLAG_BLURB(env-base, [human-name])
123 dnl
124 dnl Expand to text explaining the PKG_CONFIG_PATH environment variable,
125 dnl used in error messages.  The human-name is the name of the library, by
126 dnl default the same as env-base.
127 AC_DEFUN([DX_LIB_PKGCONFIG_BLURB],
128         [_DX_LIB_PKGCONFIG_BLURB([$1], [m4_default([$2], [$1])])])
129
130 dnl Internal helper macro for the above.
131 AC_DEFUN([_DX_LIB_PKGCONFIG_BLURB], [dnl
132 If pkg-config is installed, it may help to adjust PKG_CONFIG_PATH if
133 $2 is installed in a non-standard prefix.])
134
135 dnl DX_LIB_COMPILE_ERROR([text])
136 dnl
137 dnl Expand to C code which should fail compilation.  The given text should
138 dnl appear in any error message from the compiler, and should not contain
139 dnl any newlines.
140 AC_DEFUN([DX_LIB_COMPILE_ERROR], [m4_do([m4_newline([@%:@error $1])],
141         [m4_newline([static int AS_TR_SH([$1])@<:@-1@:>@;])])])