[lsan] Change CMakeLists to build the common LSan module for ASan.
Also, define CAN_SANITIZE_LEAKS.

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@182383 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/CMakeLists.txt b/lib/asan/CMakeLists.txt
index 851d10a..a567a4d 100644
--- a/lib/asan/CMakeLists.txt
+++ b/lib/asan/CMakeLists.txt
@@ -85,6 +85,7 @@
               $<TARGET_OBJECTS:RTInterception.${arch}>
               $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
               $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
+              $<TARGET_OBJECTS:RTLSanCommon.${arch}>
       CFLAGS ${ASAN_CFLAGS}
       DEFS ${ASAN_COMMON_DEFINITIONS}
       SYMS asan.syms)
diff --git a/lib/lsan/CMakeLists.txt b/lib/lsan/CMakeLists.txt
index 1ac0072..beb9c8a 100644
--- a/lib/lsan/CMakeLists.txt
+++ b/lib/lsan/CMakeLists.txt
@@ -3,28 +3,46 @@
 set(LSAN_CFLAGS
   ${SANITIZER_COMMON_CFLAGS})
 
+set(LSAN_COMMON_SOURCES
+  lsan_common.cc
+  lsan_common_linux.cc)
+
 set(LSAN_SOURCES
   lsan_interceptors.cc
   lsan_allocator.cc
   lsan_thread.cc
-  lsan.cc
-  lsan_common.cc
-  lsan_common_linux.cc)
+  lsan.cc)
 
 set(LSAN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 
+# The common files need to build on every arch supported by ASan.
+# (Even if they build into dummy object files.)
+filter_available_targets(LSAN_COMMON_SUPPORTED_ARCH
+  x86_64 i386 powerpc64 powerpc)
+
+# Architectures supported by the standalone LSan.
+filter_available_targets(LSAN_SUPPORTED_ARCH
+  x86_64)
+
 set(LSAN_RUNTIME_LIBRARIES)
 
-# FIXME: support LSan for other arches.
-if(CAN_TARGET_x86_64)
-  set(arch "x86_64")
-  add_compiler_rt_static_runtime(clang_rt.lsan-${arch} ${arch}
-    SOURCES ${LSAN_SOURCES}
-            $<TARGET_OBJECTS:RTInterception.${arch}>
-            $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
-            $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
-    CFLAGS ${LSAN_CFLAGS})
-  list(APPEND LSAN_RUNTIME_LIBRARIES clang_rt.lsan-${arch})
+if (NOT APPLE AND NOT ANDROID)
+  foreach(arch ${LSAN_COMMON_SUPPORTED_ARCH})
+    add_compiler_rt_object_library(RTLSanCommon ${arch}
+      SOURCES ${LSAN_COMMON_SOURCES}
+      CFLAGS ${LSAN_CFLAGS})
+  endforeach()
+
+  foreach(arch ${LSAN_SUPPORTED_ARCH})
+    add_compiler_rt_static_runtime(clang_rt.lsan-${arch} ${arch}
+      SOURCES ${LSAN_SOURCES}
+              $<TARGET_OBJECTS:RTInterception.${arch}>
+              $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
+              $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
+              $<TARGET_OBJECTS:RTLSanCommon.${arch}>
+      CFLAGS ${LSAN_CFLAGS})
+    list(APPEND LSAN_RUNTIME_LIBRARIES clang_rt.lsan-${arch})
+  endforeach()
 endif()
 
 add_subdirectory(tests)
diff --git a/lib/lsan/lsan_common.cc b/lib/lsan/lsan_common.cc
index d9e7d81..18d913d 100644
--- a/lib/lsan/lsan_common.cc
+++ b/lib/lsan/lsan_common.cc
@@ -21,7 +21,7 @@
 #include "sanitizer_common/sanitizer_stoptheworld.h"
 
 namespace __lsan {
-
+#if CAN_SANITIZE_LEAKS
 Flags lsan_flags;
 
 static void InitializeFlags() {
@@ -381,5 +381,8 @@
            remaining > 1 ? "s" : "");
   }
 }
-
+#else  // CAN_SANITIZE_LEAKS
+void InitCommonLsan() {}
+void DoLeakCheck() {}
+#endif  // CAN_SANITIZE_LEAKS
 }  // namespace __lsan
diff --git a/lib/lsan/lsan_common.h b/lib/lsan/lsan_common.h
index 00bafe5..0a8ff82 100644
--- a/lib/lsan/lsan_common.h
+++ b/lib/lsan/lsan_common.h
@@ -17,8 +17,15 @@
 
 #include "sanitizer_common/sanitizer_common.h"
 #include "sanitizer_common/sanitizer_internal_defs.h"
+#include "sanitizer_common/sanitizer_platform.h"
 #include "sanitizer_common/sanitizer_symbolizer.h"
 
+#if SANITIZER_LINUX && defined(__x86_64__)
+#define CAN_SANITIZE_LEAKS 1
+#else
+#define CAN_SANITIZE_LEAKS 0
+#endif
+
 namespace __lsan {
 
 // Chunk tags.
diff --git a/lib/lsan/lsan_common_linux.cc b/lib/lsan/lsan_common_linux.cc
index b09e2f9..cafca17 100644
--- a/lib/lsan/lsan_common_linux.cc
+++ b/lib/lsan/lsan_common_linux.cc
@@ -13,9 +13,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "sanitizer_common/sanitizer_platform.h"
-#if SANITIZER_LINUX
 #include "lsan_common.h"
 
+#if CAN_SANITIZE_LEAKS && SANITIZER_LINUX
 #include <link.h>
 
 #include "sanitizer_common/sanitizer_common.h"
@@ -120,4 +120,4 @@
 }
 
 }  // namespace __lsan
-#endif  // SANITIZER_LINUX
+#endif  // CAN_SANITIZE_LEAKS && SANITIZER_LINUX