diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index e7a8fc0335..5a0cfd7fce 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -78,6 +78,7 @@ option(onnxruntime_USE_AVX512 "Use AVX512 instructions" OFF) option(onnxruntime_USE_OPENMP "Build with OpenMP support" OFF) option(onnxruntime_BUILD_SHARED_LIB "Build a shared library" OFF) +option(onnxruntime_BUILD_APPLE_FRAMEWORK "Build a macOS/iOS framework" OFF) option(onnxruntime_ENABLE_MICROSOFT_INTERNAL "Use this option to enable/disable microsoft internal only code" OFF) option(onnxruntime_USE_NUPHAR "Build with Nuphar" OFF) option(onnxruntime_USE_VITISAI "Build with Vitis-AI" OFF) @@ -1155,7 +1156,7 @@ if(onnxruntime_USE_OPENVINO) add_definitions(-DOPENVINO_2021_2=1) elseif (${VER} MATCHES "2021.3" OR $ENV{INTEL_OPENVINO_DIR} MATCHES "2021.3") set(OPENVINO_VERSION "2021.3") - add_definitions(-DOPENVINO_2021_3=1) + add_definitions(-DOPENVINO_2021_3=1) else() message(FATAL_ERROR "Unsupported OpenVINO version: ${INTEL_OPENVINO_DIR}") endif() @@ -1542,7 +1543,10 @@ endif() # if(onnxruntime_USE_WINML) #The following files may use the 'onnxruntime_libs' and 'onnxruntime_EXTERNAL_LIBRARIES' vars -if (onnxruntime_BUILD_SHARED_LIB) +if (onnxruntime_BUILD_SHARED_LIB OR onnxruntime_BUILD_APPLE_FRAMEWORK) + if (onnxruntime_BUILD_APPLE_FRAMEWORK AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS") + message(FATAL_ERROR "onnxruntime_BUILD_APPLE_FRAMEWORK can only be enabled for macOS or iOS.") + endif() include(onnxruntime.cmake) endif() diff --git a/cmake/Info.plist.in b/cmake/Info.plist.in new file mode 100644 index 0000000000..f84bd250c8 --- /dev/null +++ b/cmake/Info.plist.in @@ -0,0 +1,20 @@ + + + + + CFBundleExecutable + ${MACOSX_FRAMEWORK_NAME} + CFBundleName + ${MACOSX_FRAMEWORK_NAME} + CFBundleIdentifier + ${MACOSX_FRAMEWORK_IDENTIFIER} + CFBundleVersion + ${ORT_VERSION} + CFBundleShortVersionString + ${ORT_VERSION} + CFBundleSignature + ???? + CFBundlePackageType + FMWK + + diff --git a/cmake/onnxruntime.cmake b/cmake/onnxruntime.cmake index 15a31de6a8..e77af251bc 100644 --- a/cmake/onnxruntime.cmake +++ b/cmake/onnxruntime.cmake @@ -33,13 +33,48 @@ add_custom_command(OUTPUT ${SYMBOL_FILE} ${CMAKE_CURRENT_BINARY_DIR}/generated_s add_custom_target(onnxruntime_generate_def ALL DEPENDS ${SYMBOL_FILE} ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c) if(WIN32) - onnxruntime_add_shared_library(onnxruntime - ${SYMBOL_FILE} - "${ONNXRUNTIME_ROOT}/core/dll/dllmain.cc" - "${ONNXRUNTIME_ROOT}/core/dll/onnxruntime.rc" + onnxruntime_add_shared_library(onnxruntime + ${SYMBOL_FILE} + "${ONNXRUNTIME_ROOT}/core/dll/dllmain.cc" + "${ONNXRUNTIME_ROOT}/core/dll/onnxruntime.rc" + ) +elseif(onnxruntime_BUILD_APPLE_FRAMEWORK) + # include both c and cxx api + set(APPLE_FRAMEWORK_HEADERS + "${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_c_api.h" + "${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_cxx_api.h" + "${REPO_ROOT}/include/onnxruntime/core/session/onnxruntime_cxx_inline.h" + ) + + # need to add header files for enabled EPs + foreach(f ${ONNXRUNTIME_PROVIDER_NAMES}) + file(GLOB _provider_headers CONFIGURE_DEPENDS + "${REPO_ROOT}/include/onnxruntime/core/providers/${f}/*.h" ) + list(APPEND APPLE_FRAMEWORK_HEADERS "${_provider_headers}") + unset(_provider_headers) + endforeach() + + # apple framework requires the header file be part of the library + onnxruntime_add_shared_library(onnxruntime + ${APPLE_FRAMEWORK_HEADERS} + "${CMAKE_CURRENT_BINARY_DIR}/generated_source.c" + ) + + set(MACOSX_FRAMEWORK_NAME "onnxruntime") + set(MACOSX_FRAMEWORK_IDENTIFIER "com.microsoft.onnxruntime") + configure_file(${REPO_ROOT}/cmake/Info.plist.in ${CMAKE_CURRENT_BINARY_DIR}/Info.plist) + + set_target_properties(onnxruntime PROPERTIES + FRAMEWORK TRUE + FRAMEWORK_VERSION A + PUBLIC_HEADER "${APPLE_FRAMEWORK_HEADERS}" + MACOSX_FRAMEWORK_INFO_PLIST ${CMAKE_CURRENT_BINARY_DIR}/Info.plist + VERSION ${ORT_VERSION} + SOVERSION ${ORT_VERSION} + ) else() - onnxruntime_add_shared_library(onnxruntime ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c) + onnxruntime_add_shared_library(onnxruntime ${CMAKE_CURRENT_BINARY_DIR}/generated_source.c) endif() add_dependencies(onnxruntime onnxruntime_generate_def ${onnxruntime_EXTERNAL_DEPENDENCIES}) @@ -133,9 +168,10 @@ set_target_properties(onnxruntime PROPERTIES LINK_DEPENDS ${SYMBOL_FILE}) set_target_properties(onnxruntime PROPERTIES VERSION ${ORT_VERSION}) install(TARGETS onnxruntime - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + FRAMEWORK DESTINATION ${CMAKE_INSTALL_BINDIR}) set_target_properties(onnxruntime PROPERTIES FOLDER "ONNXRuntime") diff --git a/cmake/onnxruntime_ios.toolchain.cmake b/cmake/onnxruntime_ios.toolchain.cmake index 81582cf072..750e4118ca 100644 --- a/cmake/onnxruntime_ios.toolchain.cmake +++ b/cmake/onnxruntime_ios.toolchain.cmake @@ -2,7 +2,7 @@ # Licensed under the MIT License. set(CMAKE_SYSTEM_NAME iOS) -if (NOT DEFINED CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM) +if (NOT DEFINED CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM AND NOT DEFINED CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY) set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED NO) endif() diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index 3a289a3a07..fc85c40885 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -97,7 +97,8 @@ function(AddTest) MACOSX_BUNDLE_BUNDLE_VERSION ${ORT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${ORT_VERSION} XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "YES" - XCODE_ATTRIBUTE_ENABLE_BITCODE "NO") + XCODE_ATTRIBUTE_ENABLE_BITCODE "NO" + XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO") xctest_add_bundle(${_UT_TARGET}_xc ${_UT_TARGET} ${TEST_SRC_DIR}/xctest/ortxctest.m @@ -319,7 +320,7 @@ set (onnxruntime_shared_lib_test_SRC ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_model_loading.cc ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/test_ort_format_models.cc ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/utils.h - ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/utils.cc + ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/utils.cc ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/custom_op_utils.h ${ONNXRUNTIME_SHARED_LIB_TEST_SRC_DIR}/custom_op_utils.cc) @@ -745,6 +746,12 @@ if(MSVC) target_compile_options(onnx_test_runner PRIVATE "$<$:SHELL:--compiler-options /utf-8>" "$<$>:/utf-8>") endif() +if(${CMAKE_SYSTEM_NAME} STREQUAL "iOS") + set_target_properties(onnx_test_runner PROPERTIES + XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO" + ) +endif() + target_link_libraries(onnx_test_runner PRIVATE onnx_test_runner_common ${GETOPT_LIB_WIDE} ${onnx_test_libs}) target_include_directories(onnx_test_runner PRIVATE ${ONNXRUNTIME_ROOT}) set_target_properties(onnx_test_runner PROPERTIES FOLDER "ONNXRuntimeTest") @@ -845,6 +852,11 @@ if (WIN32) set(SYS_PATH_LIB shlwapi) endif() endif() +if(${CMAKE_SYSTEM_NAME} STREQUAL "iOS") + set_target_properties(onnxruntime_perf_test PROPERTIES + XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO" + ) +endif() if (onnxruntime_BUILD_SHARED_LIB) set(onnxruntime_perf_test_libs @@ -978,6 +990,11 @@ if(MSVC) target_compile_options(onnxruntime_mlas_test PRIVATE "$<$:SHELL:--compiler-options /utf-8>" "$<$>:/utf-8>") endif() +if(${CMAKE_SYSTEM_NAME} STREQUAL "iOS") + set_target_properties(onnxruntime_mlas_test PROPERTIES + XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO" + ) +endif() target_include_directories(onnxruntime_mlas_test PRIVATE ${ONNXRUNTIME_ROOT}/core/mlas/inc ${ONNXRUNTIME_ROOT} ${CMAKE_CURRENT_BINARY_DIR}) set(onnxruntime_mlas_test_libs GTest::gtest GTest::gmock onnxruntime_mlas onnxruntime_common) diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index ad72044afd..5e44775cbe 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -248,6 +248,11 @@ def parse_arguments(): "--build_shared_lib", action='store_true', help="Build a shared library for the ONNXRuntime.") + # Build a shared lib + parser.add_argument( + "--build_apple_framework", action='store_true', + help="Build a macOS/iOS framework for the ONNXRuntime.") + # Build options parser.add_argument( "--cmake_extra_defines", nargs="+", @@ -305,6 +310,9 @@ def parse_arguments(): parser.add_argument( "--xcode_code_signing_team_id", default="", help="The development team ID used for code signing in Xcode") + parser.add_argument( + "--xcode_code_signing_identity", default="", + help="The development identity used for code signing in Xcode") parser.add_argument( "--use_xcode", action='store_true', help="Use Xcode as cmake generator, this is only supported on MacOS.") @@ -643,6 +651,7 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home "-Donnxruntime_BUILD_JAVA=" + ("ON" if args.build_java else "OFF"), "-Donnxruntime_BUILD_NODEJS=" + ("ON" if args.build_nodejs else "OFF"), "-Donnxruntime_BUILD_SHARED_LIB=" + ("ON" if args.build_shared_lib else "OFF"), + "-Donnxruntime_BUILD_APPLE_FRAMEWORK=" + ("ON" if args.build_apple_framework else "OFF"), "-Donnxruntime_USE_DNNL=" + ("ON" if args.use_dnnl else "OFF"), "-Donnxruntime_DNNL_GPU_RUNTIME=" + (args.dnnl_gpu_runtime if args.use_dnnl else ""), "-Donnxruntime_DNNL_OPENCL_ROOT=" + (args.dnnl_opencl_root if args.use_dnnl else ""), @@ -805,6 +814,11 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home cmake_args += ["-T", "buildsystem=1"] if args.apple_deploy_target: cmake_args += ["-DCMAKE_OSX_DEPLOYMENT_TARGET=" + args.apple_deploy_target] + # Code sign the binaries, if the code signing development identity and/or team id are provided + if args.xcode_code_signing_identity: + cmake_args += ["-DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY=" + args.xcode_code_signing_identity] + if args.xcode_code_signing_team_id: + cmake_args += ["-DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=" + args.xcode_code_signing_team_id] if args.use_coreml: if not is_macOS(): @@ -843,9 +857,6 @@ def generate_build_tree(cmake_path, source_dir, build_dir, cuda_home, cudnn_home args.ios_toolchain_file if args.ios_toolchain_file else "../cmake/onnxruntime_ios.toolchain.cmake") ] - # Code sign the binaries, if the code signing development team id is provided - if args.xcode_code_signing_team_id: - cmake_args += ["-DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM=" + args.xcode_code_signing_team_id] else: # TODO: the cross compiling on Linux is not officially supported by Apple # and is already broken with the latest codebase, so it should be removed.