| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module pref |
| 5 | |
| 6 | import os |
| 7 | import strings |
| 8 | import v.vcache |
| 9 | |
| 10 | pub const default_module_path = os.vmodules_dir() |
| 11 | |
| 12 | pub fn new_preferences() &Preferences { |
| 13 | mut p := &Preferences{} |
| 14 | p.fill_with_defaults() |
| 15 | return p |
| 16 | } |
| 17 | |
| 18 | const windows_default_gc_defines = ['gcboehm', 'gcboehm_full', 'gcboehm_incr', 'gcboehm_opt', |
| 19 | 'gcboehm_leak', 'vgc'] |
| 20 | |
| 21 | fn (p &Preferences) default_thread_stack_size() int { |
| 22 | return match p.arch { |
| 23 | .arm32, .rv32, .i386, .ppc, .wasm32 { 2 * 1024 * 1024 } |
| 24 | else { 8 * 1024 * 1024 } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn (p &Preferences) is_linux_wayland_only_session() bool { |
| 29 | if p.os != .linux { |
| 30 | return false |
| 31 | } |
| 32 | if os.getenv('DISPLAY') != '' { |
| 33 | return false |
| 34 | } |
| 35 | return os.getenv('WAYLAND_DISPLAY') != '' |
| 36 | || os.getenv('XDG_SESSION_TYPE').to_lower() == 'wayland' |
| 37 | } |
| 38 | |
| 39 | fn (mut p Preferences) expand_lookup_paths() { |
| 40 | if p.vroot == '' { |
| 41 | // Location of all vlib files |
| 42 | p.vroot = os.dir(vexe_path()) |
| 43 | } |
| 44 | p.vlib = os.join_path(p.vroot, 'vlib') |
| 45 | p.vmodules_paths = os.vmodules_paths() |
| 46 | |
| 47 | if p.lookup_path.len == 0 { |
| 48 | p.lookup_path = ['@vlib', '@vmodules'] |
| 49 | } |
| 50 | mut expanded_paths := []string{} |
| 51 | for path in p.lookup_path { |
| 52 | match path { |
| 53 | '@vlib' { expanded_paths << p.vlib } |
| 54 | '@vmodules' { expanded_paths << p.vmodules_paths } |
| 55 | else { expanded_paths << path.replace('@vroot', p.vroot) } |
| 56 | } |
| 57 | } |
| 58 | p.lookup_path = expanded_paths |
| 59 | } |
| 60 | |
| 61 | fn (mut p Preferences) expand_exclude_paths() { |
| 62 | mut res := []string{} |
| 63 | static_replacement_list := ['@vroot', p.vroot, '@vlib', p.vlib] |
| 64 | for x in p.exclude { |
| 65 | y := x.replace_each(static_replacement_list) |
| 66 | if y.contains('@vmodules') { |
| 67 | // @vmodules is a list of paths, each of which should be expanded in the complete exclusion list: |
| 68 | for vmp in p.vmodules_paths { |
| 69 | res << y.replace('@vmodules', vmp) |
| 70 | } |
| 71 | continue |
| 72 | } |
| 73 | res << y |
| 74 | } |
| 75 | p.exclude = res |
| 76 | } |
| 77 | |
| 78 | fn (mut p Preferences) setup_os_and_arch_when_not_explicitly_set() { |
| 79 | if p.os == .wasm32_emscripten { |
| 80 | // TODO: remove after `$if wasm32_emscripten {` works |
| 81 | p.parse_define('emscripten') |
| 82 | } |
| 83 | host_os := if p.backend == .wasm { OS.wasi } else { get_host_os() } |
| 84 | if p.os == ._auto { |
| 85 | p.os = host_os |
| 86 | p.build_options << '-os ${host_os.lower()}' |
| 87 | } |
| 88 | |
| 89 | if !p.output_cross_c && p.os != host_os { |
| 90 | if p.os == .linux && p.arch == ._auto { |
| 91 | // The bundled linuxroot sysroot currently only contains x86_64 runtime files. |
| 92 | p.set_default_arch(.amd64) |
| 93 | } |
| 94 | // TODO: generalise this not only for macos->linux, after considering the consequences for vab/Android: |
| 95 | if host_os == .macos && p.os == .linux { |
| 96 | p.parse_define('use_bundled_libgc') |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | fn (mut p Preferences) set_default_arch(arch Arch) { |
| 102 | if p.arch != ._auto || arch == ._auto { |
| 103 | return |
| 104 | } |
| 105 | p.arch = arch |
| 106 | p.build_options << '-arch ${arch}' |
| 107 | } |
| 108 | |
| 109 | fn arch_from_ccompiler_name(ccompiler string) Arch { |
| 110 | name := os.file_name(ccompiler).to_lower_ascii() |
| 111 | if name.contains('x86_64') || name.contains('amd64') { |
| 112 | return .amd64 |
| 113 | } |
| 114 | if name.contains('aarch64') || name.contains('arm64-v8a') || name.contains('arm64') { |
| 115 | return .arm64 |
| 116 | } |
| 117 | if name.contains('armeabi-v7a') || name.contains('armv7') |
| 118 | || name.contains('arm-linux-androideabi') || name.contains('arm32') { |
| 119 | return .arm32 |
| 120 | } |
| 121 | if name.contains('riscv64') { |
| 122 | return .rv64 |
| 123 | } |
| 124 | if name.contains('riscv32') { |
| 125 | return .rv32 |
| 126 | } |
| 127 | if name.contains('i686') || name.contains('i386') || name.contains('x86') { |
| 128 | return .i386 |
| 129 | } |
| 130 | if name.contains('s390x') { |
| 131 | return .s390x |
| 132 | } |
| 133 | if name.contains('ppc64le') { |
| 134 | return .ppc64le |
| 135 | } |
| 136 | if name.contains('loongarch64') { |
| 137 | return .loongarch64 |
| 138 | } |
| 139 | if name.contains('sparc64') { |
| 140 | return .sparc64 |
| 141 | } |
| 142 | if name.contains('ppc64') { |
| 143 | return .ppc64 |
| 144 | } |
| 145 | return ._auto |
| 146 | } |
| 147 | |
| 148 | fn (mut p Preferences) resolve_default_arch() { |
| 149 | if p.arch != ._auto { |
| 150 | return |
| 151 | } |
| 152 | host_os := if p.backend == .wasm { OS.wasi } else { get_host_os() } |
| 153 | if p.os != host_os { |
| 154 | p.set_default_arch(arch_from_ccompiler_name(p.ccompiler)) |
| 155 | } |
| 156 | p.set_default_arch(get_host_arch()) |
| 157 | } |
| 158 | |
| 159 | pub fn (mut p Preferences) defines_map_unique_keys() string { |
| 160 | mut defines_map := map[string]bool{} |
| 161 | for d in p.compile_defines { |
| 162 | defines_map[d] = true |
| 163 | } |
| 164 | for d in p.compile_defines_all { |
| 165 | defines_map[d] = true |
| 166 | } |
| 167 | keys := defines_map.keys() |
| 168 | skeys := keys.sorted() |
| 169 | return skeys.join(',') |
| 170 | } |
| 171 | |
| 172 | fn (mut p Preferences) disable_tcc_shared_backtraces() { |
| 173 | if p.is_shared && p.ccompiler_type == .tinyc && 'no_backtrace' !in p.compile_defines_all { |
| 174 | // TCC shared libraries should not depend on TCC's backtrace runtime symbols. |
| 175 | p.parse_define('no_backtrace') |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | fn (mut p Preferences) prefer_openssl_for_bsd_tinyc() { |
| 180 | if p.os in [.freebsd, .openbsd] && p.ccompiler_type == .tinyc |
| 181 | && 'use_openssl' !in p.compile_defines_all { |
| 182 | // TinyCC hangs in the bundled mbedtls path on FreeBSD/OpenBSD. |
| 183 | p.parse_define('use_openssl') |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | fn (p &Preferences) needs_source_boehm_without_thread_local_alloc() bool { |
| 188 | return p.gc_mode in [.boehm_full, .boehm_incr, .boehm_full_opt, .boehm_incr_opt, .boehm_leak] |
| 189 | && 'no_gc_thread_local_alloc' in p.compile_defines_all |
| 190 | && 'dynamic_boehm' !in p.compile_defines_all |
| 191 | } |
| 192 | |
| 193 | fn (mut p Preferences) prefer_source_boehm_without_thread_local_alloc() { |
| 194 | if !p.needs_source_boehm_without_thread_local_alloc() |
| 195 | || 'use_bundled_libgc' in p.compile_defines_all { |
| 196 | return |
| 197 | } |
| 198 | // The prebuilt bundled libgc archives are built with thread-local allocation. |
| 199 | // Use the bundled source path instead, so builtin_d_gcboehm.c.v can omit |
| 200 | // -DTHREAD_LOCAL_ALLOC while still keeping GC_THREADS enabled. |
| 201 | p.parse_define('use_bundled_libgc') |
| 202 | } |
| 203 | |
| 204 | // fill_with_defaults initializes unset preferences and derives build options from them. |
| 205 | pub fn (mut p Preferences) fill_with_defaults() { |
| 206 | p.setup_os_and_arch_when_not_explicitly_set() |
| 207 | p.expand_lookup_paths() |
| 208 | p.expand_exclude_paths() |
| 209 | rpath := os.real_path(p.path) |
| 210 | if p.out_name == '' { |
| 211 | target_dir := if os.is_dir(rpath) { rpath } else { os.dir(rpath) } |
| 212 | p.out_name = os.join_path(target_dir, p.default_output_name(rpath)) |
| 213 | // Do *NOT* be tempted to generate binaries in the current work folder, |
| 214 | // when -o is not given by default, like Go, Clang, GCC etc do. |
| 215 | // |
| 216 | // These compilers also are frequently used with an external build system, |
| 217 | // in part because of that shortcoming, to ensure that they work in a |
| 218 | // predictable work folder/environment. |
| 219 | // |
| 220 | // In comparison, with V, building an executable by default places it |
| 221 | // next to its source code, so that it can be used directly with |
| 222 | // functions like `os.resource_abs_path()` and `os.executable()` to |
| 223 | // locate resources relative to it. That enables running examples like |
| 224 | // this: |
| 225 | // `./v run examples/flappylearning/` |
| 226 | // instead of: |
| 227 | // `./v -o examples/flappylearning/flappylearning run examples/flappylearning/` |
| 228 | // This topic comes up periodically from time to time on Discord, and |
| 229 | // many CI breakages already happened, when someone decides to make V |
| 230 | // behave in this aspect similarly to the dumb behaviour of other |
| 231 | // compilers. |
| 232 | // |
| 233 | // If you do decide to break it, please *at the very least*, test it |
| 234 | // extensively, and make a PR about it, instead of committing directly |
| 235 | // and breaking the CI, VC, and users doing `v up`. |
| 236 | } else if p.out_name_is_dir { |
| 237 | p.out_name = os.join_path(p.out_name, p.default_output_name(rpath)) |
| 238 | } |
| 239 | npath := rpath.replace('\\', '/') |
| 240 | p.building_v = !p.is_repl && is_v_compiler_target(npath) |
| 241 | if !p.is_repl && !p.gc_set_by_flag && is_v2_compiler_target(npath) { |
| 242 | // v2 defaults to `-gc none`: the v2 compiler manages its own |
| 243 | // arenas/move semantics and the boehm collector slows it down |
| 244 | // and inflates peak memory (multi-GB for non-trivial builds). |
| 245 | // An explicit `-gc <mode>` from the user wins. |
| 246 | p.gc_mode = .no_gc |
| 247 | p.clear_gc_options() |
| 248 | } |
| 249 | if p.os == .linux { |
| 250 | $if !linux { |
| 251 | p.parse_define('cross_compile') |
| 252 | } |
| 253 | } |
| 254 | if p.output_cross_c { |
| 255 | // avoid linking any GC related code, since the target may not have an usable GC system |
| 256 | p.gc_mode = .no_gc |
| 257 | p.use_cache = false |
| 258 | p.skip_unused = false |
| 259 | p.parse_define('no_backtrace') // the target may not have usable backtrace() and backtrace_symbols() |
| 260 | p.parse_define('cross') // TODO: remove when `$if cross {` works |
| 261 | } |
| 262 | if p.gc_mode == .unknown { |
| 263 | if p.backend != .c || p.building_v || p.is_bare { |
| 264 | p.gc_mode = .no_gc |
| 265 | p.build_options << ['-gc', 'none'] |
| 266 | } else { |
| 267 | // enable the GC by default |
| 268 | p.gc_mode = .boehm_full_opt |
| 269 | // NOTE: these are added to p.compile_defines[_all] |
| 270 | // more than once when building modules for usecache |
| 271 | p.parse_define('gcboehm') |
| 272 | p.parse_define('gcboehm_full') |
| 273 | p.parse_define('gcboehm_opt') |
| 274 | } |
| 275 | } |
| 276 | if p.is_debug { |
| 277 | p.parse_define('debug') |
| 278 | } |
| 279 | p.try_to_use_tcc_by_default() |
| 280 | if p.ccompiler == '' { |
| 281 | p.default_c_compiler() |
| 282 | } |
| 283 | if p.cppcompiler == '' { |
| 284 | p.default_cpp_compiler() |
| 285 | } |
| 286 | p.find_cc_if_cross_compiling() |
| 287 | p.resolve_default_arch() |
| 288 | if !p.thread_stack_size_set_by_flag { |
| 289 | p.thread_stack_size = p.default_thread_stack_size() |
| 290 | } |
| 291 | p.ccompiler_type = cc_from_string(p.ccompiler) |
| 292 | p.normalize_gc_defaults_for_resolved_ccompiler() |
| 293 | p.prefer_source_boehm_without_thread_local_alloc() |
| 294 | p.is_test = p.path.ends_with('_test.v') || p.path.ends_with('_test.vv') |
| 295 | || p.path.all_before_last('.v').all_before_last('.').ends_with('_test') |
| 296 | p.is_vsh = p.path.ends_with('.vsh') || p.raw_vsh_tmp_prefix != '' |
| 297 | p.is_script = p.is_vsh || p.path.ends_with('.v') || p.path.ends_with('.vv') |
| 298 | if p.third_party_option == '' { |
| 299 | p.third_party_option = p.cflags |
| 300 | $if !windows { |
| 301 | if !p.third_party_option.contains('-fPIC') { |
| 302 | p.third_party_option += ' -fPIC' |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | final_os := p.os.lower() |
| 308 | p.parse_define(final_os) |
| 309 | if p.is_linux_wayland_only_session() && 'linux_wayland_session' !in p.compile_defines_all { |
| 310 | p.parse_define('linux_wayland_session') |
| 311 | } |
| 312 | |
| 313 | // Prepare the cache manager. All options that can affect the generated cached .c files |
| 314 | // should go into res.cache_manager.vopts, which is used as a salt for the cache hash. |
| 315 | vhash := @VHASH |
| 316 | p.cache_manager = vcache.new_cache_manager([ |
| 317 | vhash, |
| 318 | // ensure that different v versions use separate build artefacts |
| 319 | '${p.backend} | ${final_os} | ${p.ccompiler} | ${p.is_prod} | ${p.sanitize}', |
| 320 | p.defines_map_unique_keys(), |
| 321 | p.cflags.trim_space(), |
| 322 | p.third_party_option.trim_space(), |
| 323 | p.lookup_path.str(), |
| 324 | ]) |
| 325 | // eprintln('prefs.cache_manager: ${p}') |
| 326 | // disable use_cache for specific cases: |
| 327 | if os.user_os() == 'windows' { |
| 328 | p.use_cache = false |
| 329 | } |
| 330 | if p.build_mode == .build_module { |
| 331 | // eprintln('-usecache and build-module flags are not compatible') |
| 332 | p.use_cache = false |
| 333 | } |
| 334 | if p.is_shared { |
| 335 | // eprintln('-usecache and -shared flags are not compatible') |
| 336 | p.use_cache = false |
| 337 | } |
| 338 | if p.bare_builtin_dir == '' && p.os == .wasm32 { |
| 339 | p.bare_builtin_dir = os.join_path(p.vroot, 'vlib', 'builtin', 'wasm_bare') |
| 340 | } else if p.bare_builtin_dir == '' { |
| 341 | p.bare_builtin_dir = os.join_path(p.vroot, 'vlib', 'builtin', 'linux_bare') |
| 342 | } |
| 343 | |
| 344 | $if prealloc { |
| 345 | if !p.no_parallel && p.is_verbose { |
| 346 | eprintln('disabling parallel cgen, since V was built with -prealloc') |
| 347 | } |
| 348 | p.no_parallel = true |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | fn is_v_compiler_target(npath string) bool { |
| 353 | target := npath.trim_right('/') |
| 354 | return target.ends_with('cmd/v') || target.ends_with('cmd/v/v.v') || target.ends_with('cmd/v2') |
| 355 | || target.ends_with('cmd/v2/v2.v') || target.ends_with('cmd/tools/vfmt.v') |
| 356 | } |
| 357 | |
| 358 | // is_v2_compiler_target reports whether the given resolved input path is |
| 359 | // building the v2 compiler itself (the `cmd/v2` directory or its `v2.v` |
| 360 | // entry point). Used to force `-gc none` for v2 since the v2 compiler |
| 361 | // manages its own arenas and breaks under boehm. |
| 362 | fn is_v2_compiler_target(npath string) bool { |
| 363 | target := npath.trim_right('/') |
| 364 | return target.ends_with('cmd/v2') || target.ends_with('cmd/v2/v2.v') |
| 365 | } |
| 366 | |
| 367 | // normalize_gc_defaults_for_resolved_ccompiler applies compiler-dependent |
| 368 | // defaults after the effective C compiler has been resolved. |
| 369 | pub fn (mut p Preferences) normalize_gc_defaults_for_resolved_ccompiler() { |
| 370 | p.prefer_openssl_for_bsd_tinyc() |
| 371 | p.disable_tcc_shared_backtraces() |
| 372 | if p.prealloc { |
| 373 | p.gc_mode = .no_gc |
| 374 | p.clear_gc_options() |
| 375 | return |
| 376 | } |
| 377 | if p.os != .windows || p.ccompiler_type != .msvc || p.gc_set_by_flag { |
| 378 | return |
| 379 | } |
| 380 | p.gc_mode = .no_gc |
| 381 | p.clear_gc_options() |
| 382 | } |
| 383 | |
| 384 | fn (p &Preferences) default_output_name(rpath string) string { |
| 385 | filename := os.file_name(rpath).trim_space() |
| 386 | mut base := filename.all_before_last('.') |
| 387 | if os.file_ext(base) in ['.c', '.js', '.wasm'] { |
| 388 | base = base.all_before_last('.') |
| 389 | } |
| 390 | if base == '' { |
| 391 | // The file name is just `.v` or `.vsh` or `.*` |
| 392 | base = filename |
| 393 | } |
| 394 | if needs_safe_default_output_name(base, filename, rpath) { |
| 395 | base = safe_default_output_name(filename) |
| 396 | } |
| 397 | if p.raw_vsh_tmp_prefix != '' { |
| 398 | return p.raw_vsh_tmp_prefix + '.' + base |
| 399 | } |
| 400 | return base |
| 401 | } |
| 402 | |
| 403 | fn needs_safe_default_output_name(base string, filename string, rpath string) bool { |
| 404 | if base == '' || base in ['.', '..', '-'] { |
| 405 | return true |
| 406 | } |
| 407 | if base == filename && filename.starts_with('.') && !os.is_dir(rpath) { |
| 408 | return true |
| 409 | } |
| 410 | if base.ends_with('.c') || base.ends_with('.js') || base.ends_with('.wasm') { |
| 411 | return true |
| 412 | } |
| 413 | for ch in base { |
| 414 | if ch < ` ` || ch == 127 { |
| 415 | return true |
| 416 | } |
| 417 | } |
| 418 | return false |
| 419 | } |
| 420 | |
| 421 | fn safe_default_output_name(filename string) string { |
| 422 | mut sanitized := strings.new_builder(filename.len + 4) |
| 423 | for ch in filename { |
| 424 | if ch < ` ` || ch == 127 { |
| 425 | sanitized.write_u8(`_`) |
| 426 | } else { |
| 427 | sanitized.write_u8(ch) |
| 428 | } |
| 429 | } |
| 430 | sanitized.write_string('.out') |
| 431 | return sanitized.str() |
| 432 | } |
| 433 | |
| 434 | fn (mut p Preferences) find_cc_if_cross_compiling() { |
| 435 | if p.os == get_host_os() { |
| 436 | return |
| 437 | } |
| 438 | if p.ccompiler_set_by_flag { |
| 439 | // Only mingw compilers can cross-compile for Windows (others lack Windows headers), |
| 440 | // so override any non-mingw compiler with the proper cross-compiler. |
| 441 | if p.os == .windows && !p.ccompiler.contains('mingw') { |
| 442 | p.ccompiler = p.vcross_compiler_name() |
| 443 | return |
| 444 | } |
| 445 | // Respect explicit `-cc` selection even in cross-compilation mode. |
| 446 | return |
| 447 | } |
| 448 | if p.os == .windows && p.ccompiler == 'msvc' { |
| 449 | // Allow for explicit overrides like `v -showcc -cc msvc -os windows file.v`, |
| 450 | // this makes flag passing more easily debuggable on other OSes too, not only |
| 451 | // on windows (building will stop later, when -showcc already could display all |
| 452 | // options). |
| 453 | return |
| 454 | } |
| 455 | p.ccompiler = p.vcross_compiler_name() |
| 456 | } |
| 457 | |
| 458 | fn (mut p Preferences) try_to_use_tcc_by_default() { |
| 459 | preferred_tcc := default_tcc_compiler() |
| 460 | if p.ccompiler == 'tcc' { |
| 461 | p.ccompiler = if preferred_tcc != '' { preferred_tcc } else { 'tcc' } |
| 462 | return |
| 463 | } |
| 464 | if p.ccompiler == '' { |
| 465 | // -prealloc uses thread-local allocator state. The bundled tcc does not |
| 466 | // support TLS declarations, so use the platform C compiler by default. |
| 467 | if p.prealloc { |
| 468 | return |
| 469 | } |
| 470 | // -d no_gc_thread_local_alloc forces the bundled source libgc path. The |
| 471 | // bundled tcc cannot compile that source reliably, so use the platform C |
| 472 | // compiler by default. An explicit -cc still wins. |
| 473 | if p.needs_source_boehm_without_thread_local_alloc() { |
| 474 | return |
| 475 | } |
| 476 | // use an optimizing compiler (i.e. gcc or clang) on -prod mode |
| 477 | if p.is_prod { |
| 478 | return |
| 479 | } |
| 480 | // The macOS bundled tcc is sensitive to Apple SDK/header changes and |
| 481 | // app/framework includes. Keep it available through explicit `-cc tcc`, |
| 482 | // but default to the platform compiler on macOS. |
| 483 | if get_host_os() == .macos { |
| 484 | return |
| 485 | } |
| 486 | p.ccompiler = preferred_tcc |
| 487 | return |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | fn usable_system_tcc_compiler() string { |
| 492 | if get_host_os() != .termux { |
| 493 | return '' |
| 494 | } |
| 495 | system_tcc := os.find_abs_path_of_executable('tcc') or { return '' } |
| 496 | tcc_probe := os.execute('${os.quoted_path(system_tcc)} -v') |
| 497 | if tcc_probe.exit_code != 0 { |
| 498 | return '' |
| 499 | } |
| 500 | return system_tcc |
| 501 | } |
| 502 | |
| 503 | fn usable_bundled_tcc_compiler(vroot string) string { |
| 504 | vtccexe := os.join_path(vroot, 'thirdparty', 'tcc', 'tcc.exe') |
| 505 | if !os.is_file(vtccexe) || !os.is_executable(vtccexe) { |
| 506 | return '' |
| 507 | } |
| 508 | // Unsupported hosts can still have a placeholder `thirdparty/tcc/` checkout. |
| 509 | tcc_probe := os.execute('${os.quoted_path(vtccexe)} -v') |
| 510 | if tcc_probe.exit_code != 0 { |
| 511 | return '' |
| 512 | } |
| 513 | return vtccexe |
| 514 | } |
| 515 | |
| 516 | // default_tcc_compiler returns the preferred TinyCC path when it exists and works on the host. |
| 517 | pub fn default_tcc_compiler() string { |
| 518 | vexe := vexe_path() |
| 519 | vroot := os.dir(vexe) |
| 520 | bundled_tcc := usable_bundled_tcc_compiler(vroot) |
| 521 | if bundled_tcc != '' { |
| 522 | return bundled_tcc |
| 523 | } |
| 524 | return usable_system_tcc_compiler() |
| 525 | } |
| 526 | |
| 527 | // windows_default_c_compiler picks the host C compiler to default to on Windows. |
| 528 | // `gcc` is the historical default, but plenty of Windows users only have the |
| 529 | // bundled tcc that `makev.bat` provisions under `thirdparty/tcc/tcc.exe`. |
| 530 | // Falling back to the bundled tcc when `gcc` isn't on PATH avoids confusing |
| 531 | // "'gcc' is not recognized as an internal or external command" errors during |
| 532 | // `v install`, `v outdated`, etc. (https://github.com/vlang/v/issues/27119). |
| 533 | fn windows_default_c_compiler(vroot string) string { |
| 534 | if os.find_abs_path_of_executable('gcc') or { '' } != '' { |
| 535 | return 'gcc' |
| 536 | } |
| 537 | bundled_tcc := usable_bundled_tcc_compiler(vroot) |
| 538 | if bundled_tcc != '' { |
| 539 | return bundled_tcc |
| 540 | } |
| 541 | return 'gcc' |
| 542 | } |
| 543 | |
| 544 | fn (mut p Preferences) clear_gc_options() { |
| 545 | p.compile_defines = p.compile_defines.filter(it !in windows_default_gc_defines) |
| 546 | p.compile_defines_all = p.compile_defines_all.filter(it !in windows_default_gc_defines) |
| 547 | for define in windows_default_gc_defines { |
| 548 | p.compile_values.delete(define) |
| 549 | } |
| 550 | mut build_options := []string{cap: p.build_options.len + 2} |
| 551 | mut i := 0 |
| 552 | for i < p.build_options.len { |
| 553 | option := p.build_options[i] |
| 554 | if option == '-gc' { |
| 555 | i += 2 |
| 556 | continue |
| 557 | } |
| 558 | if option.starts_with('-gc ') { |
| 559 | i++ |
| 560 | continue |
| 561 | } |
| 562 | if option.starts_with('-d ') { |
| 563 | define := option[3..].all_before('=') |
| 564 | if define in windows_default_gc_defines { |
| 565 | i++ |
| 566 | continue |
| 567 | } |
| 568 | } |
| 569 | build_options << option |
| 570 | i++ |
| 571 | } |
| 572 | build_options << ['-gc', 'none'] |
| 573 | p.build_options = build_options |
| 574 | } |
| 575 | |
| 576 | pub fn (mut p Preferences) default_c_compiler() { |
| 577 | // TODO: fix $if after 'string' |
| 578 | $if windows { |
| 579 | // These modes intentionally avoid bundled tcc (see |
| 580 | // try_to_use_tcc_by_default); preserve that here so the Windows fallback |
| 581 | // does not silently re-select an incompatible compiler. |
| 582 | if p.prealloc || p.is_prod || p.needs_source_boehm_without_thread_local_alloc() { |
| 583 | p.ccompiler = 'gcc' |
| 584 | return |
| 585 | } |
| 586 | p.ccompiler = windows_default_c_compiler(os.dir(vexe_path())) |
| 587 | return |
| 588 | } |
| 589 | if p.os == .ios { |
| 590 | $if !ios { |
| 591 | ios_sdk := if p.is_ios_simulator { 'iphonesimulator' } else { 'iphoneos' } |
| 592 | ios_sdk_path_res := os.execute_or_exit('xcrun --sdk ${ios_sdk} --show-sdk-path') |
| 593 | mut isysroot := ios_sdk_path_res.output.replace('\n', '') |
| 594 | arch := if p.is_ios_simulator { |
| 595 | '-arch x86_64 -arch arm64' |
| 596 | } else { |
| 597 | '-arch armv7 -arch armv7s -arch arm64' |
| 598 | } |
| 599 | // On macOS, /usr/bin/cc is a hardlink/wrapper for xcrun. clang on darwin hosts |
| 600 | // will automatically change the build target based off of the selected sdk, making xcrun -sdk iphoneos pointless |
| 601 | p.ccompiler = '/usr/bin/cc' |
| 602 | p.cflags = '-isysroot ${isysroot} ${arch}' + p.cflags |
| 603 | return |
| 604 | } |
| 605 | } |
| 606 | p.ccompiler = 'cc' |
| 607 | return |
| 608 | } |
| 609 | |
| 610 | pub fn (mut p Preferences) default_cpp_compiler() { |
| 611 | if p.ccompiler.contains('clang') { |
| 612 | p.cppcompiler = 'clang++' |
| 613 | return |
| 614 | } |
| 615 | p.cppcompiler = 'c++' |
| 616 | } |
| 617 | |
| 618 | fn resolve_vexe_path(vexe string) string { |
| 619 | if os.is_abs_path(vexe) { |
| 620 | return os.real_path(vexe) |
| 621 | } |
| 622 | if vexe.contains('/') || vexe.contains('\\') { |
| 623 | return os.real_path(os.abs_path(vexe)) |
| 624 | } |
| 625 | if found_vexe := os.find_abs_path_of_executable(vexe) { |
| 626 | return os.real_path(found_vexe) |
| 627 | } |
| 628 | return os.real_path(os.abs_path(vexe)) |
| 629 | } |
| 630 | |
| 631 | pub fn vexe_path() string { |
| 632 | vexe := os.getenv('VEXE') |
| 633 | if vexe != '' { |
| 634 | real_vexe_path := resolve_vexe_path(vexe) |
| 635 | os.setenv('VEXE', real_vexe_path, true) |
| 636 | return real_vexe_path |
| 637 | } |
| 638 | myexe := os.executable() |
| 639 | mut real_vexe_path := myexe |
| 640 | for { |
| 641 | $if tinyc { |
| 642 | $if x32 { |
| 643 | // TODO: investigate why exactly tcc32 segfaults on os.real_path here, |
| 644 | // and remove this cludge. |
| 645 | break |
| 646 | } |
| 647 | } |
| 648 | real_vexe_path = os.real_path(real_vexe_path) |
| 649 | break |
| 650 | } |
| 651 | os.setenv('VEXE', real_vexe_path, true) |
| 652 | return real_vexe_path |
| 653 | } |
| 654 | |
| 655 | pub fn (p &Preferences) vcross_linker_name() string { |
| 656 | vlname := os.getenv('VCROSS_LINKER_NAME') |
| 657 | if vlname != '' { |
| 658 | return vlname |
| 659 | } |
| 660 | $if macos { |
| 661 | return '/opt/homebrew/opt/llvm/bin/ld.lld' |
| 662 | } |
| 663 | $if windows { |
| 664 | return 'ld.lld.exe' |
| 665 | } |
| 666 | return 'ld.lld' |
| 667 | } |
| 668 | |
| 669 | pub fn (p &Preferences) vcross_compiler_name() string { |
| 670 | vccname := os.getenv('VCROSS_COMPILER_NAME') |
| 671 | if vccname != '' { |
| 672 | return vccname |
| 673 | } |
| 674 | if p.os == .windows { |
| 675 | if p.os == .freebsd { |
| 676 | return 'clang' |
| 677 | } |
| 678 | if p.m64 { |
| 679 | return 'x86_64-w64-mingw32-gcc' |
| 680 | } |
| 681 | return 'i686-w64-mingw32-gcc' |
| 682 | } |
| 683 | if p.os == .linux { |
| 684 | return 'clang' |
| 685 | } |
| 686 | if p.os == .freebsd { |
| 687 | return 'clang' |
| 688 | } |
| 689 | if p.os == .wasm32_emscripten { |
| 690 | if os.user_os() == 'windows' { |
| 691 | return 'emcc.bat' |
| 692 | } |
| 693 | return 'emcc' |
| 694 | } |
| 695 | if p.backend == .c && !p.out_name.ends_with('.c') { |
| 696 | eprintln('Note: V can only cross compile to Windows and Linux for now by default.') |
| 697 | eprintln('It will use `cc` as a cross compiler for now, although that will probably fail.') |
| 698 | eprintln('Set `VCROSS_COMPILER_NAME` to the name of your cross compiler, for your target OS: ${p.os} .') |
| 699 | } |
| 700 | return 'cc' |
| 701 | } |
| 702 | |
| 703 | // vroot_file reads the given file, given a path relative to @VEXEROOT . |
| 704 | // Its goal is to give all backends a shared infrastructure to read their own static preludes (like C headers etc), |
| 705 | // without each having to implement their own way of lookup/embedding/caching them. |
| 706 | pub fn (mut p Preferences) vroot_file(path string) string { |
| 707 | full_path := os.join_path(p.vroot, path) |
| 708 | return os.read_file(full_path) or { '/* missing @VEXEROOT content of path: ${full_path} */' } |
| 709 | } |
| 710 | |