Skip to content

Commit

Permalink
Make: Fixed the stringzillite still having dependencies dlls on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ashbob999 committed Oct 26, 2024
1 parent c39c590 commit ecebb0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
10 changes: 0 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,6 @@ if(${STRINGZILLA_BUILD_SHARED})
"SZ_USE_ARM_NEON=1"
"SZ_USE_ARM_SVE=$<IF:$<CXX_COMPILER_ID:MSVC>,0,1>")
endif()

if (MSVC)
# Add dependencies for necessary runtime libraries in case of static linking
# This ensures that basic runtime functions are available:
# msvcrt.lib: Microsoft Visual C Runtime, required for basic C runtime functions on Windows.
# vcruntime.lib: Microsoft Visual C++ Runtime library for basic runtime functions.
# ucrt.lib: Universal C Runtime, necessary for linking basic C functions like I/O.
target_link_libraries(${target} PRIVATE msvcrt.lib vcruntime.lib ucrt.lib)
endif()

endfunction()

define_shared(stringzilla_shared)
Expand Down
25 changes: 22 additions & 3 deletions c/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ __attribute__((aligned(64))) static sz_implementations_t sz_dispatch_table;
* @brief Initializes a global static "virtual table" of supported backends
* Run it just once to avoiding unnecessary `if`-s.
*/
static void sz_dispatch_table_init(void) {
SZ_DYNAMIC void sz_dispatch_table_init(void) {
sz_implementations_t *impl = &sz_dispatch_table;
sz_capability_t caps = sz_capabilities();
sz_unused(caps); //< Unused when compiling on pre-SIMD machines.
Expand Down Expand Up @@ -232,9 +232,20 @@ static void sz_dispatch_table_init(void) {
}

#if defined(_MSC_VER)
#pragma section(".CRT$XCU", read)
__declspec(allocate(".CRT$XCU")) void (*_sz_dispatch_table_init)() = sz_dispatch_table_init;

/*
* Makes sure the sz_dispatch_table_init function is called at startup, from either an executable or when loading a DLL.
* The section name must be no more than 8 characters long, and must be between .CRT$XCA and .CRT$XCZ alphabetically
* (exclusive).
* The Mircrosft C++ compiler puts C++ initialisation code in .CRT$XCU, so avoid that section.
*
* Reference: https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170
*/
#pragma comment(linker, "/INCLUDE:_sz_dispatch_table_init")
#pragma section(".CRT$XCS", read)
__declspec(allocate(".CRT$XCS")) void (*_sz_dispatch_table_init)() = sz_dispatch_table_init;

// Called either from CRT code or out own _DLLMainCRTStartup, when a DLL is loaded.
BOOL WINAPI DllMain(HINSTANCE hints, DWORD forward_reason, LPVOID lp) {
switch (forward_reason) {
case DLL_PROCESS_ATTACH:
Expand All @@ -247,6 +258,14 @@ BOOL WINAPI DllMain(HINSTANCE hints, DWORD forward_reason, LPVOID lp) {
return TRUE;
}

#if SZ_AVOID_LIBC
// Called when the DLL is loaded, and ther is no CRT code.
BOOL WINAPI _DllMainCRTStartup(HINSTANCE hints, DWORD forward_reason, LPVOID lp) {
DllMain(hints, forward_reason, lp);
return TRUE;
}
#endif

#else
__attribute__((constructor)) static void sz_dispatch_table_init_on_gcc_or_clang(void) { sz_dispatch_table_init(); }
#endif
Expand Down

0 comments on commit ecebb0d

Please sign in to comment.