tensorrt.cmake 3.8 KB
Newer Older
1
if(NOT WITH_GPU OR NOT WITH_TENSORRT)
L
Luo Tao 已提交
2 3 4
    return()
endif()

5 6 7 8 9 10 11 12 13 14 15 16
if(WIN32)
    string(REPLACE "\\" "/" TENSORRT_ROOT "${TENSORRT_ROOT}")
    set(TR_INFER_LIB nvinfer.lib)
    set(TR_INFER_RT nvinfer.dll)
    set(TR_INFER_PLUGIN_RT nvinfer_plugin.dll)
else()
    set(TENSORRT_ROOT "/usr" CACHE PATH "TENSORRT ROOT")
    set(TR_INFER_LIB libnvinfer.a)
    set(TR_INFER_RT libnvinfer.so)
    set(TR_INFER_PLUGIN_RT libnvinfer_plugin.so)
endif()

L
Luo Tao 已提交
17 18
find_path(TENSORRT_INCLUDE_DIR NvInfer.h
    PATHS ${TENSORRT_ROOT} ${TENSORRT_ROOT}/include
J
Jeng Bai-Cheng 已提交
19
    ${TENSORRT_ROOT}/include/${CMAKE_LIBRARY_ARCHITECTURE}
L
Luo Tao 已提交
20
    $ENV{TENSORRT_ROOT} $ENV{TENSORRT_ROOT}/include
J
Jeng Bai-Cheng 已提交
21
    $ENV{TENSORRT_ROOT}/include/${CMAKE_LIBRARY_ARCHITECTURE}
L
Luo Tao 已提交
22 23 24
    NO_DEFAULT_PATH
)

J
Jeng Bai-Cheng 已提交
25
find_path(TENSORRT_LIBRARY_DIR NAMES ${TR_INFER_LIB} ${TR_INFER_RT}
L
Luo Tao 已提交
26
    PATHS ${TENSORRT_ROOT} ${TENSORRT_ROOT}/lib
J
Jeng Bai-Cheng 已提交
27
    ${TENSORRT_ROOT}/lib/${CMAKE_LIBRARY_ARCHITECTURE}
L
Luo Tao 已提交
28
    $ENV{TENSORRT_ROOT} $ENV{TENSORRT_ROOT}/lib
J
Jeng Bai-Cheng 已提交
29 30 31 32 33 34 35
    $ENV{TENSORRT_ROOT}/lib/${CMAKE_LIBRARY_ARCHITECTURE}
    NO_DEFAULT_PATH
    DOC "Path to TensorRT library."
)

find_library(TENSORRT_LIBRARY NAMES ${TR_INFER_LIB} ${TR_INFER_RT}
    PATHS ${TENSORRT_LIBRARY_DIR}
L
Luo Tao 已提交
36 37 38 39
    NO_DEFAULT_PATH
    DOC "Path to TensorRT library.")

if(TENSORRT_INCLUDE_DIR AND TENSORRT_LIBRARY)
40
    set(TENSORRT_FOUND ON)
L
Luo Tao 已提交
41 42
else()
    set(TENSORRT_FOUND OFF)
43
    message(WARNING "TensorRT is disabled. You are compiling PaddlePaddle with option -DWITH_TENSORRT=ON, but TensorRT is not found, please configure path to TensorRT with option -DTENSORRT_ROOT or install it.")
L
Luo Tao 已提交
44 45 46 47 48 49
endif()

if(TENSORRT_FOUND)
    file(READ ${TENSORRT_INCLUDE_DIR}/NvInfer.h TENSORRT_VERSION_FILE_CONTENTS)
    string(REGEX MATCH "define NV_TENSORRT_MAJOR +([0-9]+)" TENSORRT_MAJOR_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
W
Wilber 已提交
50 51 52 53 54 55
    string(REGEX MATCH "define NV_TENSORRT_MINOR +([0-9]+)" TENSORRT_MINOR_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
    string(REGEX MATCH "define NV_TENSORRT_PATCH +([0-9]+)" TENSORRT_PATCH_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
    string(REGEX MATCH "define NV_TENSORRT_BUILD +([0-9]+)" TENSORRT_BUILD_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
J
Jeng Bai-Cheng 已提交
56 57 58 59 60

    if("${TENSORRT_MAJOR_VERSION}" STREQUAL "")
        file(READ ${TENSORRT_INCLUDE_DIR}/NvInferVersion.h TENSORRT_VERSION_FILE_CONTENTS)
        string(REGEX MATCH "define NV_TENSORRT_MAJOR +([0-9]+)" TENSORRT_MAJOR_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
W
Wilber 已提交
61 62 63 64 65 66
        string(REGEX MATCH "define NV_TENSORRT_MINOR +([0-9]+)" TENSORRT_MINOR_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
        string(REGEX MATCH "define NV_TENSORRT_PATCH +([0-9]+)" TENSORRT_PATCH_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
        string(REGEX MATCH "define NV_TENSORRT_BUILD +([0-9]+)" TENSORRT_BUILD_VERSION
        "${TENSORRT_VERSION_FILE_CONTENTS}")
J
Jeng Bai-Cheng 已提交
67 68 69 70 71 72
    endif()

    if("${TENSORRT_MAJOR_VERSION}" STREQUAL "")
        message(SEND_ERROR "Failed to detect TensorRT version.")
    endif()

L
Luo Tao 已提交
73 74
    string(REGEX REPLACE "define NV_TENSORRT_MAJOR +([0-9]+)" "\\1"
        TENSORRT_MAJOR_VERSION "${TENSORRT_MAJOR_VERSION}")
W
Wilber 已提交
75 76 77 78 79 80
    string(REGEX REPLACE "define NV_TENSORRT_MINOR +([0-9]+)" "\\1"
        TENSORRT_MINOR_VERSION "${TENSORRT_MINOR_VERSION}")
    string(REGEX REPLACE "define NV_TENSORRT_PATCH +([0-9]+)" "\\1"
        TENSORRT_PATCH_VERSION "${TENSORRT_PATCH_VERSION}")
    string(REGEX REPLACE "define NV_TENSORRT_BUILD +([0-9]+)" "\\1"
        TENSORRT_BUILD_VERSION "${TENSORRT_BUILD_VERSION}")
L
Luo Tao 已提交
81 82

    message(STATUS "Current TensorRT header is ${TENSORRT_INCLUDE_DIR}/NvInfer.h. "
W
Wilber 已提交
83
        "Current TensorRT version is v${TENSORRT_MAJOR_VERSION}.${TENSORRT_MINOR_VERSION}.${TENSORRT_PATCH_VERSION}.${TENSORRT_BUILD_VERSION} ")
L
Luo Tao 已提交
84
    include_directories(${TENSORRT_INCLUDE_DIR})
N
nhzlx 已提交
85
    link_directories(${TENSORRT_LIBRARY})
86
    add_definitions(-DPADDLE_WITH_TENSORRT)
L
Luo Tao 已提交
87
endif()