New repo for repo.or.cz
[The-Artvertiser.git] / acinclude.m4
blobe28a085dfdf56ed2b8805ff7fb9a9dd23f63bb94
1 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
2 dnl
3 dnl @summary figure out how to build C programs using POSIX threads
4 dnl
5 dnl This macro figures out how to build C programs using POSIX threads.
6 dnl It sets the PTHREAD_LIBS output variable to the threads library and
7 dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
8 dnl C compiler flags that are needed. (The user can also force certain
9 dnl compiler flags/libs to be tested by setting these environment
10 dnl variables.)
11 dnl
12 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
13 dnl multi-threaded programs (defaults to the value of CC otherwise).
14 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
15 dnl
16 dnl NOTE: You are assumed to not only compile your program with these
17 dnl flags, but also link it with them as well. e.g. you should link
18 dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
19 dnl $LIBS
20 dnl
21 dnl If you are only building threads programs, you may wish to use
22 dnl these variables in your default LIBS, CFLAGS, and CC:
23 dnl
24 dnl        LIBS="$PTHREAD_LIBS $LIBS"
25 dnl        CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
26 dnl        CC="$PTHREAD_CC"
27 dnl
28 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
29 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
30 dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
31 dnl
32 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
33 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
34 dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
35 dnl default action will define HAVE_PTHREAD.
36 dnl
37 dnl Please let the authors know if this macro fails on any platform, or
38 dnl if you have any other suggestions or comments. This macro was based
39 dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
40 dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
41 dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
42 dnl We are also grateful for the helpful feedback of numerous users.
43 dnl
44 dnl @category InstalledPackages
45 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
46 dnl @version 2006-05-29
47 dnl @license GPLWithACException
49 AC_DEFUN([ACX_PTHREAD], [
50 AC_REQUIRE([AC_CANONICAL_HOST])
51 AC_LANG_SAVE
52 AC_LANG_C
53 acx_pthread_ok=no
55 # We used to check for pthread.h first, but this fails if pthread.h
56 # requires special compiler flags (e.g. on True64 or Sequent).
57 # It gets checked for in the link test anyway.
59 # First of all, check if the user has set any of the PTHREAD_LIBS,
60 # etcetera environment variables, and if threads linking works using
61 # them:
62 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
63         save_CFLAGS="$CFLAGS"
64         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
65         save_LIBS="$LIBS"
66         LIBS="$PTHREAD_LIBS $LIBS"
67         AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
68         AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
69         AC_MSG_RESULT($acx_pthread_ok)
70         if test x"$acx_pthread_ok" = xno; then
71                 PTHREAD_LIBS=""
72                 PTHREAD_CFLAGS=""
73         fi
74         LIBS="$save_LIBS"
75         CFLAGS="$save_CFLAGS"
78 # We must check for the threads library under a number of different
79 # names; the ordering is very important because some systems
80 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
81 # libraries is broken (non-POSIX).
83 # Create a list of thread flags to try.  Items starting with a "-" are
84 # C compiler flags, and other items are library names, except for "none"
85 # which indicates that we try without any flags at all, and "pthread-config"
86 # which is a program returning the flags for the Pth emulation library.
88 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
90 # The ordering *is* (sometimes) important.  Some notes on the
91 # individual items follow:
93 # pthreads: AIX (must check this before -lpthread)
94 # none: in case threads are in libc; should be tried before -Kthread and
95 #       other compiler flags to prevent continual compiler warnings
96 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
97 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
98 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
99 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
100 # -pthreads: Solaris/gcc
101 # -mthreads: Mingw32/gcc, Lynx/gcc
102 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
103 #      doesn't hurt to check since this sometimes defines pthreads too;
104 #      also defines -D_REENTRANT)
105 #      ... -mt is also the pthreads flag for HP/aCC
106 # pthread: Linux, etcetera
107 # --thread-safe: KAI C++
108 # pthread-config: use pthread-config program (for GNU Pth library)
110 case "${host_cpu}-${host_os}" in
111         *solaris*)
113         # On Solaris (at least, for some versions), libc contains stubbed
114         # (non-functional) versions of the pthreads routines, so link-based
115         # tests will erroneously succeed.  (We need to link with -pthreads/-mt/
116         # -lpthread.)  (The stubs are missing pthread_cleanup_push, or rather
117         # a function called by this macro, so we could check for that, but
118         # who knows whether they'll stub that too in a future libc.)  So,
119         # we'll just look for -pthreads and -lpthread first:
121         acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
122         ;;
123 esac
125 if test x"$acx_pthread_ok" = xno; then
126 for flag in $acx_pthread_flags; do
128         case $flag in
129                 none)
130                 AC_MSG_CHECKING([whether pthreads work without any flags])
131                 ;;
133                 -*)
134                 AC_MSG_CHECKING([whether pthreads work with $flag])
135                 PTHREAD_CFLAGS="$flag"
136                 ;;
138                 pthread-config)
139                 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
140                 if test x"$acx_pthread_config" = xno; then continue; fi
141                 PTHREAD_CFLAGS="`pthread-config --cflags`"
142                 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
143                 ;;
145                 *)
146                 AC_MSG_CHECKING([for the pthreads library -l$flag])
147                 PTHREAD_LIBS="-l$flag"
148                 ;;
149         esac
151         save_LIBS="$LIBS"
152         save_CFLAGS="$CFLAGS"
153         LIBS="$PTHREAD_LIBS $LIBS"
154         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
156         # Check for various functions.  We must include pthread.h,
157         # since some functions may be macros.  (On the Sequent, we
158         # need a special flag -Kthread to make this header compile.)
159         # We check for pthread_join because it is in -lpthread on IRIX
160         # while pthread_create is in libc.  We check for pthread_attr_init
161         # due to DEC craziness with -lpthreads.  We check for
162         # pthread_cleanup_push because it is one of the few pthread
163         # functions on Solaris that doesn't have a non-functional libc stub.
164         # We try pthread_create on general principles.
165         AC_TRY_LINK([#include <pthread.h>],
166                     [pthread_t th; pthread_join(th, 0);
167                      pthread_attr_init(0); pthread_cleanup_push(0, 0);
168                      pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
169                     [acx_pthread_ok=yes])
171         LIBS="$save_LIBS"
172         CFLAGS="$save_CFLAGS"
174         AC_MSG_RESULT($acx_pthread_ok)
175         if test "x$acx_pthread_ok" = xyes; then
176                 break;
177         fi
179         PTHREAD_LIBS=""
180         PTHREAD_CFLAGS=""
181 done
184 # Various other checks:
185 if test "x$acx_pthread_ok" = xyes; then
186         save_LIBS="$LIBS"
187         LIBS="$PTHREAD_LIBS $LIBS"
188         save_CFLAGS="$CFLAGS"
189         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
191         # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
192         AC_MSG_CHECKING([for joinable pthread attribute])
193         attr_name=unknown
194         for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
195             AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
196                         [attr_name=$attr; break])
197         done
198         AC_MSG_RESULT($attr_name)
199         if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
200             AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
201                                [Define to necessary symbol if this constant
202                                 uses a non-standard name on your system.])
203         fi
205         AC_MSG_CHECKING([if more special flags are required for pthreads])
206         flag=no
207         case "${host_cpu}-${host_os}" in
208             *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
209             *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
210         esac
211         AC_MSG_RESULT(${flag})
212         if test "x$flag" != xno; then
213             PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
214         fi
216         LIBS="$save_LIBS"
217         CFLAGS="$save_CFLAGS"
219         # More AIX lossage: must compile with xlc_r or cc_r
220         if test x"$GCC" != xyes; then
221           AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
222         else
223           PTHREAD_CC=$CC
224         fi
225 else
226         PTHREAD_CC="$CC"
229 AC_SUBST(PTHREAD_LIBS)
230 AC_SUBST(PTHREAD_CFLAGS)
231 AC_SUBST(PTHREAD_CC)
233 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
234 if test x"$acx_pthread_ok" = xyes; then
235         ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
236         :
237 else
238         acx_pthread_ok=no
239         $2
241 AC_LANG_RESTORE
242 ])dnl ACX_PTHREAD
243 dnl @synopsis AX_CHECK_GL
245 dnl Check for an OpenGL implementation. If GL is found, the required
246 dnl compiler and linker flags are included in the output variables
247 dnl "GL_CFLAGS" and "GL_LIBS", respectively. This macro adds the
248 dnl configure option "--with-apple-opengl-framework", which users can
249 dnl use to indicate that Apple's OpenGL framework should be used on Mac
250 dnl OS X. If Apple's OpenGL framework is used, the symbol
251 dnl "HAVE_APPLE_OPENGL_FRAMEWORK" is defined. If no GL implementation
252 dnl is found, "no_gl" is set to "yes".
254 dnl @category InstalledPackages
255 dnl @author Braden McDaniel <braden@endoframe.com>
256 dnl @version 2004-11-15
257 dnl @license AllPermissive
259 AC_DEFUN([AX_CHECK_GL],
260 [AC_REQUIRE([AC_PATH_X])dnl
261 AC_REQUIRE([ACX_PTHREAD])dnl
264 # There isn't a reliable way to know we should use the Apple OpenGL framework
265 # without a configure option.  A Mac OS X user may have installed an
266 # alternative GL implementation (e.g., Mesa), which may or may not depend on X.
268 AC_ARG_WITH([apple-opengl-framework],
269             [AC_HELP_STRING([--with-apple-opengl-framework],
270                             [use Apple OpenGL framework (Mac OS X only)])])
271 if test "X$with_apple_opengl_framework" = "Xyes"; then
272   AC_DEFINE([HAVE_APPLE_OPENGL_FRAMEWORK], [1],
273             [Use the Apple OpenGL framework.])
274   GL_LIBS="-framework OpenGL"
275 else
276   AC_LANG_PUSH(C)
278   AX_LANG_COMPILER_MS
279   if test X$ax_compiler_ms = Xno; then
280     GL_CFLAGS="${PTHREAD_CFLAGS}"
281     GL_LIBS="${PTHREAD_LIBS} -lm"
282   fi
284   #
285   # Use x_includes and x_libraries if they have been set (presumably by
286   # AC_PATH_X).
287   #
288   if test "X$no_x" != "Xyes"; then
289     if test -n "$x_includes"; then
290       GL_CFLAGS="-I${x_includes} ${GL_CFLAGS}"
291     fi
292     if test -n "$x_libraries"; then
293       GL_LIBS="-L${x_libraries} -lX11 ${GL_LIBS}"
294     fi
295   fi
297   AC_CHECK_HEADERS([windows.h])
299   AC_CACHE_CHECK([for OpenGL library], [ax_cv_check_gl_libgl],
300   [ax_cv_check_gl_libgl="no"
301   ax_save_CPPFLAGS="${CPPFLAGS}"
302   CPPFLAGS="${GL_CFLAGS} ${CPPFLAGS}"
303   ax_save_LIBS="${LIBS}"
304   LIBS=""
305   ax_check_libs="-lopengl32 -lGL"
306   for ax_lib in ${ax_check_libs}; do
307     if test X$ax_compiler_ms = Xyes; then
308       ax_try_lib=`echo $ax_lib | sed -e 's/^-l//' -e 's/$/.lib/'`
309     else
310       ax_try_lib="${ax_lib}"
311     fi
312     LIBS="${ax_try_lib} ${GL_LIBS} ${ax_save_LIBS}"
313     AC_LINK_IFELSE(
314     [AC_LANG_PROGRAM([[
315 # if HAVE_WINDOWS_H && defined(_WIN32)
316 #   include <windows.h>
317 # endif
318 # include <GL/gl.h>]],
319                      [[glBegin(0)]])],
320     [ax_cv_check_gl_libgl="${ax_try_lib}"; break])
321   done
322   LIBS=${ax_save_LIBS}
323   CPPFLAGS=${ax_save_CPPFLAGS}])
325   if test "X${ax_cv_check_gl_libgl}" = "Xno"; then
326     no_gl="yes"
327     GL_CFLAGS=""
328     GL_LIBS=""
329   else
330     GL_LIBS="${ax_cv_check_gl_libgl} ${GL_LIBS}"
331   fi
332   AC_LANG_POP(C)
335 AC_SUBST([GL_CFLAGS])
336 AC_SUBST([GL_LIBS])
337 ])dnl
338 dnl @synopsis AX_CHECK_GLU
340 dnl Check for GLU. If GLU is found, the required preprocessor and
341 dnl linker flags are included in the output variables "GLU_CFLAGS" and
342 dnl "GLU_LIBS", respectively. This macro adds the configure option
343 dnl "--with-apple-opengl-framework", which users can use to indicate
344 dnl that Apple's OpenGL framework should be used on Mac OS X. If
345 dnl Apple's OpenGL framework is used, the symbol
346 dnl "HAVE_APPLE_OPENGL_FRAMEWORK" is defined. If no GLU implementation
347 dnl is found, "no_glu" is set to "yes".
349 dnl @category InstalledPackages
350 dnl @author Braden McDaniel <braden@endoframe.com>
351 dnl @version 2004-11-15
352 dnl @license AllPermissive
354 AC_DEFUN([AX_CHECK_GLU],
355 [AC_REQUIRE([AX_CHECK_GL])dnl
356 AC_REQUIRE([AC_PROG_CXX])dnl
357 GLU_CFLAGS="${GL_CFLAGS}"
358 if test "X${with_apple_opengl_framework}" != "Xyes"; then
359   AC_CACHE_CHECK([for OpenGL Utility library], [ax_cv_check_glu_libglu],
360   [ax_cv_check_glu_libglu="no"
361   ax_save_CPPFLAGS="${CPPFLAGS}"
362   CPPFLAGS="${GL_CFLAGS} ${CPPFLAGS}"
363   ax_save_LIBS="${LIBS}"
364   LIBS=""
365   ax_check_libs="-lglu32 -lGLU"
366   for ax_lib in ${ax_check_libs}; do
367     if test X$ax_compiler_ms = Xyes; then
368       ax_try_lib=`echo $ax_lib | sed -e 's/^-l//' -e 's/$/.lib/'`
369     else
370       ax_try_lib="${ax_lib}"
371     fi
372     LIBS="${ax_try_lib} ${GL_LIBS} ${ax_save_LIBS}"
373     #
374     # libGLU typically links with libstdc++ on POSIX platforms. However,
375     # setting the language to C++ means that test program source is named
376     # "conftest.cc"; and Microsoft cl doesn't know what to do with such a
377     # file.
378     #
379     AC_LANG_PUSH([C++])
380     if test X$ax_compiler_ms = Xyes; then
381       AC_LANG_PUSH([C])
382     fi
383     AC_LINK_IFELSE(
384     [AC_LANG_PROGRAM([[
385 # if HAVE_WINDOWS_H && defined(_WIN32)
386 #   include <windows.h>
387 # endif
388 # include <GL/glu.h>]],
389                      [[gluBeginCurve(0)]])],
390     [ax_cv_check_glu_libglu="${ax_try_lib}"; break])
391     if test X$ax_compiler_ms = Xyes; then
392       AC_LANG_POP([C])
393     fi
394     AC_LANG_POP([C++])
395   done
396   LIBS=${ax_save_LIBS}
397   CPPFLAGS=${ax_save_CPPFLAGS}])
398   if test "X${ax_cv_check_glu_libglu}" = "Xno"; then
399     no_glu="yes"
400     GLU_CFLAGS=""
401     GLU_LIBS=""
402   else
403     GLU_LIBS="${ax_cv_check_glu_libglu} ${GL_LIBS}"
404   fi
406 AC_SUBST([GLU_CFLAGS])
407 AC_SUBST([GLU_LIBS])
409 dnl @synopsis AX_CHECK_GLUT
411 dnl Check for GLUT. If GLUT is found, the required compiler and linker
412 dnl flags are included in the output variables "GLUT_CFLAGS" and
413 dnl "GLUT_LIBS", respectively. This macro adds the configure option
414 dnl "--with-apple-opengl-framework", which users can use to indicate
415 dnl that Apple's OpenGL framework should be used on Mac OS X. If
416 dnl Apple's OpenGL framework is used, the symbol
417 dnl "HAVE_APPLE_OPENGL_FRAMEWORK" is defined. If GLUT is not found,
418 dnl "no_glut" is set to "yes".
420 dnl @category InstalledPackages
421 dnl @author Braden McDaniel <braden@endoframe.com>
422 dnl @version 2004-11-15
423 dnl @license AllPermissive
425 AC_DEFUN([AX_CHECK_GLUT],
426 [AC_REQUIRE([AX_CHECK_GLU])dnl
427 AC_REQUIRE([AC_PATH_XTRA])dnl
429 if test "X$with_apple_opengl_framework" = "Xyes"; then
430   GLUT_CFLAGS="${GLU_CFLAGS}"
431   GLUT_LIBS="-framework GLUT -lobjc ${GL_LIBS}"
432 else
433   GLUT_CFLAGS=${GLU_CFLAGS}
434   GLUT_LIBS=${GLU_LIBS}
436   #
437   # If X is present, assume GLUT depends on it.
438   #
439   if test "X${no_x}" != "Xyes"; then
440     GLUT_LIBS="${X_PRE_LIBS} -lXmu -lXi ${X_EXTRA_LIBS} ${GLUT_LIBS}"
441   fi
443   AC_LANG_PUSH(C)
445   ax_save_CPPFLAGS="${CPPFLAGS}"
446   CPPFLAGS="${GLUT_CFLAGS} ${CPPFLAGS}"
448   AC_CACHE_CHECK([for GLUT library], [ax_cv_check_glut_libglut],
449   [ax_cv_check_glut_libglut="no"
450   ax_save_LIBS="${LIBS}"
451   LIBS=""
452   ax_check_libs="-lglut32 -lglut"
453   for ax_lib in ${ax_check_libs}; do
454     if test X$ax_compiler_ms = Xyes; then
455       ax_try_lib=`echo $ax_lib | sed -e 's/^-l//' -e 's/$/.lib/'`
456     else
457       ax_try_lib="${ax_lib}"
458     fi
459     LIBS="${ax_try_lib} ${GLUT_LIBS} ${ax_save_LIBS}"
460     AC_LINK_IFELSE(
461     [AC_LANG_PROGRAM([[
462 # if HAVE_WINDOWS_H && defined(_WIN32)
463 #   include <windows.h>
464 # endif
465 # include <GL/glut.h>]],
466                      [[glutMainLoop()]])],
467     [ax_cv_check_glut_libglut="${ax_try_lib}"; break])
469   done
470   LIBS=${ax_save_LIBS}
471   ])
472   CPPFLAGS="${ax_save_CPPFLAGS}"
473   AC_LANG_POP(C)
475   if test "X${ax_cv_check_glut_libglut}" = "Xno"; then
476     no_glut="yes"
477     GLUT_CFLAGS=""
478     GLUT_LIBS=""
479   else
480     GLUT_LIBS="${ax_cv_check_glut_libglut} ${GLUT_LIBS}"
481   fi
484 AC_SUBST([GLUT_CFLAGS])
485 AC_SUBST([GLUT_LIBS])
486 ])dnl
487 dnl @synopsis AX_LANG_COMPILER_MS
489 dnl Check whether the compiler for the current language is Microsoft.
491 dnl This macro is modeled after _AC_LANG_COMPILER_GNU in the GNU
492 dnl Autoconf implementation.
494 dnl @category InstalledPackages
495 dnl @author Braden McDaniel <braden@endoframe.com>
496 dnl @version 2004-11-15
497 dnl @license AllPermissive
499 AC_DEFUN([AX_LANG_COMPILER_MS],
500 [AC_CACHE_CHECK([whether we are using the Microsoft _AC_LANG compiler],
501                 [ax_cv_[]_AC_LANG_ABBREV[]_compiler_ms],
502 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [[#ifndef _MSC_VER
503        choke me
504 #endif
505 ]])],
506                    [ax_compiler_ms=yes],
507                    [ax_compiler_ms=no])
508 ax_cv_[]_AC_LANG_ABBREV[]_compiler_ms=$ax_compiler_ms
509 ])])