1AC_DEFUN([AX_LANG_COMPILER_MS],
2[AC_CACHE_CHECK([whether we are using the Microsoft _AC_LANG compiler],
3                [ax_cv_[]_AC_LANG_ABBREV[]_compiler_ms],
4[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [[#ifndef _MSC_VER
5       choke me
6#endif
7]])],
8                   [ax_compiler_ms=yes],
9                   [ax_compiler_ms=no])
10ax_cv_[]_AC_LANG_ABBREV[]_compiler_ms=$ax_compiler_ms
11])])
12
13AC_DEFUN([ACX_PTHREAD], [
14AC_REQUIRE([AC_CANONICAL_HOST])
15AC_LANG_SAVE
16AC_LANG_C
17acx_pthread_ok=no
18
19# We used to check for pthread.h first, but this fails if pthread.h
20# requires special compiler flags (e.g. on True64 or Sequent).
21# It gets checked for in the link test anyway.
22
23# First of all, check if the user has set any of the PTHREAD_LIBS,
24# etcetera environment variables, and if threads linking works using
25# them:
26if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
27        save_CFLAGS="$CFLAGS"
28        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
29        save_LIBS="$LIBS"
30        LIBS="$PTHREAD_LIBS $LIBS"
31        AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
32        AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
33        AC_MSG_RESULT($acx_pthread_ok)
34        if test x"$acx_pthread_ok" = xno; then
35                PTHREAD_LIBS=""
36                PTHREAD_CFLAGS=""
37        fi
38        LIBS="$save_LIBS"
39        CFLAGS="$save_CFLAGS"
40fi
41
42# We must check for the threads library under a number of different
43# names; the ordering is very important because some systems
44# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
45# libraries is broken (non-POSIX).
46
47# Create a list of thread flags to try.  Items starting with a "-" are
48# C compiler flags, and other items are library names, except for "none"
49# which indicates that we try without any flags at all, and "pthread-config"
50# which is a program returning the flags for the Pth emulation library.
51
52acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
53
54# The ordering *is* (sometimes) important.  Some notes on the
55# individual items follow:
56
57# pthreads: AIX (must check this before -lpthread)
58# none: in case threads are in libc; should be tried before -Kthread and
59#       other compiler flags to prevent continual compiler warnings
60# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
61# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
62# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
63# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
64# -pthreads: Solaris/gcc
65# -mthreads: Mingw32/gcc, Lynx/gcc
66# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
67#      doesn't hurt to check since this sometimes defines pthreads too;
68#      also defines -D_REENTRANT)
69#      ... -mt is also the pthreads flag for HP/aCC
70# pthread: Linux, etcetera
71# --thread-safe: KAI C++
72# pthread-config: use pthread-config program (for GNU Pth library)
73
74case "${host_cpu}-${host_os}" in
75        *solaris*)
76
77        # On Solaris (at least, for some versions), libc contains stubbed
78        # (non-functional) versions of the pthreads routines, so link-based
79        # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
80        # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
81        # a function called by this macro, so we could check for that, but
82        # who knows whether they'll stub that too in a future libc.)  So,
83        # we'll just look for -pthreads and -lpthread first:
84
85        acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
86        ;;
87esac
88
89if test x"$acx_pthread_ok" = xno; then
90for flag in $acx_pthread_flags; do
91
92        case $flag in
93                none)
94                AC_MSG_CHECKING([whether pthreads work without any flags])
95                ;;
96
97                -*)
98                AC_MSG_CHECKING([whether pthreads work with $flag])
99                PTHREAD_CFLAGS="$flag"
100                ;;
101
102                pthread-config)
103                AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
104                if test x"$acx_pthread_config" = xno; then continue; fi
105                PTHREAD_CFLAGS="`pthread-config --cflags`"
106                PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
107                ;;
108
109                *)
110                AC_MSG_CHECKING([for the pthreads library -l$flag])
111                PTHREAD_LIBS="-l$flag"
112                ;;
113        esac
114
115        save_LIBS="$LIBS"
116        save_CFLAGS="$CFLAGS"
117        LIBS="$PTHREAD_LIBS $LIBS"
118        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
119
120        # Check for various functions.  We must include pthread.h,
121        # since some functions may be macros.  (On the Sequent, we
122        # need a special flag -Kthread to make this header compile.)
123        # We check for pthread_join because it is in -lpthread on IRIX
124        # while pthread_create is in libc.  We check for pthread_attr_init
125        # due to DEC craziness with -lpthreads.  We check for
126        # pthread_cleanup_push because it is one of the few pthread
127        # functions on Solaris that doesn't have a non-functional libc stub.
128        # We try pthread_create on general principles.
129        AC_TRY_LINK([#include <pthread.h>],
130                    [pthread_t th; pthread_join(th, 0);
131                     pthread_attr_init(0); pthread_cleanup_push(0, 0);
132                     pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
133                    [acx_pthread_ok=yes])
134
135        LIBS="$save_LIBS"
136        CFLAGS="$save_CFLAGS"
137
138        AC_MSG_RESULT($acx_pthread_ok)
139        if test "x$acx_pthread_ok" = xyes; then
140                break;
141        fi
142
143        PTHREAD_LIBS=""
144        PTHREAD_CFLAGS=""
145done
146fi
147
148# Various other checks:
149if test "x$acx_pthread_ok" = xyes; then
150        save_LIBS="$LIBS"
151        LIBS="$PTHREAD_LIBS $LIBS"
152        save_CFLAGS="$CFLAGS"
153        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
154
155        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
156        AC_MSG_CHECKING([for joinable pthread attribute])
157        attr_name=unknown
158        for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
159            AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
160                        [attr_name=$attr; break])
161        done
162        AC_MSG_RESULT($attr_name)
163        if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
164            AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
165                               [Define to necessary symbol if this constant
166                                uses a non-standard name on your system.])
167        fi
168
169        AC_MSG_CHECKING([if more special flags are required for pthreads])
170        flag=no
171        case "${host_cpu}-${host_os}" in
172            *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
173            *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
174        esac
175        AC_MSG_RESULT(${flag})
176        if test "x$flag" != xno; then
177            PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
178        fi
179
180        LIBS="$save_LIBS"
181        CFLAGS="$save_CFLAGS"
182
183        # More AIX lossage: must compile with cc_r
184        AC_CHECK_PROG(PTHREAD_CC, cc_r, cc_r, ${CC})
185else
186        PTHREAD_CC="$CC"
187fi
188
189AC_SUBST(PTHREAD_LIBS)
190AC_SUBST(PTHREAD_CFLAGS)
191AC_SUBST(PTHREAD_CC)
192
193# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
194if test x"$acx_pthread_ok" = xyes; then
195        ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
196        :
197else
198        acx_pthread_ok=no
199        $2
200fi
201AC_LANG_RESTORE
202])dnl ACX_PTHREAD
203
204AC_DEFUN([AX_CHECK_GL],
205[AC_REQUIRE([AC_PATH_X])dnl
206AC_REQUIRE([ACX_PTHREAD])dnl
207
208#
209# There isn't a reliable way to know we should use the Apple OpenGL framework
210# without a configure option.  A Mac OS X user may have installed an
211# alternative GL implementation (e.g., Mesa), which may or may not depend on X.
212#
213AC_ARG_WITH([apple-opengl-framework],
214            [AC_HELP_STRING([--with-apple-opengl-framework],
215                            [use Apple OpenGL framework (Mac OS X only)])])
216if test "X$with_apple_opengl_framework" = "Xyes"; then
217  AC_DEFINE([HAVE_APPLE_OPENGL_FRAMEWORK], [1],
218            [Use the Apple OpenGL framework.])
219  GL_LIBS="-framework OpenGL"
220else
221  AC_LANG_PUSH(C)
222
223  AX_LANG_COMPILER_MS
224  if test X$ax_compiler_ms = Xno; then
225    GL_CFLAGS="${PTHREAD_CFLAGS}"
226    GL_LIBS="${PTHREAD_LIBS} -lm"
227  fi
228
229  #
230  # Use x_includes and x_libraries if they have been set (presumably by
231  # AC_PATH_X).
232  #
233  if test "X$no_x" != "Xyes"; then
234    if test -n "$x_includes"; then
235      GL_CFLAGS="-I${x_includes} ${GL_CFLAGS}"
236    fi
237    if test -n "$x_libraries"; then
238      GL_LIBS="-L${x_libraries} -lX11 ${GL_LIBS}"
239    fi
240  fi
241
242  AC_CHECK_HEADERS([windows.h])
243
244  AC_CACHE_CHECK([for OpenGL library], [ax_cv_check_gl_libgl],
245  [ax_cv_check_gl_libgl="no"
246  ax_save_CPPFLAGS="${CPPFLAGS}"
247  CPPFLAGS="${GL_CFLAGS} ${CPPFLAGS}"
248  ax_save_LIBS="${LIBS}"
249  LIBS=""
250  ax_check_libs="-lopengl32 -lGL"
251  for ax_lib in ${ax_check_libs}; do
252    if test X$ax_compiler_ms = Xyes; then
253      ax_try_lib=`echo $ax_lib | sed -e 's/^-l//' -e 's/$/.lib/'`
254    else
255      ax_try_lib="${ax_lib}"
256    fi
257    LIBS="${ax_try_lib} ${GL_LIBS} ${ax_save_LIBS}"
258    AC_LINK_IFELSE(
259    [AC_LANG_PROGRAM([[
260# if HAVE_WINDOWS_H && defined(_WIN32)
261#   include <windows.h>
262# endif
263# include <GL/gl.h>]],
264                     [[glBegin(0)]])],
265    [ax_cv_check_gl_libgl="${ax_try_lib}"; break])
266  done
267  LIBS=${ax_save_LIBS}
268  CPPFLAGS=${ax_save_CPPFLAGS}])
269
270  if test "X${ax_cv_check_gl_libgl}" = "Xno"; then
271    no_gl="yes"
272    GL_CFLAGS=""
273    GL_LIBS=""
274  else
275    GL_LIBS="${ax_cv_check_gl_libgl} ${GL_LIBS}"
276  fi
277  AC_LANG_POP(C)
278fi
279
280AC_SUBST([GL_CFLAGS])
281AC_SUBST([GL_LIBS])
282])dnl
283