Skip to content

Commit

Permalink
CMake exports for static onnxruntime
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanozang committed Sep 24, 2024
1 parent 209ff86 commit 4a274e0
Show file tree
Hide file tree
Showing 22 changed files with 215 additions and 167 deletions.
83 changes: 53 additions & 30 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ option(onnxruntime_BUILD_UNIT_TESTS "Build ONNXRuntime unit tests" ON)
option(onnxruntime_BUILD_CSHARP "Build C# library" OFF)
option(onnxruntime_BUILD_OBJC "Build Objective-C library" OFF)
option(onnxruntime_USE_PREINSTALLED_EIGEN "Use pre-installed EIGEN. Need to provide eigen_SOURCE_PATH if turn this on." OFF)
option(onnxruntime_USE_EIGEN_PACKAGE "Use Eigen obtained via find_package. Note that onnxruntime is incompatible with the current major Eigen release. See the eigen commit in cmake/deps.txt." OFF)
option(onnxruntime_BUILD_BENCHMARKS "Build ONNXRuntime micro-benchmarks" OFF)
option(onnxruntime_USE_LLVM "Build TVM with LLVM" OFF)
option(onnxruntime_USE_VSINPU "Build with VSINPU support" OFF)
Expand Down Expand Up @@ -1683,12 +1684,10 @@ if (onnxruntime_USE_WINML)
list(APPEND ONNXRUNTIME_CMAKE_FILES winml)
endif() # if (onnxruntime_USE_WINML)

if (onnxruntime_BUILD_SHARED_LIB OR onnxruntime_BUILD_APPLE_FRAMEWORK)
if (onnxruntime_BUILD_APPLE_FRAMEWORK AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS")
message(FATAL_ERROR "onnxruntime_BUILD_APPLE_FRAMEWORK can only be enabled for macOS or iOS or visionOS.")
endif()
list(APPEND ONNXRUNTIME_CMAKE_FILES onnxruntime)
if (onnxruntime_BUILD_APPLE_FRAMEWORK AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS")
message(FATAL_ERROR "onnxruntime_BUILD_APPLE_FRAMEWORK can only be enabled for macOS or iOS or visionOS.")
endif()
list(APPEND ONNXRUNTIME_CMAKE_FILES onnxruntime)

if (onnxruntime_BUILD_JAVA)
message(STATUS "Java Build is enabled")
Expand Down Expand Up @@ -1803,33 +1802,57 @@ if (onnxruntime_ENABLE_EXTERNAL_CUSTOM_OP_SCHEMAS)
)
endif()

if(TARGET onnxruntime)
# Install
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
set(PROJECT_CONFIG_CONTENT "@PACKAGE_INIT@\n")
string(APPEND PROJECT_CONFIG_CONTENT
"include(\"\${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}Targets.cmake\")")
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/PROJECT_CONFIG_FILE" ${PROJECT_CONFIG_CONTENT})
install(EXPORT ${PROJECT_NAME}Targets
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
# Create config for find_package()
configure_package_config_file(
"${CMAKE_CURRENT_BINARY_DIR}/PROJECT_CONFIG_FILE" ${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${ORT_VERSION}
COMPATIBILITY SameMajorVersion)
include(CMakePackageConfigHelpers)

set(PROJECT_CONFIG_CONTENT "@PACKAGE_INIT@\n")

if (NOT onnxruntime_BUILD_SHARED_LIB)
string(APPEND PROJECT_CONFIG_CONTENT
"include(CMakeFindDependencyMacro)\n\
find_dependency(absl)\n\
find_dependency(date)\n\
find_dependency(Eigen3)\n\
find_dependency(nlohmann_json)\n\
find_dependency(ONNX)\n\
find_dependency(re2)\n\
find_dependency(flatbuffers)\n\
find_dependency(unofficial-nsync)\n\
find_dependency(cpuinfo)\n\
find_dependency(protobuf)\n\
find_dependency(Boost COMPONENTS mp11)\n\
find_dependency(Microsoft.GSL 4.0)\n\
find_dependency(Iconv)\n\
find_path(safeint_SOURCE_DIR NAMES \"SafeInt.hpp\" REQUIRED)\n\
add_library(safeint_interface IMPORTED INTERFACE)\n\
target_include_directories(safeint_interface INTERFACE ${safeint_SOURCE_DIR})\n\
")
endif()

string(APPEND PROJECT_CONFIG_CONTENT
"include(\"\${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}Targets.cmake\")\n")


file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/PROJECT_CONFIG_FILE" ${PROJECT_CONFIG_CONTENT})
install(EXPORT ${PROJECT_NAME}Targets
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

install(
FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
endif()
# Create config for find_package()
configure_package_config_file(
"${CMAKE_CURRENT_BINARY_DIR}/PROJECT_CONFIG_FILE" ${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${ORT_VERSION}
COMPATIBILITY SameMajorVersion)

install(
FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

if(DEFINED BUILD_AS_ARM64X)
set(ARM64X_TARGETS onnxruntime)
Expand Down
2 changes: 1 addition & 1 deletion cmake/deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ flatbuffers;https://github.com/google/flatbuffers/archive/refs/tags/v23.5.26.zip
fp16;https://github.com/Maratyszcza/FP16/archive/0a92994d729ff76a58f692d3028ca1b64b145d91.zip;b985f6985a05a1c03ff1bb71190f66d8f98a1494
fxdiv;https://github.com/Maratyszcza/FXdiv/archive/63058eff77e11aa15bf531df5dd34395ec3017c8.zip;a5658f4036402dbca7cebee32be57fb8149811e1
google_benchmark;https://github.com/google/benchmark/archive/refs/tags/v1.8.5.zip;cd47d3d272faf353600c8cc2fdec2b52d6f69177
google_nsync;https://github.com/google/nsync/archive/refs/tags/1.26.0.zip;5e7c00ef6bf5b787386fc040067903ec774e2752
google_nsync;https://github.com/google/nsync/archive/refs/tags/1.29.0.zip;acc46ec0bc736da1cf581c9bb2700f9cea210b6c
googletest;https://github.com/google/googletest/archive/refs/tags/v1.15.0.zip;9d2d0af8d77ac726ea55d44a8fa727ec98311349
#xnnpack 2024.09.04
googlexnnpack;https://github.com/google/XNNPACK/archive/309b75c9e56e0a674bf78d59872ce131f814dfb6.zip;39FA5259EAEACE0547284B63D5CEDC4F05553F5A
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/eigen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ else ()
)
endif()

FetchContent_Populate(eigen)
onnxruntime_fetchcontent_makeavailable(eigen)
set(eigen_INCLUDE_DIRS "${eigen_SOURCE_DIR}")
endif()
39 changes: 25 additions & 14 deletions cmake/external/onnxruntime_external_deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ if (NOT WIN32)
add_library(nsync::nsync_cpp ALIAS nsync_cpp)
target_include_directories(nsync_cpp PUBLIC ${google_nsync_SOURCE_DIR}/public)
endif()
if(TARGET unofficial::nsync::nsync_cpp AND NOT TARGET nsync::nsync_cpp)

if (NOT TARGET nsync::nsync_cpp)
if(TARGET unofficial::nsync::nsync_cpp)
message(STATUS "Aliasing unofficial::nsync::nsync_cpp to nsync::nsync_cpp")
add_library(nsync::nsync_cpp ALIAS unofficial::nsync::nsync_cpp)
elseif(TARGET nsync_cpp)
message(STATUS "Aliasing nsync_cpp to nsync::nsync_cpp")
add_library(nsync::nsync_cpp ALIAS nsync_cpp)
else()
message(FATAL_ERROR "Failed to import needed target nsync::nsync_cpp")
endif()
endif()
endif()

Expand Down Expand Up @@ -217,7 +225,7 @@ FetchContent_Declare(
URL ${DEP_URL_protobuf}
URL_HASH SHA1=${DEP_SHA1_protobuf}
PATCH_COMMAND ${ONNXRUNTIME_PROTOBUF_PATCH_COMMAND}
FIND_PACKAGE_ARGS NAMES Protobuf protobuf
FIND_PACKAGE_ARGS 3.21 NAMES Protobuf protobuf
)

set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests" FORCE)
Expand Down Expand Up @@ -295,19 +303,19 @@ FetchContent_Declare(
)
onnxruntime_fetchcontent_makeavailable(date)

find_package(Boost COMPONENTS mp11)

if (NOT TARGET Boost::mp11)
FetchContent_Declare(
mp11
URL ${DEP_URL_mp11}
URL_HASH SHA1=${DEP_SHA1_mp11}
FIND_PACKAGE_ARGS NAMES Boost
)
onnxruntime_fetchcontent_makeavailable(mp11)
if(NOT TARGET Boost::mp11)
if(onnxruntime_USE_VCPKG)
find_package(Boost REQUIRED)
endif()
message(STATUS "Aliasing Boost::headers to Boost::mp11")
add_library(Boost::mp11 ALIAS Boost::headers)

FetchContent_Populate(mp11)

add_library(Boost::mp11 IMPORTED INTERFACE)
target_include_directories(Boost::mp11 INTERFACE ${mp11_SOURCE_DIR}/include)
endif()

set(JSON_BuildTests OFF CACHE INTERNAL "")
Expand Down Expand Up @@ -453,7 +461,7 @@ if(NOT safeint_SOURCE_DIR)
# use fetch content rather than makeavailable because safeint only includes unconditional test targets
FetchContent_Populate(safeint)
endif()
add_library(safeint_interface INTERFACE)
add_library(safeint_interface IMPORTED INTERFACE)
target_include_directories(safeint_interface INTERFACE ${safeint_SOURCE_DIR})


Expand All @@ -463,7 +471,7 @@ if (CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "Android" OR
set(FLATBUFFERS_BUILD_FLATC OFF CACHE BOOL "FLATBUFFERS_BUILD_FLATC" FORCE)
endif()
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "FLATBUFFERS_BUILD_TESTS" FORCE)
set(FLATBUFFERS_INSTALL OFF CACHE BOOL "FLATBUFFERS_INSTALL" FORCE)
set(FLATBUFFERS_INSTALL ON CACHE BOOL "FLATBUFFERS_INSTALL" FORCE)
set(FLATBUFFERS_BUILD_FLATHASH OFF CACHE BOOL "FLATBUFFERS_BUILD_FLATHASH" FORCE)
set(FLATBUFFERS_BUILD_FLATLIB ON CACHE BOOL "FLATBUFFERS_BUILD_FLATLIB" FORCE)
if(Patch_FOUND)
Expand Down Expand Up @@ -527,7 +535,7 @@ FetchContent_Declare(
URL_HASH SHA1=${DEP_SHA1_onnx}
PATCH_COMMAND ${ONNXRUNTIME_ONNX_PATCH_COMMAND}
FIND_PACKAGE_ARGS NAMES ONNX onnx
)
)
if (NOT onnxruntime_MINIMAL_BUILD)
onnxruntime_fetchcontent_makeavailable(onnx)
else()
Expand All @@ -543,7 +551,10 @@ if(TARGET ONNX::onnx_proto AND NOT TARGET onnx_proto)
add_library(onnx_proto ALIAS ONNX::onnx_proto)
endif()

if(onnxruntime_USE_EIGEN_PACKAGE)
find_package(Eigen3 CONFIG)
endif()

if(Eigen3_FOUND)
get_target_property(eigen_INCLUDE_DIRS Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES)
else()
Expand Down Expand Up @@ -579,7 +590,7 @@ endif()
#onnxruntime_EXTERNAL_LIBRARIES could contain onnx, onnx_proto,libprotobuf, cuda/cudnn,
# dnnl/mklml, onnxruntime_codegen_tvm, tvm and pthread
# pthread is always at the last
set(onnxruntime_EXTERNAL_LIBRARIES ${onnxruntime_EXTERNAL_LIBRARIES_XNNPACK} ${WIL_TARGET} nlohmann_json::nlohmann_json onnx onnx_proto ${PROTOBUF_LIB} re2::re2 Boost::mp11 safeint_interface flatbuffers::flatbuffers ${GSL_TARGET} ${ABSEIL_LIBS} date::date ${ONNXRUNTIME_CLOG_TARGET_NAME})
set(onnxruntime_EXTERNAL_LIBRARIES ${onnxruntime_EXTERNAL_LIBRARIES_XNNPACK} ${WIL_TARGET} nlohmann_json::nlohmann_json onnx onnx_proto ${PROTOBUF_LIB} re2::re2 Boost::mp11 safeint_interface flatbuffers::flatbuffers ${GSL_TARGET} ${ABSEIL_LIBS} date::date ${ONNXRUNTIME_CLOG_TARGET_NAME} nsync::nsync_cpp)
# The source code of onnx_proto is generated, we must build this lib first before starting to compile the other source code that uses ONNX protobuf types.
# The other libs do not have the problem. All the sources are already there. We can compile them in any order.
set(onnxruntime_EXTERNAL_DEPENDENCIES onnx_proto flatbuffers::flatbuffers)
Expand Down
Loading

0 comments on commit 4a274e0

Please sign in to comment.