1# ===========================================================================
2#        http://www.gnu.org/software/autoconf-archive/ax_have_qt.html
3# ===========================================================================
4#
5# SYNOPSIS
6#
7#   AX_HAVE_QT [--with-Qt-dir=DIR] [--with-Qt-lib-dir=DIR] [--with-Qt-lib=LIB]
8#   AX_HAVE_QT [--with-Qt-include-dir=DIR] [--with-Qt-bin-dir=DIR] [--with-Qt-lib-dir=DIR] [--with-Qt-lib=LIB]
9#
10# DESCRIPTION
11#
12#   Searches common directories for Qt include files, libraries and Qt
13#   binary utilities. The macro supports several different versions of the
14#   Qt framework being installed on the same machine. Without options, the
15#   macro is designed to look for the latest library, i.e., the highest
16#   definition of QT_VERSION in qglobal.h. By use of one or more options a
17#   different library may be selected. There are two different sets of
18#   options. Both sets contain the option --with-Qt-lib=LIB which can be
19#   used to force the use of a particular version of the library file when
20#   more than one are available. LIB must be in the form as it would appear
21#   behind the "-l" option to the compiler. Examples for LIB would be
22#   "qt-mt" for the multi-threaded version and "qt" for the regular version.
23#   In addition to this, the first set consists of an option
24#   --with-Qt-dir=DIR which can be used when the installation conforms to
25#   Trolltech's standard installation, which means that header files are in
26#   DIR/include, binary utilities are in DIR/bin and the library is in
27#   DIR/lib. The second set of options can be used to indicate individual
28#   locations for the header files, the binary utilities and the library
29#   file, in addition to the specific version of the library file.
30#
31#   The following shell variable is set to either "yes" or "no":
32#
33#     have_qt
34#
35#   Additionally, the following variables are exported:
36#
37#     QT_CXXFLAGS
38#     QT_LIBS
39#     QT_MOC
40#     QT_UIC
41#     QT_LRELEASE
42#     QT_LUPDATE
43#     QT_DIR
44#
45#   which respectively contain an "-I" flag pointing to the Qt include
46#   directory (and "-DQT_THREAD_SUPPORT" when LIB is "qt-mt"), link flags
47#   necessary to link with Qt and X, the name of the meta object compiler
48#   and the user interface compiler both with full path, and finaly the
49#   variable QTDIR as Trolltech likes to see it defined (if possible).
50#
51#   Example lines for Makefile.in:
52#
53#     CXXFLAGS = @QT_CXXFLAGS@
54#     MOC      = @QT_MOC@
55#
56#   After the variables have been set, a trial compile and link is performed
57#   to check the correct functioning of the meta object compiler. This test
58#   may fail when the different detected elements stem from different
59#   releases of the Qt framework. In that case, an error message is emitted
60#   and configure stops.
61#
62#   No common variables such as $LIBS or $CFLAGS are polluted.
63#
64#   Options:
65#
66#   --with-Qt-dir=DIR: DIR is equal to $QTDIR if you have followed the
67#   installation instructions of Trolltech. Header files are in DIR/include,
68#   binary utilities are in DIR/bin and the library is in DIR/lib.
69#
70#   --with-Qt-include-dir=DIR: Qt header files are in DIR.
71#
72#   --with-Qt-bin-dir=DIR: Qt utilities such as moc and uic are in DIR.
73#
74#   --with-Qt-lib-dir=DIR: The Qt library is in DIR.
75#
76#   --with-Qt-lib=LIB: Use -lLIB to link with the Qt library.
77#
78#   If some option "=no" or, equivalently, a --without-Qt-* version is given
79#   in stead of a --with-Qt-*, "have_qt" is set to "no" and the other
80#   variables are set to the empty string.
81#
82# LICENSE
83#
84#   Copyright (c) 2008 Bastiaan Veelo <Bastiaan@Veelo.net>
85#
86#   Copying and distribution of this file, with or without modification, are
87#   permitted in any medium without royalty provided the copyright notice
88#   and this notice are preserved. This file is offered as-is, without any
89#   warranty.
90
91#serial 10
92
93dnl Calls AX_PATH_QT_DIRECT (contained in this file) as a subroutine.
94AU_ALIAS([BNV_HAVE_QT], [AX_HAVE_QT])
95AC_DEFUN([AX_HAVE_QT],
96[
97  AC_REQUIRE([AC_PROG_CXX])
98  AC_REQUIRE([AC_PATH_X])
99  AC_REQUIRE([AC_PATH_XTRA])
100
101  AC_MSG_CHECKING(for Qt)
102
103  AC_ARG_WITH([Qt-dir],
104              AS_HELP_STRING([--with-Qt-dir=DIR],
105                             [DIR is equal to $QTDIR if you have followed the
106                              installation instructions of Trolltech. Header
107                              files are in DIR/include, binary utilities are
108                              in DIR/bin. The library is in DIR/lib, unless
109                              --with-Qt-lib-dir is also set.]))
110  AC_ARG_WITH([Qt-include-dir],
111              AS_HELP_STRING([--with-Qt-include-dir=DIR],
112                             [Qt header files are in DIR]))
113  AC_ARG_WITH([Qt-bin-dir],
114              AS_HELP_STRING([--with-Qt-bin-dir=DIR],
115                             [Qt utilities such as moc and uic are in DIR]))
116  AC_ARG_WITH([Qt-lib-dir],
117              AS_HELP_STRING([--with-Qt-lib-dir=DIR],
118                             [The Qt library is in DIR]))
119  AC_ARG_WITH([Qt-lib],
120              AS_HELP_STRING([--with-Qt-lib=LIB],
121                             [Use -lLIB to link with the Qt library]))
122  if test x"$with_Qt_dir" = x"no" ||
123     test x"$with_Qt_include-dir" = x"no" ||
124     test x"$with_Qt_bin_dir" = x"no" ||
125     test x"$with_Qt_lib_dir" = x"no" ||
126     test x"$with_Qt_lib" = x"no"; then
127    # user disabled Qt. Leave cache alone.
128    have_qt="User disabled Qt."
129  else
130    # "yes" is a bogus option
131    if test x"$with_Qt_dir" = xyes; then
132      with_Qt_dir=
133    fi
134    if test x"$with_Qt_include_dir" = xyes; then
135      with_Qt_include_dir=
136    fi
137    if test x"$with_Qt_bin_dir" = xyes; then
138      with_Qt_bin_dir=
139    fi
140    if test x"$with_Qt_lib_dir" = xyes; then
141      with_Qt_lib_dir=
142    fi
143    if test x"$with_Qt_lib" = xyes; then
144      with_Qt_lib=
145    fi
146    # No Qt unless we discover otherwise
147    have_qt=no
148    # Check whether we are requested to link with a specific version
149    if test x"$with_Qt_lib" != x; then
150      ax_qt_lib="$with_Qt_lib"
151    fi
152    # Check whether we were supplied with an answer already
153    if test x"$with_Qt_dir" != x; then
154      have_qt=yes
155      ax_qt_dir="$with_Qt_dir"
156      ax_qt_include_dir="$with_Qt_dir/include"
157      ax_qt_bin_dir="$with_Qt_dir/bin"
158      ax_qt_lib_dir="$with_Qt_dir/lib"
159      # Only search for the lib if the user did not define one already
160      if test x"$ax_qt_lib" = x; then
161        ax_qt_lib="`ls $ax_qt_lib_dir/libQt* | sed -n 1p |
162                     sed s@$ax_qt_lib_dir/lib@@ | [sed s@[.].*@@]`"
163      fi
164      ax_qt_LIBS="-L$ax_qt_lib_dir -l$ax_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
165    else
166      # Use cached value or do search, starting with suggestions from
167      # the command line
168      AC_CACHE_VAL(ax_cv_have_qt,
169      [
170        # We are not given a solution and there is no cached value.
171        ax_qt_dir=NO
172        ax_qt_include_dir=NO
173        ax_qt_lib_dir=NO
174        if test x"$ax_qt_lib" = x; then
175          ax_qt_lib=NO
176        fi
177        AX_PATH_QT_DIRECT
178        if test "$ax_qt_dir" = NO ||
179           test "$ax_qt_include_dir" = NO ||
180           test "$ax_qt_lib_dir" = NO ||
181           test "$ax_qt_lib" = NO; then
182          # Problem with finding complete Qt.  Cache the known absence of Qt.
183          ax_cv_have_qt="have_qt=no"
184        else
185          # Record where we found Qt for the cache.
186          ax_cv_have_qt="have_qt=yes                  \
187                       ax_qt_dir=$ax_qt_dir          \
188               ax_qt_include_dir=$ax_qt_include_dir  \
189                   ax_qt_bin_dir=$ax_qt_bin_dir      \
190                      ax_qt_LIBS=\"$ax_qt_LIBS\""
191        fi
192      ])dnl
193      eval "$ax_cv_have_qt"
194    fi # all $ax_qt_* are set
195  fi   # $have_qt reflects the system status
196  if test x"$have_qt" = xyes; then
197    QT_CXXFLAGS="-I$ax_qt_include_dir -I$ax_qt_include_dir/Qt -I$ax_qt_include_dir/QtCore -I$ax_qt_include_dir/QtGui"
198    if test x"$ax_qt_lib" = xqt-mt; then
199        QT_CXXFLAGS="$QT_CXXFLAGS -DQT_THREAD_SUPPORT"
200    fi
201    QT_DIR="$ax_qt_dir"
202    QT_LIBS="$ax_qt_LIBS"
203    # If ax_qt_dir is defined, utilities are expected to be in the
204    # bin subdirectory
205    if test x"$ax_qt_dir" != x; then
206        if test -x "$ax_qt_dir/bin/uic"; then
207          QT_UIC="$ax_qt_dir/bin/uic"
208        else
209          # Old versions of Qt don't have uic
210          QT_UIC=
211        fi
212      QT_MOC="$ax_qt_dir/bin/moc"
213      QT_LRELEASE="$ax_qt_dir/bin/lrelease"
214      QT_LUPDATE="$ax_qt_dir/bin/lupdate"
215    else
216      # Or maybe we are told where to look for the utilities
217      if test x"$ax_qt_bin_dir" != x; then
218        if test -x "$ax_qt_bin_dir/uic"; then
219          QT_UIC="$ax_qt_bin_dir/uic"
220        else
221          # Old versions of Qt don't have uic
222          QT_UIC=
223        fi
224        QT_MOC="$ax_qt_bin_dir/moc"
225        QT_LRELEASE="$ax_qt_bin_dir/lrelease"
226        QT_LUPDATE="$ax_qt_bin_dir/lupdate"
227      else
228      # Last possibility is that they are in $PATH
229        QT_UIC="`which uic`"
230        QT_MOC="`which moc`"
231        QT_LRELEASE="`which lrelease`"
232        QT_LUPDATE="`which lupdate`"
233      fi
234    fi
235    # All variables are defined, report the result
236    AC_MSG_RESULT([$have_qt:
237    QT_CXXFLAGS=$QT_CXXFLAGS
238    QT_DIR=$QT_DIR
239    QT_LIBS=$QT_LIBS
240    QT_UIC=$QT_UIC
241    QT_MOC=$QT_MOC
242    QT_LRELEASE=$QT_LRELEASE
243    QT_LUPDATE=$QT_LUPDATE])
244  else
245    # Qt was not found
246    QT_CXXFLAGS=
247    QT_DIR=
248    QT_LIBS=
249    QT_UIC=
250    QT_MOC=
251    QT_LRELEASE=
252    QT_LUPDATE=
253    AC_MSG_RESULT($have_qt)
254  fi
255  AC_SUBST(QT_CXXFLAGS)
256  AC_SUBST(QT_DIR)
257  AC_SUBST(QT_LIBS)
258  AC_SUBST(QT_UIC)
259  AC_SUBST(QT_MOC)
260  AC_SUBST(QT_LRELEASE)
261  AC_SUBST(QT_LUPDATE)
262
263  #### Being paranoid:
264  if test x"$have_qt" = xyes; then
265    AC_MSG_CHECKING(correct functioning of Qt installation)
266    AC_CACHE_VAL(ax_cv_qt_test_result,
267    [
268      cat > ax_qt_test.h << EOF
269#include <QObject>
270class Test : public QObject
271{
272Q_OBJECT
273public:
274  Test() {}
275  ~Test() {}
276public slots:
277  void receive() {}
278signals:
279  void send();
280};
281EOF
282
283      cat > ax_qt_main.$ac_ext << EOF
284#include "ax_qt_test.h"
285#include <QApplication>
286int main( int argc, char **argv )
287{
288  QApplication app( argc, argv );
289  Test t;
290  QObject::connect( &t, SIGNAL(send()), &t, SLOT(receive()) );
291}
292EOF
293
294      ax_cv_qt_test_result="failure"
295      ax_try_1="$QT_MOC ax_qt_test.h -o moc_ax_qt_test.$ac_ext"
296      AC_TRY_EVAL(ax_try_1)
297      if test x"$ac_status" != x0; then
298        echo "$ax_err_1" >&AS_MESSAGE_LOG_FD
299        echo "configure: could not run $QT_MOC on:" >&AS_MESSAGE_LOG_FD
300        cat ax_qt_test.h >&AS_MESSAGE_LOG_FD
301      else
302        ax_try_2="$CXX $QT_CXXFLAGS -c $CXXFLAGS -o moc_ax_qt_test.o moc_ax_qt_test.$ac_ext"
303        AC_TRY_EVAL(ax_try_2)
304        if test x"$ac_status" != x0; then
305          echo "$ax_err_2" >&AS_MESSAGE_LOG_FD
306          echo "configure: could not compile:" >&AS_MESSAGE_LOG_FD
307          cat moc_ax_qt_test.$ac_ext >&AS_MESSAGE_LOG_FD
308        else
309          ax_try_3="$CXX $QT_CXXFLAGS -c $CXXFLAGS -o ax_qt_main.o ax_qt_main.$ac_ext"
310          AC_TRY_EVAL(ax_try_3)
311          if test x"$ac_status" != x0; then
312            echo "$ax_err_3" >&AS_MESSAGE_LOG_FD
313            echo "configure: could not compile:" >&AS_MESSAGE_LOG_FD
314            cat ax_qt_main.$ac_ext >&AS_MESSAGE_LOG_FD
315          else
316            ax_try_4="$CXX -o ax_qt_main ax_qt_main.o moc_ax_qt_test.o $QT_LIBS $LIBS"
317            AC_TRY_EVAL(ax_try_4)
318            if test x"$ac_status" != x0; then
319              echo "$ax_err_4" >&AS_MESSAGE_LOG_FD
320            else
321              ax_cv_qt_test_result="success"
322            fi
323          fi
324        fi
325      fi
326    ])dnl AC_CACHE_VAL ax_cv_qt_test_result
327    AC_MSG_RESULT([$ax_cv_qt_test_result])
328    if test x"$ax_cv_qt_test_result" = "xfailure"; then
329      AC_MSG_ERROR([Failed to find matching components of a complete
330                  Qt installation. Try using more options,
331                  see ./configure --help.])
332    fi
333
334    rm -f ax_qt_test.h moc_ax_qt_test.$ac_ext moc_ax_qt_test.o \
335          ax_qt_main.$ac_ext ax_qt_main.o ax_qt_main
336  fi
337])
338
339dnl Internal subroutine of AX_HAVE_QT
340dnl Set ax_qt_dir ax_qt_include_dir ax_qt_bin_dir ax_qt_lib_dir ax_qt_lib
341AC_DEFUN([AX_PATH_QT_DIRECT],
342[
343  ## Binary utilities ##
344  if test x"$with_Qt_bin_dir" != x; then
345    ax_qt_bin_dir=$with_Qt_bin_dir
346  fi
347  ## Look for header files ##
348  if test x"$with_Qt_include_dir" != x; then
349    ax_qt_include_dir="$with_Qt_include_dir"
350  else
351    # The following header file is expected to define QT_VERSION.
352    qt_direct_test_header=qglobal.h
353    # Look for the header file in a standard set of common directories.
354    ax_include_path_list="
355      /usr/include
356      `ls -dr ${QTDIR}/include 2>/dev/null`
357      `ls -dr /usr/include/qt* 2>/dev/null`
358      `ls -dr /usr/lib/qt*/include 2>/dev/null`
359      `ls -dr /usr/local/qt*/include 2>/dev/null`
360      `ls -dr /opt/qt*/include 2>/dev/null`
361      `ls -dr /Developer/qt*/include 2>/dev/null`
362    "
363    for ax_dir in $ax_include_path_list; do
364      if test -r "$ax_dir/$qt_direct_test_header"; then
365        ax_dirs="$ax_dirs $ax_dir"
366      fi
367    done
368    # Now look for the newest in this list
369    ax_prev_ver=0
370    for ax_dir in $ax_dirs; do
371      ax_this_ver=`egrep -w '#define QT_VERSION' $ax_dir/$qt_direct_test_header | sed s/'#define QT_VERSION'//`
372      if expr $ax_this_ver '>' $ax_prev_ver > /dev/null; then
373        ax_qt_include_dir=$ax_dir
374        ax_prev_ver=$ax_this_ver
375      fi
376    done
377  fi dnl Found header files.
378
379  # Are these headers located in a traditional Trolltech installation?
380  # That would be $ax_qt_include_dir stripped from its last element:
381  ax_possible_qt_dir=`dirname $ax_qt_include_dir`
382  if (test -x $ax_possible_qt_dir/bin/moc) &&
383     ((ls $ax_possible_qt_dir/lib/libQt* > /dev/null 2>/dev/null) ||
384      (ls $ax_possible_qt_dir/lib64/libQt* > /dev/null 2>/dev/null)); then
385    # Then the rest is a piece of cake
386    ax_qt_dir=$ax_possible_qt_dir
387    ax_qt_bin_dir="$ax_qt_dir/bin"
388    if test x"$with_Qt_lib_dir" != x; then
389      ax_qt_lib_dir="$with_Qt_lib_dir"
390    else
391      if (test -d $ax_qt_dir/lib64); then
392	ax_qt_lib_dir="$ax_qt_dir/lib64"
393      else
394	ax_qt_lib_dir="$ax_qt_dir/lib"
395      fi
396    fi
397    # Only look for lib if the user did not supply it already
398    if test x"$ax_qt_lib" = xNO; then
399      ax_qt_lib="`ls $ax_qt_lib_dir/libQt* | sed -n 1p |
400                   sed s@$ax_qt_lib_dir/lib@@ | [sed s@[.].*@@]`"
401    fi
402    ax_qt_LIBS="-L$ax_qt_lib_dir -l$ax_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
403  else
404    # There is no valid definition for $QTDIR as Trolltech likes to see it
405    ax_qt_dir=
406    ## Look for Qt library ##
407    if test x"$with_Qt_lib_dir" != x; then
408      ax_qt_lib_dir="$with_Qt_lib_dir"
409      # Only look for lib if the user did not supply it already
410      if test x"$ax_qt_lib" = xNO; then
411        ax_qt_lib="`ls $ax_qt_lib_dir/libQt* | sed -n 1p |
412                     sed s@$ax_qt_lib_dir/lib@@ | [sed s@[.].*@@]`"
413      fi
414      ax_qt_LIBS="-L$ax_qt_lib_dir -l$ax_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
415    else
416      # Normally, when there is no traditional Trolltech installation,
417      # the library is installed in a place where the linker finds it
418      # automatically.
419      # If the user did not define the library name, try with qt
420      if test x"$ax_qt_lib" = xNO; then
421        ax_qt_lib=qt
422      fi
423      qt_direct_test_header=qapplication.h
424      qt_direct_test_main="
425        int argc;
426        char ** argv;
427        QApplication app(argc,argv);
428      "
429      # See if we find the library without any special options.
430      # Don't add top $LIBS permanently yet
431      ax_save_LIBS="$LIBS"
432      LIBS="-l$ax_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
433      ax_qt_LIBS="$LIBS"
434      ax_save_CXXFLAGS="$CXXFLAGS"
435      CXXFLAGS="-I$ax_qt_include_dir -I$ax_qt_include_dir/Qt -I$ax_qt_include_dir/QtCore -I$ax_qt_include_dir/QtGui"
436      AC_TRY_LINK([#include <$qt_direct_test_header>],
437        $qt_direct_test_main,
438      [
439        # Success.
440        # We can link with no special library directory.
441        ax_qt_lib_dir=
442      ], [
443        # That did not work. Try the multi-threaded version
444        echo "Non-critical error, please neglect the above." >&AS_MESSAGE_LOG_FD
445        ax_qt_lib=qt-mt
446        LIBS="-l$ax_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
447        AC_TRY_LINK([#include <$qt_direct_test_header>],
448          $qt_direct_test_main,
449        [
450          # Success.
451          # We can link with no special library directory.
452          ax_qt_lib_dir=
453        ], [
454          # That did not work. Try the OpenGL version
455          echo "Non-critical error, please neglect the above." >&AS_MESSAGE_LOG_FD
456          ax_qt_lib=qt-gl
457          LIBS="-l$ax_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
458          AC_TRY_LINK([#include <$qt_direct_test_header>],
459            $qt_direct_test_main,
460          [
461            # Success.
462            # We can link with no special library directory.
463            ax_qt_lib_dir=
464          ], [
465            # That did not work. Maybe a library version I don't know about?
466            echo "Non-critical error, please neglect the above." >&AS_MESSAGE_LOG_FD
467            # Look for some Qt lib in a standard set of common directories.
468            ax_dir_list="
469              `echo $ax_qt_includes | sed ss/includess`
470              /lib
471	      /usr/lib64
472              /usr/lib
473	      /usr/local/lib64
474              /usr/local/lib
475	      /opt/lib64
476              /opt/lib
477              `ls -dr /usr/lib64/qt* 2>/dev/null`
478              `ls -dr /usr/lib64/qt*/lib64 2>/dev/null`
479              `ls -dr /usr/lib/qt* 2>/dev/null`
480              `ls -dr /usr/local/qt* 2>/dev/null`
481              `ls -dr /opt/qt* 2>/dev/null`
482            "
483            for ax_dir in $ax_dir_list; do
484              if ls $ax_dir/libQt* >/dev/null 2>/dev/null; then
485                # Gamble that it's the first one...
486                ax_qt_lib="`ls $ax_dir/libQt* | sed -n 1p |
487                            sed s@$ax_dir/lib@@ | sed s/[[.]].*//`"
488                ax_qt_lib_dir="$ax_dir"
489                break
490              fi
491            done
492            # Try with that one
493            LIBS="-l$ax_qt_lib $X_PRE_LIBS $X_LIBS -lX11 -lXext -lXmu -lXt -lXi $X_EXTRA_LIBS"
494            AC_TRY_LINK([#include <$qt_direct_test_header>],
495              $qt_direct_test_main,
496            [
497              # Success.
498              # We can link with no special library directory.
499              ax_qt_lib_dir=
500            ], [
501             : # Leave ax_qt_lib_dir defined
502            ])
503          ])
504        ])
505      ])
506      if test x"$ax_qt_lib_dir" != x; then
507        ax_qt_LIBS="-L$ax_qt_lib_dir $LIBS"
508      else
509        ax_qt_LIBS="$LIBS"
510      fi
511      LIBS="$ax_save_LIBS"
512      CXXFLAGS="$ax_save_CXXFLAGS"
513    fi dnl $with_Qt_lib_dir was not given
514  fi dnl Done setting up for non-traditional Trolltech installation
515])
516