cmake: Consolidate compile settings
[quassel.git] / CMakeLists.txt
1 # Main CMake file for building Quassel IRC
2 #
3 # See INSTALL for possible CMake options (or read the code, Luke)
4 #####################################################################
5
6 # General setup
7 #####################################################################
8
9 cmake_minimum_required(VERSION 3.5)
10
11 # Tell CMake about or own modules
12 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
13
14 # Versions
15 set(QUASSEL_MAJOR  0)
16 set(QUASSEL_MINOR 13)
17 set(QUASSEL_PATCH 50)
18 set(QUASSEL_VERSION_STRING "0.14-pre")
19
20 # Output CMake and Quassel versions as well as build type for debug reasons
21 message(STATUS "Building Quassel ${QUASSEL_VERSION_STRING}...")
22 message(STATUS "Using CMake ${CMAKE_VERSION}")
23
24 # Set up build type rather early
25 include(BuildType)
26
27 # Support ccache if found
28 # This should happen before calling project(), so compiler settings are validated.
29 option(USE_CCACHE "Enable support for ccache if available" ON)
30 if (USE_CCACHE)
31     message(STATUS "Checking for ccache")
32     find_program(CCACHE_PROGRAM ccache)
33     if (CCACHE_PROGRAM)
34         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
35         message(STATUS "Checking for ccache - enabled")
36     else()
37         message(STATUS "Checking for ccache - not found")
38     endif()
39 endif()
40
41 # Set up project
42 project(Quassel CXX)
43
44 # Let CMake handle file generation for Qt
45 set(CMAKE_AUTOMOC ON)
46 set(CMAKE_AUTORCC ON)
47 set(CMAKE_AUTOUIC ON)
48
49 # Needed, otherwise some .moc files won't be found with older CMake versions
50 set(CMAKE_INCLUDE_CURRENT_DIR ON)
51
52 # Include various CMake modules...
53 include(CMakePushCheckState)
54 include(CheckFunctionExists)
55 include(CheckIncludeFileCXX)
56 include(CheckCXXSourceCompiles)
57 include(CMakeDependentOption)
58 include(FeatureSummary)
59
60 # ... and our own
61 include(QuasselCompileSettings)
62 include(QuasselMacros)
63
64 # Options and variables that can be set on the command line
65 #####################################################################
66
67 # Select the binaries to build
68 option(WANT_CORE     "Build the core (server) binary"           ON)
69 option(WANT_QTCLIENT "Build the client-only binary"             ON)
70 option(WANT_MONO     "Build the monolithic (all-in-one) binary" ON)
71 add_feature_info(WANT_CORE WANT_CORE "Build the core (server) binary")
72 add_feature_info(WANT_QTCLIENT WANT_QTCLIENT "Build the client-only binary (requires a core to connect to)")
73 add_feature_info(WANT_MONO WANT_MONO "Build the monolithic (all-in-one) binary")
74
75 # Whether to enable integration with higher-tier KDE frameworks that require runtime support.
76 # We still optionally make use of certain Tier 1 frameworks even if WITH_KDE is disabled.
77 option(WITH_KDE "Integration with the KDE Frameworks runtime environment")
78 add_feature_info(WITH_KDE WITH_KDE "Integrate with the KDE Frameworks runtime environment")
79
80 # Icon theme support. By default, install the Breeze icon theme (may be disabled if a system installation is present)
81 option(WITH_BUNDLED_ICONS "Install required icons from the Breeze icon theme" ON)
82 add_feature_info(WITH_BUNDLED_ICONS WITH_BUNDLED_ICONS "Install required icons from the Breeze icon theme")
83
84 option(WITH_OXYGEN_ICONS "Support the Oxygen icon theme (KDE4)" OFF)
85 add_feature_info(WITH_OXYGEN_ICONS WITH_OXYGEN_ICONS "Support the Oxygen icon theme (KDE4)")
86
87 # For this, the feature info is added after we know if QtWebkit is installed
88 option(WITH_WEBKIT "WebKit support (for link previews) (legacy)" OFF)
89
90 # For this, the feature info is added after we know if QtWebEngine is installed
91 option(WITH_WEBENGINE "WebEngine support (for link previews)" ON)
92
93 if (APPLE)
94     # Notification Center is only available in > 10.8, which is Darwin v12
95     if (NOT CMAKE_SYSTEM_VERSION VERSION_LESS 12)
96         option(WITH_NOTIFICATION_CENTER "OS X Notification Center support" ON)
97         add_feature_info(WITH_NOTIFICATION_CENTER WITH_NOTIFICATION_CENTER "Use the OS X Notification Center")
98     endif()
99     find_library(CARBON_LIBRARY Carbon)
100     mark_as_advanced(CARBON_LIBRARY)
101     link_libraries(${CARBON_LIBRARY})
102 endif()
103
104 # Always embed on Windows or OSX; never embed when enabling KDE integration
105 set(EMBED_DEFAULT OFF)
106 if (WIN32 OR APPLE)
107     set(EMBED_DEFAULT ON)
108 endif()
109 cmake_dependent_option(EMBED_DATA "Embed icons and translations into the binaries instead of installing them" ${EMBED_DEFAULT}
110                                    "NOT WIN32;NOT WITH_KDE" ${EMBED_DEFAULT})
111 if (NOT EMBED_DEFAULT)
112     add_feature_info(EMBED_DATA EMBED_DATA "Embed icons and translations in the binaries instead of installing them")
113 endif()
114
115 # The following options are not for end-user consumption, so don't list them in the feature summary
116 cmake_dependent_option(DEPLOY "Add required libs to bundle resources and create a dmg" OFF "APPLE" OFF)
117
118 # List of authenticators and the cmake flags to build them
119 # (currently that's just LDAP, but more can be added here).
120 ####################################################################
121 option(WITH_LDAP "Enable LDAP authentication support if present on system" ON)
122
123 # Setup CMake
124 #####################################################################
125
126 # Visibility settings apply to all targets
127 if (POLICY CMP0063)
128     cmake_policy(SET CMP0063 NEW)
129 endif()
130
131 # Let automoc/autouic process generated files
132 if (POLICY CMP0071)
133     cmake_policy(SET CMP0071 NEW)
134 endif()
135
136 set(BUILD_SHARED_LIBS TRUE CACHE BOOL "" FORCE)
137
138 # Don't use X11 on OSX
139 if (APPLE)
140     set(CMAKE_DISABLE_FIND_PACKAGE_X11 true)
141     set(CMAKE_DISABLE_FIND_PACKAGE_XCB true)
142     set(CMAKE_DISABLE_FIND_PACKAGE_Qt5X11Extras true)
143 endif()
144
145 # Simplify later checks
146 #####################################################################
147
148 if (WANT_MONO OR WANT_QTCLIENT)
149     set(BUILD_GUI true)
150 endif()
151 if (WANT_MONO OR WANT_CORE)
152     set(BUILD_CORE true)
153 endif()
154
155 # Set up Qt
156 #####################################################################
157
158 # Find package dependencies
159 #
160 # Note that you can forcefully disable optional packages
161 # using -DCMAKE_DISABLE_FIND_PACKAGE_<PkgName>=TRUE
162 #####################################################################
163
164 set(QT_MIN_VERSION "5.5.0")
165
166 # Required Qt components
167 set(qt_components Core Network)
168 if (BUILD_GUI)
169     list(APPEND qt_components Gui Widgets)
170 endif()
171 if (BUILD_CORE)
172     list(APPEND qt_components Script Sql)
173 endif()
174
175 find_package(Qt5 ${QT_MIN_VERSION} REQUIRED COMPONENTS ${qt_components})
176 set_package_properties(Qt5 PROPERTIES TYPE REQUIRED
177     URL "https://www.qt.io/"
178     DESCRIPTION "the Qt libraries"
179 )
180 message(STATUS "Found Qt ${Qt5Core_VERSION}")
181
182 # Optional Qt components
183
184 find_package(Qt5LinguistTools QUIET)
185 set_package_properties(Qt5LinguistTools PROPERTIES TYPE RECOMMENDED
186     DESCRIPTION "contains tools for handling translation files"
187     PURPOSE "Required for having translations"
188 )
189
190 if (BUILD_GUI)
191     if (NOT WIN32)
192         find_package(Qt5DBus QUIET)
193         set_package_properties(Qt5DBus PROPERTIES TYPE RECOMMENDED
194             URL "https://www.qt.io/"
195             DESCRIPTION "D-Bus support for Qt5"
196             PURPOSE     "Needed for supporting D-Bus-based notifications and tray icon, used by most modern desktop environments"
197         )
198         if (Qt5DBus_FOUND)
199             find_package(dbusmenu-qt5 QUIET CONFIG)
200             set_package_properties(dbusmenu-qt5 PROPERTIES TYPE RECOMMENDED
201                 URL "https://launchpad.net/libdbusmenu-qt"
202                 DESCRIPTION "a library implementing the DBusMenu specification"
203                 PURPOSE     "Required for having a context menu for the D-Bus-based tray icon"
204             )
205         endif()
206     endif()
207
208     find_package(Qt5Multimedia QUIET)
209     set_package_properties(Qt5Multimedia PROPERTIES TYPE RECOMMENDED
210         URL "https://www.qt.io/"
211         DESCRIPTION "Multimedia support for Qt5"
212         PURPOSE     "Required for audio notifications"
213     )
214
215     find_package(LibsnoreQt5 0.7.0 QUIET)
216     set_package_properties(LibsnoreQt5 PROPERTIES TYPE OPTIONAL
217         URL "https://projects.kde.org/projects/playground/libs/snorenotify"
218         DESCRIPTION "a cross-platform notification framework"
219         PURPOSE     "Enable support for the snorenotify framework"
220     )
221     if (LibsnoreQt5_FOUND)
222         find_package(LibsnoreSettingsQt5 QUIET)
223         set_package_properties(LibsnoreSettingsQt5 PROPERTIES TYPE OPTIONAL
224             URL "https://projects.kde.org/projects/playground/libs/snorenotify"
225             DESCRIPTION "a cross-platform notification framework"
226             PURPOSE     "Enable support for the snorenotify framework"
227         )
228     endif()
229
230     if (WITH_WEBENGINE)
231         find_package(Qt5WebEngine QUIET)
232         set_package_properties(Qt5WebEngine PROPERTIES TYPE RECOMMENDED
233             URL "https://www.qt.io/"
234             DESCRIPTION "a WebEngine implementation for Qt"
235             PURPOSE     "Needed for displaying previews for URLs in chat"
236         )
237         if (Qt5WebEngine_FOUND)
238             find_package(Qt5WebEngineWidgets QUIET)
239             set_package_properties(Qt5WebEngineWidgets PROPERTIES TYPE RECOMMENDED
240                 URL "https://www.qt.io/"
241                 DESCRIPTION "widgets for Qt's WebEngine implementation"
242                 PURPOSE     "Needed for displaying previews for URLs in chat"
243             )
244         endif()
245     endif()
246
247     if (WITH_WEBENGINE AND Qt5WebEngineWidgets_FOUND)
248         set(HAVE_WEBENGINE true)
249     endif()
250     add_feature_info("WITH_WEBENGINE, QtWebEngine and QtWebEngineWidgets modules" HAVE_WEBENGINE "Support showing previews for URLs in chat")
251
252     if (NOT HAVE_WEBENGINE)
253         if (WITH_WEBKIT)
254             find_package(Qt5WebKit QUIET)
255             set_package_properties(Qt5WebKit PROPERTIES TYPE OPTIONAL
256                 URL "https://www.qt.io/"
257                 DESCRIPTION "a WebKit implementation for Qt"
258                 PURPOSE     "Needed for displaying previews for URLs in chat"
259                 )
260             if (Qt5WebKit_FOUND)
261                 find_package(Qt5WebKitWidgets QUIET)
262                 set_package_properties(Qt5WebKitWidgets PROPERTIES TYPE OPTIONAL
263                     URL "https://www.qt.io/"
264                     DESCRIPTION "widgets for Qt's WebKit implementation"
265                     PURPOSE     "Needed for displaying previews for URLs in chat"
266                     )
267             endif()
268         endif()
269
270         if (WITH_WEBKIT AND Qt5WebKitWidgets_FOUND)
271             set(HAVE_WEBKIT true)
272         endif()
273         add_feature_info("WITH_WEBKIT, QtWebKit and QtWebKitWidgets modules" HAVE_WEBKIT "Support showing previews for URLs in chat (legacy)")
274     endif()
275
276     # KDE Frameworks
277     ################
278
279     # extra-cmake-modules
280     if (WITH_KDE)
281         set(ecm_find_type "REQUIRED")
282         find_package(ECM NO_MODULE REQUIRED)
283     else()
284         # Even with KDE integration disabled, we optionally use tier1 frameworks if we find them
285         set(ecm_find_type "RECOMMENDED")
286         find_package(ECM NO_MODULE QUIET)
287     endif()
288
289     set_package_properties(ECM PROPERTIES TYPE ${ecm_find_type}
290         URL "https://projects.kde.org/projects/kdesupport/extra-cmake-modules"
291         DESCRIPTION "extra modules for CMake, maintained by the KDE project"
292         PURPOSE     "Required to find KDE Frameworks components"
293     )
294
295     if (ECM_FOUND)
296         list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
297         if (WITH_KDE)
298             find_package(KF5 REQUIRED COMPONENTS ConfigWidgets CoreAddons Notifications NotifyConfig Sonnet TextWidgets WidgetsAddons XmlGui)
299             set_package_properties(KF5 PROPERTIES TYPE REQUIRED
300                 URL "http://www.kde.org"
301                 DESCRIPTION "KDE Frameworks"
302                 PURPOSE     "Required for integration into the Plasma desktop"
303             )
304             message(STATUS "Found KDE Frameworks ${KF5_VERSION}")
305         endif()
306
307         # Optional KF5 tier1 components
308         find_package(KF5Sonnet QUIET)
309         set_package_properties(KF5Sonnet PROPERTIES TYPE RECOMMENDED
310             URL "http://api.kde.org/frameworks-api/frameworks5-apidocs/sonnet/html"
311             DESCRIPTION "framework for providing spell-checking capabilities"
312             PURPOSE "Enables spell-checking support in input widgets"
313         )
314     endif()
315 endif()
316
317 if (BUILD_CORE)
318     find_package(Qca-qt5 2.0 QUIET)
319     set_package_properties(Qca-qt5 PROPERTIES TYPE RECOMMENDED
320         URL "https://projects.kde.org/projects/kdesupport/qca"
321         DESCRIPTION "Qt Cryptographic Architecture"
322         PURPOSE "Required for encryption support"
323     )
324
325     if (WITH_LDAP)
326         find_package(Ldap QUIET)
327         set_package_properties(Ldap PROPERTIES TYPE OPTIONAL
328             URL "http://www.openldap.org/"
329             DESCRIPTION "LDAP (Lightweight Directory Access Protocol) libraries"
330             PURPOSE "Enables core user authentication via LDAP"
331         )
332     endif()
333 endif()
334
335 # Non-Qt-based packages
336 #####################################################################
337
338 find_package(Boost 1.56 REQUIRED)
339 set_package_properties(Boost PROPERTIES TYPE REQUIRED
340     URL "https://www.boost.org/"
341     DESCRIPTION "Boost libraries for C++"
342 )
343
344 find_package(ZLIB REQUIRED)
345 set_package_properties(ZLIB PROPERTIES TYPE REQUIRED
346     URL "http://www.zlib.net"
347     DESCRIPTION "a popular compression library"
348     PURPOSE     "Used for protocol compression"
349 )
350
351 if (NOT WIN32)
352     # Needed for generating backtraces
353     find_package(Backtrace QUIET)
354     set_package_properties(Backtrace PROPERTIES TYPE RECOMMENDED
355         DESCRIPTION "a header (and possibly library) for inspecting backtraces"
356         PURPOSE "Used for generating backtraces in case of a crash"
357     )
358 endif()
359
360 # Setup unit testing
361 #####################################################################
362
363 option(BUILD_TESTING "Enable unit tests" OFF)
364 add_feature_info(BUILD_TESTING BUILD_TESTING "Build unit tests")
365
366 if (BUILD_TESTING)
367     find_package(GTest QUIET)
368     set_package_properties(GTest PROPERTIES TYPE REQUIRED
369         DESCRIPTION "Google's unit testing framework"
370         PURPOSE "Required for building unit tests"
371     )
372
373     find_package(Qt5Test QUIET)
374     set_package_properties(Qt5Test PROPERTIES TYPE REQUIRED
375         DESCRIPTION "unit testing library for the Qt5 framework"
376         PURPOSE "Required for building unit tests"
377     )
378     enable_testing()
379
380     # GTest messes with CMAKE_CXX_FLAGS, so process them again
381     process_cmake_cxx_flags()
382 endif()
383
384 # Check for SSL support in Qt
385 #####################################################################
386
387 cmake_push_check_state(RESET)
388 set(CMAKE_REQUIRED_LIBRARIES Qt5::Core)
389 check_cxx_source_compiles("
390     #include \"qglobal.h\"
391     #if defined QT_NO_SSL
392     #  error \"No SSL support\"
393     #endif
394     int main() {}"
395     HAVE_SSL)
396 cmake_pop_check_state()
397
398 if (HAVE_SSL)
399     add_definitions(-DHAVE_SSL)
400 endif()
401 add_feature_info("SSL support in Qt" HAVE_SSL "Use secure network connections")
402
403 # Setup support for KDE Frameworks
404 #####################################################################
405
406 if (WITH_KDE)
407     add_definitions(-DHAVE_KDE -DHAVE_KF5)
408     set(WITH_KF5 TRUE)
409
410     # If KDE Frameworks are present, they're most probably providing Qt5 integration including icon loading
411     set(EMBED_DATA OFF)
412
413     include(KDEInstallDirs)
414 endif()
415
416 # This needs to come after setting up KDE integration, so we can use KDE-specific paths
417 include(QuasselInstallDirs)
418
419 # RPATH and output settings
420 #####################################################################
421
422 # Build artifacts in a well-known location; especially important for Windows DLLs
423 # (which go into RUNTIME_OUTPUT_DIRECTORY and can thus be found by executables)
424 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
425 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
426 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
427
428 # These RPATH settings allow for running directly from the build dir
429 set(CMAKE_SKIP_BUILD_RPATH            FALSE)
430 set(CMAKE_BUILD_WITH_INSTALL_RPATH    FALSE)
431 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
432
433 # Set install RPATH only if libdir isn't a system directory
434 if (IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
435     set(libdir "${CMAKE_INSTALL_LIBDIR}")
436 else()
437     set(libdir "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
438 endif()
439 list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${libdir}" is_systemdir)
440 if ("${is_systemdir}" STREQUAL "-1")
441    set(CMAKE_INSTALL_RPATH "${libdir}")
442 endif()
443
444 # Various config-dependent checks and settings
445 #####################################################################
446
447 # Check for syslog support
448 if (NOT WIN32)
449     check_include_file_cxx(syslog.h HAVE_SYSLOG)
450     add_feature_info("syslog.h" HAVE_SYSLOG "Provide support for logging to the syslog")
451 endif()
452
453 if (NOT WIN32)
454     check_function_exists(umask HAVE_UMASK)
455 endif()
456
457 if (EMBED_DATA)
458     message(STATUS "Embedding data files into the binary")
459 else()
460     message(STATUS "Installing data files separately")
461 endif()
462
463 # Windows-specific stuff
464 #####################################################################
465
466 if (WIN32)
467     link_libraries(imm32 winmm dbghelp Secur32)  # missing by default :/
468     if (MSVC)
469         link_libraries(Version dwmapi shlwapi)
470     endif()
471 endif()
472
473 # Generate version information from Git
474 #####################################################################
475
476 include(GetGitRevisionDescription)
477 get_git_head_revision(GIT_REFSPEC GIT_HEAD)
478 git_describe(GIT_DESCRIBE --long)
479
480 # If in a Git repo we can get the commit-date from a git command
481 if (GIT_HEAD)
482     execute_process(
483         COMMAND git -c log.showsignature=false show -s --format=%ct
484         WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
485         OUTPUT_VARIABLE GIT_COMMIT_DATE
486         OUTPUT_STRIP_TRAILING_WHITESPACE
487     )
488 endif()
489
490 # If not in a Git repo try to read GIT_HEAD and GIT_DESCRIBE from
491 # enviroment
492 if (NOT GIT_HEAD OR NOT GIT_DESCRIBE)
493   if (DEFINED ENV{GIT_HEAD})
494       set(GIT_HEAD $ENV{GIT_HEAD})
495   endif()
496   if (DEFINED ENV{GIT_DESCRIBE})
497      set(GIT_DESCRIBE $ENV{GIT_DESCRIBE})
498   endif()
499 endif()
500
501 # Sanitize things if we're not in a Git repo
502 if (NOT GIT_HEAD OR NOT GIT_DESCRIBE)
503     set(GIT_HEAD "")
504     set(GIT_DESCRIBE "")
505     set(GIT_COMMIT_DATE 0)
506 endif()
507
508 configure_file(version.h.in ${CMAKE_BINARY_DIR}/version.h @ONLY)
509
510 # Prepare the build
511 #####################################################################
512
513 # Add needed subdirs - the order is important, since src needs some vars set by other dirs
514 add_subdirectory(data)
515 add_subdirectory(icons)
516 add_subdirectory(pics)
517 add_subdirectory(po)
518
519 # Set up and display feature summary
520 #####################################################################
521
522 feature_summary(WHAT ALL
523                 INCLUDE_QUIET_PACKAGES
524                 FATAL_ON_MISSING_REQUIRED_PACKAGES
525 )
526
527 # Finally, compile the sources
528 # We want this after displaying the feature summary to avoid ugly
529 # CMake backtraces in case a required Qt5 module is missing
530 #####################################################################
531
532 add_subdirectory(src)
533
534 # Build tests if so desired
535 if (BUILD_TESTING)
536     add_subdirectory(tests)
537 endif()