Revert "Remove all deprecated BCC C APIs."

This reverts commit 170d420231f10d5b914fde505202c58f11f43e27.
diff --git a/lib/RenderScript/RSCompilerDriver.cpp b/lib/RenderScript/RSCompilerDriver.cpp
index 0ccb625..da87968 100644
--- a/lib/RenderScript/RSCompilerDriver.cpp
+++ b/lib/RenderScript/RSCompilerDriver.cpp
@@ -16,25 +16,17 @@
 
 #include "bcc/RenderScript/RSCompilerDriver.h"
 
-#include <llvm/Support/Path.h>
-
-#include "bcinfo/BitcodeWrapper.h"
-
 #include "bcc/RenderScript/RSExecutable.h"
-#include "bcc/RenderScript/RSScript.h"
 #include "bcc/Support/CompilerConfig.h"
 #include "bcc/Support/TargetCompilerConfigs.h"
-#include "bcc/Source.h"
 #include "bcc/Support/FileMutex.h"
 #include "bcc/Support/Log.h"
 #include "bcc/Support/InputFile.h"
 #include "bcc/Support/Initialization.h"
-#include "bcc/Support/Sha1Util.h"
 #include "bcc/Support/OutputFile.h"
 
 #include <cutils/properties.h>
 #include <utils/String8.h>
-#include <utils/StopWatch.h>
 
 using namespace bcc;
 
@@ -64,10 +56,8 @@
   delete mConfig;
 }
 
-RSExecutable *
-RSCompilerDriver::loadScriptCache(const char *pOutputPath,
-                                  const RSInfo::DependencyTableTy &pDeps) {
-  android::StopWatch load_time("bcc: RSCompilerDriver::loadScriptCache time");
+RSExecutable *RSCompilerDriver::loadScriptCache(const RSScript &pScript,
+                                                const std::string &pOutputPath){
   RSExecutable *result = NULL;
 
   if (is_force_recompile())
@@ -79,7 +69,7 @@
   FileMutex<FileBase::kReadLock> read_output_mutex(pOutputPath);
 
   if (read_output_mutex.hasError() || !read_output_mutex.lock()) {
-    ALOGE("Unable to acquire the read lock for %s! (%s)", pOutputPath,
+    ALOGE("Unable to acquire the read lock for %s! (%s)", pOutputPath.c_str(),
           read_output_mutex.getErrorMessage().c_str());
     return NULL;
   }
@@ -90,7 +80,7 @@
   InputFile *output_file = new (std::nothrow) InputFile(pOutputPath);
 
   if ((output_file == NULL) || output_file->hasError()) {
-    ALOGE("Unable to open the %s for read! (%s)", pOutputPath,
+    ALOGE("Unable to open the %s for read! (%s)", pOutputPath.c_str(),
           output_file->getErrorMessage().c_str());
     delete output_file;
     return NULL;
@@ -103,7 +93,7 @@
 
   if (!output_file->lock()) {
     ALOGE("Unable to acquire the read lock on %s for reading %s! (%s)",
-          pOutputPath, info_path.string(),
+          pOutputPath.c_str(), info_path.string(),
           output_file->getErrorMessage().c_str());
     delete output_file;
     return NULL;
@@ -113,7 +103,8 @@
   // Open and load the RS info file.
   //===--------------------------------------------------------------------===//
   InputFile info_file(info_path.string());
-  RSInfo *info = RSInfo::ReadFromFile(info_file, pDeps);
+  RSInfo *info = RSInfo::ReadFromFile(info_file,
+                                      pScript.getSourceDependencies());
 
   // Release the lock on output_file.
   output_file->unlock();
@@ -133,6 +124,12 @@
     return NULL;
   }
 
+  // TODO: Dirty hack for libRS. This can be removed once RSExecutable is public
+  //       to libRS.
+  if (!result->isThreadable()) {
+    mRSRuntime.getAddress("__clearThreadable");
+  }
+
   return result;
 }
 
@@ -173,11 +170,8 @@
   return changed;
 }
 
-RSExecutable *
-RSCompilerDriver::compileScript(RSScript &pScript,
-                                const char *pOutputPath,
-                                const RSInfo::DependencyTableTy &pDeps) {
-  android::StopWatch compile_time("bcc: RSCompilerDriver::compileScript time");
+RSExecutable *RSCompilerDriver::compileScript(RSScript &pScript,
+                                              const std::string &pOutputPath) {
   RSExecutable *result = NULL;
   RSInfo *info = NULL;
 
@@ -186,7 +180,8 @@
   //===--------------------------------------------------------------------===//
   // RS info may contains configuration (such as #optimization_level) to the
   // compiler therefore it should be extracted before compilation.
-  info = RSInfo::ExtractFromSource(pScript.getSource(), pDeps);
+  info = RSInfo::ExtractFromSource(pScript.getSource(),
+                                   pScript.getSourceDependencies());
   if (info == NULL) {
     return NULL;
   }
@@ -205,7 +200,7 @@
 
   if (write_output_mutex.hasError() || !write_output_mutex.lock()) {
     ALOGE("Unable to acquire the lock for writing %s! (%s)",
-          pOutputPath, write_output_mutex.getErrorMessage().c_str());
+          pOutputPath.c_str(), write_output_mutex.getErrorMessage().c_str());
     return NULL;
   }
 
@@ -215,7 +210,7 @@
   OutputFile *output_file = new (std::nothrow) OutputFile(pOutputPath);
 
   if ((output_file == NULL) || output_file->hasError()) {
-    ALOGE("Unable to open the %s for write! (%s)", pOutputPath,
+    ALOGE("Unable to open the %s for write! (%s)", pOutputPath.c_str(),
           output_file->getErrorMessage().c_str());
     delete info;
     delete output_file;
@@ -228,7 +223,8 @@
   bool compiler_need_reconfigure = setupConfig(pScript);
 
   if (mConfig == NULL) {
-    ALOGE("Failed to setup config for RS compiler to compile %s!", pOutputPath);
+    ALOGE("Failed to setup config for RS compiler to compile %s!",
+          pOutputPath.c_str());
     delete info;
     delete output_file;
     return NULL;
@@ -239,7 +235,7 @@
   if (compiler_need_reconfigure) {
     Compiler::ErrorCode err = mCompiler.config(*mConfig);
     if (err != Compiler::kSuccess) {
-      ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath,
+      ALOGE("Failed to config the RS compiler for %s! (%s)",pOutputPath.c_str(),
             Compiler::GetErrorString(err));
       delete info;
       delete output_file;
@@ -252,7 +248,7 @@
   //===--------------------------------------------------------------------===//
   Compiler::ErrorCode compile_result = mCompiler.compile(pScript, *output_file);
   if (compile_result != Compiler::kSuccess) {
-    ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath,
+    ALOGE("Unable to compile the source to file %s! (%s)", pOutputPath.c_str(),
           Compiler::GetErrorString(compile_result));
     delete info;
     delete output_file;
@@ -269,6 +265,10 @@
     return NULL;
   }
 
+  // TODO: Dirty hack for libRS. This can be removed once RSExecutable is public
+  //       to libRS.
+  result->setThreadable(mRSRuntime.getAddress("__isThreadable") != NULL);
+
   //===--------------------------------------------------------------------===//
   // Write out the RS info file.
   //===--------------------------------------------------------------------===//
@@ -276,107 +276,20 @@
   // successfully compiled and loaded.
   if (!result->syncInfo(/* pForce */true)) {
     ALOGW("%s was successfully compiled and loaded but its RS info file failed "
-          "to write out!", pOutputPath);
+          "to write out!", pOutputPath.c_str());
   }
 
   return result;
 }
 
-RSExecutable *RSCompilerDriver::build(BCCContext &pContext,
-                                      const char *pCacheDir,
-                                      const char *pResName,
-                                      const char *pBitcode,
-                                      size_t pBitcodeSize) {
-  android::StopWatch build_time("bcc: RSCompilerDriver::build time");
-  //===--------------------------------------------------------------------===//
-  // Check parameters.
-  //===--------------------------------------------------------------------===//
-  if ((pCacheDir == NULL) || (pResName == NULL)) {
-    ALOGE("Invalid parameter passed to RSCompilerDriver::build()! (cache dir: "
-          "%s, resource name: %s)", ((pCacheDir) ? pCacheDir : "(null)"),
-                                    ((pResName) ? pResName : "(null)"));
-    return NULL;
-  }
-
-  if ((pBitcode == NULL) || (pBitcodeSize <= 0)) {
-    ALOGE("No bitcode supplied! (bitcode: %p, size of bitcode: %u)",
-          pBitcode, static_cast<unsigned>(pBitcodeSize));
-    return NULL;
-  }
-
-  //===--------------------------------------------------------------------===//
-  // Prepare dependency information.
-  //===--------------------------------------------------------------------===//
-  RSInfo::DependencyTableTy dep_info;
-  uint8_t bitcode_sha1[20];
-  Sha1Util::GetSHA1DigestFromBuffer(bitcode_sha1, pBitcode, pBitcodeSize);
-  dep_info.push(std::make_pair(pResName, bitcode_sha1));
-
-  //===--------------------------------------------------------------------===//
-  // Construct output path.
-  //===--------------------------------------------------------------------===//
-  llvm::sys::Path output_path(pCacheDir);
-
-  // {pCacheDir}/{pResName}
-  if (!output_path.appendComponent(pResName)) {
-    ALOGE("Failed to construct output path %s/%s!", pCacheDir, pResName);
-    return NULL;
-  }
-
-  // {pCacheDir}/{pResName}.o
-  output_path.appendSuffix("o");
-
-  //===--------------------------------------------------------------------===//
-  // Load cache.
-  //===--------------------------------------------------------------------===//
-  RSExecutable *result = loadScriptCache(output_path.c_str(), dep_info);
+RSExecutable *RSCompilerDriver::build(RSScript &pScript,
+                                      const std::string &pOutputPath) {
+  RSExecutable *result = loadScriptCache(pScript, pOutputPath);
 
   if (result != NULL) {
     // Cache hit
     return result;
   }
 
-  //===--------------------------------------------------------------------===//
-  // Load the bitcode and create script.
-  //===--------------------------------------------------------------------===//
-  Source *source = Source::CreateFromBuffer(pContext, pResName,
-                                            pBitcode, pBitcodeSize);
-  if (source == NULL) {
-    return NULL;
-  }
-
-  RSScript *script = new (std::nothrow) RSScript(*source);
-  if (script == NULL) {
-    ALOGE("Out of memory when create Script object for '%s'! (output: %s)",
-          pResName, output_path.c_str());
-    delete source;
-    return NULL;
-  }
-
-  // Link RS script with RenderScript runtime.
-  if (!RSScript::LinkRuntime(*script)) {
-    ALOGE("Failed to link script '%s' with RenderScript runtime!", pResName);
-    delete script;
-    return NULL;
-  }
-
-  // Read information from bitcode wrapper.
-  bcinfo::BitcodeWrapper wrapper(pBitcode, pBitcodeSize);
-  script->setCompilerVersion(wrapper.getCompilerVersion());
-  script->setOptimizationLevel(static_cast<RSScript::OptimizationLevel>(
-                                   wrapper.getOptimizationLevel()));
-
-  //===--------------------------------------------------------------------===//
-  // Compile the script
-  //===--------------------------------------------------------------------===//
-  result = compileScript(*script, output_path.c_str(), dep_info);
-
-  // Script is no longer used. Free it to get more memory.
-  delete script;
-
-  if (result == NULL) {
-    return NULL;
-  }
-
-  return result;
+  return compileScript(pScript, pOutputPath);
 }
diff --git a/lib/RenderScript/RSInfo.cpp b/lib/RenderScript/RSInfo.cpp
index 02f6f6a..6f1b309 100644
--- a/lib/RenderScript/RSInfo.cpp
+++ b/lib/RenderScript/RSInfo.cpp
@@ -73,7 +73,7 @@
 
 bool RSInfo::CheckDependency(const RSInfo &pInfo,
                              const char *pInputFilename,
-                             const DependencyTableTy &pDeps) {
+                             const RSScript::SourceDependencyListTy &pDeps) {
   // Built-in dependencies are libbcc.so, libRS.so and libclcore.bc.
   static const unsigned NumBuiltInDependencies = 3;
 
@@ -125,15 +125,19 @@
     }
 
     for (unsigned i = 0; i < pDeps.size(); i++) {
+      const RSScript::SourceDependency &in_dep = *(pDeps[i]);
       const std::pair<const char *, const uint8_t *> &cache_dep =
           pInfo.mDependencyTable[i + NumBuiltInDependencies];
 
-      if ((::strcmp(pDeps[i].first, cache_dep.first) != 0) ||
-          (::memcmp(pDeps[i].second, cache_dep.second,
+      if ((::strncmp(in_dep.getSourceName().c_str(),
+                     cache_dep.first,
+                     in_dep.getSourceName().length()) != 0) ||
+          (::memcmp(in_dep.getSHA1Checksum(), cache_dep.second,
                     SHA1_DIGEST_LENGTH) != 0)) {
         ALOGD("Cache %s is dirty due to the source it dependends on has been "
               "changed:", pInputFilename);
-        PRINT_DEPENDENCY("given - ", pDeps[i].first, pDeps[i].second);
+        PRINT_DEPENDENCY("given - ", in_dep.getSourceName().c_str(),
+                                     in_dep.getSHA1Checksum());
         PRINT_DEPENDENCY("cache - ", cache_dep.first, cache_dep.second);
         return false;
       }
diff --git a/lib/RenderScript/RSInfoExtractor.cpp b/lib/RenderScript/RSInfoExtractor.cpp
index dd8e580..ae1085a 100644
--- a/lib/RenderScript/RSInfoExtractor.cpp
+++ b/lib/RenderScript/RSInfoExtractor.cpp
@@ -126,7 +126,7 @@
 } // end anonymous namespace
 
 RSInfo *RSInfo::ExtractFromSource(const Source &pSource,
-                                  const DependencyTableTy &pDeps)
+                                  const RSScript::SourceDependencyListTy &pDeps)
 {
   const llvm::Module &module = pSource.getModule();
   const char *module_name = module.getModuleIdentifier().c_str();
@@ -169,10 +169,13 @@
   string_pool_size += ::strlen(LibRSPath) + 1 + SHA1_DIGEST_LENGTH;
   string_pool_size += ::strlen(LibCLCorePath) + 1 + SHA1_DIGEST_LENGTH;
   for (unsigned i = 0, e = pDeps.size(); i != e; i++) {
-    // +1 for null-terminator
-    string_pool_size += ::strlen(/* name */pDeps[i].first) + 1;
-    // +SHA1_DIGEST_LENGTH for SHA-1 checksum
-    string_pool_size += SHA1_DIGEST_LENGTH;
+    const RSScript::SourceDependency *source_dep = pDeps[i];
+    if (source_dep != NULL) {
+      // +1 for null-terminator
+      string_pool_size += source_dep->getSourceName().length() + 1;
+      // +SHA1_DIGEST_LENGTH for SHA-1 checksum
+      string_pool_size += SHA1_DIGEST_LENGTH;
+    }
   }
 
   // Allocate result object
@@ -379,10 +382,14 @@
   // Record dependency information.
   //===--------------------------------------------------------------------===//
   for (unsigned i = 0, e = pDeps.size(); i != e; i++) {
-    if (!writeDependency(/* name */pDeps[i].first, /* SHA-1 */pDeps[i].second,
-                         result->mStringPool, &cur_string_pool_offset,
-                         result->mDependencyTable)) {
-      goto bail;
+    const RSScript::SourceDependency *source_dep = pDeps[i];
+    if (source_dep != NULL) {
+      if (!writeDependency(source_dep->getSourceName(),
+                           source_dep->getSHA1Checksum(),
+                           result->mStringPool, &cur_string_pool_offset,
+                           result->mDependencyTable)) {
+        goto bail;
+      }
     }
   }
 
diff --git a/lib/RenderScript/RSInfoReader.cpp b/lib/RenderScript/RSInfoReader.cpp
index 9a25c24..035209a 100644
--- a/lib/RenderScript/RSInfoReader.cpp
+++ b/lib/RenderScript/RSInfoReader.cpp
@@ -173,7 +173,8 @@
 
 } // end anonymous namespace
 
-RSInfo *RSInfo::ReadFromFile(InputFile &pInput, const DependencyTableTy &pDeps) {
+RSInfo *RSInfo::ReadFromFile(InputFile &pInput,
+                             const RSScript::SourceDependencyListTy &pDeps) {
   android::FileMap *map = NULL;
   RSInfo *result = NULL;
   const uint8_t *data;
diff --git a/lib/RenderScript/RSScript.cpp b/lib/RenderScript/RSScript.cpp
index 9058ff9..0efc43b 100644
--- a/lib/RenderScript/RSScript.cpp
+++ b/lib/RenderScript/RSScript.cpp
@@ -16,12 +16,23 @@
 
 #include "bcc/RenderScript/RSScript.h"
 
+#include <cstring>
+
+#include <llvm/ADT/STLExtras.h>
+
 #include "bcc/RenderScript/RSInfo.h"
 #include "bcc/Source.h"
 #include "bcc/Support/Log.h"
 
 using namespace bcc;
 
+RSScript::SourceDependency::SourceDependency(const std::string &pSourceName,
+                                             const uint8_t *pSHA1)
+  : mSourceName(pSourceName) {
+  ::memcpy(mSHA1, pSHA1, sizeof(mSHA1));
+  return;
+}
+
 bool RSScript::LinkRuntime(RSScript &pScript) {
   // Using the same context with the source in pScript.
   BCCContext &context = pScript.getSource().getContext();
@@ -47,9 +58,28 @@
   : Script(pSource), mInfo(NULL), mCompilerVersion(0),
     mOptimizationLevel(kOptLvl3) { }
 
+RSScript::~RSScript() {
+  llvm::DeleteContainerPointers(mSourceDependencies);
+}
+
 bool RSScript::doReset() {
   mInfo = NULL;
   mCompilerVersion = 0;
   mOptimizationLevel = kOptLvl3;
+  llvm::DeleteContainerPointers(mSourceDependencies);
+  return true;
+}
+
+bool RSScript::addSourceDependency(const std::string &pSourceName,
+                                   const uint8_t *pSHA1) {
+  SourceDependency *source_dep =
+      new (std::nothrow) SourceDependency(pSourceName, pSHA1);
+  if (source_dep == NULL) {
+    ALOGE("Out of memory when record dependency information of `%s'!",
+          pSourceName.c_str());
+    return false;
+  }
+
+  mSourceDependencies.push_back(source_dep);
   return true;
 }