Skip to content

Commit 1458dc3

Browse files
authored
cgen,pref,help: support explicitly passing -subsystem windows or -subsystem console on Windows, even for non GG apps (#22480)
1 parent 19a7fbc commit 1458dc3

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

vlib/v/gen/c/fn.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,11 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
28042804
}
28052805

28062806
fn (mut g Gen) is_gui_app() bool {
2807+
match g.pref.subsystem {
2808+
.windows { return true }
2809+
.console { return false }
2810+
.auto {}
2811+
}
28072812
if g.pref.os == .windows {
28082813
if g.force_main_console {
28092814
return false

vlib/v/help/build/build-c.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ see also `v help build`.
5151
b) `int wmain(int ___argc, wchar_t* ___argv[], wchar_t* ___envp[]){`
5252
when you are compiling console apps.
5353

54+
-subsystem <auto|console|windows>
55+
Useful to change the generated main function on Windows.
56+
Ignored on other platforms. The default is `auto`.
57+
When set to `console` V will generate a `wmain` main function.
58+
When set to `windows` V will generate a `wWinMain` main function,
59+
even when you are not compiling `gg` apps.
60+
5461
-showcc
5562
Prints the C command that is used to build the program.
5663

@@ -353,4 +360,4 @@ see also `v help build`.
353360
See https://en.wikipedia.org/wiki/Floating-point_arithmetic#%22Fast_math%22_optimization
354361
https://learn.microsoft.com/en-us/cpp/build/reference/fp-specify-floating-point-behavior?view=msvc-170&redirectedfrom=MSDN
355362
https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math
356-
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-ffast-math
363+
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-ffast-math

vlib/v/pref/pref.v

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ pub enum ColorOutput {
4545
never
4646
}
4747

48+
// Subsystem is needed for modeling passing an explicit `/subsystem:windows` or `/subsystem:console` on Windows.
49+
// By default it is `auto`. It has no effect on platforms != windows.
50+
pub enum Subsystem {
51+
auto
52+
console
53+
windows
54+
}
55+
4856
pub enum Backend {
4957
c // The (default) C backend
5058
golang // Go backend
@@ -240,6 +248,8 @@ pub mut:
240248
// use_64_int bool
241249
// forwards compatibility settings:
242250
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.
251+
//
252+
subsystem Subsystem // the type of the window app, that is going to be generated; has no effect on !windows
243253
}
244254

245255
pub struct LineInfo {
@@ -436,6 +446,19 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
436446
res.eval_argument = cmdline.option(args[i..], '-e', '')
437447
i++
438448
}
449+
'-subsystem' {
450+
subsystem := cmdline.option(args[i..], '-subsystem', '')
451+
res.subsystem = Subsystem.from(subsystem) or {
452+
mut valid := []string{}
453+
$for x in Subsystem.values {
454+
valid << x.name
455+
}
456+
eprintln('invalid subsystem: ${subsystem}')
457+
eprintln('valid values are: ${valid}')
458+
exit(1)
459+
}
460+
i++
461+
}
439462
'-gc' {
440463
gc_mode := cmdline.option(args[i..], '-gc', '')
441464
match gc_mode {

0 commit comments

Comments
 (0)