Add cxx-benchmark-unittests target
authorEric Fiselier <eric@efcs.ca>
Tue, 13 Nov 2018 23:08:31 +0000 (23:08 +0000)
committerEric Fiselier <eric@efcs.ca>
Tue, 13 Nov 2018 23:08:31 +0000 (23:08 +0000)
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.

llvm-svn: 346811

libcxx/CMakeLists.txt
libcxx/benchmarks/CMakeLists.txt
libcxx/cmake/Modules/HandleLibcxxFlags.cmake

index ea6571125d872ae23c170347b412bf8295129ecd..88bf4961fe87ec0cd00ece5f7f2c5017eeccf43c 100644 (file)
@@ -643,38 +643,48 @@ endif()
 
 # 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.
   # 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
-        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()
-    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()
-    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()
-      message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
+      message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${USE_SANITIZER}")
     endif()
-  elseif(LLVM_USE_SANITIZER AND MSVC)
+  elseif(USE_SANITIZER AND MSVC)
     message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
   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()
 
 # Configuration file flags =====================================================
index 5989dafdcad3f7587d71eaa006d40cf30b9a8d7b..c5b4bf0fba1f3fd39b43976c1ddfbc5334a9ec16 100644 (file)
@@ -11,6 +11,7 @@ set(BENCHMARK_LIBCXX_COMPILE_FLAGS
     -isystem ${LIBCXX_SOURCE_DIR}/include
     -L${LIBCXX_LIBRARY_DIR}
     -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
+    ${SANITIZER_FLAGS}
     )
 if (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH)
   list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
@@ -87,11 +88,13 @@ set(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS
     -nostdinc++
     -isystem ${LIBCXX_SOURCE_DIR}/include
     ${BENCHMARK_TEST_COMPILE_FLAGS}
+    ${SANITIZER_FLAGS}
     -Wno-user-defined-literals
 )
 set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS
     -nodefaultlibs
     -L${BENCHMARK_LIBCXX_INSTALL}/lib/
+    ${SANITIZER_FLAGS}
 )
 set(BENCHMARK_TEST_NATIVE_COMPILE_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_NATIVE_COMPILE_FLAGS)
 split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS)
+
+set(libcxx_benchmark_targets)
+
 macro(add_benchmark_test name source_file)
   set(libcxx_target ${name}_libcxx)
+  list(APPEND libcxx_benchmark_targets ${libcxx_target})
   add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
   add_dependencies(${libcxx_target} cxx google-benchmark-libcxx)
   add_dependencies(cxx-benchmarks ${libcxx_target})
@@ -123,6 +130,9 @@ macro(add_benchmark_test name source_file)
     target_link_libraries(${libcxx_target} cxx_filesystem)
   endif()
   target_link_libraries(${libcxx_target} -lbenchmark)
+  if (LLVM_USE_SANITIZER)
+    target_link_libraries(${libcxx_target} -ldl)
+  endif()
   set_target_properties(${libcxx_target}
     PROPERTIES
           OUTPUT_NAME "${name}.libcxx.out"
@@ -169,3 +179,16 @@ foreach(test_path ${BENCHMARK_TESTS})
   endif()
   add_benchmark_test(${test_name} ${test_file})
 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()
index 24fe0c5d5fb7c7dc406a8b5b09a97fe5029dc55e..fb60318a330d27d1367d2edc5cd640b5ee5ec3f8 100644 (file)
@@ -45,6 +45,29 @@ macro(check_flag_supported flag)
     check_cxx_compiler_flag("${flag}" "LIBCXX_SUPPORTS_${flagname}_FLAG")
 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.
 macro(define_if condition def)
   if (${condition})