Skip to content

Commit

Permalink
[cmake] unify compiler settings
Browse files Browse the repository at this point in the history
move settings to cmake/CompilerFlags.cmake and
cmake/CXXCompilerFlags.cmake
  • Loading branch information
akallabeth committed Nov 14, 2024
1 parent 2953c78 commit ad2ae90
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 247 deletions.
143 changes: 1 addition & 142 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,141 +203,16 @@ if(BUILD_TESTING_INTERNAL)
add_compile_definitions(BUILD_TESTING_INTERNAL)
elseif(BUILD_TESTING)
set(EXPORT_ALL_SYMBOLS OFF CACHE BOOL "testing default" FORCE)
else()
option(EXPORT_ALL_SYMBOLS "Export all symbols form library" OFF)
endif()

if (EXPORT_ALL_SYMBOLS)
# set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
add_compile_definitions(EXPORT_ALL_SYMBOLS)
endif(EXPORT_ALL_SYMBOLS)

# Compiler-specific flags
if(CMAKE_COMPILER_IS_GNUCC)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "i686")
CHECK_SYMBOL_EXISTS(__x86_64__ "" IS_X86_64)
if(IS_X86_64)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=i686")
endif()
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")

if(NOT EXPORT_ALL_SYMBOLS)
message(STATUS "GCC default symbol visibility: hidden")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
endif()
CHECK_C_COMPILER_FLAG (-Wimplicit-function-declaration Wimplicit-function-declaration)
if(Wimplicit-function-declaration)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wimplicit-function-declaration")
endif()

if (NOT OPENBSD)
CHECK_C_COMPILER_FLAG (-Wredundant-decls Wredundant-decls)
if(Wredundant-decls)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wredundant-decls")
endif()
endif()
add_compile_definitions("$<$<CONFIG:Release>:NDEBUG>")
add_compile_definitions("$<$<CONFIG:MinSizeRel>:NDEBUG>")
add_compile_options("$<$<CONFIG:RelWithDebInfo>:-g>")
add_compile_options("$<$<CONFIG:Debug>:-g>")
endif()
include(ExportAllSymbols)

set(THREAD_PREFER_PTHREAD_FLAG TRUE)

if(NOT IOS)
find_package(Threads REQUIRED)
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)
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "-fsanitize=address")
CHECK_C_COMPILER_FLAG ("-fsanitize=address" fsanitize-address)
CHECK_C_COMPILER_FLAG ("-fsanitize-blacklist=${PROJECT_BINARY_DIR}/foo.txt" fsanitize-blacklist)
CHECK_C_COMPILER_FLAG ("-fsanitize-address-use-after-scope" fsanitize-address-use-after-scope)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")

if(fsanitize-blacklist)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-blacklist=${PROJECT_SOURCE_DIR}/scripts/blacklist-address-sanitizer.txt")
endif(fsanitize-blacklist)

if(fsanitize-address-use-after-scope)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-address-use-after-scope")
endif(fsanitize-address-use-after-scope)
elseif(WITH_SANITIZE_MEMORY)
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "-fsanitize=memory")
CHECK_C_COMPILER_FLAG ("-fsanitize=memory" fsanitize-memory)
CHECK_C_COMPILER_FLAG ("-fsanitize-blacklist=${PROJECT_BINARY_DIR}/foo.txt" fsanitize-blacklist)
CHECK_C_COMPILER_FLAG ("-fsanitize-memory-use-after-dtor" fsanitize-memory-use-after-dtor)
CHECK_C_COMPILER_FLAG ("-fsanitize-memory-track-origins" fsanitize-memory-track-origins)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=memory")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=memory")

if(fsanitize-blacklist)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-blacklist=${PROJECT_SOURCE_DIR}/scripts/blacklist-memory-sanitizer.txt")
endif(fsanitize-blacklist)

if (fsanitize-memory-use-after-dtor)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-memory-use-after-dtor")
endif(fsanitize-memory-use-after-dtor)

if (fsanitize-memory-track-origins)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-memory-track-origins")
endif(fsanitize-memory-track-origins)
elseif(WITH_SANITIZE_THREAD)
list(APPEND CMAKE_REQUIRED_LINK_OPTIONS "-fsanitize=thread")
CHECK_C_COMPILER_FLAG ("-fsanitize=thread" fsanitize-thread)
CHECK_C_COMPILER_FLAG ("-fsanitize-blacklist=${PROJECT_BINARY_DIR}/foo.txt" fsanitize-blacklist)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
if(fsanitize-blacklist)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-blacklist=${PROJECT_SOURCE_DIR}/scripts/blacklist-thread-sanitizer.txt")
endif(fsanitize-blacklist)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=thread")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
endif()

file(REMOVE ${PROJECT_BINARY_DIR}/foo.txt)
set(CMAKE_REQUIRED_LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS_SAVED})

if (WITH_NO_UNDEFINED)
CHECK_C_COMPILER_FLAG (-Wl,--no-undefined no-undefined)

if(no-undefined)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-undefined" )
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined" )
endif()
endif()
endif()

if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})

add_compile_options("$<$<CONFIG:Release>:/Zi>")

add_compile_definitions(_CRT_NONSTDC_NO_DEPRECATE)
endif()

if(ANDROID)
# workaround for https://github.com/android-ndk/ndk/issues/243
string(REPLACE "-g " "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
endif()

if(WIN32)
add_compile_definitions(UNICODE _UNICODE)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
Expand Down Expand Up @@ -446,12 +321,6 @@ if(ANDROID)
endif(WITH_GPROF)
endif()

if(WITH_VALGRIND_MEMCHECK)
check_include_files(valgrind/memcheck.h FREERDP_HAVE_VALGRIND_MEMCHECK_H)
else()
unset(FREERDP_HAVE_VALGRIND_MEMCHECK_H CACHE)
endif()

if(UNIX OR CYGWIN)
set(WAYLAND_FEATURE_TYPE "RECOMMENDED")
else()
Expand Down Expand Up @@ -631,16 +500,6 @@ else()
set(FREERDP_PROXY_PLUGINDIR "${PROXY_PLUGINDIR}")
endif()

# Android profiling
if(ANDROID)
if(WITH_GPROF)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -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()

# Unit Tests

include(CTest)
Expand Down
3 changes: 1 addition & 2 deletions cmake/CFlagsToVar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ function(CFlagsToVar NAME CFG)
string(REPLACE "${CMAKE_SOURCE_DIR}" "<src dir>" C_FLAGS "${C_FLAGS}")
string(REPLACE "${CMAKE_BINARY_DIR}" "<build dir>" C_FLAGS "${C_FLAGS}")

string(APPEND C_FLAGS " ${CMAKE_C_FLAGS_${UCFG}}")
string(APPEND C_FLAGS " ${CMAKE_C_FLAGS_${UCFG}}")

string(REPLACE "\$" "\\\$" C_FLAGS "${C_FLAGS}")
string(REPLACE "\"" "\\\"" C_FLAGS "${C_FLAGS}")
set(${NAME} ${C_FLAGS} PARENT_SCOPE)
endfunction()

44 changes: 15 additions & 29 deletions cmake/CXXCompilerFlags.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include(CheckCXXCompilerFlag)
include(CommonCompilerFlags)

macro (checkCXXFlag FLAG)
check_cxx_compiler_flag("${FLAG}" CXXFLAG${FLAG})
Expand All @@ -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,
Expand All @@ -25,24 +23,9 @@ 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
list(APPEND COMMON_COMPILER_FLAGS
-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
Expand All @@ -51,18 +34,11 @@ if (ENABLE_WARNING_VERBOSE)
-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)
foreach(FLAG ${COMMON_COMPILER_FLAGS})
CheckCXXFlag(${FLAG})
endforeach()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_compile_options($<$<NOT:$<CONFIG:Debug>>:-fdebug-prefix-map=${CMAKE_SOURCE_DIR}=.>)
Expand All @@ -78,6 +54,16 @@ if (WIN32)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-DNOMINMAX>)
endif()

if(MSVC)
add_compile_options(/Gd)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})

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}")
Expand Down
28 changes: 28 additions & 0 deletions cmake/CheckAndSetFlag.cmake
Original file line number Diff line number Diff line change
@@ -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()
50 changes: 50 additions & 0 deletions cmake/CommonCompilerFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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")
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()


Loading

0 comments on commit ad2ae90

Please sign in to comment.