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.

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 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

normalized_target_os supports normalized target os handling for Preferences. 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 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 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_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_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_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 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.

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'
	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
}

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.