|
| 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 | + |
| 26 | +# If not found via CMake config, try to find manually |
| 27 | +find_path(onnxruntime_INCLUDE_DIR |
| 28 | + NAMES onnxruntime_c_api.h |
| 29 | + PATHS |
| 30 | + /usr/include |
| 31 | + /usr/local/include |
| 32 | + /opt/onnxruntime/include |
| 33 | + $ENV{ONNXRUNTIME_ROOT}/include |
| 34 | + ${CMAKE_CURRENT_LIST_DIR}/../../../../.. |
| 35 | +) |
| 36 | + |
| 37 | +find_library(onnxruntime_LIBRARY |
| 38 | + NAMES onnxruntime |
| 39 | + PATHS |
| 40 | + /usr/lib |
| 41 | + /usr/local/lib |
| 42 | + /opt/onnxruntime/lib |
| 43 | + $ENV{ONNXRUNTIME_ROOT}/lib |
| 44 | + ${CMAKE_CURRENT_LIST_DIR}/../../../../.. |
| 45 | +) |
| 46 | + |
| 47 | +# Try to determine version from header file |
| 48 | +if(onnxruntime_INCLUDE_DIR) |
| 49 | + file(STRINGS "${onnxruntime_INCLUDE_DIR}/onnxruntime_c_api.h" onnxruntime_version_str |
| 50 | + REGEX "^#define[\t ]+ORT_API_VERSION[\t ]+[0-9]+") |
| 51 | + |
| 52 | + if(onnxruntime_version_str) |
| 53 | + string(REGEX REPLACE "^#define[\t ]+ORT_API_VERSION[\t ]+([0-9]+)" "\\1" |
| 54 | + onnxruntime_VERSION "${onnxruntime_version_str}") |
| 55 | + endif() |
| 56 | +endif() |
| 57 | + |
| 58 | +include(FindPackageHandleStandardArgs) |
| 59 | +find_package_handle_standard_args(onnxruntime |
| 60 | + REQUIRED_VARS onnxruntime_LIBRARY onnxruntime_INCLUDE_DIR |
| 61 | + VERSION_VAR onnxruntime_VERSION |
| 62 | +) |
| 63 | + |
| 64 | +if(onnxruntime_FOUND) |
| 65 | + set(onnxruntime_LIBRARIES ${onnxruntime_LIBRARY}) |
| 66 | + set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INCLUDE_DIR}) |
| 67 | + |
| 68 | + if(NOT TARGET onnxruntime) |
| 69 | + add_library(onnxruntime UNKNOWN IMPORTED) |
| 70 | + set_target_properties(onnxruntime PROPERTIES |
| 71 | + IMPORTED_LOCATION "${onnxruntime_LIBRARY}" |
| 72 | + INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}" |
| 73 | + ) |
| 74 | + endif() |
| 75 | +endif() |
| 76 | + |
| 77 | +mark_as_advanced(onnxruntime_INCLUDE_DIR onnxruntime_LIBRARY) |
0 commit comments