| 1 | // shared process and temporary-directory handling for the V verification tools. |
| 2 | module tooling |
| 3 | |
| 4 | import os |
| 5 | import rand |
| 6 | import nspkg.tests.signaturefixtures |
| 7 | |
| 8 | pub struct Options { |
| 9 | pub mut: |
| 10 | compiler string = 'v' |
| 11 | pacman string |
| 12 | sanitize bool |
| 13 | self_test bool |
| 14 | record bool |
| 15 | } |
| 16 | |
| 17 | pub fn options(needs_pacman bool) !Options { |
| 18 | mut result := Options{ compiler: os.getenv_opt('V') or { 'v' } } |
| 19 | mut index := 1 |
| 20 | for index < os.args.len { |
| 21 | arg := os.args[index] |
| 22 | match arg { |
| 23 | '--v' { |
| 24 | index++ |
| 25 | if index == os.args.len { return error('--v requires a compiler path') } |
| 26 | result.compiler = os.args[index] |
| 27 | } |
| 28 | '--sanitize' { result.sanitize = true } |
| 29 | '--self-test' { result.self_test = true } |
| 30 | '--record' { result.record = true } |
| 31 | '--help', '-h' { |
| 32 | println('usage: v run ${os.file_name(@FILE)} ${if needs_pacman { |
| 33 | 'PACMAN_CHECKOUT ' |
| 34 | } else { |
| 35 | '' |
| 36 | }}[--v COMPILER] [--sanitize] [--self-test] [--record]') |
| 37 | exit(0) |
| 38 | } |
| 39 | else { |
| 40 | if !needs_pacman || result.pacman != '' || arg.starts_with('-') { |
| 41 | return error('unexpected argument: ${arg}') |
| 42 | } |
| 43 | result.pacman = os.real_path(arg) |
| 44 | } |
| 45 | } |
| 46 | index++ |
| 47 | } |
| 48 | if needs_pacman && result.pacman == '' { return error('a pacman checkout path is required') } |
| 49 | return result |
| 50 | } |
| 51 | |
| 52 | pub fn temporary(prefix string) !string { |
| 53 | path := os.join_path(os.vtmp_dir(), '${prefix}-${rand.ulid()}') |
| 54 | os.mkdir(path, mode: 0o700)! |
| 55 | return path |
| 56 | } |
| 57 | |
| 58 | pub fn command(tool string, args []string) !string { |
| 59 | return signaturefixtures.command(tool, args) |
| 60 | } |
| 61 | |
| 62 | pub fn require_no_libalpm(executable string) ! { |
| 63 | libraries := command('ldd', [executable])! |
| 64 | if libraries.contains('libalpm') { |
| 65 | return error('native executable links libalpm:\n${libraries}') |
| 66 | } |
| 67 | symbols := command('nm', ['-u', executable])! |
| 68 | for line in symbols.split_into_lines() { |
| 69 | if line.trim_space().all_after_last(' ').starts_with('alpm_') { |
| 70 | return error('native executable references libalpm symbols:\n${symbols}') |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |