STT-tensorflow/tensorflow/lite/ios/extract_object_files_main.py
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

39 lines
1.3 KiB
Python

# Copyright 2021 The TensorFlow 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.
# ==============================================================================
# Lint as: python3
"""Command line tool version of the extract_object_files module.
This command line tool version takes the archive file path and the destination
directory path as the positional command line arguments.
"""
import sys
from typing import Sequence
from tensorflow.lite.ios import extract_object_files
def main(argv: Sequence[str]) -> None:
if len(argv) != 3:
raise RuntimeError('Usage: {} <archive_file> <dest_dir>'.format(argv[0]))
archive_path = argv[1]
dest_dir = argv[2]
with open(archive_path, 'rb') as archive_file:
extract_object_files.extract_object_files(archive_file, dest_dir)
if __name__ == '__main__':
main(sys.argv)