blitz-research / dawn

BSD 3-Clause "New" or "Revised" License
5 stars 2 forks source link

Static build option #3

Closed PZerua closed 1 month ago

PZerua commented 1 month ago

Hi!

I noticed there used to be a very handy dawn_static folder with a CMake that allowed a static build of Dawn which was great to improve build times, but was deleted a few months ago. I'm looking into updating Dawn to the latest version of your fork and I'm wondering if there's still a way to make a static build. I tried copying the dawn_static folder back, but could not manage to get it working.

Thanks!

blitz-research commented 1 month ago

Things got rearranged!

The cmake code that created a static lib got moved to a different project:

https://github.com/blitz-research/libsgd/blob/ed05764e80b53ec9a4a6a1f6495c539d0dfa7d0f/libsgd/CMakeLists.txt#L33

I also now use FetchContent to build dawn:

https://github.com/blitz-research/libsgd/blob/ed05764e80b53ec9a4a6a1f6495c539d0dfa7d0f/external/CMakeLists.txt#L9

This gives you a 'lib-dawn' cmake target you could use with the static lib bundler, eg:

create_bundled_lib(libdawn_static dawn_static lib-dawn)

If you're writing an exe, you can link directly with lib-dawn, eg:

target_link_libraries(myexe lib-dawn).

...although this means your cmake project may suddenly become much larger because it will include dawn, which is the downside of the FetchContent approach.

Note they made some changes to dawn's cmake lists that mean you may have to manually call dawnProcSetProcs(&dawn::native::GetProcs()); before creating the dawn instance. I'm still trying to find out what's going on there.

PZerua commented 1 month ago

Well, It took me more time than I'm willing to admit, but I managed to build it statically :)

Note they made some changes to dawn's cmake lists that mean you may have to manually call dawnProcSetProcs(&dawn::native::GetProcs()); before creating the dawn instance. I'm still trying to find out what's going on there.

Apparently the webgpu_dawn target is now built as a SHARED library because it uses BundleLibraries (here), which uses SHARED by default (here). Your create_bundled_lib only bundles static libs, so it never gets added. Changing the default to STATIC fixed it for me and removed the need to link against dawn_proc (so no dawnProcSetProcs needed). You can see the changes I did here (ignore the default dawn options).

You might notice I had to change the definition of CreateSwapchainWGPUTexture to use VkImage_T* as a parameter, otherwise it wouldn't find the function, because apparently dawn is changing the struct definition in vulkan_platform.h.

Thanks for the help!