[ASan] Add support for running unit tests by lit (as a part of 'make check-asan' command)

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@161406 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/lit_tests/CMakeLists.txt b/lib/asan/lit_tests/CMakeLists.txt
index e48163c..2666a9a 100644
--- a/lib/asan/lit_tests/CMakeLists.txt
+++ b/lib/asan/lit_tests/CMakeLists.txt
@@ -1,15 +1,39 @@
+set(ASAN_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..)
+set(ASAN_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/..)
+
 configure_lit_site_cfg(
   ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
   ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
   )
 
-set(ASAN_TEST_DEPS
-  clang clang-headers FileCheck count not
-  clang-rt.asan-x86_64 clang-rt.asan-i386
+configure_lit_site_cfg(
+  ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.in
+  ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg
   )
 
-add_lit_testsuite(check-asan "Running the AddressSanitizer tests"
-  ${CMAKE_CURRENT_BINARY_DIR}
-  DEPENDS ${ASAN_TEST_DEPS}
+set(ASAN_TEST_DEPS
+  clang-rt.asan-x86_64 clang-rt.asan-i386
   )
-set_target_properties(check-asan PROPERTIES FOLDER "ASan tests")
+if(LLVM_INCLUDE_TESTS)
+  list(APPEND ASAN_TEST_DEPS AsanUnitTests)
+endif()
+
+if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}")
+  # Run ASan output tests only if we're not cross-compiling,
+  # and can be sure that clang would produce working binaries.
+  list(APPEND ASAN_TEST_DEPS
+    clang clang-headers FileCheck count not
+    )
+  add_lit_testsuite(check-asan "Running the AddressSanitizer tests"
+    ${CMAKE_CURRENT_BINARY_DIR}
+    DEPENDS ${ASAN_TEST_DEPS}
+    )
+  set_target_properties(check-asan PROPERTIES FOLDER "ASan tests")
+elseif(LLVM_INCLUDE_TESTS)
+  # Otherwise run only ASan unit tests.
+  add_lit_testsuite(check-asan "Running the AddressSanitizer unit tests"
+    ${CMAKE_CURRENT_BINARY_DIR}/Unit
+    DEPENDS ${ASAN_TEST_DEPS}
+    )
+  set_target_properties(check-asan PROPERTIES FOLDER "ASan unit tests")
+endif()
diff --git a/lib/asan/lit_tests/Unit/lit.cfg b/lib/asan/lit_tests/Unit/lit.cfg
new file mode 100644
index 0000000..243eb7f
--- /dev/null
+++ b/lib/asan/lit_tests/Unit/lit.cfg
@@ -0,0 +1,27 @@
+# -*- Python -*-
+
+import os
+
+def get_required_attr(config, attr_name):
+  attr_value = getattr(config, attr_name, None)
+  if not attr_value:
+    lit.fatal("No attribute %r in test configuration! You may need to run "
+              "tests from your build directory or add this attribute "
+              "to lit.site.cfg " % attr_name)
+  return attr_value
+
+# Setup attributes common for all compiler-rt projects.
+llvm_src_root = get_required_attr(config, 'llvm_src_root')
+compiler_rt_lit_unit_cfg = os.path.join(llvm_src_root, "projects",
+                                        "compiler-rt", "lib",
+                                        "lit.common.unit.cfg")
+lit.load_config(config, compiler_rt_lit_unit_cfg)
+
+# Setup config name.
+config.name = 'AddressSanitizer-Unit'
+
+# Setup test source and exec root. For unit tests, we define
+# it as build directory with ASan unit tests.
+asan_binary_dir = get_required_attr(config, "asan_binary_dir")
+config.test_exec_root = os.path.join(asan_binary_dir, "tests")
+config.test_source_root = config.test_exec_root
diff --git a/lib/asan/lit_tests/Unit/lit.site.cfg.in b/lib/asan/lit_tests/Unit/lit.site.cfg.in
new file mode 100644
index 0000000..401c3a8
--- /dev/null
+++ b/lib/asan/lit_tests/Unit/lit.site.cfg.in
@@ -0,0 +1,10 @@
+## Autogenerated by LLVM/Clang configuration.
+# Do not edit!
+
+config.target_triple = "@TARGET_TRIPLE@"
+config.llvm_src_root = "@LLVM_SOURCE_DIR@"
+config.build_type = "@CMAKE_BUILD_TYPE@"
+config.asan_binary_dir = "@ASAN_BINARY_DIR@"
+
+# Let the main config do the real work.
+lit.load_config(config, "@ASAN_SOURCE_DIR@/lit_tests/Unit/lit.cfg")
diff --git a/lib/asan/lit_tests/lit.site.cfg.in b/lib/asan/lit_tests/lit.site.cfg.in
index b4ae829..2eb9615 100644
--- a/lib/asan/lit_tests/lit.site.cfg.in
+++ b/lib/asan/lit_tests/lit.site.cfg.in
@@ -6,4 +6,4 @@
 config.clang = "@LLVM_BINARY_DIR@/bin/clang"
 
 # Let the main config do the real work.
-lit.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg")
+lit.load_config(config, "@ASAN_SOURCE_DIR@/lit_tests/lit.cfg")
diff --git a/lib/asan/tests/CMakeLists.txt b/lib/asan/tests/CMakeLists.txt
index d409d50..a17b811 100644
--- a/lib/asan/tests/CMakeLists.txt
+++ b/lib/asan/tests/CMakeLists.txt
@@ -47,10 +47,10 @@
   -DASAN_UAR=0
 )
 
-add_custom_target(AsanTests)
-set_target_properties(AsanTests PROPERTIES FOLDER "ASan tests")
+add_custom_target(AsanUnitTests)
+set_target_properties(AsanUnitTests PROPERTIES FOLDER "ASan unit tests")
 function(add_asan_test testname)
-  add_unittest(AsanTests ${testname} ${ARGN})
+  add_unittest(AsanUnitTests ${testname} ${ARGN})
   if(LLVM_BUILD_32_BITS)
     target_link_libraries(${testname} clang_rt.asan-i386)
   else()
diff --git a/lib/lit.common.unit.cfg b/lib/lit.common.unit.cfg
new file mode 100644
index 0000000..8250b4a
--- /dev/null
+++ b/lib/lit.common.unit.cfg
@@ -0,0 +1,21 @@
+# -*- Python -*-
+
+# Configuration file for 'lit' test runner.
+# This file contains common config setup rules for unit tests in various
+# compiler-rt testsuites.
+
+import os
+
+# Setup test format
+build_type = getattr(config, "build_type", "Debug")
+config.test_format = lit.formats.GoogleTest(build_type, "Test")
+
+# Setup test suffixes.
+config.suffixes = []
+
+# Propagate the temp directory. Windows requires this because it uses \Windows\
+# if none of these are present.
+if 'TMP' in os.environ:
+    config.environment['TMP'] = os.environ['TMP']
+if 'TEMP' in os.environ:
+    config.environment['TEMP'] = os.environ['TEMP']