From 6e9e348c5b2708fb4963d006905c13d32f34c7b2 Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Mon, 15 Jul 2024 14:14:30 -0700 Subject: [PATCH] Add IS_CXX option to sanitizer cmake module --- cmake/AwsSanitizers.cmake | 42 ++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/cmake/AwsSanitizers.cmake b/cmake/AwsSanitizers.cmake index 54202006eb..7c2de399b3 100644 --- a/cmake/AwsSanitizers.cmake +++ b/cmake/AwsSanitizers.cmake @@ -2,22 +2,32 @@ # SPDX-License-Identifier: Apache-2.0. include(CheckCCompilerFlag) +include(CheckCXXCompilerFlag) option(ENABLE_SANITIZERS "Enable sanitizers in debug builds" OFF) set(SANITIZERS "address;undefined" CACHE STRING "List of sanitizers to build with") -# This function checks if a sanitizer is available +# This function checks if a given sanitizer is available. +# Parameters: +# sanitizer: The sanitizer to check. +# [out_variable]: The variable to assign the result to. Defaults to HAS_SANITIZER_${sanitizer}. # Options: -# sanitizer: The sanitizer to check -# out_variable: The variable to assign the result to. Defaults to HAS_SANITIZER_${sanitizer} +# IS_CXX: This option switches the function to check the CXX compiler. function(aws_check_sanitizer sanitizer) - list(LENGTH ARGN extra_count) + set(options IS_CXX) + cmake_parse_arguments(ARG "${options}" "" "" ${ARGN}) + + # Determine the number of extra parameters passed to the function. + list(LENGTH ARG_UNPARSED_ARGUMENTS extra_count) + if(${extra_count} EQUAL 0) + # Use the default out variable. set(out_variable HAS_SANITIZER_${sanitizer}) # Sanitize the variable name to remove illegal characters string(MAKE_C_IDENTIFIER ${out_variable} out_variable) elseif(${extra_count} EQUAL 1) - set(out_variable ${ARGN}) + # Use the out variable provided by a function caller. + set(out_variable ${ARG_UNPARSED_ARGUMENTS}) else() message(FATAL_ERROR "Error: aws_check_sanitizer() called with multiple out variables") endif() @@ -32,23 +42,37 @@ function(aws_check_sanitizer sanitizer) # Need to set this here so that the flag is passed to the linker set(CMAKE_REQUIRED_FLAGS ${sanitizer_test_flag}) - check_c_compiler_flag(${sanitizer_test_flag} ${out_variable}) + if(ARG_IS_CXX) + check_cxx_compiler_flag(${sanitizer_test_flag} ${out_variable}) + else() + check_c_compiler_flag(${sanitizer_test_flag} ${out_variable}) + endif() else() set(${out_variable} 0 PARENT_SCOPE) endif() endfunction() # This function enables sanitizers on the given target -# Options: +# Parameters: # SANITIZERS: The list of extra sanitizers to enable +# Options: +# IS_CXX: This option switches the function to check the CXX compiler. function(aws_add_sanitizers target) + set(options IS_CXX) set(multiValueArgs SANITIZERS) - cmake_parse_arguments(SANITIZER "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + cmake_parse_arguments(SANITIZER "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) if (NOT ENABLE_SANITIZERS) return() endif() + # A cmake function's option can be forwarded using a variables with value set to this options. + if(SANITIZER_IS_CXX) + set(is_cxx IS_CXX) + else() + set(is_cxx "") + endif() + list(APPEND SANITIZER_SANITIZERS ${SANITIZERS}) foreach(sanitizer IN LISTS SANITIZER_SANITIZERS) @@ -57,7 +81,7 @@ function(aws_add_sanitizers target) # Sanitize the variable name to remove illegal characters string(MAKE_C_IDENTIFIER ${sanitizer_variable} sanitizer_variable) - aws_check_sanitizer(${sanitizer} ${sanitizer_variable}) + aws_check_sanitizer(${sanitizer} ${sanitizer_variable} ${is_cxx}) if(${${sanitizer_variable}}) if (NOT "${PRESENT_SANITIZERS}" STREQUAL "") set(PRESENT_SANITIZERS "${PRESENT_SANITIZERS},")