From: Manuel Nickschas Date: Wed, 22 Aug 2018 18:15:50 +0000 (+0200) Subject: cmake: Modernize compile settings; require C++14 X-Git-Tag: test-travis-01~161 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=d9e586707522241d628a90466e13722e342cc28a cmake: Modernize compile settings; require C++14 Clean up QuasselCompileSettings.cmake, treat GNU and Clang the same. Use add_compile_options() instead of CMAKE_CXX_FLAGS. Curate the list of compiler flags, and add sensible linker flags, too. Check explicitly for required compiler features using CMake's target_compile_features(), rather than hard-coding compiler versions. The set of required features basically implies that we now require a C++14 compiler (even though the new stuff isn't used in the code yet). --- diff --git a/INSTALL b/INSTALL index 03abd3ee..2de2f75c 100644 --- a/INSTALL +++ b/INSTALL @@ -23,14 +23,15 @@ Prerequisites ------------- Of course, for building Quassel you need the usual set of build tools, for -example a compiler. As we use a subset of the C++11 standard, we require a -fairly recent compiler: - -- gcc 4.8+ (available for most platforms), or -- Clang 3.3+ (available for most platforms), or -- XCode 5.0+ (available for Max OS X and based on Clang), or -- Visual C++ 2015 (available for Windows™), or -- any other compiler with decent C++11 support +example a compiler. The codebase uses the C++14 standard, so a reasonably recent +compiler is needed: + +- GCC 5.0+ (available for most platforms), or +- Clang 3.4+ (available for most platforms), or +- XCode 6.0+ (available for Max OS X and based on Clang), or +- MSVC 19+ (part of Visual Studio 2017 on Windows™) + +Other compilers may work, but are not officially supported. Furthermore, CMake 3.5 or later is required. diff --git a/cmake/QuasselCompileFeatures.cmake b/cmake/QuasselCompileFeatures.cmake new file mode 100644 index 00000000..48faef54 --- /dev/null +++ b/cmake/QuasselCompileFeatures.cmake @@ -0,0 +1,39 @@ +# The compile(r) features required to build the Quassel source. +# This list is used by CMake to determine if the compiler can build the code. +set(QUASSEL_COMPILE_FEATURES + cxx_aggregate_default_initializers + cxx_alias_templates + cxx_constexpr + cxx_decltype + cxx_default_function_template_args + cxx_defaulted_functions + cxx_defaulted_move_initializers + cxx_delegating_constructors + cxx_deleted_functions + cxx_enum_forward_declarations + cxx_explicit_conversions + cxx_final + cxx_generalized_initializers + cxx_generic_lambdas + cxx_inheriting_constructors + cxx_lambdas + cxx_lambda_init_captures + cxx_noexcept + cxx_nonstatic_member_init + cxx_nullptr + cxx_override + cxx_range_for + cxx_raw_string_literals + cxx_return_type_deduction + cxx_right_angle_brackets + cxx_rvalue_references + cxx_static_assert + cxx_strong_enums + cxx_thread_local + cxx_trailing_return_types + cxx_uniform_initialization + cxx_user_literals + cxx_variadic_macros + cxx_variadic_templates + cxx_template_template_parameters +) diff --git a/cmake/QuasselCompileSettings.cmake b/cmake/QuasselCompileSettings.cmake index 7d6f71a3..ec3e17a4 100644 --- a/cmake/QuasselCompileSettings.cmake +++ b/cmake/QuasselCompileSettings.cmake @@ -1,62 +1,63 @@ # This file contains compile flags and general build configuration for Quassel # -# (C) 2014 by the Quassel Project +# (C) 2014-2018 by the Quassel Project # # Redistribution and use is allowed according to the terms of the BSD license. # For details see the accompanying COPYING-CMAKE-SCRIPTS file. +# Helper function to check for linker flag support include(CheckCXXCompilerFlag) - -# Enable various flags on gcc -if (CMAKE_COMPILER_IS_GNUCXX) - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8") - message(FATAL_ERROR "Your compiler is too old; you need GCC 4.8+, Clang 3.3+, MSVC 19.0+, or any other compiler with full C++11 support.") - endif() - - # Let's just hope that all gccs support these options and skip the tests... - # -fno-strict-aliasing is needed apparently for Qt < 4.6 - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wnon-virtual-dtor -fno-strict-aliasing -Wundef -Wcast-align -Wpointer-arith -Wformat-security -fno-check-new -fno-common") - # set(CMAKE_CXX_FLAGS_RELEASE "-O2") # use CMake default - # set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g -O2") # use CMake default - set(CMAKE_CXX_FLAGS_DEBUG "-g -ggdb -O2 -fno-reorder-blocks -fno-schedule-insns -fno-inline") - - check_cxx_compiler_flag(-Woverloaded-virtual CXX_W_OVERLOADED_VIRTUAL) - if(CXX_W_OVERLOADED_VIRTUAL) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual") - endif() - - # Just for miniz - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing") - -# ... and for Clang -elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.3") - message(FATAL_ERROR "Your compiler is too old; you need Clang 3.3+, GCC 4.8+, MSVC 19.0+, or any other compiler with full C++11 support.") +function(check_and_set_linker_flag flag name outvar) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_FLAGS "${flag}") + check_cxx_compiler_flag("" LINKER_SUPPORTS_${name}) + if (LINKER_SUPPORTS_${name}) + set(${outvar} "${${outvar}} ${flag}" PARENT_SCOPE) endif() - - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wnon-virtual-dtor -Wno-long-long -Wundef -Wcast-align -Wchar-subscripts -Wall -W -Wextra -Wpointer-arith -Wformat-security -Woverloaded-virtual -fno-common -Wno-deprecated-register") - # set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG -DQT_NO_DEBUG") # Use CMake default - # set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g -DNDEBUG -DQT_NO_DEBUG") # Use CMake default - set(CMAKE_CXX_FLAGS_DEBUG "-g -O2 -fno-inline") - - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function -Wno-undef -fno-strict-aliasing") - -# For MSVC, at least do a version sanity check... -elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.0") - message(FATAL_ERROR "Your compiler is too old; you need at least Visual Studio 2015 (MSVC 19.0+), GCC 4.8+, Clang 3.3+, or any other compiler with full C++11 support.") - endif() - - # ... and enable exception handling (required for STL types) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") - -# Unknown/unsupported compiler + cmake_pop_check_state() +endfunction() + +# General compile settings +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED OFF) # Rely on compile features if standard is not supported +set(CMAKE_CXX_EXTENSIONS OFF) # We like to be standard conform +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# For GCC and Clang, enable a whole bunch of warnings +if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + add_compile_options( + -Wall + -Wcast-align + -Wextra + -Wformat-security + -Wno-unknown-pragmas + -Wnon-virtual-dtor + -Wpedantic + -Wundef + -fno-common + -fstack-protector-strong + -fvisibility=default + -fvisibility-inlines-hidden + "$<$>:-U_FORTIFY_SOURCE;-D_FORTIFY_SOURCE=2>" + ) + + # Check for and set linker flags + check_and_set_linker_flag("-Wl,-z,relro" RELRO LINKER_FLAGS) + check_and_set_linker_flag("-Wl,-z,now" NOW LINKER_FLAGS) + check_and_set_linker_flag("-Wl,--as-needed" AS_NEEDED LINKER_FLAGS) + + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}") else() - message(WARNING "Unknown or unsupported compiler. Make sure to enable C++11 support. Good luck.") + # For other compilers, we rely on default settings (unless someone provides a good set of options; patches welcome!) endif() # Mac build stuff if (APPLE AND DEPLOY) set(CMAKE_OSX_ARCHITECTURES "x86_64") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=10.9 -stdlib=libc++") + add_compile_options( + -mmacosx-version-min=10.9 + -stdlib=libc++ + ) endif() diff --git a/cmake/QuasselMacros.cmake b/cmake/QuasselMacros.cmake index 1c0c687c..02b3165b 100644 --- a/cmake/QuasselMacros.cmake +++ b/cmake/QuasselMacros.cmake @@ -7,6 +7,7 @@ ################################################################################################### include(CMakeParseArguments) +include(QuasselCompileFeatures) ################################################################################################### # Adds a library target for a Quassel module. @@ -41,6 +42,8 @@ function(quassel_add_module _module) PRIVATE ${CMAKE_CURRENT_BINARY_DIR} # for generated files ) + target_compile_features(${target} PUBLIC ${QUASSEL_COMPILE_FEATURES}) + # Export the target name for further use set(TARGET ${target} PARENT_SCOPE) endfunction()