microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
nobf

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cmake/ext_apple_framework.cmake

100lines · modeblame

9ab7e614Edward Chen3 years ago1if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Darwin|iOS")
2message(FATAL_ERROR "Building an Apple framework can only be enabled for MacOS or iOS.")
3endif()
4
5# build a static framework
6
7get_target_property(_ortcustomops_type ortcustomops TYPE)
8if(NOT _ortcustomops_type STREQUAL "STATIC_LIBRARY")
9message(FATAL_ERROR "ortcustomops must be a static library in order to build the Apple static framework.")
10endif()
11
12set(APPLE_FRAMEWORK_NAME "onnxruntime_extensions")
13set(APPLE_FRAMEWORK_IDENTIFIER "com.microsoft.onnxruntime_extensions")
14set(APPLE_FRAMEWORK_VERSION "${VERSION}")
15
16# public header files
17set(APPLE_FRAMEWORK_HEADERS
f9290e8bWenbing Li2 years ago18"${PROJECT_SOURCE_DIR}/include/onnxruntime_extensions.h")
9ab7e614Edward Chen3 years ago19
20# generated framework directory
21set(APPLE_FRAMEWORK_DIRECTORY
22"${PROJECT_BINARY_DIR}/static_framework/${APPLE_FRAMEWORK_NAME}.framework")
23
24# generate Info.plist and framework_info.json
25configure_file(${PROJECT_SOURCE_DIR}/cmake/apple/Info.plist.in ${PROJECT_BINARY_DIR}/Info.plist)
26configure_file(${PROJECT_SOURCE_DIR}/cmake/apple/framework_info.json.in ${PROJECT_BINARY_DIR}/framework_info.json)
27
28# gets transitive static library dependencies of targets in ${target_names}
29# ${target_names} - root target names
30# ${transitive_static_lib_deps_var} - output variable name
31function(get_transitive_static_lib_deps target_names transitive_static_lib_deps_var)
32set(targets_to_process ${target_names})
33set(processed_targets)
34
35while(true)
36# exit loop when ${targets_to_process} is empty
37list(LENGTH targets_to_process targets_to_process_len)
38if(${targets_to_process_len} EQUAL 0)
39break()
40endif()
41
42list(GET targets_to_process 0 target_to_process)
43list(POP_FRONT targets_to_process)
44
45if(${target_to_process} IN_LIST processed_targets)
46continue()
47endif()
48
49message(DEBUG "processing target: ${target_to_process}")
50
51get_target_property(target_to_process_deps ${target_to_process} LINK_LIBRARIES)
52if(target_to_process_deps)
53foreach(dep IN LISTS target_to_process_deps)
54if(NOT TARGET ${dep})
55message(DEBUG "not a target")
56continue()
57endif()
58
59get_target_property(dep_type ${dep} TYPE)
60if(NOT "${dep_type}" STREQUAL "STATIC_LIBRARY")
61message(DEBUG "not a static library")
62continue()
63endif()
64
65message(DEBUG "adding dep to process: ${dep}")
66list(APPEND targets_to_process ${dep})
67endforeach()
68endif()
69
70list(APPEND processed_targets ${target_to_process})
71endwhile()
72
73# output ${processed_targets} without the initial values in ${target_names}
74list(LENGTH target_names target_names_len)
75list(SUBLIST processed_targets ${target_names_len} -1 transitive_static_lib_deps)
76set(${transitive_static_lib_deps_var} ${transitive_static_lib_deps} PARENT_SCOPE)
77endfunction()
78
79# get list of static library dependencies to combine
80get_transitive_static_lib_deps(ortcustomops _ortcustomops_static_lib_dep_targets)
81set(_ortcustomops_all_static_lib_dep_targets
82ortcustomops ${_ortcustomops_static_lib_dep_targets})
83list(TRANSFORM _ortcustomops_all_static_lib_dep_targets
84REPLACE "(.+)" "$<TARGET_FILE:\\1>"
85OUTPUT_VARIABLE _ortcustomops_all_static_lib_deps)
86
87add_custom_command(
88TARGET ortcustomops
89POST_BUILD
90COMMAND # remove any existing framework files
91${CMAKE_COMMAND} -E rm -rf -- ${APPLE_FRAMEWORK_DIRECTORY}
92COMMAND # create output directories
93${CMAKE_COMMAND} -E make_directory ${APPLE_FRAMEWORK_DIRECTORY}/Headers
94COMMAND # copy header files
95${CMAKE_COMMAND} -E copy_if_different ${APPLE_FRAMEWORK_HEADERS} ${APPLE_FRAMEWORK_DIRECTORY}/Headers
96COMMAND # copy Info.plist
97${CMAKE_COMMAND} -E copy_if_different ${PROJECT_BINARY_DIR}/Info.plist ${APPLE_FRAMEWORK_DIRECTORY}/Info.plist
98COMMAND # combine static libraries
99libtool -static -o ${APPLE_FRAMEWORK_DIRECTORY}/onnxruntime_extensions ${_ortcustomops_all_static_lib_deps}
100)