diff options
-rw-r--r-- | kde-modules/KDECompilerSettings.cmake | 492 |
1 files changed, 244 insertions, 248 deletions
diff --git a/kde-modules/KDECompilerSettings.cmake b/kde-modules/KDECompilerSettings.cmake index 1a1d2789..6b7ab4ed 100644 --- a/kde-modules/KDECompilerSettings.cmake +++ b/kde-modules/KDECompilerSettings.cmake @@ -11,26 +11,63 @@ # kde_source_files_enable_exceptions([file1 [file2 [...]]]) # # Enables exceptions for specific source files. This should not be -# called on source files in a language other than C++. +# used on source files in a language other than C++. # # kde_target_enable_exceptions(target <INTERFACE|PUBLIC|PRIVATE>) # -# Enables exceptions for a specific target. This should not be called +# Enables exceptions for a specific target. This should not be used # on a target that has source files in a language other than C++. # # kde_enable_exceptions() # -# Enables exceptions for C++ source files compiled for the CMakeLists.txt file -# in the current directory and all subdirectories. +# Enables exceptions for C++ source files compiled for the +# CMakeLists.txt file in the current directory and all subdirectories. # -# TODO: Deal with QT_NO_EXCEPTIONS for non-gnu compilers? -# This should be defined if and only if exceptions are disabled. -# qglobal.h has some magic to set it when exceptions are disabled -# with gcc, but other compilers are unaccounted for. +############################################################ +# Toolchain minimal requirements +############################################################ -include(CheckCXXCompilerFlag) +set(_GCC_COMPILED_WITH_BAD_ALLOCATOR FALSE) + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + if ("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "4.2.0") + message(FATAL_ERROR "GCC 4.2 or later is required") + endif() + + if (NOT WIN32) + # FIXME: This test was originally used to decide whether to + # enable hidden vis for GCC >= 4.1 (it would not be + # if GCC had the mt allocator). + # 1. Is this something that matters for GCC >= 4.2? + # 2. If so, do we actually want to change the value + # of CMAKE_CXX_VISIBILITY_PRESET? + # All I can find about this is + # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19664, + # esp. comments 90/91 and 104-108. I think requiring + # GCC 4.2 and some version of binutils (2.18?) should + # be enough. + set(_GCC_COMPILED_WITH_BAD_ALLOCATOR FALSE) + exec_program(${CMAKE_C_COMPILER} ARGS ${CMAKE_C_COMPILER_ARG1} -v OUTPUT_VARIABLE _gcc_alloc_info) + string(REGEX MATCH "(--enable-libstdcxx-allocator=mt)" _GCC_COMPILED_WITH_BAD_ALLOCATOR "${_gcc_alloc_info}") + endif() +endif() + +if (NOT WIN32 AND NOT _GCC_COMPILED_WITH_BAD_ALLOCATOR) + # FIXME: in practice, this target is never going to exist at this point + if (TARGET Qt5::Core) + if(NOT QT_VISIBILITY_AVAILABLE) + message(FATAL_ERROR "Qt compiled without support for -fvisibility=hidden. This will break plugins and linking of some applications. Please fix your Qt installation (try passing --reduce-exports to configure).") + endif() + endif() +endif() + + + +############################################################ +# System API features +############################################################ # This macro is for adding definitions that affect the underlying # platform API. It makes sure that configure checks will also have @@ -40,260 +77,107 @@ macro (_KDE_ADD_PLATFORM_DEFINITIONS) set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} ${ARGV}) endmacro() -if (WIN32) - - # windows, microsoft compiler - if(MSVC OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") - # FIXME: KDE_FULL_TEMPLATE_EXPORT_INSTANTIATION is a hangover from kdemacros.h - # and should probably be removed - add_definitions(-DKDE_FULL_TEMPLATE_EXPORT_INSTANTIATION) - - # Speeds up compile times by not including everything with windows.h - _kde_add_platform_definitions(-DWIN32_LEAN_AND_MEAN) - - # Disable warnings: - # C4250: 'class1' : inherits 'class2::member' via dominance - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4250" ) - # C4251: 'identifier' : class 'type' needs to have dll-interface to be - # used by clients of class 'type2' - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4251" ) - # C4396: 'identifier' : 'function' the inline specifier cannot be used - # when a friend declaration refers to a specialization of a - # function template - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4396" ) - # C4661: 'identifier' : no suitable definition provided for explicit - # template instantiation request - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -wd4661" ) - - # to avoid a lot of deprecated warnings - add_definitions(-D_CRT_SECURE_NO_DEPRECATE - -D_CRT_SECURE_NO_WARNINGS - -D_CRT_NONSTDC_NO_DEPRECATE - -D_SCL_SECURE_NO_WARNINGS - ) - endif() - -# TODO: we should not depend on Perl or Qt already been found here -# -# # for visual studio IDE set the path correctly for custom commands -# # maybe under windows bat-files should be generated for running apps during the build -# if(MSVC_IDE) -# get_filename_component(PERL_LOCATION "${PERL_EXECUTABLE}" PATH) -# file(TO_NATIVE_PATH "${PERL_LOCATION}" PERL_PATH_WINDOWS) -# file(TO_NATIVE_PATH "${QT_BINARY_DIR}" QT_BIN_DIR_WINDOWS) -# set(CMAKE_MSVCIDE_RUN_PATH "${PERL_PATH_WINDOWS}\;${QT_BIN_DIR_WINDOWS}" -# CACHE STATIC "MSVC IDE Run path" FORCE) -# endif() - - # TODO: do we really want to have this line here ? Is it actually needed ? - # we prefer to use a different postfix for debug libs only on Windows - # does not work atm - set(CMAKE_DEBUG_POSTFIX "") - - # we don't support anything below w2k - _kde_add_platform_definitions(-D_WIN32_WINNT=0x0501 -DWINVER=0x0501 -D_WIN32_IE=0x0501) - # all winapi calls are unicodes - _kde_add_platform_definitions(-DUNICODE) - -endif (WIN32) - -if (APPLE) - # we need to set MACOSX_DEPLOYMENT_TARGET to (I believe) at least 10.2 or - # maybe 10.3 to allow -undefined dynamic_lookup; in the future we should do - # this programmatically - - # hmm... why doesn't this work? - set (ENV{MACOSX_DEPLOYMENT_TARGET} 10.3) +if (UNIX) + # Enable basically everything portable across modern UNIX systems. + # See http://www.delorie.com/gnu/docs/glibc/libc_13.html + # + # NB: we do NOT define _BSD_SOURCE, as with GNU libc that requires linking + # against the -lbsd-compat library (it changes the behaviour of some + # functions). + _kde_add_platform_definitions(-D_XOPEN_SOURCE=500) + # FIXME: do we need this? + _kde_add_platform_definitions(-D_GNU_SOURCE) + + # Enable extra API for using 64-bit file offsets on 32-bit systems. + # FIXME: do we need this? Does it make sense on non-glibc systems? + _kde_add_platform_definitions(-D_LARGEFILE64_SOURCE) endif() - -if ("${CMAKE_SYSTEM_NAME}" MATCHES Linux OR "${CMAKE_SYSTEM_NAME}" STREQUAL GNU) - # Get glibc to provide useful functions - # - # _BSD_SOURCE: is/was needed by glibc for snprintf to be available when - # building C files. - # See commit 4a44862b2d178c1d2e1eb4da90010d19a1e4a42c. - _kde_add_platform_definitions(-D_XOPEN_SOURCE=500 -D_BSD_SOURCE -D_GNU_SOURCE) +if (WIN32) + # Speeds up compile times by not including everything with windows.h + # See http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx + _kde_add_platform_definitions(-DWIN32_LEAN_AND_MEAN) + + # Target Windows XP / Windows Server 2003 + # This enables various bits of new API + # See http://msdn.microsoft.com/en-us/library/windows/desktop/aa383745%28v=vs.85%29.aspx + _kde_add_platform_definitions(-D_WIN32_WINNT=0x0501 -DWINVER=0x0501 -D_WIN32_IE=0x0501) + + # Use the Unicode versions of Windows API by default + # See http://msdn.microsoft.com/en-us/library/windows/desktop/dd317766%28v=vs.85%29.aspx + _kde_add_platform_definitions(-DUNICODE) endif() -if (UNIX) - # 64-bit file offsets - _kde_add_platform_definitions(-D_LARGEFILE64_SOURCE) -endif (UNIX) - - -if (APPLE) - # "-undefined dynamic_lookup" means we don't care about missing symbols at - # link-time by default this is bad, but unavoidable until there is the - # equivalent of libtool -no-undefined implemented or perhaps it already is, - # and I just don't know where to look ;) - - set (CMAKE_SHARED_LINKER_FLAGS "-single_module -multiply_defined suppress ${CMAKE_SHARED_LINKER_FLAGS}") - set (CMAKE_MODULE_LINKER_FLAGS "-multiply_defined suppress ${CMAKE_MODULE_LINKER_FLAGS}") - #set(CMAKE_SHARED_LINKER_FLAGS "-single_module -undefined dynamic_lookup -multiply_defined suppress") - #set(CMAKE_MODULE_LINKER_FLAGS "-undefined dynamic_lookup -multiply_defined suppress") - - # removed -Os, was there a special reason for using -Os instead of -O2 ?, Alex - # optimization flags are set below for the various build types - set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common") - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common") -endif (APPLE) - -# TODO: why don't we go inside that block on the BSDs ? -if (CMAKE_SYSTEM_NAME MATCHES Linux OR CMAKE_SYSTEM_NAME STREQUAL GNU) - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - # TODO: does the Intel compiler also support --enable-new-dtags ? ...probably since they are only forwarded to the linker.... - set ( CMAKE_SHARED_LINKER_FLAGS "-Wl,--enable-new-dtags -Wl,--fatal-warnings -Wl,--no-undefined -lc ${CMAKE_SHARED_LINKER_FLAGS}") - set ( CMAKE_MODULE_LINKER_FLAGS "-Wl,--enable-new-dtags -Wl,--fatal-warnings -Wl,--no-undefined -lc ${CMAKE_MODULE_LINKER_FLAGS}") - set ( CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_EXE_LINKER_FLAGS}") - endif() - - if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") - set ( CMAKE_SHARED_LINKER_FLAGS "-Wl,--fatal-warnings -Wl,--no-undefined -lc ${CMAKE_SHARED_LINKER_FLAGS}") - set ( CMAKE_MODULE_LINKER_FLAGS "-Wl,--fatal-warnings -Wl,--no-undefined -lc ${CMAKE_MODULE_LINKER_FLAGS}") - endif () -endif (CMAKE_SYSTEM_NAME MATCHES Linux OR CMAKE_SYSTEM_NAME STREQUAL GNU) - ############################################################ -# compiler specific settings +# Language and toolchain features ############################################################ -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) - - # this macro is for internal use only. - macro(_KDE_INSERT_FLAG FLAG VAR DOC) - if(NOT ${VAR} MATCHES "${FLAG}") - set(${VAR} "${${VAR}} ${FLAG}" CACHE STRING "Flags used by the linker during ${DOC} builds." FORCE) - endif() - endmacro() - - # make sure that no header adds libcmt by default using - # #pragma comment(lib, "libcmt.lib") as done by mfc/afx.h - _kde_insert_flag("/NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "Release with Debug Info") - _kde_insert_flag("/NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt" CMAKE_EXE_LINKER_FLAGS_RELEASE "release") - _kde_insert_flag("/NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt" CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "release minsize") - _kde_insert_flag("/NODEFAULTLIB:libcmtd /DEFAULTLIB:msvcrtd" CMAKE_EXE_LINKER_FLAGS_DEBUG "debug") +# Pick sensible versions of the C and C++ standards +# FIXME: MSVC, Intel on windows? +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=iso9899:1990") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") +elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" AND NOT WIN32) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") endif() -# TODO: this is for BSD, this looks like something which should be done in CMake -# # This macro is for internal use only -# # Return the directories present in gcc's include path. -# macro(_DETERMINE_GCC_SYSTEM_INCLUDE_DIRS _lang _result) -# set(${_result}) -# set(_gccOutput) -# file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy" "\n" ) -# execute_process(COMMAND ${CMAKE_C_COMPILER} -v -E -x ${_lang} -dD dummy -# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/CMakeFiles -# ERROR_VARIABLE _gccOutput -# OUTPUT_VARIABLE _gccStdout ) -# file(REMOVE "${CMAKE_BINARY_DIR}/CMakeFiles/dummy") -# -# if( "${_gccOutput}" MATCHES "> search starts here[^\n]+\n *(.+) *\n *End of (search) list" ) -# SET(${_result} ${CMAKE_MATCH_1}) -# STRING(REPLACE "\n" " " ${_result} "${${_result}}") -# SEPARATE_ARGUMENTS(${_result}) -# ENDIF( "${_gccOutput}" MATCHES "> search starts here[^\n]+\n *(.+) *\n *End of (search) list" ) -# endmacro(_DETERMINE_GCC_SYSTEM_INCLUDE_DIRS _lang) - - -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") +# Default to hidden visibility for symbols +set(CMAKE_CXX_VISIBILITY_PRESET hidden) +set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) - if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "4.2.0") - message(FATAL_ERROR "GCC 4.2 or later is required") - endif() +if (UNIX AND NOT APPLE) + # Enable adding DT_RUNPATH, which means that LD_LIBRARY_PATH takes precedence + # over the built-in rPath + set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_SHARED_LINKER_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_MODULE_LINKER_FLAGS}") + set(CMAKE_EXE_LINKER_FLAGS "-Wl,--enable-new-dtags ${CMAKE_EXE_LINKER_FLAGS}") +endif() -# if (CMAKE_COMPILER_IS_GNUCC) -# _DETERMINE_GCC_SYSTEM_INCLUDE_DIRS(c _dirs) -# set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES -# ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES} ${_dirs}) -# endif (CMAKE_COMPILER_IS_GNUCC) +if (CMAKE_SYSTEM_NAME STREQUAL GNU) + # Enable multithreading with the pthread library + # FIXME: Is this actually necessary to have here? + # Can CMakeLists.txt files that require it use FindThreads.cmake + # instead? + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pthread") +endif() -# _DETERMINE_GCC_SYSTEM_INCLUDE_DIRS(c++ _dirs) -# set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES -# ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES} ${_dirs}) -endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") +############################################################ +# Turn off exceptions by default +# +# This involves enough code to be separate from the +# previous section. +############################################################ +# TODO: Deal with QT_NO_EXCEPTIONS for non-gnu compilers? +# This should be defined if and only if exceptions are disabled. +# qglobal.h has some magic to set it when exceptions are disabled +# with gcc, but other compilers are unaccounted for. +# Turn off exceptions by default if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-long-long -std=iso9899:1990 -Wundef -Wcast-align -Werror-implicit-function-declaration -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -Wmissing-format-attribute -fno-common") - # As of Qt 4.6.x we need to override the new exception macros if we want compile with -fno-exceptions - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wnon-virtual-dtor -Wno-long-long -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wpointer-arith -Wformat-security -fno-exceptions -fno-check-new -fno-common") - - if (CMAKE_SYSTEM_NAME STREQUAL GNU) - set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread") - set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pthread") - endif () - - # gcc under Windows - if (MINGW) - set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--export-all-symbols") - set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--export-all-symbols") - endif () - - check_cxx_compiler_flag(-Woverloaded-virtual __KDE_HAVE_W_OVERLOADED_VIRTUAL) - if(__KDE_HAVE_W_OVERLOADED_VIRTUAL) - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual") - endif() - - set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=return-type") - - set(_GCC_COMPILED_WITH_BAD_ALLOCATOR FALSE) - exec_program(${CMAKE_C_COMPILER} ARGS ${CMAKE_C_COMPILER_ARG1} -v OUTPUT_VARIABLE _gcc_alloc_info) - string(REGEX MATCH "(--enable-libstdcxx-allocator=mt)" _GCC_COMPILED_WITH_BAD_ALLOCATOR "${_gcc_alloc_info}") - - if (NOT _GCC_COMPILED_WITH_BAD_ALLOCATOR AND NOT WIN32) - if (TARGET Qt5::Core) - if(NOT QT_VISIBILITY_AVAILABLE) - message(FATAL_ERROR "Qt compiled without support for -fvisibility=hidden. This will break plugins and linking of some applications. Please fix your Qt installation (try passing --reduce-exports to configure).") - endif() - endif() - endif() - -endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - - -if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - # Note that exceptions are enabled by default when building with clang. That - # is, -fno-exceptions is not set in CMAKE_CXX_FLAGS below. This is because a - # lot of code in different KDE modules ends up including code that throws - # exceptions. Most (or all) of the occurrences are in template code that - # never gets instantiated. Contrary to GCC, ICC and MSVC, clang (most likely - # rightfully) complains about that. Trying to work around the issue by - # passing -fdelayed-template-parsing brings other problems, as noted in - # http://lists.kde.org/?l=kde-core-devel&m=138157459706783&w=2. - # The generated code will be slightly bigger, but there is no way to avoid - # it. - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-long-long -std=iso9899:1990 -Wundef -Wcast-align -Werror-implicit-function-declaration -Wchar-subscripts -Wall -W -Wpointer-arith -Wwrite-strings -Wformat-security -Wmissing-format-attribute -fno-common") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wnon-virtual-dtor -Wno-long-long -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wpointer-arith -Wformat-security -Woverloaded-virtual -fno-common -Werror=return-type") - - if (CMAKE_SYSTEM_NAME STREQUAL GNU) - set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread") - set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -pthread") - endif () - - if (NOT WIN32) - if (TARGET Qt5::Core) - if(NOT QT_VISIBILITY_AVAILABLE) - message(FATAL_ERROR "Qt compiled without support for -fvisibility=hidden. This will break plugins and linking of some applications. Please fix your Qt installation (try passing --reduce-exports to configure).") - endif() - endif() - endif() -endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - - -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -w1 -Wpointer-arith -fno-common") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -w1 -Wpointer-arith -fno-exceptions -fno-common") - -endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") - -set(CMAKE_CXX_VISIBILITY_PRESET hidden) -set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") +#elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + # Note that exceptions are enabled by default when building with clang. That + # is, -fno-exceptions is not set in CMAKE_CXX_FLAGS below. This is because a + # lot of code in different KDE modules ends up including code that throws + # exceptions. Most (or all) of the occurrences are in template code that + # never gets instantiated. Contrary to GCC, ICC and MSVC, clang (most likely + # rightfully) complains about that. Trying to work around the issue by + # passing -fdelayed-template-parsing brings other problems, as noted in + # http://lists.kde.org/?l=kde-core-devel&m=138157459706783&w=2. + # The generated code will be slightly bigger, but there is no way to avoid + # it. +elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" AND NOT WIN32) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") +#elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR +# (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) + # FIXME: are exceptions disabled by default on WIN32? +endif() macro(_kdecompilersettings_append_exception_flag VAR) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") @@ -323,8 +207,6 @@ function(KDE_SOURCE_FILES_ENABLE_EXCEPTIONS) endfunction() function(KDE_TARGET_ENABLE_EXCEPTIONS target mode) - # TODO we need to unset QT_NO_EXCEPTIONS for non-GNU compilers - # (although we currently do not set it anyway) target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:MSVC>:-EHsc>") if (WIN32) target_compile_options(${target} ${mode} "$<$<CXX_COMPILER_ID:Intel>:-EHsc>") @@ -338,7 +220,6 @@ function(KDE_TARGET_ENABLE_EXCEPTIONS target mode) endfunction() function(KDE_ENABLE_EXCEPTIONS) - # We set CMAKE_CXX_FLAGS, rather than add_compile_options(), because # we only want to affect the compilation of C++ source files. @@ -353,6 +234,121 @@ function(KDE_ENABLE_EXCEPTIONS) _kdecompilersettings_append_exception_flag(CMAKE_CXX_FLAGS) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE) - endfunction() + + +############################################################ +# Better diagnostics (warnings, errors) +############################################################ + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR + "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR + ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" AND NOT WIN32)) + # Linker warnings should be treated as errors + set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fatal-warnings ${CMAKE_SHARED_LINKER_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fatal-warnings ${CMAKE_MODULE_LINKER_FLAGS}") + + # Do not allow undefined symbols, even in non-symbolic shared libraries + set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_MODULE_LINKER_FLAGS}") +endif() + +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + set(_KDE_COMMON_WARNING_FLAGS "-Wall -Wextra -Wcast-align -Wchar-subscripts -Wformat-security -Wno-long-long -Wpointer-arith -Wundef") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_KDE_COMMON_WARNING_FLAGS} -Wmissing-format-attribute -Wwrite-strings") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_KDE_COMMON_WARNING_FLAGS} -Wnon-virtual-dtor -Woverloaded-virtual") + + # Make some warnings errors + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror=implicit-function-declaration") + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=return-type") +endif() + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" AND NOT WIN32) + # -w1 turns on warnings and errors + # FIXME: someone needs to have a closer look at the Intel compiler options + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -w1 -Wpointer-arith") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -w1 -Wpointer-arith") +endif() + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR + (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) + # FIXME: do we not want to set the warning level up to level 3? (/W3) + # FIXME: does Intel really follow MSVC *this* closely on Windows? + # Disable warnings: + # C4250: 'class1' : inherits 'class2::member' via dominance + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250") + # C4251: 'identifier' : class 'type' needs to have dll-interface to be + # used by clients of class 'type2' + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") + # C4396: 'identifier' : 'function' the inline specifier cannot be used + # when a friend declaration refers to a specialization of a + # function template + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4396") + # C4661: 'identifier' : no suitable definition provided for explicit + # template instantiation request + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4661") +endif() + +if (WIN32) + # Disable deprecation warnings for some API + # FIXME: do we really want this? + add_definitions(-D_CRT_SECURE_NO_DEPRECATE + -D_CRT_SECURE_NO_WARNINGS + -D_CRT_NONSTDC_NO_DEPRECATE + -D_SCL_SECURE_NO_WARNINGS + ) +endif() + + + +############################################################ +# Hacks +# +# Anything in this section should be thoroughly documented, +# including what problems it is supposed to fix and in what +# circumstances those problems occur. Include links to any +# relevant bug reports. +############################################################ + +if (APPLE) + # FIXME: is this still needed? + # Apple's Mach-O linker apparently does not like having uninitialized global + # variables in a common block (only relevant for C code) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common") + + # FIXME: why are these needed? + set (CMAKE_SHARED_LINKER_FLAGS "-single_module -multiply_defined suppress ${CMAKE_SHARED_LINKER_FLAGS}") + set (CMAKE_MODULE_LINKER_FLAGS "-multiply_defined suppress ${CMAKE_MODULE_LINKER_FLAGS}") +endif() + +if (UNIX AND NOT APPLE) + # We apparently need to explicitly link against libc + # FIXME: really!? + set(CMAKE_SHARED_LINKER_FLAGS "-lc ${CMAKE_SHARED_LINKER_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "-lc ${CMAKE_MODULE_LINKER_FLAGS}") +endif() + +if (WIN32) + if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR + (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) + # make sure that no header adds libcmt by default using + # #pragma comment(lib, "libcmt.lib") as done by mfc/afx.h + set(CMAKE_EXE_LINKER_FLAGS "/NODEFAULTLIB:libcmt /DEFAULTLIB:msvcrt ${CMAKE_EXE_LINKER_FLAGS}") + endif() +endif() + +if (MINGW AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + # FIXME: do our export macros not deal with this properly? + # maybe we only need this for modules? + set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--export-all-symbols") + set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--export-all-symbols") +endif() + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR + "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR + ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" AND NOT WIN32)) + # FIXME: why? + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common") +endif() + |