From cdb12ccf6f2bca024907388d77c920a129b7ff6a Mon Sep 17 00:00:00 2001 From: Arsen Tufankjian Date: Tue, 25 Oct 2022 05:55:15 -0700 Subject: [PATCH] Fixing support for mingw toolchains that target the newer ucrt (#642) Fixes #641. --- CMakeLists.txt | 6 ++++++ lib/filestream.c | 14 +++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b0e317512..17eea99d9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -356,6 +356,12 @@ macro(commom_lib_settings lib write) lib/internalexport_mingw.def $<${write}:lib/internalexport_write_mingw.def> ) + # Need these flags if mingw happens to target the ucrt (new) rather + # than the legacy msvcrt. Otherwise tests will fail to run because + # the necessary dlls will be missing. If we statically link + # them instead it's fine. This does not cause any abberations if + # the mingw toolchain targets msvcrt instead. + target_link_options(${lib} PUBLIC -static-libgcc -static-libstdc++) else() target_sources( ${lib} diff --git a/lib/filestream.c b/lib/filestream.c index 38217cd362..b1e0eba7c6 100644 --- a/lib/filestream.c +++ b/lib/filestream.c @@ -307,13 +307,13 @@ KTX_error_code ktxFileStream_getsize(ktxStream* str, ktx_size_t* size) assert(str->type == eStreamTypeFile); - // Need to flush so that fstat will return the current size. - // Can ignore return value. The only error that can happen is to tell you - // it was a NOP because the file is read only. -#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(__MINGW32__) - // Bug in VS2013 msvcrt. fflush on FILE open for READ changes file offset - // to 4096. - if (str->data.file->_flag & _IOWRT) + // Need to flush so that fstat will return the current size. + // Can ignore return value. The only error that can happen is to tell you + // it was a NOP because the file is read only. +#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(__MINGW64__) && !defined(_UCRT) + // Bug in VS2013 msvcrt. fflush on FILE open for READ changes file offset + // to 4096. + if (str->data.file->_flag & _IOWRT) #endif (void)fflush(str->data.file); statret = fstat(fileno(str->data.file), &statbuf);