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