Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 4a84f51

Browse files
authored
Separate dynamic loading from static linking (#3456)
* Separate dynamic loading from static linking * Missed find_my_pathname
1 parent d789370 commit 4a84f51

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ option(NGRAPH_JSON_ENABLE "Enable JSON based serialization and tracing features"
185185
option(NGRAPH_STATIC_LIB_ENABLE "Enable build nGraph static library" FALSE)
186186
option(NGRAPH_INTERPRETER_STATIC_LIB_ENABLE "Enable build INTERPRETER backend static library" FALSE)
187187
option(NGRAPH_CPU_STATIC_LIB_ENABLE "Enable build CPU backend static library" FALSE)
188+
option(NGRAPH_DYNAMIC_COMPONENTS_ENABLE "Enable dynamic loading of components" TRUE)
188189

189190
if (NGRAPH_CPU_ENABLE
190191
AND
@@ -263,6 +264,7 @@ NORMALIZE_BOOL(NGRAPH_JSON_ENABLE)
263264
NORMALIZE_BOOL(NGRAPH_STATIC_LIB_ENABLE)
264265
NORMALIZE_BOOL(NGRAPH_INTERPRETER_STATIC_LIB_ENABLE)
265266
NORMALIZE_BOOL(NGRAPH_CPU_STATIC_LIB_ENABLE)
267+
NORMALIZE_BOOL(NGRAPH_DYNAMIC_COMPONENTS_ENABLE)
266268

267269
message(STATUS "NGRAPH_UNIT_TEST_ENABLE: ${NGRAPH_UNIT_TEST_ENABLE}")
268270
message(STATUS "NGRAPH_TOOLS_ENABLE: ${NGRAPH_TOOLS_ENABLE}")
@@ -289,6 +291,7 @@ message(STATUS "NGRAPH_JSON_ENABLE: ${NGRAPH_JSON_ENABLE}")
289291
message(STATUS "NGRAPH_STATIC_LIB_ENABLE: ${NGRAPH_STATIC_LIB_ENABLE}")
290292
message(STATUS "NGRAPH_INTERPRETER_STATIC_LIB_ENABLE: ${NGRAPH_INTERPRETER_STATIC_LIB_ENABLE}")
291293
message(STATUS "NGRAPH_CPU_STATIC_LIB_ENABLE: ${NGRAPH_CPU_STATIC_LIB_ENABLE}")
294+
message(STATUS "NGRAPH_DYNAMIC_COMPONENTS_ENABLE: ${NGRAPH_DYNAMIC_COMPONENTS_ENABLE}")
292295

293296
#-----------------------------------------------------------------------------------------------
294297
# Installation logic...
@@ -402,6 +405,10 @@ if (NGRAPH_STATIC_LIB_ENABLE)
402405
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_STATIC_LIB_ENABLE")
403406
endif()
404407

408+
if (NGRAPH_DYNAMIC_COMPONENTS_ENABLE)
409+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNGRAPH_DYNAMIC_COMPONENTS_ENABLE")
410+
endif()
411+
405412
if (NGRAPH_PLAIDML_ENABLE)
406413
find_package(PlaidML CONFIG)
407414
if (NOT PLAIDML_FOUND)

src/ngraph/runtime/backend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ std::string runtime::Backend::s_backend_shared_library_search_directory;
3737
// This finds the full path of the containing shared library
3838
static string find_my_pathname()
3939
{
40-
#ifdef NGRAPH_STATIC_LIB_ENABLE
41-
return "";
42-
#else
40+
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
4341
#ifdef _WIN32
4442
HMODULE hModule = GetModuleHandleW(L"ngraph.dll");
4543
WCHAR wpath[MAX_PATH];
@@ -55,6 +53,8 @@ static string find_my_pathname()
5553
dladdr(reinterpret_cast<void*>(find_my_pathname), &dl_info);
5654
return dl_info.dli_fname;
5755
#endif
56+
#else
57+
return "";
5858
#endif
5959
}
6060

src/ngraph/runtime/backend_manager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
using namespace std;
3333
using namespace ngraph;
3434

35-
#ifdef NGRAPH_STATIC_LIB_ENABLE
36-
#define DLERROR() ""
37-
#else
35+
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
3836
#ifdef _WIN32
3937
#define CLOSE_LIBRARY(a) FreeLibrary(a)
4038
#define DLSYM(a, b) GetProcAddress(a, b)
@@ -44,6 +42,8 @@ using namespace ngraph;
4442
#define DLSYM(a, b) dlsym(a, b)
4543
#define DLERROR() dlerror()
4644
#endif
45+
#else
46+
#define DLERROR() ""
4747
#endif
4848

4949
unordered_map<string, runtime::BackendConstructor*>& runtime::BackendManager::get_registry()
@@ -107,7 +107,7 @@ shared_ptr<runtime::Backend> runtime::BackendManager::create_backend(const std::
107107
}
108108
else
109109
{
110-
#ifndef NGRAPH_STATIC_LIB_ENABLE
110+
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
111111
DL_HANDLE handle = open_shared_library(type);
112112
if (!handle)
113113
{
@@ -153,7 +153,7 @@ DL_HANDLE runtime::BackendManager::open_shared_library(string type)
153153
string lib_suffix = SHARED_LIB_SUFFIX;
154154

155155
DL_HANDLE handle = nullptr;
156-
#ifndef NGRAPH_STATIC_LIB_ENABLE
156+
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
157157

158158
// strip off attributes, IE:CPU becomes IE
159159
auto colon = type.find(":");
@@ -190,7 +190,7 @@ DL_HANDLE runtime::BackendManager::open_shared_library(string type)
190190
map<string, string> runtime::BackendManager::get_registered_device_map()
191191
{
192192
map<string, string> rc;
193-
#ifndef NGRAPH_STATIC_LIB_ENABLE
193+
#ifdef NGRAPH_DYNAMIC_COMPONENTS_ENABLE
194194
string my_directory =
195195
file_util::get_directory(Backend::get_backend_shared_library_search_directory());
196196
vector<string> backend_list;

0 commit comments

Comments
 (0)