diff --git a/cmd/v/help/build-c.txt b/cmd/v/help/build-c.txt index f617ce2a9b..f4f0f14209 100644 --- a/cmd/v/help/build-c.txt +++ b/cmd/v/help/build-c.txt @@ -119,7 +119,12 @@ see also `v help build`. explicitly supported platforms without source changes. -m32, -m64 - Specify whether 32-bit or 64-bit machine code is generated. + Whether 32-bit or 64-bit machine code will be generated. + NB: if you need to produce 32-bit code, *and* you are cross compiling + to another OS, you may need to also set the environment variable + VCROSS_COMPILER_NAME, in order to override the default cross compiler, + that V will use (`x86_64-w64-mingw32-gcc` for targeting Windows, and + `clang` for targeting Linux from other operating systems). -sanitize Pass flags related to sanitization to the C compiler. diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 72e8a2a6bd..f25dcc36a8 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -39,10 +39,6 @@ You can also seek #help on Discord: https://discord.gg/vlang ' ) -const ( - mingw_cc = 'x86_64-w64-mingw32-gcc' -) - fn (mut v Builder) find_win_cc() ? { $if !windows { return @@ -900,7 +896,7 @@ fn (mut c Builder) cc_windows_cross() { all_args << args all_args << '-municode' c.dump_c_options(all_args) - mut cmd := '$builder.mingw_cc ' + all_args.join(' ') + mut cmd := pref.vcross_compiler_name(pref.cc_to_windows) + ' ' + all_args.join(' ') // cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 ${pref.default_module_path}/$c.out_name_c' if c.pref.is_verbose || c.pref.show_cc { println(cmd) diff --git a/vlib/v/pref/default.v b/vlib/v/pref/default.v index 213bd84e18..6a8fa5a0ab 100644 --- a/vlib/v/pref/default.v +++ b/vlib/v/pref/default.v @@ -138,6 +138,10 @@ pub fn (mut p Preferences) fill_with_defaults() { } } +pub const cc_to_windows = 'x86_64-w64-mingw32-gcc' + +pub const cc_to_linux = 'clang' + fn (mut p Preferences) find_cc_if_cross_compiling() { if p.os == .windows { $if !windows { @@ -147,14 +151,14 @@ fn (mut p Preferences) find_cc_if_cross_compiling() { // options). if p.ccompiler != 'msvc' { // Cross compiling to Windows - p.ccompiler = 'x86_64-w64-mingw32-gcc' + p.ccompiler = vcross_compiler_name(pref.cc_to_windows) } } } if p.os == .linux { $if !linux { // Cross compiling to Linux - p.ccompiler = 'clang' + p.ccompiler = vcross_compiler_name(pref.cc_to_linux) } } } @@ -210,3 +214,11 @@ pub fn vexe_path() string { os.setenv('VEXE', real_vexe_path, true) return real_vexe_path } + +pub fn vcross_compiler_name(vccname_default string) string { + vccname := os.getenv('VCROSS_COMPILER_NAME') + if vccname != '' { + return vccname + } + return vccname_default +}