Add a way to link against different driver names.

The existing linker path hard-coded "-lRSDriver" into the command line, but
this won't work for partner drivers that have a different implementation
loaded. In order to still properly handle use of the CPU driver, this needs
to change depending on whether we actually loaded an OVERRIDE_RS_DRIVER or
not.

bug 20894664

Change-Id: I0c4a4f12f5db819b234952bc8f364ac6300f147b
diff --git a/cpu_ref/rsCpuExecutable.cpp b/cpu_ref/rsCpuExecutable.cpp
index 1553da2..e783108 100644
--- a/cpu_ref/rsCpuExecutable.cpp
+++ b/cpu_ref/rsCpuExecutable.cpp
@@ -110,12 +110,20 @@
 
 #ifndef RS_COMPATIBILITY_LIB
 
-bool SharedLibraryUtils::createSharedLibrary(const char *cacheDir, const char *resName) {
+bool SharedLibraryUtils::createSharedLibrary(const char *driverName,
+                                             const char *cacheDir,
+                                             const char *resName) {
     std::string sharedLibName = findSharedObjectName(cacheDir, resName);
     std::string objFileName = cacheDir;
     objFileName.append("/");
     objFileName.append(resName);
     objFileName.append(".o");
+    // Should be something like "libRSDriver.so".
+    std::string linkDriverName = driverName;
+    // Remove ".so" and replace "lib" with "-l".
+    // This will leave us with "-lRSDriver" instead.
+    linkDriverName.erase(linkDriverName.length() - 3);
+    linkDriverName.replace(0, 3, "-l");
 
     const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
     const char *mTriple = "-mtriple=" DEFAULT_TARGET_TRIPLE_STRING;
@@ -126,7 +134,7 @@
         "-shared",
         "-nostdlib",
         compiler_rt, mTriple, libPath,
-        "-lRSDriver", "-lm", "-lc",
+        linkDriverName.c_str(), "-lm", "-lc",
         objFileName.c_str(),
         "-o", sharedLibName.c_str(),
         nullptr
diff --git a/cpu_ref/rsCpuExecutable.h b/cpu_ref/rsCpuExecutable.h
index 785f53d..fd79ca1 100644
--- a/cpu_ref/rsCpuExecutable.h
+++ b/cpu_ref/rsCpuExecutable.h
@@ -29,7 +29,9 @@
 class SharedLibraryUtils {
 public:
 #ifndef RS_COMPATIBILITY_LIB
-    static bool createSharedLibrary(const char* cacheDir, const char* resName);
+    static bool createSharedLibrary(const char* driverName,
+                                    const char* cacheDir,
+                                    const char* resName);
 #endif
 
     // Load the shared library referred to by cacheDir and resName. If we have
diff --git a/cpu_ref/rsCpuScript.cpp b/cpu_ref/rsCpuScript.cpp
index 3f64534..3282374 100644
--- a/cpu_ref/rsCpuScript.cpp
+++ b/cpu_ref/rsCpuScript.cpp
@@ -385,7 +385,8 @@
             return false;
         }
 
-        if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
+        if (!SharedLibraryUtils::createSharedLibrary(mCtx->getContext()->getDriverName(),
+                                                     cacheDir, resName)) {
             ALOGE("Linker: Failed to link object file '%s'", resName);
             mCtx->unlockMutex();
             return false;
diff --git a/cpu_ref/rsCpuScriptGroup2.cpp b/cpu_ref/rsCpuScriptGroup2.cpp
index 1cc382f..2329b75 100644
--- a/cpu_ref/rsCpuScriptGroup2.cpp
+++ b/cpu_ref/rsCpuScriptGroup2.cpp
@@ -397,7 +397,8 @@
     // Create and load the shared lib
     //===--------------------------------------------------------------------===//
 
-    if (!SharedLibraryUtils::createSharedLibrary(cacheDir, resName)) {
+    if (!SharedLibraryUtils::createSharedLibrary(
+            getCpuRefImpl()->getContext()->getDriverName(), cacheDir, resName)) {
         ALOGE("Failed to link object file '%s'", resName);
         unlink(objFilePath.c_str());
         return;
diff --git a/rsContext.cpp b/rsContext.cpp
index 0bf9d9d..29b4036 100644
--- a/rsContext.cpp
+++ b/rsContext.cpp
@@ -517,6 +517,7 @@
     }
 
     mHasSurface = false;
+    mDriverName = NULL;
 
     timerInit();
     timerSet(RS_TIMER_INTERNAL);
diff --git a/rsContext.h b/rsContext.h
index 5965cae..6cb0ed7 100644
--- a/rsContext.h
+++ b/rsContext.h
@@ -283,6 +283,11 @@
         return mCacheDir;
     }
 
+    // Returns the actual loaded driver's name (like "libRSDriver.so").
+    const char * getDriverName() {
+        return mDriverName;
+    }
+
 
 protected:
 
@@ -336,6 +341,11 @@
     bool mHasSurface;
     bool mIsContextLite;
 
+    // This holds the name of the driver (like "libRSDriver.so").
+    // Since this is always just a static string, we don't have to
+    // allocate, copy, or free any memory here.
+    const char* mDriverName;
+
     Vector<ObjectBase *> mNames;
 
     uint64_t mTimers[_RS_TIMER_TOTAL];
diff --git a/rsDriverLoader.cpp b/rsDriverLoader.cpp
index 37bd12a..528af0f 100644
--- a/rsDriverLoader.cpp
+++ b/rsDriverLoader.cpp
@@ -204,6 +204,9 @@
         goto error;
     }
 
+    // Only map in the actual driver name if we successfully load the runtime.
+    mDriverName = filename;
+
     return true;