1 # ===========================================================================
2 # http://www.gnu.org/software/autoconf-archive/ax_check_gl.html
3 # ===========================================================================
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".
16 # You could disable OpenGL using --with-gl=no
18 # You could choose a specific OpenGL libs using --with-gl=lib_name
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
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.
29 # You should use something like this in your headers:
31 # #if defined(HAVE_WINDOWS_H) && defined(_WIN32)
32 # # include <windows.h>
36 # #elif defined(HAVE_OPENGL_GL_H)
37 # # include <OpenGL/gl.h>
44 # Copyright (c) 2009 Braden McDaniel <braden@endoframe.com>
45 # Copyright (c) 2012 Bastien Roucaries <roucaries.bastien+autoconf@gmail.com>
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.
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.
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/>.
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.
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.
75 m4_define([_AX_CHECK_GL_PROGRAM],
77 # if defined(HAVE_WINDOWS_H) && defined(_WIN32)
82 # elif defined(HAVE_OPENGL_GL_H)
83 # include <OpenGL/gl.h>
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
96 # if defined(HAVE_WINDOWS_H) && defined(_WIN32)
103 AC_DEFUN([_AX_CHECK_GL_SAVE_FLAGS],
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}"
111 dnl local restore flags
112 AC_DEFUN([_AX_CHECK_GL_RESTORE_FLAGS],
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}"
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],
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"])])
135 # compile the example program
136 AC_DEFUN([_AX_CHECK_GL_COMPILE],
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()
148 # compile the example program (cache)
149 AC_DEFUN([_AX_CHECK_GL_COMPILE_CV],
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}"
157 # link the example program
158 AC_DEFUN([_AX_CHECK_GL_LINK],
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()
172 # link the example program (cache)
173 AC_DEFUN([_AX_CHECK_GL_LINK_CV],
175 AC_CACHE_CHECK([for linking a minimal OpenGL program],[ax_cv_check_gl_link_opengl],
177 ax_cv_check_gl_link_opengl="${ax_check_gl_link_opengl}"])
178 ax_check_gl_link_opengl="${ax_cv_check_gl_link_opengl}"
181 dnl Check headers manually (default case)
182 AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DEFAULT],
183 [AC_REQUIRE([AC_PATH_XTRA])
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()
198 # darwin headers without X
199 AC_DEFUN([_AX_CHECK_GL_MANUAL_HEADERS_DARWIN_NOX],[
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()
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()])
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])
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}"])
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],
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])])
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"],[],
259 [GL_LIBS="${ac_cv_search_glBegin} ${GL_LIBS}"])
260 _AX_CHECK_GL_RESTORE_FLAGS()
264 # dnl try to found lib under darwin
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],
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"
275 AS_IF([test "X$ax_check_gl_need_x" = "Xyes"],
276 [# libs already set by -framework cflag
278 ldhacks="$ldhack1 $ldhack2 $ldhack3"],
279 [# do not use framework ldflags in case of x version
280 darwinlibs="GL gl MesaGL"
283 ax_check_gl_link_opengl="no"
284 for extralibs in " " $darwinlibs; do
285 for extraldflags in " " ldhacks; do
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()
296 AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],[break])
298 AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],[break])
300 GL_LIBS="$extralibs ${GL_LIBS}"
301 GL_LDFLAGS="$extraldflags ${GL_LDFLAGS}"
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])
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])],
313 [*-mingw*],[_AX_CHECK_GL_MANUAL_LIBS_GENERIC([opengl32 GL gl MesaGL])],
314 [_AX_CHECK_GL_MANUAL_LIBS_GENERIC([GL gl MesaGL])])
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}"
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
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])
333 AS_IF([test "X$ax_check_gl_compile_opengl" = "Xyes"],
334 [_AX_CHECK_GL_MANUAL_LIBS],
335 [ax_check_gl_lib_opengl=no])
337 AS_IF([test "X$ax_check_gl_lib_opengl" = "Xyes"],
338 [_AX_CHECK_GL_LINK_CV()],
339 [ax_check_gl_link_opengl=no])
341 AS_IF([test "X$ax_check_gl_link_opengl" = "Xyes"],
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
350 AC_REQUIRE([PKG_PROG_PKG_CONFIG])
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"],[
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()
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}"])
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!")])
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!")])
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!")])
382 # test if gl link with X
383 AC_DEFUN([_AX_CHECK_GL_WITH_X],
385 # try if opengl need X
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()
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
403 # does we need X or not
404 _AX_CHECK_GL_NEED_X()
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()])
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()],
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}"],
433 # ax_check_gl entry point
434 AC_DEFUN([AX_CHECK_GL],
435 [AC_REQUIRE([AC_PATH_X])dnl
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)
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"])
451 [ax_check_gl_want_gl="yes"])
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])])])
461 # now do the real testing
462 AS_IF([test X$ax_check_gl_want_gl != "Xno"],
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}"])],
479 AC_SUBST([GL_CFLAGS])
481 AC_SUBST([GL_LDFLAGS])