From c99b9797e1b212e20d008a99492ae3c6504699e9 Mon Sep 17 00:00:00 2001 From: Robert Adam Date: Sun, 13 Nov 2022 17:30:54 +0100 Subject: [PATCH] BUILD(cmake): Module to fetch compiler-dependent flags Using a separate module instead of encoding the different flags for different compilers into our own cmake source code should clean things up a bit and make the intention more clear (as the flags sometimes have rather cryptic names). This commit also contains a functional change in that it removes the "fast-math" compile option (from optimized builds) as this is incompatible with at least Opus. --- CMakeLists.txt | 1 + cmake/compiler.cmake | 56 ++++++++++++++++++++------------------------ src/CMakeLists.txt | 2 ++ 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c5180625424..1d45412096d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" "${CMAKE_SOURCE_DIR}/cmake/FindModules" "${3RDPARTY_DIR}/FindPythonInterpreter" + "${3RDPARTY_DIR}/cmake-compiler-flags" ) diff --git a/cmake/compiler.cmake b/cmake/compiler.cmake index 7e7b4582217..b76d791059e 100644 --- a/cmake/compiler.cmake +++ b/cmake/compiler.cmake @@ -3,6 +3,7 @@ # that can be found in the LICENSE file at the root of the # Mumble source tree or at . +include(CompilerFlags) include(CheckCXXCompilerFlag) if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) @@ -20,15 +21,26 @@ if(WIN32) add_compile_definitions(_WIN32_WINNT=0x0601) endif() -if(MSVC) - add_compile_options( - "$<$:/Ox>" - "$<$:/fp:fast>" - ) +set(WANTED_FEATURES "ENABLE_MOST_WARNINGS") + +if(CMAKE_BUILD_TYPE STREQUAL "Release") + list(APPEND WANTED_FEATURES "OPTIMIZE_FOR_SPEED") +elseif(CMAKE_BUILD_TYPE STREQUAL "Debug") + list(APPEND WANTED_FEATURES "OPTIMIZE_FOR_DEBUG") +endif() - # Needed in order to not run into C1128: number of sections exceeded object file format limit - add_compile_options(/bigobj) +if(warnings-as-errors) + list(APPEND WANTED_FEATURES "ENABLE_WARNINGS_AS_ERRORS") +endif() + +get_compiler_flags( + ${WANTED_FEATURES} + OUTPUT_VARIABLE MUMBLE_COMPILER_FLAGS +) + +message(STATUS "Using (among others) the following compiler flags: ${MUMBLE_COMPILER_FLAGS}") +if(MSVC) if(32_BIT) # SSE2 code is generated by default, unless an explicit arch is set. # Our 32 bit binaries should not contain any SSE2 code, so override the default. @@ -54,35 +66,17 @@ if(MSVC) "/ignore:4099" ) endif() - - if(warnings-as-errors) - add_compile_options("/WX") - add_link_options("/WX") - endif() elseif(UNIX OR MINGW) add_compile_options( "-fvisibility=hidden" - "-Wall" - "-Wextra" ) - # Avoid "File too big" error - check_cxx_compiler_flag("-Wa,-mbig-obj" COMPILER_HAS_MBIG_OBJ) - if (${COMPILER_HAS_MBIG_OBJ}) - add_compile_options("-Wa,-mbig-obj") - endif() - if(optimize) add_compile_options( - "-O3" "-march=native" ) endif() - if(warnings-as-errors) - add_compile_options("-Werror") - endif() - if(APPLE) add_link_options("-Wl,-dead_strip") @@ -129,9 +123,11 @@ elseif(UNIX OR MINGW) endif() function(target_disable_warnings TARGET) - if(MSVC) - target_compile_options(${TARGET} PRIVATE "/w") - else() - target_compile_options(${TARGET} PRIVATE "-w") - endif() + get_compiler_flags( + DISABLE_ALL_WARNINGS + DISABLE_DEFAULT_FLAGS + OUTPUT_VARIABLE NO_WARNING_FLAGS + ) + + target_compile_options(${TARGET} PRIVATE ${NO_WARNING_FLAGS}) endfunction() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0bd281cc5a6..f1b7cb02df7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,8 @@ find_pkg(OpenSSL find_pkg(Protobuf REQUIRED) +add_compile_options(${MUMBLE_COMPILER_FLAGS}) + add_library(shared STATIC) protobuf_generate(LANGUAGE cpp TARGET shared PROTOS ${PROTO_FILE} OUT_VAR BUILT_PROTO_FILES)