From 1458dc38dd1d9bfcf5321e19d9c7ceea04d1c861 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 11 Oct 2024 01:54:22 +0300 Subject: [PATCH] cgen,pref,help: support explicitly passing `-subsystem windows` or `-subsystem console` on Windows, even for non GG apps (#22480) --- vlib/v/gen/c/fn.v | 5 +++++ vlib/v/help/build/build-c.txt | 9 ++++++++- vlib/v/pref/pref.v | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index cbac82b09d9328..9bfe7646df502f 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2804,6 +2804,11 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as } fn (mut g Gen) is_gui_app() bool { + match g.pref.subsystem { + .windows { return true } + .console { return false } + .auto {} + } if g.pref.os == .windows { if g.force_main_console { return false diff --git a/vlib/v/help/build/build-c.txt b/vlib/v/help/build/build-c.txt index 668b19f984662c..9ddc4048029516 100644 --- a/vlib/v/help/build/build-c.txt +++ b/vlib/v/help/build/build-c.txt @@ -51,6 +51,13 @@ see also `v help build`. b) `int wmain(int ___argc, wchar_t* ___argv[], wchar_t* ___envp[]){` when you are compiling console apps. + -subsystem + Useful to change the generated main function on Windows. + Ignored on other platforms. The default is `auto`. + When set to `console` V will generate a `wmain` main function. + When set to `windows` V will generate a `wWinMain` main function, + even when you are not compiling `gg` apps. + -showcc Prints the C command that is used to build the program. @@ -353,4 +360,4 @@ see also `v help build`. See https://en.wikipedia.org/wiki/Floating-point_arithmetic#%22Fast_math%22_optimization https://learn.microsoft.com/en-us/cpp/build/reference/fp-specify-floating-point-behavior?view=msvc-170&redirectedfrom=MSDN https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math - https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-ffast-math \ No newline at end of file + https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-ffast-math diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 6c0b0b85c8e53e..f5105c59224dde 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -45,6 +45,14 @@ pub enum ColorOutput { never } +// Subsystem is needed for modeling passing an explicit `/subsystem:windows` or `/subsystem:console` on Windows. +// By default it is `auto`. It has no effect on platforms != windows. +pub enum Subsystem { + auto + console + windows +} + pub enum Backend { c // The (default) C backend golang // Go backend @@ -240,6 +248,8 @@ pub mut: // use_64_int bool // forwards compatibility settings: relaxed_gcc14 bool = true // turn on the generated pragmas, that make gcc versions > 14 a lot less pedantic. The default is to have those pragmas in the generated C output, so that gcc-14 can be used on Arch etc. + // + subsystem Subsystem // the type of the window app, that is going to be generated; has no effect on !windows } pub struct LineInfo { @@ -436,6 +446,19 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin res.eval_argument = cmdline.option(args[i..], '-e', '') i++ } + '-subsystem' { + subsystem := cmdline.option(args[i..], '-subsystem', '') + res.subsystem = Subsystem.from(subsystem) or { + mut valid := []string{} + $for x in Subsystem.values { + valid << x.name + } + eprintln('invalid subsystem: ${subsystem}') + eprintln('valid values are: ${valid}') + exit(1) + } + i++ + } '-gc' { gc_mode := cmdline.option(args[i..], '-gc', '') match gc_mode {