Warning, /frameworks/extra-cmake-modules/kde-modules/KDECompilerSettings.cmake is written in an unsupported language. File is not indexed.
0001 # SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org> 0002 # SPDX-FileCopyrightText: 2013 Stephen Kelly <steveire@gmail.com> 0003 # SPDX-FileCopyrightText: 2012-2013 Raphael Kubo da Costa <rakuco@FreeBSD.org> 0004 # SPDX-FileCopyrightText: 2007 Matthias Kretz <kretz@kde.org> 0005 # SPDX-FileCopyrightText: 2006-2007 Laurent Montel <montel@kde.org> 0006 # SPDX-FileCopyrightText: 2006-2013 Alex Neundorf <neundorf@kde.org> 0007 # SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org> 0008 # 0009 # SPDX-License-Identifier: BSD-3-Clause 0010 0011 #[=======================================================================[.rst: 0012 KDECompilerSettings 0013 ------------------- 0014 0015 Set useful compile and link flags for C++ (and C) code. 0016 0017 Enables many more warnings than the default, and sets stricter modes 0018 for some compiler features. By default, exceptions are disabled; 0019 ``kde_target_enable_exceptions()`` can be used to re-enable them for a 0020 specific target. 0021 0022 .. note:: 0023 It is recommended to include this module with the ``NO_POLICY_SCOPE`` 0024 flag, otherwise you may get spurious warnings with some versions of CMake. 0025 0026 Since 5.85 newer settings are controlled by a variable 0027 ``KDE_COMPILERSETTINGS_LEVEL``, taking an ECM version as value. That 0028 version can not be greater than the minimum required ECM version. 0029 The settings which are default at that version will then be used, 0030 but can be overridden by more fine-grained controls (see respective settings). 0031 This variable needs to be set before including this module, otherwise 0032 defaults to the minimum required ECM version. 0033 0034 Modern code 0035 ~~~~~~~~~~~ 0036 0037 The following CMake C standard default variables are set: 0038 0039 For ``KDE_COMPILERSETTINGS_LEVEL`` >= 5.85: 0040 0041 - ``CMAKE_C_STANDARD``: ``99`` 0042 - ``CMAKE_C_STANDARD_REQUIRED``: ``TRUE`` 0043 - ``CMAKE_C_EXTENSIONS``: ``OFF`` 0044 0045 Otherwise: 0046 0047 - ``CMAKE_C_STANDARD``: ``90`` 0048 - ``CMAKE_C_STANDARD_REQUIRED``: not modified 0049 - ``CMAKE_C_EXTENSIONS``: not modified 0050 0051 If the variable ``CMAKE_C_STANDARD`` is already set when including this module, 0052 none of the above variables will be modified. 0053 0054 The following CMake C++ standard default variables are set: 0055 0056 For ``KDE_COMPILERSETTINGS_LEVEL`` >= 5.85: 0057 0058 - ``CMAKE_CXX_STANDARD``: ``17`` 0059 - ``CMAKE_CXX_STANDARD_REQUIRED``: ``TRUE`` 0060 - ``CMAKE_CXX_EXTENSIONS``: ``OFF`` 0061 0062 Otherwise: 0063 0064 - ``CMAKE_CXX_STANDARD``: ``11`` 0065 - ``CMAKE_CXX_STANDARD_REQUIRED``: ``TRUE`` 0066 - ``CMAKE_CXX_EXTENSIONS``: not modified. 0067 0068 If the variable ``CMAKE_CXX_STANDARD`` is already set when including this module, 0069 none of the above variables will be modified. 0070 0071 0072 The following C++ compiler flags are set: 0073 0074 - ``-pedantic`` (GNU and Clang compilers, since 5.85) 0075 0076 Can be disabled by setting ``KDE_SKIP_PEDANTIC_WARNINGS_SETTINGS`` to ``TRUE`` 0077 before including this module (default is ``FALSE`` for 0078 ``KDE_COMPILERSETTINGS_LEVEL`` >= 5.85, ``TRUE`` otherwise). 0079 0080 - ``-Wmissing-include-dirs`` (GNU compilers, since 5.85) 0081 0082 Can be disabled by setting ``KDE_SKIP_MISSING_INCLUDE_DIRS_WARNINGS_SETTINGS`` to ``TRUE`` 0083 before including this module (default is ``FALSE`` for 0084 ``KDE_COMPILERSETTINGS_LEVEL`` >= 5.85, ``TRUE`` otherwise). 0085 0086 - ``-Wzero-as-null-pointer-constant`` (GNU and Clang compilers, since 5.85) 0087 0088 Can be disabled by setting ``KDE_SKIP_NULLPTR_WARNINGS_SETTINGS`` to ``TRUE`` 0089 before including this module (default is ``FALSE`` for 0090 ``KDE_COMPILERSETTINGS_LEVEL`` >= 5.85, ``TRUE`` otherwise). 0091 0092 - ``-Werror=undef`` (GNU and Clang compilers, since 5.96.0) 0093 0094 - Qt related preprocessor definitions (since 5.85.0): 0095 0096 - ``-DQT_NO_CAST_TO_ASCII`` 0097 - ``-DQT_NO_CAST_FROM_ASCII`` 0098 - ``-DQT_NO_URL_CAST_FROM_STRING`` 0099 - ``-DQT_NO_CAST_FROM_BYTEARRAY`` 0100 - ``-DQT_USE_QSTRINGBUILDER`` 0101 - ``-DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT`` 0102 - ``-DQT_NO_KEYWORDS`` 0103 - ``-DQT_NO_FOREACH`` 0104 - ``-DQT_STRICT_ITERATORS`` 0105 0106 Strict iterators are not enabled on Windows, because they lead 0107 to a link error when application code iterates over a QVector<QPoint> for 0108 instance, unless Qt itself was also built with strict iterators. 0109 See example at https://bugreports.qt.io/browse/AUTOSUITE-946 0110 0111 Can be controlled by setting ``KDE_QT_MODERNCODE_DEFINITIONS_LEVEL`` to the 0112 version of ECM where the wanted set of definitions has been added 0113 before including this module (default is ``KDE_COMPILERSETTINGS_LEVEL``). 0114 To disable individual definitions instead use ``remove_definitions()`` directly 0115 after including this module. 0116 0117 Functions 0118 ~~~~~~~~~ 0119 0120 This module provides the following functions:: 0121 0122 kde_source_files_enable_exceptions([file1 [file2 [...]]]) 0123 0124 Enables exceptions for specific source files. This should not be 0125 used on source files in a language other than C++. 0126 0127 :: 0128 0129 kde_target_enable_exceptions(target <INTERFACE|PUBLIC|PRIVATE>) 0130 0131 Enables exceptions for a specific target. This should not be used 0132 on a target that has source files in a language other than C++. 0133 0134 :: 0135 0136 kde_enable_exceptions() 0137 0138 Enables exceptions for C++ source files compiled for the 0139 CMakeLists.txt file in the current directory and all subdirectories. 0140 0141 Variables 0142 ~~~~~~~~~ 0143 0144 Inclusion of this module defines the following variables: 0145 0146 ``ENABLE_BSYMBOLICFUNCTIONS`` 0147 indicates whether we make use of ``-Bsymbolic-functions`` for linking. 0148 It ensures libraries bind global function references locally rather than 0149 at runtime. 0150 This option only has an effect on ELF-based systems. 0151 0152 The option is disabled by default except when using 0153 :kde-module:`KDEFrameworkCompilerSettings` where it's enabled. Projects can enable 0154 it by calling ``set(ENABLE_BSYMBOLICFUNCTIONS ON)`` or passing 0155 ``-DENABLE BSYMBOLICFUNCTIONS=ON`` when configuring the build directory. 0156 0157 Since 5.85 0158 0159 Example usages: 0160 0161 .. code-block:: cmake 0162 0163 # needing some macro/feature only available with ECM 5.80.0 0164 find_package(ECM 5.80.0 NO_MODULE) 0165 0166 # requiring ECM 5.80.0 above will default KDE_COMPILERSETTINGS_LEVEL also to 5.80.0, 0167 # thus not activate any newer settings 0168 include(KDECompilerSettings NO_POLICY_SCOPE) 0169 0170 .. code-block:: cmake 0171 0172 # needing some macro/feature only available with ECM 5.87.0 0173 find_package(ECM 5.87.0 NO_MODULE) 0174 0175 # project uses settings default as of KDECompilerSettings in ECM 5.85.0 0176 set(KDE_COMPILERSETTINGS_LEVEL 5.85.0) 0177 include(KDECompilerSettings NO_POLICY_SCOPE) 0178 0179 .. code-block:: cmake 0180 0181 # needing some macro/feature only available with ECM 5.87.0 0182 find_package(ECM 5.87.0 NO_MODULE) 0183 0184 # project mainly uses settings default as of KDECompilerSettings in ECM 5.85.0 0185 # with some small twisting 0186 set(KDE_COMPILERSETTINGS_LEVEL 5.85.0) 0187 # not ready yet for pedantic compilers 0188 set(KDE_SKIP_PEDANTIC_WARNINGS_SETTINGS TRUE) 0189 # avoid any Qt definitions 0190 set(KDE_QT_MODERNCODE_DEFINITIONS_LEVEL 5.84.0) 0191 include(KDECompilerSettings NO_POLICY_SCOPE) 0192 0193 .. code-block:: cmake 0194 0195 # needing some macro/feature only available with ECM 5.85.0 0196 find_package(ECM 5.85.0 NO_MODULE) 0197 0198 # requiring ECM 5.85.0 above will default KDE_COMPILERSETTINGS_LEVEL also to 5.85.0, 0199 # which again defaults KDE_QT_MODERNCODE_DEFINITIONS_LEVEL also to 5.85.0 0200 include(KDECompilerSettings NO_POLICY_SCOPE) 0201 # project is fine with almost all added Qt definitions as of 5.85.0, but not these ones: 0202 remove_definitions( 0203 -DQT_NO_KEYWORDS 0204 -DQT_NO_FOREACH 0205 ) 0206 0207 Since pre-1.0.0. 0208 #]=======================================================================] 0209 0210 ############################################################ 0211 # Select and check KDE_COMPILERSETTINGS_LEVEL 0212 # For a specified version of KDE_COMPILERSETTINGS_LEVEL always the same set 0213 # of settings needs to be used, to give that version a meaning, even more as 0214 # the settings are usually more strict and can break builds which build fine 0215 # without the setting. 0216 # As at the time of version x it is usually unknown what future versions x+y 0217 # will offer as settings, the minimum required version of ECM sets the upper 0218 # limit then for the level version. 0219 if(NOT DEFINED KDE_COMPILERSETTINGS_LEVEL) 0220 set(KDE_INTERNAL_COMPILERSETTINGS_LEVEL "${ECM_GLOBAL_FIND_VERSION}") 0221 else() 0222 if(KDE_COMPILERSETTINGS_LEVEL VERSION_GREATER "${ECM_GLOBAL_FIND_VERSION}") 0223 message(FATAL_ERROR "KDE_COMPILERSETTINGS_LEVEL (${KDE_COMPILERSETTINGS_LEVEL}) cannot be newer than the min. required ECM version (${ECM_GLOBAL_FIND_VERSION}).") 0224 endif() 0225 set(KDE_INTERNAL_COMPILERSETTINGS_LEVEL "${KDE_COMPILERSETTINGS_LEVEL}") 0226 endif() 0227 0228 include("${ECM_MODULE_DIR}/ECMSourceVersionControl.cmake") 0229 0230 ############################################################ 0231 # Default build type 0232 # If no build type was specified, default to using a debug build if the 0233 # source directory is a git clone. 0234 # Otherwise, leave it empty, to let distro packagers choose the flags. 0235 ############################################################ 0236 if (ECM_SOURCE_UNDER_VERSION_CONTROL AND NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) 0237 set(default_build_type "Debug") 0238 message(STATUS "Setting build type to '${default_build_type}' as none was specified.") 0239 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE "${default_build_type}") 0240 # Set the possible values of build type for cmake-gui 0241 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS 0242 "Debug" "Release" "MinSizeRel" "RelWithDebInfo") 0243 endif() 0244 0245 ############################################################ 0246 # Toolchain minimal requirements 0247 # 0248 # Note that only compilers officially supported by Qt are 0249 # supported by this file; workarounds for older compilers 0250 # will generally not be included. See 0251 # https://qt-project.org/doc/qt-5/supported-platforms.html 0252 # and 0253 # https://community.kde.org/Frameworks/Policies#Frameworks_compiler_requirements_and_C.2B.2B11 0254 # for more details. 0255 ############################################################ 0256 0257 macro(_kde_compiler_min_version min_version) 0258 if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${min_version}) 0259 message(WARNING "Version ${CMAKE_CXX_COMPILER_VERSION} of the ${CMAKE_CXX_COMPILER_ID} C++ compiler is not supported. Please use version ${min_version} or later.") 0260 endif() 0261 endmacro() 0262 0263 if (MSVC) 0264 # MSVC_VERSION 1600 = VS 10.0 = Windows SDK 7 0265 # See: cmake --help-variable MSVC_VERSION 0266 # and https://developer.mozilla.org/en-US/docs/Windows_SDK_versions 0267 if (${MSVC_VERSION} LESS 1600) 0268 message(WARNING "Your MSVC version (${MSVC_VERSION}) is not supported. Please use the Windows SDK version 7 or later (or Microsoft Visual Studio 2010 or later).") 0269 endif() 0270 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0271 if (WIN32) 0272 _kde_compiler_min_version("4.7") 0273 elseif (APPLE) 0274 # FIXME: Apple heavily modifies GCC, so checking the 0275 # GCC version on OS/X is not very useful. 0276 else() 0277 _kde_compiler_min_version("4.5") 0278 endif() 0279 elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0280 _kde_compiler_min_version("3.1") 0281 else() 0282 message(WARNING "${CMAKE_CXX_COMPILER_ID} is not a supported C++ compiler.") 0283 endif() 0284 0285 0286 0287 ############################################################ 0288 # System API features 0289 ############################################################ 0290 0291 # This macro is for adding definitions that affect the underlying 0292 # platform API. It makes sure that configure checks will also have 0293 # the same defines, so that the checks match compiled code. 0294 macro (_KDE_ADD_PLATFORM_DEFINITIONS) 0295 add_definitions(${ARGV}) 0296 set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} ${ARGV}) 0297 endmacro() 0298 0299 include(CheckSymbolExists) 0300 check_symbol_exists("__GLIBC__" "stdlib.h" LIBC_IS_GLIBC) 0301 if (LIBC_IS_GLIBC) 0302 # Enable everything in GNU libc. Any code using non-portable features 0303 # needs to perform feature tests, but this ensures that any such features 0304 # will be found if they exist. 0305 # 0306 # NB: we do NOT define _BSD_SOURCE, as with GNU libc that requires linking 0307 # against the -lbsd-compat library (it changes the behaviour of some 0308 # functions). This, however, means that strlcat and strlcpy are not 0309 # provided by glibc. 0310 _kde_add_platform_definitions(-D_GNU_SOURCE) 0311 endif () 0312 0313 if (UNIX) 0314 # Enable extra API for using 64-bit file offsets on 32-bit systems. 0315 # FIXME: this is included in _GNU_SOURCE in glibc; do other libc 0316 # implementation recognize it? 0317 _kde_add_platform_definitions(-D_LARGEFILE64_SOURCE) 0318 0319 include(CheckCXXSourceCompiles) 0320 0321 # By default (in glibc, at least), on 32bit platforms off_t is 32 bits, 0322 # which causes a SIGXFSZ when trying to manipulate files larger than 2Gb 0323 # using libc calls (note that this issue does not occur when using QFile). 0324 check_cxx_source_compiles(" 0325 #include <sys/types.h> 0326 /* Check that off_t can represent 2**63 - 1 correctly. 0327 We can't simply define LARGE_OFF_T to be 9223372036854775807, 0328 since some C++ compilers masquerading as C compilers 0329 incorrectly reject 9223372036854775807. */ 0330 #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) 0331 int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; 0332 int main() { return 0; } 0333 " _OFFT_IS_64BIT) 0334 0335 if (NOT _OFFT_IS_64BIT) 0336 _kde_add_platform_definitions(-D_FILE_OFFSET_BITS=64) 0337 endif () 0338 endif() 0339 0340 if (WIN32) 0341 # Speeds up compile times by not including everything with windows.h 0342 # See http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx 0343 _kde_add_platform_definitions(-DWIN32_LEAN_AND_MEAN) 0344 0345 # Target Windows Vista 0346 # This enables various bits of new API 0347 # See http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx 0348 _kde_add_platform_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600 -D_WIN32_IE=0x0600) 0349 0350 # Use the Unicode versions of Windows API by default 0351 # See http://msdn.microsoft.com/en-us/library/windows/desktop/dd317766%28v=vs.85%29.aspx 0352 _kde_add_platform_definitions(-DUNICODE -D_UNICODE) 0353 0354 # As stated in http://msdn.microsoft.com/en-us/library/4hwaceh6.aspx M_PI only gets defined 0355 # if _USE_MATH_DEFINES is defined, with mingw this has a similar effect as -D_GNU_SOURCE on math.h 0356 _kde_add_platform_definitions(-D_USE_MATH_DEFINES) 0357 0358 # Don't define MIN and MAX in windows.h 0359 # the defines break the use of std::max 0360 _kde_add_platform_definitions(-DNOMINMAX) 0361 endif() 0362 0363 0364 0365 ############################################################ 0366 # Language and toolchain features 0367 ############################################################ 0368 0369 # Pick sensible versions of the C and C++ standards. 0370 if (NOT CMAKE_C_STANDARD) 0371 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_GREATER_EQUAL 5.85.0) 0372 set(CMAKE_C_STANDARD 99) 0373 set(CMAKE_C_STANDARD_REQUIRED TRUE) 0374 set(CMAKE_C_EXTENSIONS OFF) 0375 else() 0376 set(CMAKE_C_STANDARD 90) 0377 endif() 0378 endif() 0379 if (NOT CMAKE_CXX_STANDARD) 0380 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_GREATER_EQUAL 5.85.0) 0381 set(CMAKE_CXX_STANDARD 17) 0382 set(CMAKE_CXX_EXTENSIONS OFF) 0383 else() 0384 set(CMAKE_CXX_STANDARD 11) 0385 endif() 0386 set(CMAKE_CXX_STANDARD_REQUIRED TRUE) 0387 endif() 0388 0389 # Do not merge uninitialized global variables. 0390 # This is mostly a "principle of least surprise" thing, but also 0391 # has performance benefits. 0392 # See https://www.ibm.com/developerworks/community/blogs/zTPF/entry/benefits_of_the_fnocommon_compile_option_peter_lemieszewski?lang=en 0393 # Note that this only applies to C code; C++ already behaves like this. 0394 if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR 0395 CMAKE_C_COMPILER_ID MATCHES "Clang" OR 0396 (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND NOT WIN32)) 0397 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common") 0398 endif() 0399 0400 # Do not treat the operator name keywords and, bitand, bitor, compl, not, or and xor as synonyms as keywords. 0401 # They're not supported under Visual Studio out of the box thus using them limits the portability of code 0402 if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR 0403 CMAKE_C_COMPILER_ID MATCHES "Clang" OR 0404 (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND NOT WIN32)) 0405 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-operator-names") 0406 endif() 0407 0408 # Default to hidden visibility for symbols 0409 set(CMAKE_C_VISIBILITY_PRESET hidden) 0410 set(CMAKE_CXX_VISIBILITY_PRESET hidden) 0411 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) 0412 if (POLICY CMP0063) 0413 # No sane project should be affected by CMP0063, so suppress the warnings 0414 # generated by the above visibility settings in CMake >= 3.3 0415 cmake_policy(SET CMP0063 NEW) 0416 endif() 0417 0418 if (UNIX AND NOT APPLE AND NOT CYGWIN) 0419 # Enable adding DT_RUNPATH, which means that LD_LIBRARY_PATH takes precedence 0420 # over the built-in rPath 0421 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_SHARED_LINKER_FLAGS}") 0422 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_MODULE_LINKER_FLAGS}") 0423 set(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_EXE_LINKER_FLAGS}") 0424 endif() 0425 0426 if (CMAKE_SYSTEM_NAME STREQUAL GNU) 0427 # Enable multithreading with the pthread library 0428 # FIXME: Is this actually necessary to have here? 0429 # Can CMakeLists.txt files that require it use FindThreads.cmake 0430 # instead? 0431 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread") 0432 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pthread") 0433 endif() 0434 0435 if (MSVC) 0436 # Our source files are UTF-8 encoded, and assuming that is also the 0437 # default behavior of GCC/Clang. Not so for MSVC though, so force 0438 # that to UTF-8 explicitly, as that will otherwise cause compile-time 0439 # and runtime issues when dealing with string literals outside of 7-bit ASCII. 0440 add_compile_options(/utf-8) 0441 endif() 0442 0443 0444 ############################################################ 0445 # Turn off exceptions by default 0446 # 0447 # This involves enough code to be separate from the 0448 # previous section. 0449 ############################################################ 0450 0451 # TODO: Deal with QT_NO_EXCEPTIONS for non-gnu compilers? 0452 # This should be defined if and only if exceptions are disabled. 0453 # qglobal.h has some magic to set it when exceptions are disabled 0454 # with gcc, but other compilers are unaccounted for. 0455 0456 # Turn off exceptions by default 0457 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0458 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") 0459 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0460 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") 0461 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT WIN32) 0462 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") 0463 #elseif (MSVC OR (WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "Intel")) 0464 # Exceptions appear to be disabled by default for MSVC 0465 # http://msdn.microsoft.com/en-us/library/1deeycx5.aspx 0466 0467 # FIXME: are exceptions disabled by default for Intel? 0468 endif() 0469 0470 macro(_kdecompilersettings_append_exception_flag VAR) 0471 if (MSVC) 0472 set(${VAR} "${${VAR}} -EHsc") 0473 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") 0474 if (WIN32) 0475 set(${VAR} "${${VAR}} -EHsc") 0476 else() 0477 set(${VAR} "${${VAR}} -fexceptions") 0478 endif() 0479 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0480 set(${VAR} "${${VAR}} -fexceptions") 0481 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0482 set(${VAR} "${${VAR}} -fexceptions") 0483 endif() 0484 string(STRIP "${${VAR}}" ${VAR}) 0485 endmacro() 0486 0487 function(KDE_SOURCE_FILES_ENABLE_EXCEPTIONS) 0488 foreach(source_file ${ARGV}) 0489 get_source_file_property(flags ${source_file} COMPILE_FLAGS) 0490 if(NOT flags) 0491 # If COMPILE_FLAGS is not set, get_source_file_property() sets it to 0492 # NOTFOUND, which breaks build if we concatenate anything to 0493 # the "NOTFOUND" string. 0494 # Note that NOTFOUND evaluates to False, so we do enter the if. 0495 set(flags "") 0496 endif() 0497 _kdecompilersettings_append_exception_flag(flags) 0498 set_source_files_properties(${source_file} COMPILE_FLAGS "${flags}") 0499 endforeach() 0500 endfunction() 0501 0502 function(KDE_TARGET_ENABLE_EXCEPTIONS target mode) 0503 target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:MSVC>:-EHsc>") 0504 if (WIN32) 0505 target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:Intel>:-EHsc>") 0506 else() 0507 target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:Intel>:-fexceptions>") 0508 endif() 0509 target_compile_options(${target} ${mode} 0510 "$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-fexceptions>") 0511 endfunction() 0512 0513 function(KDE_ENABLE_EXCEPTIONS) 0514 # We set CMAKE_CXX_FLAGS, rather than add_compile_options(), because 0515 # we only want to affect the compilation of C++ source files. 0516 0517 # strip any occurrences of -DQT_NO_EXCEPTIONS; this should only be defined 0518 # if exceptions are disabled 0519 # the extra spaces mean we will not accentially mangle any other options 0520 string(REPLACE " -DQT_NO_EXCEPTIONS " " " CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} ") 0521 # this option is common to several compilers, so just always remove it 0522 string(REPLACE " -fno-exceptions " " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 0523 # strip undoes the extra spaces we put in above 0524 string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS) 0525 0526 _kdecompilersettings_append_exception_flag(CMAKE_CXX_FLAGS) 0527 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE) 0528 endfunction() 0529 0530 0531 0532 ############################################################ 0533 # Better diagnostics (warnings, errors) 0534 ############################################################ 0535 0536 if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT APPLE) OR 0537 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) OR 0538 (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT WIN32)) 0539 # Linker warnings should be treated as errors 0540 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fatal-warnings ${CMAKE_SHARED_LINKER_FLAGS}") 0541 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fatal-warnings ${CMAKE_MODULE_LINKER_FLAGS}") 0542 0543 # Do not allow undefined symbols, even in non-symbolic shared libraries 0544 # On OpenBSD we must disable this to allow the stuff to properly compile without explicit libc specification 0545 if (NOT CMAKE_SYSTEM_NAME MATCHES "OpenBSD") 0546 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}") 0547 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_MODULE_LINKER_FLAGS}") 0548 endif() 0549 endif() 0550 0551 set(_KDE_GCC_COMMON_WARNING_FLAGS "-Wall -Wextra -Wcast-align -Wchar-subscripts -Wformat-security -Wno-long-long -Wpointer-arith -Wundef") 0552 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0553 # -Wgnu-zero-variadic-macro-arguments (part of -pedantic) is triggered by every qCDebug() call and therefore results 0554 # in a lot of noise. This warning is only notifying us that clang is emulating the GCC behaviour 0555 # instead of the exact standard wording so we can safely ignore it 0556 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-gnu-zero-variadic-macro-arguments") 0557 endif() 0558 if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") 0559 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_KDE_GCC_COMMON_WARNING_FLAGS} -Wmissing-format-attribute -Wwrite-strings") 0560 # Make some warnings errors 0561 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration") 0562 endif() 0563 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0564 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_KDE_GCC_COMMON_WARNING_FLAGS} -Wnon-virtual-dtor -Woverloaded-virtual") 0565 # Make some warnings errors 0566 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=return-type -Werror=init-self") 0567 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_GREATER_EQUAL 5.96.0) 0568 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=undef") 0569 endif() 0570 endif() 0571 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR 0572 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 3.5)) 0573 # -Wvla: use of variable-length arrays (an extension to C++) 0574 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wvla") 0575 endif() 0576 if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0) OR 0577 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 3.5)) 0578 include(CheckCXXCompilerFlag) 0579 check_cxx_compiler_flag(-Wdate-time HAVE_DATE_TIME) 0580 if (HAVE_DATE_TIME) 0581 # -Wdate-time: warn if we use __DATE__ or __TIME__ (we want to be able to reproduce the exact same binary) 0582 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wdate-time") 0583 endif() 0584 endif() 0585 0586 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0587 if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0.0) 0588 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsuggest-override -Wlogical-op" ) 0589 endif() 0590 endif() 0591 0592 # -w1 turns on warnings and errors 0593 # FIXME: someone needs to have a closer look at the Intel compiler options 0594 if (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND NOT WIN32) 0595 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -w1 -Wpointer-arith") 0596 endif() 0597 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT WIN32) 0598 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -w1 -Wpointer-arith") 0599 endif() 0600 0601 if (MSVC) 0602 # FIXME: do we not want to set the warning level up to level 3? (/W3) 0603 # Disable warnings: 0604 # C4250: 'class1' : inherits 'class2::member' via dominance 0605 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250") 0606 # C4251: 'identifier' : class 'type' needs to have dll-interface to be 0607 # used by clients of class 'type2' 0608 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") 0609 # C4396: 'identifier' : 'function' the inline specifier cannot be used 0610 # when a friend declaration refers to a specialization of a 0611 # function template 0612 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4396") 0613 # C4661: 'identifier' : no suitable definition provided for explicit 0614 # template instantiation request 0615 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4661") 0616 0617 if (CMAKE_CXX_STANDARD GREATER_EQUAL 11) 0618 # Ensure __cplusplus is correct, otherwise it defaults to 199711L which isn't true 0619 # https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-160 0620 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus") 0621 endif() 0622 endif() 0623 0624 option(ENABLE_BSYMBOLICFUNCTIONS "Make use of -Bsymbolic-functions" OFF) 0625 if (ENABLE_BSYMBOLICFUNCTIONS) 0626 set(_SYMBOLIC_FUNCTIONS_COMPILER_OPTION "-Wl,-Bsymbolic-functions") 0627 list(APPEND CMAKE_REQUIRED_LIBRARIES "${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0628 0629 include(CheckCXXSourceCompiles) 0630 0631 check_cxx_source_compiles( "int main () { return 0; }" BSYMBOLICFUNCTIONS_AVAILABLE ) 0632 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0633 if (BSYMBOLICFUNCTIONS_AVAILABLE) 0634 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0635 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0636 endif() 0637 endif() 0638 0639 if (WIN32) 0640 # Disable deprecation warnings for some API 0641 # FIXME: do we really want this? 0642 add_definitions(-D_CRT_SECURE_NO_DEPRECATE 0643 -D_CRT_SECURE_NO_WARNINGS 0644 -D_CRT_NONSTDC_NO_DEPRECATE 0645 -D_SCL_SECURE_NO_WARNINGS 0646 ) 0647 endif() 0648 0649 if (APPLE) 0650 #full Single Unix Standard v3 (SUSv3) conformance (the Unix API) 0651 _kde_add_platform_definitions(-D_DARWIN_C_SOURCE) 0652 #Cocoa is unconditional since we only support OS X 10.6 and above 0653 _kde_add_platform_definitions(-DQT_MAC_USE_COCOA) 0654 endif() 0655 0656 ############################################################ 0657 # Modern code 0658 ############################################################ 0659 0660 function(_kde_set_default_skip_variable_by_min_ecm _var_name _ecm_version) 0661 if(NOT DEFINED ${_var_name}) 0662 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_LESS ${_ecm_version}) 0663 set(${_var_name} TRUE PARENT_SCOPE) 0664 else() 0665 set(${_var_name} FALSE PARENT_SCOPE) 0666 endif() 0667 endif() 0668 endfunction() 0669 0670 if(NOT DEFINED KDE_QT_MODERNCODE_DEFINITIONS_LEVEL) 0671 set(KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL ${KDE_INTERNAL_COMPILERSETTINGS_LEVEL}) 0672 else() 0673 set(KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL ${KDE_QT_MODERNCODE_DEFINITIONS_LEVEL}) 0674 endif() 0675 0676 if (KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL VERSION_GREATER_EQUAL "5.85.0") 0677 add_definitions( 0678 -DQT_NO_CAST_TO_ASCII 0679 -DQT_NO_CAST_FROM_ASCII 0680 -DQT_NO_URL_CAST_FROM_STRING 0681 -DQT_NO_CAST_FROM_BYTEARRAY 0682 -DQT_USE_QSTRINGBUILDER 0683 -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT 0684 -DQT_NO_KEYWORDS 0685 -DQT_NO_FOREACH 0686 ) 0687 if (NOT WIN32) 0688 # Strict iterators can't be used on Windows, they lead to a link error 0689 # when application code iterates over a QVector<QPoint> for instance, unless 0690 # Qt itself was also built with strict iterators. 0691 # See example at https://bugreports.qt.io/browse/AUTOSUITE-946 0692 add_definitions(-DQT_STRICT_ITERATORS) 0693 endif() 0694 endif() 0695 0696 _kde_set_default_skip_variable_by_min_ecm(KDE_SKIP_PEDANTIC_WARNINGS_SETTINGS "5.85.0") 0697 0698 if (NOT KDE_SKIP_PEDANTIC_WARNINGS_SETTINGS) 0699 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0700 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic") 0701 endif() 0702 endif() 0703 0704 _kde_set_default_skip_variable_by_min_ecm(KDE_SKIP_NULLPTR_WARNINGS_SETTINGS "5.85.0") 0705 0706 if (NOT KDE_SKIP_NULLPTR_WARNINGS_SETTINGS) 0707 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0708 if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0.0") 0709 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wzero-as-null-pointer-constant" ) 0710 endif() 0711 endif() 0712 0713 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0714 if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0.0") 0715 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wzero-as-null-pointer-constant" ) 0716 endif() 0717 endif() 0718 endif() 0719 0720 _kde_set_default_skip_variable_by_min_ecm(KDE_SKIP_MISSING_INCLUDE_DIRS_WARNINGS_SETTINGS "5.85.0") 0721 0722 if (NOT KDE_SKIP_MISSING_INCLUDE_DIRS_WARNINGS_SETTINGS) 0723 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0724 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-include-dirs") 0725 endif() 0726 endif() 0727 0728 ############################################################ 0729 # Hacks 0730 # 0731 # Anything in this section should be thoroughly documented, 0732 # including what problems it is supposed to fix and in what 0733 # circumstances those problems occur. Include links to any 0734 # relevant bug reports. 0735 ############################################################ 0736 0737 if (APPLE) 0738 # FIXME: why are these needed? The commit log is unhelpful 0739 # (it was introduced in svn path=/trunk/KDE/kdelibs/; revision=503025 - 0740 # kdelibs git commit 4e4cb9cb9a2216b63d3eabf88b8fe94ee3c898cf - 0741 # with the message "mac os x fixes for the cmake build") 0742 set (CMAKE_SHARED_LINKER_FLAGS "-single_module -multiply_defined suppress ${CMAKE_SHARED_LINKER_FLAGS}") 0743 set (CMAKE_MODULE_LINKER_FLAGS "-multiply_defined suppress ${CMAKE_MODULE_LINKER_FLAGS}") 0744 endif() 0745 0746 if (MINGW AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0747 # This was copied from the Phonon build settings, where it had the comment 0748 # "otherwise undefined symbol in phononcore.dll errors occurs", with the commit 0749 # message "set linker flag --export-all-symbols for all targets, otherwise 0750 # some depending targets could not be build" 0751 # FIXME: do our export macros not deal with this properly? 0752 set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--export-all-symbols") 0753 set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--export-all-symbols") 0754 endif() 0755 0756 if (CMAKE_GENERATOR STREQUAL "Ninja" AND 0757 ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 4.9) OR 0758 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 3.5))) 0759 # Force colored warnings in Ninja's output, if the compiler has -fdiagnostics-color support. 0760 # Rationale in https://github.com/ninja-build/ninja/issues/814 0761 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always") 0762 endif() 0763 0764 include("${ECM_MODULE_DIR}/ECMEnableSanitizers.cmake") 0765 include("${ECM_MODULE_DIR}/ECMCoverageOption.cmake") 0766 0767 ############################################################ 0768 # Clean-up 0769 ############################################################ 0770 # unset again, to not leak into caller scope and avoid usage there 0771 set(KDE_INTERNAL_COMPILERSETTINGS_LEVEL) 0772 set(KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL)