widberg / bgfx.cmake

https://github.com/bkaradzic/bgfx.cmake. Independently maintained CMake build scripts for bgfx. Released under public domain.
https://github.com/bkaradzic/bgfx.cmake
Creative Commons Zero v1.0 Universal
286 stars 256 forks source link

shared lib #21

Closed labuzm closed 4 years ago

labuzm commented 6 years ago

Hello, do You have any advices for building bgfx as a shared library? I did something like this:

ExternalProject_Add(
    bgfx
    CMAKE_ARGS
    "-DBUILD_SHARED_LIBS=1"
    "-DCMAKE_POSITION_INDEPENDENT_CODE=1"
    ...
)

but it causes shaderc and other tools to be built with dynamic dependences which I would like to avoid. Currently I'm hacking it around setting BUILD_SHARED_LIBS=0 in the sharderc.cmake. Do you know any cleaner and more elegant solution?

JoshuaBrookover commented 6 years ago

A bit on building BGFX as a shared lib first: As far as I'm aware, bgfx does not support being built as a shared library unless you also intend to use the C99 API. In particular, it lacks the dllimport/dllexport statements necessary to build a DLL on Windows. If it weren't for this, I would have properly supported shared libs in bgfx.cmake a while ago. It may be possible to circumvent this using WINDOWS_EXPORT_ALL_SYMBOLS, but I haven't tried.

Now to your exact question, I'm not entirely clear on what you're asking. If you built bgfx as a shared library, then all the tools would need to link against it dynamically. I don't understand how BUILD_SHARED_LIBS being set to 0 is getting around the issue.

I believe the proper way to accomplish this would be to build bgfx as both a shared library and a static library, and attempt generating exports for Windows (if that becomes a necessity, possibly using that cmake flag). The lazy way to do this is to add two add_library statements both with the CMake sources (one SHARED and one STATIC), but the slightly cleaner way would be something like:

add_library( bgfx OBJECT ${BGFX_SOURCES} )
add_library( bgfx_shared SHARED $<TARGET_OBJECTS:bgfx> )
add_library( bgfx_static STATIC $<TARGET_OBJECTS:bgfx> )

With that you might also have to go through and fix all the references to bgfx throughout the cmake scripts, as well as make the tools explicitly use bgfx_static.

Depending on your exact use case there might be a better solution. Hopefully something here was useful.

labuzm commented 6 years ago

As for now I'm focusing only on Linux and yes I do use C99 API, sorry for not mentioning that.

In fact I did build bgfx successfully as a shared lib, the problem I had was with shaderc. Setting BUILD_SHARED_LIBS flag globally would cause all libs to be built as shared unless the lib was explicitly added as a static. This would also affect shaderc's dependencies (glsl-optimizer and others), so I ended up with a binary with dependencies in non standard location where linker could not find them (cmake strips rpath during the install step). Basically I tried to compile bgfx as a shared lib and sharderc as statically linked binary without modifying your code, but it turned out it would be easier to tweak cmake scripts according to my needs. Thank you very much for your help and for this awesome project!

JoshuaBrookover commented 6 years ago

So if I understand correctly, you are looking for some change we could make to bgfx.cmake so you don't need your own branch?

labuzm commented 6 years ago

Yes, that's right.

JoshuaBrookover commented 6 years ago

I might give this a go at some point. I definitely see the value in what you're asking for.

jpvanoosten commented 4 years ago

Is it not restrictive to require the BUILD_SHARED_LIBS property to be OFF to use this project? Wouldn't it be better to explicitly mark libs as STATIC if they shouldn't be compiled as shared libraries? Or am I missing something?

https://github.com/JoshuaBrookover/bgfx.cmake/blob/83f791ee15a494d697f9cadae3cf9678b9a282ef/cmake/bgfx.cmake#L38 https://github.com/JoshuaBrookover/bgfx.cmake/blob/83f791ee15a494d697f9cadae3cf9678b9a282ef/cmake/3rdparty/glsl-optimizer.cmake#L30 https://github.com/JoshuaBrookover/bgfx.cmake/blob/83f791ee15a494d697f9cadae3cf9678b9a282ef/cmake/3rdparty/glsl-optimizer.cmake#L42 https://github.com/JoshuaBrookover/bgfx.cmake/blob/83f791ee15a494d697f9cadae3cf9678b9a282ef/cmake/3rdparty/glsl-optimizer.cmake#L56

Just to name a few...

You said "bgfx does not support being built as a shared library" so why didn't you mark those libraries explicitly as STATIC?

bwrsandman commented 4 years ago

I have no problem building BGFX as a shared library on Linux. It's more typical to have shared libraries on Linux than it is on Windows. Plus, those using the c99 api on would not complain. It would be nice to have the option. This would require adding a ${BGFX_LIBRARY_TYPE} option defaulting to STATIC but with SHARED option, then adding it to the add_library statements.

Additionally, there is support for shared. https://github.com/bkaradzic/bgfx/blob/bbd41124b7069bf259096588d1cc1d82fd74d328/scripts/bgfx.lua#L42-L50