CMake构建支持CUDA的共享库

时间:2019-12-23 04:39:30

标签: plugins cmake cuda

我正在尝试将darknet.so(使用CUDA)集成到第三方插件中。我在编码集成方面很成功,但不确定如何解决CMake。我想知道如何正确地为它写cmake。下面我展示了末端的cmake的样子:

cmake_minimum_required(VERSION 3.3.2)
project(ai_plugin)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(UNIX)
    # In Linux, for the plugin .so library, set `rpath` to "$ORIGIN" and do not set `runpath`, thus
    # enabling the lookup of the dependencies in the plugin dir first.
    string(APPEND CMAKE_SHARED_LINKER_FLAGS " -Wl,--disable-new-dtags")
endif()
set(CMAKE_SKIP_BUILD_RPATH ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH "$ORIGIN")

if(WIN32)
    set(API_IMPORT_MACRO "__declspec(dllimport)")
    set(API_EXPORT_MACRO "__declspec(dllexport)")
else()
    set(API_IMPORT_MACRO "")
    set(API_EXPORT_MACRO "__attribute__((visibility(\"default\")))")
endif()

enable_testing()

#--------------------------------------------------------------------------------------------------
#for OpenCV libraries version > 4
#Find and add opencv libraries
find_package(OpenCV REQUIRED core imgproc highgui imgcodecs dnn video videoio tracking)

if(${OpenCV_VERSION} VERSION_LESS 4.0.0)
    MESSAGE(FATAL_ERROR "OpenCV version is not compatible : ${OpenCV_VERSION}")
else()
    MESSAGE("OpenCV version found : ${OpenCV_VERSION}")
    MESSAGE("OpenCV include path  : ${OpenCV_INCLUDE_DIRS}")
    MESSAGE("OpenCV libs          : ${OpenCV_LIBS}")
ENDIF()

#--------------------------------------------------------------------------------------------------
#Darknet
find_library( DARKNET_LIB
   NAMES dark
   PATHS /usr/local/lib/
   NO_DEFAULT_PATH
)
#--------------------------------------------------------------------------------------------------
# Define nx_kit lib, static.

set(nxKitLibraryType "STATIC" CACHE STRING "" FORCE)
set(nxKitWithTests "YES" CACHE STRING "" FORCE)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../nx_kit
    ${CMAKE_CURRENT_BINARY_DIR}/nx_kit)

#--------------------------------------------------------------------------------------------------
# Define nx_sdk lib, static, depends on nx_kit.

set(SDK_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/../../src)
file(GLOB_RECURSE SDK_SRC ${SDK_SRC_DIR}/*)

add_library(nx_sdk STATIC ${SDK_SRC})
target_include_directories(nx_sdk PUBLIC ${SDK_SRC_DIR})
target_link_libraries(nx_sdk PRIVATE nx_kit)

target_compile_definitions(nx_sdk PRIVATE NX_PLUGIN_API="")

#--------------------------------------------------------------------------------------------------
# Define nx_sdk_ut executable, depends on nx_sdk.

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../unit_tests ${CMAKE_CURRENT_BINARY_DIR}/unit_tests)

#--------------------------------------------------------------------------------------------------
# Define ai_plugin lib, dynamic, depends on nx_kit and nx_sdk.

set(AI_PLUGIN_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/src)
file(GLOB_RECURSE AI_PLUGIN_SRC ${AI_PLUGIN_SRC_DIR}/*)

add_library(ai_plugin SHARED ${AI_PLUGIN_SRC})
include_directories( ${OpenCV_INCLUDE_DIRS})
include_directories( /usr/local/cuda/include )

set_target_properties( ai_plugin PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_include_directories(ai_plugin PRIVATE ${AI_PLUGIN_SRC_DIR})
target_link_libraries( ai_plugin PUBLIC nx_kit nx_sdk) #< Made PUBLIC for unit tests.
target_link_libraries( ai_plugin PUBLIC ${OpenCV_LIBS})
target_link_libraries( ai_plugin PUBLIC ${DARKNET_LIB} )
target_link_libraries( ai_plugin PUBLIC cuda cudart cublas curand cudnn )

target_compile_definitions(ai_plugin
    PRIVATE NX_PLUGIN_API=${API_EXPORT_MACRO}
    INTERFACE NX_PLUGIN_API=${API_IMPORT_MACRO} #< Needed to link unit tests exe to plugin lib.
)

if(NOT WIN32)
    target_link_libraries(ai_plugin PRIVATE pthread)
endif()

* N.B:AlexeyAB和pjreddie对Darknet有不同的命名约定。所以。一个叫做libdarknet.so,另一个叫做libdarknet.so

该插件会以静默方式构建,但可能不会被调用,可能是因为未初始化CUDA。在代码中,我仅使用与Darknet相关的对象。我想了解cmake指令的正确方法,以构建启用了cuda的插件(共享对象)。该插件项目将没有任何.cu文件。它建立在另一个具有.cu文件的共享库上。另请注意,我无权构建消费者项目。我只负责构建插件。

一些来自NX VMS SERVER的已记录消息:

WARNING nx::vms::server::analytics::SdkObjectFactory(0x7f8d8809c690): Unable to find an SDK plugin object. Plugin name: AI plugin, plugin Id ({49b257b8-5e33-036b-1019-8c783bababf7})

2019-12-23 21:05:25.228  25721 WARNING nx::vms::server::analytics::SdkObjectFactory(0x7f8d8809c690): Unable to find an SDK plugin object. Plugin name: AI plugin, plugin Id ({e457cae5-c7a9-dd43-ccfe-edfe8d08b73d})

2019-12-23 21:05:25.236  25721 WARNING nx::vms::server::analytics::SdkObjectFactory(0x7f8d8809c690): Plugin Resource AI plugin ({49b257b8-5e33-036b-1019-8c783bababf7}) has no corresponding Plugin object

2019-12-23 21:05:25.276  25721    INFO nx::vms_server_plugins::analytics::dahua::Engine: Dahua engine manifest loaded from resource :/dahua/manifest.json

2019-12-23 21:05:25.303  25721 WARNING nx::vms::server::analytics::SdkObjectFactory(0x7f8d8809c690): Plugin Resource AI plugin ({e457cae5-c7a9-dd43-ccfe-edfe8d08b73d}) has no corresponding Plugin object

0 个答案:

没有答案