Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: customize and rename some flags #80

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 34 additions & 47 deletions cmake/build/CompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include_guard(GLOBAL)

include(${CMAKE_CURRENT_LIST_DIR}/../Common.cmake)

set(COMPILER_WARNINGS_MSVC
set(COMPILER_FLAGS_WARNINGS_CXX
/W4 # Baseline reasonable warnings
/w14242 # 'identifier': conversion from 'type1' to 'type2', possible loss of
# data
Expand Down Expand Up @@ -40,10 +40,6 @@ set(COMPILER_WARNINGS_MSVC
/w14928 # illegal copy-initialization; more than one user-defined conversion
# has been implicitly applied
/permissive- # standards conformance mode for MSVC compiler.
)

set(COMPILER_WARNINGS_GNU
# GNU
-Wall # all warnings on
-Wextra # reasonable and standard
-Wshadow # warn the user if a variable declaration shadows one from a parent
Expand Down Expand Up @@ -77,7 +73,7 @@ set(COMPILER_WARNINGS_GNU
# 'override' or 'final'
)

set(COMPILER_WARNINGS_CUDA
set(COMPILER_FLAGS_WARNINGS_CUDA
-Wall # Wall all warnings
-Wextra # Reasonable and standard extra warnings
-Wunused # Warn on anything being unused
Expand All @@ -86,63 +82,52 @@ set(COMPILER_WARNINGS_CUDA
# context
)

set(COMPILER_WARNINGS_AS_ERRORS
# MSVC
/WX "Enable warnings as errors in MSVC"
# GNU
-Werror "Enable warnings as errors in GNU")

option(COMPILER_FLAGS_WARNINGS_AS_ERRORS "Treat Warnings As Errors" OFF)

message(
STATUS
"Use Compiler flags:
Compiler Flags Options:
COMPILER_WARNINGS_MSVC: ${COMPILER_WARNINGS_MSVC}
COMPILER_WARNINGS_GNU: ${COMPILER_WARNINGS_GNU}
COMPILER_WARNINGS_CUDA: ${COMPILER_WARNINGS_CUDA}
COMPILER_WARNINGS_AS_ERRORS: ${COMPILER_WARNINGS_AS_ERRORS}
COMPILER_FLAGS_WARNINGS_CXX: ${COMPILER_FLAGS_WARNINGS_CXX}
COMPILER_FLAGS_WARNINGS_CUDA: ${COMPILER_FLAGS_WARNINGS_CUDA}
COMPILER_FLAGS_WARNINGS_AS_ERRORS: If treat warnings as errors. Default is OFF.
COMPILER_FLAGS_SKIP_TARGETS_REGEXES: List of regexes to skip targets. Default is empty."
)

if(MSVC)
set(_warnings_cxx_temp ${COMPILER_WARNINGS_MSVC})
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES
".*Clang")
set(_warnings_cxx_temp ${COMPILER_WARNINGS_GNU})
else()
message(
AUTHOR_WARNING
"No compiler warnings set for CXX compiler: '${CMAKE_CXX_COMPILER_ID}'")
endif()

message(VERBOSE "Check Compiler Warnings CXX: ${_warnings_cxx_temp}")

foreach(_warn ${_warnings_cxx_temp})
foreach(_warn ${COMPILER_FLAGS_WARNINGS_CXX})
check_and_append_flag(FLAGS "${_warn}" TARGETS compiler_warnings_cxx)
endforeach()

if(COMPILER_FLAGS_WARNINGS_AS_ERRORS)
check_and_append_flag(FLAGS "${COMPILER_WARNINGS_AS_ERRORS}" TARGETS
compiler_warnings_cxx)
foreach(_warn /WX -Werror)
check_and_append_flag(FLAGS "${_warn}" TARGETS compiler_warnings_cxx)
endforeach()
endif()

unset(_warnings_cxx_temp)

# use the same warning flags for C
set(compiler_warnings_c "${compiler_warnings_cxx}")

foreach(_warn ${COMPILER_WARNINGS_CUDA})
foreach(_warn ${COMPILER_FLAGS_WARNINGS_CUDA})
check_and_append_flag(FLAGS "${_warn}" TARGETS compiler_warnings_cuda)
endforeach()

flags_to_list(compiler_warnings_cxx "${compiler_warnings_cxx}")
flags_to_list(compiler_warnings_c "${compiler_warnings_c}")
flags_to_list(compiler_warnings_cxx "${compiler_warnings_cxx}")
flags_to_list(compiler_warnings_cuda "${compiler_warnings_cuda}")
message(STATUS "Final Compiler Warnings for C: ${compiler_warnings_c}")
message(STATUS "Final Compiler Warnings for CXX: ${compiler_warnings_cxx}")
message(STATUS "Final Compiler Warnings for CUDA: ${compiler_warnings_cuda}")
message(STATUS "Compiler final warnings for C: ${compiler_warnings_c}")
message(STATUS "Compiler final warnings for CXX: ${compiler_warnings_cxx}")
message(STATUS "Compiler final warnings for CUDA: ${compiler_warnings_cuda}")

add_custom_target(compiler_flags_warnings)
set_target_properties(compiler_flags_warnings
PROPERTIES _c "${compiler_warnings_c}")
set_target_properties(compiler_flags_warnings
PROPERTIES _cxx "${compiler_warnings_cxx}")
set_target_properties(compiler_flags_warnings
PROPERTIES _cuda "${compiler_warnings_cuda}")
unset(compiler_warnings_c)
unset(compiler_warnings_cxx)
unset(compiler_warnings_cuda)

#[[
Function to apply compiler warnings to a target.
Expand All @@ -160,18 +145,20 @@ function(warn_target target)
endforeach()
endif()

get_target_property(_c compiler_flags_warnings _c)
get_target_property(_cxx compiler_flags_warnings _cxx)
get_target_property(_cuda compiler_flags_warnings _cuda)

message(
VERBOSE
"Applying compiler warnings to target ${target} by ${CMAKE_CURRENT_FUNCTION}:
Compiler Warnings for CXX: ${compiler_warnings_cxx}
Compiler Warnings for C: ${compiler_warnings_c}
Compiler Warnings for CUDA: ${compiler_warnings_cuda}")
Compiler Warnings for C: ${_c}
Compiler Warnings for CXX: ${_cxx}
Compiler Warnings for CUDA: ${_cuda}")

options_target(
${target}
FLAGS
$<$<COMPILE_LANGUAGE:CXX>:${compiler_warnings_cxx}> # C++ warnings
$<$<COMPILE_LANGUAGE:C>:${_warnings_cxx_temp}> # C warnings
$<$<COMPILE_LANGUAGE:CUDA>:${compiler_warnings_cuda}> # Cuda warnings
${target} FLAGS $<$<COMPILE_LANGUAGE:C>:${_c}> # C warnings
$<$<COMPILE_LANGUAGE:CXX>:${_cxx}> # C++ warnings
$<$<COMPILE_LANGUAGE:CUDA>:${_cuda}> # Cuda warnings
)
endfunction()
22 changes: 17 additions & 5 deletions cmake/build/Hardening.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ endif()
flags_to_list(hardening_links "${hardening_links}")

# Handle the conflics between hardening ubsan and asan
if(san_available_flags MATCHES "-fsanitize=address")
if(san_available_flags
AND san_available_flags MATCHES "-fsanitize=address"
AND hardening_flags MATCHES "-fsanitize-minimal-runtime")
message(
WARNING "Try to disable usan minimal runtime due to conflict with asan")
list(REMOVE_ITEM hardening_flags "-fsanitize=undefined"
Expand All @@ -134,8 +136,15 @@ if(san_available_flags MATCHES "-fsanitize=address")
"-fsanitize-minimal-runtime" "-fno-sanitize-recover=undefined")
endif()

message(STATUS "Final Hardening flags: ${hardening_flags}")
message(STATUS "Final Hardening links: ${hardening_links}")
message(STATUS "Hardening final flags: ${hardening_flags}")
message(STATUS "Hardening final links: ${hardening_links}")

add_custom_target(hardening_flags)
set_target_properties(hardening_flags PROPERTIES _flags "${hardening_flags}")
set_target_properties(hardening_flags PROPERTIES _links "${hardening_links}")

unset(hardening_flags)
unset(hardening_links)

function(harden_target target)
if(NOT USE_HARDENING)
Expand Down Expand Up @@ -171,8 +180,11 @@ function(harden_target target)
endif()
endif()

set(FLAGS ${hardening_flags} ${target_flags})
set(LINKS ${hardening_links} ${target_flags})
get_target_property(_flags hardening_flags _flags)
get_target_property(_links hardening_flags _links)

set(FLAGS ${_flags} ${target_flags})
set(LINKS ${_links} ${target_flags})

message(VERBOSE "Hardening target ${target} by ${CMAKE_CURRENT_FUNCTION}:
Hardening compiling flags: ${FLAGS}
Expand Down
28 changes: 16 additions & 12 deletions cmake/build/Sanitizer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,13 @@ message(
STATUS
"Use sanitizer with USE_SANITIZER: ${USE_SANITIZER}
Sanitizer Options:
USE_SANITIZER:
OFF - disable sanitizer.
Address - detects most issues dealing with memory, checking with ${USE_SANITIZER_ASAN_FLAGS}.
Memory - detects uninitialized reads, checking with ${USE_SANITIZER_MSAN_FLAGS}.
Undefined - detects the use of various undefined behaviours, checking with ${USE_SANITIZER_USAN_FLAGS}.
Thread - detects data races for multi-threaded code, checking with ${USE_SANITIZER_TSAN_FLAGS}.
Leak - detects memory leaks, checking with ${USE_SANITIZER_LSAN_FLAGS}.
CFI - detects potential undefined behaviours to subvert the program's control flow, checking with ${USE_SANITIZER_CFI_FLAGS}.
EnableMSVCAnnotations - enable Microsoft Visual C++ annotations.
USE_SANITIZER: OFF, Address, Memory, Undefined, Thread, Leak, CFI, EnableMSVCAnnotations
USE_SANITIZER_ASAN_FLAGS: ${USE_SANITIZER_ASAN_FLAGS}
USE_SANITIZER_MSAN_FLAGS: ${USE_SANITIZER_MSAN_FLAGS}
USE_SANITIZER_USAN_FLAGS: ${USE_SANITIZER_USAN_FLAGS}
USE_SANITIZER_TSAN_FLAGS: ${USE_SANITIZER_TSAN_FLAGS}
USE_SANITIZER_LSAN_FLAGS: ${USE_SANITIZER_LSAN_FLAGS}
USE_SANITIZER_CFI_FLAGS: ${USE_SANITIZER_CFI_FLAGS}
USE_SANITIZER_EXTRA_FLAGS: Extra flags to pass to the sanitizer. Default to empty.
USE_SANITIZER_BLACKLIST_FILE: Path to a blacklist file for Undefined sanitizer. Default to empty.
USE_SANITIZER_SKIP_TARGETS_REGEXES: Regexes to skip targets to sanitize. Default to enable all targets instrumented.
Expand Down Expand Up @@ -241,6 +239,10 @@ endif()
flags_to_list(san_available_flags "${san_available_flags}")
message(STATUS "Sanitizer final flags: ${san_available_flags}")

add_custom_target(sanitizer_flags)
set_target_properties(sanitizer_flags PROPERTIES _san "${san_available_flags}")
unset(san_available_flags)

#[[
A function to copy sanitizer runtime when open ASAN flags on windows.Basically,
it copy clang_rt.asan*.dll to target location.
Expand Down Expand Up @@ -331,10 +333,12 @@ function(sanitize_target target)
endforeach()
endif()

get_target_property(_san sanitizer_flags _san)

if(NOT MSVC)
set(FLAGS FLAGS ${san_available_flags})
set(FLAGS FLAGS ${_san})
foreach(sanitizer address memory undefined thread leak cfi)
if("-fsanitize=${sanitizer}" IN_LIST san_available_flags)
if("-fsanitize=${sanitizer}" IN_LIST _san)
list(APPEND _links "-fsanitize=${sanitizer}")
endif()
endforeach()
Expand All @@ -351,7 +355,7 @@ function(sanitize_target target)
_DISABLE_STRING_ANNOTATION)
endif()

set(FLAGS FLAGS ${san_available_flags} /Zi /INCREMENTAL:NO)
set(FLAGS FLAGS ${_san} /Zi /INCREMENTAL:NO)
set(LINKS LINKS /INCREMENTAL:NO)
endif()

Expand Down
Loading