Workaround for libtool's lake of support of -F on Darwin.
[autotroll.git] / build-aux / autotroll.m4
blobe14bdb781d2e141bd680a4e467e815ec1368a3b6
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 =  lcdrange.cpp lcdrange.moc.cpp lcdrange.h main.cpp
57 # lcdrange_CXXFLAGS = $(AM_CXXFLAGS) $(QT_CXXFLAGS)
58 # lcdrange_CPPFLAGS = $(AM_CPPFLAGS) $(QT_CPPFLAGS)
59 # lcdrange_LDFLAGS  = $(QT_LDFLAGS) $(LDFLAGS)
60 # lcdrange_LDADD    = $(QT_LIBS) $(LDADD)
62 # BUILT_SOURCES = lcdrange.moc.cpp
63 # -------------------------------------------------------------------------
65 m4_pattern_forbid([^AT_])dnl
66 m4_pattern_forbid([^_AT_])dnl
68 # AT_WITH_QT([QT_modules], [QT_config], [QT_misc])
69 # ------------------------------------------------
70 # Enable Qt support and add an option --with-qt to the configure script.
72 # The QT_modules argument is optional and defines extra modules to enable or
73 # disable (it's equivalent to the QT variable in .pro files). Modules can be
74 # specified as follows:
76 # AT_WITH_QT   => No argument -> No QT value.
77 #                                Qmake sets it to "core gui" by default.
78 # AT_WITH_QT([xml])   => QT += xml
79 # AT_WITH_QT([+xml])  => QT += xml
80 # AT_WITH_QT([-gui])  => QT -= gui
81 # AT_WITH_QT([xml -gui +sql svg])  => QT += xml sql svg
82 #                                     QT -= gui
84 # The QT_config argument is also optional and follows the same convention as
85 # QT_modules. Instead of changing the QT variable, it changes the CONFIG
86 # variable, which is used to tweak configuration and compiler options.
88 # The last argument, QT_misc (also optional) will be copied as-is the .pro
89 # file used to guess how to compile Qt apps. You may use it to further tweak
90 # the build process of Qt apps if tweaking the QT or CONFIG variables isn't
91 # enough for you.
92 AC_DEFUN([AT_WITH_QT],
93 [ AC_REQUIRE([AC_CANONICAL_HOST])
94   AC_REQUIRE([AC_CANONICAL_BUILD])
95   AC_REQUIRE([AC_PROG_CXX])
97   test x"$TROLL" != x && echo 'ViM rox emacs.'
99 dnl Memo: AC_ARG_WITH(package, help-string, [if-given], [if-not-given])
100   AC_ARG_WITH([qt],
101               [AS_HELP_STRING([--with-qt],
102                  [Path to Qt @<:@Look in PATH and /usr/local/Trolltech@:>@])],
103               [QT_PATH=$withval], [QT_PATH=])
105   # Find Qt.
106   if test -d /usr/local/Trolltech; then
107     # Try to find the latest version.
108     tmp_qt_paths=`echo /usr/local/Trolltech/*/bin | tr ' ' '\n' | sort -nr \
109                                               | xargs | sed 's/  */:/g'`
110   fi
112   # Find qmake.
113   AC_PATH_PROGS([QMAKE], [qmake], [missing], [$QT_DIR:$QT_PATH:$PATH:$tmp_qt_paths])
114   if test x"$QMAKE" = xmissing; then
115     AC_MSG_ERROR([Cannot find qmake in your PATH. Try using --with-qt.])
116   fi
118   # Find moc (Meta Object Compiler).
119   AC_PATH_PROGS([MOC], [moc], [missing], [$QT_PATH:$PATH:$tmp_qt_paths])
120   if test x"$MOC" = xmissing; then
121     AC_MSG_ERROR([Cannot find moc (Meta Object Compiler) in your PATH. Try using --with-qt.])
122   fi
124   # Find uic (User Interface Compiler).
125   AC_PATH_PROGS([UIC], [uic], [missing], [$QT_PATH:$PATH:$tmp_qt_paths])
126   if test x"$UIC" = xmissing; then
127     AC_MSG_ERROR([Cannot find uic (User Interface Compiler) in your PATH. Try using --with-qt.])
128   fi
130   # If we don't know the path to Qt, guess it from the path to qmake.
131   if test x"$QT_PATH" = x; then
132     QT_PATH=`dirname "$QMAKE"`
133   fi
134   if test x"$QT_PATH" = x; then
135     AC_MSG_ERROR([Cannot find the path to your Qt install. Use --with-qt.])
136   fi
137   AC_SUBST([QT_PATH])
139   # Get ready to build a test-app with Qt.
140   mkdir conftest.dir
141   cd conftest.dir
142   cat >conftest.h <<_ASEOF
143 #include <QObject>
145 class Foo: public QObject
147   Q_OBJECT;
148 public:
149   Foo();
150   ~Foo() {}
151 public slots:
152   void setValue(int value);
153 signals:
154   void valueChanged(int newValue);
155 private:
156   int value_;
158 _ASEOF
160   cat >conftest.cpp <<_ASEOF
161 #include "conftest.h"
162 Foo::Foo()
163   : value_ (42)
165   connect(this, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
168 void Foo::setValue(int value)
170   value_ = value;
173 int main()
175   Foo f;
177 _ASEOF
178   if $QMAKE -project; then :; else
179     AC_MSG_ERROR([Calling $QMAKE -project failed.])
180   fi
182   # Find the .pro file generated by qmake.
183   pro_file='conftest.dir.pro'
184   test -f $pro_file || pro_file=`echo *.pro`
185   if test -f "$pro_file"; then :; else
186     AC_MSG_ERROR([Can't find the .pro file generated by Qmake.])
187   fi
189 dnl Tweak the value of QT in the .pro if have been the 1st arg.
190 m4_ifval([$1], [_AT_TWEAK_PRO_FILE([QT], [$1])])
192 dnl Tweak the value of CONFIG in the .pro if have been given a 2nd arg.
193 m4_ifval([$2], [_AT_TWEAK_PRO_FILE([CONFIG], [$2])])
195 m4_ifval([$3],
196 [ # Add the extra-settings the user wants to set in the .pro
197   echo "$3" >>"$pro_file"
200   echo "$as_me:$LINENO: Invoking $QMAKE on $pro_file" >&AS_MESSAGE_LOG_FD
201   sed 's/^/| /' "$pro_file" >&AS_MESSAGE_LOG_FD
203   if $QMAKE; then :; else
204     AC_MSG_ERROR([Calling $QMAKE failed.])
205   fi
206   # Try to compile a simple Qt app.
207   AC_CACHE_CHECK([whether we can build a simple Qt app], [at_cv_qt_build],
208   [at_cv_qt_build=ko
209   : ${MAKE=make}
211   if $MAKE >&AS_MESSAGE_LOG_FD 2>&1; then
212     at_cv_qt_build='ok, looks like Qt 4'
213   else
214     echo "$as_me:$LINENO: Build failed, trying to #include <qobject.h> \
215 instead" >&AS_MESSAGE_LOG_FD
216     sed 's/<QObject>/<qobject.h>/' conftest.h > tmp.h
217     mv tmp.h conftest.h
218     if $MAKE >&AS_MESSAGE_LOG_FD 2>&1; then
219       at_cv_qt_build='ok, looks like Qt 3'
220     else
221       at_cv_qt_build=ko
222       echo "$as_me:$LINENO: failed program was:" >&AS_MESSAGE_LOG_FD
223       sed 's/^/| /' conftest.h >&AS_MESSAGE_LOG_FD
224       echo "$as_me:$LINENO: failed program was:" >&AS_MESSAGE_LOG_FD
225       sed 's/^/| /' conftest.cpp >&AS_MESSAGE_LOG_FD
226     fi
227   fi
228   ])dnl end: AC_CACHE_CHECK(at_cv_qt_build)
230   if test x"$at_cv_qt_build" = xko; then
231     AC_MSG_ERROR([Cannot build a test Qt program])
232   fi
233   QT_VERSION_MAJOR=`echo "$at_cv_qt_build" | sed 's/^[^0-9]*//'`
234   AC_SUBST([QT_VERSION_MAJOR])
236   # This sed filter is applied after an expression of the form: /^FOO.*=/!d;
237   # It starts by removing the beginning of the line, removing references to
238   # SUBLIBS, removing unnecessary whitespaces at the beginning, and prefixes
239   # all variable uses by QT_.
240   qt_sed_filter='s///;
241                  s/$(SUBLIBS)//g;
242                  s/^ *//;
243                  s/\$(\(@<:@A-Z_@:>@@<:@A-Z_@:>@*\))/$(QT_\1)/g'
245   # Find the DEFINES of Qt (should have been named CPPFLAGS).
246   AC_CACHE_CHECK([for the DEFINES to use with Qt], [at_cv_env_QT_DEFINES],
247   [at_cv_env_QT_DEFINES=`sed "/^DEFINES@<:@^A-Z@:>@*=/!d;$qt_sed_filter" Makefile`])
248   AC_SUBST([QT_DEFINES], [$at_cv_env_QT_DEFINES])
250   # Find the CFLAGS of Qt (We can use Qt in C?!)
251   AC_CACHE_CHECK([for the CFLAGS to use with Qt], [at_cv_env_QT_CFLAGS],
252   [at_cv_env_QT_CFLAGS=`sed "/^CFLAGS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" Makefile`])
253   AC_SUBST([QT_CFLAGS], [$at_cv_env_QT_CFLAGS])
255   # Find the CXXFLAGS of Qt.
256   AC_CACHE_CHECK([for the CXXFLAGS to use with Qt], [at_cv_env_QT_CXXFLAGS],
257   [at_cv_env_QT_CXXFLAGS=`sed "/^CXXFLAGS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" Makefile`])
258   AC_SUBST([QT_CXXFLAGS], [$at_cv_env_QT_CXXFLAGS])
260   # Find the INCPATH of Qt.
261   AC_CACHE_CHECK([for the INCPATH to use with Qt], [at_cv_env_QT_INCPATH],
262   [at_cv_env_QT_INCPATH=`sed "/^INCPATH@<:@^A-Z@:>@*=/!d;$qt_sed_filter" Makefile`])
263   AC_SUBST([QT_INCPATH], [$at_cv_env_QT_INCPATH])
265   AC_SUBST([QT_CPPFLAGS], ["$at_cv_env_QT_DEFINES $at_cv_env_QT_INCPATH"])
267   # Find the LFLAGS of Qt (Should have been named LDFLAGS)
268   AC_CACHE_CHECK([for the LDFLAGS to use with Qt], [at_cv_env_QT_LDFLAGS],
269   [at_cv_env_QT_LDFLAGS=`sed "/^LFLAGS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" Makefile`])
270   AC_SUBST([QT_LFLAGS], [$at_cv_env_QT_LDFLAGS])
271   AC_SUBST([QT_LDFLAGS], [$at_cv_env_QT_LDFLAGS])
273   AC_MSG_CHECKING([whether host operating system is Darwin])
274   at_darwin="no"
275   case $host_os in
276     darwin*)
277       at_darwin="yes"
278       ;;
279   esac
280   AC_MSG_RESULT([$at_darwin])
282   # Find the LIBS of Qt.
283   AC_CACHE_CHECK([for the LIBS to use with Qt], [at_cv_env_QT_LIBS],
284   [at_cv_env_QT_LIBS=`sed "/^LIBS@<:@^A-Z@:>@*=/!d;$qt_sed_filter" Makefile`
285    if test x$at_darwin = xyes; then
286      # Fix QT_LIBS: as of today Libtool (GNU Libtool 1.5.23a) doesn't handle
287      # -F properly. The "bug" has been fixed on 22 October 2006
288      # by Peter O'Gorman but we provide backward compatibility here.
289      at_cv_env_QT_LIBS=`echo "$at_cv_env_QT_LIBS" \
290                              | sed 's/^-F/-Wl,-F/;s/ -F/ -Wl,-F/g'`
291    fi
292   ])
293   AC_SUBST([QT_LIBS], [$at_cv_env_QT_LIBS])
295   cd ..
296   rm -rf conftest.dir
299 # _AT_TWEAK_PRO_FILE(QT_VAR, VALUE)
300 # ---------------------------
301 # @internal. Tweak the variable QT_VAR in the .pro.
302 # VALUE is an IFS-separated list of value and each value is rewritten
303 # as follows:
304 #   +value  => QT_VAR += value
305 #   -value  => QT_VAR -= value
306 #    value  => QT_VAR += value
307 AC_DEFUN([_AT_TWEAK_PRO_FILE],
308 [ # Tweak the value of $1 in the .pro file for $2.
310   qt_conf=''
311   for at_mod in $2; do
312     at_mod=`echo "$at_mod" | sed 's/^-//; tough
313                                   s/^+//; beef
314                                   :ough
315                                   s/^/$1 -= /;n
316                                   :eef
317                                   s/^/$1 += /'`
318     qt_conf="$qt_conf
319 $at_mod"
320   done
321   echo "$qt_conf" | sed 1d >>"$pro_file"