Findonnxruntime.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2025 Sony Semiconductor Solutions Corporation.
  2. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  3. # Find ONNX Runtime library
  4. #
  5. # This module defines the following variables:
  6. #
  7. # ::
  8. #
  9. # onnxruntime_FOUND - True if onnxruntime is found
  10. # onnxruntime_INCLUDE_DIRS - Include directories for onnxruntime
  11. # onnxruntime_LIBRARIES - List of libraries for onnxruntime
  12. # onnxruntime_VERSION - Version of onnxruntime
  13. #
  14. # ::
  15. #
  16. # Example usage:
  17. #
  18. # find_package(onnxruntime)
  19. # if(onnxruntime_FOUND)
  20. # target_link_libraries(app onnxruntime)
  21. # endif()
  22. # First try to find ONNX Runtime using the CMake config file
  23. # FIXME: This is a temporary workaround for ONNX Runtime's broken CMake config on Linux.
  24. # See https://github.com/microsoft/onnxruntime/issues/25279
  25. # Once the upstream issue is fixed, this conditional can be safely removed.
  26. if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
  27. find_package(onnxruntime CONFIG QUIET)
  28. if(onnxruntime_FOUND)
  29. return()
  30. endif()
  31. endif()
  32. # If not found via CMake config, try to find manually
  33. find_path(onnxruntime_INCLUDE_DIR
  34. NAMES onnxruntime_c_api.h
  35. PATHS
  36. /usr/include
  37. /usr/local/include
  38. /opt/onnxruntime/include
  39. $ENV{ONNXRUNTIME_ROOT}/include
  40. ${CMAKE_CURRENT_LIST_DIR}/../../../../..
  41. )
  42. find_library(onnxruntime_LIBRARY
  43. NAMES onnxruntime
  44. PATHS
  45. /usr/lib
  46. /usr/local/lib
  47. /opt/onnxruntime/lib
  48. $ENV{ONNXRUNTIME_ROOT}/lib
  49. ${CMAKE_CURRENT_LIST_DIR}/../../../../..
  50. )
  51. # Try to determine version from header file
  52. if(onnxruntime_INCLUDE_DIR)
  53. file(STRINGS "${onnxruntime_INCLUDE_DIR}/onnxruntime_c_api.h" onnxruntime_version_str
  54. REGEX "^#define[\t ]+ORT_API_VERSION[\t ]+[0-9]+")
  55. if(onnxruntime_version_str)
  56. string(REGEX REPLACE "^#define[\t ]+ORT_API_VERSION[\t ]+([0-9]+)" "\\1"
  57. onnxruntime_VERSION "${onnxruntime_version_str}")
  58. endif()
  59. endif()
  60. include(FindPackageHandleStandardArgs)
  61. find_package_handle_standard_args(onnxruntime
  62. REQUIRED_VARS onnxruntime_LIBRARY onnxruntime_INCLUDE_DIR
  63. VERSION_VAR onnxruntime_VERSION
  64. )
  65. if(onnxruntime_FOUND)
  66. set(onnxruntime_LIBRARIES ${onnxruntime_LIBRARY})
  67. set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INCLUDE_DIR})
  68. if(NOT TARGET onnxruntime::onnxruntime)
  69. add_library(onnxruntime::onnxruntime UNKNOWN IMPORTED)
  70. set_target_properties(onnxruntime::onnxruntime PROPERTIES
  71. IMPORTED_LOCATION "${onnxruntime_LIBRARY}"
  72. INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}"
  73. )
  74. endif()
  75. endif()
  76. mark_as_advanced(onnxruntime_INCLUDE_DIR onnxruntime_LIBRARY)