1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

windows: do not link gdi32/shell32 to console applications

* read os.args from argv when we have console
* dynamically load CommandLineToArgvW when we are GUI app
* link gdi32/shell32 in glfw module
This commit is contained in:
vitalyster 2019-12-27 11:27:19 +03:00 committed by Alexander Medvednikov
parent 55dbb8b81c
commit 3f0f8bac49
8 changed files with 65 additions and 22 deletions

View File

@ -28,14 +28,18 @@ if not exist "%gccpath%" (
goto:msvcstrap
)
gcc -std=c99 -w -o v2.exe vc\v_win.c
gcc -std=c99 -DV_BOOTSTRAP -w -o v2.exe vc\v_win.c
if %ERRORLEVEL% NEQ 0 (
echo gcc failed to compile - Create an issue at 'https://github.com/vlang'
exit /b 1
)
echo Now using V to build V...
v2.exe -o v3.exe v.v
rem TODO: remove when v.c is updated
set VFLAGS=-cflags -DV_BOOTSTRAP -o v3.c v.v
v2.exe
gcc -std=c99 -DV_BOOTSTRAP -w -o v3.exe vc\v_win.c
set VFLAGS=
v3.exe -o v.exe -prod v.v
if %ERRORLEVEL% NEQ 0 (
echo v.exe failed to compile itself - Create an issue at 'https://github.com/vlang'
@ -70,17 +74,21 @@ if exist "%InstallDir%\Common7\Tools\vsdevcmd.bat" (
set ObjFile=.v.c.obj
cl.exe /nologo /w /volatile:ms /Fo%ObjFile% /O2 /MD vc\v_win.c user32.lib kernel32.lib advapi32.lib shell32.lib /link /NOLOGO /OUT:v2.exe /INCREMENTAL:NO
cl.exe /nologo /w /volatile:ms /Fo%ObjFile% /O2 /MD /D_VBOOTSTRAP vc\v_win.c user32.lib kernel32.lib advapi32.lib shell32.lib /link /NOLOGO /OUT:v2.exe /INCREMENTAL:NO
if %ERRORLEVEL% NEQ 0 (
echo cl.exe failed to build V
goto :compileerror
)
echo rebuild from source (twice, in case of C definitions changes)
v2.exe -cc msvc -o v3.exe v.v
v3.exe -cc msvc -o v.exe -prod v.v
rem TODO: remove when v.c is updated
set VFLAGS=-cc msvc -cflags /DV_BOOTSTRAP -o v3.c v.v
v2.exe
cl.exe /nologo /w /volatile:ms /Fo%ObjFile% /O2 /MD /D_VBOOTSTRAP v3.c user32.lib kernel32.lib advapi32.lib shell32.lib /link /NOLOGO /OUT:v3.exe /INCREMENTAL:NO
set VFLAGS=
v3.exe -cc msvc -o v -prod v.v
if %ERRORLEVEL% NEQ 0 (
echo V failed to build itself
echo V failed to build itself with error %ERRORLEVEL%
goto :compileerror
)

3
v.v
View File

@ -119,8 +119,7 @@ fn v_command(command string, args []string) {
// v.gen_doc_html_for_module(args.last())
}
else {
println('v $command: unknown command')
println('Run "v help" for usage.')
panic('v $command: unknown command\nRun "v help" for usage.')
}
}
exit(0)

View File

@ -324,9 +324,6 @@ fn C._fullpath() int
fn C.GetCommandLine() voidptr
fn C.CommandLineToArgvW() &voidptr
fn C.LocalFree()

View File

@ -263,6 +263,9 @@ fn (v mut V) cc() {
if v.os == .mac {
a << '-mmacosx-version-min=10.7'
}
if v.os == .windows {
a << '-municode'
}
cflags := v.get_os_cflags()
// add .o files
a << cflags.c_options_only_object_files()

View File

@ -557,10 +557,42 @@ pub fn (v mut V) generate_main() {
}
pub fn (v mut V) gen_main_start(add_os_args bool) {
v.cgen.genln('int main(int argc, char** argv) { ')
if (v.os == .windows) {
if 'glfw' in v.table.imports {
v.cgen.genln('#ifdef V_BOOTSTRAP')
v.cgen.genln('int main(int argc, char** argv) { ')
v.cgen.genln('#else')
// GUI application
v.cgen.genln('int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, LPWSTR cmd_line, int show_cmd) { ')
v.cgen.genln(' typedef LPWSTR*(WINAPI *cmd_line_to_argv)(LPCWSTR, int*);')
v.cgen.genln(' HMODULE shell32_module = LoadLibrary(L"shell32.dll");')
v.cgen.genln(' cmd_line_to_argv CommandLineToArgvW = (cmd_line_to_argv)GetProcAddress(shell32_module, "CommandLineToArgvW");')
v.cgen.genln(' int argc;')
v.cgen.genln(' wchar_t** argv = CommandLineToArgvW(cmd_line, &argc);')
v.cgen.genln(' os__args = os__init_os_args_wide(argc, argv);')
v.cgen.genln('#endif')
} else {
v.cgen.genln('#ifdef V_BOOTSTRAP')
v.cgen.genln('int main(int argc, char** argv) { ')
v.cgen.genln('#else')
// Console application
v.cgen.genln('int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) { ')
v.cgen.genln('#endif')
}
} else {
v.cgen.genln('int main(int argc, char** argv) { ')
}
v.cgen.genln(' init();')
if add_os_args && 'os' in v.table.imports {
v.cgen.genln(' os__args = os__init_os_args(argc, (byteptr*)argv);')
if v.os == .windows {
v.cgen.genln('#ifdef V_BOOTSTRAP')
v.cgen.genln(' os__args = os__init_os_args(argc, (byteptr*)argv);')
v.cgen.genln('#else')
v.cgen.genln(' os__args = os__init_os_args_wide(argc, argv);')
v.cgen.genln('#endif')
} else {
v.cgen.genln(' os__args = os__init_os_args(argc, (byteptr*)argv);')
}
}
v.generate_hotcode_reloading_main_caller()
v.cgen.genln('')

View File

@ -246,7 +246,7 @@ pub fn (v mut V) cc_msvc() {
// Emily:
// Not all of these are needed (but the compiler should discard them if they are not used)
// these are the defaults used by msbuild and visual studio
mut real_libs := ['kernel32.lib', 'user32.lib', 'gdi32.lib', 'winspool.lib', 'comdlg32.lib', 'advapi32.lib', 'shell32.lib', 'ole32.lib', 'oleaut32.lib', 'uuid.lib', 'odbc32.lib', 'odbccp32.lib']
mut real_libs := ['kernel32.lib', 'user32.lib', 'advapi32.lib']
sflags := v.get_os_cflags().msvc_string_flags()
real_libs << sflags.real_libs
inc_paths := sflags.inc_paths

View File

@ -21,7 +21,7 @@ import gl
#flag freebsd -I/usr/local/include
#flag freebsd -Wl,-L/usr/local/lib,-lglfw
#flag linux -lglfw
#flag windows -lglfw3
#flag windows -lgdi32 -lshell32 -lglfw3
#include <GLFW/glfw3.h>
// #flag darwin -framework Carbon
// #flag darwin -framework Cocoa

View File

@ -74,15 +74,19 @@ mut:
bInheritHandle bool
}
fn init_os_args(argc int, argv &byteptr) []string {
fn init_os_args(argc int, argv &byteptr) []string {
mut args := []string
mut args_list := &voidptr(0)
mut args_count := 0
args_list = C.CommandLineToArgvW(C.GetCommandLine(), &args_count)
for i := 0; i < args_count; i++ {
args << string_from_wide(&u16(args_list[i]))
for i := 0; i < argc; i++ {
args << string(argv[i])
}
return args
}
fn init_os_args_wide(argc int, argv &byteptr) []string {
mut args := []string
for i := 0; i < argc; i++ {
args << string_from_wide(&u16(argv[i]))
}
C.LocalFree(args_list)
return args
}