diff --git a/mediapipe/tasks/ios/BUILD b/mediapipe/tasks/ios/BUILD new file mode 100644 index 000000000..8786ed49f --- /dev/null +++ b/mediapipe/tasks/ios/BUILD @@ -0,0 +1,100 @@ +# Copyright 2023 The MediaPipe Authors. All Rights Reserved. +# +# 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. + +load( + "//mediapipe/tasks/ios:ios.bzl", + "MPP_TASK_MINIMUM_OS_VERSION", + "strip_api_include_path_prefix", +) +load( + "@build_bazel_rules_apple//apple:apple.bzl", + "apple_static_xcframework", + "apple_static_library", +) + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) + +config_setting( + name = "avoid_linking_graphs", + define_values = { + "MEDIAPIPE_AVOID_LINKING_GRAPHS": "1", + }, + visibility = ["//visibility:public"], +) + +strip_api_include_path_prefix( + name = "strip_api_include_path", + hdr_labels = [ + "//mediapipe/tasks/ios/common:sources/MPPCommon.h", + "//mediapipe/tasks/ios/components/containers:sources/MPPCategory.h", + "//mediapipe/tasks/ios/components/containers:sources/MPPClassificationResult.h", + "//mediapipe/tasks/ios/components/containers:sources/MPPEmbedding.h", + "//mediapipe/tasks/ios/components/containers:sources/MPPEmbeddingResult.h", + "//mediapipe/tasks/ios/core:sources/MPPBaseOptions.h", + "//mediapipe/tasks/ios/core:sources/MPPTaskOptions.h", + "//mediapipe/tasks/ios/core:sources/MPPTaskResult.h", + "//mediapipe/tasks/ios/text/text_classifier:sources/MPPTextClassifier.h", + "//mediapipe/tasks/ios/text/text_classifier:sources/MPPTextClassifierOptions.h", + "//mediapipe/tasks/ios/text/text_classifier:sources/MPPTextClassifierResult.h", + "//mediapipe/tasks/ios/text/text_embedder:sources/MPPTextEmbedder.h", + "//mediapipe/tasks/ios/text/text_embedder:sources/MPPTextEmbedderOptions.h", + "//mediapipe/tasks/ios/text/text_embedder:sources/MPPTextEmbedderResult.h", + ], +) + +apple_static_xcframework( + name = "MediaPipeTaskText_framework", + public_hdrs = [ + ":MPPBaseOptions.h", + ":MPPCategory.h", + ":MPPClassificationResult.h", + ":MPPEmbedding.h", + ":MPPEmbeddingResult.h", + ":MPPCommon.h", + ":MPPTaskOptions.h", + ":MPPTaskResult.h", + ":MPPTextClassifier.h", + ":MPPTextClassifierOptions.h", + ":MPPTextClassifierResult.h", + ":MPPTextEmbedder.h", + ":MPPTextEmbedderOptions.h", + ":MPPTextEmbedderResult.h", + ], + bundle_name = "MediaPipeTaskText", + ios = { + "simulator" : ["arm64", "x86_64"], + "device" : ["arm64"], + }, + minimum_os_versions = { + "ios": MPP_TASK_MINIMUM_OS_VERSION, + }, + deps = [ + "//mediapipe/tasks/ios/text/text_classifier:MPPTextClassifier", + "//mediapipe/tasks/ios/text/text_embedder:MPPTextEmbedder", + "@org_tensorflow//third_party/icu/data:conversion_data", + ], +) + +apple_static_library( + name = "MediaPipeTaskText_GraphLibrary", + platform_type = "ios", + minimum_os_version = MPP_TASK_MINIMUM_OS_VERSION, + deps = [ + "//mediapipe/tasks/cc/text/text_classifier:text_classifier_graph", + "//mediapipe/tasks/cc/text/text_embedder:text_embedder_graph", + "@org_tensorflow//third_party/icu/data:conversion_data", + ], +) \ No newline at end of file diff --git a/mediapipe/tasks/ios/ios.bzl b/mediapipe/tasks/ios/ios.bzl index 8fe2a24a1..fdfc85d5f 100644 --- a/mediapipe/tasks/ios/ios.bzl +++ b/mediapipe/tasks/ios/ios.bzl @@ -1,3 +1,40 @@ """MediaPipe Task Library Helper Rules for iOS""" MPP_TASK_MINIMUM_OS_VERSION = "11.0" + +# When the static framework is built with bazel, the all header files are moved +# to the "Headers" directory with no header path prefixes. This auxiliary rule +# is used for stripping the path prefix to the C/iOS API header files included by +# other C/iOS API header files. +# In case of C header files includes start with a keyword of "#include'. +# Imports in iOS header files start with a keyword of '#import'. +def strip_api_include_path_prefix(name, hdr_labels, prefix = ""): + """Create modified header files with the import path stripped out. + + Args: + name: The name to be used as a prefix to the generated genrules. + hdr_labels: List of header labels to strip out the include path. Each + label must end with a colon followed by the header file name. + prefix: Optional prefix path to prepend to the header inclusion path. + """ + for hdr_label in hdr_labels: + hdr_filename = hdr_label.split(":")[-1] + + # The last path component of iOS header files is sources/some_file.h + # Hence it wiill contain a '/'. So the string can be split at '/' to get + # the header file name. + if "/" in hdr_filename: + hdr_filename = hdr_filename.split("/")[-1] + + hdr_basename = hdr_filename.split(".")[0] + native.genrule( + name = "{}_{}".format(name, hdr_basename), + srcs = [hdr_label], + outs = [hdr_filename], + cmd = """ + sed 's|#\\([a-z]*\\) ".*/\\([^/]\\{{1,\\}}\\.h\\)"|#\\1 "{}\\2"|'\ + "$(location {})"\ + > "$@" + """.format(prefix, hdr_label), + ) + diff --git a/mediapipe/tasks/ios/text/text_classifier/BUILD b/mediapipe/tasks/ios/text/text_classifier/BUILD index 7913340ac..97cf27677 100644 --- a/mediapipe/tasks/ios/text/text_classifier/BUILD +++ b/mediapipe/tasks/ios/text/text_classifier/BUILD @@ -49,7 +49,6 @@ objc_library( ":MPPTextClassifierOptions", ":MPPTextClassifierResult", "//mediapipe/tasks/cc/components/containers/proto:classifications_cc_proto", - "//mediapipe/tasks/cc/text/text_classifier:text_classifier_graph", "//mediapipe/tasks/ios/common/utils:MPPCommonUtils", "//mediapipe/tasks/ios/common/utils:NSStringHelpers", "//mediapipe/tasks/ios/core:MPPTaskInfo", @@ -58,5 +57,10 @@ objc_library( "//mediapipe/tasks/ios/text/core:MPPTextTaskRunner", "//mediapipe/tasks/ios/text/text_classifier/utils:MPPTextClassifierOptionsHelpers", "//mediapipe/tasks/ios/text/text_classifier/utils:MPPTextClassifierResultHelpers", - ], + ] + select({ + "//mediapipe/tasks/ios:avoid_linking_graphs": [], + "//conditions:default": [ + "//mediapipe/tasks/cc/text/text_classifier:text_classifier_graph", + ], + }), ) diff --git a/mediapipe/tasks/ios/text/text_embedder/BUILD b/mediapipe/tasks/ios/text/text_embedder/BUILD index a600d5366..a2edd8a2a 100644 --- a/mediapipe/tasks/ios/text/text_embedder/BUILD +++ b/mediapipe/tasks/ios/text/text_embedder/BUILD @@ -48,7 +48,6 @@ objc_library( deps = [ ":MPPTextEmbedderOptions", ":MPPTextEmbedderResult", - "//mediapipe/tasks/cc/text/text_embedder:text_embedder_graph", "//mediapipe/tasks/ios/common/utils:MPPCommonUtils", "//mediapipe/tasks/ios/common/utils:NSStringHelpers", "//mediapipe/tasks/ios/components/utils:MPPCosineSimilarity", @@ -58,5 +57,10 @@ objc_library( "//mediapipe/tasks/ios/text/core:MPPTextTaskRunner", "//mediapipe/tasks/ios/text/text_embedder/utils:MPPTextEmbedderOptionsHelpers", "//mediapipe/tasks/ios/text/text_embedder/utils:MPPTextEmbedderResultHelpers", - ], + ] + select({ + "//mediapipe/tasks/ios:avoid_linking_graphs": [], + "//conditions:default": [ + "//mediapipe/tasks/cc/text/text_embedder:text_embedder_graph", + ], + }), )