STT-tensorflow/tensorflow/lite/ios/BUILD.apple
YoungSeok Yoon dff3c8a47a Fix iOS static framework link error by using custom object file extractor
With the bazel version update from 3.1.0 to 3.7.2, the object file hash values
are no longer added when their names are unique within an objc_library. See:
* https://github.com/bazelbuild/bazel/issues/11846
* https://github.com/bazelbuild/bazel/pull/11958

However, object name collision can still happen when there is a source code with
the same basename in a transitive dependency tree. Normally there is no problem
with this, but this had an unfortunate interaction with the symbol hiding script
we use for building the iOS static framework. That is, when an archive file (.a)
contains more than one object file with the same name, the 'ar -x' command
executed as part of the symbol hiding script would overwrite the conflicting
object file, causing the overwritten object file to be not included in the final
static framework.

This was causing a link error at CocoaPods lint step, as it had some missing
function definitions. This is now fixed by using a custom extraction script
written in Python, which gracefully handles the object file name collision.

This also fixes a previously existed race condition when the symbol hiding
script is run in parallel for multiple static framework targets, by using
separate temporary directories while extracting the object files.

Verified that this fix works with CocoaPods lint.

PiperOrigin-RevId: 351118550
Change-Id: Iec26e4720c21f271822785032d5fb6f4717eebca
2021-01-11 03:32:23 -08:00

212 lines
6.1 KiB
Plaintext

# TensorFlow Lite for iOS
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load(
"//tensorflow/lite/ios:ios.bzl",
"TFL_MINIMUM_OS_VERSION",
"strip_common_include_path_prefix",
"tflite_ios_static_framework",
)
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_static_framework")
# buildifier: disable=same-origin-load
load("//tensorflow:tensorflow.bzl", "pytype_strict_binary")
# buildifier: disable=same-origin-load
load("//tensorflow:tensorflow.bzl", "pytype_strict_library")
load("//tensorflow:tensorflow.bzl", "py_strict_test")
package(
default_visibility = [
"//tensorflow/lite:__subpackages__",
],
licenses = ["notice"], # Apache 2.0
)
sh_binary(
name = "hide_symbols_with_allowlist",
srcs = [
"hide_symbols_with_allowlist.sh",
],
visibility = [
"//tensorflow/lite:__subpackages__",
"//tensorflow_lite_support:__subpackages__",
],
)
pytype_strict_library(
name = "extract_object_files",
srcs = [
"extract_object_files.py",
],
)
pytype_strict_binary(
name = "extract_object_files_main",
srcs = [
"extract_object_files_main.py",
],
python_version = "PY3",
srcs_version = "PY3",
visibility = [
"//tensorflow/lite:__subpackages__",
"//tensorflow_lite_support:__subpackages__",
],
deps = [
":extract_object_files",
],
)
filegroup(
name = "extract_object_files_testdata",
srcs = glob(["testdata/**"]),
)
py_strict_test(
name = "extract_object_files_test",
srcs = [
"extract_object_files_test.py",
],
data = [
":extract_object_files_testdata",
],
python_version = "PY3",
srcs_version = "PY3",
deps = [
":extract_object_files",
"//tensorflow/python/platform",
"//tensorflow/python/platform:client_testlib",
"@absl_py//absl/testing:parameterized",
],
)
strip_common_include_path_prefix(
name = "strip_common_include_path_core",
hdr_labels = [
"//tensorflow/lite/c:c_api.h",
"//tensorflow/lite/c:common.h",
"//tensorflow/lite/delegates/xnnpack:xnnpack_delegate.h",
],
)
strip_common_include_path_prefix(
name = "strip_common_include_path_subspecs",
hdr_labels = [
"//tensorflow/lite/delegates/coreml:coreml_delegate.h",
"//tensorflow/lite/delegates/gpu:metal_delegate.h",
],
prefix = "TensorFlowLiteC/",
)
# bazel build -c opt --config=ios_fat //tensorflow/lite/ios:TensorFlowLiteC_framework
tflite_ios_static_framework(
name = "TensorFlowLiteC_framework",
hdrs = [
":c_api.h",
":common.h",
":xnnpack_delegate.h",
"//tensorflow/lite/c:c_api_types.h",
],
allowlist_symbols_file = ":allowlist_TensorFlowLiteC.txt",
bundle_name = "TensorFlowLiteC",
minimum_os_version = TFL_MINIMUM_OS_VERSION,
deps = [
":tensorflow_lite_c",
],
)
# This target builds the flex delegate as a separate static framework, which
# does not include the TensorFlow Lite runtime. As this target does not contain
# TensorFlow Lite runtime, it is intended to be linked along with the
# TensorFlowLiteC framework above in a composable way.
#
# The flex delegate cannot be built for i386, so it can't be built with ios_fat
# config.
#
# bazel build -c opt --config=ios --ios_multi_cpus=armv7,arm64,x86_64 //tensorflow/lite/ios:TensorFlowLiteSelectTfOps_framework
ios_static_framework(
name = "TensorFlowLiteSelectTfOps_framework",
avoid_deps = ["//tensorflow/lite/c:common"],
bundle_name = "TensorFlowLiteSelectTfOps",
minimum_os_version = TFL_MINIMUM_OS_VERSION,
deps = [
"//tensorflow/lite/delegates/flex:delegate",
],
)
# This target builds the Core ML delegate as a separate static framework, which
# does not include the TensorFlow Lite runtime. As this target does not contain
# TensorFlow Lite runtime, it is intended to be linked along with the
# TensorFlowLiteC framework above in a composable way.
#
# bazel build -c opt --config=ios_fat //tensorflow/lite/ios:TensorFlowLiteCCoreML_framework
tflite_ios_static_framework(
name = "TensorFlowLiteCCoreML_framework",
hdrs = [
":coreml_delegate.h",
],
allowlist_symbols_file = ":allowlist_TensorFlowLiteCCoreML.txt",
bundle_name = "TensorFlowLiteCCoreML",
minimum_os_version = TFL_MINIMUM_OS_VERSION,
deps = [
"//tensorflow/lite/delegates/coreml:coreml_delegate",
],
)
# This target builds the Metal delegate as a separate static framework, which
# does not include the TensorFlow Lite runtime. As this target does not contain
# TensorFlow Lite runtime, it is intended to be linked along with the
# TensorFlowLiteC framework above in a composable way.
#
# bazel build -c opt --config=ios_fat //tensorflow/lite/ios:TensorFlowLiteCMetal_framework
tflite_ios_static_framework(
name = "TensorFlowLiteCMetal_framework",
hdrs = [
":metal_delegate.h",
],
allowlist_symbols_file = ":allowlist_TensorFlowLiteCMetal.txt",
bundle_name = "TensorFlowLiteCMetal",
minimum_os_version = TFL_MINIMUM_OS_VERSION,
deps = [
"//tensorflow/lite/delegates/gpu:metal_delegate",
],
)
cc_library(
name = "tensorflow_lite_c",
hdrs = [
"//tensorflow/lite/c:c_api.h",
"//tensorflow/lite/c:c_api_types.h",
"//tensorflow/lite/c:common.h",
"//tensorflow/lite/delegates/xnnpack:xnnpack_delegate.h",
],
tags = [
"nobuilder",
"swift_module=TensorFlowLiteC",
],
deps = [
"//tensorflow/lite/c:c_api",
"//tensorflow/lite/c:common",
"//tensorflow/lite/delegates/xnnpack:xnnpack_delegate",
],
)
# Used for building TensorFlowLiteC framework.
build_test(
name = "framework_build_test",
# build_test targets are not meant to be run with sanitizers.
tags = [
"noasan",
"nomsan",
"notsan",
# TODO(b/176993122): restore once the apple_genrule issue is resolved.
"notap",
],
targets = [
":TensorFlowLiteCCoreML_framework",
":TensorFlowLiteCMetal_framework",
":TensorFlowLiteC_framework",
":TensorFlowLiteSelectTfOps_framework",
],
)