[llvm] r177992 - Add CMake option LLVM_USE_SANITIZER={Address, Memory, MemoryWithOrigins} to simplify bootstrap of LLVM/Clang under ASan/MSan
Alexey Samsonov
samsonov at google.com
Tue Mar 26 00:49:47 PDT 2013
Author: samsonov
Date: Tue Mar 26 02:49:46 2013
New Revision: 177992
URL: http://llvm.org/viewvc/llvm-project?rev=177992&view=rev
Log:
Add CMake option LLVM_USE_SANITIZER={Address,Memory,MemoryWithOrigins} to simplify bootstrap of LLVM/Clang under ASan/MSan
Modified:
llvm/trunk/CMakeLists.txt
llvm/trunk/cmake/modules/HandleLLVMOptions.cmake
Modified: llvm/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/CMakeLists.txt?rev=177992&r1=177991&r2=177992&view=diff
==============================================================================
--- llvm/trunk/CMakeLists.txt (original)
+++ llvm/trunk/CMakeLists.txt Tue Mar 26 02:49:46 2013
@@ -193,6 +193,9 @@ if( LLVM_USE_OPROFILE )
endif( NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
endif( LLVM_USE_OPROFILE )
+set(LLVM_USE_SANITIZER "" CACHE STRING
+ "Define the sanitizer used to build binaries and tests.")
+
# Define an option controlling whether we should build for 32-bit on 64-bit
# platforms, where supported.
if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
Modified: llvm/trunk/cmake/modules/HandleLLVMOptions.cmake
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/cmake/modules/HandleLLVMOptions.cmake?rev=177992&r1=177991&r2=177992&view=diff
==============================================================================
--- llvm/trunk/cmake/modules/HandleLLVMOptions.cmake (original)
+++ llvm/trunk/cmake/modules/HandleLLVMOptions.cmake Tue Mar 26 02:49:46 2013
@@ -60,9 +60,9 @@ else(WIN32)
endif(WIN32)
function(add_flag_or_print_warning flag)
- check_c_compiler_flag(${flag} C_SUPPORTS_${flag})
- check_cxx_compiler_flag(${flag} CXX_SUPPORTS_${flag})
- if (C_SUPPORTS_${flag} AND CXX_SUPPORTS_${flag})
+ check_c_compiler_flag(${flag} C_SUPPORTS_FLAG)
+ check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG)
+ if (C_SUPPORTS_FLAG AND CXX_SUPPORTS_FLAG)
message(STATUS "Building with ${flag}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
@@ -85,6 +85,13 @@ function(append_if condition value)
endif()
endfunction()
+macro(add_flag_if_supported flag)
+ check_c_compiler_flag(${flag} C_SUPPORTS_FLAG)
+ append_if(C_SUPPORTS_FLAG "${flag}" CMAKE_C_FLAGS)
+ check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG)
+ append_if(CXX_SUPPORTS_FLAG "${flag}" CMAKE_CXX_FLAGS)
+endmacro()
+
if( LLVM_ENABLE_PIC )
if( XCODE )
# Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
@@ -223,6 +230,38 @@ elseif( LLVM_COMPILER_IS_GCC_COMPATIBLE
endif (LLVM_ENABLE_WERROR)
endif( MSVC )
+macro(append_common_sanitizer_flags)
+ # Append -fno-omit-frame-pointer and turn on debug info to get better
+ # stack traces.
+ add_flag_if_supported("-fno-omit-frame-pointer")
+ if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
+ NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
+ add_flag_if_supported("-gline-tables-only")
+ endif()
+endmacro()
+
+# Turn on sanitizers if necessary.
+if(LLVM_USE_SANITIZER)
+ if (LLVM_ON_UNIX)
+ if (LLVM_USE_SANITIZER STREQUAL "Address")
+ append_common_sanitizer_flags()
+ add_flag_or_print_warning("-fsanitize=address")
+ elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
+ append_common_sanitizer_flags()
+ add_flag_or_print_warning("-fsanitize=memory")
+ # -pie is required for MSan.
+ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie")
+ if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
+ add_flag_or_print_warning("-fsanitize-memory-track-origins")
+ endif()
+ else()
+ message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
+ endif()
+ else()
+ message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
+ endif()
+endif()
+
add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
add_llvm_definitions( -D__STDC_FORMAT_MACROS )
add_llvm_definitions( -D__STDC_LIMIT_MACROS )
More information about the llvm-commits
mailing list