-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathFindTensorRT.cmake
More file actions
145 lines (124 loc) · 4.33 KB
/
FindTensorRT.cmake
File metadata and controls
145 lines (124 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
cmake_minimum_required(VERSION 3.17.0)
function(_guess_path var_name required_files)
set(_result "")
foreach(path_entry IN LISTS ARGN)
if(NOT EXISTS "${path_entry}")
message(DEBUG "skip non-existing path '${path_entry}'")
continue()
endif()
set(_ok TRUE)
foreach(required_file IN LISTS required_files)
if(NOT EXISTS "${path_entry}/${required_file}")
set(_ok FALSE)
message(DEBUG "'${path_entry}' missing '${required_file}'")
break()
endif()
endforeach()
if(_ok)
list(APPEND _result "${path_entry}")
message(DEBUG "accept '${path_entry}'")
else()
message(DEBUG "reject '${path_entry}'")
endif()
endforeach()
if(_result STREQUAL "")
message(
FATAL_ERROR
"_guess_path(${var_name}) failed: no valid path found. required_files='${required_files}' candidates='${ARGN}'"
)
endif()
set(${var_name}
"${_result}"
PARENT_SCOPE)
endfunction()
# add library
add_library(TensorRT IMPORTED INTERFACE)
add_library(TensorRT::TensorRT ALIAS TensorRT)
set(TRT_VERSION
CACHE
STRING
"TensorRT version, e.g. \"8.6.1.6\" or \"8.6.1.6+cuda12.0.1.011\", \"8.6.1.6.Windows10.x86_64.cuda-12.0\" etc"
)
if(NOT TRT_VERSION STREQUAL "" AND NOT $ENV{TRT_VERSION} STREQUAL "")
message(
WARNING
"TRT_VERSION defined by cmake and environment variable both, using the later one"
)
endif()
if(NOT $ENV{TRT_VERSION} STREQUAL "")
set(TRT_VERSION $ENV{TRT_VERSION})
endif()
string(REGEX MATCH "([0-9]+)" _match ${TRT_VERSION})
set(TRT_MAJOR_VERSION "${_match}")
unset(_match)
if(WIN32)
set(TensorRT_DIR "C:/Program Files/TensorRT-${TRT_VERSION}")
if(NOT EXISTS "${TensorRT_DIR}")
message(
FATAL_ERROR
"TensorRT_DIR=${TensorRT_DIR} does not exist!"
)
endif()
if(${TRT_MAJOR_VERSION} GREATER_EQUAL 10)
set(_modules nvinfer_10 nvinfer_plugin_10 nvinfer_vc_plugin_10
nvinfer_dispatch_10 nvinfer_lean_10)
message(DEBUG "Using ${_modules}")
else()
set(_modules nvinfer nvinfer_plugin nvinfer_vc_plugin nvinfer_dispatch
nvinfer_lean)
endif()
set(TensorRT_LIBRARY_DIR "${TensorRT_DIR}/lib")
set(TensorRT_INCLUDE_DIR "${TensorRT_DIR}/include")
elseif(UNIX)
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" _trt_arch)
set(_trt_include_candidates)
if(_trt_arch MATCHES "^(aarch64|arm64|arch64)$")
set(_trt_include_candidates "/usr/include/aarch64-linux-gnu" "/usr/include"
"/usr/local/cuda/targets/aarch64-linux/include")
set(_trt_library_candidates
"/usr/local/tensorrt/targets/aarch64-linux-gnu/lib"
"/usr/lib/aarch64-linux-gnu" "/usr/lib/aarch64-linux-gnu/tegra"
"/usr/lib")
elseif(_trt_arch MATCHES "^(x86_64|amd64)$")
set(_trt_include_candidates
"/usr/local/tensorrt/targets/x86_64-linux-gnu/include"
"/usr/include/x86_64-linux-gnu" "/usr/include")
set(_trt_library_candidates
"/usr/local/tensorrt/targets/x86_64-linux-gnu/lib"
"/usr/lib/x86_64-linux-gnu" "/usr/lib")
else()
message(FATAL_ERROR "Unknown architecture")
endif()
set(_modules nvinfer nvinfer_plugin)
if(${TRT_MAJOR_VERSION} GREATER_EQUAL 8)
list(APPEND _modules nvinfer_vc_plugin nvinfer_dispatch nvinfer_lean)
endif()
_guess_path(TensorRT_LIBRARY_DIR "libnvinfer.so;libnvinfer_plugin.so"
${_trt_library_candidates})
message(STATUS "TensorRT libraries: ${TensorRT_LIBRARY_DIR}")
_guess_path(TensorRT_INCLUDE_DIR "NvInfer.h" ${_trt_include_candidates})
message(STATUS "TensorRT includes: ${TensorRT_INCLUDE_DIR}")
endif()
foreach(lib IN LISTS _modules)
find_library(
TensorRT_${lib}_LIBRARY
NAMES ${lib}
HINTS ${TensorRT_LIBRARY_DIR})
list(APPEND TensorRT_LIBRARIES ${TensorRT_${lib}_LIBRARY})
endforeach()
target_link_libraries(TensorRT INTERFACE ${TensorRT_LIBRARIES})
message(STATUS "Found TensorRT libs: ${TensorRT_LIBRARIES}")
set_target_properties(
TensorRT
PROPERTIES C_STANDARD 17
CXX_STANDARD 17
POSITION_INDEPENDENT_CODE ON
SKIP_BUILD_RPATH TRUE
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH "$ORIGIN"
INTERFACE_INCLUDE_DIRECTORIES "${TensorRT_INCLUDE_DIR}")
unset(TRT_MAJOR_VERSION)
unset(_modules)
unset(_trt_include_candidates)
unset(_trt_library_candidates)
unset(_trt_arch)