Skip to content

Commit

Permalink
Pre-link when creating static library for apple framework (microsoft#…
Browse files Browse the repository at this point in the history
…18241)

### Description
<!-- Describe your changes. -->
Pre-link with `ld -r` to apply symbol visibility when the static library
is created to replicate XCode's Single Object Pre-link.

Current builds set the visibility flags but that doesn't get applied
until the static library is linked into something else, which can be too
late. Pre-linking fixes this.

The pre-link uses the .o files from the ORT static libraries and the .a
files from external libraries. This combination limits the symbols
included from the .a files to things required by the ORT .o files.

In order to minimize changes elsewhere in the build we extract the .o
files from the ORT static libraries using `ar -x`.

Re-ordered the pieces use to build the Apple framework to make it a
little more readable.
Fixed a couple of misc issues with missing symbols from the minimal
build that show up when pre-linking is applied.

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
Will hopefully address microsoft#17722
  • Loading branch information
skottmckay authored and kleiti committed Mar 22, 2024
1 parent ed89615 commit b50fc09
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
81 changes: 57 additions & 24 deletions cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -282,44 +282,77 @@ endif()

# Assemble the Apple static framework (iOS and macOS)
if(onnxruntime_BUILD_APPLE_FRAMEWORK)
if(${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
set(STATIC_FRAMEWORK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}-${CMAKE_OSX_SYSROOT})
else() # macOS
set(STATIC_FRAMEWORK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif()

# Setup the various directories required. Remove any existing ones so we start with a clean directory.
set(STATIC_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/static_libraries)
file(MAKE_DIRECTORY ${STATIC_LIB_DIR})
set(STATIC_LIB_TEMP_DIR ${STATIC_LIB_DIR}/temp)
add_custom_command(TARGET onnxruntime PRE_BUILD COMMAND ${CMAKE_COMMAND} -E rm -rf ${STATIC_LIB_DIR})
add_custom_command(TARGET onnxruntime PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${STATIC_LIB_DIR})
add_custom_command(TARGET onnxruntime PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${STATIC_LIB_TEMP_DIR})

# Remove the existing files in the STATIC_LIB_DIR folder
file(GLOB _OLD_STATIC_LIBS ${STATIC_LIB_DIR}/*.a)
file(REMOVE "${_OLD_STATIC_LIBS}")
set(STATIC_FRAMEWORK_DIR ${STATIC_FRAMEWORK_OUTPUT_DIR}/static_framework/onnxruntime.framework)
add_custom_command(TARGET onnxruntime PRE_BUILD COMMAND ${CMAKE_COMMAND} -E rm -rf ${STATIC_FRAMEWORK_DIR})
add_custom_command(TARGET onnxruntime PRE_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory ${STATIC_FRAMEWORK_DIR})

# replicate XCode's Single Object Pre-Link
# link the internal onnxruntime .o files with the external .a files into a single relocatable object
# to enforce symbol visibility. doing it this way limits the symbols included from the .a files to symbols used
# by the ORT .o files.

# Go through all the static libraries, and create symbolic links
foreach(_LIB ${onnxruntime_INTERNAL_LIBRARIES} ${onnxruntime_EXTERNAL_LIBRARIES})
# If it's an onnxruntime library, extract .o files to a separate directory for each library to avoid any clashes
# with filenames (e.g. utils.o)
foreach(_LIB ${onnxruntime_INTERNAL_LIBRARIES} )
GET_TARGET_PROPERTY(_LIB_TYPE ${_LIB} TYPE)
if(_LIB_TYPE STREQUAL "STATIC_LIBRARY")
add_custom_command(TARGET onnxruntime POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:${_LIB}> ${STATIC_LIB_DIR}/$<TARGET_LINKER_FILE_NAME:${_LIB}>)
set(CUR_STATIC_LIB_OBJ_DIR ${STATIC_LIB_TEMP_DIR}/$<TARGET_LINKER_FILE_BASE_NAME:${_LIB}>)
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CUR_STATIC_LIB_OBJ_DIR})

add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ar ARGS -x $<TARGET_FILE:${_LIB}>
WORKING_DIRECTORY ${CUR_STATIC_LIB_OBJ_DIR})
endif()
endforeach()

if(${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
set(STATIC_FRAMEWORK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}-${CMAKE_OSX_SYSROOT})
else() # macOS
set(STATIC_FRAMEWORK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif()
# for external libraries we create a symlink to the .a file
foreach(_LIB ${onnxruntime_EXTERNAL_LIBRARIES})
GET_TARGET_PROPERTY(_LIB_TYPE ${_LIB} TYPE)
if(_LIB_TYPE STREQUAL "STATIC_LIBRARY")
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
$<TARGET_FILE:${_LIB}> ${STATIC_LIB_DIR}/$<TARGET_LINKER_FILE_NAME:${_LIB}>)
endif()
endforeach()

# Assemble the static framework
set(STATIC_FRAMEWORK_DIR ${STATIC_FRAMEWORK_OUTPUT_DIR}/static_framework/onnxruntime.framework)
set(STATIC_FRAMEWORK_HEADER_DIR ${STATIC_FRAMEWORK_DIR}/Headers)
file(MAKE_DIRECTORY ${STATIC_FRAMEWORK_DIR})
# Remove all files under STATIC_FRAMEWORK_DIR (if any)
file(GLOB_RECURSE _OLD_STATIC_FRAMEWORK ${STATIC_FRAMEWORK_DIR}/*.*)
file(REMOVE "${_OLD_STATIC_FRAMEWORK}")
# do the pre-link with `ld -r` to create a single relocatable object with correct symbol visibility
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ld ARGS -r -o ${STATIC_LIB_DIR}/prelinked_objects.o */*.o ../*.a
WORKING_DIRECTORY ${STATIC_LIB_TEMP_DIR})

# create the static library
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND libtool -static -o ${STATIC_FRAMEWORK_DIR}/onnxruntime prelinked_objects.o
WORKING_DIRECTORY ${STATIC_LIB_DIR})

# Assemble the other pieces of the static framework
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy_if_different ${INFO_PLIST_PATH} ${STATIC_FRAMEWORK_DIR}/Info.plist)

# add the framework header files
set(STATIC_FRAMEWORK_HEADER_DIR ${STATIC_FRAMEWORK_DIR}/Headers)
file(MAKE_DIRECTORY ${STATIC_FRAMEWORK_HEADER_DIR})

# copy the header files one by one, and the Info.plist
foreach(h_ ${ONNXRUNTIME_PUBLIC_HEADERS})
get_filename_component(HEADER_NAME_ ${h_} NAME)
add_custom_command(TARGET onnxruntime POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${h_} ${STATIC_FRAMEWORK_HEADER_DIR}/${HEADER_NAME_})
add_custom_command(TARGET onnxruntime POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy_if_different ${h_} ${STATIC_FRAMEWORK_HEADER_DIR}/${HEADER_NAME_})
endforeach()
add_custom_command(TARGET onnxruntime POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INFO_PLIST_PATH} ${STATIC_FRAMEWORK_DIR}/Info.plist)

# link the static library
add_custom_command(TARGET onnxruntime POST_BUILD COMMAND libtool -static -o ${STATIC_FRAMEWORK_DIR}/onnxruntime *.a WORKING_DIRECTORY ${STATIC_LIB_DIR})
endif()
2 changes: 2 additions & 0 deletions cmake/onnxruntime_graph.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ if (onnxruntime_MINIMAL_BUILD)
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/onnx_deprecated_operators.cc"
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/onnx_function_util.h"
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/onnx_function_util.cc"
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/shape_inference_functions.h"
"${ONNXRUNTIME_ROOT}/core/graph/contrib_ops/shape_inference_functions.cc"
"${ONNXRUNTIME_ROOT}/core/graph/function_template.h"
"${ONNXRUNTIME_ROOT}/core/graph/function_utils.h"
"${ONNXRUNTIME_ROOT}/core/graph/function_utils.cc"
Expand Down
10 changes: 10 additions & 0 deletions onnxruntime/core/providers/cpu/generator/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,14 @@ template Status MultinomialComputeShared<int64_t>(AllocatorPtr& alloc,
std::default_random_engine& generator,
Tensor& Y);

#if !defined(DISABLE_CONTRIB_OPS)
// used by onnxruntime/contrib_ops/cpu/transformers/sampling_cpu_helper.h
template Status MultinomialComputeShared<int32_t>(AllocatorPtr& alloc,
const Tensor& X,
const int64_t batch_size,
const int64_t num_classes,
const int64_t num_samples,
std::default_random_engine& generator,
Tensor& Y);
#endif
} // namespace onnxruntime

0 comments on commit b50fc09

Please sign in to comment.