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