// shared process and temporary-directory handling for the V verification tools. module tooling import os import rand import nspkg.tests.signaturefixtures pub struct Options { pub mut: compiler string = 'v' pacman string sanitize bool self_test bool record bool } pub fn options(needs_pacman bool) !Options { mut result := Options{ compiler: os.getenv_opt('V') or { 'v' } } mut index := 1 for index < os.args.len { arg := os.args[index] match arg { '--v' { index++ if index == os.args.len { return error('--v requires a compiler path') } result.compiler = os.args[index] } '--sanitize' { result.sanitize = true } '--self-test' { result.self_test = true } '--record' { result.record = true } '--help', '-h' { println('usage: v run ${os.file_name(@FILE)} ${if needs_pacman { 'PACMAN_CHECKOUT ' } else { '' }}[--v COMPILER] [--sanitize] [--self-test] [--record]') exit(0) } else { if !needs_pacman || result.pacman != '' || arg.starts_with('-') { return error('unexpected argument: ${arg}') } result.pacman = os.real_path(arg) } } index++ } if needs_pacman && result.pacman == '' { return error('a pacman checkout path is required') } return result } pub fn temporary(prefix string) !string { path := os.join_path(os.vtmp_dir(), '${prefix}-${rand.ulid()}') os.mkdir(path, mode: 0o700)! return path } pub fn command(tool string, args []string) !string { return signaturefixtures.command(tool, args) } pub fn require_no_libalpm(executable string) ! { libraries := command('ldd', [executable])! if libraries.contains('libalpm') { return error('native executable links libalpm:\n${libraries}') } symbols := command('nm', ['-u', executable])! for line in symbols.split_into_lines() { if line.trim_space().all_after_last(' ').starts_with('alpm_') { return error('native executable references libalpm symbols:\n${symbols}') } } }