Add labels for debug menu items.
[familia.git] / m4 / ax_check_gl.m4
1 # ===========================================================================
2 #        http://www.gnu.org/software/autoconf-archive/ax_check_gl.html
3 # ===========================================================================
4 #
5 # SYNOPSIS
6 #
7 #   AX_CHECK_GL
8 #
9 # DESCRIPTION
10 #
11 #   Check for an OpenGL implementation. If GL is found, the required
12 #   compiler and linker flags are included in the output variables
13 #   "GL_CFLAGS", "GL_LIBS", "GL_LDFLAGS", respectively. If no usable GL
14 #   implementation is found, "no_gl" is set to "yes".
15 #
16 #   You could disable OpenGL using --with-gl=no
17 #
18 #   You could choose a specific OpenGL libs using --with-gl=lib_name
19 #
20 #   Under darwin, cygwin and mingw target you could prefer the OpenGL
21 #   implementation that link with X setting --with-gl=x or without X support
22 #   with --with-gl=nox. Notes that this script try to guess the right
23 #   implementation.
24 #
25 #   If the header "GL/gl.h" is found, "HAVE_GL_GL_H" is defined. If the
26 #   header "OpenGL/gl.h" is found, HAVE_OPENGL_GL_H is defined. These
27 #   preprocessor definitions may not be mutually exclusive.
28 #
29 #   You should use something like this in your headers:
30 #
31 #     #if defined(HAVE_WINDOWS_H) && defined(_WIN32)
32 #     # include <windows.h>
33 #     #endif
34 #     #ifdef HAVE_GL_GL_H
35 #     # include <GL/gl.h>
36 #     #elif defined(HAVE_OPENGL_GL_H)
37 #     # include <OpenGL/gl.h>
38 #     #else
39 #     # error no gl.h
40 #     #endif
41 #
42 # LICENSE
43 #
44 #   Copyright (c) 2009 Braden McDaniel <braden@endoframe.com>
45 #   Copyright (c) 2012 Bastien Roucaries <roucaries.bastien+autoconf@gmail.com>
46 #
47 #   This program is free software; you can redistribute it and/or modify it
48 #   under the terms of the GNU General Public License as published by the
49 #   Free Software Foundation; either version 2 of the License, or (at your
50 #   option) any later version.
51 #
52 #   This program is distributed in the hope that it will be useful, but
53 #   WITHOUT ANY WARRANTY; without even the implied warranty of
54 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
55 #   Public License for more details.
56 #
57 #   You should have received a copy of the GNU General Public License along
58 #   with this program. If not, see <http://www.gnu.org/licenses/>.
59 #
60 #   As a special exception, the respective Autoconf Macro's copyright owner
61 #   gives unlimited permission to copy, distribute and modify the configure
62 #   scripts that are the output of Autoconf when processing the Macro. You
63 #   need not follow the terms of the GNU General Public License when using
64 #   or distributing such scripts, even though portions of the text of the
65 #   Macro appear in them. The GNU General Public License (GPL) does govern
66 #   all other use of the material that constitutes the Autoconf Macro.
67 #
68 #   This special exception to the GPL applies to versions of the Autoconf
69 #   Macro released by the Autoconf Archive. When you make and distribute a
70 #   modified version of the Autoconf Macro, you may extend this special
71 #   exception to the GPL to apply to your modified version as well.
72
73 #serial 15
74
75 m4_define([_AX_CHECK_GL_PROGRAM],
76           [AC_LANG_PROGRAM([[
77 # if defined(HAVE_WINDOWS_H) && defined(_WIN32)
78 #   include <windows.h>
79 # endif
80 # ifdef HAVE_GL_GL_H
81 #   include <GL/gl.h>
82 # elif defined(HAVE_OPENGL_GL_H)
83 #   include <OpenGL/gl.h>
84 # else
85 #   error no gl.h
86 # endif
87 ]],[[glBegin(0)]])])
88
89 dnl Default include : add windows.h
90 dnl see http://www.opengl.org/wiki/Platform_specifics:_Windows
91 dnl (acceded 20120801)
92 AC_DEFUN([_AX_CHECK_GL_INCLUDES_DEFAULT],dnl
93 [
94   AC_INCLUDES_DEFAULT
95   [
96   # if defined(HAVE_WINDOWS_H) && defined(_WIN32)
97   #   include <windows.h>
98   # endif
99   ]
100 ])
101
102 dnl local save flags
103 AC_DEFUN([_AX_CHECK_GL_SAVE_FLAGS],
104 [dnl
105 ax_check_gl_saved_libs="${LIBS}"
106 ax_check_gl_saved_cflags="${CFLAGS}"
107 ax_check_gl_saved_cppflags="${CPPFLAGS}"
108 ax_check_gl_saved_ldflags="${LDFLAGS}"
109 ])
110
111 dnl local restore flags
112 AC_DEFUN([_AX_CHECK_GL_RESTORE_FLAGS],
113 [dnl
114 LIBS="${ax_check_gl_saved_libs}"
115 CFLAGS="${ax_check_gl_saved_cflags}"
116 CPPFLAGS="${ax_check_gl_saved_cppflags}"
117 LDFLAGS="${ax_check_gl_saved_ldflags}"
118 ])
119
120 # set the varible ax_check_gl_need_x
121 # this variable determine if we need opengl that link with X
122 # value are default aka try the first library wether if it link or not with x
123 # yes that means we need a opengl with x
124 # no that means we do not need an opengl with x
125 AC_DEFUN([_AX_CHECK_GL_NEED_X],
126 [dnl
127  # do not check if empty : allow a subroutine to modify the choice
128  AS_IF([test "X$ax_check_gl_need_x" = "X"],
129        [ax_check_gl_need_x="default"
130         AS_IF([test "X$no_x" = "Xyes"],[ax_check_gl_need_x="no"])
131         AS_IF([test "X$ax_check_gl_want_gl" = "Xnox"],[ax_check_gl_need_x="no"])
132         AS_IF([test "X$ax_check_gl_want_gl" = "Xx"],[ax_check_gl_need_x="yes"])])
133 ])
134
135 # compile the example program
136 AC_DEFUN([_AX_CHECK_GL_COMPILE],
137 [dnl
138  AC_LANG_PUSH([C])
139  _AX_CHECK_GL_SAVE_FLAGS()
140  CFLAGS="${GL_CFLAGS} ${CFLAGS}"
141  AC_COMPILE_IFELSE([_AX_CHECK_GL_PROGRAM],
142                    [ax_check_gl_compile_opengl="yes"],
143                    [ax_check_gl_compile_opengl="no"])
144  _AX_CHECK_GL_RESTORE_FLAGS()
145  AC_LANG_POP([C])
146 ])
147
148 # compile the example program (cache)
149 AC_DEFUN([_AX_CHECK_GL_COMPILE_CV],
150 [dnl
151  AC_CACHE_CHECK([for compiling a minimal OpenGL program],[ax_cv_check_gl_compile_opengl],
152                 [_AX_CHECK_GL_COMPILE()
153                  ax_cv_check_gl_compile_opengl="${ax_check_gl_compile_opengl}"])
154  ax_check_gl_compile_opengl="${ax_cv_check_gl_compile_opengl}"
155 ])
156
157 # link the example program
158 AC_DEFUN([_AX_CHECK_GL_LINK],
159 [dnl
160  AC_LANG_PUSH([C])
161  _AX_CHECK_GL_SAVE_FLAGS()
162  CFLAGS="${GL_CFLAGS} ${CFLAGS}"
163  LIBS="${GL_LIBS} ${LIBS}"
164  LDFLAGS="${GL_LDFLAGS} ${LDFLAGS}"
165  AC_LINK_IFELSE([_AX_CHECK_GL_PROGRAM],
166                 [ax_check_gl_link_opengl="yes"],
167                 [ax_check_gl_link_opengl="no"])
168  _AX_CHECK_GL_RESTORE_FLAGS()
169  AC_LANG_POP([C])
170 ])
171
172 # link the example program (cache)
173 AC_DEFUN([_AX_CHECK_GL_LINK_CV],
174 [dnl
175  AC_CACHE_CHECK([for linking a minimal OpenGL program],[ax_cv_check_gl_link_opengl],
176                 [_AX_CHECK_GL_LINK()
177                  ax_cv_check_gl_link_opengl="${ax_check_gl_link_opengl}"])
178  ax_check_gl_link_opengl="${ax_cv_check_gl_link_opengl}"
179 ])
180
181 dnl Check headers manually (default case)
182 AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DEFAULT],
183 [AC_REQUIRE([AC_PATH_XTRA])
184  AC_LANG_PUSH([C])
185  _AX_CHECK_GL_SAVE_FLAGS()
186  CFLAGS="${GL_CFLAGS} ${CFLAGS}"
187  # see comment in _AX_CHECK_GL_INCLUDES_DEFAULT
188  AC_CHECK_HEADERS([windows.h],[],[],[AC_INCLUDES_DEFAULT])
189  # FIXME: use extra cflags
190  AC_CHECK_HEADERS([GL/gl.h],[ax_check_gl_have_headers="yes"],
191                             [ax_check_gl_have_headers_headers="no"],
192                             [_AX_CHECK_GL_INCLUDES_DEFAULT()])
193  # do not try darwin specific OpenGl/gl.h
194  _AX_CHECK_GL_RESTORE_FLAGS()
195  AC_LANG_POP([C])
196 ])
197
198 # darwin headers without X
199 AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX],[
200  AC_LANG_PUSH([C])
201  _AX_CHECK_GL_SAVE_FLAGS()
202  # FIXME: use -framework opengl as an extra cflags
203  CFLAGS="-framework opengl ${GL_CFLAGS} ${CFLAGS}"
204  AC_CHECK_HEADERS([OpenGL/gl.h],[ax_check_gl_have_headers="yes"],
205                                 [ax_check_gl_have_headers_headers="no"],
206                                 [_AX_CHECK_GL_INCLUDES_DEFAULT()])
207  AS_IF([test "X$ax_check_gl_have_headers" = "yes"],
208        [GL_CFLAGS="-framework opengl ${GL_CFLAGS}"])
209  _AX_CHECK_GL_SAVE_FLAGS()
210  AC_LANG_POP([C])
211 ])
212
213 # check header for darwin
214 AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DARWIN],
215 [AC_REQUIRE([_AX_CHECK_GL_NEED_X])dnl
216  AS_CASE([$ax_check_gl_need_x],
217          # try to get X libs if possible do not use framework
218          [yes],[_AX_CHECK_GL_MANUAL_HEADERS_DEFAULT()]
219          [no],[_AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX()]
220          # per default use framework that will select if possible no_x version
221          [_AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX()
222           # if not found set that we need x in order to found the good library
223           AS_IF([test "X$ax_check_gl_have_headers" = "yes"],
224                 [ax_check_gl_need_x="no"],
225                 [ax_check_gl_need_x="yes"
226                  # retry with general test
227                  _AX_CHECK_GL_MANUAL_HEADERS_DEFAULT()])
228  ])
229 ])
230
231 dnl Check headers manually: subroutine must set ax_check_gl_have_headers={yes,no}
232 AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS],
233 [AC_REQUIRE([AC_CANONICAL_HOST])
234  AS_CASE([${host}],
235          [*-darwin*],[_AX_CHECK_GL_MANUAL_HEADERS_DARWIN],
236          [_AX_CHECK_GL_MANUAL_HEADERS_DEFAULT()])
237  AC_CACHE_CHECK([for OpenGL headers],[ax_cv_check_gl_have_headers],
238                 [ax_cv_check_gl_have_headers="${ax_check_gl_have_headers}"])
239 ])
240
241 # dnl try to found library (generic case)
242 # dnl $1 is set to the library to found
243 AC_DEFUN([_AX_CHECK_GL_MANUAL_LIBS_GENERIC],
244 [dnl
245  ax_check_gl_manual_libs_generic_extra_libs="$1"
246  AS_IF([test "X$ax_check_gl_manual_libs_generic_extra_libs" = "X"],
247        [AC_MSG_ERROR([AX_CHECK_GL_MANUAL_LIBS_GENERIC argument must no be empty])])
248
249  AC_LANG_PUSH([C])
250  _AX_CHECK_GL_SAVE_FLAGS()
251  CFLAGS="${GL_CFLAGS} ${CFLAGS}"
252  LIBS="${GL_LIBS} ${LIBS}"
253  AC_SEARCH_LIBS([glBegin],[$ax_check_gl_manual_libs_generic_extra_libs],
254                 [ax_check_gl_lib_opengl="yes"],
255                 [ax_check_gl_lib_opengl="no"])
256  AS_CASE([$ac_cv_search_glBegin],
257          ["none required"],[],
258          [no],[],
259          [GL_LIBS="${ac_cv_search_glBegin} ${GL_LIBS}"])
260   _AX_CHECK_GL_RESTORE_FLAGS()
261   AC_LANG_PUSH([C])
262 ])
263
264 # dnl try to found lib under darwin
265 # darwin opengl hack
266 # see http://web.archive.org/web/20090410052741/http://developer.apple.com/qa/qa2007/qa1567.html
267 # and http://web.eecs.umich.edu/~sugih/courses/eecs487/glut-howto/
268 AC_DEFUN([_AX_CHECK_GL_MANUAL_LIBS_DARWIN],
269 [# ldhack list
270  ldhack1 = "-Wl,-framework,OpenGL"
271  ldhack2 = "-Wl,-dylib_file,/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib"
272  ldhack3 = "$ldhack1,$ldhack2"
273
274  # select hack
275  AS_IF([test "X$ax_check_gl_need_x" = "Xyes"],
276        [# libs already set by -framework cflag
277         darwinlibs=""
278         ldhacks="$ldhack1 $ldhack2 $ldhack3"],
279        [# do not use framework ldflags in case of x version
280         darwinlibs="GL gl MesaGL"
281         ldhack="$ldhack2"])
282
283  ax_check_gl_link_opengl="no"
284  for extralibs in " " $darwinlibs; do
285    for extraldflags in " " ldhacks; do
286        AC_LANG_PUSH([C])
287         _AX_CHECK_GL_SAVE_FLAGS()
288        CFLAGS="${GL_CFLAGS} ${CFLAGS}"
289        LIBS="$extralibs ${GL_LIBS} ${LIBS}"
290        LDFLAGS="$extraldflags ${GL_LDFLAGS} ${LDFLAGS}"
291        AC_LINK_IFELSE([_AX_CHECK_GL_PROGRAM],
292                       [ax_check_gl_link_opengl="yes"],
293                       [ax_check_gl_link_opengl="no"])
294        _AX_CHECK_GL_RESTORE_FLAGS()
295        AC_LANG_POP([C])
296        AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],[break])
297    done;
298    AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],[break])
299  done;
300  GL_LIBS="$extralibs ${GL_LIBS}"
301  GL_LDFLAGS="$extraldflags ${GL_LDFLAGS}"
302 ])
303
304 dnl Check library manually: subroutine must set
305 dnl $ax_check_gl_lib_opengl={yes,no}
306 AC_DEFUN([_AX_CHECK_GL_MANUAL_LIBS],
307 [AC_REQUIRE([AC_CANONICAL_HOST])
308  AS_CASE([${host}],
309          [*-darwin*],[_AX_CHECK_GL_MANUAL_LIBS_DARWIN()],
310          # try first cygwin version
311          [*-cygwin*],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([GL gl MesaGL opengl32])],
312          # try first native
313          [*-mingw*],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([opengl32 GL gl MesaGL])],
314          [_AX_CHECK_GL_MANUAL_LIBS_GENERIC([GL gl MesaGL])])
315
316  AC_CACHE_CHECK([for OpenGL libraries],[ax_cv_check_gl_lib_opengl],
317                 [ax_cv_check_gl_lib_opengl="${ax_check_gl_lib_opengl}"])
318  ax_check_gl_lib_opengl="${ax_cv_check_gl_lib_opengl}"
319 ])
320
321 # manually check aka old way
322 AC_DEFUN([_AX_CHECK_GL_MANUAL],
323 [AC_REQUIRE([AC_CANONICAL_HOST])dnl
324  AC_REQUIRE([AC_PATH_XTRA])dnl
325
326  no_gl="yes"
327
328  _AX_CHECK_GL_MANUAL_HEADERS()
329  AS_IF([test "X$ax_check_gl_have_headers" = "Xyes"],
330        [_AX_CHECK_GL_COMPILE_CV()],
331        [ax_check_gl_compile_opengl=no])
332
333  AS_IF([test "X$ax_check_gl_compile_opengl" = "Xyes"],
334        [_AX_CHECK_GL_MANUAL_LIBS],
335        [ax_check_gl_lib_opengl=no])
336
337  AS_IF([test "X$ax_check_gl_lib_opengl" = "Xyes"],
338        [_AX_CHECK_GL_LINK_CV()],
339        [ax_check_gl_link_opengl=no])
340
341  AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],
342        [no_gl="no"],
343        [no_gl="yes"])
344 ])dnl
345
346
347 # try to test using pkgconfig: set ax_check_gl_pkg_config=no if not found
348 AC_DEFUN([_AX_CHECK_GL_PKG_CONFIG],dnl
349 [dnl
350  AC_REQUIRE([PKG_PROG_PKG_CONFIG])
351
352  PKG_CHECK_MODULES([GL],[gl],[ax_check_gl_pkg_config=yes],[ax_check_gl_pkg_config=no])
353  AS_IF([test "X$ax_check_gl_pkg_config" = "Xyes"],[
354         # check headers
355         AC_LANG_PUSH([C])
356         _AX_CHECK_GL_SAVE_FLAGS()
357         CFLAGS="${GL_CFLAGS} ${CFLAGS}"
358         AC_CHECK_HEADERS([windows.h],[],[],[AC_INCLUDES_DEFAULT])
359         AC_CHECK_HEADERS([GL/gl.h OpenGL/gl.h],
360                          [ax_check_gl_have_headers="yes";break],
361                          [ax_check_gl_have_headers_headers="no"],
362                          [_AX_CHECK_GL_INCLUDES_DEFAULT()])
363         _AX_CHECK_GL_RESTORE_FLAGS()
364         AC_LANG_POP([C])
365         AC_CACHE_CHECK([for OpenGL headers],[ax_cv_check_gl_have_headers],
366                        [ax_cv_check_gl_have_headers="${ax_check_gl_have_headers}"])
367
368         # pkgconfig library are suposed to work ...
369         AS_IF([test "X$ax_cv_check_gl_have_headers" = "Xno"],
370               [AC_MSG_ERROR("Pkgconfig detected OpenGL library has no headers!")])
371
372         _AX_CHECK_GL_COMPILE_CV()
373         AS_IF([test "X$ax_cv_check_gl_compile_opengl" = "Xno"],
374               [AC_MSG_ERROR("Pkgconfig detected opengl library could not be used for compiling minimal program!")])
375
376         _AX_CHECK_GL_LINK_CV()
377         AS_IF([test "X$ax_cv_check_gl_link_opengl" = "Xno"],
378               [AC_MSG_ERROR("Pkgconfig detected opengl library could not be used for linking minimal program!")])
379   ])
380 ])
381
382 # test if gl link with X
383 AC_DEFUN([_AX_CHECK_GL_WITH_X],
384 [
385  # try if opengl need X
386  AC_LANG_PUSH([C])
387  _AX_CHECK_GL_SAVE_FLAGS()
388  CFLAGS="${GL_CFLAGS} ${CFLAGS}"
389  LIBS="${GL_LIBS} ${LIBS}"
390  LDFLAGS="${GL_LDFLAGS} ${LDFLAGS}"
391  AC_LINK_IFELSE([AC_LANG_CALL([], [glXQueryVersion])],
392                 [ax_check_gl_link_implicitly_with_x="yes"],
393                 [ax_check_gl_link_implicitly_with_x="no"])
394  _AX_CHECK_GL_RESTORE_FLAGS()
395  AC_LANG_POP([C])
396 ])
397
398 # internal routine: entry point if gl not disable
399 AC_DEFUN([_AX_CHECK_GL],[dnl
400  AC_REQUIRE([PKG_PROG_PKG_CONFIG])
401  AC_REQUIRE([AC_PATH_X])dnl
402
403  # does we need X or not
404  _AX_CHECK_GL_NEED_X()
405
406  # try first pkgconfig
407  AC_MSG_CHECKING([for a working OpenGL implementation by pkg-config])
408  AS_IF([test "X${PKG_CONFIG}" = "X"],
409        [ AC_MSG_RESULT([no])
410          ax_check_gl_pkg_config=no],
411        [ AC_MSG_RESULT([yes])
412          _AX_CHECK_GL_PKG_CONFIG()])
413
414  # if no pkgconfig or pkgconfig fail try manual way
415  AS_IF([test "X$ax_check_gl_pkg_config" = "Xno"],
416        [_AX_CHECK_GL_MANUAL()],
417        [no_gl=no])
418
419
420  # test if need to test X compatibility
421  AS_IF([test $no_gl = no],
422        [# test X compatibility
423         AS_IF([test X$ax_check_gl_need_x != "Xdefault"],
424               [AC_CACHE_CHECK([wether OpenGL link implictly with X],[ax_cv_check_gl_link_with_x],
425                               [_AX_CHECK_GL_WITH_X()
426                                ax_cv_check_gl_link_with_x="${ax_check_gl_link_implicitly_with_x}"])
427                                AS_IF([test "X${ax_cv_check_gl_link_with_x}" = "X${ax_check_gl_need_x}"],
428                                      [no_gl="no"],
429                                      [no_gl=yes])])
430              ])
431 ])
432
433 # ax_check_gl entry point
434 AC_DEFUN([AX_CHECK_GL],
435 [AC_REQUIRE([AC_PATH_X])dnl
436
437  AC_ARG_WITH([gl],
438   [AS_HELP_STRING([--with-gl@<:@=ARG@:>@],
439     [use opengl (ARG=yes),
440      using the specific lib (ARG=<lib>),
441      using the OpenGL lib that link with X (ARG=x),
442      using the OpenGL lib that link without X (ARG=nox),
443      or disable it (ARG=no)
444      @<:@ARG=yes@:>@ ])],
445     [
446     AS_CASE(["$withval"],
447             ["no"|"NO"],[ax_check_gl_want_gl="no"],
448             ["yes"|"YES"],[ax_check_gl_want_gl="yes"],
449             [ax_check_gl_want_gl="$withval"])
450     ],
451     [ax_check_gl_want_gl="yes"])
452
453  # check consistency of parameters
454  AS_IF([test "X$have_x" = "Xdisabled"],
455        [AS_IF([test X$ax_check_gl_want_gl = "Xx"],
456               [AC_MSG_ERROR([You prefer OpenGL with X and asked for no X support])])])
457
458  # set flags
459  no_gl="yes"
460
461  # now do the real testing
462  AS_IF([test X$ax_check_gl_want_gl != "Xno"],
463        [_AX_CHECK_GL()])
464
465  AC_MSG_CHECKING([for a working OpenGL implementation])
466  AS_IF([test "X$no_gl" = "Xno"],
467        [AC_MSG_RESULT([yes])
468         AC_MSG_CHECKING([for CFLAGS needed for OpenGL])
469         AC_MSG_RESULT(["${GL_CFLAGS}"])
470         AC_MSG_CHECKING([for LIBS needed for OpenGL])
471         AC_MSG_RESULT(["${GL_LIBS}"])
472         AC_MSG_CHECKING([for LDFLAGS needed for OpenGL])
473         AC_MSG_RESULT(["${GL_LDFLAGS}"])],
474        [AC_MSG_RESULT([no])
475         GL_CFLAGS=""
476         GL_LIBS=""
477         GL_LDFLAGS=""])
478
479  AC_SUBST([GL_CFLAGS])
480  AC_SUBST([GL_LIBS])
481  AC_SUBST([GL_LDFLAGS])
482 ])