Revert r182465 and add lsan-common library to makefile-based build

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@182470 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Makefile.mk b/lib/Makefile.mk
index 8b912c4..8054c35 100644
--- a/lib/Makefile.mk
+++ b/lib/Makefile.mk
@@ -21,6 +21,7 @@
 SubDirs += tsan
 SubDirs += msan
 SubDirs += ubsan
+SubDirs += lsan
 
 # Define the variables for this specific directory.
 Sources := $(foreach file,$(wildcard $(Dir)/*.c),$(notdir $(file)))
diff --git a/lib/asan/CMakeLists.txt b/lib/asan/CMakeLists.txt
index 98544a4..a567a4d 100644
--- a/lib/asan/CMakeLists.txt
+++ b/lib/asan/CMakeLists.txt
@@ -85,7 +85,7 @@
               $<TARGET_OBJECTS:RTInterception.${arch}>
               $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
               $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
-              #$<TARGET_OBJECTS:RTLSanCommon.${arch}>
+              $<TARGET_OBJECTS:RTLSanCommon.${arch}>
       CFLAGS ${ASAN_CFLAGS}
       DEFS ${ASAN_COMMON_DEFINITIONS}
       SYMS asan.syms)
diff --git a/lib/asan/asan_allocator2.cc b/lib/asan/asan_allocator2.cc
index 79fda78..4c57aff 100644
--- a/lib/asan/asan_allocator2.cc
+++ b/lib/asan/asan_allocator2.cc
@@ -768,7 +768,7 @@
 template <typename Callable> void ForEachChunk(Callable const &callback) {
   __asan::allocator.ForEachChunk(callback);
 }
-#if 0
+#if CAN_SANITIZE_LEAKS
 template void ForEachChunk<ProcessPlatformSpecificAllocationsCb>(
     ProcessPlatformSpecificAllocationsCb const &callback);
 template void ForEachChunk<PrintLeakedCb>(PrintLeakedCb const &callback);
diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc
index 539ed9b..f989c5c 100644
--- a/lib/asan/asan_rtl.cc
+++ b/lib/asan/asan_rtl.cc
@@ -560,12 +560,12 @@
 
   InitializeAllocator();
 
-#if 0
+#if CAN_SANITIZE_LEAKS
   __lsan::InitCommonLsan();
   if (flags()->detect_leaks) {
     Atexit(__lsan::DoLeakCheck);
   }
-#endif
+#endif  // CAN_SANITIZE_LEAKS
 
   if (flags()->verbosity) {
     Report("AddressSanitizer Init done\n");
diff --git a/lib/lsan/Makefile.mk b/lib/lsan/Makefile.mk
new file mode 100644
index 0000000..aae5c32
--- /dev/null
+++ b/lib/lsan/Makefile.mk
@@ -0,0 +1,23 @@
+#===- lib/lsan/Makefile.mk ---------------------------------*- Makefile -*--===#
+#
+#                     The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+#===------------------------------------------------------------------------===#
+
+ModuleName := lsan_common
+SubDirs := 
+
+Sources := $(foreach file,$(wildcard $(Dir)/lsan_common*.cc),$(notdir $(file)))
+ObjNames := $(Sources:%.cc=%.o)
+
+Implementation := Generic
+
+# FIXME: use automatic dependencies?
+Dependencies := $(wildcard $(Dir)/*.h)
+Dependencies += $(wildcard $(Dir)/../sanitizer_common/*.h)
+
+# Define a convenience variable for all the asan functions.
+LsanCommonFunctions := $(Sources:%.cc=%)
diff --git a/make/platform/clang_linux.mk b/make/platform/clang_linux.mk
index 985e485..05efdb6 100644
--- a/make/platform/clang_linux.mk
+++ b/make/platform/clang_linux.mk
@@ -130,7 +130,7 @@
 FUNCTIONS.asan-i386 := $(AsanFunctions) $(InterceptionFunctions) \
                                         $(SanitizerCommonFunctions)
 FUNCTIONS.asan-x86_64 := $(AsanFunctions) $(InterceptionFunctions) \
-                                          $(SanitizerCommonFunctions)
+                         $(SanitizerCommonFunctions) $(LsanCommonFunctions)
 FUNCTIONS.asan-arm-android := $(AsanFunctions) $(InterceptionFunctions) \
                                           $(SanitizerCommonFunctions)
 FUNCTIONS.tsan-x86_64 := $(TsanFunctions) $(InterceptionFunctions) \