Make it possible to build SameBoy for Windows while linking against Windows' msvct.dll

This commit is contained in:
Lior Halphon 2025-03-23 23:18:17 +02:00
parent e2b22a0df6
commit 4e35048f46
6 changed files with 1410 additions and 9 deletions

View File

@ -322,6 +322,13 @@ CFLAGS += -Wno-deprecated-declarations
ifeq ($(PLATFORM),windows32)
CFLAGS += -Wno-deprecated-declarations # Seems like Microsoft deprecated every single LIBC function
LDFLAGS += -Wl,/NODEFAULTLIB:libcmt.lib
ifneq ($(USE_MSVCRT_DLL),)
CFLAGS += -D_NO_CRT_STDIO_INLINE -DUSE_MSVCRT_DLL
$(BIN)/SDL/sameboy.exe: $(OBJ)/Windows/msvcrt.lib
$(BIN)/SDL/sameboy_debugger.exe: $(OBJ)/Windows/msvcrt.lib
endif
endif
endif
@ -894,6 +901,10 @@ $(INC)/%.h: Core/%.h
-@$(MKDIR) -p $(dir $@)
sed "s/'/@SINGLE_QUOTE@/g" $^ | cppp $(CPPP_FLAGS) | sed "s/@SINGLE_QUOTE@/'/g" > $@
# Generate msvcrt.lib so we can use the always-present msvcrt.dll
$(OBJ)/Windows/msvcrt.lib: Windows/msvcrt.def
lib.exe /MACHINE:X64 /def:$< /out:$@
# Clean
clean:
rm -rf build

91
Windows/crt.c Executable file
View File

@ -0,0 +1,91 @@
#include <windows.h>
#include <stdio.h>
#ifdef USE_MSVCRT_DLL
/* Stub-out functions imported by VS's msvcrt.lib but are not implemented in msvcrt.dll */
/* We don't use exceptions, these are used by the uncaught exception handler and can be replaced with aborts */
void *__current_exception(void)
{
abort();
return NULL;
}
void *__current_exception_context(void)
{
abort();
return NULL;
}
/* terminate uses a mangled symbol on msvcrt.dll */
void msvcrt_terminate(void) __asm__("?terminate@@YAXXZ");
void terminate(void)
{
msvcrt_terminate();
}
/* We need to redirect these to msvcrt.dll's atexit */
int _crt_atexit(void (*function)(void))
{
return ((typeof(atexit) *)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "atexit"))(function);
}
int _register_onexit_function(void *table, void (*function)(void))
{
return ((typeof(atexit) *)GetProcAddress(GetModuleHandleA("msvcrt.dll"), "atexit"))(function);
}
/* Various imported function we don't need and can nop-out */
int *__p__commode(void)
{
static int dummy;
return &dummy;
}
int _configthreadlocale(int flag)
{
return 0;
}
errno_t _configure_narrow_argv(unsigned mode)
{
return 0;
}
char *_get_narrow_winmain_command_line(void)
{
return NULL;
}
int _initialize_narrow_environment()
{
return 0;
}
int _initialize_onexit_table(void *table)
{
return 0;
}
void _register_thread_local_exe_atexit_callback(const void *callback)
{
}
int _seh_filter_exe(unsigned exception_num, void *exception)
{
return 0;
}
void _set_app_type(unsigned type)
{
}
int _set_new_mode(int new_mode)
{
return 0;
}
#endif

18
Windows/math.c Executable file
View File

@ -0,0 +1,18 @@
#include <math.h>
#ifdef USE_MSVCRT_DLL
/* "Old" (Pre-2015) Windows headers/libc don't have round and exp2. */
__attribute__((no_builtin)) double round(double f)
{
return f >= 0? (int)(f + 0.5) : (int)(f - 0.5);
}
__attribute__((no_builtin)) double exp2(double f)
{
return pow(2, f);
}
#endif

View File

@ -1,9 +0,0 @@
#pragma once
#include_next <math.h>
#ifndef __MINGW32__
/* "Old" (Pre-2015) Windows headers/libc don't have round. */
static inline double round(double f)
{
return f >= 0? (int)(f + 0.5) : (int)(f - 0.5);
}
#endif

1276
Windows/msvcrt.def Normal file

File diff suppressed because it is too large Load Diff

14
Windows/stdio.c Executable file
View File

@ -0,0 +1,14 @@
#include <stdio.h>
#include <unistd.h>
#ifdef USE_MSVCRT_DLL
FILE *__acrt_iob_func(unsigned index)
{
static FILE *files[3];
if (index > sizeof(files) / sizeof(files[0])) return NULL;
if (files[index]) return files[index];
return (files[index] = fdopen(index, index == STDIN_FILENO? "r" : "w"));
}
#endif