Debian Codegraph @

cmake/Findonnxruntime.cmake

raw
1 # Copyright (c), ETH Zurich and UNC Chapel Hill.
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 #
14 # * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 # its contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30
31 # Find package module for onnxruntime library.
32 #
33 # The following variables are set by this module:
34 #
35 # onnxruntime_FOUND: TRUE if onnxruntime is found.
36 # onnxruntime::onnxruntime: Imported target to link against.
37 #
38 # The following variables control the behavior of this module:
39 #
40 # onnxruntime_CONFIG_DIR_HINTS: List of additional directories in which to
41 # search for onnxruntime CMake configs.
42 # onnxruntime_INCLUDE_DIR_HINTS: List of additional directories in which to
43 # search for onnxruntime includes.
44 # onnxruntime_LIBRARY_DIR_HINTS: List of additional directories in which to
45 # search for onnxruntime libraries.
46
47 set(onnxruntime_CONFIG_DIR_HINTS "" CACHE PATH "onnxruntime config directory")
48 set(onnxruntime_INCLUDE_DIR_HINTS "" CACHE PATH "onnxruntime include directory")
49 set(onnxruntime_LIBRARY_DIR_HINTS "" CACHE PATH "onnxruntime library directory")
50
51 unset(onnxruntime_FOUND)
52 unset(onnxruntime_INCLUDE_DIRS)
53 unset(onnxruntime_LIBRARIES)
54
55 find_package(onnxruntime CONFIG QUIET PATHS ${onnxruntime_CONFIG_DIR_HINTS})
56 if(TARGET onnxruntime::onnxruntime)
57 set(onnxruntime_FOUND TRUE)
58 message(STATUS "Found onnxruntime")
59 message(STATUS " Target : onnxruntime::onnxruntime")
60 else()
61 find_path(onnxruntime_INCLUDE_DIRS
62 NAMES
63 onnxruntime/onnxruntime_cxx_api.h
64 PATHS
65 ${onnxruntime_INCLUDE_DIR_HINTS}
66 /usr/include
67 /usr/local/include
68 /sw/include
69 /opt/include
70 /opt/local/include)
71 list(APPEND onnxruntime_LIBRARY_DIR_HINTS
72 /usr/lib64
73 /usr/lib
74 /usr/local/lib64
75 /usr/local/lib
76 /sw/lib
77 /opt/lib
78 /opt/local/lib)
79 find_library(onnxruntime_LIBRARIES
80 NAMES onnxruntime libonnxruntime
81 PATHS ${onnxruntime_LIBRARY_DIR_HINTS})
82 find_library(onnxruntime_PROVIDERS_SHARED_LIBRARY
83 NAMES onnxruntime_providers_shared libonnxruntime_providers_shared
84 PATHS ${onnxruntime_LIBRARY_DIR_HINTS})
85 if(CUDA_ENABLED)
86 find_library(onnxruntime_PROVIDERS_CUDA_LIBRARY
87 NAMES onnxruntime_providers_cuda libonnxruntime_providers_cuda
88 PATHS ${onnxruntime_LIBRARY_DIR_HINTS})
89 endif()
90
91 if(onnxruntime_INCLUDE_DIRS AND onnxruntime_LIBRARIES)
92 if(onnxruntime_PROVIDERS_SHARED_LIBRARY)
93 list(APPEND onnxruntime_LIBRARIES ${onnxruntime_PROVIDERS_SHARED_LIBRARY})
94 endif()
95 if(onnxruntime_PROVIDERS_CUDA_LIBRARY)
96 list(APPEND onnxruntime_LIBRARIES ${onnxruntime_PROVIDERS_CUDA_LIBRARY})
97 endif()
98 set(onnxruntime_FOUND TRUE)
99 message(STATUS "Found onnxruntime")
100 message(STATUS " Includes : ${onnxruntime_INCLUDE_DIRS}")
101 message(STATUS " Libraries : ${onnxruntime_LIBRARIES}")
102 else()
103 set(onnxruntime_FOUND FALSE)
104 endif()
105
106 add_library(onnxruntime::onnxruntime INTERFACE IMPORTED)
107 target_include_directories(
108 onnxruntime::onnxruntime INTERFACE ${onnxruntime_INCLUDE_DIRS}/onnxruntime)
109 target_link_libraries(
110 onnxruntime::onnxruntime INTERFACE ${onnxruntime_LIBRARIES})
111 # This is a hack to make sure that the onnxruntime dll is copied to the output directory,
112 # since vcpkg's custom add_library/add_executable macros copy any dependencies from vcpkg's
113 # installed directory to the output directory.
114 # See: https://github.com/microsoft/vcpkg/blob/fb7ba3b89b0d8e3e56b0508a144fe85015edfab6/scripts/buildsystems/vcpkg.cmake#L607
115 if(IS_WINDOWS AND VCPKG_INSTALLED_DIR)
116 foreach(_lib ${onnxruntime_LIBRARIES})
117 get_filename_component(_lib_dir "${_lib}" DIRECTORY)
118 get_filename_component(_lib_name "${_lib}" NAME_WE)
119 set(_dlls
120 "${_lib_dir}/${_lib_name}.dll"
121 "${_lib_dir}/../bin/${_lib_name}.dll")
122 foreach(_dll ${_dlls})
123 if( EXISTS "${_dll}" )
124 file(COPY "${_dll}" DESTINATION "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/bin")
125 endif()
126 endforeach()
127 endforeach()
128 endif()
129 endif()
130
131 if(NOT onnxruntime_FOUND AND onnxruntime_FIND_REQUIRED)
132 message(FATAL_ERROR "Could not find onnxruntime")
133 endif()