From 252a900b91b7a0f798ae260151e3a147a352eddc Mon Sep 17 00:00:00 2001 From: Denis Kopyrin Date: Tue, 2 Jul 2024 09:45:36 +0800 Subject: [PATCH] Added support for windows MinGW build (#800) --- extract_assets.py | 12 +++++++++--- sm64.ld | 16 ++++++++-------- tools/FlipsSrc/Flips.h | 31 +------------------------------ tools/Makefile | 30 +++++++++++++++++++++++------- tools/armips.cpp | 11 +++-------- tools/filesizer.c | 15 ++++++++++++++- tools/fixlights.py | 4 ++-- tools/n64graphics.c | 1 + 8 files changed, 61 insertions(+), 59 deletions(-) diff --git a/extract_assets.py b/extract_assets.py index 90928bc3..b5c68ef5 100755 --- a/extract_assets.py +++ b/extract_assets.py @@ -177,9 +177,15 @@ def main(): # presence of the correct roms automatically # Make sure tools exist - subprocess.check_call( - ["make", "-s", "-C", "tools/", "n64graphics", "skyconv", "mio0", "aifc_decode"] - ) + tools = [ "n64graphics", "skyconv", "mio0", "aifc_decode" ] + if os.name == 'nt': + tools = [tool + ".exe" for tool in tools] + make = "mingw32-make" + else: + make = "make" + + cmd = [make, "-s", "-C", "tools/"] + tools + subprocess.check_call(cmd) # Go through the assets in roughly alphabetical order (but assets in the same # mio0 file still go together). diff --git a/sm64.ld b/sm64.ld index 98cb8f80..4b5f0df4 100755 --- a/sm64.ld +++ b/sm64.ld @@ -175,9 +175,9 @@ SECTIONS #ifdef LIBPL BUILD_DIR/lib/libpl*.o(.text*); #endif - */ULTRALIB.a:*.o(.text*); + *ULTRALIB.a:*.o(.text*); */libnustd.a:*.o(.text*); - */libgcc.a:*.o(.text*); + *libgcc.a:*.o(.text*); */libz.a:*.o(.text*); */libhvqm2.a:*.o(.text*); lib/rspboot.o(.text*); @@ -197,7 +197,7 @@ SECTIONS #ifdef LIBPL BUILD_DIR/lib/libpl*.o(.*data*); #endif - */ULTRALIB.a:*.o(.data*); + *ULTRALIB.a:*.o(.data*); */libhvqm2.a:*.o(.data*); */libz.a:*.o(.data*); #include "rspdata.inc.ld" @@ -215,8 +215,8 @@ SECTIONS #ifdef LIBPL BUILD_DIR/lib/libpl*.o(.rodata*); #endif - */ULTRALIB.a:*.o(.rodata*); - */libgcc.a:*.o(.rodata*); + *ULTRALIB.a:*.o(.rodata*); + *libgcc.a:*.o(.rodata*); */libz.a:*.o(.rodata*); lib/PR/hvqm/hvqm2sp1.o(.rodata*); @@ -234,9 +234,9 @@ SECTIONS #ifdef LIBPL BUILD_DIR/lib/libpl*.o(.*bss*); #endif - */ULTRALIB.a:*.o(COMMON); - */ULTRALIB.a:*.o(.scommon); - */ULTRALIB.a:*.o(.*bss*); + *ULTRALIB.a:*.o(COMMON); + *ULTRALIB.a:*.o(.scommon); + *ULTRALIB.a:*.o(.*bss*); */libhvqm2.a:*.o(.bss*); */libz.a:*.o(.bss*); . = ALIGN(0x8); diff --git a/tools/FlipsSrc/Flips.h b/tools/FlipsSrc/Flips.h index da3182a0..8154e6b0 100644 --- a/tools/FlipsSrc/Flips.h +++ b/tools/FlipsSrc/Flips.h @@ -109,30 +109,6 @@ public: #define flipsversion "Floating IPS" -#if defined(FLIPS_WINDOWS) -#define UNICODE -#define _WIN32_WINNT 0x0501 -#define _WIN32_IE 0x0600 - -#define NOMINMAX // this seems automatically on in C++ - crazy. -#ifdef __MINGW32__ -#include -#undef __USE_MINGW_ANSI_STDIO // must remove this, to avoid a libgcc_s_sjlj-1.dll dependency on 32bit -#endif // comments say libstdc++ demands a POSIX printf, but I'm not using that, so I don't care -#include -#include -#include -#include -#include -#include -#include - -#define wcsicmp _wcsicmp // wcsicmp deprecated? okay, have a define -#define wcsdup _wcsdup -#define wtoi _wtoi - - -#else #include #include #include @@ -190,7 +166,6 @@ static inline char* strdup(const char * in) memcpy(ret, in, len+1); return ret; } -#endif #ifndef __cplusplus @@ -341,11 +316,7 @@ void bpsdeltaEnd(); int GUIShow(LPCWSTR filename); void GUILoadConfig(); //LPCWSTR GUIGetFileFor(uint32_t crc32); // use FindRomForPatch instead -#ifdef FLIPS_WINDOWS -void GUIClaimConsole(); -#else -#define GUIClaimConsole() // all other platforms have consoles already -#endif +#define GUIClaimConsole() //the OS port is responsible for main() //Module name: crc32 diff --git a/tools/Makefile b/tools/Makefile index cd5d6b10..764b0c8a 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -10,6 +10,14 @@ LDFLAGS := -lm ALL_PROGRAMS := armips filesizer rncpack n64graphics n64graphics_ci mio0 slienc n64cksum textconv aifc_decode aiff_extract_codebook vadpcm_enc tabledesign extract_data_for_mio skyconv flips LIBAUDIOFILE := audiofile/libaudiofile.a +ifeq ($(OS),Windows_NT) + EXT := .exe + OUTPUTS := $(ALL_PROGRAMS:=.exe) +else + EXT := + OUTPUTS := $(ALL_PROGRAMS) +endif + # Only build armips from tools if it is not found on the system ifeq ($(call find-command,armips),) BUILD_PROGRAMS := $(ALL_PROGRAMS) @@ -43,7 +51,7 @@ aifc_decode_SOURCES := aifc_decode.c aiff_extract_codebook_SOURCES := aiff_extract_codebook.c -tabledesign: $(LIBAUDIOFILE) +tabledesign$(EXT): $(LIBAUDIOFILE) tabledesign_SOURCES := sdk-tools/tabledesign/codebook.c sdk-tools/tabledesign/estimate.c sdk-tools/tabledesign/print.c sdk-tools/tabledesign/tabledesign.c tabledesign_CFLAGS := -Iaudiofile -Wno-uninitialized tabledesign_LDFLAGS := -Laudiofile -laudiofile -lstdc++ @@ -56,22 +64,30 @@ extract_data_for_mio_SOURCES := extract_data_for_mio.c skyconv_SOURCES := skyconv.c n64graphics.c utils.c skyconv_CFLAGS := -g -I../include -armips: CC := $(CXX) +armips$(EXT): CC := $(CXX) armips_SOURCES := armips.cpp -armips_CFLAGS := -std=c++11 -fno-exceptions -fno-rtti -pipe +armips_CFLAGS := -std=gnu++11 -fno-exceptions -fno-rtti -pipe armips_LDFLAGS := -pthread ifeq ($(HOST_ENV),MinGW) armips_LDFLAGS += -municode endif -flips: CC := $(CXX) +flips$(EXT): CC := $(CXX) flips_SOURCES := FlipsSrc/Flips.cpp +ifeq ($(OS),Windows_NT) +flips_CFLAGS := -Wall -Wextra -fopenmp -Os -flto -fuse-linker-plugin -fomit-frame-pointer -fmerge-all-constants \ +-fvisibility=hidden -fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables \ +-ffunction-sections -fdata-sections -Wl,--gc-sections +else flips_CFLAGS := -Wall -Wextra -fopenmp -Os -flto -fuse-linker-plugin -fomit-frame-pointer -fmerge-all-constants \ -fvisibility=hidden -fno-exceptions -fno-unwind-tables -fno-asynchronous-unwind-tables \ -ffunction-sections -fdata-sections -Wl,--gc-sections \ +-fvisibility=hidden -fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables \ +-ffunction-sections -fdata-sections -Wl,--gc-sections \ -Wl,-z,relro,-z,now,--as-needed,--hash-style=gnu,--relax +endif -all-except-recomp: $(LIBAUDIOFILE) $(BUILD_PROGRAMS) +all-except-recomp: $(LIBAUDIOFILE) $(OUTPUTS) all: all-except-recomp @@ -83,8 +99,8 @@ clean: distclean: clean define COMPILE -$(1): $($1_SOURCES) - $$(CC) $(CFLAGS) $($1_CFLAGS) $$^ -o $$@ $($1_LDFLAGS) $(LDFLAGS) +$(1)$$(EXT): $($1_SOURCES) + $$(CC) $(CFLAGS) $($1_CFLAGS) $$^ -o $(1) $($1_LDFLAGS) $(LDFLAGS) endef $(foreach p,$(BUILD_PROGRAMS),$(eval $(call COMPILE,$(p)))) diff --git a/tools/armips.cpp b/tools/armips.cpp index 271bf558..15ab748d 100644 --- a/tools/armips.cpp +++ b/tools/armips.cpp @@ -15496,7 +15496,7 @@ int64_t fileSize(const std::wstring& fileName) { #ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA attr; - if (!GetFileAttributesEx(fileName.c_str(),GetFileExInfoStandard,&attr) + if (!GetFileAttributesExW(fileName.c_str(),GetFileExInfoStandard,&attr) || (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) return 0; return ((int64_t) attr.nFileSizeHigh << 32) | (int64_t) attr.nFileSizeLow; @@ -15514,10 +15514,10 @@ bool fileExists(const std::wstring& strFilename) { #ifdef _WIN32 #ifdef ARMIPS_WINDOWS_UWP - return GetFileAttributes(strFilename.c_str()) != INVALID_FILE_ATTRIBUTES; + return GetFileAttributesW(strFilename.c_str()) != INVALID_FILE_ATTRIBUTES; #else int OldMode = SetErrorMode(SEM_FAILCRITICALERRORS); - bool success = GetFileAttributes(strFilename.c_str()) != INVALID_FILE_ATTRIBUTES; + bool success = GetFileAttributesW(strFilename.c_str()) != INVALID_FILE_ATTRIBUTES; SetErrorMode(OldMode); return success; #endif @@ -19877,8 +19877,6 @@ int wmain(int argc, wchar_t* argv[]) return runFromCommandLine(arguments); } -#ifndef _WIN32 - int main(int argc, char* argv[]) { // convert input to wstring @@ -19901,6 +19899,3 @@ int main(int argc, char* argv[]) delete[] wargv; return result; } - -#endif - diff --git a/tools/filesizer.c b/tools/filesizer.c index c44d8a02..c2329a16 100644 --- a/tools/filesizer.c +++ b/tools/filesizer.c @@ -1,4 +1,17 @@ -#include +#ifndef _WIN32 + #include +#else +#define __bswap_constant_32(x) \ + ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \ + (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24)) + +static __inline unsigned int +__bswap_32 (unsigned int __bsx) +{ + return __bswap_constant_32 (__bsx); +} +#endif + #include #include #include diff --git a/tools/fixlights.py b/tools/fixlights.py index a5449238..9920f38b 100755 --- a/tools/fixlights.py +++ b/tools/fixlights.py @@ -45,7 +45,7 @@ lightStructs = {} # FIRST PASS: COLLECT LIGHT STRUCT INFO, REMOVE LIGHT STRUCTS for file in lightFiles: changed = False - with open(file, "r") as f: + with open(file, "r", encoding='utf-8') as f: fileLines = f.readlines() index = 0 @@ -96,7 +96,7 @@ def index_delta(mat_name, orig_index, delta): # SECOND PASS - CHANGE LIGHT COMMANDS for file in lightFiles: changed = False - with open(file, "r") as f: + with open(file, "r", encoding='utf-8') as f: fileLines = f.readlines() index = 0 dl_index = -1 diff --git a/tools/n64graphics.c b/tools/n64graphics.c index 30d1060b..1368c6a4 100644 --- a/tools/n64graphics.c +++ b/tools/n64graphics.c @@ -21,6 +21,7 @@ #define SCALE_3_8(VAL_) ((VAL_) * 0x24) #define SCALE_8_3(VAL_) ((VAL_) / 0x24) +#define bcopy(s1, s2, n) memcpy((s2), (s1), (n)) typedef struct {