Skip to content

Commit

Permalink
perf: add include and exclude flags to instruction functions (#100)
Browse files Browse the repository at this point in the history
Signed-off-by: msclock <msclock@qq.com>
  • Loading branch information
msclock committed Apr 14, 2024
1 parent f5e8892 commit f71cff9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 9 deletions.
10 changes: 8 additions & 2 deletions cmake/Common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Example:
check_and_append_flags(FLAGS "-fno-omit-frame-pointer" TARGETS CMAKE_C_FLAGS CMAKE_CXX_FLAGS REQUIRED)
]]
function(check_and_append_flag)
set(_opts REQUIRED)
set(_opts REQUIRED QUOTELESS)
set(_single_opts FLAGS)
set(_multi_opts TARGETS)
cmake_parse_arguments(PARSE_ARGV 0 "arg" "${_opts}" "${_single_opts}"
Expand All @@ -130,8 +130,14 @@ function(check_and_append_flag)
message(DEBUG " Appending ${arg_FLAGS} to ${arg_TARGETS}")

foreach(_target ${arg_TARGETS})
if(arg_QUOTELESS)
append_variable_quoteless("${arg_FLAGS}" ${_target})
else()
append_variable("${arg_FLAGS}" ${_target})
endif()
# forwarded variables are not updated in the parent scope
set(${_target}
"${${_target}} ${arg_FLAGS}"
"${${_target}}"
PARENT_SCOPE)
endforeach()
else()
Expand Down
38 changes: 38 additions & 0 deletions cmake/build/CompilerFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ unset(compiler_warnings_cuda)
Function to apply compiler warnings to a target.
]]
function(warn_target target)
set(_opts)
set(_single_opts)
set(_multi_opts EXCLUDE_FLAGS INCLUDE_FLAGS)
cmake_parse_arguments(PARSE_ARGV 0 arg "${_opts}" "${_single_opts}"
"${_multi_opts}")

if(COMPILER_FLAGS_SKIP_TARGETS_REGEXES)
foreach(regex ${COMPILER_FLAGS_SKIP_TARGETS_REGEXES})
if(target MATCHES "${regex}")
Expand All @@ -178,6 +184,38 @@ function(warn_target target)
get_target_property(_cxx compiler_flags_warnings _cxx)
get_target_property(_cuda compiler_flags_warnings _cuda)

if(arg_INCLUDE_FLAGS)
message(VERBOSE
"Including flags: ${arg_INCLUDE_FLAGS} for target ${target}")
foreach(_include_flag ${arg_INCLUDE_FLAGS})
check_and_append_flag(FLAGS "${_include_flag}" TARGETS _c QUOTELESS)
check_and_append_flag(FLAGS "${_include_flag}" TARGETS _cxx QUOTELESS)
check_and_append_flag(FLAGS "${_include_flag}" TARGETS _cuda QUOTELESS)
endforeach()
message(
VERBOSE
"Compiler warnings flags with included flags for ${target}:
Compiler Warnings for C: ${_c}
Compiler Warnings for CXX: ${_cxx}
Compiler Warnings for CUDA: ${_cuda}")
endif()

if(arg_EXCLUDE_FLAGS)
message(VERBOSE
"Excluding flags: ${arg_EXCLUDE_FLAGS} for target ${target}")
foreach(_exclude_flag ${arg_EXCLUDE_FLAGS})
list(REMOVE_ITEM _c "${_exclude_flag}")
list(REMOVE_ITEM _cxx "${_exclude_flag}")
list(REMOVE_ITEM _cuda "${_exclude_flag}")
endforeach()
message(
VERBOSE
"Compiler warnings flags with excluded flags for ${target}:
Compiler Warnings for C: ${_c}
Compiler Warnings for CXX: ${_cxx}
Compiler Warnings for CUDA: ${_cuda}")
endif()

message(
VERBOSE
"Applying compiler warnings to target ${target} by ${CMAKE_CURRENT_FUNCTION}:
Expand Down
31 changes: 31 additions & 0 deletions cmake/build/Hardening.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ message(
STATUS
"Use hardening compilation with USE_HARDENING: ${USE_HARDENING}
Hardening Options:
USE_HARDENING: Enable hardening compilation flags. Default is ${USE_HARDENING}.
USE_HARDENING_FLAGS: Default is ${USE_HARDENING_FLAGS}
USE_HARDENING_LINKS: Default is ${USE_HARDENING_LINKS}
USE_HARDENING_SKIP_TARGETS_REGEXES: List of regexes to skip targts. Default is empty."
Expand Down Expand Up @@ -152,6 +153,12 @@ unset(hardening_flags)
unset(hardening_links)

function(harden_target target)
set(_opts)
set(_single_opts)
set(_multi_opts EXCLUDE_FLAGS INCLUDE_FLAGS)
cmake_parse_arguments(PARSE_ARGV 0 arg "${_opts}" "${_single_opts}"
"${_multi_opts}")

if(NOT USE_HARDENING)
message(
VERBOSE
Expand Down Expand Up @@ -191,6 +198,30 @@ function(harden_target target)
set(FLAGS ${_flags} ${target_flags})
set(LINKS ${_links} ${target_flags})

if(arg_INCLUDE_FLAGS)
message(VERBOSE
"Including flags: ${arg_INCLUDE_FLAGS} for target ${target}")
foreach(_include_flag ${arg_INCLUDE_FLAGS})
check_and_append_flag(FLAGS "${_include_flag}" TARGETS FLAGS QUOTELESS)
check_and_append_flag(FLAGS "${_include_flag}" TARGETS LINKS QUOTELESS)
endforeach()
message(VERBOSE "Hardening flags with included flags for ${target}:
Hardening compiling flags: ${FLAGS}
Hardening linking flags: ${LINKS}")
endif()

if(arg_EXCLUDE_FLAGS)
message(VERBOSE
"Excluding flags: ${arg_EXCLUDE_FLAGS} for target ${target}")
foreach(_exclude_flag ${arg_EXCLUDE_FLAGS})
list(REMOVE_ITEM FLAGS "${_exclude_flag}")
list(REMOVE_ITEM LINKS "${_exclude_flag}")
endforeach()
message(VERBOSE "Hardening flags with excluded flags for ${target}:
Hardening compiling flags: ${FLAGS}
Hardening linking flags: ${LINKS}")
endif()

message(VERBOSE "Hardening target ${target} by ${CMAKE_CURRENT_FUNCTION}:
Hardening compiling flags: ${FLAGS}
Hardening linking flags: ${LINKS}")
Expand Down
54 changes: 47 additions & 7 deletions cmake/build/Sanitizer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ Note:

]]
function(sanitize_target target)
set(_opts)
set(_single_opts)
set(_multi_opts EXCLUDE_FLAGS INCLUDE_FLAGS)
cmake_parse_arguments(PARSE_ARGV 0 arg "${_opts}" "${_single_opts}"
"${_multi_opts}")

if(NOT USE_SANITIZER)
return()
endif()
Expand Down Expand Up @@ -283,30 +289,64 @@ function(sanitize_target target)
get_target_property(_san sanitizer_flags _san)

if(NOT MSVC)
set(FLAGS FLAGS ${_san})
set(FLAGS ${_san})
foreach(sanitizer address memory undefined thread leak cfi)
if("-fsanitize=${sanitizer}" IN_LIST _san)
list(APPEND _links "-fsanitize=${sanitizer}")
endif()
endforeach()

if(_links)
set(LINKS LINKS ${_links})
set(LINKS ${_links})
endif()
else()
# MSVC support
if(NOT USE_SANITIZER MATCHES [[enablemsvcannotations]])
# https://learn.microsoft.com/en-us/answers/questions/864574/enabling-address-sanitizer-results-in-error-lnk203
# https://learn.microsoft.com/en-us/cpp/sanitizers/error-container-overflow?view=msvc-170
set(DEFINITIONS DEFINITIONS _DISABLE_VECTOR_ANNOTATION
_DISABLE_STRING_ANNOTATION)
set(DEFINITIONS _DISABLE_VECTOR_ANNOTATION _DISABLE_STRING_ANNOTATION)
endif()

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

if(arg_INCLUDE_FLAGS)
message(VERBOSE
"Including flags: ${arg_INCLUDE_FLAGS} for target ${target}")
foreach(_include_flag ${arg_INCLUDE_FLAGS})
check_and_append_flag(FLAGS "${_include_flag}" TARGETS FLAGS QUOTELESS)
check_and_append_flag(FLAGS "${_include_flag}" TARGETS LINKS QUOTELESS)
endforeach()
message(VERBOSE "Sanitize flags with included flags for ${target}:
Sanitize compiling flags: ${FLAGS}
Sanitize linking flags: ${LINKS}")
endif()

if(arg_EXCLUDE_FLAGS)
message(VERBOSE
"Excluding flags: ${arg_EXCLUDE_FLAGS} for target ${target}")
foreach(_exclude_flag ${arg_EXCLUDE_FLAGS})
list(REMOVE_ITEM FLAGS "${_exclude_flag}")
list(REMOVE_ITEM LINKS "${_exclude_flag}")
endforeach()
message(VERBOSE "Sanitize flags with excluded flags for ${target}:
Sanitize compiling flags: ${FLAGS}
Sanitize linking flags: ${LINKS}")
endif()

options_target(${target} ${FLAGS} ${LINKS} ${DEFINITIONS})
message(VERBOSE "Sanitize target ${target} by ${CMAKE_CURRENT_FUNCTION}:
Sanitize compiling flags: ${FLAGS}
Sanitize linking flags: ${LINKS}")

options_target(
${target}
FLAGS
${FLAGS}
LINKS
${LINKS}
DEFINITIONS
${DEFINITIONS})

copy_sanitizer_runtime(${target})
endfunction()

0 comments on commit f71cff9

Please sign in to comment.