浏览代码

Add cxx-benchmark-unittests target

This patch adds the cxx-benchmark-unittests target so we can start
getting test coverage on the benchmarks, including building with
sanitizers. Because we're only looking for test-coverage, the benchmarks
run for the shortest time possible, and in parallel.

The target is excluded from all by default. It only
builds and runs the libcxx configurations of the benchmarks, and not
any versions built against the systems native standard library.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@346811 91177308-0d34-0410-b5e6-96231b3b80d8
Eric Fiselier 6 年之前
父节点
当前提交
2d7654ea66
共有 3 个文件被更改,包括 78 次插入22 次删除
  1. 32 22
      CMakeLists.txt
  2. 23 0
      benchmarks/CMakeLists.txt
  3. 23 0
      cmake/Modules/HandleLibcxxFlags.cmake

+ 32 - 22
CMakeLists.txt

@@ -643,38 +643,48 @@ endif()
 
 
 # Sanitizer flags =============================================================
 # Sanitizer flags =============================================================
 
 
-# Configure for sanitizers. If LIBCXX_STANDALONE_BUILD then we have to do
-# the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
-if (LIBCXX_STANDALONE_BUILD)
-  set(LLVM_USE_SANITIZER "" CACHE STRING
-      "Define the sanitizer used to build the library and tests")
+function(get_sanitizer_flags OUT_VAR  USE_SANITIZER)
+  set(SANITIZER_FLAGS)
+  set(USE_SANITIZER "${USE_SANITIZER}")
   # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
   # 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.
   # 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)
-    add_flags_if_supported("-fno-omit-frame-pointer")
-    add_flags_if_supported("-gline-tables-only")
+  if (USE_SANITIZER AND NOT MSVC)
+    append_flags_if_supported(SANITIZER_FLAGS "-fno-omit-frame-pointer")
+    append_flags_if_supported(SANITIZER_FLAGS "-gline-tables-only")
 
 
     if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
     if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
-        NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
-        add_flags_if_supported("-gline-tables-only")
+            NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
+      append_flags_if_supported(SANITIZER_FLAGS "-gline-tables-only")
     endif()
     endif()
-    if (LLVM_USE_SANITIZER STREQUAL "Address")
-      add_flags("-fsanitize=address")
-    elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
-      add_flags(-fsanitize=memory)
-      if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
-        add_flags("-fsanitize-memory-track-origins")
+    if (USE_SANITIZER STREQUAL "Address")
+      append_flags(SANITIZER_FLAGS "-fsanitize=address")
+    elseif (USE_SANITIZER MATCHES "Memory(WithOrigins)?")
+      append_flags(SANITIZER_FLAGS -fsanitize=memory)
+      if (USE_SANITIZER STREQUAL "MemoryWithOrigins")
+        append_flags(SANITIZER_FLAGS "-fsanitize-memory-track-origins")
       endif()
       endif()
-    elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
-      add_flags("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all")
-    elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
-      add_flags(-fsanitize=thread)
+    elseif (USE_SANITIZER STREQUAL "Undefined")
+      append_flags(SANITIZER_FLAGS "-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all")
+    elseif (USE_SANITIZER STREQUAL "Thread")
+      append_flags(SANITIZER_FLAGS -fsanitize=thread)
     else()
     else()
-      message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
+      message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${USE_SANITIZER}")
     endif()
     endif()
-  elseif(LLVM_USE_SANITIZER AND MSVC)
+  elseif(USE_SANITIZER AND MSVC)
     message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
     message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
   endif()
   endif()
+  set(${OUT_VAR} "${SANITIZER_FLAGS}" PARENT_SCOPE)
+endfunction()
+
+# Configure for sanitizers. If LIBCXX_STANDALONE_BUILD then we have to do
+# the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
+if (LIBCXX_STANDALONE_BUILD)
+  set(LLVM_USE_SANITIZER "" CACHE STRING
+      "Define the sanitizer used to build the library and tests")
+endif()
+get_sanitizer_flags(SANITIZER_FLAGS "${LLVM_USE_SANITIZER}")
+if (LIBCXX_STANDALONE_BUILD AND SANITIZER_FLAGS)
+  add_flags(${SANITIZER_FLAGS})
 endif()
 endif()
 
 
 # Configuration file flags =====================================================
 # Configuration file flags =====================================================

+ 23 - 0
benchmarks/CMakeLists.txt

@@ -11,6 +11,7 @@ set(BENCHMARK_LIBCXX_COMPILE_FLAGS
     -isystem ${LIBCXX_SOURCE_DIR}/include
     -isystem ${LIBCXX_SOURCE_DIR}/include
     -L${LIBCXX_LIBRARY_DIR}
     -L${LIBCXX_LIBRARY_DIR}
     -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
     -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
+    ${SANITIZER_FLAGS}
     )
     )
 if (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH)
 if (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH)
   list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
   list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
@@ -87,11 +88,13 @@ set(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS
     -nostdinc++
     -nostdinc++
     -isystem ${LIBCXX_SOURCE_DIR}/include
     -isystem ${LIBCXX_SOURCE_DIR}/include
     ${BENCHMARK_TEST_COMPILE_FLAGS}
     ${BENCHMARK_TEST_COMPILE_FLAGS}
+    ${SANITIZER_FLAGS}
     -Wno-user-defined-literals
     -Wno-user-defined-literals
 )
 )
 set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS
 set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS
     -nodefaultlibs
     -nodefaultlibs
     -L${BENCHMARK_LIBCXX_INSTALL}/lib/
     -L${BENCHMARK_LIBCXX_INSTALL}/lib/
+    ${SANITIZER_FLAGS}
 )
 )
 set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS
 set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS
   ${BENCHMARK_NATIVE_TARGET_FLAGS}
   ${BENCHMARK_NATIVE_TARGET_FLAGS}
@@ -106,8 +109,12 @@ split_list(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS)
 split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS)
 split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS)
 split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS)
 split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS)
 split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS)
 split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS)
+
+set(libcxx_benchmark_targets)
+
 macro(add_benchmark_test name source_file)
 macro(add_benchmark_test name source_file)
   set(libcxx_target ${name}_libcxx)
   set(libcxx_target ${name}_libcxx)
+  list(APPEND libcxx_benchmark_targets ${libcxx_target})
   add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
   add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
   add_dependencies(${libcxx_target} cxx google-benchmark-libcxx)
   add_dependencies(${libcxx_target} cxx google-benchmark-libcxx)
   add_dependencies(cxx-benchmarks ${libcxx_target})
   add_dependencies(cxx-benchmarks ${libcxx_target})
@@ -123,6 +130,9 @@ macro(add_benchmark_test name source_file)
     target_link_libraries(${libcxx_target} cxx_filesystem)
     target_link_libraries(${libcxx_target} cxx_filesystem)
   endif()
   endif()
   target_link_libraries(${libcxx_target} -lbenchmark)
   target_link_libraries(${libcxx_target} -lbenchmark)
+  if (LLVM_USE_SANITIZER)
+    target_link_libraries(${libcxx_target} -ldl)
+  endif()
   set_target_properties(${libcxx_target}
   set_target_properties(${libcxx_target}
     PROPERTIES
     PROPERTIES
           OUTPUT_NAME "${name}.libcxx.out"
           OUTPUT_NAME "${name}.libcxx.out"
@@ -169,3 +179,16 @@ foreach(test_path ${BENCHMARK_TESTS})
   endif()
   endif()
   add_benchmark_test(${test_name} ${test_file})
   add_benchmark_test(${test_name} ${test_file})
 endforeach()
 endforeach()
+
+
+add_custom_target(cxx-benchmark-unittests)
+foreach(libcxx_tg ${libcxx_benchmark_targets})
+  message("Adding test ${libcxx_tg}")
+  # Add a target that runs the benchmark for the smallest possible time, simply so we get test
+  # and sanitizer coverage on the targets.
+  add_custom_target(${libcxx_tg}_test
+          COMMAND ${libcxx_tg} --benchmark_min_time=0.01
+          COMMENT "Running test ${libcxx_tg}"
+          )
+  add_dependencies(cxx-benchmark-unittests ${libcxx_tg}_test)
+endforeach()

+ 23 - 0
cmake/Modules/HandleLibcxxFlags.cmake

@@ -45,6 +45,29 @@ macro(check_flag_supported flag)
     check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
     check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
 endmacro()
 endmacro()
 
 
+macro(append_flags DEST)
+  foreach(value ${ARGN})
+    list(APPEND ${DEST} ${value})
+    list(APPEND ${DEST} ${value})
+  endforeach()
+endmacro()
+
+# If the specified 'condition' is true then append the specified list of flags to DEST
+macro(append_flags_if condition DEST)
+  if (${condition})
+    list(APPEND ${DEST} ${ARGN})
+  endif()
+endmacro()
+
+# Add each flag in the list specified by DEST if that flag is supported by the current compiler.
+macro(append_flags_if_supported DEST)
+  foreach(flag ${ARGN})
+    mangle_name("${flag}" flagname)
+    check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
+    append_flags_if(LIBCXX_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})
+  endforeach()
+endmacro()
+
 # Add a macro definition if condition is true.
 # Add a macro definition if condition is true.
 macro(define_if condition def)
 macro(define_if condition def)
   if (${condition})
   if (${condition})