[libcxx] r215872 - [libcxx] Add support for LLVM_USE_SANITIZER to libcxx when being built standalone and in-tree

Eric Fiselier eric at efcs.ca
Sun Aug 17 22:03:47 PDT 2014


Author: ericwf
Date: Mon Aug 18 00:03:46 2014
New Revision: 215872

URL: http://llvm.org/viewvc/llvm-project?rev=215872&view=rev
Log:
[libcxx] Add support for LLVM_USE_SANITIZER to libcxx when being built standalone and in-tree

Summary:
This patch adds support for LLVM_USE_SANITIZER when being built in-tree and standalone. 

This patch does the following things:
1. define the LLVM_USE_SANITIZER option to "" when being built standalone. This also helps show we support it.
2. Translate LLVM_USE_SANITIZER when standalone in a very similar way done in llvm/cmake/HandleLLVMOptions.cmake.
3. Add config.llvm_use_sanitizer to lit.site.cfg.in
4. Add code to translate config.llvm_use_sanitizer's value into the needed compile flags in lit.cfg.

Currently lit.cfg assumes that that the compiler supports '-fno-omit-frame-pointer' while CMakeLists.txt actually checks to see if its supported. We could pass this information to lit but I'm not sure its needed. 

Reviewers: mclow.lists, danalbert

Reviewed By: danalbert

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D4949

Modified:
    libcxx/trunk/CMakeLists.txt
    libcxx/trunk/cmake/config-ix.cmake
    libcxx/trunk/test/lit.cfg
    libcxx/trunk/test/lit.site.cfg.in

Modified: libcxx/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/CMakeLists.txt?rev=215872&r1=215871&r2=215872&view=diff
==============================================================================
--- libcxx/trunk/CMakeLists.txt (original)
+++ libcxx/trunk/CMakeLists.txt Mon Aug 18 00:03:46 2014
@@ -43,6 +43,10 @@ option(LIBCXX_ENABLE_WERROR "Fail and st
 option(LIBCXX_ENABLE_CXX1Y "Enable -std=c++1y and use of c++1y language features if the compiler supports it." OFF)
 option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
 option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON)
+if (LIBCXX_BUILT_STANDALONE)
+  set(LLVM_USE_SANITIZER "" CACHE STRING
+      "Define the sanitizer used to build the library and tests")
+endif()
 
 set(CXXABIS none libcxxabi libcxxrt libstdc++ libsupc++)
 if (NOT LIBCXX_CXX_ABI)
@@ -285,6 +289,29 @@ if (MSVC)
   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 endif()
 
+# Configure for sanitizers. If LIBCXX_BUILT_STANDALONE then we have to do
+# the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
+if (LIBCXX_BUILT_STANDALONE)
+  # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
+  # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do.
+  if (LLVM_USE_SANITIZER AND NOT MSVC)
+    append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_OMIT_FRAME_POINTER_FLAG
+            "-fno-omit-frame-pointer")
+    if (LLVM_USE_SANITIZER STREQUAL "Address")
+      list(APPEND LIBCXX_CXX_FEATURE_FLAGS "-fsanitize=address")
+    elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
+      list(APPEND LIBCXX_CXX_FEATURE_FLAGS "-fsanitize=memory")
+      if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
+        list(APPEND LIBCXX_CXX_FEATURE_FLAGS "-fsanitize-memory-track-origins")
+      endif()
+    else()
+      message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
+    endif()
+  elseif(MSVC)
+    message(WARNING "LLVM_USE_SANITIZER is not supported with MSVC")
+  endif()
+endif()
+
 string(REPLACE ";" " " LIBCXX_CXX_REQUIRED_FLAGS "${LIBCXX_CXX_REQUIRED_FLAGS}")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_REQUIRED_FLAGS}")
 

Modified: libcxx/trunk/cmake/config-ix.cmake
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/cmake/config-ix.cmake?rev=215872&r1=215871&r2=215872&view=diff
==============================================================================
--- libcxx/trunk/cmake/config-ix.cmake (original)
+++ libcxx/trunk/cmake/config-ix.cmake Mon Aug 18 00:03:46 2014
@@ -2,27 +2,28 @@ include(CheckLibraryExists)
 include(CheckCXXCompilerFlag)
 
 # Check compiler flags
-check_cxx_compiler_flag(-std=c++11            LIBCXX_HAS_STDCXX11_FLAG)
-check_cxx_compiler_flag(-std=c++1y            LIBCXX_HAS_STDCXX1Y_FLAG)
-check_cxx_compiler_flag(-fPIC                 LIBCXX_HAS_FPIC_FLAG)
-check_cxx_compiler_flag(-nodefaultlibs        LIBCXX_HAS_NODEFAULTLIBS_FLAG)
-check_cxx_compiler_flag(-nostdinc++           LIBCXX_HAS_NOSTDINCXX_FLAG)
-check_cxx_compiler_flag(-Wall                 LIBCXX_HAS_WALL_FLAG)
-check_cxx_compiler_flag(-W                    LIBCXX_HAS_W_FLAG)
-check_cxx_compiler_flag(-Wno-unused-parameter LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG)
-check_cxx_compiler_flag(-Wwrite-strings       LIBCXX_HAS_WWRITE_STRINGS_FLAG)
-check_cxx_compiler_flag(-Wno-long-long        LIBCXX_HAS_WNO_LONG_LONG_FLAG)
-check_cxx_compiler_flag(-pedantic             LIBCXX_HAS_PEDANTIC_FLAG)
-check_cxx_compiler_flag(-Werror               LIBCXX_HAS_WERROR_FLAG)
-check_cxx_compiler_flag(-Wno-error            LIBCXX_HAS_WNO_ERROR_FLAG)
-check_cxx_compiler_flag(-fno-exceptions       LIBCXX_HAS_FNO_EXCEPTIONS_FLAG)
-check_cxx_compiler_flag(-fno-rtti             LIBCXX_HAS_FNO_RTTI_FLAG)
-check_cxx_compiler_flag(/WX                   LIBCXX_HAS_WX_FLAG)
-check_cxx_compiler_flag(/WX-                  LIBCXX_HAS_NO_WX_FLAG)
-check_cxx_compiler_flag(/EHsc                 LIBCXX_HAS_EHSC_FLAG)
-check_cxx_compiler_flag(/EHs-                 LIBCXX_HAS_NO_EHS_FLAG)
-check_cxx_compiler_flag(/EHa-                 LIBCXX_HAS_NO_EHA_FLAG)
-check_cxx_compiler_flag(/GR-                  LIBCXX_HAS_NO_GR_FLAG)
+check_cxx_compiler_flag(-std=c++11              LIBCXX_HAS_STDCXX11_FLAG)
+check_cxx_compiler_flag(-std=c++1y              LIBCXX_HAS_STDCXX1Y_FLAG)
+check_cxx_compiler_flag(-fPIC                   LIBCXX_HAS_FPIC_FLAG)
+check_cxx_compiler_flag(-fno-omit-frame-pointer LIBCXX_HAS_NO_OMIT_FRAME_POINTER_FLAG)
+check_cxx_compiler_flag(-nodefaultlibs          LIBCXX_HAS_NODEFAULTLIBS_FLAG)
+check_cxx_compiler_flag(-nostdinc++             LIBCXX_HAS_NOSTDINCXX_FLAG)
+check_cxx_compiler_flag(-Wall                   LIBCXX_HAS_WALL_FLAG)
+check_cxx_compiler_flag(-W                      LIBCXX_HAS_W_FLAG)
+check_cxx_compiler_flag(-Wno-unused-parameter   LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG)
+check_cxx_compiler_flag(-Wwrite-strings         LIBCXX_HAS_WWRITE_STRINGS_FLAG)
+check_cxx_compiler_flag(-Wno-long-long          LIBCXX_HAS_WNO_LONG_LONG_FLAG)
+check_cxx_compiler_flag(-pedantic               LIBCXX_HAS_PEDANTIC_FLAG)
+check_cxx_compiler_flag(-Werror                 LIBCXX_HAS_WERROR_FLAG)
+check_cxx_compiler_flag(-Wno-error              LIBCXX_HAS_WNO_ERROR_FLAG)
+check_cxx_compiler_flag(-fno-exceptions         LIBCXX_HAS_FNO_EXCEPTIONS_FLAG)
+check_cxx_compiler_flag(-fno-rtti               LIBCXX_HAS_FNO_RTTI_FLAG)
+check_cxx_compiler_flag(/WX                     LIBCXX_HAS_WX_FLAG)
+check_cxx_compiler_flag(/WX-                    LIBCXX_HAS_NO_WX_FLAG)
+check_cxx_compiler_flag(/EHsc                   LIBCXX_HAS_EHSC_FLAG)
+check_cxx_compiler_flag(/EHs-                   LIBCXX_HAS_NO_EHS_FLAG)
+check_cxx_compiler_flag(/EHa-                   LIBCXX_HAS_NO_EHA_FLAG)
+check_cxx_compiler_flag(/GR-                    LIBCXX_HAS_NO_GR_FLAG)
 
 # Check libraries
 check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)

Modified: libcxx/trunk/test/lit.cfg
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/lit.cfg?rev=215872&r1=215871&r2=215872&view=diff
==============================================================================
--- libcxx/trunk/test/lit.cfg (original)
+++ libcxx/trunk/test/lit.cfg Mon Aug 18 00:03:46 2014
@@ -314,6 +314,22 @@ else:
     lit_config.note('using user specified std: \'-std={}\''.format(std))
 compile_flags += ['-std={}'.format(std)]
 
+built_w_san = getattr(config, 'llvm_use_sanitizer')
+if built_w_san and built_w_san.strip():
+    built_w_san = built_w_san.strip()
+    compile_flags += ['-fno-omit-frame-pointer']
+    if built_w_san == 'Address':
+        compile_flags += ['-fsanitize=address']
+        config.available_features.add('asan')
+    elif built_w_san == 'Memory' or built_w_san == 'MemoryWithOrigins':
+        compile_flags += ['-fsanitize=memory']
+        if built_w_san == 'MemoryWithOrigins':
+            compile_flags += ['-fsanitize-memory-track-origins']
+        config.available_features.add('msan')
+    else:
+        lit_config.fatal(
+            'unsupported value for libcxx_use_sanitizer: {}'.format(built_w_san))
+
 # Configure extra linker parameters.
 exec_env = {}
 if sys.platform == 'darwin':

Modified: libcxx/trunk/test/lit.site.cfg.in
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/lit.site.cfg.in?rev=215872&r1=215871&r2=215872&view=diff
==============================================================================
--- libcxx/trunk/test/lit.site.cfg.in (original)
+++ libcxx/trunk/test/lit.site.cfg.in Mon Aug 18 00:03:46 2014
@@ -6,6 +6,7 @@ config.libcxx_obj_root       = "@LIBCXX_
 config.python_executable     = "@PYTHON_EXECUTABLE@"
 config.enable_shared         = @LIBCXX_ENABLE_SHARED@
 config.cxx_abi               = "@LIBCXX_CXX_ABI_LIBNAME@"
+config.llvm_use_sanitizer    = "@LLVM_USE_SANITIZER@"
 
 # Let the main config do the real work.
 lit_config.load_config(config, "@LIBCXX_SOURCE_DIR@/test/lit.cfg")





More information about the cfe-commits mailing list