From cc774bb3016813e67591c019d716f8e0861f8bb9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 12 Jun 2015 17:43:33 +0200 Subject: [PATCH] Fix building and using the library with MinGW -std=c++{98,11} options. These options enable "strict ANSI" mode in MinGW which omits declarations of POSIX functions from the standard headers. To allow the library and, possibly even more importantly, the user code including our headers, to compile with these options, declare the functions that we need ourselves. This might appear to go against the spirit of "strict ANSI" mode, but the only alternative would be to not use such functions at all and silently cripple the library when -std=c++NN is used, compared to -std=g++NN case, and this doesn't seem appealing neither. Closes #16984. --- include/wx/filefn.h | 19 ++++++++++++++++++- include/wx/math.h | 4 ++++ include/wx/msw/gccpriv.h | 23 +++++++++++++++++++++++ include/wx/msw/private.h | 2 ++ include/wx/platform.h | 1 + include/wx/wxcrtbase.h | 8 ++++++++ src/common/filefn.cpp | 2 ++ src/common/time.cpp | 17 +++++++++++++++++ src/common/utilscmn.cpp | 8 ++++++++ src/common/wxcrt.cpp | 4 ++++ src/stc/scintilla/include/Scintilla.h | 11 +++++++++++ 11 files changed, 98 insertions(+), 1 deletion(-) diff --git a/include/wx/filefn.h b/include/wx/filefn.h index da07dcf477..900af8efd2 100644 --- a/include/wx/filefn.h +++ b/include/wx/filefn.h @@ -197,8 +197,22 @@ enum wxPosixPermissions #define wxFtell _ftelli64 #elif wxCHECK_MINGW32_VERSION(3, 5) // mingw-runtime version (not gcc) #define wxHAS_HUGE_STDIO_FILES + + wxDECL_FOR_STRICT_MINGW32(int, fseeko64, (FILE*, long long, int)); #define wxFseek fseeko64 - #define wxFtell ftello64 + + #ifdef wxNEEDS_STRICT_ANSI_WORKAROUNDS + // Unfortunately ftello64() is not defined in the library for + // whatever reason but as an inline function, so define wxFtell() + // here similarly. + inline long long wxFtell(FILE* fp) + { + fpos_t pos; + return fgetpos(fp, &pos) == 0 ? pos : -1LL; + } + #else + #define wxFtell ftello64 + #endif #endif // other Windows compilers (Borland) don't have huge file support (or at @@ -324,6 +338,9 @@ enum wxPosixPermissions #define wxCRT_OpenW _wopen #endif + wxDECL_FOR_STRICT_MINGW32(int, _wmkdir, (const wchar_t*)) + wxDECL_FOR_STRICT_MINGW32(int, _wrmdir, (const wchar_t*)) + #define wxCRT_AccessW _waccess #define wxCRT_ChmodW _wchmod #define wxCRT_MkDirW _wmkdir diff --git a/include/wx/math.h b/include/wx/math.h index a1478355fc..ba298f17b0 100644 --- a/include/wx/math.h +++ b/include/wx/math.h @@ -71,6 +71,10 @@ #else #define wxFinite(x) isfinite(x) #endif +#elif defined(wxNEEDS_STRICT_ANSI_WORKAROUNDS) + wxDECL_FOR_STRICT_MINGW32(int, _finite, (double)); + + #define wxFinite(x) _finite(x) #elif ( defined(__GNUG__)||defined(__GNUWIN32__)||defined(__DJGPP__)|| \ defined(__SGI_CC__)||defined(__SUNCC__)||defined(__XLC__)|| \ defined(__HPUX__) ) && ( !defined(wxOSX_USE_IPHONE) || wxOSX_USE_IPHONE == 0 ) diff --git a/include/wx/msw/gccpriv.h b/include/wx/msw/gccpriv.h index 4f1bc4f299..acebc1f1e8 100644 --- a/include/wx/msw/gccpriv.h +++ b/include/wx/msw/gccpriv.h @@ -132,5 +132,28 @@ #endif #endif +/* + Traditional MinGW (but not MinGW-w64 nor TDM-GCC) omits many POSIX + functions from their headers when compiled with __STRICT_ANSI__ defined. + Unfortunately this means that they are not available when using -std=c++98 + (not very common) or -std=c++11 (much more so), but we still need them even + in this case. As the intention behind using -std=c++11 is probably to get + the new C++11 features and not disable the use of POSIX functions, we just + manually declare the functions we need in this case if necessary. + */ +#if defined(__MINGW32_TOOLCHAIN__) && defined(__STRICT_ANSI__) + #define wxNEEDS_STRICT_ANSI_WORKAROUNDS + + /* + This macro is somewhat unusual as it takes the list of parameters + inside parentheses and includes semicolon inside it as putting the + semicolon outside wouldn't do the right thing when this macro is empty. + */ + #define wxDECL_FOR_STRICT_MINGW32(rettype, func, params) \ + extern "C" _CRTIMP rettype __cdecl __MINGW_NOTHROW func params ; +#else + #define wxDECL_FOR_STRICT_MINGW32(rettype, func, params) +#endif + #endif /* _WX_MSW_GCCPRIV_H_ */ diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index fcb27c260c..b779051ae2 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -181,6 +181,8 @@ extern LONG APIENTRY _EXPORT || defined(__MINGW32__) #define wxGetOSFHandle(fd) ((HANDLE)_get_osfhandle(fd)) #define wxOpenOSFHandle(h, flags) (_open_osfhandle(wxPtrToUInt(h), flags)) + + wxDECL_FOR_STRICT_MINGW32(FILE*, _fdopen, (int, const char*)) #define wx_fdopen _fdopen #endif diff --git a/include/wx/platform.h b/include/wx/platform.h index 43feae73fe..c398f119d3 100644 --- a/include/wx/platform.h +++ b/include/wx/platform.h @@ -430,6 +430,7 @@ # define wxCHECK_W32API_VERSION(maj, min) (0) # undef wxCHECK_MINGW32_VERSION # define wxCHECK_MINGW32_VERSION( major, minor ) (0) +# define wxDECL_FOR_STRICT_MINGW32(rettype, func, params) #endif diff --git a/include/wx/wxcrtbase.h b/include/wx/wxcrtbase.h index c69a9482eb..ac90547c26 100644 --- a/include/wx/wxcrtbase.h +++ b/include/wx/wxcrtbase.h @@ -73,6 +73,9 @@ #ifdef wxNEED_ISASCII inline int isascii(int c) { return (unsigned)c < 0x80; } + + // Avoid further (re)definitions of it. + #define isascii isascii #endif #ifdef _WIN32_WCE @@ -451,6 +454,11 @@ WXDLLIMPEXP_BASE wchar_t *wxCRT_StrtokW(wchar_t *psz, const wchar_t *delim, wcha #define wxCRT_Rename rename #else /* Unicode filenames */ + wxDECL_FOR_STRICT_MINGW32(FILE*, _wfopen, (const wchar_t*, const wchar_t*)) + wxDECL_FOR_STRICT_MINGW32(FILE*, _wfreopen, (const wchar_t*, const wchar_t*, FILE*)) + wxDECL_FOR_STRICT_MINGW32(int, _wrename, (const wchar_t*, const wchar_t*)) + wxDECL_FOR_STRICT_MINGW32(int, _wremove, (const wchar_t*)) + /* WinCE CRT doesn't provide these functions so use our own */ #ifdef __WXWINCE__ WXDLLIMPEXP_BASE int wxCRT_Rename(const wchar_t *src, diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index fa53b4f6a9..18cd9ef682 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -86,6 +86,8 @@ #define HAVE_WGETCWD #endif +wxDECL_FOR_STRICT_MINGW32(int, _fileno, (FILE*)) + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- diff --git a/src/common/time.cpp b/src/common/time.cpp index 6dbeac9f5a..f51a0b1fa6 100644 --- a/src/common/time.cpp +++ b/src/common/time.cpp @@ -22,6 +22,23 @@ #pragma hdrstop #endif +// This is a horrible hack which only works because we don't currently include +// from wx/wxprec.h. It is needed because we need timezone-related +// stuff from MinGW time.h, but it is not compiled in strict ANSI mode and it +// is too complicated to be dealt with using wxDECL_FOR_STRICT_MINGW32(). So we +// just include the header after undefining __STRICT_ANSI__ to get all the +// declarations we need -- and then define it back to avoid inconsistencies in +// all our other headers. +// +// Note that the same hack is used for "environ" in utilscmn.cpp, so if the +// code here is modified because this hack becomes unnecessary or a better +// solution is found, the code there should be updated as well. +#ifdef wxNEEDS_STRICT_ANSI_WORKAROUNDS + #undef __STRICT_ANSI__ + #include + #define __STRICT_ANSI__ +#endif + #include "wx/time.h" #ifndef WX_PRECOMP diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index aa0323418d..77df1aad2f 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -23,6 +23,14 @@ #pragma hdrstop #endif +// See comment about this hack in time.cpp: here we do it for environ external +// variable which can't be easily declared when using MinGW in strict ANSI mode. +#ifdef wxNEEDS_STRICT_ANSI_WORKAROUNDS + #undef __STRICT_ANSI__ + #include + #define __STRICT_ANSI__ +#endif + #ifndef WX_PRECOMP #include "wx/app.h" #include "wx/string.h" diff --git a/src/common/wxcrt.cpp b/src/common/wxcrt.cpp index 0e49b8d351..1eeab52c86 100644 --- a/src/common/wxcrt.cpp +++ b/src/common/wxcrt.cpp @@ -73,6 +73,10 @@ #include #endif +wxDECL_FOR_STRICT_MINGW32(int, vswprintf, (wchar_t*, const wchar_t*, __VALIST)); +wxDECL_FOR_STRICT_MINGW32(int, _putws, (const wchar_t*)); +wxDECL_FOR_STRICT_MINGW32(void, _wperror, (const wchar_t*)); + WXDLLIMPEXP_BASE size_t wxMB2WC(wchar_t *buf, const char *psz, size_t n) { // assume that we have mbsrtowcs() too if we have wcsrtombs() diff --git a/src/stc/scintilla/include/Scintilla.h b/src/stc/scintilla/include/Scintilla.h index 462f5b5c35..08acc5453b 100644 --- a/src/stc/scintilla/include/Scintilla.h +++ b/src/stc/scintilla/include/Scintilla.h @@ -42,6 +42,17 @@ typedef long sptr_t; typedef sptr_t (*SciFnDirect)(sptr_t ptr, unsigned int iMessage, uptr_t wParam, sptr_t lParam); +/* Workaround for building with MinGW (not MinGW-w64) in strict ANSI mode which + * is, notably, enabled by -std=c++NN options. */ +#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) && defined(__STRICT_ANSI__) +/* Other headers included by an application using Scintilla might already + * define isascii() too, for the same reasons, try to play nicely with the. */ +#ifndef isascii +inline int isascii(int c) { return !(c & ~0x7F); } +#define isascii isascii +#endif +#endif + /* ++Autogenerated -- start of section automatically generated from Scintilla.iface */ #define INVALID_POSITION -1 #define SCI_START 2000