Skip to content

Commit b39c877

Browse files
committed
MinGW-based libs: Add wide entry points
1 parent b1eb884 commit b39c877

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

azure-pipelines.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
variables:
3838
VSINSTALLDIR: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\
3939
# NB: update windows/build_mingw.sha256sums as well
40-
MINGW_VER: 7.0.0
40+
MINGW_VER: 7.0.0-2
4141
D_VERSION: 2.077.1
4242
steps:
4343
- template: .azure-pipelines/build-mingw-libs.yml

windows/mingw/buildsdk.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ void buildMsvcrt(string outDir)
379379
// compile some additional objects
380380
foreach (i; 0 .. 3)
381381
addObj(format!"msvcrt_stub%d.obj"(i), format!"/D_APPTYPE=%d msvcrt_stub.c"(i));
382+
foreach (i; 1 .. 3) // not needed for DLLs
383+
addObj(format!"msvcrt_stub_wide%d.obj"(i), format!"/D_APPTYPE=%d /D_UNICODE msvcrt_stub.c"(i));
382384
addObj("msvcrt_data.obj", "msvcrt_data.c");
383385
addObj("msvcrt_atexit.obj", "msvcrt_atexit.c");
384386
if (!x64)

windows/mingw/msvcrt_stub.c

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ extern _PVFV __xp_z[];
2323
extern _PVFV __xt_a[];
2424
extern _PVFV __xt_z[];
2525

26-
extern int main (int, char **, char **);
2726
extern void _setargv (void);
2827
extern void term_atexit();
2928

@@ -78,7 +77,14 @@ __pragma(comment(linker, "/alternatename:_DllMain@12=___DefaultDllMain@12"));
7877
#else // _APPTYPE != __UNKNOWN_APP
7978

8079
extern int __argc;
80+
81+
#ifndef _UNICODE
8182
extern char **__argv;
83+
extern int main(int, char **, char **);
84+
#else
85+
extern wchar_t **__wargv;
86+
extern int wmain(int, wchar_t **, wchar_t **);
87+
#endif
8288

8389
#if MSVCRT_VERSION >= 140 // UCRT
8490

@@ -95,10 +101,17 @@ enum _crt_argv_mode
95101
_crt_argv_expanded_arguments,
96102
};
97103

104+
#ifndef _UNICODE
98105
extern int _initialize_narrow_environment();
99106
extern char **_get_initial_narrow_environment();
100107
extern int _configure_narrow_argv(int);
101108
extern char *_get_narrow_winmain_command_line();
109+
#else // _UNICODE
110+
extern int _initialize_wide_environment();
111+
extern wchar_t **_get_initial_wide_environment();
112+
extern int _configure_wide_argv(int);
113+
extern wchar_t *_get_wide_winmain_command_line();
114+
#endif
102115

103116
#else // MSVCRT_VERSION < 140
104117

@@ -107,55 +120,91 @@ extern char *_get_narrow_winmain_command_line();
107120
* it as a dummy, with a declared size of zero in its first and only field).
108121
*/
109122
typedef struct _startupinfo { int mode; } _startupinfo;
110-
extern void __getmainargs( int *argc, char ***argv, char ***penv, int glob, _startupinfo *info );
111-
123+
#ifndef _UNICODE
124+
extern int __getmainargs(int *argc, char ***argv, char ***penv, int glob, _startupinfo *info);
125+
#else
126+
extern int __wgetmainargs(int *argc, wchar_t ***argv, wchar_t ***penv, int glob, _startupinfo *info);
112127
#endif
113128

129+
#endif // MSVCRT_VERSION < 140
130+
114131
/* The function mainCRTStartup() is the entry point for all
115132
* console/desktop programs.
116133
*/
117134
#if _APPTYPE == __CONSOLE_APP
135+
#ifndef _UNICODE
118136
void mainCRTStartup(void)
119-
#else
137+
#else
138+
void wmainCRTStartup(void)
139+
#endif
140+
#else // desktop app
141+
#ifndef _UNICODE
120142
void WinMainCRTStartup(void)
143+
#else
144+
void wWinMainCRTStartup(void)
145+
#endif
121146
#endif
122147
{
123148
int nRet;
124149
__set_app_type(_APPTYPE);
125150
__ref_oldnames = 0; // drag in alternate definitions
126151

127152
#if MSVCRT_VERSION >= 140 // UCRT
153+
#ifndef _UNICODE
128154
_configure_narrow_argv(_crt_argv_unexpanded_arguments);
129155
_initialize_narrow_environment();
130156
char **envp = _get_initial_narrow_environment();
131-
#else
157+
#else
158+
_configure_wide_argv(_crt_argv_unexpanded_arguments);
159+
_initialize_wide_environment();
160+
wchar_t **wenvp = _get_initial_wide_environment();
161+
#endif
162+
#else // MSVCRT_VERSION < 140
132163
/* The MSVCRT.DLL start-up hook requires this invocation
133164
* protocol...
134165
*/
135-
char **envp = NULL;
136166
_startupinfo start_info = { 0 };
167+
#ifndef _UNICODE
168+
char **envp = NULL;
137169
__getmainargs(&__argc, &__argv, &envp, 0, &start_info);
138-
#endif
170+
#else
171+
wchar_t **wenvp = NULL;
172+
__wgetmainargs(&__argc, &__wargv, &wenvp, 0, &start_info);
173+
#endif
174+
#endif // MSVCRT_VERSION < 140
139175

140176
_initterm_e(__xi_a, __xi_z);
141177
_initterm(__xc_a, __xc_z);
142178

143179
#if _APPTYPE == __CONSOLE_APP
180+
#ifndef _UNICODE
144181
nRet = main(__argc, __argv, envp);
145-
#else
182+
#else
183+
nRet = wmain(__argc, __wargv, wenvp);
184+
#endif
185+
#else // desktop app
146186
{
147187
STARTUPINFOA startupInfo;
148188
GetStartupInfoA(&startupInfo);
149189
int showWindowMode = startupInfo.dwFlags & STARTF_USESHOWWINDOW
150190
? startupInfo.wShowWindow : SW_SHOWDEFAULT;
151-
#if MSVCRT_VERSION >= 140 // UCRT
191+
#ifndef _UNICODE
192+
#if MSVCRT_VERSION >= 140 // UCRT
152193
LPSTR lpszCommandLine = _get_narrow_winmain_command_line();
153-
#else
194+
#else
154195
LPSTR lpszCommandLine = GetCommandLineA();
155-
#endif
196+
#endif
156197
nRet = WinMain((HINSTANCE)&__ImageBase, NULL, lpszCommandLine, showWindowMode);
198+
#else // _UNICODE
199+
#if MSVCRT_VERSION >= 140 // UCRT
200+
LPWSTR lpszCommandLine = _get_wide_winmain_command_line();
201+
#else
202+
LPWSTR lpszCommandLine = GetCommandLineW();
203+
#endif
204+
nRet = wWinMain((HINSTANCE)&__ImageBase, NULL, lpszCommandLine, showWindowMode);
205+
#endif
157206
}
158-
#endif
207+
#endif // desktop app
159208

160209
term_atexit();
161210
_initterm(__xp_a, __xp_z);

0 commit comments

Comments
 (0)