Skip to content

Commit 4043fb5

Browse files
committed
Add support for shared library build
1 parent 90add1d commit 4043fb5

File tree

5 files changed

+111
-16
lines changed

5 files changed

+111
-16
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ bazel-spirv-reflect
77
bazel-SPIRV-Reflect
88
bazel-testlogs
99
/.vs
10-
/.vscode
10+
/.vscode
11+
12+
# CLion-specific:
13+
/.idea
14+
/cmake-build*

CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.5)
22

33
project(spirv-reflect)
44

5+
OPTION(BUILD_SHARED_LIBS "Build shared library" OFF)
56
OPTION(SPIRV_REFLECT_EXECUTABLE "Build spirv-reflect executable" ON)
67

78
OPTION(SPIRV_REFLECT_STATIC_LIB "Build a SPIRV-Reflect static library" OFF)
@@ -12,14 +13,15 @@ OPTION(SPIRV_REFLECT_ENABLE_ASAN "Use address sanitization" OFF)
1213
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1314
set(CMAKE_CXX_STANDARD 14)
1415

15-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
1616

1717
if (SPIRV_REFLECT_ENABLE_ASAN)
1818
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
1919
add_link_options(-fsanitize=address)
2020
endif()
2121

2222
if (SPIRV_REFLECT_EXECUTABLE)
23+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
24+
2325
# ==========================================================================
2426
# Compile spirv_reflect.c as C
2527
# ==========================================================================
@@ -118,3 +120,18 @@ if(SPIRV_REFLECT_STATIC_LIB)
118120
ARCHIVE DESTINATION lib)
119121
endif()
120122

123+
if(BUILD_SHARED_LIBS)
124+
add_library(spirv-reflect SHARED ${CMAKE_CURRENT_SOURCE_DIR}/spirv_reflect.h
125+
${CMAKE_CURRENT_SOURCE_DIR}/spirv_reflect.c)
126+
127+
target_include_directories(spirv-reflect
128+
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
129+
130+
target_compile_definitions(spirv-reflect PUBLIC SPIRV_REFLECT_SHARED)
131+
target_compile_options(spirv-reflect PRIVATE
132+
$<$<CXX_COMPILER_ID:GNU>:-fvisibility=hidden>)
133+
134+
install(TARGETS spirv-reflect
135+
LIBRARY DESTINATION lib
136+
ARCHIVE DESTINATION lib)
137+
endif ()

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ If the project wants to use it's own SPIRV-Header path, it can set `SPIRV_REFLEC
4949
target_compile_definitions(project_name PUBLIC SPIRV_REFLECT_USE_SYSTEM_SPIRV_H)
5050
```
5151

52+
### Building a shared library
53+
54+
To facilitate integration into managed languages with FFI support (such as Java and Python),
55+
this project could be built as a shared library. This mode is controlled by the `BUILD_SHARED_LIBS`
56+
CMake option:
57+
58+
```shell
59+
mkdir build && cd build
60+
cmake -DBUILD_SHARED_LIBS=ON -DSPIRV_REFLECT_EXECUTABLE=OFF ..
61+
make
62+
```
63+
64+
Please note that shared library mode is not intended for anything else but this. **Especially** it
65+
is not intended for the regular use case of the shared libraries: packaging and shipping a single
66+
global library for all applications in the system. Header files of SPIRV-Reflect expose several C
67+
structures that you are supposed to allocate by yourself without any way of communicating the
68+
actual runtime structure size. Therefore, you must treat the resulting artifact in the same way
69+
you are dealing with static libraries: runtime library version must **exactly** match the version
70+
of `spirv_reflect.h` file you used to compile your app or generate its FFI bindings.
71+
5272
## Building Samples
5373

5474
**This step is only necessary when building/running SPIRV-Reflect's example applications.**

spirv_reflect.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17+
#define SPIRV_REFLECT_IMPLEMENTATION // turn dllimports into dllexports
1718
#include "spirv_reflect.h"
1819

1920
#include <assert.h>

0 commit comments

Comments
 (0)