]> git.lyx.org Git - lyx.git/blob - config/qt.m4
Back to development
[lyx.git] / config / qt.m4
1 dnl check a particular libname
2 AC_DEFUN([QT_TRY_LINK],
3 [
4         SAVE_LIBS="$LIBS"
5         LIBS="$LIBS $1"
6         AC_LINK_IFELSE([AC_LANG_PROGRAM([[
7         #include <qglobal.h>
8         #include <qstring.h>
9                 ]], [[
10         QString s("mangle_failure");
11         #if (QT_VERSION < 400)
12         break_me_(\\\);
13         #endif
14         ]])],[qt_cv_libname=$1],[])
15         LIBS="$SAVE_LIBS"
16 ])
17
18 dnl check we can do a compile
19 AC_DEFUN([QT_CHECK_COMPILE],
20 [
21         AC_MSG_CHECKING([for Qt library name])
22
23         AC_CACHE_VAL(qt_cv_libname,
24         [
25                 SAVE_CXXFLAGS=$CXXFLAGS
26                 CXXFLAGS="$CXXFLAGS $QT_INCLUDES $QT_LDFLAGS"
27                 qt_corelibs="-lQtCore -lQtCore4"
28                 qt_guilibs="'-lQtCore -lQtGui' '-lQtCore4 -lQtGui4'"
29                 if test $USE_QT6 = "yes" ; then
30                     qt_corelibs="-lQt6Core -lQt6Core5Compat"
31                     qt_guilibs="-lQt6Core -lQt6Core5Compat -lQt6Concurrent -lQt6Gui -lQt6Svg -lQt6Widgets"
32                 elif test $USE_QT5 = "yes" ; then
33                     qt_corelibs="-lQt5Core"
34                     qt_guilibs="-lQt5Core -lQt5Concurrent -lQt5Gui -lQt5Svg -lQt5Widgets"
35                 fi
36                 for libname in $qt_corelibs '-framework QtCore'
37                 do
38                         QT_TRY_LINK($libname)
39                         if test -n "$qt_cv_libname"; then
40                                 QT_CORE_LIB="$qt_cv_libname"
41                                 break;
42                         fi
43                 done
44                 qt_cv_libname=
45                 for libname in $qt_guilibs \
46                                '-framework QtCore -framework QtConcurrent -framework QtSvg -framework QtWidgets -framework QtMacExtras -framework QtGui'\
47                                '-framework QtCore -framework QtGui'
48                 do
49                         QT_TRY_LINK($libname)
50                         if test -n "$qt_cv_libname"; then
51                                 break;
52                         fi
53                 done
54                 CXXFLAGS=$SAVE_CXXFLAGS
55         ])
56
57         if test -z "$qt_cv_libname"; then
58                 if test x$USE_QT6 = xyes ; then
59                         AC_MSG_RESULT([failed, retrying with Qt5])
60                 elif test x$USE_QT5 = xyes ; then
61                         AC_MSG_RESULT([failed, retrying with Qt4])
62                 else
63                         AC_MSG_RESULT([failed])
64                         AC_MSG_ERROR([cannot compile a simple Qt executable. Check you have the right \$QTDIR.])
65                 fi
66         else
67                 AC_MSG_RESULT([$qt_cv_libname])
68         fi
69 ])
70
71
72 AC_DEFUN([QT_FIND_TOOL],
73 [
74         $1=
75         qt_major=4
76         if test "x$USE_QT6" != "xno" ; then
77                 qt_major=6
78         elif test "x$USE_QT5" != "xno" ; then
79                 qt_major=5
80         fi
81         qt_ext="qt$qt_major"
82
83         if test -n "$qt_cv_bin" ; then
84                 AC_PATH_PROGS($1, [$2], [], $qt_cv_bin)
85         elif qtchooser -l 2>/dev/null | grep -q ^$qt_ext\$ >/dev/null ; then
86                 AC_PATH_PROG(qtc_path, qtchooser, [], [$PATH])
87                 AC_PATH_PROG($2_path, $2, [], [$PATH])
88                 qtc_path=`dirname "$qtc_path"`
89                 $2_path=`dirname "$$2_path"`
90                 if test "$qtc_path" = "$$2_path" ; then
91                         AC_CHECK_PROG($1, $2, [$2 -qt=$qt_ext],, [$PATH])
92                 fi
93         fi
94         if test -z "$$1"; then
95                 AC_CHECK_PROGS($1, [$2-$qt_ext $2$qt_major $2],[],$PATH)
96         fi
97         if test -z "$$1"; then
98                 AC_MSG_ERROR([cannot find $2 binary.])
99         fi
100 ])
101
102
103 dnl get Qt version we're using
104 AC_DEFUN([QT_GET_VERSION],
105 [
106         AC_CACHE_CHECK([Qt version],lyx_cv_qtversion,
107         [
108                 SAVE_CPPFLAGS=$CPPFLAGS
109                 CPPFLAGS="$CPPFLAGS $QT_INCLUDES"
110
111                 cat > conftest.$ac_ext <<EOF
112 #line __oline__ "configure"
113 #include "confdefs.h"
114 #include <qglobal.h>
115 "%%%"QT_VERSION_STR"%%%"
116 EOF
117                 lyx_cv_qtversion=`(eval "$ac_cpp conftest.$ac_ext") 2>&5 | \
118                         grep '^"%%%"'  2>/dev/null | \
119                         sed -e 's/"%%%"//g' -e 's/"//g'`
120                 rm -f conftest.$ac_ext
121                 CPPFLAGS=$SAVE_CPPFLAGS
122         ])
123
124         QTLIB_VERSION=$lyx_cv_qtversion
125         AC_SUBST(QTLIB_VERSION)
126 ])
127
128 dnl start here
129 AC_DEFUN([QT_DO_IT_ALL],
130 [
131         dnl this variable is precious
132         AC_ARG_VAR(QTDIR, [the place where the Qt files are, e.g. /usr/lib/qt4])
133
134         AC_ARG_WITH(qt-dir, [AS_HELP_STRING([--with-qt-dir], [where the root of Qt is installed])],
135                 [ qt_cv_dir=`eval echo "$withval"/` ])
136
137         AC_ARG_WITH(qt-includes, [AS_HELP_STRING([--with-qt-includes], [where the Qt includes are])],
138                 [ qt_cv_includes=`eval echo "$withval"` ])
139
140         AC_ARG_WITH(qt-libraries, [AS_HELP_STRING([--with-qt-libraries], [where the Qt library is installed])],
141                 [  qt_cv_libraries=`eval echo "$withval"` ])
142
143         dnl pay attention to $QTDIR unless overridden
144         if test -z "$qt_cv_dir"; then
145                 qt_cv_dir=$QTDIR
146         fi
147
148         dnl derive inc/lib if needed
149         if test -n "$qt_cv_dir"; then
150                 if test -z "$qt_cv_includes"; then
151                         qt_cv_includes=$qt_cv_dir/include
152                 fi
153                 if test -z "$qt_cv_libraries"; then
154                         qt_cv_libraries=$qt_cv_dir/lib
155                 fi
156         fi
157
158         dnl compute the binary dir too
159         if test -n "$qt_cv_dir"; then
160                 qt_cv_bin=$qt_cv_dir/bin
161         fi
162
163         dnl Preprocessor flags
164         QT_CPPFLAGS="-DQT_NO_STL -DQT_NO_KEYWORDS"
165         case ${host} in
166         *mingw*) QT_CPPFLAGS="-DQT_DLL $QT_CPPFLAGS";;
167         esac
168         AC_SUBST(QT_CPPFLAGS)
169
170         dnl Check if it possible to do a pkg-config
171         PKG_PROG_PKG_CONFIG
172         dnl Not possible with Qt6 (QTBUG-86080)
173         if test x$USE_QT6 = xno ; then
174             if test -n "$PKG_CONFIG" ; then
175                     QT_DO_PKG_CONFIG
176             fi
177             if test "$pkg_failed" != "no" ; then
178                     QT_DO_MANUAL_CONFIG
179             fi
180         else
181             QT6_QMAKE_CONFIG
182             if test -z "$QT_LIB"; then
183                     QT_DO_MANUAL_CONFIG
184             fi
185         fi
186
187         if test -z "$QT_LIB"; then
188           dnl Try again with Qt5 and then Qt4 if configuring for Qt6/5 fails
189           if test x$USE_QT6 = xyes ; then
190                 USE_QT6=no
191                 USE_QT5=yes
192                 AC_SUBST([USE_QT6])
193                 AC_SUBST([USE_QT5])
194                 if test -n "$PKG_CONFIG" ; then
195                   QT_DO_PKG_CONFIG
196                 fi
197                 if test "$pkg_failed" != "no" ; then
198                   QT_DO_MANUAL_CONFIG
199                 fi
200                 if test -z "$QT_LIB"; then
201                   AC_MSG_ERROR([cannot find qt libraries.])
202                 fi
203           elif test x$USE_QT5 = xyes ; then
204                 USE_QT6=no
205                 USE_QT5=no
206                 AC_SUBST([USE_QT6])
207                 AC_SUBST([USE_QT5])
208                 if test -n "$PKG_CONFIG" ; then
209                   QT_DO_PKG_CONFIG
210                 fi
211                 if test "$pkg_failed" != "no" ; then
212                   QT_DO_MANUAL_CONFIG
213                 fi
214                 if test -z "$QT_LIB"; then
215                   AC_MSG_ERROR([cannot find qt libraries.])
216                 fi
217           else
218                 AC_MSG_ERROR([cannot find qt libraries.])
219           fi
220         fi
221
222         dnl Check qt version
223         AS_VERSION_COMPARE($QTLIB_VERSION, $1,
224         [AC_MSG_ERROR([LyX requires at least version $1 of Qt. Only version $QTLIB_VERSION has been found.])
225         ])
226
227         save_CPPFLAGS=$CPPFLAGS
228         CPPFLAGS="$save_CPPFLAGS $QT_CORE_INCLUDES"
229         AC_CHECK_HEADER(QtGui/qtgui-config.h,
230           [lyx_qt5_config=QtGui/qtgui-config.h],
231           [lyx_qt5_config=qconfig.h],[-])
232         AC_CHECK_HEADER(QtGui/private/qtgui-config_p.h,
233           [lyx_qt6_config=QtGui/private/qtgui-config_p.h],
234           [lyx_qt6_config=qconfig.h],[-])
235         AC_MSG_CHECKING([whether Qt uses the X Window system])
236         if test x$USE_QT6 = xyes ; then
237           dnl FIXME: Check whether defining QPA_XCB makes sense with Qt6
238           AC_PREPROC_IFELSE([AC_LANG_SOURCE([
239             [#include <$lyx_qt6_config>]
240             [#if !defined(QT_FEATURE_xcb) || QT_FEATURE_xcb < 0]
241             [#error Fail]
242             [#endif]])],
243             [AC_MSG_RESULT(yes)
244              AC_DEFINE(QPA_XCB, 1, [Define if Qt uses the X Window System])],
245             [AC_MSG_RESULT(no)])
246         elif test x$USE_QT5 = xyes ; then
247           AC_EGREP_CPP(xcb,
248             [#include <$lyx_qt5_config>
249             QT_QPA_DEFAULT_PLATFORM_NAME],
250             [AC_MSG_RESULT(yes)
251              AC_DEFINE(QPA_XCB, 1, [Define if Qt uses the X Window System])],
252             [AC_MSG_RESULT(no)])
253         else
254           AC_PREPROC_IFELSE([AC_LANG_SOURCE([
255             [#include <qglobal.h>],
256             [#ifndef Q_WS_X11],
257             [#error Fail],
258             [#endif]])],
259             qt_use_x11=yes,
260             qt_use_x11=no)
261           AC_MSG_RESULT($qt_use_x11)
262           if test "x$qt_use_x11" = "xyes"; then
263             QT_LIB="$QT_LIB -lX11"
264           fi
265         fi
266         CPPFLAGS=$save_CPPFLAGS
267
268         QT_FIND_TOOL([QT_MOC], [moc])
269         QT_FIND_TOOL([QT_UIC], [uic])
270         QT_FIND_TOOL([QT_RCC], [rcc])
271
272         dnl Safety check
273         mocqtver=`$QT_MOC -v 2>&1 | sed -e 's/.*\([[0-9]]\.[[0-9]]*\.[[0-9]]\).*/\1/'`
274         if test "x$mocqtver" != "x$QTLIB_VERSION"; then
275                 LYX_WARNING([The found moc compiler is for Qt $mocqtver but the Qt library version is $QTLIB_VERSION.])
276         fi
277 ])
278
279 AC_DEFUN([QT_DO_PKG_CONFIG],
280 [
281         dnl tell pkg-config to look also in $qt_cv_dir/lib.
282         save_PKG_CONFIG_PATH=$PKG_CONFIG_PATH
283         if test -n "$qt_cv_dir" ; then
284           PKG_CONFIG_PATH=$qt_cv_dir/lib:$qt_cv_dir/lib/pkgconfig:$PKG_CONFIG_PATH
285           export PKG_CONFIG_PATH
286         fi
287         qt_corelibs="QtCore"
288         qt_guilibs="QtCore QtGui QtSvg"
289         if test "x$USE_QT5" != "xno" ; then
290                 qt_corelibs="Qt5Core"
291                 qt_guilibs="Qt5Core Qt5Concurrent Qt5Gui Qt5Svg Qt5Widgets"
292                 lyx_use_x11extras=false
293                 PKG_CHECK_EXISTS(Qt5X11Extras, [lyx_use_x11extras=true], [])
294                 if $lyx_use_x11extras; then
295                         qt_guilibs="$qt_guilibs Qt5X11Extras xcb"
296                         AC_DEFINE(HAVE_QT5_X11_EXTRAS, 1,
297                                 [Define if you have the Qt5X11Extras module])
298                 fi
299                 lyx_use_winextras=false
300                 PKG_CHECK_EXISTS(Qt5WinExtras, [lyx_use_winextras=true], [])
301                 if $lyx_use_winextras; then
302                         qt_guilibs="$qt_guilibs Qt5WinExtras"
303                 fi
304                 lyx_use_macextras=false
305                 PKG_CHECK_EXISTS(Qt5MacExtras, [lyx_use_macextras=true], [])
306                 if $lyx_use_macextras; then
307                         qt_guilibs="$qt_guilibs Qt5MacExtras"
308                 fi
309         fi
310         PKG_CHECK_MODULES(QT_CORE, $qt_corelibs,,[:])
311         if test "$pkg_failed" = "no" ; then
312                 QT_CORE_INCLUDES=$QT_CORE_CFLAGS
313                 AC_SUBST(QT_CORE_INCLUDES)
314                 QT_CORE_LDFLAGS=`$PKG_CONFIG --libs-only-L $qt_corelibs`
315                 AC_SUBST(QT_CORE_LDFLAGS)
316                 QT_CORE_LIB=`$PKG_CONFIG --libs-only-l $qt_corelibs`
317                 AC_SUBST(QT_CORE_LIB)
318         fi
319         PKG_CHECK_MODULES(QT_FRONTEND, $qt_guilibs,,[:])
320         if test "$pkg_failed" = "no" ; then
321                 QT_INCLUDES=$QT_FRONTEND_CFLAGS
322                 dnl QT_LDFLAGS=$QT_FRONTEND_LIBS
323                 QT_LDFLAGS=`$PKG_CONFIG --libs-only-L $qt_guilibs`
324                 AC_SUBST(QT_INCLUDES)
325                 AC_SUBST(QT_LDFLAGS)
326                 QTLIB_VERSION=`$PKG_CONFIG --modversion $qt_corelibs`
327                 AC_SUBST(QTLIB_VERSION)
328                 QT_LIB=`$PKG_CONFIG --libs-only-l $qt_guilibs`
329                 AC_SUBST(QT_LIB)
330                 dnl LIBS="$LIBS `$PKG_CONFIG --libs-only-other $qt_guilibs`"
331         fi
332         PKG_CONFIG_PATH=$save_PKG_CONFIG_PATH
333         dnl Actually, the values of QT_LIB and QT_CORE_LIB can be completely
334         dnl wrong on OS X, where everything goes to --libs-only-other.
335         dnl As a quick workaround, let us assign better values. A better patch
336         dnl exists for next cycle.
337         QT_CORE_LIB=$QT_CORE_LIBS
338         QT_CORE_LDFLAGS=
339         QT_LIB=$QT_FRONTEND_LIBS
340         QT_LDFLAGS=
341 ])
342
343 AC_DEFUN([QT_DO_MANUAL_CONFIG],
344 [
345         dnl Check for X libraries
346         case ${host} in
347         *mingw*) ;;
348         *) \
349           AC_PATH_X \
350           AC_PATH_XTRA \
351         ;;
352         esac
353         case $have_x in
354             yes) LIBS="$X_PRE_LIBS $LIBS $X_LIBS -lX11 $X_EXTRA_LIBS"
355                  CPPFLAGS="$CPPFLAGS $X_CFLAGS";;
356              no) AC_MSG_ERROR([cannot find X window libraries and/or headers.]);;
357         disable) ;;
358         esac
359
360         dnl flags for compilation
361         QT_INCLUDES=
362         QT_LDFLAGS=
363         QT_CORE_INCLUDES=
364         QT_CORE_LDFLAGS=
365         if test -n "$qt_cv_includes"; then
366                 QT_INCLUDES="-I$qt_cv_includes"
367                 for i in Qt QtCore QtGui QtWidgets QtSvg QtConcurrent QtMacExtras; do
368                         QT_INCLUDES="$QT_INCLUDES -I$qt_cv_includes/$i"
369                 done
370                 QT_CORE_INCLUDES="-I$qt_cv_includes -I$qt_cv_includes/QtCore"
371         fi
372         case "$qt_cv_libraries" in
373         *framework*)
374                 QT_LDFLAGS="-F$qt_cv_libraries"
375                 QT_CORE_LDFLAGS="-F$qt_cv_libraries"
376                 ;;
377         "")
378                 ;;
379         *)
380                 QT_LDFLAGS="-L$qt_cv_libraries"
381                 QT_CORE_LDFLAGS="-L$qt_cv_libraries"
382                 ;;
383         esac
384         AC_SUBST(QT_INCLUDES)
385         AC_SUBST(QT_CORE_INCLUDES)
386         AC_SUBST(QT_LDFLAGS)
387         AC_SUBST(QT_CORE_LDFLAGS)
388
389         QT_CHECK_COMPILE
390
391         QT_LIB=$qt_cv_libname;
392         AC_SUBST(QT_LIB)
393         AC_SUBST(QT_CORE_LIB)
394
395         if test -n "$qt_cv_libname"; then
396                 QT_GET_VERSION
397         fi
398 ])
399
400 AC_DEFUN([QT6_QMAKE_CONFIG],
401 [
402         dnl Use first suitable qmake in PATH
403         AC_CHECK_PROGS([QT_QMAKE], [qmake-qt6 qmake6], [qmake], $PATH)
404         AC_MSG_CHECKING([for Qt6])
405         qtver=`$QT_QMAKE -v | grep -o "Qt version ."`
406         if test "$qtver" = "Qt version 6"; then
407             dnl Use a .pro file for getting qmake's variables
408             lyx_test_qt_dir=`mktemp -d`
409             lyx_test_qt_pro="$lyx_test_qt_dir/test.pro"
410             lyx_test_qt_mak="$lyx_test_qt_dir/Makefile"
411             cat > $lyx_test_qt_pro << EOF1
412 qtHaveModule(core):             QT += core
413 qtHaveModule(core5compat):      QT += core5compat
414 percent.target = %
415 percent.commands = @echo -n "\$(\$(@))\ "
416 QMAKE_EXTRA_TARGETS += percent
417 EOF1
418             $QT_QMAKE $lyx_test_qt_pro -o $lyx_test_qt_mak 1>/dev/null 2>&1
419             QT_CORE_INCLUDES=`cd $lyx_test_qt_dir; make -s -f $lyx_test_qt_mak INCPATH | sed 's/-I\. //g'`
420             qt_corelibs=`cd $lyx_test_qt_dir; make -s -f $lyx_test_qt_mak LIBS`
421             QT_CORE_LDFLAGS=`echo $qt_corelibs | tr ' ' '\n' | grep -e "^-L" | tr '\n' ' '`
422             if test -z "$QT_CORE_LDFLAGS"; then
423                 QT_CORE_LDFLAGS="-L`$QT_QMAKE -query QT_INSTALL_LIBS`"
424                 QT_CORE_LIB="$qt_corelibs"
425             else
426                 QT_CORE_LIB=`echo $qt_corelibs | tr ' ' '\n' | grep -e "^-l" | tr '\n' ' '`
427             fi
428             if test -z "$QT_CORE_LIB"; then
429                 AC_MSG_RESULT(no)
430             else
431                 AC_SUBST(QT_CORE_INCLUDES)
432                 AC_SUBST(QT_CORE_LDFLAGS)
433                 AC_SUBST(QT_CORE_LIB)
434                 cat > $lyx_test_qt_pro << EOF2
435 qtHaveModule(core):             QT += core
436 qtHaveModule(core5compat):      QT += core5compat
437 qtHaveModule(concurrent):       QT += concurrent
438 qtHaveModule(gui):              QT += gui
439 qtHaveModule(svg):              QT += svg
440 qtHaveModule(widgets):          QT += widgets
441 percent.target = %
442 percent.commands = @echo -n "\$(\$(@))\ "
443 QMAKE_EXTRA_TARGETS += percent
444 EOF2
445                 $QT_QMAKE $lyx_test_qt_pro -o $lyx_test_qt_mak 1>/dev/null 2>&1
446                 QT_INCLUDES=`cd $lyx_test_qt_dir; make -s -f $lyx_test_qt_mak INCPATH | sed 's/-I\. //g'`
447                 qt_guilibs=`cd $lyx_test_qt_dir; make -s -f $lyx_test_qt_mak LIBS`
448                 QT_LDFLAGS=`echo $qt_guilibs | tr ' ' '\n' | grep -e "^-L" | tr '\n' ' '`
449                 if test -z "$QT_LDFLAGS"; then
450                     QT_LDFLAGS="-L`$QT_QMAKE -query QT_INSTALL_LIBS`"
451                     QT_LIB="$qt_guilibs"
452                 else
453                     QT_LIB=`echo $qt_guilibs | tr ' ' '\n' | grep -e "^-l" | tr '\n' ' '`
454                 fi
455                 QTLIB_VERSION=`$QT_QMAKE -v | grep "Qt version" | sed -e 's/.*\([[0-9]]\.[[0-9]]*\.[[0-9]]\).*/\1/'`
456                 if test -z "$QT_LIB"; then
457                     AC_MSG_RESULT(no)
458                 else
459                     AC_MSG_RESULT(yes)
460                     AC_SUBST(QT_INCLUDES)
461                     AC_SUBST(QT_LDFLAGS)
462                     AC_SUBST(QT_LIB)
463                     AC_SUBST(QTLIB_VERSION)
464                 fi
465             fi
466             rm $lyx_test_qt_pro $lyx_test_qt_mak $lyx_test_qt_dir/.qmake.stash
467             rmdir $lyx_test_qt_dir
468         else
469             AC_MSG_RESULT(no)
470         fi
471 ])