Просмотр исходного кода

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.
Tatsuhiro Tsujikawa 11 лет назад
Родитель
Сommit
1188e4f1a2
6 измененных файлов с 25 добавлено и 34 удалено
  1. 11 7
      configure.ac
  2. 2 21
      src/GZipFile.cc
  3. 1 1
      src/WinConsoleFile.cc
  4. 2 2
      src/WinConsoleFile.h
  5. 7 1
      src/fmt.h
  6. 2 2
      src/timespec.h

+ 11 - 7
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)

+ 2 - 21
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<ssize_t>(buflen_)) {
-    while(static_cast<ssize_t>(buflen_) < len) {
-      buflen_ *= 2;
-    }
-    buf_ = reinterpret_cast<char*>(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);
 }
 

+ 1 - 1
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;
   }

+ 2 - 2
src/WinConsoleFile.h

@@ -36,10 +36,10 @@
 #ifndef D_WIN_CONSOLE_FILE_H
 #define D_WIN_CONSOLE_FILE_H
 
-#include <string>
-
 #include "OutputFile.h"
 
+#include <string>
+
 namespace aria2 {
 
   // This is a wrapper class for WriteConsoleW

+ 7 - 1
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
 

+ 2 - 2
src/timespec.h

@@ -38,8 +38,8 @@
 
 #include <time.h>
 
-#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