Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake exports for static onnxruntime #22173

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 52 additions & 30 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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 @@ -1708,12 +1709,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 @@ -1828,33 +1827,56 @@ 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(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
14 changes: 10 additions & 4 deletions cmake/external/abseil-cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ set(BUILD_TESTING 0)
set(ABSL_BUILD_TESTING OFF)
set(ABSL_BUILD_TEST_HELPERS OFF)
set(ABSL_USE_EXTERNAL_GOOGLETEST ON)

# Both abseil and xnnpack create a target called memory, which
# results in a duplicate target if ABSL_ENABLE_INSTALL is on.
if (onnxruntime_USE_XNNPACK)
set(ABSL_ENABLE_INSTALL OFF)
else()
set(ABSL_ENABLE_INSTALL ON)
endif()

if(Patch_FOUND AND WIN32)
set(ABSL_PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/abseil/absl_windows.patch)
else()
set(ABSL_PATCH_COMMAND "")
endif()
if(WIN32 AND NOT Patch_FOUND)
#see https://github.com/google/re2/issues/425 and https://github.com/google/re2/issues/436
set(ABSL_ENABLE_INSTALL ON)
endif()

# NB! Advancing Abseil version changes its internal namespace,
# currently absl::lts_20240116 which affects abseil-cpp.natvis debugger
# visualization file, that must be adjusted accordingly, unless we eliminate
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()
31 changes: 18 additions & 13 deletions cmake/external/onnxruntime_external_deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ endif()
# for cross-compiling
#2. if ONNX_CUSTOM_PROTOC_EXECUTABLE is not set, Compile everything(including protoc) from source code.
if(Patch_FOUND)
set(ONNXRUNTIME_PROTOBUF_PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/protobuf/protobuf_cmake.patch)
set(ONNXRUNTIME_PROTOBUF_PATCH_COMMAND ${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/protobuf/protobuf_cmake.patch &&
${Patch_EXECUTABLE} --binary --ignore-whitespace -p1 < ${PROJECT_SOURCE_DIR}/patches/protobuf/protobuf_android_log.patch)
else()
set(ONNXRUNTIME_PROTOBUF_PATCH_COMMAND "")
endif()
Expand Down Expand Up @@ -274,23 +275,23 @@ 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 "")
set(JSON_Install OFF CACHE INTERNAL "")
set(JSON_Install ON CACHE INTERNAL "")

FetchContent_Declare(
nlohmann_json
Expand Down Expand Up @@ -400,6 +401,7 @@ if ((CPUINFO_SUPPORTED OR onnxruntime_USE_XNNPACK) AND NOT ANDROID)
endif()
endif()

set(GSL_INSTALL ON)
if(onnxruntime_USE_CUDA)
FetchContent_Declare(
GSL
Expand Down Expand Up @@ -432,7 +434,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 @@ -442,7 +444,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 @@ -506,7 +508,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 @@ -522,7 +524,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
Loading
Loading