1# -*- mode: autoconf -*-
2#
3# AX_CHECK_GL
4#
5# Check for an OpenGL implementation.  If GL is found, the required compiler
6# and linker flags are included in the output variables "GL_CFLAGS" and
7# "GL_LIBS", respectively.  If no usable GL implementation is found, "no_gl"
8# is set to "yes".
9#
10# If the header "GL/gl.h" is found, "HAVE_GL_GL_H" is defined.  If the header
11# "OpenGL/gl.h" is found, HAVE_OPENGL_GL_H is defined.  These preprocessor
12# definitions may not be mutually exclusive.
13#
14# version: 2.7
15# author: Braden McDaniel <braden@endoframe.com>
16#
17# This program is free software; you can redistribute it and/or modify
18# it under the terms of the GNU General Public License as published by
19# the Free Software Foundation; either version 2, or (at your option)
20# any later version.
21#
22# This program is distributed in the hope that it will be useful,
23# but WITHOUT ANY WARRANTY; without even the implied warranty of
24# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25# GNU General Public License for more details.
26#
27# You should have received a copy of the GNU General Public License
28# along with this program; if not, write to the Free Software
29# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30# 02110-1301, USA.
31#
32# As a special exception, the you may copy, distribute and modify the
33# configure scripts that are the output of Autoconf when processing
34# the Macro.  You need not follow the terms of the GNU General Public
35# License when using or distributing such scripts.
36#
37AC_DEFUN([AX_CHECK_GL],
38[AC_REQUIRE([AC_CANONICAL_HOST])dnl
39AC_REQUIRE([AC_PATH_X])dnl
40AC_REQUIRE([AC_PROG_SED])dnl
41
42AC_LANG_PUSH([C])
43AX_LANG_COMPILER_MS
44
45#
46# Use x_includes and x_libraries if they have been set (presumably by
47# AC_PATH_X).
48#
49AS_IF([test X$no_x != Xyes -a -n "$x_includes"],
50      [GL_CFLAGS="-I$x_includes $GL_CFLAGS"])
51
52AC_CHECK_HEADERS([windows.h])
53
54ax_save_CPPFLAGS=$CPPFLAGS
55CPPFLAGS="$GL_CFLAGS $CPPFLAGS"
56AC_CHECK_HEADERS([GL/gl.h OpenGL/gl.h], , , [
57# if defined(HAVE_WINDOWS_H) && defined(_WIN32)
58#   include <windows.h>
59# endif
60])
61CPPFLAGS=$ax_save_CPPFLAGS
62
63m4_define([AX_CHECK_GL_PROGRAM],
64          [AC_LANG_PROGRAM([[
65# if defined(HAVE_WINDOWS_H) && defined(_WIN32)
66#   include <windows.h>
67# endif
68# ifdef HAVE_GL_GL_H
69#   include <GL/gl.h>
70# elif defined(HAVE_OPENGL_GL_H)
71#   include <OpenGL/gl.h>
72# else
73#   error no gl.h
74# endif]],
75                           [[glBegin(0)]])])
76
77m4_define([AX_CHECK_GL_GLX_PROGRAM],
78          [AC_LANG_PROGRAM([[
79# if defined(HAVE_WINDOWS_H) && defined(_WIN32)
80#   include <windows.h>
81# endif
82# ifdef HAVE_GL_GL_H
83#   include <GL/gl.h>
84# elif defined(HAVE_OPENGL_GL_H)
85#   include <OpenGL/gl.h>
86# else
87#   error no gl.h
88# endif]],
89                           [[glXQueryVersion(0, 0, 0)]])])
90
91AC_CACHE_CHECK([for OpenGL library], [ax_cv_check_gl_libgl],
92[ax_cv_check_gl_libgl=no
93case $host_cpu in
94  x86_64) ax_check_gl_libdir=lib64 ;;
95  *)      ax_check_gl_libdir=lib ;;
96esac
97ax_save_CPPFLAGS=$CPPFLAGS
98CPPFLAGS="$CPPFLAGS $GL_CFLAGS"
99ax_save_LDFLAGS=$LDFLAGS
100AS_IF([test X$no_x != Xyes -a -n "$x_libraries"],
101      [LDFLAGS="$LDFLAGS -L$x_libraries"])
102ax_save_LIBS=$LIBS
103ax_check_libs="-lopengl32 -lGL"
104for ax_lib in $ax_check_libs; do
105  AS_IF([test X$ax_compiler_ms = Xyes],
106        [ax_try_lib=`echo $ax_lib | $SED -e 's/^-l//' -e 's/$/.lib/'`],
107        [ax_try_lib=$ax_lib])
108  LDFLAGS="$ax_save_LDFLAGS $GL_LIBS"
109  LIBS="$ax_try_lib $ax_save_LIBS"
110AC_LINK_IFELSE([AX_CHECK_GL_PROGRAM],
111               [ax_cv_check_gl_libgl=$ax_try_lib; break],
112               [ax_check_gl_dylib_flag='-dylib_file /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib:/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib'
113                LDFLAGS="$ax_save_LDFLAGS $GL_LIBS $ax_check_gl_dylib_flag"
114                AC_LINK_IFELSE([AX_CHECK_GL_PROGRAM],
115                               [ax_cv_check_gl_libgl="$ax_check_gl_dylib_flag $ax_try_lib"; break])])
116done
117
118#
119# If no_x is "yes", we don't want to wind up using a libGL that is
120# linked with X11.  Test to see if the found libGL includes GLX
121# functions.  If it does and no_x is "yes", we want to reset
122# ax_cv_check_gl_libgl back to "no".
123#
124# Note that LDFLAGS and LIBS should still have whatever values they
125# had when we broke out of the test loop above; use that.
126#
127AS_IF([test "X$ax_cv_check_gl_libgl" != Xno],
128      [AC_LINK_IFELSE([AX_CHECK_GL_GLX_PROGRAM],
129                      [AS_IF([test X$no_x = Xyes],
130                             [ax_cv_check_gl_libgl=no])])])
131
132LIBS=$ax_save_LIBS
133AS_IF([test "X$ax_cv_check_gl_libgl" = Xno],
134      [LDFLAGS="$ax_save_LDFLAGS -framework OpenGL"
135      AC_LINK_IFELSE([AX_CHECK_GL_PROGRAM],
136                     [ax_cv_check_gl_libgl='-framework OpenGL'])])
137
138LDFLAGS=$ax_save_LDFLAGS
139CPPFLAGS=$ax_save_CPPFLAGS])
140
141AS_IF([test "X$ax_cv_check_gl_libgl" = Xno],
142      [no_gl=yes; GL_CFLAGS=""; GL_LIBS=""],
143      [GL_LIBS="$ax_cv_check_gl_libgl $GL_LIBS"])
144AC_LANG_POP([C])
145
146AC_SUBST([GL_CFLAGS])
147AC_SUBST([GL_LIBS])
148])dnl
149