Build debug version version of runtime (libclcore_debug.bc).

Bug: 7343201
Change-Id: I9ca9acd7b0e23762d28de3af0aa60d4a6d75e5dd
diff --git a/lib/Renderscript/RSInfo.cpp b/lib/Renderscript/RSInfo.cpp
index e3854e1..4aa2e69 100644
--- a/lib/Renderscript/RSInfo.cpp
+++ b/lib/Renderscript/RSInfo.cpp
@@ -34,6 +34,7 @@
 const char RSInfo::LibCompilerRTPath[] = "/system/lib/libcompiler_rt.so";
 const char RSInfo::LibRSPath[] = "/system/lib/libRS.so";
 const char RSInfo::LibCLCorePath[] = "/system/lib/libclcore.bc";
+const char RSInfo::LibCLCoreDebugPath[] = "/system/lib/libclcore_debug.bc";
 #if defined(ARCH_X86_HAVE_SSE2)
 const char RSInfo::LibCLCoreX86Path[] = "/system/lib/libclcore_x86.bc";
 #endif
@@ -45,6 +46,7 @@
 const uint8_t *RSInfo::LibCompilerRTSHA1 = NULL;
 const uint8_t *RSInfo::LibRSSHA1 = NULL;
 const uint8_t *RSInfo::LibCLCoreSHA1 = NULL;
+const uint8_t *RSInfo::LibCLCoreDebugSHA1 = NULL;
 #if defined(ARCH_ARM_HAVE_NEON)
 const uint8_t *RSInfo::LibCLCoreNEONSHA1 = NULL;
 #endif
@@ -69,6 +71,8 @@
   LibRSSHA1 = reinterpret_cast<const uint8_t *>(::dlsym(h, "libRS_so_SHA1"));
   LibCLCoreSHA1 =
       reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_bc_SHA1"));
+  LibCLCoreDebugSHA1 =
+      reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_debug_bc_SHA1"));
 #if defined(ARCH_ARM_HAVE_NEON)
   LibCLCoreNEONSHA1 =
       reinterpret_cast<const uint8_t *>(::dlsym(h, "libclcore_neon_bc_SHA1"));
@@ -101,9 +105,9 @@
   // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc plus
   // libclcore_neon.bc if NEON is available on the target device.
 #if !defined(ARCH_ARM_HAVE_NEON)
-  static const unsigned NumBuiltInDependencies = 4;
-#else
   static const unsigned NumBuiltInDependencies = 5;
+#else
+  static const unsigned NumBuiltInDependencies = 6;
 #endif
 
   LoadBuiltInSHA1Information();
@@ -123,9 +127,11 @@
         pInfo.mDependencyTable[2];
     const std::pair<const char *, const uint8_t *> &cache_libclcore_dep =
         pInfo.mDependencyTable[3];
+    const std::pair<const char *, const uint8_t *> &cache_libclcore_debug_dep =
+        pInfo.mDependencyTable[4];
 #if defined(ARCH_ARM_HAVE_NEON)
     const std::pair<const char *, const uint8_t *> &cache_libclcore_neon_dep =
-        pInfo.mDependencyTable[4];
+        pInfo.mDependencyTable[5];
 #endif
 
     // Check libbcc.so.
@@ -163,19 +169,30 @@
     if (::memcmp(cache_libclcore_dep.second, LibCLCoreSHA1,
                  SHA1_DIGEST_LENGTH) != 0) {
         ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
-              LibRSPath);
+              LibCLCorePath);
         PRINT_DEPENDENCY("current - ", LibCLCorePath, LibCLCoreSHA1);
         PRINT_DEPENDENCY("cache - ", cache_libclcore_dep.first,
                                      cache_libclcore_dep.second);
         return false;
     }
 
+    // Check libclcore_debug.bc.
+    if (::memcmp(cache_libclcore_debug_dep.second, LibCLCoreDebugSHA1,
+                 SHA1_DIGEST_LENGTH) != 0) {
+        ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
+              LibCLCoreDebugPath);
+        PRINT_DEPENDENCY("current - ", LibCLCoreDebugPath, LibCLCoreDebugSHA1);
+        PRINT_DEPENDENCY("cache - ", cache_libclcore_debug_dep.first,
+                                     cache_libclcore_debug_dep.second);
+        return false;
+    }
+
 #if defined(ARCH_ARM_HAVE_NEON)
     // Check libclcore_neon.bc if NEON is available.
     if (::memcmp(cache_libclcore_neon_dep.second, LibCLCoreNEONSHA1,
                  SHA1_DIGEST_LENGTH) != 0) {
         ALOGD("Cache %s is dirty due to %s has been updated.", pInputFilename,
-              LibRSPath);
+              LibCLCoreNEONPath);
         PRINT_DEPENDENCY("current - ", LibCLCoreNEONPath, LibCLCoreNEONSHA1);
         PRINT_DEPENDENCY("cache - ", cache_libclcore_neon_dep.first,
                                      cache_libclcore_neon_dep.second);
diff --git a/lib/Renderscript/RSInfoExtractor.cpp b/lib/Renderscript/RSInfoExtractor.cpp
index 9963794..cd8f916 100644
--- a/lib/Renderscript/RSInfoExtractor.cpp
+++ b/lib/Renderscript/RSInfoExtractor.cpp
@@ -169,6 +169,7 @@
   string_pool_size += ::strlen(LibCompilerRTPath) + 1 + SHA1_DIGEST_LENGTH;
   string_pool_size += ::strlen(LibRSPath) + 1 + SHA1_DIGEST_LENGTH;
   string_pool_size += ::strlen(LibCLCorePath) + 1 + SHA1_DIGEST_LENGTH;
+  string_pool_size += ::strlen(LibCLCoreDebugPath) + 1 + SHA1_DIGEST_LENGTH;
 #if defined(ARCH_ARM_HAVE_NEON)
   string_pool_size += ::strlen(LibCLCoreNEONPath) + 1 + SHA1_DIGEST_LENGTH;
 #endif
@@ -389,6 +390,12 @@
       goto bail;
     }
 
+    if (!writeDependency(LibCLCoreDebugPath, LibCLCoreDebugSHA1,
+                         result->mStringPool, &cur_string_pool_offset,
+                         result->mDependencyTable)) {
+      goto bail;
+    }
+
 #if defined(ARCH_ARM_HAVE_NEON)
     if (!writeDependency(LibCLCoreNEONPath, LibCLCoreNEONSHA1,
                          result->mStringPool, &cur_string_pool_offset,
diff --git a/lib/Renderscript/runtime/Android.mk b/lib/Renderscript/runtime/Android.mk
index 80b310d..f5c912b 100755
--- a/lib/Renderscript/runtime/Android.mk
+++ b/lib/Renderscript/runtime/Android.mk
@@ -78,6 +78,16 @@
 
 include $(LOCAL_PATH)/build_bc_lib.mk
 
+# Build a debug version of the library
+include $(CLEAR_VARS)
+LOCAL_MODULE := libclcore_debug.bc
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_CLASS := SHARED_LIBRARIES
+rs_debug_runtime := 1
+LOCAL_SRC_FILES := $(clcore_files)
+
+include $(LOCAL_PATH)/build_bc_lib.mk
+
 # Build an optimized version of the library if the device is SSE2- or above
 # capable.
 ifeq ($(ARCH_X86_HAVE_SSE2),true)
diff --git a/lib/Renderscript/runtime/build_bc_lib.mk b/lib/Renderscript/runtime/build_bc_lib.mk
index f621969..4e2f1aa 100644
--- a/lib/Renderscript/runtime/build_bc_lib.mk
+++ b/lib/Renderscript/runtime/build_bc_lib.mk
@@ -33,7 +33,12 @@
              -emit-llvm \
              -target armv7-none-linux-gnueabi \
              -fsigned-char \
-	     $(bc_translated_clang_cc1_cflags)
+             $(bc_translated_clang_cc1_cflags)
+
+ifeq ($(rs_debug_runtime),1)
+bc_cflags += -DRS_DEBUG_RUNTIME
+endif
+rs_debug_runtime:=
 
 c_sources := $(filter %.c,$(LOCAL_SRC_FILES))
 ll_sources := $(filter %.ll,$(LOCAL_SRC_FILES))
@@ -47,10 +52,11 @@
 $(c_bc_files): PRIVATE_INCLUDES := \
     frameworks/rs/scriptc \
     external/clang/lib/Headers
+$(c_bc_files): PRIVATE_CFLAGS := $(bc_cflags)
 
 $(c_bc_files): $(intermediates)/%.bc: $(LOCAL_PATH)/%.c  $(CLANG)
 	@mkdir -p $(dir $@)
-	$(hide) $(CLANG) $(addprefix -I, $(PRIVATE_INCLUDES)) $(bc_cflags) $< -o $@
+	$(hide) $(CLANG) $(addprefix -I, $(PRIVATE_INCLUDES)) $(PRIVATE_CFLAGS) $< -o $@
 
 $(ll_bc_files): $(intermediates)/%.bc: $(LOCAL_PATH)/%.ll $(LLVM_AS)
 	@mkdir -p $(dir $@)
diff --git a/lib/Renderscript/runtime/rs_allocation.c b/lib/Renderscript/runtime/rs_allocation.c
index 903a350..5cc9de2 100644
--- a/lib/Renderscript/runtime/rs_allocation.c
+++ b/lib/Renderscript/runtime/rs_allocation.c
@@ -2,8 +2,6 @@
 #include "rs_graphics.rsh"
 #include "rs_structs.h"
 
-#define RS_DEBUG_RUNTIME 0
-
 // Opaque Allocation type operations
 extern uint32_t __attribute__((overloadable))
     rsAllocationGetDimX(rs_allocation a) {
@@ -56,7 +54,7 @@
     }
 }
 
-#if RS_DEBUG_RUNTIME
+#ifdef RS_DEBUG_RUNTIME
 #define ELEMENT_AT(T)                                                   \
     extern void __attribute__((overloadable))                           \
         rsSetElementAt_##T(rs_allocation a, const T *val, uint32_t x);  \