From 1188e4f1a2272a86091ea7a45ad4c11aac7c032e Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 24 Mar 2014 00:07:20 +0900 Subject: [PATCH] Fix mingw32 build with gcc 4.8.2 Now mingw-w64 C++ compiler defines __USE_MINGW_ANSI_STDIO to 1, we have to follow it as well. We hope that mingw version of stdio function behaves like Linux ones. We have not tested them thoroughly yet. pthread for windows defines its own HAVE_STRUCT_TIMESPEC macro. To avoid warning, we rename our version of it as HAVE_A2_STRUCT_TIMESPEC. --- configure.ac | 18 +++++++++++------- src/GZipFile.cc | 23 ++--------------------- src/WinConsoleFile.cc | 2 +- src/WinConsoleFile.h | 4 ++-- src/fmt.h | 8 +++++++- src/timespec.h | 4 ++-- 6 files changed, 25 insertions(+), 34 deletions(-) diff --git a/configure.ac b/configure.ac index 5ef51f2b..cedd4862 100644 --- a/configure.ac +++ b/configure.ac @@ -29,9 +29,9 @@ case "$host" in *mingw*) win_build=yes LIBS="$LIBS -lws2_32 -lwsock32 -lgdi32 -lwinmm -liphlpapi -lpsapi" - # Deactivate __USE_MINGW_ANSI_STDIO because it causes lots of errors for - # PRId64. - CPPFLAGS="-D__USE_MINGW_ANSI_STDIO=0 $CPPFLAGS" + # C++ headers defines __USE_MINGW_ANSI_STDIO to 1 unconditionally. + # We have to use it as well nonetheless. + CPPFLAGS="-D__USE_MINGW_ANSI_STDIO=1 $CPPFLAGS" ;; esac @@ -667,10 +667,16 @@ AC_TYPE_UINT64_T AC_TYPE_UINT8_T AC_TYPE_PID_T AC_C_VOLATILE -AC_CHECK_TYPES([ptrdiff_t, struct timespec]) +AC_CHECK_TYPES([ptrdiff_t]) +AC_CHECK_TYPE([struct timespec], [have_timespec=yes], [have_timespec=no]) AC_C_BIGENDIAN AC_SYS_LARGEFILE +if test "x$have_timespec" = "xyes"; then + AC_DEFINE([HAVE_A2_STRUCT_TIMESPEC], [1], + [Define to 1 if the system has the type `struct timespec'.]) +fi + # Checks for library functions. AM_GNU_GETTEXT([external]) AM_GNU_GETTEXT_VERSION([0.18]) @@ -847,9 +853,7 @@ case "$host" in AM_CONDITIONAL([HAVE_GETADDRINFO], true) dnl defined in ws2tcpip.h, but missing in C:\mingw\lib\libws2_32.a AM_CONDITIONAL([HAVE_GAI_STRERROR], false) - if test "x$have_clock_gettime" != "xyes"; then - AM_CONDITIONAL([HAVE_TIMEGETTIME], true) - fi + AM_CONDITIONAL([HAVE_TIMEGETTIME], [test "x$have_clock_gettime" != "xyes"]) ;; *) AM_CONDITIONAL([MINGW_BUILD], false) diff --git a/src/GZipFile.cc b/src/GZipFile.cc index a6229373..fe834271 100644 --- a/src/GZipFile.cc +++ b/src/GZipFile.cc @@ -159,26 +159,7 @@ int GZipFile::onFlush() int GZipFile::onVprintf(const char* format, va_list va) { ssize_t len; -#ifdef __MINGW32__ - // Windows vsnprintf returns -1 when output is truncated, so we - // cannot use same logic in non-MINGW32 code. - len = _vscprintf(format, va); - if(len == 0) { - return 0; - } - // Include terminate null - ++len; - if(len > static_cast(buflen_)) { - while(static_cast(buflen_) < len) { - buflen_ *= 2; - } - buf_ = reinterpret_cast(realloc(buf_, buflen_)); - } - len = vsnprintf(buf_, buflen_, format, va); - if(len < 0) { - return len; - } -#else // !__MINGW32__ + for(;;) { len = vsnprintf(buf_, buflen_, format, va); // len does not include terminating null @@ -196,7 +177,7 @@ int GZipFile::onVprintf(const char* format, va_list va) break; } } -#endif // !__MINGW32__ + return gzwrite(fp_, buf_, len); } diff --git a/src/WinConsoleFile.cc b/src/WinConsoleFile.cc index 6ef59d25..9ef08302 100644 --- a/src/WinConsoleFile.cc +++ b/src/WinConsoleFile.cc @@ -128,7 +128,7 @@ size_t WinConsoleFile::write(const char* str) int WinConsoleFile::vprintf(const char* format, va_list va) { - ssize_t r = _vscprintf(format, va); + ssize_t r = vsnprintf(NULL, 0, format, va); if (r <= 0) { return 0; } diff --git a/src/WinConsoleFile.h b/src/WinConsoleFile.h index 67312f90..072ff01b 100644 --- a/src/WinConsoleFile.h +++ b/src/WinConsoleFile.h @@ -36,10 +36,10 @@ #ifndef D_WIN_CONSOLE_FILE_H #define D_WIN_CONSOLE_FILE_H -#include - #include "OutputFile.h" +#include + namespace aria2 { // This is a wrapper class for WriteConsoleW diff --git a/src/fmt.h b/src/fmt.h index e94ecb26..4a9256f4 100644 --- a/src/fmt.h +++ b/src/fmt.h @@ -42,7 +42,13 @@ namespace aria2 { -std::string fmt(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); +std::string fmt(const char* fmt, ...) +#ifdef __MINGW32__ + __attribute__ ((format (__MINGW_PRINTF_FORMAT, 1, 2))) +#else // !__MINGW32__ + __attribute__ ((format (printf, 1, 2))) +#endif // !__MINGW32__ + ; } // namespace aria2 diff --git a/src/timespec.h b/src/timespec.h index ba941236..038709ff 100644 --- a/src/timespec.h +++ b/src/timespec.h @@ -38,8 +38,8 @@ #include -#ifndef HAVE_STRUCT_TIMESPEC +#ifndef HAVE_A2_STRUCT_TIMESPEC struct timespec { time_t tv_sec; long tv_nsec; }; -#endif // !HAVE_STRUCT_TIMESPEC +#endif // !HAVE_A2_STRUCT_TIMESPEC #endif // D_TIMESPEC_H