Version vendor libprotobuf-cpp-* libraries

Vendor prebuilts reference the protobuf runtime libraries, but the
runtime libraries do not have a stable ABI.  Add a version suffix
to the names of the libraries when used by vendor modules so that
old versions can be left in place when the protobuf version is
updated.

Also adds a test that verifies that the library names match the
version number to ensure the version number gets updated when
protobuf is updated.

Bug: 117607748
Test: libprotobuf_vendor_suffix_test
Change-Id: Iee5bbe95cb898f8ab552028f32e4b40d67f54f23
diff --git a/Android.bp b/Android.bp
index f0f1387..e767f01 100644
--- a/Android.bp
+++ b/Android.bp
@@ -79,6 +79,10 @@
         android: {
             shared_libs: ["liblog"],
         },
+        vendor: {
+            // This suffix must be updated when a new version is imported.
+            suffix: "-3.9.1",
+        },
     },
 }
 
@@ -375,6 +379,20 @@
     rtti: true,
 }
 
+cc_test {
+    name: "libprotobuf_vendor_suffix_test",
+    vendor: true,
+    srcs: ["vendor_suffix_test.cpp"],
+    shared_libs: [
+        "libprotobuf-cpp-lite",
+        "libprotobuf-cpp-full",
+    ],
+    static_libs: ["libbase"],
+    stl: "libc++",
+    test_suites: ["general-tests"],
+    test_config: "vendor_suffix_test.config",
+}
+
 java_defaults {
     name: "libprotobuf_errorprone_defaults",
     errorprone: {
diff --git a/METADATA b/METADATA
index 4617d91..64306e4 100644
--- a/METADATA
+++ b/METADATA
@@ -11,6 +11,7 @@
     type: GIT
     value: "https://github.com/protocolbuffers/protobuf"
   }
+  // Also update the suffix in Android.bp when updating the version.
   version: "v3.9.1"
   last_upgrade_date { year: 2019 month: 8 day: 23 }
   license_type: PERMISSIVE
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..21a5334
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+  "presubmit": [
+    {
+      "name": "libprotobuf_vendor_suffix_test"
+    }
+  ]
+}
diff --git a/vendor_suffix_test.config b/vendor_suffix_test.config
new file mode 100644
index 0000000..eb8bab7
--- /dev/null
+++ b/vendor_suffix_test.config
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs libprotobuf_vendor_suffix_test.">
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-suite-tag" value="apct-native" />
+
+    <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
+        <option name="cleanup" value="true" />
+        <!-- This file could be autogenerated if the default config handled tests that linked against vendor libraries -->
+        <option name="push" value="libprotobuf_vendor_suffix_test->/data/nativetest/vendor/libprotobuf_vendor_suffix_test/libprotobuf_vendor_suffix_test" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.GTest" >
+        <option name="native-test-device-path" value="/data/nativetest/vendor/libprotobuf_vendor_suffix_test" />
+        <option name="module-name" value="libprotobuf_vendor_suffix_test" />
+    </test>
+</configuration>
diff --git a/vendor_suffix_test.cpp b/vendor_suffix_test.cpp
new file mode 100644
index 0000000..bd06422
--- /dev/null
+++ b/vendor_suffix_test.cpp
@@ -0,0 +1,42 @@
+#include <link.h>
+
+#include <string>
+#include <vector>
+
+#include <android-base/stringprintf.h>
+
+#include <gtest/gtest.h>
+
+#include <google/protobuf/message_lite.h>
+
+TEST(vendor_suffix, suffix) {
+    std::vector<std::string> libs;
+    dl_iterate_phdr([](dl_phdr_info* info, size_t, void* data) -> int {
+        auto local_libs = static_cast<decltype(&libs)>(data);
+        std::string name = info->dlpi_name;
+        size_t libprotobuf = name.find("libprotobuf-cpp");
+        if (libprotobuf != name.npos) {
+            local_libs->push_back(name.substr(libprotobuf, name.size()));
+        }
+        return 0;
+    }, &libs);
+
+    std::sort(libs.begin(), libs.end());
+
+    std::string version = android::base::StringPrintf("-%d.%d.%d",
+       GOOGLE_PROTOBUF_VERSION / 1000000,
+       GOOGLE_PROTOBUF_VERSION / 1000 % 1000,
+       GOOGLE_PROTOBUF_VERSION % 1000);
+
+    std::string suffix = GOOGLE_PROTOBUF_VERSION_SUFFIX;
+    if (suffix != "") {
+        version += "-" + suffix;
+    }
+
+    std::vector<std::string> expect = {
+        "libprotobuf-cpp-full" + version + ".so",
+        "libprotobuf-cpp-lite" + version + ".so",
+    };
+
+    ASSERT_EQ(expect, libs);
+}