From 87c5cf3b48300cb046aa66bb9075c22ff4afc64a Mon Sep 17 00:00:00 2001 From: Eremey Valetov Date: Mon, 4 May 2026 16:45:20 -0400 Subject: [PATCH] Windows MSVC build: more compat-layer fixes Round 2 of c67b631 cleanup. After the dirent + utime fixes, the MSVC link surface still had: - LNK2005 'fopen already defined': dropped g_fopen so we no longer override the SDK's fopen. UTF-8 paths still work on Windows 10 with the active-codepage manifest; non-Unicode codepages will see ANSI translation. This is good enough for the public release; a full UTF-8 fopen wrapper can be added later if needed. - LNK2019 'unresolved S_ISDIR / S_ISREG': MSVC's defines _S_IFDIR / _S_IFREG but not the POSIX S_IS* macros. Add them in the unistd.h shim (which main.c already pulls). - LNK1181 'cannot open input file m.lib': test_merkle and test_rans linked libm unconditionally. Math is in the default CRT on MSVC; link 'm' only on non-Windows. - 'unistd.h' not found in test_blockstore.c: it actually only needs getpid(). Use + #define getpid _getpid on MSVC, keep elsewhere. --- cli/CMakeLists.txt | 2 +- cli/src/compat/include/msvc/unistd.h | 8 ++++++++ tests/CMakeLists.txt | 12 ++++++++++-- tests/src/test_blockstore.c | 5 +++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 4f4b67c..e2ddb55 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -29,7 +29,7 @@ if(WIN32) NO_OLDNAMES g_err g_errx g_warn g_warnx g_vwarn g_vwarnx g_verr g_verrx g_getprogname g_setlinebuf g_fnmatch - g_compat__utf8_console g_compat__wpath g_fopen + g_compat__utf8_console g_compat__wpath g_access g_unlink g_chdir g_mkdir g_chmod g_utime g_opendir ) diff --git a/cli/src/compat/include/msvc/unistd.h b/cli/src/compat/include/msvc/unistd.h index 6a8f8ee..52d380e 100644 --- a/cli/src/compat/include/msvc/unistd.h +++ b/cli/src/compat/include/msvc/unistd.h @@ -24,6 +24,14 @@ #define PATH_MAX 260 #endif +#include +#ifndef S_ISDIR +#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) +#endif +#ifndef S_ISREG +#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) +#endif + /* Provided by compat_win32.c (UTF-8-aware via wide-char APIs) */ int access(const char *path, int mode); int unlink(const char *path); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ae08797..e6bdaaf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -52,7 +52,11 @@ target_compile_features(test_cdc PRIVATE c_std_99) add_test(NAME cdc COMMAND test_cdc) add_executable(test_merkle src/test_merkle.c) -target_link_libraries(test_merkle PRIVATE uc2 m) +if(WIN32) + target_link_libraries(test_merkle PRIVATE uc2) +else() + target_link_libraries(test_merkle PRIVATE uc2 m) +endif() target_include_directories(test_merkle PRIVATE "${PROJECT_BINARY_DIR}/lib") target_compile_features(test_merkle PRIVATE c_std_99) add_test(NAME merkle COMMAND test_merkle) @@ -76,7 +80,11 @@ target_compile_features(test_delta PRIVATE c_std_99) add_test(NAME delta COMMAND test_delta) add_executable(test_rans src/test_rans.c) -target_link_libraries(test_rans PRIVATE uc2 m) +if(WIN32) + target_link_libraries(test_rans PRIVATE uc2) +else() + target_link_libraries(test_rans PRIVATE uc2 m) +endif() target_include_directories(test_rans PRIVATE "${PROJECT_BINARY_DIR}/lib") target_compile_features(test_rans PRIVATE c_std_99) add_test(NAME rans COMMAND test_rans) diff --git a/tests/src/test_blockstore.c b/tests/src/test_blockstore.c index ac875ea..6442cfd 100644 --- a/tests/src/test_blockstore.c +++ b/tests/src/test_blockstore.c @@ -4,7 +4,12 @@ #include #include #include +#ifdef _MSC_VER +#include +#define getpid _getpid +#else #include +#endif #include #include