From 61ce228576f2533e2e856d260ec9f942e2a38ed9 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 27 Sep 2023 11:02:35 -0700 Subject: [PATCH] Add cc_binary target for C Libraries PiperOrigin-RevId: 568902427 --- .../containers/category_converter.cc | 8 +++- .../containers/category_converter.h | 5 ++- .../classification_result_converter.cc | 3 +- .../classification_result_converter.h | 2 +- .../classifier_options_converter.cc | 6 +-- .../processors/classifier_options_converter.h | 2 +- .../tasks/c/core/base_options_converter.cc | 3 +- .../tasks/c/core/base_options_converter.h | 2 +- mediapipe/tasks/c/text/text_classifier/BUILD | 37 ++++++++++++++++++- .../c/text/text_classifier/text_classifier.cc | 12 +++--- .../c/text/text_classifier/text_classifier.h | 14 ++++--- 11 files changed, 70 insertions(+), 24 deletions(-) diff --git a/mediapipe/tasks/c/components/containers/category_converter.cc b/mediapipe/tasks/c/components/containers/category_converter.cc index 572ff5961..20179d316 100644 --- a/mediapipe/tasks/c/components/containers/category_converter.cc +++ b/mediapipe/tasks/c/components/containers/category_converter.cc @@ -15,10 +15,14 @@ limitations under the License. #include "mediapipe/tasks/c/components/containers/category_converter.h" +#include "mediapipe/tasks/c/components/containers/category.h" +#include "mediapipe/tasks/cc/components/containers/category.h" + namespace mediapipe::tasks::c::components::containers { -void CppConvertToCategory(mediapipe::tasks::components::containers::Category in, - Category* out) { +void CppConvertToCategory( + const mediapipe::tasks::components::containers::Category& in, + Category* out) { out->index = in.index; out->score = in.score; out->category_name = diff --git a/mediapipe/tasks/c/components/containers/category_converter.h b/mediapipe/tasks/c/components/containers/category_converter.h index daa0e7364..c3e48f6e3 100644 --- a/mediapipe/tasks/c/components/containers/category_converter.h +++ b/mediapipe/tasks/c/components/containers/category_converter.h @@ -21,8 +21,9 @@ limitations under the License. namespace mediapipe::tasks::c::components::containers { -void CppConvertToCategory(mediapipe::tasks::components::containers::Category in, - Category* out); +void CppConvertToCategory( + const mediapipe::tasks::components::containers::Category& in, + Category* out); } // namespace mediapipe::tasks::c::components::containers diff --git a/mediapipe/tasks/c/components/containers/classification_result_converter.cc b/mediapipe/tasks/c/components/containers/classification_result_converter.cc index c2fd0b704..1325aec9b 100644 --- a/mediapipe/tasks/c/components/containers/classification_result_converter.cc +++ b/mediapipe/tasks/c/components/containers/classification_result_converter.cc @@ -17,11 +17,12 @@ limitations under the License. #include "mediapipe/tasks/c/components/containers/category.h" #include "mediapipe/tasks/c/components/containers/category_converter.h" +#include "mediapipe/tasks/cc/components/containers/classification_result.h" namespace mediapipe::tasks::c::components::containers { void CppConvertToClassificationResult( - mediapipe::tasks::components::containers::ClassificationResult in, + const mediapipe::tasks::components::containers::ClassificationResult& in, ClassificationResult* out) { out->has_timestamp_ms = in.timestamp_ms.has_value(); if (out->has_timestamp_ms) { diff --git a/mediapipe/tasks/c/components/containers/classification_result_converter.h b/mediapipe/tasks/c/components/containers/classification_result_converter.h index a81d76e82..be4c745bc 100644 --- a/mediapipe/tasks/c/components/containers/classification_result_converter.h +++ b/mediapipe/tasks/c/components/containers/classification_result_converter.h @@ -22,7 +22,7 @@ limitations under the License. namespace mediapipe::tasks::c::components::containers { void CppConvertToClassificationResult( - mediapipe::tasks::components::containers::ClassificationResult in, + const mediapipe::tasks::components::containers::ClassificationResult& in, ClassificationResult* out); } // namespace mediapipe::tasks::c::components::containers diff --git a/mediapipe/tasks/c/components/processors/classifier_options_converter.cc b/mediapipe/tasks/c/components/processors/classifier_options_converter.cc index 11f9ecb49..e7a8f4e4a 100644 --- a/mediapipe/tasks/c/components/processors/classifier_options_converter.cc +++ b/mediapipe/tasks/c/components/processors/classifier_options_converter.cc @@ -19,10 +19,10 @@ limitations under the License. #include "mediapipe/tasks/c/components/processors/classifier_options.h" #include "mediapipe/tasks/cc/components/processors/classifier_options.h" -namespace mediapipe::c::components::processors { +namespace mediapipe::tasks::c::components::processors { void CppConvertToClassifierOptions( - ClassifierOptions in, + const ClassifierOptions& in, mediapipe::tasks::components::processors::ClassifierOptions* out) { out->display_names_locale = in.display_names_locale; out->max_results = in.max_results; @@ -38,4 +38,4 @@ void CppConvertToClassifierOptions( } } -} // namespace mediapipe::c::components::processors +} // namespace mediapipe::tasks::c::components::processors diff --git a/mediapipe/tasks/c/components/processors/classifier_options_converter.h b/mediapipe/tasks/c/components/processors/classifier_options_converter.h index 7f8019f04..2daa5c15f 100644 --- a/mediapipe/tasks/c/components/processors/classifier_options_converter.h +++ b/mediapipe/tasks/c/components/processors/classifier_options_converter.h @@ -22,7 +22,7 @@ limitations under the License. namespace mediapipe::tasks::c::components::processors { void CppConvertToClassifierOptions( - ClassifierOptions in, + const ClassifierOptions& in, mediapipe::tasks::components::processors::ClassifierOptions* out); } // namespace mediapipe::tasks::c::components::processors diff --git a/mediapipe/tasks/c/core/base_options_converter.cc b/mediapipe/tasks/c/core/base_options_converter.cc index c0bdf1539..c06d86b10 100644 --- a/mediapipe/tasks/c/core/base_options_converter.cc +++ b/mediapipe/tasks/c/core/base_options_converter.cc @@ -15,11 +15,12 @@ limitations under the License. #include "mediapipe/tasks/c/core/base_options_converter.h" +#include "mediapipe/tasks/c/core/base_options.h" #include "mediapipe/tasks/cc/core/base_options.h" namespace mediapipe::tasks::c::components::containers { -void CppConvertToBaseOptions(BaseOptions in, +void CppConvertToBaseOptions(const BaseOptions& in, mediapipe::tasks::core::BaseOptions* out) { out->model_asset_buffer = std::make_unique(in.model_asset_buffer); diff --git a/mediapipe/tasks/c/core/base_options_converter.h b/mediapipe/tasks/c/core/base_options_converter.h index 0890857fc..90db6397d 100644 --- a/mediapipe/tasks/c/core/base_options_converter.h +++ b/mediapipe/tasks/c/core/base_options_converter.h @@ -21,7 +21,7 @@ limitations under the License. namespace mediapipe::tasks::c::components::containers { -void CppConvertToBaseOptions(BaseOptions in, +void CppConvertToBaseOptions(const BaseOptions& in, mediapipe::tasks::core::BaseOptions* out); } // namespace mediapipe::tasks::c::components::containers diff --git a/mediapipe/tasks/c/text/text_classifier/BUILD b/mediapipe/tasks/c/text/text_classifier/BUILD index e095e2680..ca6936658 100644 --- a/mediapipe/tasks/c/text/text_classifier/BUILD +++ b/mediapipe/tasks/c/text/text_classifier/BUILD @@ -17,7 +17,7 @@ package(default_visibility = ["//mediapipe/tasks:internal"]) licenses(["notice"]) cc_library( - name = "text_classifier", + name = "text_classifier_lib", srcs = ["text_classifier.cc"], hdrs = ["text_classifier.h"], visibility = ["//visibility:public"], @@ -31,4 +31,39 @@ cc_library( "//mediapipe/tasks/cc/text/text_classifier", "@com_google_absl//absl/log:absl_log", ], + alwayslink = 1, +) + +# bazel build -c opt --linkopt -s --strip always --define MEDIAPIPE_DISABLE_GPU=1 \ +# //mediapipe/tasks/c/text/text_classifier:libtext_classifier.so +cc_binary( + name = "libtext_classifier.so", + linkopts = [ + "-Wl,-soname=libtext_classifier.so", + "-fvisibility=hidden", + ], + linkshared = True, + tags = [ + "manual", + "nobuilder", + "notap", + ], + deps = [":text_classifier_lib"], +) + +# bazel build --config darwin_arm64 -c opt --strip always --define MEDIAPIPE_DISABLE_GPU=1 \ +# //mediapipe/tasks/c/text/text_classifier:libtext_classifier.dylib +cc_binary( + name = "libtext_classifier.dylib", + linkopts = [ + "-Wl,-install_name,libtext_classifier.dylib", + "-fvisibility=hidden", + ], + linkshared = True, + tags = [ + "manual", + "nobuilder", + "notap", + ], + deps = [":text_classifier_lib"], ) diff --git a/mediapipe/tasks/c/text/text_classifier/text_classifier.cc b/mediapipe/tasks/c/text/text_classifier/text_classifier.cc index 388d03b94..0de123965 100644 --- a/mediapipe/tasks/c/text/text_classifier/text_classifier.cc +++ b/mediapipe/tasks/c/text/text_classifier/text_classifier.cc @@ -16,9 +16,9 @@ limitations under the License. #include "mediapipe/tasks/c/text/text_classifier/text_classifier.h" #include +#include #include "absl/log/absl_log.h" -#include "mediapipe/tasks/c/components/containers/classification_result.h" #include "mediapipe/tasks/c/components/containers/classification_result_converter.h" #include "mediapipe/tasks/c/components/processors/classifier_options.h" #include "mediapipe/tasks/c/components/processors/classifier_options_converter.h" @@ -38,7 +38,7 @@ using ::mediapipe::tasks::c::components::processors:: using ::mediapipe::tasks::text::text_classifier::TextClassifier; } // namespace -TextClassifier* CppTextClassifierCreate(TextClassifierOptions options) { +TextClassifier* CppTextClassifierCreate(const TextClassifierOptions& options) { auto cpp_options = std::make_unique< ::mediapipe::tasks::text::text_classifier::TextClassifierOptions>(); @@ -80,13 +80,13 @@ void CppTextClassifierClose(void* classifier) { extern "C" { -void* text_classifier_create(struct TextClassifierOptions options) { +void* text_classifier_create(struct TextClassifierOptions* options) { return mediapipe::tasks::c::text::text_classifier::CppTextClassifierCreate( - options); + *options); } -bool text_classifier_classify(void* classifier, char* utf8_str, - TextClassifierResult* result) { +int text_classifier_classify(void* classifier, char* utf8_str, + TextClassifierResult* result) { return mediapipe::tasks::c::text::text_classifier::CppTextClassifierClassify( classifier, utf8_str, result); } diff --git a/mediapipe/tasks/c/text/text_classifier/text_classifier.h b/mediapipe/tasks/c/text/text_classifier/text_classifier.h index 2c084bbed..1ba140883 100644 --- a/mediapipe/tasks/c/text/text_classifier/text_classifier.h +++ b/mediapipe/tasks/c/text/text_classifier/text_classifier.h @@ -20,11 +20,15 @@ limitations under the License. #include "mediapipe/tasks/c/components/processors/classifier_options.h" #include "mediapipe/tasks/c/core/base_options.h" +#ifndef MP_EXPORT +#define MP_EXPORT __attribute__((visibility("default"))) +#endif // MP_EXPORT + #ifdef __cplusplus extern "C" { #endif -typedef ClassificationResult TextClassifierResult; +typedef struct ClassificationResult TextClassifierResult; // The options for configuring a MediaPipe text classifier task. struct TextClassifierOptions { @@ -38,14 +42,14 @@ struct TextClassifierOptions { }; // Creates a TextClassifier from the provided `options`. -void* text_classifier_create(struct TextClassifierOptions options); +MP_EXPORT void* text_classifier_create(struct TextClassifierOptions* options); // Performs classification on the input `text`. -bool text_classifier_classify(void* classifier, char* utf8_str, - TextClassifierResult* result); +MP_EXPORT int text_classifier_classify(void* classifier, char* utf8_str, + TextClassifierResult* result); // Shuts down the TextClassifier when all the work is done. Frees all memory. -void text_classifier_close(void* classifier); +MP_EXPORT void text_classifier_close(void* classifier); #ifdef __cplusplus } // extern C