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.
This commit is contained in:
Robert Adam 2022-11-13 17:30:54 +01:00
parent f8c79aed15
commit 2e7e7ee666
4 changed files with 33 additions and 31 deletions

View File

@ -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"
)

View File

@ -3,6 +3,7 @@
# that can be found in the LICENSE file at the root of the
# Mumble source tree or at <https://www.mumble.info/LICENSE>.
include(CompilerFlags)
include(CheckCXXCompilerFlag)
if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
@ -20,15 +21,26 @@ if(WIN32)
add_compile_definitions(_WIN32_WINNT=0x0601)
endif()
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()
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)
add_compile_options(
"$<$<CONFIG:Release>:/Ox>"
"$<$<CONFIG:Release>:/fp:fast>"
)
# Needed in order to not run into C1128: number of sections exceeded object file format limit
add_compile_options(/bigobj)
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()

View File

@ -14,6 +14,7 @@ if(BUILD_OVERLAY_XCOMPILE)
LANGUAGES CXX
)
list(APPEND CMAKE_MODULE_PATH "${3RDPARTY_DIR}/cmake-compiler-flags")
include("${MUMBLE_SOURCE_ROOT}/cmake/compiler.cmake")
endif()
@ -162,6 +163,7 @@ if(64_BIT AND MSVC)
"-DMUMBLE_SOURCE_ROOT=${CMAKE_SOURCE_DIR}"
"-DMUMBLE_BINARY_DIR=${CMAKE_BINARY_DIR}"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-D3RDPARTY_DIR=${3RDPARTY_DIR}"
# Force MSVC, because CMake prioritizes MinGW over it.
"-DCMAKE_C_COMPILER=cl.exe"
"-DCMAKE_CXX_COMPILER=cl.exe"
@ -184,6 +186,7 @@ if(64_BIT AND MSVC)
"-DMUMBLE_SOURCE_ROOT=${CMAKE_SOURCE_DIR}"
"-DMUMBLE_BINARY_DIR=${CMAKE_BINARY_DIR}"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-D3RDPARTY_DIR=${3RDPARTY_DIR}"
# Force MSVC, because CMake prioritizes MinGW over it.
"-DCMAKE_C_COMPILER=cl.exe"
"-DCMAKE_CXX_COMPILER=cl.exe"

View File

@ -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)