Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rachguo authored and rachguo committed Mar 15, 2024
1 parent 90ebcfb commit 944eb25
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import sys


# Note: This script is mainly used for handling extracting duplicate named .o files under different subdirectories for
# Note: This script is mainly used for sanity checking/validating the files in the .a library equal to the .o files
# in the source dir to handle the case of source files having duplicate names under different subdirectories for
# each onnxruntime library. (Only applicable when doing a Mac Catalyst build.)
def main():
source_dir = sys.argv[1]
Expand All @@ -23,16 +24,17 @@ def main():

dest_file = f"{dest_name_without_extension}.o"
while os.path.exists(os.path.join(dest_dir, dest_file)):
print("Duplicate named files: " + os.path.join(dest_dir, dest_file))
print("Duplicate file name from source: " + os.path.join(source_dir, subdir, file_name))
counter += 1
dest_file = f"{dest_name_without_extension}_{counter}.o"
print("Renamed file name in destination: " + os.path.join(dest_dir, dest_file))

destination_path = os.path.join(dest_dir, dest_file)
source_file = os.path.join(source_dir, subdir, file_name)
shutil.copy(source_file, destination_path)

# Sanity check to ensure the number of .o object from the original cmake source directory matches with the number
# of .o files extracted from each onnxruntime library
# of .o files extracted from each .a onnxruntime library
file_lists_from_static_lib = []
with open(files_from_static_lib) as file:
filenames = file.readlines()
Expand Down
16 changes: 9 additions & 7 deletions cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,19 @@ if(onnxruntime_BUILD_APPLE_FRAMEWORK)
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CUR_STATIC_LIB_OBJ_DIR})
if (PLATFORM_NAME STREQUAL "macabi")
# Handle the case where extracting .o files with duplicate names under different subdirectories within
# There exists several duplicate names for source files under different subdirectories within
# each onnxruntime library. (e.g. onnxruntime/contrib_ops/cpu/element_wise_ops.o
# vs. onnxruntime/providers/core/cpu/math/element_wise_ops.o)
# Simply use 'ar ARGS -x' for extracting the .o files would possibly cause duplicate naming files being overwritten.
# In that case, using 'ar ARGS -x' to extract the .o files from .a lib would possibly cause duplicate naming files being overwritten
# and lead to missing undefined symbol error in the generated binary.
# So we use the below python script as a sanity check to do a recursive find of all .o files in ${CUR_TARGET_CMAKE_SOURCE_LIB_DIR}
# and verifies that matches the content of the .a, and then copy from the source dir.
# TODO: The copying action here isn't really necessary. For future fix, consider using the script extracts from the ar with the rename to potentially
# make both maccatalyst and other builds do the same thing.
set(CUR_TARGET_CMAKE_SOURCE_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_LIB}.dir)
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ar -t $<TARGET_FILE:${_LIB}> | grep "\.o$" > object_file_list.txt
WORKING_DIRECTORY ${CUR_STATIC_LIB_OBJ_DIR})
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND mkdir -p ${CUR_STATIC_LIB_OBJ_DIR}
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_CURRENT_SOURCE_DIR}/handle_duplicate_object_files.py ${CUR_TARGET_CMAKE_SOURCE_LIB_DIR} ${CUR_STATIC_LIB_OBJ_DIR} ${CUR_STATIC_LIB_OBJ_DIR}/object_file_list.txt
COMMAND ar -t $<TARGET_FILE:${_LIB}> | grep "\.o$" > ${_LIB}.object_file_list.txt
COMMAND ${CMAKE_COMMAND} -E env python3 ${CMAKE_CURRENT_SOURCE_DIR}/maccatalyst_prepare_objects_for_prelink.py ${CUR_TARGET_CMAKE_SOURCE_LIB_DIR} ${CUR_STATIC_LIB_OBJ_DIR} ${CUR_STATIC_LIB_OBJ_DIR}/${_LIB}.object_file_list.txt
WORKING_DIRECTORY ${CUR_STATIC_LIB_OBJ_DIR})
else()
add_custom_command(TARGET onnxruntime POST_BUILD
Expand Down

0 comments on commit 944eb25

Please sign in to comment.