From 52a8b320effdc97e00a30076915e664076342a6d Mon Sep 17 00:00:00 2001 From: anders_k Date: Tue, 21 Jan 2014 14:13:00 +0000 Subject: [PATCH] POSIX fixes for native and crossplatform utils git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@6427 212acab6-be3b-0410-9dea-997c60f758d6 --- Contrib/Library/LibraryLocal/LibraryLocal.cpp | 3 ++- Contrib/MakeLangId/SConscript | 2 +- Contrib/Makensisw/utils.h | 3 +++ Contrib/System/Source/System.c | 4 +-- Contrib/VPatch/Source/GenPat/tchar.h | 4 +++ SCons/Config/gnu | 16 +++++++++--- SConstruct | 9 ++++--- Source/Platform.h | 4 +-- Source/ResourceEditor.cpp | 3 +-- Source/exehead/Ui.c | 2 +- Source/tchar.h | 7 ++++- Source/utf.cpp | 5 +--- Source/utf.h | 8 ++++++ Source/util.cpp | 7 +++-- Source/winchar.cpp | 26 +++++++++---------- Source/winchar.h | 9 ------- 16 files changed, 66 insertions(+), 46 deletions(-) diff --git a/Contrib/Library/LibraryLocal/LibraryLocal.cpp b/Contrib/Library/LibraryLocal/LibraryLocal.cpp index 83ee1607..cf4807a8 100644 --- a/Contrib/Library/LibraryLocal/LibraryLocal.cpp +++ b/Contrib/Library/LibraryLocal/LibraryLocal.cpp @@ -41,7 +41,8 @@ int GetTLBVersion(tstring& filepath, DWORD& high, DWORD & low) hr = LoadTypeLib(fullpath, &typeLib); #else // If built without UNICODE, we still need to convert this string to a Unicode string. - WCHAR *ole_filename = wcsdup_fromTchar(fullpath, CP_ACP); + WCHAR *ole_filename = WinWStrDupFromTChar(fullpath); + if (!ole_filename) return 0; hr = LoadTypeLib(ole_filename, &typeLib); free(ole_filename); #endif diff --git a/Contrib/MakeLangId/SConscript b/Contrib/MakeLangId/SConscript index a5e698f3..744038ae 100644 --- a/Contrib/MakeLangId/SConscript +++ b/Contrib/MakeLangId/SConscript @@ -20,5 +20,5 @@ libs = Split(""" Import('BuildUtil') -BuildUtil(target, files, libs, res = res, resources = resources, entry = 'NSISWinMainNOCRT') +BuildUtil(target, files, libs, res = res, resources = resources, entry = 'NSISWinMainNOCRT', nodeflib = True) diff --git a/Contrib/Makensisw/utils.h b/Contrib/Makensisw/utils.h index 6a9804b4..9ea71891 100644 --- a/Contrib/Makensisw/utils.h +++ b/Contrib/Makensisw/utils.h @@ -27,6 +27,9 @@ #include "resource.h" #include "toolbar.h" +#ifdef COUNTOF +#undef COUNTOF +#endif #define COUNTOF(a) (sizeof(a)/sizeof(a[0])) #define MRU_LIST_SIZE 5 diff --git a/Contrib/System/Source/System.c b/Contrib/System/Source/System.c index f335106a..6068ae67 100644 --- a/Contrib/System/Source/System.c +++ b/Contrib/System/Source/System.c @@ -672,7 +672,7 @@ SystemProc *PrepareProc(BOOL NeedForCall) // Numeric inline #ifdef __GNUC__ #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // temp3 is set to 0 when we start parsing a new parameter +#pragma GCC diagnostic ignored "-Wuninitialized" // temp3 is set to 0 when we start parsing a new parameter #endif if (temp3 == 0) #ifdef __GNUC__ @@ -748,7 +748,7 @@ SystemProc *PrepareProc(BOOL NeedForCall) // Next parameter is output or something else #ifdef __GNUC__ #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#pragma GCC diagnostic ignored "-Wuninitialized" #endif temp3++; #ifdef __GNUC__ diff --git a/Contrib/VPatch/Source/GenPat/tchar.h b/Contrib/VPatch/Source/GenPat/tchar.h index bbdaad0a..9bd14351 100644 --- a/Contrib/VPatch/Source/GenPat/tchar.h +++ b/Contrib/VPatch/Source/GenPat/tchar.h @@ -21,7 +21,11 @@ # define terr cerr # define _T(x) x # define _tmain main +# ifdef _WIN32 # define _tunlink _unlink +# else +# define _tunlink unlink +# endif # define FOPEN fopen typedef std::string tstring; diff --git a/SCons/Config/gnu b/SCons/Config/gnu index 427e408e..e3d1e1cd 100644 --- a/SCons/Config/gnu +++ b/SCons/Config/gnu @@ -135,12 +135,15 @@ plugin_uenv.Append(CPPDEFINES = ['_UNICODE', 'UNICODE']) ### cross-platform util environment -cp_util_env = tdefenv.Clone() +if defenv['PLATFORM'] == 'win32': + cp_util_env = tdefenv.Clone() +else: + cp_util_env = defenv.Clone() cp_util_env.Append(CPPPATH = ['#$BUILD_CONFIG']) if cp_util_env['PLATFORM'] == 'win32': - cp_util_env.Append(LINKFLAGS = ['$ALIGN_FLAG']) + cp_util_env.Append(LINKFLAGS = ['$ALIGN_FLAG']) # 512 bytes align if not defenv['DEBUG']: cp_util_env.Append(CCFLAGS = ['-O2']) # optimize @@ -153,9 +156,16 @@ conf.Finish() ### util environment -util_env = cp_util_env.Clone() +util_env = tdefenv.Clone() cross_env(util_env) +util_env.Append(CPPPATH = ['#$BUILD_CONFIG']) + +if not defenv['DEBUG']: + util_env.Append(CCFLAGS = ['-O2']) # optimize +util_env.Append(CCFLAGS = ['-Wall']) # all warnings +util_env.Append(CCFLAGS = ['-fno-strict-aliasing']) # not safe for strict aliasing + util_env.Append(LINKFLAGS = ['-mwindows']) # build windows executables util_env.Append(LINKFLAGS = ['$ALIGN_FLAG']) # 512 bytes align diff --git a/SConstruct b/SConstruct index f48b2ff4..bc0c7b7b 100644 --- a/SConstruct +++ b/SConstruct @@ -728,10 +728,11 @@ def BuildUtilEnv(defines = None, flags = None, libs = None, libs.remove('z') AddZLib(env, platform) - if cli: - env.Append(LINKFLAGS = env['SUBSYS_CON']) - else: - env.Append(LINKFLAGS = env['SUBSYS_WIN']) + if platform == 'win32': + if cli: + env.Append(LINKFLAGS = env['SUBSYS_CON']) + else: + env.Append(LINKFLAGS = env['SUBSYS_WIN']) AddEnvStandardFlags(env, defines, flags, libs, entry, nodeflib) diff --git a/Source/Platform.h b/Source/Platform.h index 13a32ee3..b41221bf 100644 --- a/Source/Platform.h +++ b/Source/Platform.h @@ -22,8 +22,8 @@ // some definitions for non Win32 platforms were taken from MinGW's free Win32 library -#if defined(__cplusplus) && defined(MAKENSIS) && !defined(_MSC_VER) -template class NSISCHARTYPE{ T _c; public: NSISCHARTYPE(){} NSISCHARTYPE(T c):_c(c){} operator T()const{ return _c; } operator int()const{ return (int) _c; } operator bool()const{ return _c != 0; } }; +#if defined(__cplusplus) && defined(MAKENSIS) && (!defined(_MSC_VER) || _MSC_VER > 1200) +template class NSISCHARTYPE{ T _c; public: NSISCHARTYPE(){} NSISCHARTYPE(T c):_c(c){} operator T()const{ return _c; } }; typedef NSISCHARTYPE WINWCHAR; // WINWCHAR is always UTF16LE and should not be passed to wcs* functions #else typedef unsigned short WINWCHAR; diff --git a/Source/ResourceEditor.cpp b/Source/ResourceEditor.cpp index 7edc6a5a..edd591ec 100644 --- a/Source/ResourceEditor.cpp +++ b/Source/ResourceEditor.cpp @@ -222,11 +222,10 @@ bool CResourceEditor::UpdateResourceW(const WINWCHAR* szType, WINWCHAR* szName, #ifndef _UNICODE static WINWCHAR* ResStringToUnicode(const char *szString) { if (IS_INTRESOURCE(szString)) return MAKEINTRESOURCEWINW((ULONG_PTR)szString); - WINWCHAR *s = WinWStrDupFromTChar(szString, CP_ACP); + WINWCHAR *s = WinWStrDupFromTChar(szString); if (!s) throw std::bad_alloc(); return s; } - static void FreeUnicodeResString(WINWCHAR* szwString) { if (!IS_INTRESOURCE(szwString)) free(szwString); } diff --git a/Source/exehead/Ui.c b/Source/exehead/Ui.c index dad7fc49..c5b08508 100644 --- a/Source/exehead/Ui.c +++ b/Source/exehead/Ui.c @@ -1109,7 +1109,7 @@ static INT_PTR CALLBACK DirProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM l #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // available_set is checked first so available is initialized +#pragma GCC diagnostic ignored "-Wuninitialized" // available_set is checked first so available is initialized #endif if (available_set && available < total) error = NSIS_INSTDIR_NOT_ENOUGH_SPACE; diff --git a/Source/tchar.h b/Source/tchar.h index 74fe212a..dc6bffd2 100644 --- a/Source/tchar.h +++ b/Source/tchar.h @@ -183,10 +183,15 @@ typedef unsigned char _TUCHAR; // string comparisons #define _tcscmp strcmp -#define _tcsicmp _stricmp #define _tcsncmp strncmp #define _tcsncicmp _strnicmp +#ifdef _WIN32 +#define _tcsicmp _stricmp #define _tcsnicmp _strnicmp +#else +#define _tcsicmp strcasecmp +#define _tcsnicmp strncasecmp +#endif // upper / lower #define _tcslwr _strlwr diff --git a/Source/utf.cpp b/Source/utf.cpp index dc141f45..60ef621c 100644 --- a/Source/utf.cpp +++ b/Source/utf.cpp @@ -22,10 +22,7 @@ UINT StrLenUTF16(const void*str) { - unsigned short *p = (unsigned short *) str; - UINT cch = 0; - for(;p[cch];) ++cch; - return cch; + return sizeof(wchar_t) == 2 ? wcslen((wchar_t*)str) : InlineStrLenUTF16(str); } bool StrSetUTF16LE(tstring&dest, const void*src) diff --git a/Source/utf.h b/Source/utf.h index 3c5a34ea..1b8d0686 100644 --- a/Source/utf.h +++ b/Source/utf.h @@ -73,6 +73,14 @@ inline bool UTF8_GetTrailCount(unsigned char chFirst, unsigned char &cb) return true; } +inline UINT InlineStrLenUTF16(const void*str) +{ + unsigned short *p = (unsigned short *) str; + UINT cch = 0; + for(;p[cch];) ++cch; + return cch; +} + #ifdef MAKENSIS #include #include diff --git a/Source/util.cpp b/Source/util.cpp index c29f3b98..9a824e8e 100644 --- a/Source/util.cpp +++ b/Source/util.cpp @@ -406,6 +406,7 @@ int _wstat(const wchar_t *Path, struct stat *pS) return retval; } +#ifdef _UNICODE static int NSISRT_wsystem(const wchar_t *wcmd) { if (!wcmd) return system(NULL); @@ -416,6 +417,7 @@ static int NSISRT_wsystem(const wchar_t *wcmd) NSISRT_free(cmd); return retval; } +#endif const char* nsis_iconv_get_host_endian_ucs4_code() { @@ -945,8 +947,9 @@ int sane_system(const TCHAR *command) #endif // ~_UNICODE #else // !_WIN32 #ifndef _UNICODE - PATH_CONVERT(command); - return _tsystem(command); + TCHAR* cmd = const_cast(command); + PATH_CONVERT(cmd); + return _tsystem(cmd); #else return NSISRT_wsystem(command); #endif diff --git a/Source/winchar.cpp b/Source/winchar.cpp index 9902114a..8b7697cb 100644 --- a/Source/winchar.cpp +++ b/Source/winchar.cpp @@ -46,7 +46,11 @@ int WinWStrNICmpASCII(const WINWCHAR *a, const char *b, size_t n) #ifndef _WIN32 size_t WinWStrLen(const WINWCHAR *s) { +#ifdef MAKENSIS // Only makensis implements all the functions in utf.cpp return StrLenUTF16(s); +#else + return sizeof(wchar_t) == 2 ? wcslen((wchar_t*)str) : InlineStrLenUTF16(str); +#endif } WINWCHAR* WinWStrCpy(WINWCHAR *d, const WINWCHAR *s) @@ -84,9 +88,17 @@ WINWCHAR* WinWStrDupFromWinWStr(const WINWCHAR *s) WINWCHAR* WinWStrDupFromTChar(const TCHAR *s) { +#ifdef MAKENSIS WCToUTF16LEHlpr cnv; if (!cnv.Create(s)) throw runtime_error("Unicode conversion failed"); return (WINWCHAR*) cnv.Detach(); +#else + // NOTE: Anything outside the ASCII range will not convert correctly! + size_t cch = strlen(s); + WINWCHAR* p = (WINWCHAR*) malloc(++cch * 2); + if (p) for (size_t i = 0; i < cch; ++i) p[i] = s[i]; + return p; +#endif } int WinWStrToInt(const WINWCHAR *s) @@ -102,17 +114,3 @@ int WinWStrToInt(const WINWCHAR *s) return ((int)v) * sign; } #endif // ~!_WIN32 - -#if 0 -WCHAR *wcsdup_fromansi(const char* s, unsigned int codepage/* = CP_ACP*/) -{ - int l = MultiByteToWideChar(codepage, 0, s, -1, 0, 0); - if (l == 0) - throw runtime_error("Unicode conversion failed"); - - WCHAR *ws = new WCHAR[l + 1]; - if (MultiByteToWideChar(codepage, 0, s, -1, ws, l + 1) == 0) - throw runtime_error("Unicode conversion failed"); - return ws; -} -#endif diff --git a/Source/winchar.h b/Source/winchar.h index 80458baf..7731dd7a 100644 --- a/Source/winchar.h +++ b/Source/winchar.h @@ -44,13 +44,4 @@ int WinWStrToInt(const WINWCHAR *s); inline WINWCHAR* WinWStrDupFromTChar(const TCHAR *s, unsigned int codepage) { return WinWStrDupFromTChar(s); } #endif - -#if 0 -WCHAR *wcsdup_fromansi(const char* s, unsigned int codepage = CP_ACP); -#ifdef _UNICODE -#define wcsdup_fromTchar(s, codepage) _wcsdup(s) // codepage is not used in this mode -#else -#define wcsdup_fromTchar(s, codepage) wcsdup_fromansi(s, codepage) -#endif -#endif #endif // ~INC_NSIS_WINCHAR