How to build tensorflow lite support ios framework on my onw?

Hi, I want to build tensorflow lite support library for ios device.

First i download the source code of tflite-support and tensorflow.

Then i realize that if want to build tensorflow lite as external library, i need to copy partial related packages in tflite-support WORKSPACE.

I try to resolve errors from build error message. And try to fix the depencency issues.

Currently i can success build “TensorFlowLiteTaskVisionC_framework” by this command “bazel build -c opt --config=ios_arm64 //tensorflow_lite_support/ios:TensorFlowLiteTaskVisionC_framework”.

But i still cannot pass the build by “bazel build -c opt --config=ios_arm64 //tensorflow_lite_support/ios:TensorFlowLiteTaskVision_framework”.

I notice i have encounter c++ version errors such as below:

ERROR: /Users/chung-iwu/codeBase/tensorflow/tflite-support/tensorflow_lite_support/ios/task/vision/utils/BUILD:24:13: Compiling tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm failed: (Exit
 1): wrapped_clang_pp failed: error executing command (from target //tensorflow_lite_support/ios/task/vision/utils:GMLImageCppUtils) external/local_config_cc/wrapped_clang_pp -target arm64-apple-ios12.0 '-stdlib=libc
++' '-std=gnu++11' '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign ... (remaining 49 arguments skipped)
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:58:
In file included from external/com_google_absl/absl/functional/function_ref.h:53:
In file included from external/com_google_absl/absl/base/attributes.h:37:
In file included from external/com_google_absl/absl/base/config.h:86:
external/com_google_absl/absl/base/policy_checks.h:79:2: error: "C++ versions less than C++14 are not supported."
#error "C++ versions less than C++14 are not supported."
 ^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:22:
In file included from external/com_google_absl/absl/container/inlined_vector.h:53:
In file included from external/com_google_absl/absl/container/internal/inlined_vector.h:30:
external/com_google_absl/absl/container/internal/compressed_tuple.h:77:16: error: no member named 'is_final' in namespace 'std'
         !std::is_final<T>::value &&
          ~~~~~^
external/com_google_absl/absl/container/internal/compressed_tuple.h:77:25: error: 'T' does not refer to a value
         !std::is_final<T>::value &&
                        ^
external/com_google_absl/absl/container/internal/compressed_tuple.h:74:20: note: declared here
template <typename T>
                   ^
external/com_google_absl/absl/container/internal/compressed_tuple.h:77:29: error: no member named 'value' in the global namespace
         !std::is_final<T>::value &&
                          ~~^
external/com_google_absl/absl/container/internal/compressed_tuple.h:75:16: error: no return statement in constexpr function
constexpr bool ShouldUseBase() {
               ^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:22:
In file included from external/com_google_absl/absl/container/inlined_vector.h:53:
In file included from external/com_google_absl/absl/container/internal/inlined_vector.h:31:
external/com_google_absl/absl/memory/memory.h:97:12: error: no member named 'make_unique' in namespace 'std'
using std::make_unique;
      ~~~~~^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:22:
In file included from external/com_google_absl/absl/container/inlined_vector.h:53:
In file included from external/com_google_absl/absl/container/internal/inlined_vector.h:33:
In file included from external/com_google_absl/absl/types/span.h:69:
external/com_google_absl/absl/types/internal/span.h:119:21: error: no template named 'remove_const_t' in namespace 'std'; did you mean simply 'remove_const_t'?
  using Container = std::remove_const_t<T>;
                    ^~~~~~~~~~~~~~~~~~~
                    remove_const_t
external/com_google_absl/absl/meta/type_traits.h:241:1: note: 'remove_const_t' declared here
using remove_const_t = typename std::remove_const<T>::type;
^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:22:
In file included from external/com_google_absl/absl/container/inlined_vector.h:53:
In file included from external/com_google_absl/absl/container/internal/inlined_vector.h:33:
In file included from external/com_google_absl/absl/types/span.h:69:
external/com_google_absl/absl/types/internal/span.h:130:24: error: no template named 'enable_if_t' in namespace 'std'; did you mean simply 'enable_if_t'?
using EnableIfIsView = std::enable_if_t<IsView<T>::value, int>;
                       ^~~~~~~~~~~~~~~~
                       enable_if_t
external/com_google_absl/absl/meta/type_traits.h:307:1: note: 'enable_if_t' declared here
using enable_if_t = typename std::enable_if<B, T>::type;
^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:22:
In file included from external/com_google_absl/absl/container/inlined_vector.h:53:
In file included from external/com_google_absl/absl/container/internal/inlined_vector.h:33:
In file included from external/com_google_absl/absl/types/span.h:69:
external/com_google_absl/absl/types/internal/span.h:133:27: error: no template named 'enable_if_t' in namespace 'std'; did you mean simply 'enable_if_t'?
using EnableIfNotIsView = std::enable_if_t<!IsView<T>::value, int>;
                          ^~~~~~~~~~~~~~~~
                          enable_if_t
external/com_google_absl/absl/meta/type_traits.h:307:1: note: 'enable_if_t' declared here
using enable_if_t = typename std::enable_if<B, T>::type;
^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:23:
In file included from external/com_google_absl/absl/strings/cord.h:79:
In file included from external/com_google_absl/absl/crc/internal/crc_cord_state.h:23:
In file included from external/com_google_absl/absl/crc/crc32c.h:32:
In file included from external/com_google_absl/absl/strings/str_format.h:78:
In file included from external/com_google_absl/absl/strings/internal/str_format/arg.h:34:
In file included from external/com_google_absl/absl/strings/internal/has_absl_stringify.h:21:
external/com_google_absl/absl/strings/string_view.h:338:18: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const]
  constexpr void remove_prefix(size_type n) {
                 ^
                                            const
external/com_google_absl/absl/strings/string_view.h:348:18: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const]
  constexpr void remove_suffix(size_type n) {
                 ^
                                            const
external/com_google_absl/absl/strings/string_view.h:356:18: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const]
  constexpr void swap(string_view& s) noexcept {
                 ^
                                      const
external/com_google_absl/absl/strings/string_view.h:340:10: error: cannot assign to non-static data member within const member function 'remove_prefix'
    ptr_ += n;
    ~~~~ ^
external/com_google_absl/absl/strings/string_view.h:338:18: note: member function 'absl::string_view::remove_prefix' is declared const here
  constexpr void remove_prefix(size_type n) {
  ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
external/com_google_absl/absl/strings/string_view.h:341:13: error: cannot assign to non-static data member within const member function 'remove_prefix'
    length_ -= n;
    ~~~~~~~ ^
external/com_google_absl/absl/strings/string_view.h:338:18: note: member function 'absl::string_view::remove_prefix' is declared const here
  constexpr void remove_prefix(size_type n) {
  ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
external/com_google_absl/absl/strings/string_view.h:338:18: error: constexpr function's return type 'void' is not a literal type
  constexpr void remove_prefix(size_type n) {
                 ^
external/com_google_absl/absl/strings/string_view.h:350:13: error: cannot assign to non-static data member within const member function 'remove_suffix'
    length_ -= n;
    ~~~~~~~ ^
external/com_google_absl/absl/strings/string_view.h:348:18: note: member function 'absl::string_view::remove_suffix' is declared const here
  constexpr void remove_suffix(size_type n) {
  ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
external/com_google_absl/absl/strings/string_view.h:348:18: error: constexpr function's return type 'void' is not a literal type
  constexpr void remove_suffix(size_type n) {
                 ^
external/com_google_absl/absl/strings/string_view.h:358:11: error: no viable overloaded '='
    *this = s;
    ~~~~~ ^ ~
external/com_google_absl/absl/strings/string_view.h:161:7: note: candidate function (the implicit copy assignment operator) not viable: 'this' argument has type 'const absl::string_view', but method is not marked con
st
class string_view {
      ^
external/com_google_absl/absl/strings/string_view.h:161:7: note: candidate function (the implicit move assignment operator) not viable: 'this' argument has type 'const absl::string_view', but method is not marked con
st
external/com_google_absl/absl/strings/string_view.h:356:18: error: constexpr function's return type 'void' is not a literal type
  constexpr void swap(string_view& s) noexcept {
                 ^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:23:
In file included from external/com_google_absl/absl/strings/cord.h:79:
In file included from external/com_google_absl/absl/crc/internal/crc_cord_state.h:23:
In file included from external/com_google_absl/absl/crc/crc32c.h:32:
In file included from external/com_google_absl/absl/strings/str_format.h:78:
In file included from external/com_google_absl/absl/strings/internal/str_format/arg.h:34:
external/com_google_absl/absl/strings/internal/has_absl_stringify.h:46:8: error: no template named 'enable_if_t' in namespace 'std'; did you mean simply 'enable_if_t'?
    T, std::enable_if_t<std::is_void<decltype(AbslStringify(
       ^~~~~~~~~~~~~~~~
       enable_if_t
external/com_google_absl/absl/meta/type_traits.h:307:1: note: 'enable_if_t' declared here
using enable_if_t = typename std::enable_if<B, T>::type;
^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:23:
In file included from external/com_google_absl/absl/strings/cord.h:79:
In file included from external/com_google_absl/absl/crc/internal/crc_cord_state.h:23:
In file included from external/com_google_absl/absl/crc/crc32c.h:32:
In file included from external/com_google_absl/absl/strings/str_format.h:78:
external/com_google_absl/absl/strings/internal/str_format/arg.h:141:8: error: no template named 'enable_if_t' in namespace 'std'; did you mean simply 'enable_if_t'?
    -> std::enable_if_t<std::is_enum<T>::value &&
       ^~~~~~~~~~~~~~~~
       enable_if_t
external/com_google_absl/absl/meta/type_traits.h:307:1: note: 'enable_if_t' declared here
using enable_if_t = typename std::enable_if<B, T>::type;
^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:23:
In file included from external/com_google_absl/absl/strings/cord.h:79:
In file included from external/com_google_absl/absl/crc/internal/crc_cord_state.h:23:
In file included from external/com_google_absl/absl/crc/crc32c.h:32:
In file included from external/com_google_absl/absl/strings/str_format.h:78:
external/com_google_absl/absl/strings/internal/str_format/arg.h:160:8: error: no template named 'enable_if_t' in namespace 'std'; did you mean simply 'enable_if_t'?
    -> std::enable_if_t<!std::is_enum<T>::value &&
       ^~~~~~~~~~~~~~~~
       enable_if_t
external/com_google_absl/absl/meta/type_traits.h:307:1: note: 'enable_if_t' declared here
using enable_if_t = typename std::enable_if<B, T>::type;
^
In file included from tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm:15:
In file included from ./tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h:19:
In file included from external/com_google_absl/absl/status/status.h:59:
In file included from external/com_google_absl/absl/status/internal/status_internal.h:23:
In file included from external/com_google_absl/absl/strings/cord.h:79:
In file included from external/com_google_absl/absl/crc/internal/crc_cord_state.h:23:
In file included from external/com_google_absl/absl/crc/crc32c.h:32:
In file included from external/com_google_absl/absl/strings/str_format.h:79:
In file included from external/com_google_absl/absl/strings/internal/str_format/bind.h:26:
In file included from external/com_google_absl/absl/strings/internal/str_format/checker.h:22:
external/com_google_absl/absl/strings/internal/str_format/constexpr_parser.h:44:20: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [
-Wconstexpr-not-const]
    constexpr void set_value(int value) {
                   ^
                                        const
external/com_google_absl/absl/strings/internal/str_format/constexpr_parser.h:55:20: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [
-Wconstexpr-not-const]
    constexpr void set_from_arg(int value) {
                   ^
                                           const
fatal error: too many errors emitted, stopping now [-ferror-limit=]
5 warnings and 20 errors generated.
Error in child process '/usr/bin/xcrun'. 1
Target //tensorflow_lite_support/ios:TensorFlowLiteTaskVision_framework failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 4.295s, Critical Path: 1.27s
INFO: 11 processes: 9 internal, 2 local.
FAILED: Build did NOT complete successfully

PS:
I have update some external library version such as tensorflow, com_google_absl, rules_apple, rules_ios, rules_swift to meet the configuration of tensorflow source code.

  1. Set Up Bazel: Install Bazel build system on your system.
  2. Clone TensorFlow Repository: Clone the TensorFlow repository from GitHub.
  3. Configure TensorFlow for iOS: Configure TensorFlow for iOS builds by running the configuration script in the TensorFlow repository.
  4. Build TensorFlow Lite Framework: Use Bazel to build the TensorFlow Lite framework for iOS.
  5. Integrate with Xcode: Once the TensorFlow Lite framework is built, integrate it into your Xcode project by adding it as a dependency and linking against it.
  6. Use TensorFlow Lite APIs: Finally, use TensorFlow Lite APIs in your iOS app to perform machine learning tasks.

You can find more detailed instructions and guidance in TensorFlow’s official documentation for iOS development

TensorFlow 2.16 + Python 3.12 – JARaaS Hybrid RAG - 6/16/2024

Building TensorFlow Lite for iOS can be challenging, particularly when dealing with C++ version compatibility issues. Based on your provided error log and steps, here are a few suggestions to help you resolve these issues and successfully build the TensorFlow Lite Task Vision framework for iOS:

Steps to Build TensorFlow Lite Support for iOS:

  1. Update your Build Tools and Environment:
    Ensure you have the necessary tools and their latest versions:

    • Bazel: Ensure you have the latest stable version.
    • Xcode: Update to the latest version and ensure command line tools are installed and licensed.
    xcode-select --install
    sudo xcodebuild -license
    
  2. Ensure C++14 or Greater is Used:
    The errors indicate that C++14 standard or later is required. Update your build configuration to use C++14.

    Modify the BUILD file or your Bazel configuration to specify the C++ version:

    copts = [
        "-stdlib=libc++",
        "-std=c++14"
    ]
    
  3. Set Up Dependencies:
    Update the versions of external dependencies like TensorFlow, com_google_absl, and other rules to the versions that are compatible with the C++14 standard.

    # Example to update dependencies in WORKSPACE
    http_archive(
        name = "com_google_absl",
        urls = ["https://github.com/abseil/abseil-cpp/archive/refs/tags/20210324.2.tar.gz"],
        strip_prefix = "abseil-cpp-20210324.2",
    )
    
  4. Clean and Rebuild:
    Sometimes, you need to clean your Bazel build cache to resolve build errors from previous builds.

    bazel clean --expunge
    bazel build -c opt --config=ios_arm64 //tensorflow_lite_support/ios:TensorFlowLiteTaskVision_framework
    
  5. Check for Flag Compatibility:
    Ensure that any custom flags or optimizations do not conflict with the required build configuration for iOS. Double-check any specific configurations in .bazelrc.

Example of Build Command:

Here’s an example command based on your provided steps:

bazel build -c opt --config=ios_arm64 //tensorflow_lite_support/ios:TensorFlowLiteTaskVision_framework

If you encounter further errors, use --verbose_failures to get more detailed output, which can be critical in diagnosing build issues.

bazel build -c opt --config=ios_arm64 //tensorflow_lite_support/ios:TensorFlowLiteTaskVision_framework --verbose_failures

Sources:

  • Build from Source: build_from_source.md (internal document)
  • Errors Encountered During Build: errors.md (internal document)
  • Windows GPU Build Specifications: source_windows.md (internal document)

Keep these suggestions in mind while troubleshooting, and ensure all your tools and dependencies are up-to-date and correctly configured. If you still encounter issues, a detailed examination of your build configuration and dependencies will be required.

Hi all. I finally figure out how to build xcframework for my applications. And my apps can call api without install frameworks by cocoapods.

Below is what i did.

Bazel build

  1. I use pyenv to manage python version on my pc. And i currently use 3.11
  2. Install numpy python package
  3. Install cmake 3.24.4 by binary from official site
  4. build below frameworks by script mediapipe/tasks/ios/build_ios_framework.sh
FRAMEWORK_NAME=MediaPipeTasksCommon ./mediapipe/tasks/ios/build_ios_framework.sh
FRAMEWORK_NAME=MediaPipeTasksVision ./mediapipe/tasks/ios/build_ios_framework.sh

Xcode project settings
Once build successfull, i uncompress and move frameworks and graphs to my xcode project.

MediaPipeTasksVision.xcframework
MediaPipeTasksCommon.xcframework
graph_libraries

And modify Build Settings / Other Linker Flags as below.

Debug & Release

-ObjC -l"c++" -framework "AVFoundation" -framework "Accelerate" -framework "AssetsLibrary" -framework "CoreFoundation" -framework "CoreGraphics" -framework "CoreImage" -framework "CoreMedia" -framework "CoreVideo" -framework "MediaPipeTasksCommon" -framework "MediaPipeTasksVision" -framework "QuartzCore"

for Any iOS Simulator SDK

$(inherited) -force_load $(PROJECT_DIR)/$LIBPATH/libMediaPipeTasksCommon_simulator_graph.a

for Any iOS SDK

$(inherited) -force_load $(PROJECT_DIR)/$LIBPATH/libMediaPipeTasksCommon_device_graph.a

PS: $LIBPATH indecates folder path of your graph files.