File tree Expand file tree Collapse file tree 12 files changed +77
-12
lines changed Expand file tree Collapse file tree 12 files changed +77
-12
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
24
24
# options
25
25
option (MMDEPLOY_SHARED_LIBS "build shared libs" OFF )
26
26
option (MMDEPLOY_BUILD_SDK "build MMDeploy SDK" OFF )
27
+ option (MMDEPLOY_DYNAMIC_BACKEND "dynamic load backend" OFF )
27
28
option (MMDEPLOY_BUILD_SDK_MONOLITHIC "build single lib for SDK API" ON )
28
29
option (MMDEPLOY_BUILD_TEST "build unittests" OFF )
29
30
option (MMDEPLOY_BUILD_SDK_PYTHON_API "build SDK Python API" OFF )
@@ -39,6 +40,10 @@ set(MMDEPLOY_TARGET_DEVICES "cpu" CACHE STRING "target devices to support")
39
40
set (MMDEPLOY_TARGET_BACKENDS "" CACHE STRING "target inference engines to support" )
40
41
set (MMDEPLOY_CODEBASES "all" CACHE STRING "select OpenMMLab codebases" )
41
42
43
+ if ((NOT MMDEPLOY_BUILD_SDK_MONOLITHIC ) AND MMDEPLOY_DYNAMIC_BACKEND )
44
+ set (MMDEPLOY_DYNAMIC_BACKEND OFF )
45
+ endif ()
46
+
42
47
if (NOT CMAKE_BUILD_TYPE )
43
48
set (CMAKE_BUILD_TYPE Release CACHE STRING "choose 'Release' as default build type" FORCE )
44
49
endif ()
Original file line number Diff line number Diff line change @@ -12,6 +12,15 @@ function (mmdeploy_export NAME)
12
12
RUNTIME DESTINATION bin )
13
13
endfunction ()
14
14
15
+ macro (mmdeploy_add_net NAME )
16
+ if (MMDEPLOY_DYNAMIC_BACKEND )
17
+ mmdeploy_add_library (${NAME} SHARED ${ARGN} )
18
+ target_link_libraries (${PROJECT_NAME} PRIVATE mmdeploy )
19
+ set (BACKEND_LIB_NAMES ${BACKEND_LIB_NAMES} ${PROJECT_NAME} PARENT_SCOPE )
20
+ else ()
21
+ mmdeploy_add_module (${NAME} ${ARGN} )
22
+ endif ()
23
+ endmacro ()
15
24
16
25
function (mmdeploy_add_library NAME )
17
26
# EXCLUDE: exclude from registering & exporting
Original file line number Diff line number Diff line change 1
1
// Copyright (c) OpenMMLab. All rights reserved.
2
2
3
- #include <Windows.h>
4
-
3
+ #include <string>
5
4
#include <cstdio>
6
5
6
+ #ifdef _WIN32
7
+ #include <Windows.h>
8
+ #else
9
+ #include <dlfcn.h>
10
+ #endif
11
+
12
+ #ifdef _WIN32
13
+ #define LIBPREFIX ""
14
+ #define LIBSUFFIX ".dll"
15
+ #elif defined(__APPLE__)
16
+ #define LIBPREFIX "lib"
17
+ #define LIBSUFFIX ".dylib"
18
+ #else
19
+ #define LIBPREFIX "lib"
20
+ #define LIBSUFFIX ".so"
21
+ #endif
22
+
7
23
namespace mmdeploy {
8
24
namespace {
9
25
10
26
void* mmdeploy_load_library(const char* name) {
11
27
fprintf(stderr, "loading %s ...\n", name);
12
- auto handle = LoadLibraryA(name);
28
+
29
+ #ifdef _WIN32
30
+ auto handle = LoadLibraryExA(name, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
31
+ #else
32
+ auto handle = dlopen(name, RTLD_NOW | RTLD_GLOBAL);
33
+ #endif
34
+
13
35
if (!handle) {
14
36
fprintf(stderr, "failed to load library %s\n", name);
15
37
return nullptr;
@@ -26,7 +48,8 @@ class Loader {
26
48
@_MMDEPLOY_DYNAMIC_MODULES@
27
49
};
28
50
for (const auto name : modules) {
29
- mmdeploy_load_library(name);
51
+ std::string libname = std::string{} + LIBPREFIX + name + LIBSUFFIX;
52
+ mmdeploy_load_library(libname.c_str());
30
53
}
31
54
}
32
55
};
Original file line number Diff line number Diff line change @@ -80,5 +80,14 @@ if (MMDEPLOY_BUILD_SDK_CSHARP_API OR MMDEPLOY_BUILD_SDK_MONOLITHIC)
80
80
set_target_properties (mmdeploy PROPERTIES
81
81
VERSION ${MMDEPLOY_VERSION}
82
82
SOVERSION ${MMDEPLOY_VERSION_MAJOR} )
83
+ if (APPLE )
84
+ set_target_properties (mmdeploy PROPERTIES
85
+ INSTALL_RPATH "@loader_path"
86
+ BUILD_RPATH "@loader_path" )
87
+ else ()
88
+ set_target_properties (mmdeploy PROPERTIES
89
+ INSTALL_RPATH "\$ ORIGIN"
90
+ BUILD_RPATH "\$ ORIGIN" )
91
+ endif ()
83
92
mmdeploy_export (mmdeploy )
84
93
endif ()
Original file line number Diff line number Diff line change 2
2
3
3
project (mmdeploy_net_module )
4
4
5
+ set (BACKEND_LIB_NAMES )
6
+
5
7
if ("trt" IN_LIST MMDEPLOY_TARGET_BACKENDS )
6
8
add_subdirectory (trt )
7
9
endif ()
@@ -46,5 +48,22 @@ if ("rknn" IN_LIST MMDEPLOY_TARGET_BACKENDS)
46
48
add_subdirectory (rknn )
47
49
endif ()
48
50
49
- mmdeploy_add_module (${PROJECT_NAME} net_module.cpp )
51
+ if (MMDEPLOY_DYNAMIC_BACKEND )
52
+ set (_MODULE_STR ${BACKEND_LIB_NAMES} )
53
+ list (TRANSFORM _MODULE_STR REPLACE "(.+)" "\"\\ 1\" " )
54
+ string (JOIN ",\n " _MODULE_STR ${_MODULE_STR} )
55
+ set (_MMDEPLOY_DYNAMIC_MODULES ${_MODULE_STR} )
56
+
57
+ set (_LOADER_NAME net_loader )
58
+ set (_LOADER_PATH ${${PROJECT_NAME}_BINARY_DIR}/${_LOADER_NAME}.cpp )
59
+ configure_file (
60
+ ${CMAKE_SOURCE_DIR} /cmake/loader.cpp.in
61
+ ${_LOADER_PATH} )
62
+ if (NOT (WIN32 OR APPLE ))
63
+ SET (_DL_LIB dl )
64
+ endif ()
65
+ endif ()
66
+
67
+ mmdeploy_add_module (${PROJECT_NAME} net_module.cpp ${_LOADER_PATH} )
68
+ target_link_libraries (${PROJECT_NAME} PUBLIC ${_DL_LIB} )
50
69
add_library (mmdeploy::net_module ALIAS ${PROJECT_NAME} )
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ project(mmdeploy_coreml_net)
5
5
if ("cpu" IN_LIST MMDEPLOY_TARGET_DEVICES )
6
6
find_library (CORE_ML CoreML )
7
7
find_library (FOUNDATION Foundation )
8
- mmdeploy_add_module (${PROJECT_NAME} coreml_net.mm )
8
+ mmdeploy_add_net (${PROJECT_NAME} coreml_net.mm )
9
9
target_include_directories (${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} )
10
10
target_link_libraries (${PROJECT_NAME} PRIVATE ${CORE_ML} ${FOUNDATION} )
11
11
add_library (mmdeploy::coreml_net ALIAS ${PROJECT_NAME} )
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ if ("cpu" IN_LIST MMDEPLOY_TARGET_DEVICES)
6
6
7
7
find_package (ncnn REQUIRED )
8
8
9
- mmdeploy_add_module (${PROJECT_NAME} ncnn_net.cpp )
9
+ mmdeploy_add_net (${PROJECT_NAME} ncnn_net.cpp )
10
10
target_link_libraries (${PROJECT_NAME} PRIVATE mmdeploy_ncnn_ops_obj )
11
11
target_link_libraries (${PROJECT_NAME} PRIVATE ncnn )
12
12
add_library (mmdeploy::ncnn_net ALIAS ${PROJECT_NAME} )
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ project(mmdeploy_openvino_net)
5
5
if ("cpu" IN_LIST MMDEPLOY_TARGET_DEVICES )
6
6
find_package (InferenceEngine REQUIRED )
7
7
8
- mmdeploy_add_module (${PROJECT_NAME} openvino_net.cpp )
8
+ mmdeploy_add_net (${PROJECT_NAME} openvino_net.cpp )
9
9
target_link_libraries (${PROJECT_NAME} PRIVATE
10
10
${InferenceEngine_LIBRARIES} )
11
11
add_library (mmdeploy::openvino_net ALIAS ${PROJECT_NAME} )
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ project(mmdeploy_ort_net)
4
4
5
5
include (${CMAKE_SOURCE_DIR} /cmake/modules/FindONNXRUNTIME.cmake )
6
6
7
- mmdeploy_add_module (${PROJECT_NAME} ort_net.cpp )
7
+ mmdeploy_add_net (${PROJECT_NAME} ort_net.cpp )
8
8
target_include_directories (${PROJECT_NAME} PRIVATE ${ONNXRUNTIME_DIR} /include )
9
9
target_link_libraries (${PROJECT_NAME} PRIVATE mmdeploy_onnxruntime_ops_obj )
10
10
target_link_libraries (${PROJECT_NAME} PUBLIC onnxruntime )
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ if (MMDEPLOY_TORCHSCRIPT_SDK_BACKEND)
8
8
find_package (Torch REQUIRED )
9
9
find_package (TorchVision QUIET )
10
10
11
- mmdeploy_add_module (${PROJECT_NAME} torch_net.cpp )
11
+ mmdeploy_add_net (${PROJECT_NAME} torch_net.cpp )
12
12
13
13
target_link_libraries (${PROJECT_NAME} PRIVATE
14
14
${TORCH_LIBRARIES} )
You can’t perform that action at this time.
0 commit comments