cmake for compiler-rt: add a function to set output dirs for compiler runtimes equal to directory used by Clang driver. Use it for ASan runtime. Also, make sure that ASan unit tests depend on the ASan runtime.

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@160721 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 21fdd77..6701965 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -1,3 +1,25 @@
+# Compute the Clang version from the LLVM version.
+# FIXME: We should be able to reuse CLANG_VERSION variable calculated
+#        in Clang cmake files, instead of copying the rules here.
+string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
+       ${PACKAGE_VERSION})
+
+# Call add_clang_runtime_static_library(<target_library>) to make
+# sure that static <target_library> is built in the directory
+# where Clang driver expects to find it.
+if (APPLE)
+  set(CLANG_RUNTIME_LIB_DIR
+      ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/darwin)
+elseif (UNIX)
+  # Assume Linux.
+  set(CLANG_RUNTIME_LIB_DIR
+      ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/linux)
+endif()
+function(add_clang_runtime_static_library target_name)
+  set_target_properties(${target_name} PROPERTIES
+                        ARCHIVE_OUTPUT_DIRECTORY ${CLANG_RUNTIME_LIB_DIR})
+endfunction()
+
 # First, add the subdirectories which contain feature-based runtime libraries
 # and several convenience helper libraries.
 add_subdirectory(asan)
diff --git a/lib/asan/CMakeLists.txt b/lib/asan/CMakeLists.txt
index 5bd9e7e..ce985f5 100644
--- a/lib/asan/CMakeLists.txt
+++ b/lib/asan/CMakeLists.txt
@@ -45,6 +45,9 @@
   ASAN_NEEDS_SEGV=1
   )
 
+# FIXME: We need to build universal binaries on OS X instead of
+# two arch-specific binaries.
+
 if(CAN_TARGET_X86_64)
   add_library(clang_rt.asan-x86_64 STATIC
     ${ASAN_SOURCES}
@@ -57,6 +60,7 @@
     )
   set_property(TARGET clang_rt.asan-x86_64 APPEND PROPERTY COMPILE_DEFINITIONS
     ${ASAN_COMMON_DEFINITIONS})
+  add_clang_runtime_static_library(clang_rt.asan-x86_64)
 endif()
 if(CAN_TARGET_I386)
   add_library(clang_rt.asan-i386 STATIC
@@ -70,6 +74,7 @@
     )
   set_property(TARGET clang_rt.asan-i386 APPEND PROPERTY COMPILE_DEFINITIONS
     ${ASAN_COMMON_DEFINITIONS})
+  add_clang_runtime_static_library(clang_rt.asan-i386)
 endif()
 
 if(LLVM_INCLUDE_TESTS)
diff --git a/lib/asan/tests/CMakeLists.txt b/lib/asan/tests/CMakeLists.txt
index 8a9da45..d409d50 100644
--- a/lib/asan/tests/CMakeLists.txt
+++ b/lib/asan/tests/CMakeLists.txt
@@ -99,7 +99,7 @@
               -c -o "${output_obj}"
               ${CMAKE_CURRENT_SOURCE_DIR}/${source}
       MAIN_DEPENDENCY ${source}
-      DEPENDS clang ${ARGN}
+      DEPENDS clang clang_rt.asan-i386 clang_rt.asan-x86_64 ${ARGN}
       )
   endfunction()