From be9ce00fc8850ff647b5b9d5e3034f97f5d08b40 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 23 Apr 2026 18:17:14 +0300 Subject: [PATCH] builder: fix Cross compiling for Linux on Windows does not work (fixes #12128) --- vlib/v/builder/builder_test.v | 22 ++++++++++++++++++++++ vlib/v/builder/cbuilder/cbuilder.v | 12 +++++++----- vlib/v/builder/compile.v | 7 ++++++- vlib/v/pref/pref_test.v | 1 + 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/vlib/v/builder/builder_test.v b/vlib/v/builder/builder_test.v index fa9753360..27d163e3b 100644 --- a/vlib/v/builder/builder_test.v +++ b/vlib/v/builder/builder_test.v @@ -1,6 +1,8 @@ module main import os +import v.builder +import v.pref const vexe = @VEXE const test_path = os.join_path(os.vtmp_dir(), 'run_check') @@ -226,6 +228,26 @@ fn test_missing_library_is_reported_without_compiler_bug_hint() { assert !normalized_output.contains('This is a V compiler bug') } +fn test_windows_host_c_compiler_probe_is_skipped_for_non_windows_targets() { + assert builder.should_find_windows_host_c_compiler(&pref.Preferences{ + backend: .c + os: .windows + }) + assert !builder.should_find_windows_host_c_compiler(&pref.Preferences{ + backend: .c + os: .linux + }) + assert !builder.should_find_windows_host_c_compiler(&pref.Preferences{ + backend: .c + os: .windows + output_cross_c: true + }) + assert !builder.should_find_windows_host_c_compiler(&pref.Preferences{ + backend: .js_browser + os: .windows + }) +} + fn test_run_with_obscure_source_filenames() { if os.user_os() == 'windows' { return diff --git a/vlib/v/builder/cbuilder/cbuilder.v b/vlib/v/builder/cbuilder/cbuilder.v index 719143b1d..c1a331118 100644 --- a/vlib/v/builder/cbuilder/cbuilder.v +++ b/vlib/v/builder/cbuilder/cbuilder.v @@ -18,11 +18,12 @@ pub fn compile_c(mut b builder.Builder) { println('all .v files before:') } $if windows { - b.find_win_cc() or { - if b.pref.ccompiler_set_by_flag && b.pref.ccompiler != 'msvc' { - builder.verror(err.msg()) - } - builder.verror(' + if builder.should_find_windows_host_c_compiler(b.pref) { + b.find_win_cc() or { + if b.pref.ccompiler_set_by_flag && b.pref.ccompiler != 'msvc' { + builder.verror(err.msg()) + } + builder.verror(' ================== Error: no C compiler detected. @@ -37,6 +38,7 @@ You can also use `v doctor`, to see what V knows about your current environment. You can also seek #help on Discord: https://discord.gg/vlang ') + } } } mut files := b.get_builtin_files() diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index 8e587187c..e23948fd2 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -10,11 +10,16 @@ import v.vmod pub type FnBackend = fn (mut b Builder) +// should_find_windows_host_c_compiler reports whether Windows host compiler probing is needed. +pub fn should_find_windows_host_c_compiler(pref_ &pref.Preferences) bool { + return pref_.backend == .c && pref_.os == .windows && !pref_.output_cross_c +} + pub fn compile(command string, pref_ &pref.Preferences, backend_cb FnBackend) { check_if_input_file_exists(pref_) check_if_output_folder_is_writable(pref_) $if windows { - if pref_.backend == .c { + if should_find_windows_host_c_compiler(pref_) { // Resolve the effective Windows C compiler before builder initialization. mut probe := Builder{ pref: unsafe { pref_ } diff --git a/vlib/v/pref/pref_test.v b/vlib/v/pref/pref_test.v index 1a653d847..18acab9b0 100644 --- a/vlib/v/pref/pref_test.v +++ b/vlib/v/pref/pref_test.v @@ -230,6 +230,7 @@ fn test_cross_compile_defaults_linux_to_amd64() { target := os.join_path(vroot, 'examples', 'hello_world.v') prefs, _ := pref.parse_args_and_show_errors([], ['', '-os', 'linux', target], false) assert prefs.arch == .amd64 + assert prefs.ccompiler == 'clang' } fn test_cross_compile_infers_android_arch_from_vcross_compiler_name() { -- 2.39.5