Skip to content

Commit

Permalink
Add support for shared library build
Browse files Browse the repository at this point in the history
  • Loading branch information
makkarpov committed Jul 27, 2024
1 parent 90add1d commit 4043fb5
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 16 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ bazel-spirv-reflect
bazel-SPIRV-Reflect
bazel-testlogs
/.vs
/.vscode
/.vscode

# CLion-specific:
/.idea
/cmake-build*
19 changes: 18 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.5)

project(spirv-reflect)

OPTION(BUILD_SHARED_LIBS "Build shared library" OFF)
OPTION(SPIRV_REFLECT_EXECUTABLE "Build spirv-reflect executable" ON)

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

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")

if (SPIRV_REFLECT_ENABLE_ASAN)
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address)
endif()

if (SPIRV_REFLECT_EXECUTABLE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")

# ==========================================================================
# Compile spirv_reflect.c as C
# ==========================================================================
Expand Down Expand Up @@ -118,3 +120,18 @@ if(SPIRV_REFLECT_STATIC_LIB)
ARCHIVE DESTINATION lib)
endif()

if(BUILD_SHARED_LIBS)
add_library(spirv-reflect SHARED ${CMAKE_CURRENT_SOURCE_DIR}/spirv_reflect.h
${CMAKE_CURRENT_SOURCE_DIR}/spirv_reflect.c)

target_include_directories(spirv-reflect
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

target_compile_definitions(spirv-reflect PUBLIC SPIRV_REFLECT_SHARED)
target_compile_options(spirv-reflect PRIVATE
$<$<CXX_COMPILER_ID:GNU>:-fvisibility=hidden>)

install(TARGETS spirv-reflect
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
endif ()
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ If the project wants to use it's own SPIRV-Header path, it can set `SPIRV_REFLEC
target_compile_definitions(project_name PUBLIC SPIRV_REFLECT_USE_SYSTEM_SPIRV_H)
```

### Building a shared library

To facilitate integration into managed languages with FFI support (such as Java and Python),
this project could be built as a shared library. This mode is controlled by the `BUILD_SHARED_LIBS`
CMake option:

```shell
mkdir build && cd build
cmake -DBUILD_SHARED_LIBS=ON -DSPIRV_REFLECT_EXECUTABLE=OFF ..
make
```

Please note that shared library mode is not intended for anything else but this. **Especially** it
is not intended for the regular use case of the shared libraries: packaging and shipping a single
global library for all applications in the system. Header files of SPIRV-Reflect expose several C
structures that you are supposed to allocate by yourself without any way of communicating the
actual runtime structure size. Therefore, you must treat the resulting artifact in the same way
you are dealing with static libraries: runtime library version must **exactly** match the version
of `spirv_reflect.h` file you used to compile your app or generate its FFI bindings.

## Building Samples

**This step is only necessary when building/running SPIRV-Reflect's example applications.**
Expand Down
1 change: 1 addition & 0 deletions spirv_reflect.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
*/

#define SPIRV_REFLECT_IMPLEMENTATION // turn dllimports into dllexports
#include "spirv_reflect.h"

#include <assert.h>
Expand Down
Loading

0 comments on commit 4043fb5

Please sign in to comment.