]> git.lyx.org Git - lyx.git/blob - development/cmake/ConfigureChecks.cmake
Amend(2) 580dea63: get rid of Qt4 code also in cmake build
[lyx.git] / development / cmake / ConfigureChecks.cmake
1 # This file is part of LyX, the document processor.
2 # Licence details can be found in the file COPYING.
3 #
4 # Copyright (c) 2006, Peter Kümmel, <syntheticpp@gmx.net>
5 #
6
7 include(CheckIncludeFile)
8 include(CheckIncludeFileCXX)
9 include(CheckIncludeFiles)
10 include(CheckSymbolExists)
11 include(CheckFunctionExists)
12 include(CheckLibraryExists)
13 include(CheckTypeSize)
14 include(CheckCXXSourceCompiles)
15 include(CheckCXXSourceRuns)
16 include(MacroBoolTo01)
17 include(TestBigEndian)
18
19 test_big_endian(WORDS_BIGENDIAN)
20
21 #check_include_file_cxx(istream HAVE_ISTREAM)
22 #check_include_file_cxx(ostream HAVE_OSTREAM)
23 #check_include_file_cxx(sstream HAVE_SSTREAM)
24 #check_include_file_cxx(ios HAVE_IOS)
25 #check_include_file_cxx(locale HAVE_LOCALE)
26
27 # defines will be written to configIncludes.h
28 set(Include_Defines)
29 foreach(_h_file aspell.h aspell/aspell.h limits.h locale.h
30         stdlib.h sys/stat.h sys/time.h sys/types.h sys/utime.h
31         sys/socket.h unistd.h inttypes.h utime.h string.h argz.h)
32         string(REGEX REPLACE "[/\\.]" "_" _hf ${_h_file})
33         string(TOUPPER ${_hf} _HF)
34         check_include_files(${_h_file} HAVE_${_HF})
35         set(Include_Defines "${Include_Defines}#cmakedefine HAVE_${_HF} 1\n")
36 endforeach()
37 check_include_file_cxx(regex HAVE_REGEX)
38 set(Include_Defines "${Include_Defines}#cmakedefine HAVE_REGEX 1\n")
39 configure_file(${LYX_CMAKE_DIR}/configIncludes.cmake ${TOP_BINARY_DIR}/configIncludes.h.cmake)
40 configure_file(${TOP_BINARY_DIR}/configIncludes.h.cmake ${TOP_BINARY_DIR}/configIncludes.h)
41
42 # defines will be written to configFunctions.h
43 set(Function_Defines)
44 foreach(_f alloca __argz_count __argz_next __argz_stringify
45         chmod close _close dcgettext fcntl fork __fsetlocking
46         getcwd getegid getgid getpid _getpid gettext getuid lstat lockf mempcpy mkdir _mkdir
47         mkfifo open _open pclose _pclose popen _popen putenv readlink
48         setenv setlocale strcasecmp stpcpy strdup strerror strtoul tsearch unsetenv wcslen)
49   string(TOUPPER ${_f} _UF)
50   check_function_exists(${_f} HAVE_${_UF})
51   set(Function_Defines "${Function_Defines}#cmakedefine HAVE_${_UF} 1\n")
52 endforeach()
53 configure_file(${LYX_CMAKE_DIR}/configFunctions.cmake ${TOP_BINARY_DIR}/configFunctions.h.cmake)
54 configure_file(${TOP_BINARY_DIR}/configFunctions.h.cmake ${TOP_BINARY_DIR}/configFunctions.h)
55
56 check_symbol_exists(alloca "malloc.h" HAVE_SYMBOL_ALLOCA)
57 check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF)
58 check_symbol_exists(wprintf "stdio.h" HAVE_WPRINTF)
59 check_symbol_exists(snprintf "stdio.h" HAVE_SNPRINTF)
60 check_symbol_exists(printf "stdio.h" HAVE_POSIX_PRINTF)
61 check_symbol_exists(pid_t "sys/types.h" HAVE_PID_T)
62 check_symbol_exists(intmax_t "inttypes.h" HAVE_INTTYPES_H_WITH_UINTMAX)
63 check_symbol_exists(uintmax_t "stdint.h" HAVE_STDINT_H_WITH_UINTMAX)
64 check_symbol_exists(LC_MESSAGES "locale.h" HAVE_LC_MESSAGES)
65 check_symbol_exists(PATH_MAX "limits.h" HAVE_DEF_PATH_MAX)
66
67 check_type_size(intmax_t HAVE_INTMAX_T)
68 macro_bool_to_01(HAVE_UINTMAX_T HAVE_STDINT_H_WITH_UINTMAX)
69
70 check_type_size("long double"  HAVE_LONG_DOUBLE)
71 check_type_size("long long"  HAVE_LONG_LONG_INT)
72 check_type_size(wchar_t HAVE_WCHAR_T)
73 check_type_size(wint_t  HAVE_WINT_T)
74
75 if(HUNSPELL_FOUND)
76   # check whether hunspell C++ (rather than C) ABI is provided
77   set(HunspellTestFile "${CMAKE_BINARY_DIR}/hunspelltest.cpp")
78   file(WRITE "${HunspellTestFile}"
79   "
80   #include <hunspell.hxx>
81
82   int main()
83   {
84     Hunspell sp(\"foo\", \"bar\");
85     int i = sp.stem(\"test\").size();
86   return(0);
87   }
88   "
89   )
90
91 # The trick with faking the link command (see the else block) does not work
92 # with XCode (350a9daf).
93 if(APPLE OR LYX_EXTERNAL_HUNSPELL)
94   try_compile(HAVE_HUNSPELL_CXXABI
95     "${CMAKE_BINARY_DIR}"
96     "${HunspellTestFile}"
97     CMAKE_FLAGS
98       "-DINCLUDE_DIRECTORIES:STRING=${HUNSPELL_INCLUDE_DIR}"
99     LINK_LIBRARIES ${HUNSPELL_LIBRARY}
100     OUTPUT_VARIABLE  LOG2)
101 else()
102   try_compile(HAVE_HUNSPELL_CXXABI
103     "${CMAKE_BINARY_DIR}"
104     "${HunspellTestFile}"
105     CMAKE_FLAGS
106       "-DINCLUDE_DIRECTORIES:STRING=${HUNSPELL_INCLUDE_DIR}"
107       # At this point, ../lib/libhunspell.a has not been built so we
108       # cannot complete the linking.
109       "-DCMAKE_CXX_LINK_EXECUTABLE='${CMAKE_COMMAD} echo dummy (fake) link command since libhunspell.a not built yet.'"
110     OUTPUT_VARIABLE  LOG2)
111 endif()
112
113   message(STATUS "HAVE_HUNSPELL_CXXABI = ${HAVE_HUNSPELL_CXXABI}")
114   #message(STATUS "LOG2 = ${LOG2}")
115   if(LYX_EXTERNAL_HUNSPELL AND (LYX_STDLIB_DEBUG OR LYX_DEBUG_GLIBC OR LYX_DEBUG_GLIBC_PEDANTIC) AND HAVE_HUNSPELL_CXXABI)
116     message(FATAL_ERROR "Compiling LyX with stdlib-debug and system hunspell libraries may lead to crashes. Consider using '-DLYX_STDLIB_DEBUG=OFF -DLYX_DEBUG_GLIBC=OFF -DLYX_DEBUG_GLIBC_PEDANTIC=OFF' or -DLYX_EXTERNAL_HUNSPELL=OFF.")
117   endif()
118 endif()
119
120 #check_cxx_source_compiles(
121 #       "
122 #       #include <algorithm>
123 #       using std::count;
124 #       int countChar(char * b, char * e, char const c)
125 #       {
126 #               return count(b, e, c);
127 #       }
128 #       int main(){return 0;}
129 #       "
130 #HAVE_STD_COUNT)
131
132 #check_cxx_source_compiles(
133 #       "
134 #       #include <cctype>
135 #       using std::tolower;
136 #       int main(){return 0;}
137 #       "
138 #CXX_GLOBAL_CSTD)
139
140 check_cxx_source_compiles(
141         "
142         #include <iconv.h>
143         // this declaration will fail when there already exists a non const char** version which returns size_t
144         double iconv(iconv_t cd,  char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
145         int main() { return 0; }
146         "
147 HAVE_ICONV_CONST)
148
149 check_cxx_source_compiles(
150         "
151         int i[ ( sizeof(wchar_t)==2 ? 1 : -1 ) ];
152         int main(){return 0;}
153         "
154 SIZEOF_WCHAR_T_IS_2)
155
156 check_cxx_source_compiles(
157         "
158         int i[ ( sizeof(wchar_t)==4 ? 1 : -1 ) ];
159         int main(){return 0;}
160         "
161 SIZEOF_WCHAR_T_IS_4)
162
163 check_cxx_source_compiles(
164         "
165         int i[ ( sizeof(long long)>sizeof(long) ? 1 : -1 ) ];
166         int main(){return 0;}
167         "
168 SIZEOF_LONG_LONG_GREATER_THAN_SIZEOF_LONG)
169
170 if(LYX_DISABLE_CALLSTACK_PRINTING)
171   set(LYX_CALLSTACK_PRINTING OFF CACHE BOOL "Print callstack when crashing")
172 else()
173   check_cxx_source_compiles(
174         "
175         #include <execinfo.h>
176         #include <cxxabi.h>
177         int main() {
178                 void* array[200];
179                 size_t size = backtrace(array, 200);
180                 backtrace_symbols(array, size);
181                 int status = 0;
182                 abi::__cxa_demangle(\"abcd\", 0, 0, &status);
183         }
184         "
185   LYX_CALLSTACK_PRINTING)
186 endif()
187
188 # Check whether STL is libstdc++
189 check_cxx_source_compiles(
190         "
191         #include <vector>
192         int main() {
193         #if ! defined(__GLIBCXX__) && ! defined(__GLIBCPP__)
194                 this is not libstdc++
195         #endif
196                 return(0);
197         }
198         "
199 lyx_cv_lib_stdcxx)
200
201 # Check whether STL is libstdc++ with C++11 ABI
202 check_cxx_source_compiles(
203         "
204         #include <vector>
205         int main() {
206         #if ! defined(_GLIBCXX_USE_CXX11_ABI) || ! _GLIBCXX_USE_CXX11_ABI
207                 this is not libstdc++ using the C++11 ABI
208         #endif
209                 return(0);
210         }
211         "
212 USE_GLIBCXX_CXX11_ABI)
213
214 check_cxx_source_compiles(
215         "
216         #ifndef __clang__
217                 this is not clang
218         #endif
219         int main() {
220           return(0);
221         }
222         "
223 lyx_cv_prog_clang)
224
225 check_cxx_source_compiles(
226   "
227   #include <memory>
228   using std::make_unique;
229   int main() {
230     return(0);
231   }
232   "
233 HAVE_DEF_MAKE_UNIQUE)
234
235 check_cxx_source_compiles(
236   "
237   #include <mutex>
238   static std::once_flag flag;
239   int main() {
240     std::call_once(flag, [](){ return; });
241     return(0);
242   }
243   "
244 LYX_USE_STD_CALL_ONCE)
245
246 if (ENCHANT_FOUND)
247   set(CMAKE_REQUIRED_INCLUDES ${ENCHANT_INCLUDE_DIR})
248   set(CMAKE_REQUIRED_LIBRARIES ${ENCHANT_LIBRARY})
249   # Check, whether enchant is version 2.x at least
250   check_cxx_source_compiles(
251     "
252     #include <enchant++.h>
253     enchant::Broker broker;
254     int main() {
255       return(0);
256     }
257     "
258   HAVE_ENCHANT2)
259   if (HAVE_ENCHANT2)
260     message(STATUS "ENCHANT2 found")
261   endif()
262 endif()
263
264 set(USE_LLVM_LIBCPP)
265 set(STD_STRING_USES_COW)
266 set(USE_GLIBCXX_CXX11_ABI)
267 if(lyx_cv_lib_stdcxx)
268   if(NOT USE_GLIBCXX_CXX11_ABI)
269     set(STD_STRING_USES_COW 1)
270   endif()
271 else()
272   if(lyx_cv_prog_clang)
273     # use libc++ provided by llvm instead of GNU libstdc++
274     set(USE_LLVM_LIBCPP 1)
275   endif()
276 endif()
277
278 set(QPA_XCB)
279 set(HAVE_QT5_X11_EXTRAS)
280 set(HAVE_QT6_X11_EXTRAS)
281 if (LYX_USE_QT MATCHES "QT5|QT6")
282   if (LYX_USE_QT MATCHES "QT5")
283     set(QtVal Qt5)
284   else()
285     set(QtVal Qt6)
286   endif()
287   set(CMAKE_REQUIRED_INCLUDES ${${QtVal}Core_INCLUDE_DIRS})
288   set(CMAKE_REQUIRED_FLAGS)
289   #message(STATUS "${QtVal}Core_INCLUDE_DIRS = ${${QtVal}Core_INCLUDE_DIRS}")
290   check_include_file_cxx(QtGui/qtgui-config.h HAVE_QTGUI_CONFIG_H)
291   if (HAVE_QTGUI_CONFIG_H)
292     set(lyx_qt_config "QtGui/qtgui-config.h")
293   else()
294     set(lyx_qt_config "QtCore/qconfig.h")
295   endif()
296   if(WIN32)
297     set(QT_USES_X11 OFF CACHE BOOL "Win32 compiled without X11")
298     # The try_run for minngw would not work here anyway
299   else()
300     check_cxx_source_runs(
301       "
302       #include <${lyx_qt_config}>
303       #include <string>
304       using namespace std;
305       string a(QT_QPA_DEFAULT_PLATFORM_NAME);
306       int main(int argc, char **argv)
307       {
308         if (a.compare(\"xcb\") == 0)
309           return(0);
310         else
311           return 1;
312       }
313       "
314       QT_USES_X11)
315     if(QT_USES_X11 AND NOT LYX_USE_QT MATCHES "QT6")
316       set(QPA_XCB ${QT_USES_X11})
317       message(STATUS "Found QPA_XCB = ${QPA_XCB}")
318     endif()
319   endif()
320
321   if (${QtVal}X11Extras_FOUND)
322     get_target_property(_x11extra_prop ${QtVal}::X11Extras IMPORTED_CONFIGURATIONS)
323     get_target_property(_x11extra_link_libraries ${QtVal}::X11Extras IMPORTED_LOCATION_${_x11extra_prop})
324     set(CMAKE_REQUIRED_LIBRARIES ${_x11extra_link_libraries})
325     set(CMAKE_REQUIRED_INCLUDES ${${QtVal}X11Extras_INCLUDE_DIRS})
326     set(CMAKE_REQUIRED_FLAGS "${${QtVal}X11Extras_EXECUTABLE_COMPILE_FLAGS} -fPIC -DQT_NO_VERSION_TAGGING")
327     #message(STATUS "CMAKE_REQUIRED_LIBRARIES = ${_x11extra_link_libraries}")
328     #message(STATUS "CMAKE_REQUIRED_INCLUDES = ${${QtVal}X11Extras_INCLUDE_DIRS}")
329     #message(STATUS "CMAKE_REQUIRED_FLAGS = ${CMAKE_REQUIRED_FLAGS}")
330     check_cxx_source_compiles(
331             "
332             #include <QtX11Extras/QX11Info>
333             int main()
334             {
335               bool isX11 = QX11Info::isPlatformX11();
336             }
337             "
338       QT_HAS_X11_EXTRAS)
339     string(TOUPPER ${QtVal} QTVAL)
340     set(HAVE_${QTVAL}_X11_EXTRAS ${QT_HAS_X11_EXTRAS})
341     set(LYX_${QTVAL}_X11_EXTRAS_LIBRARY ${_x11extra_link_libraries})
342   endif()
343   if (${QtVal}WinExtras_FOUND)
344     get_target_property(_winextra_prop ${QtVal}::WinExtras IMPORTED_CONFIGURATIONS)
345     string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
346     get_target_property(_winextra_link_libraries ${QtVal}::WinExtras IMPORTED_LOCATION_${BUILD_TYPE})
347     set(CMAKE_REQUIRED_LIBRARIES ${_winextra_link_libraries})
348     set(CMAKE_REQUIRED_INCLUDES ${${QtVal}WinExtras_INCLUDE_DIRS})
349     set(CMAKE_REQUIRED_FLAGS ${${QtVal}WinExtras_EXECUTABLE_COMPILE_FLAGS})
350   endif()
351 else()
352   message(FATAL_ERROR "Check for QT_USES_X11: Not handled LYX_USE_QT (= ${LYX_USE_QT})")
353 endif()