-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Can you guys provide a clear CMakeLists.txt example on how to include onnxruntime for a C++ project? #3124
Comments
onnxruntime doesn't support CMake find_package yet. |
I second this request. |
This issue has been automatically marked as stale due to inactivity and will be closed in 7 days if no further activity occurs. If further support is needed, please provide an update and/or more details. |
This issue has been automatically closed due to inactivity. Please reactivate if further support is needed. |
Hi all, A) The hacky way via linux environment variables B) Create libraryConfig.cmake to enable If the maintainers are interested, I can create a PR with the Cmake configuration. For the time being, I'm pasting
|
@snnn A script like onnxruntimeConfig.cmake is today the StateOftheArt practive for any library to cleanly integrate with any other project using cmake. I would be ok to prepare a PR to integrate it in the onnxrt master unless @jcarius would prefer to. |
@WilliamTambellini thank you for picking this up and please go ahead, I don't have the time at the moment to make a clean PR. Apart from the
|
- Address review comment opentrack#1306 (comment) - Update ONNXRuntime module to use a specific `onnxruntime` target. - Use OpenCV components instead. NOTE: ONNXRuntinme may provide their own ONNXRuntimeConfig.cmake and ONNXRuntimeVersion.cmake in the future. See microsoft/onnxruntime#3124 for reference
Hi all, # Find onnxruntime library
#
# usage:
# find_package(onnxruntime REQUIRED)
# if wants to check for particular execution provider:
# find_package(onnxruntime REQUIRED COMPONENTS onnxruntime_providers_<PROVIDER_NAME>)
# find_package(onnxruntime REQUIRED COMPONENTS onnxruntime_providers_cuda onnxruntime_providers_tensorrt)
# tested with cuda and tensorrt
#
#
# Note: This file only search libs of execution providers, not their dependencies
#
include(FindPackageHandleStandardArgs)
# checking if onnxruntime_ROOT contain cmake find_package compatible config
file(GLOB __cmake_config_file "${onnxruntime_ROOT}/lib/cmake/onnxruntime/onnxruntimeConfig.cmake")
LIST(LENGTH __cmake_config_file __cmake_config_file_count)
if(__cmake_config_file_count GREATER 0)
set(ARGS "")
if(${onnxruntime_FIND_VERSION})
list(APPEND ARGS ${onnxruntime_FIND_VERSION})
endif()
if(${onnxruntime_FIND_VERSION_EXACT})
list(APPEND ARGS "EXACT")
endif()
if (${onnxruntime_FIND_QUIET})
list(APPEND ARGS "QUIET")
elseif (${onnxruntime_FIND_REQUIRED})
list(APPEND ARGS "REQUIRED")
endif()
if(onnxruntime_FIND_COMPONENTS)
list(APPEND ARGS "COMPONENTS" ${onnxruntime_FIND_COMPONENTS})
endif()
# recalling find_package with args but with specified path
find_package(onnxruntime ${ARGS} CONFIG PATHS ${onnxruntime_ROOT})
else()
# checking if onnxruntime_ROOT contain nuget package
file(GLOB __nupkg_file "${onnxruntime_ROOT}/Microsoft.ML.OnnxRuntime.*.nupkg")
LIST(LENGTH __nupkg_file __nuget_file_count)
if(__nuget_file_count GREATER 0)
list (GET __nupkg_file 0 __nupkg_file)
STRING(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" onnxruntime_VERSION "${__nupkg_file}")
message(STATUS "Loading using nuget")
set(ORT_INCLUDE_DIR "${onnxruntime_ROOT}/build/native/include/")
if(WIN32)
set(ORT_LIBRARY_DIR "${onnxruntime_ROOT}/runtimes/win-x64/native/")
elseif(UNIX)
set(ORT_LIBRARY_DIR "${onnxruntime_ROOT}/runtimes/linux-x64/native/")
else()
message(FATAL_ERROR "Nuget package only support Win and linux")
endif()
set(onnxruntime_FOUND FALSE)
if(EXISTS ${ORT_INCLUDE_DIR} AND EXISTS ${ORT_LIBRARY_DIR})
set(onnxruntime_FOUND TRUE)
endif()
if(onnxruntime_FOUND)
add_library(onnxruntime::onnxruntime SHARED IMPORTED)
set_target_properties(onnxruntime::onnxruntime PROPERTIES
IMPORTED_CONFIGURATIONS RELEASE
INTERFACE_INCLUDE_DIRECTORIES "${ORT_INCLUDE_DIR}"
)
if(WIN32)
set_target_properties(onnxruntime::onnxruntime PROPERTIES
IMPORTED_IMPLIB "${ORT_LIBRARY_DIR}onnxruntime.lib"
IMPORTED_LOCATION "${ORT_LIBRARY_DIR}onnxruntime.dll"
)
elseif(UNIX AND NOT APPLE)
set_target_properties(onnxruntime::onnxruntime PROPERTIES IMPORTED_LOCATION "${ORT_LIBRARY_DIR}libonnxruntime.so")
endif()
endif()
elseif(EXISTS "${onnxruntime_ROOT}/bin" AND EXISTS "${onnxruntime_ROOT}/include" AND EXISTS "${onnxruntime_ROOT}/lib")
set(ORT_INCLUDE_DIR "${onnxruntime_ROOT}/include/")
set(ORT_RUNTIME_DIR "${onnxruntime_ROOT}/bin/")
set(ORT_LIBRARY_DIR "${onnxruntime_ROOT}/lib/")
set(onnxruntime_FOUND TRUE)
add_library(onnxruntime::onnxruntime SHARED IMPORTED)
set_target_properties(onnxruntime::onnxruntime PROPERTIES
IMPORTED_CONFIGURATIONS RELEASE
INTERFACE_INCLUDE_DIRECTORIES "${ORT_INCLUDE_DIR}"
)
if(WIN32)
set_target_properties(onnxruntime::onnxruntime PROPERTIES
IMPORTED_IMPLIB "${ORT_LIBRARY_DIR}onnxruntime.lib"
IMPORTED_LOCATION "${ORT_RUNTIME_DIR}onnxruntime.dll"
)
elseif(UNIX AND NOT APPLE)
set_target_properties(onnxruntime::onnxruntime PROPERTIES IMPORTED_LOCATION "${ORT_LIBRARY_DIR}libonnxruntime.so")
endif()
endif()
if(onnxruntime_FOUND)
function(import_providers provider_name)
if(WIN32)
set(lib_path "${ORT_LIBRARY_DIR}onnxruntime_providers_${provider_name}.dll")
elseif(UNIX AND NOT APPLE)
set(lib_path "${ORT_LIBRARY_DIR}libonnxruntime_providers_${provider_name}.so")
endif()
if(EXISTS ${lib_path})
add_library(onnxruntime::onnxruntime_providers_${provider_name} MODULE IMPORTED)
set_target_properties(onnxruntime::onnxruntime_providers_${provider_name} PROPERTIES
IMPORTED_CONFIGURATIONS RELEASE
INTERFACE_INCLUDE_DIRECTORIES ${ORT_INCLUDE_DIR}
IMPORTED_LOCATION ${lib_path}
)
set(onnxruntime_onnxruntime_providers_${provider_name}_FOUND TRUE PARENT_SCOPE)
endif()
endfunction(import_providers)
set(ORT_PROVIDERS cuda;tensorrt)
foreach(__component ${ORT_PROVIDERS})
import_providers(${__component})
endforeach()
find_package_handle_standard_args(
onnxruntime
REQUIRED_VARS onnxruntime_ROOT
FOUND_VAR onnxruntime_FOUND
VERSION_VAR onnxruntime_VERSION
HANDLE_COMPONENTS
)
endif()
endif() It can be used as a cmake module to find onnxruntime, and I have tested it with onnxruntime current cmake install package, NuGet package, and cmake package config file. |
any moredern way to use this? |
@lucasjinreal |
**Description**: Adds support for cmake find_package. **Motivation and Context** As mentioned in issue #7150 onnxruntime doesn't have support for CMake find_package, this PR adds that and also adds the CMake package version file. Now anyone can link onnxruntime like this: ```cmake find_package(onnxruntime) add_executable(test Source.cpp) target_link_libraries(test PRIVATE onnxruntime::onnxruntime) ``` this also simplifies #3124
@snnn could you update example at https://github.com/microsoft/onnxruntime-inference-examples/blob/main/c_cxx/CMakeLists.txt for cmake? as #8919 has been merged. |
@prateek9623 Can you provide a CMakeLists.txt file to illustrate how to use it with cuda provider? |
@shizidushu Let's say you have this list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(CUDAToolkit 11 REQUIRED COMPONENTS cublas)
find_package(CUDNN 8 REQUIRED)
find_package(onnxruntime 1.13 REQUIRED COMPONENTS onnxruntime_providers_cuda OPTIONAL_COMPONENTS onnxruntime_providers_tensorrt)
add_executable(Test Source.cpp)
target_link_libraries(Test PRIVATE onnxruntime::onnxruntime) And your source file: #include "onnxruntime_cxx_api.h" |
@prateek9623 I found some problem when using you cmake in linux.
|
Is your feature request related to a problem? Please describe.
I want to be able to do find(onnxruntime) in my CMakeLists.txt and move on with my day. Can you guys provide a clear CMakeLists.txt example on how to include onnxruntime for a C++ project?
System information
Describe the solution you'd like
Can you guys provide a clear CMakeLists.txt example on how to include onnxruntime for a C++ project?
Describe alternatives you've considered
Looked everywhere online for a solution.
The text was updated successfully, but these errors were encountered: