Skip to content

Commit 6a1b04c

Browse files
msvc: split multi-token -I/-L values from pkg-config (#26637)
1 parent a9423b5 commit 6a1b04c

1 file changed

Lines changed: 84 additions & 3 deletions

File tree

‎vlib/v/builder/msvc_windows.v‎

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ pub fn (mut v Builder) cc_msvc() {
373373
if v.pref.ldflags != '' {
374374
a << v.pref.ldflags.trim_space()
375375
}
376+
// Remove stray quoted gcc-style -I/-L/-l tokens (e.g. "-I...") that can leak into MSVC args.
377+
mut filtered_args := []string{cap: a.len}
378+
for arg in a {
379+
mut s := arg.trim_space()
380+
if s.len >= 3 && s[0] == `"` && s[1] == `-` && s[2] in [`I`, `L`, `l`] {
381+
continue
382+
}
383+
filtered_args << arg
384+
}
385+
a = filtered_args.clone()
376386
v.dump_c_options(a)
377387
args := '\xEF\xBB\xBF' + a.join(' ') // write a BOM to indicate the utf8 encoding of the file
378388
// write args to a file so that we dont smash createprocess
@@ -662,12 +672,73 @@ pub fn (mut v Builder) msvc_string_flags(cflags []cflag.CFlag) MsvcStringFlags {
662672
}
663673
}
664674
} else if flag.name == '-I' {
665-
inc_paths << '/I"${os.real_path(flag.value)}"'
675+
should_split := flag.value.contains(' -I') || flag.value.contains(' -L')
676+
|| flag.value.contains(' -l') || flag.value.contains(' -Wl,')
677+
|| flag.value.contains(' "-I') || flag.value.contains(' "-L')
678+
|| flag.value.contains(' "-l') || flag.value.contains(' "-Wl,')
679+
|| flag.value.contains('\t-I') || flag.value.contains('\t-L')
680+
|| flag.value.contains('\t-l') || flag.value.contains('\t-Wl,')
681+
|| flag.value.contains('\t"-I') || flag.value.contains('\t"-L')
682+
|| flag.value.contains('\t"-l') || flag.value.contains('\t"-Wl,')
683+
if !should_split {
684+
inc_paths << '/I"${os.real_path(flag.value)}"'
685+
} else {
686+
parts := split_quoted_flags(flag.value)
687+
if parts.len == 0 {
688+
continue
689+
}
690+
for part in parts {
691+
if part == '' {
692+
continue
693+
}
694+
if apply_gnu_flag_to_msvc(part, mut inc_paths, mut lib_paths, mut
695+
real_libs)
696+
{
697+
continue
698+
}
699+
if !part.starts_with('-') {
700+
inc_paths << '/I"${os.real_path(strip_quotes(part))}"'
701+
} else {
702+
other_flags << strip_quotes(part)
703+
}
704+
}
705+
}
666706
} else if flag.name == '-D' {
667707
defines << '/D${flag.value}'
668708
} else if flag.name == '-L' {
669-
lib_paths << flag.value
670-
lib_paths << flag.value + os.path_separator + 'msvc'
709+
should_split := flag.value.contains(' -I') || flag.value.contains(' -L')
710+
|| flag.value.contains(' -l') || flag.value.contains(' -Wl,')
711+
|| flag.value.contains(' "-I') || flag.value.contains(' "-L')
712+
|| flag.value.contains(' "-l') || flag.value.contains(' "-Wl,')
713+
|| flag.value.contains('\t-I') || flag.value.contains('\t-L')
714+
|| flag.value.contains('\t-l') || flag.value.contains('\t-Wl,')
715+
|| flag.value.contains('\t"-I') || flag.value.contains('\t"-L')
716+
|| flag.value.contains('\t"-l') || flag.value.contains('\t"-Wl,')
717+
if !should_split {
718+
lib_paths << flag.value
719+
lib_paths << flag.value + os.path_separator + 'msvc'
720+
} else {
721+
parts := split_quoted_flags(flag.value)
722+
if parts.len == 0 {
723+
continue
724+
}
725+
for part in parts {
726+
if part == '' {
727+
continue
728+
}
729+
if apply_gnu_flag_to_msvc(part, mut inc_paths, mut lib_paths, mut
730+
real_libs)
731+
{
732+
continue
733+
}
734+
if !part.starts_with('-') {
735+
lib_paths << part
736+
lib_paths << part + os.path_separator + 'msvc'
737+
} else {
738+
other_flags << strip_quotes(part)
739+
}
740+
}
741+
}
671742
// The above allows putting msvc specific .lib files in a subfolder msvc/ ,
672743
// where gcc will NOT find them, but cl will do...
673744
// Note: gcc is smart enough to not need .lib files at all in most cases, the .dll is enough.
@@ -700,6 +771,16 @@ pub fn (mut v Builder) msvc_string_flags(cflags []cflag.CFlag) MsvcStringFlags {
700771
for l in lib_paths {
701772
lpaths << '/LIBPATH:"${os.real_path(l)}"'
702773
}
774+
// Drop only stray quoted gcc-style -I/-L/-l tokens that leak into MSVC args.
775+
mut filtered_other_flags := []string{cap: other_flags.len}
776+
for of in other_flags {
777+
ofs := of.trim_space()
778+
if ofs.len >= 3 && ofs[0] == `"` && ofs[1] == `-` && ofs[2] in [`I`, `L`, `l`] {
779+
continue
780+
}
781+
filtered_other_flags << of
782+
}
783+
other_flags = filtered_other_flags.clone()
703784
return MsvcStringFlags{
704785
real_libs: real_libs
705786
inc_paths: inc_paths

0 commit comments

Comments
 (0)