[cmake] refactor configuration

* Split common stuff to reusable files
* Disable INTERPROCEDURAL_OPTIMIZATION for SDL2/3 resource targets
This commit is contained in:
akallabeth
2024-11-12 09:36:25 +01:00
parent 2bb75c195b
commit 0ae7c4b52d
19 changed files with 181 additions and 277 deletions

View File

@@ -1,4 +1,5 @@
include(CheckCXXCompilerFlag)
include(CommonCompilerFlags)
macro(checkCXXFlag FLAG)
check_cxx_compiler_flag("${FLAG}" CXXFLAG${FLAG})
@@ -9,9 +10,6 @@ macro(checkCXXFlag FLAG)
endif()
endmacro()
option(ENABLE_WARNING_VERBOSE "enable -Weveryting (and some exceptions) for compile" OFF)
option(ENABLE_WARNING_ERROR "enable -Werror for compile" OFF)
if(ENABLE_WARNING_VERBOSE)
if(MSVC)
# Remove previous warning definitions,
@@ -21,57 +19,37 @@ if(ENABLE_WARNING_VERBOSE)
)
string(REGEX REPLACE "(^| )[/-]W[ ]*[1-9]" " " "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
endforeach()
set(C_WARNING_FLAGS /W4 /wo4324)
else()
set(C_WARNING_FLAGS
-Weverything
-Wall
-Wpedantic
-Wno-padded
-Wno-switch-enum
-Wno-cast-align
-Wno-declaration-after-statement
-Wno-unsafe-buffer-usage
-Wno-reserved-identifier
-Wno-covered-switch-default
-Wno-disabled-macro-expansion
-Wno-ctad-maybe-unsupported
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-pre-c++17-compat
-Wno-exit-time-destructors
-Wno-gnu-zero-variadic-macro-arguments
list(
APPEND
COMMON_COMPILER_FLAGS
-Wno-declaration-after-statement
-Wno-ctad-maybe-unsupported
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-pre-c++17-compat
-Wno-exit-time-destructors
-Wno-gnu-zero-variadic-macro-arguments
)
endif()
foreach(FLAG ${C_WARNING_FLAGS})
checkcxxflag(${FLAG})
endforeach()
endif()
if(ENABLE_WARNING_ERROR)
checkcxxflag(-Werror)
endif()
checkcxxflag(-fno-omit-frame-pointer)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 10)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-ffile-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fdebug-prefix-map=${CMAKE_BINARY_DIR}=./build>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fmacro-prefix-map=${CMAKE_BINARY_DIR}=./build>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-ffile-prefix-map=${CMAKE_BINARY_DIR}=./build>)
endif()
endif()
foreach(FLAG ${COMMON_COMPILER_FLAGS})
checkcxxflag(${FLAG})
endforeach()
# https://stackoverflow.com/questions/4913922/possible-problems-with-nominmax-on-visual-c
if(WIN32)
add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:NOMINMAX>)
endif()
if(MSVC)
add_compile_options(/Gd)
add_compile_options("$<$<CONFIG:Debug>:/Zi>")
add_compile_definitions(_CRT_NONSTDC_NO_DEPRECATE)
endif()
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} CACHE STRING "default CXXFLAGS")
message("Using CXXFLAGS ${CMAKE_CXX_FLAGS}")
message("Using CXXFLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}")

View File

@@ -0,0 +1,28 @@
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
macro(CheckAndSetFlag FLAG)
if(FLAG)
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
unset(C_FLAG)
unset(CXX_FLAG)
if("C" IN_LIST languages)
check_c_compiler_flag("${FLAG}" C_FLAG)
if(C_FLAG)
string(APPEND CMAKE_C_FLAGS " ${FLAG}")
endif()
endif()
if("CXX" IN_LIST languages)
check_cxx_compiler_flag("${FLAG}" CXX_FLAG)
if(CXX_FLAG)
string(APPEND CMAKE_CXX_FLAGS " ${FLAG}")
endif()
endif()
if(NOT C_FLAG AND NOT CXX_FLAG)
message(WARNING "compiler does not support ${FLAG}")
endif()
endif()
endmacro()

View File

@@ -60,6 +60,7 @@ function(cleaning_configure_file RSRC RDST)
# Create a target to recreate the configuration file if something changes.
string(SHA256 DST_HASH "${DST}")
string(SUBSTRING "${DST_HASH}" 0 8 DST_HASH)
if(NOT TARGET ct-${DST_HASH})
add_custom_target(
ct-${DST_HASH} COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${DST_DIR}"

View File

@@ -0,0 +1,46 @@
include(CheckAndSetFlag)
option(ENABLE_WARNING_VERBOSE "enable -Weveryting (and some exceptions) for compile" OFF)
option(ENABLE_WARNING_ERROR "enable -Werror for compile" OFF)
set(COMMON_COMPILER_FLAGS "")
if(ENABLE_WARNING_VERBOSE)
if(MSVC)
list(APPEND COMMON_COMPILER_FLAGS /W4 /wo4324)
else()
list(
APPEND
COMMON_COMPILER_FLAGS
-Weverything
-Wall
-Wpedantic
-Wno-padded
-Wno-switch-enum
-Wno-cast-align
-Wno-unsafe-buffer-usage
-Wno-reserved-identifier
-Wno-covered-switch-default
-Wno-disabled-macro-expansion
)
endif()
endif()
if(ENABLE_WARNING_ERROR)
list(APPEND COMMON_COMPILER_FLAGS -Werror)
endif()
list(APPEND COMMON_COMPILER_FLAGS -fno-omit-frame-pointer -Wredundant-decls)
include(ExportAllSymbols)
include(CompilerSanitizerOptions)
if(CMAKE_C_COMPILER_ID MATCHES ".*Clang.*" OR (CMAKE_C_COMPILER_ID MATCHES "GNU" AND CMAKE_C_COMPILER_VERSION
VERSION_GREATER_EQUAL 10)
)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-ffile-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fdebug-prefix-map=${CMAKE_BINARY_DIR}=./build>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fmacro-prefix-map=${CMAKE_BINARY_DIR}=./build>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-ffile-prefix-map=${CMAKE_BINARY_DIR}=./build>)
endif()

View File

@@ -1,4 +1,5 @@
include(CheckCCompilerFlag)
include(CommonCompilerFlags)
macro(checkCFlag FLAG)
check_c_compiler_flag("${FLAG}" CFLAG${FLAG})
@@ -9,9 +10,6 @@ macro(checkCFlag FLAG)
endif()
endmacro()
option(ENABLE_WARNING_VERBOSE "enable -Weveryting (and some exceptions) for compile" OFF)
option(ENABLE_WARNING_ERROR "enable -Werror for compile" OFF)
if(ENABLE_WARNING_VERBOSE)
if(MSVC)
# Remove previous warning definitions,
@@ -21,45 +19,25 @@ if(ENABLE_WARNING_VERBOSE)
)
string(REGEX REPLACE "(^| )[/-]W[ ]*[1-9]" " " "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
endforeach()
set(C_WARNING_FLAGS /W4 /wo4324)
else()
set(C_WARNING_FLAGS
-Weverything
-Wall
-Wpedantic
-Wno-padded
-Wno-switch-enum
-Wno-cast-align
-Wno-declaration-after-statement
-Wno-unsafe-buffer-usage
-Wno-reserved-identifier
-Wno-covered-switch-default
-Wno-disabled-macro-expansion
-Wno-pre-c11-compat
-Wno-gnu-zero-variadic-macro-arguments
list(APPEND COMMON_COMPILER_FLAGS -Wno-declaration-after-statement -Wno-pre-c11-compat
-Wno-gnu-zero-variadic-macro-arguments
)
endif()
foreach(FLAG ${C_WARNING_FLAGS})
checkcflag(${FLAG})
endforeach()
endif()
if(ENABLE_WARNING_ERROR)
checkcflag(-Werror)
endif()
list(APPEND COMMON_COMPILER_FLAGS -Wimplicit-function-declaration)
checkcflag(-fno-omit-frame-pointer)
foreach(FLAG ${COMMON_COMPILER_FLAGS})
checkcflag(${FLAG})
endforeach()
if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 10)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-ffile-prefix-map=${CMAKE_SOURCE_DIR}=.>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fdebug-prefix-map=${CMAKE_BINARY_DIR}=./build>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fmacro-prefix-map=${CMAKE_BINARY_DIR}=./build>)
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-ffile-prefix-map=${CMAKE_BINARY_DIR}=./build>)
# Android profiling
if(ANDROID)
if(WITH_GPROF)
checkandsetflag(-pg)
set(PROFILER_LIBRARIES "${FREERDP_EXTERNAL_PROFILER_PATH}/obj/local/${ANDROID_ABI}/libandroid-ndk-profiler.a")
include_directories(SYSTEM "${FREERDP_EXTERNAL_PROFILER_PATH}")
endif()
endif()

View File

@@ -0,0 +1,48 @@
include(CMakeDependentOption)
cmake_dependent_option(
WITH_VALGRIND_MEMCHECK "Compile with valgrind helpers." OFF
"NOT WITH_SANITIZE_ADDRESS; NOT WITH_SANITIZE_MEMORY; NOT WITH_SANITIZE_THREAD" OFF
)
cmake_dependent_option(
WITH_SANITIZE_ADDRESS "Compile with gcc/clang address sanitizer." OFF
"NOT WITH_VALGRIND_MEMCHECK; NOT WITH_SANITIZE_MEMORY; NOT WITH_SANITIZE_THREAD" OFF
)
cmake_dependent_option(
WITH_SANITIZE_MEMORY "Compile with gcc/clang memory sanitizer." OFF
"NOT WITH_VALGRIND_MEMCHECK; NOT WITH_SANITIZE_ADDRESS; NOT WITH_SANITIZE_THREAD" OFF
)
cmake_dependent_option(
WITH_SANITIZE_THREAD "Compile with gcc/clang thread sanitizer." OFF
"NOT WITH_VALGRIND_MEMCHECK; NOT WITH_SANITIZE_ADDRESS; NOT WITH_SANITIZE_MEMORY" OFF
)
if(WITH_VALGRIND_MEMCHECK)
check_include_files(valgrind/memcheck.h FREERDP_HAVE_VALGRIND_MEMCHECK_H)
else()
unset(FREERDP_HAVE_VALGRIND_MEMCHECK_H CACHE)
endif()
# Enable address sanitizer, where supported and when required
if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_REQUIRED_LINK_OPTIONS_SAVED ${CMAKE_REQUIRED_LINK_OPTIONS})
file(WRITE ${PROJECT_BINARY_DIR}/foo.txt "")
if(WITH_SANITIZE_ADDRESS)
add_compile_options(-fsanitize=address)
add_compile_options(-fsanitize-address-use-after-scope)
add_link_options(-fsanitize=address)
elseif(WITH_SANITIZE_MEMORY)
add_compile_options(-fsanitize=memory)
add_compile_options(-fsanitize-memory-use-after-dtor)
add_compile_options(-fsanitize-memory-track-origins)
add_link_options(-fsanitize=memory)
elseif(WITH_SANITIZE_THREAD)
add_compile_options(-fsanitize=thread)
add_link_options(-fsanitize=thread)
endif()
option(WITH_NO_UNDEFINED "Add -Wl,--no-undefined" OFF)
if(WITH_NO_UNDEFINED)
add_link_options(-Wl,--no-undefined)
endif()
endif()

View File

@@ -26,24 +26,7 @@ option(WITH_JPEG "Use JPEG decoding." OFF)
include(CompilerDetect)
if(NOT WIN32)
cmake_dependent_option(
WITH_VALGRIND_MEMCHECK "Compile with valgrind helpers." OFF
"NOT WITH_SANITIZE_ADDRESS; NOT WITH_SANITIZE_MEMORY; NOT WITH_SANITIZE_THREAD" OFF
)
cmake_dependent_option(
WITH_SANITIZE_ADDRESS "Compile with gcc/clang address sanitizer." OFF
"NOT WITH_VALGRIND_MEMCHECK; NOT WITH_SANITIZE_MEMORY; NOT WITH_SANITIZE_THREAD" OFF
)
cmake_dependent_option(
WITH_SANITIZE_MEMORY "Compile with gcc/clang memory sanitizer." OFF
"NOT WITH_VALGRIND_MEMCHECK; NOT WITH_SANITIZE_ADDRESS; NOT WITH_SANITIZE_THREAD" OFF
)
cmake_dependent_option(
WITH_SANITIZE_THREAD "Compile with gcc/clang thread sanitizer." OFF
"NOT WITH_VALGRIND_MEMCHECK; NOT WITH_SANITIZE_ADDRESS; NOT WITH_SANITIZE_MEMORY" OFF
)
else()
if(WIN32)
if(NOT UWP)
option(WITH_MEDIA_FOUNDATION "Enable H264 media foundation decoder." OFF)
endif()

View File

@@ -0,0 +1,11 @@
include(CheckAndSetFlag)
option(EXPORT_ALL_SYMBOLS "Export all symbols form library" OFF)
if(EXPORT_ALL_SYMBOLS)
add_compile_definitions(EXPORT_ALL_SYMBOLS)
else()
message(STATUS "${} default symbol visibility: hidden")
checkandsetflag(-fvisibility=hidden)
endif()