Merge "fmtlib: build and run the tests." am: 52b05fe0a0

Original change: https://android-review.googlesource.com/c/platform/external/fmtlib/+/1486343

Change-Id: I1aab745880625f2a176e7070b187ce4d4d1ad00e
diff --git a/Android.bp b/Android.bp
index d7a6dec..42cf8c9 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,25 +1,26 @@
 cc_defaults {
-    name: "fmtlib-defaults",
-    srcs: ["src/format.cc"],
+    name: "fmtlib-non-test-defaults",
     cflags: [
         "-fno-exceptions",
-        "-Wall",
-        "-Werror",
         // If built without exceptions, libfmt uses assert.
+        // The tests *require* exceptions, so we can't win here.
+        // (This is also why we have two cc_defaults in this file.)
+        // Unless proven to be a bad idea, let's at least have some run-time
+        // checking.
         "-UNDEBUG",
     ],
-    sanitize: {
-        misc_undefined: ["integer"],
-    },
+    srcs: ["src/format.cc"],
     local_include_dirs: ["include"],
     export_include_dirs: ["include"],
+    visibility: ["//system/libbase"],
     min_sdk_version: "29",
 }
 
 // This is built into libbase.  If you want to use this library, link to libbase instead.
 cc_library_static {
     name: "fmtlib",
-    defaults: ["fmtlib-defaults"],
+    defaults: ["fmtlib-non-test-defaults"],
+
     vendor_available: true,
     ramdisk_available: true,
     vendor_ramdisk_available: true,
@@ -42,7 +43,60 @@
 
 cc_library_static {
     name: "fmtlib_ndk",
-    defaults: ["fmtlib-defaults"],
+    defaults: ["fmtlib-non-test-defaults"],
     sdk_version: "current",
     stl: "c++_static",
 }
+
+cc_defaults {
+    name: "fmtlib-test-defaults",
+    srcs: [
+        "src/format.cc",
+        "src/os.cc",
+        "test/gtest-extra.cc",
+        "test/util.cc",
+    ],
+    local_include_dirs: ["include"],
+    host_supported: true,
+    test_suites: ["general-tests"],
+    // The tests require exceptions and RTTI.
+    cflags: ["-fexceptions"],
+    rtti: true,
+    // The usual "gtest *and* gmock, please" dance...
+    gtest: false,
+    include_dirs: [
+        "external/googletest/googlemock/include/gmock",
+        "external/googletest/googletest/include/gtest",
+    ],
+    static_libs: [
+        "libgmock",
+        "libgtest",
+        "libgtest_main",
+    ],
+}
+
+// Most of the fmtlib tests.
+cc_test {
+    name: "fmtlib_test",
+    defaults: ["fmtlib-test-defaults"],
+    srcs: [
+        "test/chrono-test.cc",
+        "test/color-test.cc",
+        "test/core-test.cc",
+        "test/format-test.cc",
+        // Some of the os-test tests deliberately try to do bad things with
+        // file descriptors, but Android's fdsan won't let them.
+        // "test/os-test.cc",
+        "test/printf-test.cc",
+        "test/ranges-test.cc",
+        "test/scan-test.cc",
+    ],
+}
+
+// This one needs to be separate because some of the test names overlap with
+// other tests.
+cc_test {
+    name: "fmtlib_ostream_test",
+    defaults: ["fmtlib-test-defaults"],
+    srcs: ["test/ostream-test.cc"],
+}
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..caeecd6
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,10 @@
+{
+  "presubmit": [
+    {
+      "name": "fmtlib_test"
+    },
+    {
+      "name": "fmtlib_ostream_test"
+    }
+  ]
+}