DirectXShaderCompiler
DirectXShaderCompiler copied to clipboard
How can I build `dxil` and `dxcompiler` as a static library?
I know that it would be best to leave them as DLLs so that a driver upgrade of Windows SDK update can upgrade and "fix" my compiler.
However I'm mostly targetting SPIR-V and running the compilation at runtime and I have a habit of static linking all my depdendencies into the Engine (even if the engine itself is a DLL).
Anyone tried this before? Especially on Windows?
It seems "pretty trivial" to achieve this, and is especially relevant in Rust land where we "prefer" to link as many dependencies statically for "self-contained" and redistributable artifacts: https://github.com/Traverse-Research/hassle-rs/issues/57 (as much as dynamic libstdc++ counts towards that :sweat_smile:).
The simple and obvious way to get a static dxcompiler is by dropping SHARED:
https://github.com/microsoft/DirectXShaderCompiler/blob/667fb773cc2fb40bc31dba6d53e5230a196838ff/tools/clang/tools/dxcompiler/CMakeLists.txt#L127
Or even better, keeping SHARED and adding STATIC to get a libdxcompiler.so and libdxcompiler_static.a at the same time.
Would it be okay to commit this to the repo, optionally hide it behind an option()?
Then, two problems remain:
- CMake expects consumers of this static library to be aware of some "config" it can generate, denoting many other (mostly static) library dependencies that also have to be linked, rather than providing one massive
archive with all that has been generated tobuild/lib. Can we re-archive that to provide a (mostly) self-supporting.a? DllMainhas to be called. By keeping this symbol alive (without calling into it), theconstructorattribute persists so that the ELF loader calls it.
And depending on the situation, dynamic libstdc++ may or may not be desired (but static std is hard IIRC).
CC @pow2clk, you're probably the right person to ping about all this :grimacing:
(Note that most of this is written with a Linux hat on, but I imagine it being similar on Windows albeit different terminology/filenames)
In the end we've had to go shared because DXC doesn't compile under C++20 std option
https://github.com/hexops/mach-dxcompiler
~Additionally if you don't need DXIL.dll:~ ~https://github.com/Oxsomi/DirectXShaderCompiler/commit/df5bf3ae06caf0aa06e2b90da1b8a8b3c190d413 this shows you exactly how to build dxcompiler.lib and disable all CLI projects so they don't whine about anything.~ ~Flags are over here: https://github.com/Oxsomi/core3/blob/542aa674628cc8b2b1a472e5491ce971cc9a89bf/CMakeLists.txt#L666~ Actually, it seems like this works fine on windows, but somehow this completely breaks on linux. Oh joy.