Support both dynamic and static framworks in ios C library

PiperOrigin-RevId: 357516469
Change-Id: I8a3933a1a3e2c3742201b1b571b208eca5efb34c
This commit is contained in:
Thai Nguyen 2021-02-14 23:34:43 -08:00 committed by TensorFlower Gardener
parent fc85b20003
commit de8e18a12a
3 changed files with 46 additions and 11 deletions
tensorflow/lite

View File

@ -10,8 +10,9 @@ details on how to use them in your iOS projects.
In some cases, you might wish to use a local build of TensorFlow Lite, for
example when you want to make local changes to TensorFlow Lite and test those
changes in your iOS app. To create a universal iOS framework for TensorFlow Lite
locally, you need to build it using Bazel on a macOS machine.
changes in your iOS app or you prefer using static framework to our provided
dynamic one. To create a universal iOS framework for TensorFlow Lite locally,
you need to build it using Bazel on a macOS machine.
### Install Xcode
@ -42,7 +43,7 @@ Run the `./configure` script in the root TensorFlow checkout directory, and
answer "Yes" when the script asks if you wish to build TensorFlow with iOS
support.
### Build TensorFlowLiteC framework
### Build TensorFlowLiteC dynamic framework (recommended)
Note: This step is not necessary if (1) you are using Bazel for your app, or (2)
you only want to test local changes to the Swift or Objective-C APIs. In these
@ -64,6 +65,22 @@ arm64, and x86_64 (but no i386). To see the full list of build flags used when
you specify `--config=ios_fat`, please refer to the iOS configs section in the
[`.bazelrc` file][bazelrc].
### Build TensorFlowLiteC static framework
By default, we only distribute the dynamic framework via Cocoapods. If you want
to use the static framework instead, you can build the `TensorFlowLiteC` static
framework with the following command:
```
bazel build --config=ios_fat -c opt \
//tensorflow/lite/ios:TensorFlowLiteC_static_framework
```
The command will generate a file named `TensorFlowLiteC_static_framework.zip`
under `bazel-bin/tensorflow/lite/ios/` directory under your TensorFlow root
directory. This static framework can be used in the exact same way as the
dynamic one.
## Use in your own application
### CocoaPods developers

View File

@ -5,7 +5,7 @@ load(
"//tensorflow/lite/ios:ios.bzl",
"TFL_MINIMUM_OS_VERSION",
"strip_common_include_path_prefix",
"tflite_ios_static_framework",
"tflite_ios_framework",
)
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_static_framework")
@ -99,7 +99,7 @@ strip_common_include_path_prefix(
)
# bazel build -c opt --config=ios_fat //tensorflow/lite/ios:TensorFlowLiteC_framework
tflite_ios_static_framework(
tflite_ios_framework(
name = "TensorFlowLiteC_framework",
hdrs = [
":c_api.h",
@ -115,6 +115,23 @@ tflite_ios_static_framework(
],
)
# Similar to TensorFlowLiteC_framework but this is a static framework and symbol
# hiding is not applied. Note both have the same bundle name.
ios_static_framework(
name = "TensorFlowLiteC_static_framework",
hdrs = [
":c_api.h",
":common.h",
":xnnpack_delegate.h",
"//tensorflow/lite/c:c_api_types.h",
],
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
@ -140,7 +157,7 @@ ios_static_framework(
# TensorFlowLiteC framework above in a composable way.
#
# bazel build -c opt --config=ios_fat //tensorflow/lite/ios:TensorFlowLiteCCoreML_framework
tflite_ios_static_framework(
tflite_ios_framework(
name = "TensorFlowLiteCCoreML_framework",
hdrs = [
":coreml_delegate.h",
@ -159,7 +176,7 @@ tflite_ios_static_framework(
# TensorFlowLiteC framework above in a composable way.
#
# bazel build -c opt --config=ios_fat //tensorflow/lite/ios:TensorFlowLiteCMetal_framework
tflite_ios_static_framework(
tflite_ios_framework(
name = "TensorFlowLiteCMetal_framework",
hdrs = [
":metal_delegate.h",
@ -208,5 +225,6 @@ build_test(
":TensorFlowLiteCMetal_framework",
":TensorFlowLiteC_framework",
":TensorFlowLiteSelectTfOps_framework",
":TensorFlowLiteC_static_framework",
],
)

View File

@ -17,17 +17,17 @@ TFL_DISABLED_SANITIZER_TAGS = [
"notsan",
]
# iOS static framework with symbol allowlist. Exported C++ symbols might cause
# symbol collision with other libraries. List of symbols to allowlist can be
# iOS framework with symbol allowlist. Exported C++ symbols might cause symbol
# collision with other libraries. List of symbols to allowlist can be
# generated by running `nm -m -g FRAMEWORK_LIBRARY | grep _TfLite` for framework
# built with `ios_static_framework` rule.
def tflite_ios_static_framework(
def tflite_ios_framework(
name,
bundle_name,
allowlist_symbols_file,
exclude_resources = True,
**kwargs):
"""TFLite variant of ios_static_framework with symbol hiding.
"""Apply symbol hiding to the output of ios_static_framework.
Args:
name: The name of the target.