Skip to content

Commit 17cf27b

Browse files
committed
multi platform shared library
1 parent d981a78 commit 17cf27b

File tree

5 files changed

+204
-1
lines changed

5 files changed

+204
-1
lines changed

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,33 @@ message(STATUS "Building xplugin v${${PROJECT_NAME}_VERSION}")
3535

3636
set(XPLUGIN_HEADERS
3737
${XPLUGIN_INCLUDE_DIR}/xplugin/xplugin_config.hpp
38+
${XPLUGIN_INCLUDE_DIR}/xplugin/xshared_library.hpp
3839
)
3940

4041
set(XPLUGIN_SOURCES
42+
${XPLUGIN_SOURCE_DIR}/xunix_shared_library.cpp
43+
${XPLUGIN_SOURCE_DIR}/xwindows_shared_library.cpp
4144
)
4245

4346
# Output
4447
# ======
48+
49+
add_library(xplugin SHARED ${XPLUGIN_SOURCES} ${XPLUGIN_HEADERS})
50+
51+
if (APPLE)
52+
set_target_properties(xplugin PROPERTIES
53+
MACOSX_RPATH ON
54+
)
55+
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib; ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
56+
else()
57+
set_target_properties(xplugin PROPERTIES
58+
BUILD_WITH_INSTALL_RPATH 1
59+
)
60+
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib; ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
61+
set(CMAKE_SKIP_BUILD_RPATH FALSE)
62+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
63+
endif()
64+
65+
target_include_directories(xplugin PUBLIC $<BUILD_INTERFACE:${XPLUGIN_INCLUDE_DIR}>
66+
$<INSTALL_INTERFACE:include>)
67+

include/xplugin/xplugin_config.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#define XPLUGIN_API
2121
#endif
2222

23-
// Project version
2423
#define XPLUGIN_VERSION_MAJOR 0
2524
#define XPLUGIN_VERSION_MINOR 0
2625
#define XPLUGIN_VERSION_PATCH 0

include/xplugin/xshared_library.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/***************************************************************************
2+
* Copyright (c) 2018, Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#include <string>
11+
12+
#include "xplugin_config.hpp"
13+
14+
namespace xp
15+
{
16+
class XPLUGIN_API xshared_library
17+
{
18+
public:
19+
20+
virtual ~xshared_library() {}
21+
22+
xshared_library(const xshared_library&) = delete;
23+
xshared_library& operator=(const xshared_library&) = delete;
24+
25+
xshared_library(xshared_library&&) = delete;
26+
xshared_library& operator=(xshared_library&&) = delete;
27+
28+
static xshared_library* open(const std::string& name);
29+
static void close(xshared_library* library);
30+
static const char* get_error();
31+
32+
template <class T>
33+
T find_symbol(const std::string& name);
34+
35+
protected:
36+
37+
xshared_library() {}
38+
39+
private:
40+
41+
virtual void* find_symbol(const std::string& name) = 0;
42+
};
43+
44+
template <class T>
45+
inline T xshared_library::find_symbol(const std::string& name)
46+
{
47+
return reinterpret_cast<T>(name);
48+
}
49+
50+
inline void xshared_library::close(xshared_library* library)
51+
{
52+
delete library;
53+
}
54+
}
55+

src/xunix_shared_library.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/***************************************************************************
2+
* Copyright (c) 2018, Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#if (defined(__linux__) || defined(__unix__) || defined(__APPLE__))
11+
12+
#include <dlfcn.h>
13+
#include "xplugin/xshared_library.hpp"
14+
15+
namespace xp
16+
{
17+
class xunix_shared_library : public xshared_library
18+
{
19+
public:
20+
21+
xunix_shared_library(void* handle);
22+
virtual ~xunix_shared_library();
23+
24+
private:
25+
26+
void* find_symbol(const std::string& name);
27+
28+
void* m_handle;
29+
};
30+
31+
xunix_shared_library::xunix_shared_library(void* handle)
32+
: m_handle(handle)
33+
{
34+
}
35+
36+
xunix_shared_library::~xunix_shared_library()
37+
{
38+
dlclose(m_handle);
39+
m_handle = nullptr;
40+
}
41+
42+
void* xunix_shared_library::find_symbol(const std::string& name)
43+
{
44+
void* sym = dlsym(m_handle, name.c_str());
45+
return sym;
46+
}
47+
48+
const char* xshared_library::get_error()
49+
{
50+
return dlerror();
51+
}
52+
53+
xshared_library* xshared_library::open(const std::string& name)
54+
{
55+
void* handle = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL);
56+
return handle ? new xunix_shared_library(handle) : nullptr;
57+
}
58+
}
59+
60+
#endif

src/xwindows_shared_library.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/***************************************************************************
2+
* Copyright (c) 2018, Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#ifdef _WIN32
11+
12+
#include <windows.h>
13+
#include "xplugin/xshared_library.hpp"
14+
15+
namespace xp
16+
{
17+
class xwindows_shared_library : public xshared_library
18+
{
19+
public:
20+
21+
private:
22+
23+
xwindows_shared_library(HINSTANCE handle);
24+
virtual ~xwindows_shared_library();
25+
26+
private:
27+
28+
void* find_symbol(const std::string& name);
29+
30+
HINSTANCE m_handle;
31+
};
32+
33+
xwindows_shared_library::xwindows_shared_library(HINSTANCE handle)
34+
: m_handle(handle)
35+
{
36+
}
37+
38+
xwindows_shared_library::~xwindows_shared_library()
39+
{
40+
FreeLibrary(m_handle);
41+
}
42+
43+
void* xindows_shared_library::find_symbol(const std::string& name)
44+
{
45+
void* sym = GetProcAddress(name.c_str());
46+
return sym;
47+
}
48+
49+
const char* xwindows_shared_library::get_error()
50+
{
51+
char buffer[255];
52+
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),
53+
0, buffer,sizeof(buffer),0);
54+
return buffer;
55+
}
56+
57+
xshared_library* xshared_library::open(const std::string& name)
58+
{
59+
HINSTANCE handle = LoadLibrary(name.c_str());
60+
return handle ? new xwindows_shared_library(handle) : nullptr;
61+
}
62+
63+
}
64+
65+
#endif
66+

0 commit comments

Comments
 (0)