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 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_GREATER_EQUAL 5.240.0 OR QT_MAJOR_VERSION STREQUAL "6") 0346 # Target Windows 10 0347 # This enables various bits of new API 0348 # See http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx 0349 # Windows 10 is the default by Qt6 hence we do not need the next line, but we keep it disabled 0350 # to not to start from scratch in case we want to target a different version in the future 0351 # _kde_add_platform_definitions(-D_WIN32_WINNT=0x0A00 -DWINVER=0x0A00 -D_WIN32_IE=0x0A00) 0352 else() 0353 # Target Windows Vista 0354 # This enables various bits of new API 0355 # See http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx 0356 _kde_add_platform_definitions(-D_WIN32_WINNT=0x0600 -DWINVER=0x0600 -D_WIN32_IE=0x0600) 0357 endif() 0358 0359 # Use the Unicode versions of Windows API by default 0360 # See http://msdn.microsoft.com/en-us/library/windows/desktop/dd317766%28v=vs.85%29.aspx 0361 _kde_add_platform_definitions(-DUNICODE -D_UNICODE) 0362 0363 # As stated in http://msdn.microsoft.com/en-us/library/4hwaceh6.aspx M_PI only gets defined 0364 # if _USE_MATH_DEFINES is defined, with mingw this has a similar effect as -D_GNU_SOURCE on math.h 0365 _kde_add_platform_definitions(-D_USE_MATH_DEFINES) 0366 0367 # Don't define MIN and MAX in windows.h 0368 # the defines break the use of std::max 0369 _kde_add_platform_definitions(-DNOMINMAX) 0370 endif() 0371 0372 0373 0374 ############################################################ 0375 # Language and toolchain features 0376 ############################################################ 0377 0378 # Pick sensible versions of the C and C++ standards. 0379 if (NOT CMAKE_C_STANDARD) 0380 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_GREATER_EQUAL 5.85.0) 0381 set(CMAKE_C_STANDARD 99) 0382 set(CMAKE_C_STANDARD_REQUIRED TRUE) 0383 set(CMAKE_C_EXTENSIONS OFF) 0384 else() 0385 set(CMAKE_C_STANDARD 90) 0386 endif() 0387 endif() 0388 if (NOT CMAKE_CXX_STANDARD) 0389 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_GREATER_EQUAL 5.85.0) 0390 set(CMAKE_CXX_STANDARD 17) 0391 set(CMAKE_CXX_EXTENSIONS OFF) 0392 else() 0393 set(CMAKE_CXX_STANDARD 11) 0394 endif() 0395 set(CMAKE_CXX_STANDARD_REQUIRED TRUE) 0396 endif() 0397 0398 # Do not merge uninitialized global variables. 0399 # This is mostly a "principle of least surprise" thing, but also 0400 # has performance benefits. 0401 # See https://www.ibm.com/developerworks/community/blogs/zTPF/entry/benefits_of_the_fnocommon_compile_option_peter_lemieszewski?lang=en 0402 # Note that this only applies to C code; C++ already behaves like this. 0403 if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR 0404 CMAKE_C_COMPILER_ID MATCHES "Clang" OR 0405 (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND NOT WIN32)) 0406 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common") 0407 endif() 0408 0409 # Do not treat the operator name keywords and, bitand, bitor, compl, not, or and xor as synonyms as keywords. 0410 # They're not supported under Visual Studio out of the box thus using them limits the portability of code 0411 if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR 0412 CMAKE_C_COMPILER_ID MATCHES "Clang" OR 0413 (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND NOT WIN32)) 0414 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-operator-names") 0415 endif() 0416 0417 # Default to hidden visibility for symbols 0418 set(CMAKE_C_VISIBILITY_PRESET hidden) 0419 set(CMAKE_CXX_VISIBILITY_PRESET hidden) 0420 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) 0421 if (POLICY CMP0063) 0422 # No sane project should be affected by CMP0063, so suppress the warnings 0423 # generated by the above visibility settings in CMake >= 3.3 0424 cmake_policy(SET CMP0063 NEW) 0425 endif() 0426 0427 if (UNIX AND NOT APPLE AND NOT CYGWIN) 0428 # Enable adding DT_RUNPATH, which means that LD_LIBRARY_PATH takes precedence 0429 # over the built-in rPath 0430 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_SHARED_LINKER_FLAGS}") 0431 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_MODULE_LINKER_FLAGS}") 0432 set(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_EXE_LINKER_FLAGS}") 0433 endif() 0434 0435 if (CMAKE_SYSTEM_NAME STREQUAL GNU) 0436 # Enable multithreading with the pthread library 0437 # FIXME: Is this actually necessary to have here? 0438 # Can CMakeLists.txt files that require it use FindThreads.cmake 0439 # instead? 0440 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread") 0441 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pthread") 0442 endif() 0443 0444 if (MSVC) 0445 # Our source files are UTF-8 encoded, and assuming that is also the 0446 # default behavior of GCC/Clang. Not so for MSVC though, so force 0447 # that to UTF-8 explicitly, as that will otherwise cause compile-time 0448 # and runtime issues when dealing with string literals outside of 7-bit ASCII. 0449 add_compile_options(/utf-8) 0450 endif() 0451 0452 0453 ############################################################ 0454 # Turn off exceptions by default 0455 # 0456 # This involves enough code to be separate from the 0457 # previous section. 0458 ############################################################ 0459 0460 # TODO: Deal with QT_NO_EXCEPTIONS for non-gnu compilers? 0461 # This should be defined if and only if exceptions are disabled. 0462 # qglobal.h has some magic to set it when exceptions are disabled 0463 # with gcc, but other compilers are unaccounted for. 0464 0465 # Turn off exceptions by default 0466 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0467 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") 0468 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0469 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") 0470 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT WIN32) 0471 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") 0472 #elseif (MSVC OR (WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "Intel")) 0473 # Exceptions appear to be disabled by default for MSVC 0474 # http://msdn.microsoft.com/en-us/library/1deeycx5.aspx 0475 0476 # FIXME: are exceptions disabled by default for Intel? 0477 endif() 0478 0479 macro(_kdecompilersettings_append_exception_flag VAR) 0480 if (MSVC) 0481 set(${VAR} "${${VAR}} -EHsc") 0482 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") 0483 if (WIN32) 0484 set(${VAR} "${${VAR}} -EHsc") 0485 else() 0486 set(${VAR} "${${VAR}} -fexceptions") 0487 endif() 0488 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0489 set(${VAR} "${${VAR}} -fexceptions") 0490 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0491 set(${VAR} "${${VAR}} -fexceptions") 0492 endif() 0493 string(STRIP "${${VAR}}" ${VAR}) 0494 endmacro() 0495 0496 function(KDE_SOURCE_FILES_ENABLE_EXCEPTIONS) 0497 foreach(source_file ${ARGV}) 0498 get_source_file_property(flags ${source_file} COMPILE_FLAGS) 0499 if(NOT flags) 0500 # If COMPILE_FLAGS is not set, get_source_file_property() sets it to 0501 # NOTFOUND, which breaks build if we concatenate anything to 0502 # the "NOTFOUND" string. 0503 # Note that NOTFOUND evaluates to False, so we do enter the if. 0504 set(flags "") 0505 endif() 0506 _kdecompilersettings_append_exception_flag(flags) 0507 set_source_files_properties(${source_file} COMPILE_FLAGS "${flags}") 0508 endforeach() 0509 endfunction() 0510 0511 function(KDE_TARGET_ENABLE_EXCEPTIONS target mode) 0512 target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:MSVC>:-EHsc>") 0513 if (WIN32) 0514 target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:Intel>:-EHsc>") 0515 else() 0516 target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:Intel>:-fexceptions>") 0517 endif() 0518 target_compile_options(${target} ${mode} 0519 "$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-fexceptions>") 0520 endfunction() 0521 0522 function(KDE_ENABLE_EXCEPTIONS) 0523 # We set CMAKE_CXX_FLAGS, rather than add_compile_options(), because 0524 # we only want to affect the compilation of C++ source files. 0525 0526 # strip any occurrences of -DQT_NO_EXCEPTIONS; this should only be defined 0527 # if exceptions are disabled 0528 # the extra spaces mean we will not accentially mangle any other options 0529 string(REPLACE " -DQT_NO_EXCEPTIONS " " " CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} ") 0530 # this option is common to several compilers, so just always remove it 0531 string(REPLACE " -fno-exceptions " " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 0532 # strip undoes the extra spaces we put in above 0533 string(STRIP "${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS) 0534 0535 _kdecompilersettings_append_exception_flag(CMAKE_CXX_FLAGS) 0536 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE) 0537 endfunction() 0538 0539 0540 0541 ############################################################ 0542 # Better diagnostics (warnings, errors) 0543 ############################################################ 0544 0545 if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT APPLE) OR 0546 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT APPLE) OR 0547 (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT WIN32)) 0548 # Linker warnings should be treated as errors 0549 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fatal-warnings ${CMAKE_SHARED_LINKER_FLAGS}") 0550 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fatal-warnings ${CMAKE_MODULE_LINKER_FLAGS}") 0551 0552 string(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" compileflags) 0553 if("${CMAKE_CXX_FLAGS} ${${compileflags}}" MATCHES "-fsanitize") 0554 set(sanitizers_enabled TRUE) 0555 else() 0556 set(sanitizers_enabled FALSE) 0557 endif() 0558 0559 # Do not allow undefined symbols, even in non-symbolic shared libraries 0560 # On OpenBSD we must disable this to allow the stuff to properly compile without explicit libc specification 0561 if (NOT CMAKE_SYSTEM_NAME MATCHES "OpenBSD" AND (NOT sanitizers_enabled OR NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")) 0562 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}") 0563 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_MODULE_LINKER_FLAGS}") 0564 endif() 0565 endif() 0566 0567 set(_KDE_GCC_COMMON_WARNING_FLAGS "-Wall -Wextra -Wcast-align -Wchar-subscripts -Wformat-security -Wno-long-long -Wpointer-arith -Wundef") 0568 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0569 # -Wgnu-zero-variadic-macro-arguments (part of -pedantic) is triggered by every qCDebug() call and therefore results 0570 # in a lot of noise. This warning is only notifying us that clang is emulating the GCC behaviour 0571 # instead of the exact standard wording so we can safely ignore it 0572 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-gnu-zero-variadic-macro-arguments") 0573 endif() 0574 if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang") 0575 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_KDE_GCC_COMMON_WARNING_FLAGS} -Wmissing-format-attribute -Wwrite-strings") 0576 # Make some warnings errors 0577 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration") 0578 endif() 0579 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0580 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_KDE_GCC_COMMON_WARNING_FLAGS} -Wnon-virtual-dtor -Woverloaded-virtual") 0581 # Make some warnings errors 0582 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=return-type -Werror=init-self") 0583 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_GREATER_EQUAL 5.96.0) 0584 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=undef") 0585 endif() 0586 elseif(MSVC) 0587 # similar to -Werror=return-type 0588 # https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4715?view=msvc-170 0589 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /we4715") 0590 endif() 0591 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR 0592 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 3.5)) 0593 # -Wvla: use of variable-length arrays (an extension to C++) 0594 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wvla") 0595 endif() 0596 if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0) OR 0597 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 3.5)) 0598 include(CheckCXXCompilerFlag) 0599 check_cxx_compiler_flag(-Wdate-time HAVE_DATE_TIME) 0600 if (HAVE_DATE_TIME) 0601 # -Wdate-time: warn if we use __DATE__ or __TIME__ (we want to be able to reproduce the exact same binary) 0602 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wdate-time") 0603 endif() 0604 endif() 0605 0606 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0607 if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5.0.0) 0608 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsuggest-override -Wlogical-op" ) 0609 endif() 0610 endif() 0611 0612 # -w1 turns on warnings and errors 0613 # FIXME: someone needs to have a closer look at the Intel compiler options 0614 if (CMAKE_C_COMPILER_ID STREQUAL "Intel" AND NOT WIN32) 0615 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -w1 -Wpointer-arith") 0616 endif() 0617 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT WIN32) 0618 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -w1 -Wpointer-arith") 0619 endif() 0620 0621 if (MSVC) 0622 # enable linter like warnings including deprecation warnings 0623 # https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170 0624 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3") 0625 # Disable warnings: 0626 # C4250: 'class1' : inherits 'class2::member' via dominance 0627 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250") 0628 # C4251: 'identifier' : class 'type' needs to have dll-interface to be 0629 # used by clients of class 'type2' 0630 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") 0631 # C4396: 'identifier' : 'function' the inline specifier cannot be used 0632 # when a friend declaration refers to a specialization of a 0633 # function template 0634 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4396") 0635 # C4661: 'identifier' : no suitable definition provided for explicit 0636 # template instantiation request 0637 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4661") 0638 0639 if (CMAKE_CXX_STANDARD GREATER_EQUAL 11) 0640 # Ensure __cplusplus is correct, otherwise it defaults to 199711L which isn't true 0641 # https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-160 0642 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus") 0643 endif() 0644 endif() 0645 0646 option(ENABLE_BSYMBOLICFUNCTIONS "Make use of -Bsymbolic-functions" OFF) 0647 if (ENABLE_BSYMBOLICFUNCTIONS) 0648 set(_SYMBOLIC_FUNCTIONS_COMPILER_OPTION "-Wl,-Bsymbolic-functions") 0649 list(APPEND CMAKE_REQUIRED_LIBRARIES "${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0650 0651 include(CheckCXXSourceCompiles) 0652 0653 check_cxx_source_compiles( "int main () { return 0; }" BSYMBOLICFUNCTIONS_AVAILABLE ) 0654 list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0655 if (BSYMBOLICFUNCTIONS_AVAILABLE) 0656 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0657 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${_SYMBOLIC_FUNCTIONS_COMPILER_OPTION}") 0658 endif() 0659 endif() 0660 0661 if (WIN32) 0662 # Disable deprecation warnings for some API 0663 # FIXME: do we really want this? 0664 add_definitions(-D_CRT_SECURE_NO_DEPRECATE 0665 -D_CRT_SECURE_NO_WARNINGS 0666 -D_CRT_NONSTDC_NO_DEPRECATE 0667 -D_SCL_SECURE_NO_WARNINGS 0668 ) 0669 endif() 0670 0671 if (APPLE) 0672 #full Single Unix Standard v3 (SUSv3) conformance (the Unix API) 0673 _kde_add_platform_definitions(-D_DARWIN_C_SOURCE) 0674 #Cocoa is unconditional since we only support OS X 10.6 and above 0675 _kde_add_platform_definitions(-DQT_MAC_USE_COCOA) 0676 endif() 0677 0678 ############################################################ 0679 # Modern code 0680 ############################################################ 0681 0682 function(_kde_set_default_skip_variable_by_min_ecm _var_name _ecm_version) 0683 if(NOT DEFINED ${_var_name}) 0684 if (KDE_INTERNAL_COMPILERSETTINGS_LEVEL VERSION_LESS ${_ecm_version}) 0685 set(${_var_name} TRUE PARENT_SCOPE) 0686 else() 0687 set(${_var_name} FALSE PARENT_SCOPE) 0688 endif() 0689 endif() 0690 endfunction() 0691 0692 if(NOT DEFINED KDE_QT_MODERNCODE_DEFINITIONS_LEVEL) 0693 set(KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL ${KDE_INTERNAL_COMPILERSETTINGS_LEVEL}) 0694 else() 0695 set(KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL ${KDE_QT_MODERNCODE_DEFINITIONS_LEVEL}) 0696 endif() 0697 0698 if (KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL VERSION_GREATER_EQUAL "5.85.0") 0699 add_definitions( 0700 -DQT_NO_CAST_TO_ASCII 0701 -DQT_NO_CAST_FROM_ASCII 0702 -DQT_NO_URL_CAST_FROM_STRING 0703 -DQT_NO_CAST_FROM_BYTEARRAY 0704 -DQT_USE_QSTRINGBUILDER 0705 -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT 0706 -DQT_NO_KEYWORDS 0707 -DQT_NO_FOREACH 0708 ) 0709 if (NOT WIN32) 0710 # Strict iterators can't be used on Windows, they lead to a link error 0711 # when application code iterates over a QVector<QPoint> for instance, unless 0712 # Qt itself was also built with strict iterators. 0713 # See example at https://bugreports.qt.io/browse/AUTOSUITE-946 0714 add_definitions(-DQT_STRICT_ITERATORS) 0715 endif() 0716 endif() 0717 0718 _kde_set_default_skip_variable_by_min_ecm(KDE_SKIP_PEDANTIC_WARNINGS_SETTINGS "5.85.0") 0719 0720 if (NOT KDE_SKIP_PEDANTIC_WARNINGS_SETTINGS) 0721 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0722 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic") 0723 endif() 0724 endif() 0725 0726 _kde_set_default_skip_variable_by_min_ecm(KDE_SKIP_NULLPTR_WARNINGS_SETTINGS "5.85.0") 0727 0728 if (NOT KDE_SKIP_NULLPTR_WARNINGS_SETTINGS) 0729 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0730 if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0.0") 0731 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wzero-as-null-pointer-constant" ) 0732 endif() 0733 endif() 0734 0735 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 0736 if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0.0") 0737 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wzero-as-null-pointer-constant" ) 0738 endif() 0739 endif() 0740 endif() 0741 0742 _kde_set_default_skip_variable_by_min_ecm(KDE_SKIP_MISSING_INCLUDE_DIRS_WARNINGS_SETTINGS "5.85.0") 0743 0744 if (NOT KDE_SKIP_MISSING_INCLUDE_DIRS_WARNINGS_SETTINGS) 0745 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0746 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-include-dirs") 0747 endif() 0748 endif() 0749 0750 ############################################################ 0751 # Hacks 0752 # 0753 # Anything in this section should be thoroughly documented, 0754 # including what problems it is supposed to fix and in what 0755 # circumstances those problems occur. Include links to any 0756 # relevant bug reports. 0757 ############################################################ 0758 0759 if (APPLE) 0760 # FIXME: why are these needed? The commit log is unhelpful 0761 # (it was introduced in svn path=/trunk/KDE/kdelibs/; revision=503025 - 0762 # kdelibs git commit 4e4cb9cb9a2216b63d3eabf88b8fe94ee3c898cf - 0763 # with the message "mac os x fixes for the cmake build") 0764 set (CMAKE_SHARED_LINKER_FLAGS "-single_module -multiply_defined suppress ${CMAKE_SHARED_LINKER_FLAGS}") 0765 set (CMAKE_MODULE_LINKER_FLAGS "-multiply_defined suppress ${CMAKE_MODULE_LINKER_FLAGS}") 0766 endif() 0767 0768 if (MINGW AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 0769 # This was copied from the Phonon build settings, where it had the comment 0770 # "otherwise undefined symbol in phononcore.dll errors occurs", with the commit 0771 # message "set linker flag --export-all-symbols for all targets, otherwise 0772 # some depending targets could not be build" 0773 # FIXME: do our export macros not deal with this properly? 0774 set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--export-all-symbols") 0775 set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--export-all-symbols") 0776 endif() 0777 0778 if (CMAKE_GENERATOR STREQUAL "Ninja" AND 0779 ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 4.9) OR 0780 (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 3.5))) 0781 # Force colored warnings in Ninja's output, if the compiler has -fdiagnostics-color support. 0782 # Rationale in https://github.com/ninja-build/ninja/issues/814 0783 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always") 0784 endif() 0785 0786 include("${ECM_MODULE_DIR}/ECMEnableSanitizers.cmake") 0787 include("${ECM_MODULE_DIR}/ECMCoverageOption.cmake") 0788 0789 ############################################################ 0790 # Clean-up 0791 ############################################################ 0792 # unset again, to not leak into caller scope and avoid usage there 0793 set(KDE_INTERNAL_COMPILERSETTINGS_LEVEL) 0794 set(KDE_INTERNAL_QT_MODERNCODE_DEFINITIONS_LEVEL)