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 <sys/stat.h> 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 <process.h> + #define getpid _getpid on MSVC, keep
  <unistd.h> elsewhere.
This commit is contained in:
Eremey Valetov
2026-05-04 16:45:20 -04:00
parent 345aabd423
commit 87c5cf3b48
4 changed files with 24 additions and 3 deletions
+1 -1
View File
@@ -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
)
+8
View File
@@ -24,6 +24,14 @@
#define PATH_MAX 260
#endif
#include <sys/stat.h>
#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);
+10 -2
View File
@@ -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)
+5
View File
@@ -4,7 +4,12 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef _MSC_VER
#include <process.h>
#define getpid _getpid
#else
#include <unistd.h>
#endif
#include <uc2/uc2_blockstore.h>
#include <uc2/uc2_merkle.h>