Skip to content

Commit

Permalink
BUILD(cmake): Module to fetch compiler-dependent flags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Krzmbrzl committed Jan 7, 2024
1 parent f8c79ae commit 2e7e7ee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)


Expand Down
56 changes: 26 additions & 30 deletions cmake/compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,15 +21,26 @@ if(WIN32)
add_compile_definitions(_WIN32_WINNT=0x0601)
endif()

if(MSVC)
add_compile_options(
"$<$<CONFIG:Release>:/Ox>"
"$<$<CONFIG:Release>:/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.
Expand All @@ -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")

Expand Down Expand Up @@ -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()
3 changes: 3 additions & 0 deletions overlay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2e7e7ee

Please sign in to comment.