tensorrt.cmake 7.3 KB
Newer Older
1 2
if("${TRT_ROOT_DIR}" STREQUAL "" AND NOT "$ENV{TRT_ROOT_DIR}" STREQUAL "")
  set(TRT_ROOT_DIR $ENV{TRT_ROOT_DIR})
3 4
endif()

5
if(MGE_CUDA_USE_STATIC)
6 7 8 9 10 11 12 13 14 15 16 17 18 19
  find_library(
    TRT_LIBRARY
    NAMES libnvinfer_static.a nvinfer.lib
    PATHS ${ALTER_LD_LIBRARY_PATHS} ${TRT_ROOT_DIR} ${CMAKE_INSTALL_PREFIX}
    HINTS ${ALTER_LIBRARY_PATHS}
    PATH_SUFFIXES lib lib64
    DOC "TRT library.")
  find_library(
    TRT_PLUGIN_LIBRARY
    NAMES libnvinfer_plugin_static.a nvinfer_plugin.lib
    PATHS ${ALTER_LD_LIBRARY_PATHS} ${TRT_ROOT_DIR} ${CMAKE_INSTALL_PREFIX}
    HINTS ${ALTER_LIBRARY_PATHS}
    PATH_SUFFIXES lib lib64
    DOC "TRT plugin library.")
20
else()
21 22 23 24 25 26 27 28 29 30 31 32 33 34
  find_library(
    TRT_LIBRARY
    NAMES libnvinfer.so libnvinfer.dylib nvinfer.dll
    PATHS ${ALTER_LD_LIBRARY_PATHS} ${TRT_ROOT_DIR} ${CMAKE_INSTALL_PREFIX}
    HINTS ${ALTER_LIBRARY_PATHS}
    PATH_SUFFIXES lib lib64
    DOC "TRT library.")
  find_library(
    TRT_PLUGIN_LIBRARY
    NAMES libnvinfer_plugin.so libnvinfer_plugin.dylib nvinfer_plugin.dll
    PATHS ${ALTER_LD_LIBRARY_PATHS} ${TRT_ROOT_DIR} ${CMAKE_INSTALL_PREFIX}
    HINTS ${ALTER_LIBRARY_PATHS}
    PATH_SUFFIXES lib lib64
    DOC "TRT plugin library.")
35 36 37
endif()

if(TRT_LIBRARY STREQUAL "TRT_LIBRARY-NOTFOUND")
38 39 40 41
  message(
    FATAL_ERROR
      "Can not find TensorRT Library, please refer to scripts/cmake-build/BUILD_README.md to init TRT env"
  )
42
endif()
43
if(TRT_PLUGIN_LIBRARY STREQUAL "TRT_PLUGIN_LIBRARY-NOTFOUND")
44 45 46 47
  message(
    FATAL_ERROR
      "Can not find TensorRT Plugin Library, please refer to scripts/cmake-build/BUILD_README.md to init TRT env"
  )
48
endif()
49 50

get_filename_component(__found_trt_root ${TRT_LIBRARY}/../.. REALPATH)
51 52 53 54 55 56 57 58 59 60 61 62
find_path(
  TRT_INCLUDE_DIR
  NAMES NvInfer.h
  HINTS ${TRT_ROOT_DIR} ${CUDA_TOOLKIT_INCLUDE} ${__found_trt_root}
  PATH_SUFFIXES include
  DOC "Path to TRT include directory.")
find_path(
  TRT_PLUGIN_INCLUDE_DIR
  NAMES NvInferPlugin.h
  HINTS ${TRT_ROOT_DIR} ${CUDA_TOOLKIT_INCLUDE} ${__found_trt_root}
  PATH_SUFFIXES include
  DOC "Path to TRT plugin include directory.")
63 64

if(TRT_INCLUDE_DIR STREQUAL "TRT_INCLUDE_DIR-NOTFOUND")
65 66 67 68
  message(
    FATAL_ERROR
      "Can not find TensorRT INCLUDE, please refer to scripts/cmake-build/BUILD_README.md to init TRT env"
  )
69
endif()
70
if(TRT_PLUGIN_INCLUDE_DIR STREQUAL "TRT_PLUGIN_INCLUDE_DIR-NOTFOUND")
71 72 73 74
  message(
    FATAL_ERROR
      "Can not find TensorRT Plugin INCLUDE, please refer to scripts/cmake-build/BUILD_README.md to init TRT env"
  )
75
endif()
76

77 78 79 80 81 82
file(STRINGS "${TRT_INCLUDE_DIR}/NvInfer.h" TensorRT_MAJOR
     REGEX "^#define NV_TENSORRT_MAJOR [0-9]+.*$")
file(STRINGS "${TRT_INCLUDE_DIR}/NvInfer.h" TensorRT_MINOR
     REGEX "^#define NV_TENSORRT_MINOR [0-9]+.*$")
file(STRINGS "${TRT_INCLUDE_DIR}/NvInfer.h" TensorRT_PATCH
     REGEX "^#define NV_TENSORRT_PATCH [0-9]+.*$")
83

84 85 86 87 88 89 90
if(TensorRT_MAJOR STREQUAL "")
  file(STRINGS "${TRT_INCLUDE_DIR}/NvInferVersion.h" TensorRT_MAJOR
       REGEX "^#define NV_TENSORRT_MAJOR [0-9]+.*$")
  file(STRINGS "${TRT_INCLUDE_DIR}/NvInferVersion.h" TensorRT_MINOR
       REGEX "^#define NV_TENSORRT_MINOR [0-9]+.*$")
  file(STRINGS "${TRT_INCLUDE_DIR}/NvInferVersion.h" TensorRT_PATCH
       REGEX "^#define NV_TENSORRT_PATCH [0-9]+.*$")
91 92
endif()

93 94 95 96 97 98 99 100
string(REGEX REPLACE "^#define NV_TENSORRT_MAJOR ([0-9]+).*$" "\\1"
                     TensorRT_VERSION_MAJOR "${TensorRT_MAJOR}")
string(REGEX REPLACE "^#define NV_TENSORRT_MINOR ([0-9]+).*$" "\\1"
                     TensorRT_VERSION_MINOR "${TensorRT_MINOR}")
string(REGEX REPLACE "^#define NV_TENSORRT_PATCH ([0-9]+).*$" "\\1"
                     TensorRT_VERSION_PATCH "${TensorRT_PATCH}")
set(TRT_VERSION_STRING
    "${TensorRT_VERSION_MAJOR}.${TensorRT_VERSION_MINOR}.${TensorRT_VERSION_PATCH}")
101 102

if(MGE_CUDA_USE_STATIC)
103 104
  add_library(libnvinfer STATIC IMPORTED)
  add_library(libnvinfer_plugin STATIC IMPORTED)
105
else()
106 107
  add_library(libnvinfer SHARED IMPORTED)
  add_library(libnvinfer_plugin SHARED IMPORTED)
108 109
endif()

110 111 112 113 114 115
set_target_properties(
  libnvinfer PROPERTIES IMPORTED_LOCATION ${TRT_LIBRARY} INTERFACE_INCLUDE_DIRECTORIES
                                                         ${TRT_INCLUDE_DIR})
set_target_properties(
  libnvinfer_plugin PROPERTIES IMPORTED_LOCATION ${TRT_PLUGIN_LIBRARY}
                               INTERFACE_INCLUDE_DIRECTORIES ${TRT_PLUGIN_INCLUDE_DIR})
116

117 118
message(
  STATUS "Found TensorRT: ${__found_trt_root} (found version: ${TRT_VERSION_STRING})")
119

120
if(TensorRT_VERSION_MAJOR GREATER_EQUAL 7)
121 122 123 124 125 126 127 128 129 130 131 132 133
  if(MGE_CUDA_USE_STATIC)
    find_library(
      LIBMYELIN_COMPILER
      NAMES libmyelin_compiler_static.a myelin_compiler_static.lib
      PATHS ${__found_trt_root}/lib)
    if(LIBMYELIN_COMPILER STREQUAL "LIBMYELIN_COMPILER-NOTFOUND")
      message(FATAL_ERROR "Can not find LIBMYELIN_COMPILER Library")
    else()
      message(STATUS "Found TensorRT myelin_compiler: ${LIBMYELIN_COMPILER}")
    endif()
    add_library(libmyelin_compiler STATIC IMPORTED)
    set_target_properties(libmyelin_compiler PROPERTIES IMPORTED_LOCATION
                                                        ${LIBMYELIN_COMPILER})
134

135 136 137 138 139 140 141 142 143 144 145 146
    find_library(
      LIBMYELIN_EXECUTOR
      NAMES libmyelin_executor_static.a myelin_executor_static.lib
      PATHS ${__found_trt_root}/lib)
    if(LIBMYELIN_EXECUTOR STREQUAL "LIBMYELIN_EXECUTOR-NOTFOUND")
      message(FATAL_ERROR "Can not find LIBMYELIN_EXECUTOR Library")
    else()
      message(STATUS "Found TensorRT libmyelin_executor: ${LIBMYELIN_EXECUTOR}")
    endif()
    add_library(libmyelin_executor STATIC IMPORTED)
    set_target_properties(libmyelin_executor PROPERTIES IMPORTED_LOCATION
                                                        ${LIBMYELIN_EXECUTOR})
147

148 149 150 151 152 153 154 155 156 157 158 159 160
    find_library(
      LIBMYELIN_PATTERN_RUNTIME
      NAMES libmyelin_pattern_runtime_static.a myelin_pattern_runtime_static.lib
      PATHS ${__found_trt_root}/lib)
    if(LIBMYELIN_PATTERN_RUNTIME STREQUAL "LIBMYELIN_PATTERN_RUNTIME-NOTFOUND")
      message(FATAL_ERROR "Can not find LIBMYELIN_PATTERN_RUNTIME Library")
    else()
      message(
        STATUS "Found TensorRT libmyelin_pattern_runtime: ${LIBMYELIN_PATTERN_RUNTIME}")
    endif()
    add_library(libmyelin_pattern_runtime STATIC IMPORTED)
    set_target_properties(libmyelin_pattern_runtime
                          PROPERTIES IMPORTED_LOCATION ${LIBMYELIN_PATTERN_RUNTIME})
161

162 163 164 165 166 167
    find_library(
      LIBMYELIN_PATTERN_LIBRARY
      NAMES libmyelin_pattern_library_static.a myelin_pattern_library_static.lib
      PATHS ${__found_trt_root}/lib)
    if(LIBMYELIN_PATTERN_LIBRARY STREQUAL "LIBMYELIN_PATTERN_LIBRARY-NOTFOUND")
      message(FATAL_ERROR "Can not find LIBMYELIN_PATTERN_LIBRARY Library")
168
    else()
169 170 171 172 173 174 175 176 177 178 179
      message(
        STATUS "Found TensorRT libmyelin_pattern_library: ${LIBMYELIN_PATTERN_LIBRARY}")
    endif()
    add_library(libmyelin_pattern_library STATIC IMPORTED)
    set_target_properties(libmyelin_pattern_library
                          PROPERTIES IMPORTED_LOCATION ${LIBMYELIN_PATTERN_LIBRARY})
  else()
    find_library(
      LIBMYELIN_SHARED
      NAMES libmyelin.so myelin.dll
      PATHS ${__found_trt_root}/lib)
180

181 182 183 184
    if(LIBMYELIN_SHARED STREQUAL "LIBMYELIN_SHARED-NOTFOUND")
      message(FATAL_ERROR "Can not find LIBMYELIN_SHARED Library")
    else()
      message(STATUS "Found TensorRT libmyelin_shared: ${LIBMYELIN_SHARED}")
185
    endif()
186 187 188
    add_library(libmyelin SHARED IMPORTED)
    set_target_properties(libmyelin PROPERTIES IMPORTED_LOCATION ${LIBMYELIN_SHARED})
  endif()
189
endif()