Import fmit upstream version 0.97.6
[fmit.git] / m4 / bnv_have_qt.m4
1 dnl Copyright (C) 2001, 2002, 2003, 2005, 2006 Bastiaan Veelo
2
3 dnl THANKS! This code includes bug fixes and contributions made by:
4 dnl Tim McClarren,
5 dnl Dennis R. Weilert,
6 dnl Qingning Huo,
7 dnl Brian Mingus,
8 dnl Jens Hannemann,
9 dnl Pavel Roskin,
10 dnl Scott J. Bertin.
11
12 dnl ChangeLog
13 dnl 2006-03-12  * Hide output of ls and fix an m4 quoting problem (due to Scott J. Bertin).
14 dnl 2006-02-13  * Check compiler return value instead of parsing the error stream,
15 dnl               which detected warnings as false negatives (due to Jens Hannemann).
16 dnl 2006-02-02  * Spelling of "Success".
17 dnl             * Fixed unsave test for $bnv_qt_lib without quotes.
18 dnl             * Put dnl in front of all comments.
19 dnl             * Changed -l$bnv_qt_lib_dir into -L$bnv_qt_lib_dir (all due to Pavel Roskin).
20 dnl 2006-01-19  * Support for 64bit architectures.
21 dnl             * Updated documentation.
22 dnl 2006-01-18: * Fix "cat: bnv_qt_test.c: No such file or directory" (due to Jens Hannemann).
23 dnl             * Hide output of failing ls.
24 dnl 2006-01-11: * Check in /Developer on Mac OS X; Check in $QTDIR (due to Brian Mingus).
25
26 dnl Calls BNV_PATH_QT_DIRECT (contained in this file) as a subroutine.
27 AC_DEFUN([BNV_HAVE_QT],
28 [
29   AC_REQUIRE([AC_PROG_CXX])
30   AC_REQUIRE([AC_PATH_X])
31   AC_REQUIRE([AC_PATH_XTRA])
32
33   AC_MSG_CHECKING(for Qt)
34
35   AC_ARG_WITH([Qt-dir],
36     [  --with-Qt-dir=DIR       DIR is equal to $QTDIR if you have followed the
37                           installation instructions of Trolltech. Header
38                           files are in DIR/include, binary utilities are
39                           in DIR/bin. The library is in DIR/lib, unless
40                           --with-Qt-lib-dir is also set.])
41   AC_ARG_WITH([Qt-include-dir],
42     [  --with-Qt-include-dir=DIR
43                           Qt header files are in DIR])
44   AC_ARG_WITH([Qt-bin-dir],
45     [  --with-Qt-bin-dir=DIR   Qt utilities such as moc and uic are in DIR])
46   AC_ARG_WITH([Qt-lib-dir],
47     [  --with-Qt-lib-dir=DIR   The Qt library is in DIR])
48   AC_ARG_WITH([Qt-lib],
49     [  --with-Qt-lib=LIB       Use -lLIB to link with the Qt library])
50   if test x"$with_Qt_dir" = x"no" ||
51      test x"$with_Qt_include-dir" = x"no" ||
52      test x"$with_Qt_bin_dir" = x"no" ||
53      test x"$with_Qt_lib_dir" = x"no" ||
54      test x"$with_Qt_lib" = x"no"; then
55     # user disabled Qt. Leave cache alone.
56     have_qt="User disabled Qt."
57   else
58     # "yes" is a bogus option
59     if test x"$with_Qt_dir" = xyes; then
60       with_Qt_dir=
61     fi
62     if test x"$with_Qt_include_dir" = xyes; then
63       with_Qt_include_dir=
64     fi
65     if test x"$with_Qt_bin_dir" = xyes; then
66       with_Qt_bin_dir=
67     fi
68     if test x"$with_Qt_lib_dir" = xyes; then
69       with_Qt_lib_dir=
70     fi
71     if test x"$with_Qt_lib" = xyes; then
72       with_Qt_lib=
73     fi
74     # No Qt unless we discover otherwise
75     have_qt=no
76     # Check whether we are requested to link with a specific version
77     if test x"$with_Qt_lib" != x; then
78       bnv_qt_lib="$with_Qt_lib"
79     fi
80     # Check whether we were supplied with an answer already
81     if test x"$with_Qt_dir" != x; then
82       have_qt=yes
83       bnv_qt_dir="$with_Qt_dir"
84       bnv_qt_include_dir="$with_Qt_dir/include"
85       bnv_qt_bin_dir="$with_Qt_dir/bin"
86       bnv_qt_lib_dir="$with_Qt_dir/lib"
87       # Only search for the lib if the user did not define one already
88       if test x"$bnv_qt_lib" = x; then
89         bnv_qt_lib="`ls $bnv_qt_lib_dir/libqt* | sed -n 1p |
90                      sed s@$bnv_qt_lib_dir/lib@@ | [sed s@[.].*@@]`"
91       fi
92       bnv_qt_LIBS="-L$bnv_qt_lib_dir -l$bnv_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
93     else
94       # Use cached value or do search, starting with suggestions from
95       # the command line
96       AC_CACHE_VAL(bnv_cv_have_qt,
97       [
98         # We are not given a solution and there is no cached value.
99         bnv_qt_dir=NO
100         bnv_qt_include_dir=NO
101         bnv_qt_lib_dir=NO
102         if test x"$bnv_qt_lib" = x; then
103           bnv_qt_lib=NO
104         fi
105         BNV_PATH_QT_DIRECT
106         if test "$bnv_qt_dir" = NO ||
107            test "$bnv_qt_include_dir" = NO ||
108            test "$bnv_qt_lib_dir" = NO ||
109            test "$bnv_qt_lib" = NO; then
110           # Problem with finding complete Qt.  Cache the known absence of Qt.
111           bnv_cv_have_qt="have_qt=no"
112         else
113           # Record where we found Qt for the cache.
114           bnv_cv_have_qt="have_qt=yes                  \
115                        bnv_qt_dir=$bnv_qt_dir          \
116                bnv_qt_include_dir=$bnv_qt_include_dir  \
117                    bnv_qt_bin_dir=$bnv_qt_bin_dir      \
118                       bnv_qt_LIBS=\"$bnv_qt_LIBS\""
119         fi
120       ])dnl
121       eval "$bnv_cv_have_qt"
122     fi # all $bnv_qt_* are set
123   fi   # $have_qt reflects the system status
124   if test x"$have_qt" = xyes; then
125     QT_CXXFLAGS="-I$bnv_qt_include_dir"
126     if test x"$bnv_qt_lib" = xqt-mt; then
127         QT_CXXFLAGS="$QT_CXXFLAGS -DQT_THREAD_SUPPORT"
128     fi
129     QT_DIR="$bnv_qt_dir"
130     QT_LIBS="$bnv_qt_LIBS"
131     # If bnv_qt_dir is defined, utilities are expected to be in the
132     # bin subdirectory
133     if test x"$bnv_qt_dir" != x; then
134         if test -x "$bnv_qt_dir/bin/uic"; then
135           QT_UIC="$bnv_qt_dir/bin/uic"
136         else
137           # Old versions of Qt don't have uic
138           QT_UIC=
139         fi
140       QT_MOC="$bnv_qt_dir/bin/moc"
141     else
142       # Or maybe we are told where to look for the utilities
143       if test x"$bnv_qt_bin_dir" != x; then
144         if test -x "$bnv_qt_bin_dir/uic"; then
145           QT_UIC="$bnv_qt_bin_dir/uic"
146         else
147           # Old versions of Qt don't have uic
148           QT_UIC=
149         fi
150         QT_MOC="$bnv_qt_bin_dir/moc"
151       else
152       # Last possibility is that they are in $PATH
153         QT_UIC="`which uic`"
154         QT_MOC="`which moc`"
155       fi
156     fi
157     # All variables are defined, report the result
158     AC_MSG_RESULT([$have_qt:
159     QT_CXXFLAGS=$QT_CXXFLAGS
160     QT_DIR=$QT_DIR
161     QT_LIBS=$QT_LIBS
162     QT_UIC=$QT_UIC
163     QT_MOC=$QT_MOC])
164   else
165     # Qt was not found
166     QT_CXXFLAGS=
167     QT_DIR=
168     QT_LIBS=
169     QT_UIC=
170     QT_MOC=
171     AC_MSG_RESULT($have_qt)
172   fi
173   AC_SUBST(QT_CXXFLAGS)
174   AC_SUBST(QT_DIR)
175   AC_SUBST(QT_LIBS)
176   AC_SUBST(QT_UIC)
177   AC_SUBST(QT_MOC)
178
179   #### Being paranoid:
180   if test x"$have_qt" = xyes; then
181     AC_MSG_CHECKING(correct functioning of Qt installation)
182     AC_CACHE_VAL(bnv_cv_qt_test_result,
183     [
184       cat > bnv_qt_test.h << EOF
185 #include <qobject.h>
186 class Test : public QObject
187 {
188 Q_OBJECT
189 public:
190   Test() {}
191   ~Test() {}
192 public slots:
193   void receive() {}
194 signals:
195   void send();
196 };
197 EOF
198
199       cat > bnv_qt_main.$ac_ext << EOF
200 #include "bnv_qt_test.h"
201 #include <qapplication.h>
202 int main( int argc, char **argv )
203 {
204   QApplication app( argc, argv );
205   Test t;
206   QObject::connect( &t, SIGNAL(send()), &t, SLOT(receive()) );
207 }
208 EOF
209
210       bnv_cv_qt_test_result="failure"
211       bnv_try_1="$QT_MOC bnv_qt_test.h -o moc_bnv_qt_test.$ac_ext >/dev/null 2>/dev/null"
212       AC_TRY_EVAL(bnv_try_1)
213       if test x"$ac_status" != x0; then
214         echo "$bnv_err_1" >&AC_FD_CC
215         echo "configure: could not run $QT_MOC on:" >&AC_FD_CC
216         cat bnv_qt_test.h >&AC_FD_CC
217       else
218         bnv_try_2="$CXX $QT_CXXFLAGS -c $CXXFLAGS -o moc_bnv_qt_test.o moc_bnv_qt_test.$ac_ext >/dev/null 2>/dev/null"
219         AC_TRY_EVAL(bnv_try_2)
220         if test x"$ac_status" != x0; then
221           echo "$bnv_err_2" >&AC_FD_CC
222           echo "configure: could not compile:" >&AC_FD_CC
223           cat moc_bnv_qt_test.$ac_ext >&AC_FD_CC
224         else
225           bnv_try_3="$CXX $QT_CXXFLAGS -c $CXXFLAGS -o bnv_qt_main.o bnv_qt_main.$ac_ext >/dev/null 2>/dev/null"
226           AC_TRY_EVAL(bnv_try_3)
227           if test x"$ac_status" != x0; then
228             echo "$bnv_err_3" >&AC_FD_CC
229             echo "configure: could not compile:" >&AC_FD_CC
230             cat bnv_qt_main.$ac_ext >&AC_FD_CC
231           else
232             bnv_try_4="$CXX $LIBS -o bnv_qt_main bnv_qt_main.o moc_bnv_qt_test.o $QT_LIBS >/dev/null 2>/dev/null"
233             AC_TRY_EVAL(bnv_try_4)
234             if test x"$ac_status" != x0; then
235               echo "$bnv_err_4" >&AC_FD_CC
236             else
237               bnv_cv_qt_test_result="success"
238             fi
239           fi
240         fi
241       fi
242     ])dnl AC_CACHE_VAL bnv_cv_qt_test_result
243     AC_MSG_RESULT([$bnv_cv_qt_test_result]);
244     if test x"$bnv_cv_qt_test_result" = "xfailure"; then
245       AC_MSG_ERROR([Failed to find matching components of a complete
246                   Qt installation. Try using more options,
247                   see ./configure --help.])
248     fi
249
250     rm -f bnv_qt_test.h moc_bnv_qt_test.$ac_ext moc_bnv_qt_test.o \
251           bnv_qt_main.$ac_ext bnv_qt_main.o bnv_qt_main
252   fi
253 ])
254
255 dnl Internal subroutine of BNV_HAVE_QT
256 dnl Set bnv_qt_dir bnv_qt_include_dir bnv_qt_bin_dir bnv_qt_lib_dir bnv_qt_lib
257 AC_DEFUN([BNV_PATH_QT_DIRECT],
258 [
259   ## Binary utilities ##
260   if test x"$with_Qt_bin_dir" != x; then
261     bnv_qt_bin_dir=$with_Qt_bin_dir
262   fi
263   ## Look for header files ##
264   if test x"$with_Qt_include_dir" != x; then
265     bnv_qt_include_dir="$with_Qt_include_dir"
266   else
267     # The following header file is expected to define QT_VERSION.
268     qt_direct_test_header=qglobal.h
269     # Look for the header file in a standard set of common directories.
270     bnv_include_path_list="
271       /usr/include
272       `ls -dr ${QTDIR}/include 2>/dev/null`
273       `ls -dr /usr/include/qt* 2>/dev/null`
274       `ls -dr /usr/lib/qt*/include 2>/dev/null`
275       `ls -dr /usr/local/qt*/include 2>/dev/null`
276       `ls -dr /opt/qt*/include 2>/dev/null`
277       `ls -dr /Developer/qt*/include 2>/dev/null`
278     "
279     for bnv_dir in $bnv_include_path_list; do
280       if test -r "$bnv_dir/$qt_direct_test_header"; then
281         bnv_dirs="$bnv_dirs $bnv_dir"
282       fi
283     done
284     # Now look for the newest in this list
285     bnv_prev_ver=0
286     for bnv_dir in $bnv_dirs; do
287       bnv_this_ver=`egrep -w '#define QT_VERSION' $bnv_dir/$qt_direct_test_header | sed s/'#define QT_VERSION'//`
288       if expr $bnv_this_ver '>' $bnv_prev_ver > /dev/null; then
289         bnv_qt_include_dir=$bnv_dir
290         bnv_prev_ver=$bnv_this_ver
291       fi
292     done
293   fi dnl Found header files.
294
295   # Are these headers located in a traditional Trolltech installation?
296   # That would be $bnv_qt_include_dir stripped from its last element:
297   bnv_possible_qt_dir=`dirname $bnv_qt_include_dir`
298   if (test -x $bnv_possible_qt_dir/bin/moc) &&
299      ((ls $bnv_possible_qt_dir/lib/libqt* > /dev/null 2>/dev/null) ||
300       (ls $bnv_possible_qt_dir/lib64/libqt* > /dev/null 2>/dev/null)); then
301     # Then the rest is a piece of cake
302     bnv_qt_dir=$bnv_possible_qt_dir
303     bnv_qt_bin_dir="$bnv_qt_dir/bin"
304     if test x"$with_Qt_lib_dir" != x; then
305       bnv_qt_lib_dir="$with_Qt_lib_dir"
306     else
307       if (test -d $bnv_qt_dir/lib64); then
308         bnv_qt_lib_dir="$bnv_qt_dir/lib64"
309       else
310         bnv_qt_lib_dir="$bnv_qt_dir/lib"
311       fi
312     fi
313     # Only look for lib if the user did not supply it already
314     if test x"$bnv_qt_lib" = xNO; then
315       bnv_qt_lib="`ls $bnv_qt_lib_dir/libqt* | sed -n 1p |
316                    sed s@$bnv_qt_lib_dir/lib@@ | [sed s@[.].*@@]`"
317     fi
318     bnv_qt_LIBS="-L$bnv_qt_lib_dir -l$bnv_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
319   else
320     # There is no valid definition for $QTDIR as Trolltech likes to see it
321     bnv_qt_dir=
322     ## Look for Qt library ##
323     if test x"$with_Qt_lib_dir" != x; then
324       bnv_qt_lib_dir="$with_Qt_lib_dir"
325       # Only look for lib if the user did not supply it already
326       if test x"$bnv_qt_lib" = xNO; then
327         bnv_qt_lib="`ls $bnv_qt_lib_dir/libqt* | sed -n 1p |
328                      sed s@$bnv_qt_lib_dir/lib@@ | [sed s@[.].*@@]`"
329       fi
330       bnv_qt_LIBS="-L$bnv_qt_lib_dir -l$bnv_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
331     else
332       # Normally, when there is no traditional Trolltech installation,
333       # the library is installed in a place where the linker finds it
334       # automatically.
335       # If the user did not define the library name, try with qt
336       if test x"$bnv_qt_lib" = xNO; then
337         bnv_qt_lib=qt
338       fi
339       qt_direct_test_header=qapplication.h
340       qt_direct_test_main="
341         int argc;
342         char ** argv;
343         QApplication app(argc,argv);
344       "
345       # See if we find the library without any special options.
346       # Don't add top $LIBS permanently yet
347       bnv_save_LIBS="$LIBS"
348       LIBS="-l$bnv_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
349       bnv_qt_LIBS="$LIBS"
350       bnv_save_CXXFLAGS="$CXXFLAGS"
351       CXXFLAGS="-I$bnv_qt_include_dir"
352       AC_TRY_LINK([#include <$qt_direct_test_header>],
353         $qt_direct_test_main,
354       [
355         # Success.
356         # We can link with no special library directory.
357         bnv_qt_lib_dir=
358       ], [
359         # That did not work. Try the multi-threaded version
360         echo "Non-critical error, please neglect the above." >&AC_FD_CC
361         bnv_qt_lib=qt-mt
362         LIBS="-l$bnv_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
363         AC_TRY_LINK([#include <$qt_direct_test_header>],
364           $qt_direct_test_main,
365         [
366           # Success.
367           # We can link with no special library directory.
368           bnv_qt_lib_dir=
369         ], [
370           # That did not work. Try the OpenGL version
371           echo "Non-critical error, please neglect the above." >&AC_FD_CC
372           bnv_qt_lib=qt-gl
373           LIBS="-l$bnv_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
374           AC_TRY_LINK([#include <$qt_direct_test_header>],
375             $qt_direct_test_main,
376           [
377             # Success.
378             # We can link with no special library directory.
379             bnv_qt_lib_dir=
380           ], [
381             # That did not work. Maybe a library version I don't know about?
382             echo "Non-critical error, please neglect the above." >&AC_FD_CC
383             # Look for some Qt lib in a standard set of common directories.
384             bnv_dir_list="
385               `echo $bnv_qt_includes | sed ss/includess`
386               /lib
387               /usr/lib64
388               /usr/lib
389               /usr/local/lib64
390               /usr/local/lib
391               /opt/lib64
392               /opt/lib
393               `ls -dr /usr/lib64/qt* 2>/dev/null`
394               `ls -dr /usr/lib64/qt*/lib64 2>/dev/null`
395               `ls -dr /usr/lib/qt* 2>/dev/null`
396               `ls -dr /usr/local/qt* 2>/dev/null`
397               `ls -dr /opt/qt* 2>/dev/null`
398             "
399             for bnv_dir in $bnv_dir_list; do
400               if ls $bnv_dir/libqt* >/dev/null 2>/dev/null; then
401                 # Gamble that it's the first one...
402                 bnv_qt_lib="`ls $bnv_dir/libqt* | sed -n 1p |
403                             sed s@$bnv_dir/lib@@ | sed s/[[.]].*//`"
404                 bnv_qt_lib_dir="$bnv_dir"
405                 break
406               fi
407             done
408             # Try with that one
409             LIBS="-l$bnv_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
410             AC_TRY_LINK([#include <$qt_direct_test_header>],
411               $qt_direct_test_main,
412             [
413               # Success.
414               # We can link with no special library directory.
415               bnv_qt_lib_dir=
416             ], [
417               # Leave bnv_qt_lib_dir defined
418             ])
419           ])
420         ])
421       ])
422       if test x"$bnv_qt_lib_dir" != x; then
423         bnv_qt_LIBS="-L$bnv_qt_lib_dir $LIBS"
424       else
425         bnv_qt_LIBS="$LIBS"
426       fi
427       LIBS="$bnv_save_LIBS"
428       CXXFLAGS="$bnv_save_CXXFLAGS"
429     fi dnl $with_Qt_lib_dir was not given
430   fi dnl Done setting up for non-traditional Trolltech installation
431 ])