Skip to content

v.pref

Constants #

const supported_test_runners = ['normal', 'simple', 'tap', 'dump', 'teamcity']

supported_test_runners lists the reporter implementations that the v test -test-runner <name> option accepts.

const known_backend_suffixes = ['c', 'js', 'native', 'wasm']

known_backend_suffixes lists the backend names that a .<backend>.v file suffix may use. They take precedence over known_arch_names when a suffix is read, because wasm spells both a backend and an architecture and AGENTS.md documents *.wasm.v as the WASM backend split. Reading it as an architecture instead both hides the file from -b wasm and makes every native host skip it as foreign. arm64 is deliberately absent: foo_test.arm64.v names an architecture, while the native backend is spelled native in a file name.

const known_arch_names = ['amd64', 'x64', 'x86_64', 'arm64', 'aarch64', 'x86', 'i386', 'i486',
	'i586', 'i686', 'x32', 'x86_32', 'ia-32', 'ia32', 'arm32', 'aarch32', 'arm', 'armv7', 'armv7l',
	'rv32', 'risc-v32', 'riscv32', 'rv64', 'risc-v64', 'risc-v', 'riscv', 'riscv64', 'ppc', 'ppc32',
	'powerpc', 'ppc64', 'ppc64le', 's390x', 'loongarch64', 'sparc64', 'wasm', 'wasm32']

known_arch_names lists every architecture name that a _<arch>.v file suffix or an -arch <name> value may use, aliases included.

const native_backend_replacement_message = '`-b native` has been replaced by `-b arm64` and `-b x64`.\n' +
	'Enable ARM64 support with `v -compile-backend arm64 self`, then use `v -b arm64 -gc none file.v`.\n' +
	'On Linux AMD64, first run `v -compile-backend x64 self`, then `v -b x64 file.v`.'

native_backend_replacement_message explains how to replace the removed native backend option.

const module_search_stop_marker = '.v.mod.stop'

module_search_stop_marker is the file that explicitly ends the module search in a directory: the module lookups that walk up from a file do not continue above a directory that holds it. It is what a test session's temp directory carries, so that tests never resolve against whatever contains that directory.

const cross_target_c_macros = {
	'windows':           '_WIN32'
	'qnx':               '__QNX__'
	'serenity':          '__serenity__'
	'vinix':             '__vinix__'
	'freebsd':           '__FreeBSD__'
	'openbsd':           '__OpenBSD__'
	'netbsd':            '__NetBSD__'
	'dragonfly':         '__DragonFly__'
	'termux':            '__TERMUX__'
	'solaris':           '__sun'
	'haiku':             '__HAIKU__'
	'wasm32_emscripten': '__EMSCRIPTEN__'
	'wasm32':            '__wasm32__'
	'tinyc':             '__TINYC__'
	'clang':             '__clang__'
	'mingw':             '__MINGW32__'
	'msvc':              '_MSC_VER'
	'cplusplus':         '__cplusplus'
	'amd64':             '__V_amd64'
	'aarch64':           '__V_arm64'
	'arm64':             '__V_arm64'
	'arm32':             '__V_arm32'
	'i386':              '__V_x86'
	'x86':               '__V_x86'
	'rv64':              '__V_rv64'
	'riscv64':           '__V_rv64'
	'rv32':              '__V_rv32'
	'riscv32':           '__V_rv32'
	's390x':             '__V_s390x'
	'ppc64le':           '__V_ppc64le'
	'ppc64':             '__V_ppc64'
	'ppc':               '__V_ppc'
	'loongarch64':       '__V_loongarch64'
	'sparc64':           '__V_sparc64'
	'x64':               'TARGET_IS_64BIT'
	'x32':               'TARGET_IS_32BIT'
	'little_endian':     'TARGET_ORDER_IS_LITTLE'
	'big_endian':        'TARGET_ORDER_IS_BIG'
}

cross_target_c_macros maps a target-dependent $if flag to the single C preprocessor macro that decides it. Flags whose C spelling needs more than one macro (because a compiler defines several of them at once) are handled in cross_target_c_condition instead. The architecture and word-size macros are the ones write_arch_macros derives from the C compiler's own target, so one portable snapshot stays correct on every platform it is later compiled on.

Only flags that comptime_flag_value itself decides from the target belong here: everything else ($if glibc, $if mach, ...) is a user define there and must stay resolved while generating, or portable output would give it a different meaning than an ordinary build does.

fn arch_from_string #

fn arch_from_string(name string) ?string

arch_from_string returns the canonical name of the architecture that name spells, or none when name does not name an architecture at all (c and js name a backend).

fn ccompiler_can_assemble #

fn ccompiler_can_assemble(ccompiler string) bool

ccompiler_can_assemble reports whether ccompiler is a GCC or Clang compatible driver, one that preprocesses and assembles a .S file with -c. tcc and MSVC cannot; a cc or gcc name is trusted only after its version output rules out an alias for tcc.

fn ccompiler_is_tinycc #

fn ccompiler_is_tinycc(ccompiler string) bool

ccompiler_is_tinycc reports whether ccompiler is TinyCC. It resolves the path and any symlink, then checks the file name with tinycc_file_name. Any other executable is asked for its version banner, so a copy of tcc under another name is still recognised. Mentions of TinyCC in metadata such as an installed directory do not identify a compiler. A name that does not resolve to an executable is judged by its name alone. Local launchable files precede PATH. Windows also permits loadable PE files with other extensions.

fn ccompiler_version_is_tinycc #

fn ccompiler_version_is_tinycc(output string) bool

ccompiler_version_is_tinycc reports whether output contains a TinyCC version or branding banner line. Paths and other metadata containing the compiler's name are ignored.

fn comptime_flag_is_target_dependent #

fn comptime_flag_is_target_dependent(name string) bool

comptime_flag_is_target_dependent reports whether a $if flag is decided by the target rather than by the build, i.e. whether portable C has to defer it to the C preprocessor.

fn comptime_flag_value #

fn comptime_flag_value(p &Preferences, name string) bool

comptime_flag_value supports comptime flag value handling for pref.

fn comptime_optional_flag_value #

fn comptime_optional_flag_value(p &Preferences, name string) bool

comptime_optional_flag_value supports comptime optional flag value handling for pref.

fn comptime_pkgconfig_value #

fn comptime_pkgconfig_value(name string) bool

comptime_pkgconfig_value supports comptime pkgconfig value handling for pref.

fn cross_target_c_condition #

fn cross_target_c_condition(name string) ?string

cross_target_c_condition returns the C preprocessor expression deciding a target-dependent $if flag, or none when the flag is target independent and can still be resolved while generating portable C.

The expressions mirror comptime_flag_value, mutual exclusions included. A C compiler targeting Android defines __linux__ next to __ANDROID__, and the iOS SDK defines __APPLE__ next to the iOS marker, but V treats those as distinct targets, so each broader guard excludes the narrower one. Without that, os.user_os() - which tests $if linux before $if android - would report linux on Android and select Linux-only code everywhere else.

fn darwin_release_rejects_tcc_executables #

fn darwin_release_rejects_tcc_executables(release string) bool

darwin_release_rejects_tcc_executables reports whether a Darwin kernel release, as uname -r prints it, can load TCC-linked executables with corrupted globals. A release that does not start with a decimal major version is not rejected.

fn default_thread_stack_size_for_bits #

fn default_thread_stack_size_for_bits(pointer_bits int) int

default_thread_stack_size_for_bits returns the spawned-thread stack size for a target with pointer_bits-bit pointers.

fn directive_target_arch #

fn directive_target_arch(prefix string) ?string

directive_target_arch returns the architecture a #flag or #include target prefix (#flag amd64 -msse2) names, or none when the word is not such a prefix.

fn directive_target_os #

fn directive_target_os(prefix string) ?string

directive_target_os returns the operating system a #flag or #include target prefix (#flag linux -lm) names, or none when the word is not such a prefix.

fn expand_module_search_paths #

fn expand_module_search_paths(spec string, vroot string) []string

expand_module_search_paths expands the value of -path: the search roots are separated by | (or the OS path delimiter), @vlib and @vmodules stand for the vlib folder and the global module folders, and @vroot for the V root folder.

fn file_has_incompatible_os_suffix #

fn file_has_incompatible_os_suffix(file string, current_os string) bool

file_has_incompatible_os_suffix converts file has incompatible os suffix data for pref.

fn file_has_incompatible_target_suffix #

fn file_has_incompatible_target_suffix(file string, target Target) bool

fn find_system_assembler #

fn find_system_assembler() ?string

find_system_assembler returns a GCC or Clang compatible compiler driver found on PATH, one that can assemble a preprocessed .S file, or none.

fn get_build_facts_and_defines #

fn get_build_facts_and_defines() ([]string, []string)

get_build_facts_and_defines returns the build facts and the compile time defines of the current build, as recorded in the VBUILD_FACTS and VBUILD_DEFINES environment variables. The V driver reads the same two variables when it evaluates the // vtest build: constraints of a file, so test runners use this to construct the very same environment.

fn get_test_v_files_from_dir #

fn get_test_v_files_from_dir(dir string, user_defines []string, backend string, target_os string) []string

get_test_v_files_from_dir returns backend/target/define-compatible test files in dir.

fn get_test_v_files_from_dir_for_target #

fn get_test_v_files_from_dir_for_target(dir string, user_defines []string, backend string, target Target) []string

get_test_v_files_from_dir_for_target returns tests compatible with the complete target.

fn get_v_files_from_dir #

fn get_v_files_from_dir(dir string, user_defines []string, target_os string) []string

get_v_files_from_dir returns get v files from dir data for pref.

fn get_v_files_from_dir_for_backend_target #

fn get_v_files_from_dir_for_backend_target(dir string, user_defines []string, backend string,
	target Target) []string

get_v_files_from_dir_for_backend_target returns sources compatible with the complete target and with the file family of backend. The empty backend has no family of its own and keeps every candidate, which is the list the callers that do not know the backend had before.

fn get_v_files_from_dir_for_target #

fn get_v_files_from_dir_for_target(dir string, user_defines []string, target Target) []string

get_v_files_from_dir_for_target returns sources compatible with the complete target.

fn has_macos_v3_caller_environment #

fn has_macos_v3_caller_environment() bool

has_macos_v3_caller_environment reports whether the macOS driver transported the environment that should remain visible to compiled programs.

fn host_arch #

fn host_arch() string

host_arch returns the normalized architecture of the compiler process.

fn host_os_name #

fn host_os_name() string

host_os_name returns the OS name of the machine running the compiler, spelled the way the _<os>.v file suffixes and the // vtest build: constraints spell it. Termux is reported apart from Android: it runs natively on the device, so its toolchain has no access to the Android SDK headers.

fn host_rejects_tcc_executables #

fn host_rejects_tcc_executables() bool

host_rejects_tcc_executables reports whether this Darwin release was affected by the historical TinyCC Mach-O zero-fill bug. Retained for compatibility; compiler selection no longer consults this version check. Fixed TCC bundles can run on these releases.

fn host_target #

fn host_target() Target

host_target returns the platform on which the compiler process is running.

fn is_module_search_stop_dir #

fn is_module_search_stop_dir(dir string) bool

is_module_search_stop_dir reports whether dir holds the module_search_stop_marker.

fn is_retired_modules_namespace #

fn is_retired_modules_namespace(dir string, module_resolution_root string) bool

is_retired_modules_namespace reports whether a directory is the modules/ a project used to keep its modules in -- the virtual lookup root this compiler no longer searches. module_resolution_root is the root holding the active entry sources. A modules ancestor of that root is a project which merely carries the name; a sibling beneath the same project root is the retired namespace.

fn is_supported_arch #

fn is_supported_arch(arch string) bool

is_supported_arch reports whether arch is a canonical architecture name that a target can be built for; spellings such as x86_64 must be passed through normalized_arch first.

fn is_test_file_for_backend #

fn is_test_file_for_backend(path string, backend string) bool

fn is_test_file_for_platform #

fn is_test_file_for_platform(path string, backend string, target Target) bool

is_test_file_for_platform reports whether path is a test for backend and target.

fn is_test_file_for_target #

fn is_test_file_for_target(path string, backend string, target_os string) bool

is_test_file_for_target reports whether path is a test file for backend that is compatible with target_os. Platform-qualified tests use names such as foo_windows_test.v and must not be added to a non-Windows test harness.

fn macos_v3_caller_env_value #

fn macos_v3_caller_env_value(name string) string

macos_v3_caller_env_value returns the caller-visible value for compile-time $env.

fn macos_v3_caller_environment #

fn macos_v3_caller_environment() map[string]string

macos_v3_caller_environment returns the environment that delegated run children should see.

fn new_preferences #

fn new_preferences() &Preferences

new_preferences supports new preferences handling for pref.

fn normalized_arch #

fn normalized_arch(target_arch string) string

normalized_arch canonicalizes common architecture aliases.

fn normalized_os #

fn normalized_os(target_os string) string

normalized_os supports normalized os handling for pref.

fn option_may_consume_value #

fn option_may_consume_value(option string) bool

option_may_consume_value reports whether an option can consume the following argument.

fn os_is_target_of #

fn os_is_target_of(this_os string, target string) bool

os_is_target_of reports whether this_os is one of the systems named by the OS suffix target, for example nix names Linux and FreeBSD, but not Windows.

fn pkgconfig_flags_args #

fn pkgconfig_flags_args(raw string) ![]string

pkgconfig_flags_args builds the argument vector for a #pkgconfig flag query. Explicit compiler/linker flag filters must not be widened to --cflags --libs. With no flag filter, retain both defaults, including for --static shorthand.

fn resolve_module_alias_path #

fn resolve_module_alias_path(search_root string, mod string) ?string

resolve_module_alias_path resolves @[alias: 'path'] module name declarations stored in alias.v. An alias applies to submodules as well.

fn set_build_flags_and_defines #

fn set_build_flags_and_defines(facts []string, defines []string)

set_build_flags_and_defines records the build facts and the compile time defines in the VBUILD_FACTS and VBUILD_DEFINES environment variables, for the test runners and compilers that this process starts.

fn suffix_is_backend_name #

fn suffix_is_backend_name(name string) bool

suffix_is_backend_name reports whether a .<name>.v suffix names a backend.

fn supported_test_runners_list #

fn supported_test_runners_list() string

supported_test_runners_list returns supported_test_runners, formatted for a diagnostic.

fn target_from #

fn target_from(os_name string, arch_name string) !Target

target_from validates and canonicalizes an OS/architecture pair.

fn tcc_with_prod_requested #

fn tcc_with_prod_requested(args []string) bool

tcc_with_prod_requested reports whether a production C build selects TinyCC. Compiler option values are skipped and the last compiler/backend options win. Executable aliases follow ccompiler_is_tinycc in the caller's working directory.

fn tcc_with_prod_requested_in #

fn tcc_with_prod_requested_in(args []string, cwd string) bool

tcc_with_prod_requested_in reports whether a production C build selects TinyCC in cwd. Relative compiler paths and wrapper version queries use cwd without changing the caller's working directory. Compiler option values are skipped and the last compiler/backend win.

fn tinycc_file_name #

fn tinycc_file_name(file_name string) bool

tinycc_file_name reports whether file_name is tcc or tinyc, with an optional .exe and an optional target prefix such as x86_64-w64-mingw32-tcc. A name that only contains tcc, such as distcc or notcc, is not TinyCC.

fn vexe_path #

fn vexe_path() string

vexe_path returns the absolute path of the V compiler executable. Tools that V launches inherit it through $VEXE; everything else falls back to detection from the running executable.

fn vmod_root_for_dir #

fn vmod_root_for_dir(start string) ?string

vmod_root_for_dir returns the nearest directory at or above start that holds a v.mod. A .v.mod.stop marker or a .git, .hg or .svn entry ends the walk.

fn Subsystem.from #

fn Subsystem.from[W](input W) !Subsystem

from returns the Subsystem value of an integer or of a field name, or an error if there is none.

enum Subsystem #

enum Subsystem {
	auto
	console
	windows
}

Subsystem selects the Windows executable subsystem.

struct Preferences #

struct Preferences {
pub mut:
	verbose        bool
	no_parallel    bool
	output_file    string
	target         Target = host_target()
	user_defines   []string
	compile_values map[string]string
	backend        string = 'c'
	// fastc_native_bridge selects separate native ownership for executable output.
	fastc_native_bridge   bool
	ccompiler             string = 'gcc'
	c99                   bool
	force_bounds_checking bool
	enable_globals        bool
	vroot                 string = detect_vroot()
	vexe                  string = detect_vexe()
	vhash                 string
	vcurrent_hash         string
	selfhost              bool
	building_v            bool // compiling the V compiler itself: no generics, skip monomorphization
	is_prod               bool
	warn_about_allocs     bool
	is_debug              bool
	is_test               bool // at least one compatible user test file is being compiled
	is_fmt                bool // preserve source-only syntax needed by the V formatter
	migrate_json2         bool // rewrite supported legacy json calls while formatting
	is_livemain           bool
	is_liveshared         bool
	is_shared             bool
	subsystem             Subsystem
	no_builtin            bool
	no_preludes           bool
	module_search_paths   []string
	// module_resolution_root is the directory that owns the entry sources. It
	// distinguishes a project's retired `modules/` lookup level from a project
	// whose own root happens to carry that name.
	module_resolution_root string
	thread_stack_size      int = 8 * 1024 * 1024
	// target_libc_headers marks a target that supplies the C library headers
	// itself, so the generated C must include them rather than restate what they
	// declare. A kernel, compiling `-nostdinc` against its own header tree, is the
	// case. It cannot be inferred from the target OS, which also hosts ordinary
	// programs that link the host's libc. Distinct from V1's `-freestanding`,
	// which means no libc at all.
	target_libc_headers bool
	// V3 backends currently do not lower V inline-assembly nodes. Keep this an
	// explicit capability so guarded stdlib assembly selects its software path.
	supports_inline_asm            bool
	preserve_comptime_conditionals bool
	// exclude holds the `-exclude` glob patterns, already expanded for `@vroot`,
	// `@vlib` and `@vmodules`. A source file whose path matches one of them is
	// dropped from every directory listing, e.g. `-exclude @vlib/math/*.c.v`
	// selects the pure V implementations of the math module.
	exclude []string
	// output_cross_c requests portable C that is not tied to one target OS,
	// architecture or C compiler (`-os cross`). Target-dependent `$if` branches
	// are all kept and decided by the C preprocessor instead of by the checker.
	output_cross_c bool
	// raw_vsh_file is the real path of an input without the `.vsh` extension that
	// is still compiled as a V script (`-raw-vsh-tmp-prefix`).
	raw_vsh_file string
pub:
	build_date      string
	build_time      string
	build_timestamp string
pub mut:
	// hot_fn extracts raw machine code for a single function only (hot reload).
	hot_fn string
}

Preferences represents preferences data used by pref.

fn (Preferences) without_excluded #

fn (p &Preferences) without_excluded(files []string) []string

without_excluded returns files, minus the ones matched by a -exclude pattern.

fn (Preferences) get_vlib_module_path #

fn (p &Preferences) get_vlib_module_path(mod string) string

get_vlib_module_path returns get vlib module path data for Preferences.

fn (Preferences) get_module_path #

fn (p &Preferences) get_module_path(mod string, importing_file_path string) string

get_module_path returns get module path data for Preferences.

fn (Preferences) installed_module_roots #

fn (p &Preferences) installed_module_roots() []string

installed_module_roots returns the directories modules are installed into: vlib and the user's vmodules directories. Explicit -path roots are not included because they may contain modules owned by the current project.

fn (Preferences) normalized_target_os #

fn (p &Preferences) normalized_target_os() string

fn (Preferences) normalized_target_arch #

fn (p &Preferences) normalized_target_arch() string

normalized_target_arch returns the canonical target architecture.

fn (Preferences) comptime_platform #

fn (p &Preferences) comptime_platform() string

comptime_platform returns the established @PLATFORM name for the selected target.

fn (Preferences) is_cross_target #

fn (p &Preferences) is_cross_target() bool

is_cross_target reports whether is cross target applies in pref.

struct Target #

struct Target {
pub:
	os            string
	arch          string
	abi           string
	endian        string
	pointer_bits  int
	object_format string
}

Target is the canonical description of the platform for which code is generated. Host properties must not be used for target-dependent source selection or semantics.

fn (Target) default_thread_stack_size #

fn (target Target) default_thread_stack_size() int

default_thread_stack_size returns the target-specific spawned-thread stack size.