Fix last changes.
[autotroll.git] / build-aux / autotroll.m4
blob2a51cfb374385985ec43113da2400f50485225a6
1 # Build Qt apps with the autotools (Autoconf/Automake).
2 # M4 macros.
3 # This file is part of AutoTroll.
4 # Copyright (C) 2006  Benoit Sigoure <benoit.sigoure@lrde.epita.fr>
6 # AutoTroll is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 # USA.
21  # ------------- #
22  # DOCUMENTATION #
23  # ------------- #
25 # Disclaimer: Never tested with anything else than Qt 4.2! Feedback welcome.
26 # Simply invoke AT_WITH_QT in your configure.ac. AT_WITH_QT can take
27 # arguments which are documented in depth below. The default arguments are
28 # equivalent to the default .pro file generated by qmake.
30 # Invoking AT_WITH_QT will do the following:
31 #  - Add a --with-qt option to your configure
32 #  - Find qmake, moc and uic and save them in the make variables $(QMAKE),
33 #    $(MOC), $(UIC).
34 #  - Save the path to Qt in $(QT_PATH)
35 #  - Find the flags to use Qt, that is:
36 #     * $(QT_DEFINES): -D's defined by qmake.
37 #     * $(QT_CFLAGS): CFLAGS as defined by qmake (C?!)
38 #     * $(QT_CXXFLAGS): CXXFLAGS as defined by qmake.
39 #     * $(QT_INCPATH): -I's defined by qmake.
40 #     * $(QT_CPPFLAGS): Same as $(QT_DEFINES) + $(QT_INCPATH)
41 #     * $(QT_LFLAGS): LFLAGS defined by qmake.
42 #     * $(QT_LDFLAGS): Same thing as $(QT_LFLAGS).
43 #     * $(QT_LIBS): LIBS defined by qmake.
45 # You *MUST* invoke $(MOC) and/or $(UIC) where necessary. AutoTroll provides
46 # you with Makerules to ease this, here is a sample Makefile.am to use with
47 # AutoTroll which builds the code given in the chapter 7 of the Qt Tutorial:
48 # http://doc.trolltech.com/4.2/tutorial-t7.html
50 # -------------------------------------------------------------------------
51 # include $(top_srcdir)/build-aux/autotroll.mk
53 # ACLOCAL_AMFLAGS = -I build-aux
55 # bin_PROGRAMS = lcdrange
56 # lcdrange_SOURCES =  $(BUILT_SOURCES) lcdrange.cpp lcdrange.h main.cpp
57 # lcdrange_CXXFLAGS = $(QT_CXXFLAGS) $(AM_CXXFLAGS)
58 # lcdrange_CPPFLAGS = $(QT_CPPFLAGS) $(AM_CPPFLAGS)
59 # lcdrange_LDFLAGS  = $(QT_LDFLAGS) $(LDFLAGS)
60 # lcdrange_LDADD    = $(QT_LIBS) $(LDADD)
62 # BUILT_SOURCES = lcdrange.moc.cpp
63 # -------------------------------------------------------------------------
65 # Note that your MOC, UIC and QRC files *MUST* be listed manually in
66 # BUILT_SOURCES. If you name them properly (eg: .moc.cc, .qrc.cc, .ui.cc -- of
67 # course you can use .cpp or .cxx or .C rather than .cc) AutoTroll will build
68 # them automagically for you (using implicit rules defined in autotroll.mk).
70 m4_pattern_forbid([^AT_])dnl
71 m4_pattern_forbid([^_AT_])dnl
73 # AT_WITH_QT([QT_modules], [QT_config], [QT_misc])
74 # ------------------------------------------------
75 # Enable Qt support and add an option --with-qt to the configure script.
77 # The QT_modules argument is optional and defines extra modules to enable or
78 # disable (it's equivalent to the QT variable in .pro files). Modules can be
79 # specified as follows:
81 # AT_WITH_QT   => No argument -> No QT value.
82 #                                Qmake sets it to "core gui" by default.
83 # AT_WITH_QT([xml])   => QT += xml
84 # AT_WITH_QT([+xml])  => QT += xml
85 # AT_WITH_QT([-gui])  => QT -= gui
86 # AT_WITH_QT([xml -gui +sql svg])  => QT += xml sql svg
87 #                                     QT -= gui
89 # The QT_config argument is also optional and follows the same convention as
90 # QT_modules. Instead of changing the QT variable, it changes the CONFIG
91 # variable, which is used to tweak configuration and compiler options.
93 # The last argument, QT_misc (also optional) will be copied as-is the .pro
94 # file used to guess how to compile Qt apps. You may use it to further tweak
95 # the build process of Qt apps if tweaking the QT or CONFIG variables isn't
96 # enough for you.
97 AC_DEFUN([AT_WITH_QT],
98 [ AC_REQUIRE([AC_CANONICAL_HOST])
99   AC_REQUIRE([AC_CANONICAL_BUILD])
100   AC_REQUIRE([AC_PROG_CXX])
102   test x"$TROLL" != x && echo 'ViM rox emacs.'
104 dnl Memo: AC_ARG_WITH(package, help-string, [if-given], [if-not-given])
105   AC_ARG_WITH([qt],
106               [AS_HELP_STRING([--with-qt],
107                  [Path to Qt @<:@Look in PATH and /usr/local/Trolltech@:>@])],
108               [QT_PATH=$withval], [QT_PATH=])
110   # Find Qt.
111   if test -d /usr/local/Trolltech; then
112     # Try to find the latest version.
113     tmp_qt_paths=`echo /usr/local/Trolltech/*/bin | tr ' ' '\n' | sort -nr \
114                                               | xargs | sed 's/  */:/g'`
115   fi
117   # Find qmake.
118   AC_PATH_PROGS([QMAKE], [qmake], [missing], [$QT_DIR:$QT_PATH:$PATH:$tmp_qt_paths])
119   if test x"$QMAKE" = xmissing; then
120     AC_MSG_ERROR([Cannot find qmake in your PATH. Try using --with-qt.])
121   fi
123   # Find moc (Meta Object Compiler).
124   AC_PATH_PROGS([MOC], [moc], [missing], [$QT_PATH:$PATH:$tmp_qt_paths])
125   if test x"$MOC" = xmissing; then
126     AC_MSG_ERROR([Cannot find moc (Meta Object Compiler) in your PATH. Try using --with-qt.])
127   fi
129   # Find uic (User Interface Compiler).
130   AC_PATH_PROGS([UIC], [uic], [missing], [$QT_PATH:$PATH:$tmp_qt_paths])
131   if test x"$UIC" = xmissing; then
132     AC_MSG_ERROR([Cannot find uic (User Interface Compiler) in your PATH. Try using --with-qt.])
133   fi
135   # Find rcc (Qt Resource Compiler).
136   AC_PATH_PROGS([RCC], [rcc], [false], [$QT_PATH:$PATH:$tmp_qt_paths])
137   if test x"$UIC" = xfalse; then
138     AC_MSG_WARN([Cannot find rcc (Qt Resource Compiler) in your PATH. Try using --with-qt.])
139   fi
141   # If we don't know the path to Qt, guess it from the path to qmake.
142   if test x"$QT_PATH" = x; then
143     QT_PATH=`dirname "$QMAKE"`
144   fi
145   if test x"$QT_PATH" = x; then
146     AC_MSG_ERROR([Cannot find the path to your Qt install. Use --with-qt.])
147   fi
148   AC_SUBST([QT_PATH])
150   # Get ready to build a test-app with Qt.
151   mkdir conftest.dir
152   cd conftest.dir
153   cat >conftest.h <<_ASEOF
154 #include <QObject>
156 class Foo: public QObject
158   Q_OBJECT;
159 public:
160   Foo();
161   ~Foo() {}
162 public slots:
163   void setValue(int value);
164 signals:
165   void valueChanged(int newValue);
166 private:
167   int value_;
169 _ASEOF
171   cat >conftest.cpp <<_ASEOF
172 #include "conftest.h"
173 Foo::Foo()
174   : value_ (42)
176   connect(this, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
179 void Foo::setValue(int value)
181   value_ = value;
184 int main()
186   Foo f;
188 _ASEOF
189   if $QMAKE -project; then :; else
190     AC_MSG_ERROR([Calling $QMAKE -project failed.])
191   fi
193   # Find the .pro file generated by qmake.
194   pro_file='conftest.dir.pro'
195   test -f $pro_file || pro_file=`echo *.pro`
196   if test -f "$pro_file"; then :; else
197     AC_MSG_ERROR([Can't find the .pro file generated by Qmake.])
198   fi
200 dnl Tweak the value of QT in the .pro if have been the 1st arg.
201 m4_ifval([$1], [_AT_TWEAK_PRO_FILE([QT], [$1])])
203 dnl Tweak the value of CONFIG in the .pro if have been given a 2nd arg.
204 m4_ifval([$2], [_AT_TWEAK_PRO_FILE([CONFIG], [$2])])
206 m4_ifval([$3],
207 [ # Add the extra-settings the user wants to set in the .pro
208   echo "$3" >>"$pro_file"
211   echo "$as_me:$LINENO: Invoking $QMAKE on $pro_file" >&AS_MESSAGE_LOG_FD
212   sed 's/^/| /' "$pro_file" >&AS_MESSAGE_LOG_FD
214   if $QMAKE; then :; else
215     AC_MSG_ERROR([Calling $QMAKE failed.])
216   fi
217   # Try to compile a simple Qt app.
218   AC_CACHE_CHECK([whether we can build a simple Qt app], [at_cv_qt_build],
219   [at_cv_qt_build=ko
220   : ${MAKE=make}
222   if $MAKE >&AS_MESSAGE_LOG_FD 2>&1; then
223     at_cv_qt_build='ok, looks like Qt 4'
224   else
225     echo "$as_me:$LINENO: Build failed, trying to #include <qobject.h> \
226 instead" >&AS_MESSAGE_LOG_FD
227     sed 's/<QObject>/<qobject.h>/' conftest.h > tmp.h && mv tmp.h conftest.h
228     if $MAKE >&AS_MESSAGE_LOG_FD 2>&1; then
229       at_cv_qt_build='ok, looks like Qt 3'
230     else
231       # Sometimes (such as on Debian) build will fail because Qt hasn't been
232       # installed in debug mode and qmake tries (by default) to build apps in
233       # debug mode => Try again in release mode.
234       echo "$as_me:$LINENO: Build failed, trying to enforce release mode" \
235             >&AS_MESSAGE_LOG_FD
237       _AT_TWEAK_PRO_FILE([CONFIG], [+release])
239       sed 's/<qobject.h>/<QObject>/' conftest.h > tmp.h && mv tmp.h conftest.h
240       if $MAKE >&AS_MESSAGE_LOG_FD 2>&1; then
241         at_cv_qt_build='ok, looks like Qt 4, release mode forced'
242       else
243         echo "$as_me:$LINENO: Build failed, trying to #include <qobject.h> \
244 instead" >&AS_MESSAGE_LOG_FD
245         sed 's/<QObject>/<qobject.h>/' conftest.h > tmp.h && mv tmp.h conftest.h
246         if $MAKE >&AS_MESSAGE_LOG_FD 2>&1; then
247           at_cv_qt_build='ok, looks like Qt 3, release mode forced'
248         else
249           at_cv_qt_build=ko
250           echo "$as_me:$LINENO: failed program was:" >&AS_MESSAGE_LOG_FD
251           sed 's/^/| /' conftest.h >&AS_MESSAGE_LOG_FD
252           echo "$as_me:$LINENO: failed program was:" >&AS_MESSAGE_LOG_FD
253           sed 's/^/| /' conftest.cpp >&AS_MESSAGE_LOG_FD
254         fi # if make with Qt3-style #include and release mode forced.
255       fi # if make with Qt4-style #include and release mode forced.
256     fi # if make with Qt3-style #include.
257   fi # if make with Qt4-style #include.
258   ])dnl end: AC_CACHE_CHECK(at_cv_qt_build)
260   if test x"$at_cv_qt_build" = xko; then
261     AC_MSG_ERROR([Cannot build a test Qt program])
262   fi
263   QT_VERSION_MAJOR=`echo "$at_cv_qt_build" | sed 's/^[^0-9]*//'`
264   AC_SUBST([QT_VERSION_MAJOR])
266   # This sed filter is applied after an expression of the form: /^FOO.*=/!d;
267   # It starts by removing the beginning of the line, removing references to
268   # SUBLIBS, removing unnecessary whitespaces at the beginning, and prefixes
269   # all variable uses by QT_.
270   qt_sed_filter='s///;
271                  s/$(SUBLIBS)//g;
272                  s/^ *//;
273                  s/\$(\(@<:@A-Z_@:>@@<:@A-Z_@:>@*\))/$(QT_\1)/g'
275   # Find the Makefile (qmake happens to generate a fake Makefile which invokes
276   # a Makefile.Debug or Makefile.Release). We we have both, we'll pick the
277   # Makefile.Release. The reason is that the main difference is that release
278   # uses -Os and debug -g. We can override -Os by passing another -O but we
279   # usually don't override -g.
280   if test -f Makefile.Release; then
281     at_mfile='Makefile.Release'
282   else
283     at_mfile='Makefile'
284   fi
285   if test -f $at_mfile; then :; else
286     AC_MSG_ERROR([Cannot find the Makefile generated by qmake.])
287   fi
289   # Find the DEFINES of Qt (should have been named CPPFLAGS).
290   AC_CACHE_CHECK([for the DEFINES to use with Qt], [at_cv_env_QT_DEFINES],
291   [at_cv_env_QT_DEFINES=`sed "/^DEFINES@<:@^A-Z@:>@*=/!d;$qt_sed_filter" $at_mfile`])
292   AC_SUBST([QT_DEFINES], [$at_cv_env_QT_DEFINES])
294   # Find the CFLAGS of Qt (We can use Qt in C?!)
295   AC_CACHE_CHECK([for the CFLAGS to use with Qt], [at_cv_env_QT_CFLAGS],
296   [at_cv_env_QT_CFLAGS=`sed "/^CFLAGS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" $at_mfile`])
297   AC_SUBST([QT_CFLAGS], [$at_cv_env_QT_CFLAGS])
299   # Find the CXXFLAGS of Qt.
300   AC_CACHE_CHECK([for the CXXFLAGS to use with Qt], [at_cv_env_QT_CXXFLAGS],
301   [at_cv_env_QT_CXXFLAGS=`sed "/^CXXFLAGS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" $at_mfile`])
302   AC_SUBST([QT_CXXFLAGS], [$at_cv_env_QT_CXXFLAGS])
304   # Find the INCPATH of Qt.
305   AC_CACHE_CHECK([for the INCPATH to use with Qt], [at_cv_env_QT_INCPATH],
306   [at_cv_env_QT_INCPATH=`sed "/^INCPATH@<:@^A-Z@:>@*=/!d;$qt_sed_filter" $at_mfile`])
307   AC_SUBST([QT_INCPATH], [$at_cv_env_QT_INCPATH])
309   AC_SUBST([QT_CPPFLAGS], ["$at_cv_env_QT_DEFINES $at_cv_env_QT_INCPATH"])
311   # Find the LFLAGS of Qt (Should have been named LDFLAGS)
312   AC_CACHE_CHECK([for the LDFLAGS to use with Qt], [at_cv_env_QT_LDFLAGS],
313   [at_cv_env_QT_LDFLAGS=`sed "/^LFLAGS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" $at_mfile`])
314   AC_SUBST([QT_LFLAGS], [$at_cv_env_QT_LDFLAGS])
315   AC_SUBST([QT_LDFLAGS], [$at_cv_env_QT_LDFLAGS])
317   AC_MSG_CHECKING([whether host operating system is Darwin])
318   at_darwin="no"
319   case $host_os in
320     darwin*)
321       at_darwin="yes"
322       ;;
323   esac
324   AC_MSG_RESULT([$at_darwin])
326   # Find the LIBS of Qt.
327   AC_CACHE_CHECK([for the LIBS to use with Qt], [at_cv_env_QT_LIBS],
328   [at_cv_env_QT_LIBS=`sed "/^LIBS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" $at_mfile`
329    if test x$at_darwin = xyes; then
330      # Fix QT_LIBS: as of today Libtool (GNU Libtool 1.5.23a) doesn't handle
331      # -F properly. The "bug" has been fixed on 22 October 2006
332      # by Peter O'Gorman but we provide backward compatibility here.
333      at_cv_env_QT_LIBS=`echo "$at_cv_env_QT_LIBS" \
334                              | sed 's/^-F/-Wl,-F/;s/ -F/ -Wl,-F/g'`
335    fi
336   ])
337   AC_SUBST([QT_LIBS], [$at_cv_env_QT_LIBS])
339   cd ..
340   rm -rf conftest.dir
343 # _AT_TWEAK_PRO_FILE(QT_VAR, VALUE)
344 # ---------------------------
345 # @internal. Tweak the variable QT_VAR in the .pro.
346 # VALUE is an IFS-separated list of value and each value is rewritten
347 # as follows:
348 #   +value  => QT_VAR += value
349 #   -value  => QT_VAR -= value
350 #    value  => QT_VAR += value
351 AC_DEFUN([_AT_TWEAK_PRO_FILE],
352 [ # Tweak the value of $1 in the .pro file for $2.
354   qt_conf=''
355   for at_mod in $2; do
356     at_mod=`echo "$at_mod" | sed 's/^-//; tough
357                                   s/^+//; beef
358                                   :ough
359                                   s/^/$1 -= /;n
360                                   :eef
361                                   s/^/$1 += /'`
362     qt_conf="$qt_conf
363 $at_mod"
364   done
365   echo "$qt_conf" | sed 1d >>"$pro_file"