module vmakepkg import json2 import os fn C.mkdtemp(&char) &char fn private_dir(parent string) !string { path := os.join_path(parent, '.vpac-XXXXXX').clone() if C.mkdtemp(&char(path.str)) == unsafe { nil } { return os.last_error() } return path } fn command_output(argv []string) !string { result := os.exec(argv) if result.exit_code != 0 { return error('${argv[0]} exited with status ${result.exit_code}: ${result.output.trim_space()}') } return result.output.trim_space() } fn filename_part(value string) bool { return value.len > 0 && value !in ['.', '..'] && !value.starts_with('-') && value.bytes().all(it.is_alnum() || it in [`@`, `_`, `+`, `.`, `-`]) } // the builder has a fresh private working directory per invocation. the caller // receives the completed split set only after every archive and signature exists. pub fn native_build(path string, sign bool) ![]string { if os.geteuid() == 0 { return error('native package builds require an unprivileged user') } recipe := os.abs_path(path) if os.base(recipe) !in ['PKGBUILD.scm', 'PKGBUILD.vile', 'PKGBUILD.vsh'] { return error('native build expects PKGBUILD.scm, PKGBUILD.vile or PKGBUILD.vsh') } start := os.dir(recipe) work := private_dir(start)! mut complete := false defer { if !complete { os.rmdir_all(work) or { eprintln('remove incomplete build: ${err}') } } } src := os.join_path(work, 'src') os.mkdir(src)! mut runner := [os.executable(), '--recipe-run', recipe] mut desc := NativeRecipe{} if os.file_ext(recipe) == '.vsh' { mut compiler := os.getenv('VPAC_V') if compiler == '' { compiler = 'v' } exe := os.join_path(work, 'vsh-runner') mut compile := [compiler] // this executable and its hooks target the builder's ABI. supplying it // keeps V from invoking the host's ldd shell script to guess the libc. $if linux && glibc ? { compile << '-glibc' } $else $if linux && musl ? { compile << '-musl' } compile << ['-skip-running', '-o', exe, recipe] run(compile)! description := os.join_path(work, 'recipe.json') run([exe, 'generate', description])! desc = json2.decode[NativeRecipe](os.read_file(description)!)! runner = [exe] } else { desc = native_description(recipe_load(recipe)!)! } spec := desc.spec if spec.pkgnames.len == 0 || spec.pkgnames.any(!filename_part(it)) || spec.pkgrel < 1 { return error('native recipe needs valid package names and a positive pkgrel') } host_arch := os.uname().machine pkg_arch := if host_arch in spec.arch { host_arch } else if 'any' in spec.arch { if os.file_ext(recipe) == '.vsh' && spec.hooks.len > 0 { host_arch } else { 'any' } } else { return error('package does not support ${host_arch}') } mut deps := spec.depends.clone() deps << spec.makedepends deps << spec.checkdepends if deps.len > 0 { mut query := [os.executable(), '--makepkg-query', '-T'] query << deps command_output(query)! } for opt in spec.options { if opt !in ['docs', '!strip', '!debug'] { return error('unsupported native package option: ${opt}') } } native_sources(spec, start, src)! original := os.getwd() os.chdir(src)! defer { os.chdir(original) or { eprintln('restore working directory: ${err}') } } os.setenv('VPAC_STARTDIR', start, true) os.setenv('VPAC_SRCDIR', src, true) os.setenv('VPAC_PKGNAME', spec.pkgnames[0], true) os.setenv('VPAC_PKGVER', spec.pkgver, true) os.unsetenv('VPAC_PKGDIR') if 'prepare' in desc.phases { run(phase_args(runner, 'prepare'))! } version := if desc.dynamic { command_output(phase_args(runner, 'pkgver'))! } else { spec.pkgver } if !filename_part(version) || version.contains('-') { return error('invalid pkgver: ${version}') } os.setenv('VPAC_PKGVER', version, true) for phase in ['build', 'check'] { if phase in desc.phases { run(phase_args(runner, phase))! } } hook := if spec.hooks.len == 0 { '' } else if os.file_ext(recipe) == '.vsh' { runner[0] } else { write_recipe_hook(recipe, work, desc.hook_files)! } pkgs := os.join_path(work, 'pkg') os.mkdir(pkgs)! fullver := if spec.epoch > 0 { '${spec.epoch}:${version}-${spec.pkgrel}' } else { '${version}-${spec.pkgrel}' } mut archives := []string{} for name in spec.pkgnames { pkg := os.join_path(pkgs, name) os.mkdir(pkg)! os.setenv('VPAC_PKGDIR', pkg, true) os.setenv('VPAC_PKGNAME', name, true) run(phase_args(runner, 'package'))! archive := os.join_path(work, '${name}-${version}-${spec.pkgrel}-${pkg_arch}.pkg.tar.zst') native_archive(desc, name, fullver, pkg_arch, pkg, archive, hook, recipe)! if sign { run(['gpg', '--batch', '--detach-sign', '--output', archive + '.sig', archive])! } archives << archive } complete = true return archives } fn phase_args(runner []string, phase string) []string { mut args := runner.clone() args << phase return args }