Convert libtextclassifier to Android.bp

See build/soong/README.md for more information.

This no longer supports LIBTEXTCLASSIFIER_STRIP_OPTS as a command line
argument, that would require some custom go code to define your own
module type. Instead, I've just left the setting that can be uncommented
/ changed as desired.

Test: cd external/libtextclassifier; mma
Test: atest frameworks/base/core/tests/coretests/src/android/view/textclassifier/TextClassifierTest.java
Change-Id: I65959ec248b04dbaa08edf38dab2edf7d124c1ed
Merged-In: I65959ec248b04dbaa08edf38dab2edf7d124c1ed
diff --git a/Android.bp b/Android.bp
index 1255a76..3c04a18 100644
--- a/Android.bp
+++ b/Android.bp
@@ -22,7 +22,7 @@
     name: "libtextclassifier_hash_defaults",
     srcs: [
         "utils/hash/farmhash.cc",
-        "util/hash/hash.cc"
+        "util/hash/hash.cc",
     ],
     cflags: [
         "-DNAMESPACE_FOR_HASH_FUNCTIONS=farmhash",
@@ -52,3 +52,163 @@
     no_framework_libs: true,
     srcs: ["java/**/*.java"],
 }
+
+cc_defaults {
+    name: "libtextclassifier_defaults",
+
+    // For debug / treemap purposes.
+    //strip: {
+    //    keep_symbols: true,
+    //},
+
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-deprecated-declarations",
+        "-Wno-ignored-qualifiers",
+        "-Wno-missing-field-initializers",
+        "-Wno-sign-compare",
+        "-Wno-tautological-constant-out-of-range-compare",
+        "-Wno-undefined-var-template",
+        "-Wno-unused-function",
+        "-Wno-unused-parameter",
+        "-Wno-extern-c-compat",
+
+        "-fvisibility=hidden",
+        "-DLIBTEXTCLASSIFIER_UNILIB_ICU",
+        "-DZLIB_CONST",
+        "-DSAFTM_COMPACT_LOGGING",
+        "-DTC3_UNILIB_JAVAICU",
+        "-DTC3_CALENDAR_JAVAICU",
+    ],
+
+    product_variables: {
+        debuggable: {
+            // Only enable debug logging in userdebug/eng builds.
+            cflags: ["-DTC_DEBUG_LOGGING=1"],
+        },
+    },
+
+    generated_headers: [
+        "libtextclassifier_fbgen_zlib_buffer",
+        "libtextclassifier_fbgen_intent_config",
+        "libtextclassifier_fbgen_annotator_model",
+    ],
+
+    header_libs: [
+        "tensorflow_headers",
+        "flatbuffer_headers",
+    ],
+
+    shared_libs: [
+        "liblog",
+        "libtflite",
+        "libz",
+    ],
+
+    static_libs: [
+        "libutf",
+    ],
+}
+
+// -----------------
+// Generate headers with FlatBuffer schema compiler.
+// -----------------
+
+genrule {
+    name: "libtextclassifier_fbgen_zlib_buffer",
+    tools: ["flatc"],
+    cmd: "$(location flatc) --cpp --no-union-value-namespacing --gen-object-api --keep-prefix -o $(genDir)/utils/zlib $(in)",
+    srcs: ["utils/zlib/buffer.fbs"],
+    out: ["utils/zlib/buffer_generated.h"],
+}
+
+genrule {
+    name: "libtextclassifier_fbgen_intent_config",
+    tools: ["flatc"],
+    cmd: "$(location flatc) --cpp --no-union-value-namespacing --gen-object-api --keep-prefix -o $(genDir)/utils/intents $(in)",
+    srcs: ["utils/intents/intent-config.fbs"],
+    out: ["utils/intents/intent-config_generated.h"],
+}
+
+genrule {
+    name: "libtextclassifier_fbgen_annotator_model",
+    tools: ["flatc"],
+    // "depfile" is used here in conjunction with flatc's -M to gather the deps of annotator/model.fbs
+    cmd: "$(location flatc) --cpp --no-union-value-namespacing --gen-object-api --keep-prefix -I external/libtextclassifier -M $(in) >$(depfile) && " +
+        "$(location flatc) --cpp --no-union-value-namespacing --gen-object-api --keep-prefix -I external/libtextclassifier -o $(genDir)/annotator $(in)",
+    depfile: true,
+    srcs: ["annotator/model.fbs"],
+    out: ["annotator/model_generated.h"],
+}
+
+// -----------------
+// libtextclassifier
+// -----------------
+cc_library_shared {
+    name: "libtextclassifier",
+    defaults: ["libtextclassifier_defaults"],
+
+    srcs: ["**/*.cc"],
+    exclude_srcs: [
+        "**/*_test.cc",
+        "**/*-test-lib.cc",
+        "test-util.*",
+    ],
+
+    required: [
+        "libtextclassifier_annotator_en_model",
+        "libtextclassifier_annotator_universal_model",
+    ],
+
+    version_script: "jni.lds",
+}
+
+// -----------------------
+// libtextclassifier_tests
+// -----------------------
+cc_test {
+    name: "libtextclassifier_tests",
+    defaults: ["libtextclassifier_defaults"],
+
+    test_suites: ["device-tests"],
+
+    data: [
+        "annotator/test_data/**/*",
+    ],
+
+    srcs: ["**/*.cc"],
+    // TODO: Do not filter out tflite test once the dependency issue is resolved.
+    exclude_srcs: ["utils/tflite/*_test.cc"],
+
+    static_libs: ["libgmock"],
+
+    multilib: {
+        lib32: {
+            cppflags: ["-DTC3_TEST_DATA_DIR=\"/data/nativetest/libtextclassifier_tests/test_data/\""],
+        },
+        lib64: {
+            cppflags: ["-DTC3_TEST_DATA_DIR=\"/data/nativetest64/libtextclassifier_tests/test_data/\""],
+        },
+    },
+}
+
+// ----------------
+// Annotator models
+// ----------------
+
+prebuilt_etc {
+    name: "libtextclassifier_annotator_en_model",
+    filename: "textclassifier.en.model",
+    owner: "google",
+    src: "models/textclassifier.en.model",
+    sub_dir: "textclassifier",
+}
+
+prebuilt_etc {
+    name: "libtextclassifier_annotator_universal_model",
+    filename: "textclassifier.universal.model",
+    owner: "google",
+    src: "models/textclassifier.universal.model",
+    sub_dir: "textclassifier",
+}
diff --git a/Android.mk b/Android.mk
deleted file mode 100644
index 5017185..0000000
--- a/Android.mk
+++ /dev/null
@@ -1,151 +0,0 @@
-# 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.
-
-# Useful environment variables that can be set on the mmma command line, as
-# <key>=<value> pairs:
-#
-# LIBTEXTCLASSIFIER_STRIP_OPTS: (optional) value for LOCAL_STRIP_MODULE (for all
-#   modules we build).  NOT for prod builds.  Can be set to keep_symbols for
-#   debug / treemap purposes.
-
-
-LOCAL_PATH := $(call my-dir)
-
-# Custom C/C++ compilation flags:
-MY_LIBTEXTCLASSIFIER_WARNING_CFLAGS := \
-    -Wall \
-    -Werror \
-    -Wno-deprecated-declarations \
-    -Wno-ignored-qualifiers \
-    -Wno-missing-field-initializers \
-    -Wno-sign-compare \
-    -Wno-tautological-constant-out-of-range-compare \
-    -Wno-undefined-var-template \
-    -Wno-unused-function \
-    -Wno-unused-parameter \
-    -Wno-extern-c-compat
-
-MY_LIBTEXTCLASSIFIER_CFLAGS := \
-    $(MY_LIBTEXTCLASSIFIER_WARNING_CFLAGS) \
-    -fvisibility=hidden \
-    -DLIBTEXTCLASSIFIER_UNILIB_ICU \
-    -DZLIB_CONST \
-    -DSAFTM_COMPACT_LOGGING \
-    -DTC3_UNILIB_JAVAICU \
-    -DTC3_CALENDAR_JAVAICU
-
-# Only enable debug logging in userdebug/eng builds.
-ifneq (,$(filter userdebug eng, $(TARGET_BUILD_VARIANT)))
-  MY_LIBTEXTCLASSIFIER_CFLAGS += -DTC_DEBUG_LOGGING=1
-endif
-
-# -----------------
-# libtextclassifier
-# -----------------
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := libtextclassifier
-LOCAL_MODULE_CLASS := SHARED_LIBRARIES
-LOCAL_CPP_EXTENSION := .cc
-
-include $(LOCAL_PATH)/generate_flatbuffers.mk
-
-LOCAL_CFLAGS += $(MY_LIBTEXTCLASSIFIER_CFLAGS)
-LOCAL_STRIP_MODULE := $(LIBTEXTCLASSIFIER_STRIP_OPTS)
-
-LOCAL_SRC_FILES := $(filter-out tests/% %_test.cc test-util.%,$(call all-subdir-cpp-files))
-
-LOCAL_C_INCLUDES := $(TOP)/external/zlib
-LOCAL_C_INCLUDES += $(TOP)/external/tensorflow
-LOCAL_C_INCLUDES += $(TOP)/external/flatbuffers/include
-LOCAL_C_INCLUDES += $(TOP)/external/libutf
-LOCAL_C_INCLUDES += $(intermediates)
-
-LOCAL_SHARED_LIBRARIES += liblog
-LOCAL_SHARED_LIBRARIES += libtflite
-LOCAL_SHARED_LIBRARIES += libz
-
-LOCAL_STATIC_LIBRARIES += libutf
-
-LOCAL_REQUIRED_MODULES := libtextclassifier_annotator_en_model
-LOCAL_REQUIRED_MODULES += libtextclassifier_annotator_universal_model
-
-LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/jni.lds
-LOCAL_LDFLAGS += -Wl,-version-script=$(LOCAL_PATH)/jni.lds
-LOCAL_CPPFLAGS_32 += -DTC3_TEST_DATA_DIR="\"/data/nativetest/libtextclassifier_tests/test_data/\""
-LOCAL_CPPFLAGS_64 += -DTC3_TEST_DATA_DIR="\"/data/nativetest64/libtextclassifier_tests/test_data/\""
-
-include $(BUILD_SHARED_LIBRARY)
-
-# -----------------------
-# libtextclassifier_tests
-# -----------------------
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libtextclassifier_tests
-LOCAL_MODULE_CLASS := NATIVE_TESTS
-LOCAL_COMPATIBILITY_SUITE := device-tests
-LOCAL_MODULE_TAGS := tests
-LOCAL_CPP_EXTENSION := .cc
-
-include $(LOCAL_PATH)/generate_flatbuffers.mk
-
-LOCAL_CFLAGS += $(MY_LIBTEXTCLASSIFIER_CFLAGS)
-LOCAL_STRIP_MODULE := $(LIBTEXTCLASSIFIER_STRIP_OPTS)
-
-LOCAL_TEST_DATA := $(call find-test-data-in-subdirs, $(LOCAL_PATH), *, annotator/test_data)
-
-LOCAL_CPPFLAGS_32 += -DTC3_TEST_DATA_DIR="\"/data/nativetest/libtextclassifier_tests/test_data/\""
-LOCAL_CPPFLAGS_64 += -DTC3_TEST_DATA_DIR="\"/data/nativetest64/libtextclassifier_tests/test_data/\""
-
-# TODO: Do not filter out tflite test once the dependency issue is resolved.
-LOCAL_SRC_FILES := $(filter-out utils/tflite/%_test.cc,$(call all-subdir-cpp-files))
-
-LOCAL_C_INCLUDES := $(TOP)/external/zlib
-LOCAL_C_INCLUDES += $(TOP)/external/tensorflow
-LOCAL_C_INCLUDES += $(TOP)/external/flatbuffers/include
-LOCAL_C_INCLUDES += $(TOP)/external/libutf
-LOCAL_C_INCLUDES += $(intermediates)
-
-LOCAL_SHARED_LIBRARIES += liblog
-LOCAL_SHARED_LIBRARIES += libtflite
-LOCAL_SHARED_LIBRARIES += libz
-
-LOCAL_STATIC_LIBRARIES += libgmock
-LOCAL_STATIC_LIBRARIES += libutf
-
-include $(BUILD_NATIVE_TEST)
-
-# ----------------
-# Annotator models
-# ----------------
-
-include $(CLEAR_VARS)
-LOCAL_MODULE        := libtextclassifier_annotator_en_model
-LOCAL_MODULE_STEM   := textclassifier.en.model
-LOCAL_MODULE_CLASS  := ETC
-LOCAL_MODULE_OWNER  := google
-LOCAL_SRC_FILES     := ./models/textclassifier.en.model
-LOCAL_MODULE_PATH   := $(TARGET_OUT_ETC)/textclassifier
-include $(BUILD_PREBUILT)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE        := libtextclassifier_annotator_universal_model
-LOCAL_MODULE_STEM   := textclassifier.universal.model
-LOCAL_MODULE_CLASS  := ETC
-LOCAL_MODULE_OWNER  := google
-LOCAL_SRC_FILES     := ./models/textclassifier.universal.model
-LOCAL_MODULE_PATH   := $(TARGET_OUT_ETC)/textclassifier
-include $(BUILD_PREBUILT)
diff --git a/generate_flatbuffers.mk b/generate_flatbuffers.mk
deleted file mode 100644
index 1522463..0000000
--- a/generate_flatbuffers.mk
+++ /dev/null
@@ -1,46 +0,0 @@
-FLATC := $(HOST_OUT_EXECUTABLES)/flatc$(HOST_EXECUTABLE_SUFFIX)
-
-define transform-fbs-to-cpp
-@echo "Flatc: $@ <= $(PRIVATE_INPUT_FBS)"
-@rm -f $@
-@mkdir -p $(dir $@)
-$(FLATC) \
-    --cpp \
-    --no-union-value-namespacing \
-    --gen-object-api \
-    --keep-prefix \
-    -I $(INPUT_DIR) \
-    -o $(dir $@) \
-    $(PRIVATE_INPUT_FBS) \
-    || exit 33
-[ -f $@ ] || exit 33
-endef
-
-intermediates := $(call local-generated-sources-dir)
-
-# Generate utils/zlib/buffer_generated.h using FlatBuffer schema compiler.
-UTILS_ZLIB_BUFFER_FBS := $(LOCAL_PATH)/utils/zlib/buffer.fbs
-UTILS_ZLIB_BUFFER_H := $(intermediates)/utils/zlib/buffer_generated.h
-$(UTILS_ZLIB_BUFFER_H): PRIVATE_INPUT_FBS := $(UTILS_ZLIB_BUFFER_FBS)
-$(UTILS_ZLIB_BUFFER_H): INPUT_DIR := $(LOCAL_PATH)
-$(UTILS_ZLIB_BUFFER_H): $(FLATC) $(UTILS_ZLIB_BUFFER_FBS)
-	$(transform-fbs-to-cpp)
-LOCAL_GENERATED_SOURCES += $(UTILS_ZLIB_BUFFER_H)
-
-# Generate utils/intent/intent-config_generated.h using FlatBuffer schema compiler.
-INTENT_CONFIG_FBS := $(LOCAL_PATH)/utils/intents/intent-config.fbs
-INTENT_CONFIG_H := $(intermediates)/utils/intents/intent-config_generated.h
-$(INTENT_CONFIG_H): PRIVATE_INPUT_FBS := $(INTENT_CONFIG_FBS)
-$(INTENT_CONFIG_H): INPUT_DIR := $(LOCAL_PATH)
-$(INTENT_CONFIG_H): $(FLATC) $(INTENT_CONFIG_FBS)
-	$(transform-fbs-to-cpp)
-LOCAL_GENERATED_SOURCES += $(INTENT_CONFIG_H)
-
-# Generate annotator/model_generated.h using FlatBuffer schema compiler.
-ANNOTATOR_MODEL_FBS := $(LOCAL_PATH)/annotator/model.fbs
-ANNOTATOR_MODEL_H := $(intermediates)/annotator/model_generated.h
-$(ANNOTATOR_MODEL_H): PRIVATE_INPUT_FBS := $(ANNOTATOR_MODEL_FBS)
-$(ANNOTATOR_MODEL_H): INPUT_DIR := $(LOCAL_PATH)
-$(ANNOTATOR_MODEL_H): $(FLATC) $(ANNOTATOR_MODEL_FBS) $(INTENT_CONFIG_H)
-	$(transform-fbs-to-cpp)
-LOCAL_GENERATED_SOURCES += $(ANNOTATOR_MODEL_H)