[PATCH] Add CMake option LLVM_USE_SANITIZER to allow bootstrap of LLVM/Clang under ASan/MSan.
Alexey Samsonov
samsonov at google.com
Mon Feb 25 05:59:12 PST 2013
Hi chandlerc, eugenis,
This flag adds CMake variable
LLVM_USE_SANITIZER={Address,Memory,MemoryWithOrigins} that would set up
compile flags necessary for building LLVM under chosen sanitizer.
http://llvm-reviews.chandlerc.com/D459
Files:
CMakeLists.txt
cmake/modules/HandleLLVMOptions.cmake
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -193,6 +193,9 @@
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 )
Index: cmake/modules/HandleLLVMOptions.cmake
===================================================================
--- cmake/modules/HandleLLVMOptions.cmake
+++ cmake/modules/HandleLLVMOptions.cmake
@@ -4,6 +4,7 @@
include(AddLLVMDefinitions)
include(CheckCCompilerFlag)
+include(CheckCXXCompilerFlag)
if( CMAKE_COMPILER_IS_GNUCXX )
set(LLVM_COMPILER_IS_GCC_COMPATIBLE ON)
@@ -58,32 +59,47 @@
endif(UNIX)
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)
+ 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)
+ else()
+ message(WARNING "${flag} is not supported.")
+ endif()
+endfunction()
+
+function(append_if variable value condition)
+ if (${condition})
+ set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
+ endif()
+endfunction()
+
+function(add_flag_if_supported flag)
+ check_c_compiler_flag(${flag} C_SUPPORTS_FLAG)
+ append_if(CMAKE_C_FLAGS "${flag}" C_SUPPORTS_FLAG)
+ check_cxx_compiler_flag(${flag} CXX_SUPPORTS_FLAG)
+ append_if(CMAKE_CXX_FLAGS "${flag}" CXX_SUPPORTS_FLAG)
+endfunction()
+
if( LLVM_ENABLE_PIC )
if( XCODE )
# Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
# know how to disable this, so just force ENABLE_PIC off for now.
message(WARNING "-fPIC not supported with Xcode.")
elseif( WIN32 OR CYGWIN)
# On Windows all code is PIC. MinGW warns if -fPIC is used.
else()
- include(CheckCXXCompilerFlag)
- check_cxx_compiler_flag("-fPIC" SUPPORTS_FPIC_FLAG)
- if( SUPPORTS_FPIC_FLAG )
- message(STATUS "Building with -fPIC")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
- set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
- else( SUPPORTS_FPIC_FLAG )
- message(WARNING "-fPIC not supported.")
- endif()
+ add_flag_or_print_warning("-fPIC")
if( WIN32 OR CYGWIN)
# MinGW warns if -fvisibility-inlines-hidden is used.
else()
check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
- if( SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG )
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
- endif()
- endif()
+ append_if(CMAKE_CXX_FLAGS "-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
+ endif()
endif()
endif()
@@ -194,30 +210,43 @@
if (LLVM_ENABLE_PEDANTIC)
add_llvm_definitions( -pedantic -Wno-long-long )
check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
- if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
- set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
- endif()
+ append_if(CMAKE_CXX_FLAGS "-Wno-nested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
endif (LLVM_ENABLE_PEDANTIC)
check_cxx_compiler_flag("-Werror -Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
- if( CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG )
- set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wcovered-switch-default" )
- endif()
+ append_if(CMAKE_CXX_FLAGS "-Wcovered-switch-default" CXX_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
check_c_compiler_flag("-Werror -Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
- if( C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG )
- set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcovered-switch-default" )
- endif()
- if (USE_NO_UNINITIALIZED)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-uninitialized")
- endif()
- if (USE_NO_MAYBE_UNINITIALIZED)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized")
- endif()
+ append_if(CMAKE_C_FLAGS "-Wcovered-switch-default" C_SUPPORTS_COVERED_SWITCH_DEFAULT_FLAG)
+ append_if(CMAKE_CXX_FLAGS "-Wno-uninitialized" USE_NO_UNINITIALIZED)
+ append_if(CMAKE_CXX_FLAGS "-Wno-maybe-uninitialized" USE_NO_MAYBE_UNINITIALIZED)
endif (LLVM_ENABLE_WARNINGS)
if (LLVM_ENABLE_WERROR)
add_llvm_definitions( -Werror )
endif (LLVM_ENABLE_WERROR)
endif( MSVC )
+function(append_common_sanitizer_flags)
+ if(NOT LLVM_ENABLE_PIC)
+ message(WARNING "LLVM_USE_SANITIZER can only be used with LLVM_ENABLE_PIC")
+ endif()
+ # Append -fno-omit-frame-pointer and -g to get better stack traces.
+ add_flag_if_supported("-fno-omit-frame-pointer")
+ add_flag_if_supported("-g")
+endfunction()
+
+string(TOUPPER "${LLVM_USE_SANITIZER}" uppercase_LLVM_USE_SANITIZER)
+if (uppercase_LLVM_USE_SANITIZER STREQUAL "ADDRESS")
+ append_common_sanitizer_flags()
+ add_flag_or_print_warning("-fsanitize=address")
+elseif (uppercase_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(uppercase_LLVM_USE_SANITIZER STREQUAL "MEMORYWITHORIGINS")
+ add_flag_or_print_warning("-fsanitize-memory-track-origins")
+ endif()
+endif()
+
add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
add_llvm_definitions( -D__STDC_FORMAT_MACROS )
add_llvm_definitions( -D__STDC_LIMIT_MACROS )
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D459.1.patch
Type: text/x-patch
Size: 5857 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130225/f78e582b/attachment.bin>
More information about the llvm-commits
mailing list