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