module vmakepkg import os import json2 import vile.scheme // ------------------------------------------------------------ PKGBUILD.vsh // // PKGBUILD.vsh is the same package written as a plain V script, with the // phases as ordinary V functions: // // import vmakepkg // import os // // fn spec() !vmakepkg.Spec { // return vmakepkg.Spec{ // pkgname: 'foobar' // pkgver: '2.5' // pkgrel: 2 // pkgdesc: 'Text editor for X11' // arch: ['x86_64'] // url: 'https://example.com/foobar/' // license: 'Apache-2.0' // depends: ['glibc'] // options: ['!strip' 'docs'] // source: ['foobar-2.5.tar.gz::https://example.com/2.5.tar.gz'] // b2sums: ['...'] // hooks: ['post-install'] // } // } // // fn build() ! { // vmakepkg.run(['cmake', '-B', 'build', '-DCMAKE_INSTALL_PREFIX=/usr'])! // vmakepkg.run(['cmake', '--build', 'build'])! // } // // fn package() ! { // vmakepkg.run(['env', 'DESTDIR=' + vmakepkg.pkgdir()!, 'cmake', '--install', 'build'])! // } // // fn post_install(v []string) ! { // println('hint: run foobar-setup once') // } // // vmakepkg.vsh_entry( // spec: spec // build: build // package: package // hooks: {'post-install': post_install} // ) or { eprintln(err); exit(1) } // // vpac compiles the script once and invokes the executable for metadata, // phases and packaged install hooks. run takes an argv array. // Spec is a vsh package's head, the same fields the recipe format reads pub struct Spec { pub mut: pkgname string pkgnames []string pkgver string pkgrel i64 epoch i64 pkgdesc string arch []string url string license string depends []string makedepends []string checkdepends []string optdepends []string provides []string conflicts []string backup []string options []string source []string validpgpkeys []string b2sums []string sha256sums []string hooks []string } // spec_names reads pkgname from a spec: the bare name or the split list. // both spellings together are one intent, so the same name is not repeated, // the order of first mention kept fn spec_names(s &Spec) ![]string { mut named := []string{} if s.pkgname != '' { named << s.pkgname } named << s.pkgnames mut out := []string{} for it in named { if it in out { continue } out << it } if out.len == 0 { return error('pkgbuild: #:pkgname is required') } for it in out { if it.contains(' ') { return error('pkgbuild #:pkgname: "${it}" has whitespace in it') } } return out } // recipe_list wraps string items into a scheme list, the shape the head reader // walks for the array fields fn recipe_list(items []string) scheme.Value { mut out := []scheme.Value{} for it in items { out << scheme.string_value(it) } return scheme.list(out) } // spec_meta hands the spec to the same head the recipe format walks, as one // metadata: the lists normalised, the sums and fingerprints checked, and // the declared phases and hooks must match functions in the compiled entry. fn spec_meta(s &Spec, have map[string]bool) !PackageMeta { mut vals := map[string]scheme.Value{} names := spec_names(s)! // name is the pkgbase, the canonical first of the names: for a // split-only spec the bare pkgname spelling is empty and this is what // carries one vals['name'] = scheme.string_value(names[0]) vals['pkgname'] = recipe_list(names) if s.pkgver != '' { vals['pkgver'] = scheme.string_value(s.pkgver) } vals['pkgrel'] = scheme.integer(s.pkgrel) // as the recipe's #:epoch: 0 means absent, and a negative one is a mistake // to say, not an epoch to leave out if s.epoch < 0 { return error('pkgbuild #:epoch is negative; 0 means absent') } if s.epoch > 0 { vals['epoch'] = scheme.integer(s.epoch) } if s.pkgdesc == '' { return error('pkgbuild: #:pkgdesc is required') } vals['pkgdesc'] = scheme.string_value(s.pkgdesc) if s.arch.len == 0 { return error('pkgbuild: #:arch is required') } mut arches := []string{} for a in s.arch { arches << recipe_arch(a) } vals['arch'] = recipe_list(arches) if s.license != '' { vals['license'] = recipe_list([s.license]) } if s.url != '' { vals['url'] = scheme.string_value(s.url) } if s.license != '' { vals['license'] = scheme.string_value(s.license) } map2 := { 'depends': s.depends 'makedepends': s.makedepends 'checkdepends': s.checkdepends 'optdepends': s.optdepends 'provides': s.provides 'conflicts': s.conflicts 'options': s.options 'source': s.source } for key, items in map2 { if items.len > 0 { vals[key] = recipe_list(items) } } if s.backup.len > 0 { for b in s.backup { if b.starts_with('/') { return error('pkgbuild #:backup: "${b}" is absolute; backup paths are relative, no leading slash') } } vals['backup'] = recipe_list(s.backup) } if s.validpgpkeys.len > 0 { for i, k in s.validpgpkeys { recipe_key40(k, i)! } vals['validpgpkeys'] = recipe_list(s.validpgpkeys) } if s.b2sums.len > 0 { for it in s.b2sums { recipe_hex(it, 128, 'b2sums')! } vals['b2sums'] = recipe_list(s.b2sums) } if s.sha256sums.len > 0 { for it in s.sha256sums { recipe_hex(it, 64, 'sha256sums')! } vals['sha256sums'] = recipe_list(s.sha256sums) } for phase in ['prepare', 'build', 'check', 'package', 'pkgver'] { if have[phase] { vals[phase] = scheme.nil_value() } } for h in s.hooks { if h !in recipe_hooks { return error('pkgbuild hooks: #:${h} is not a hook makepkg knows') } if !have[h] { return error('pkgbuild hooks: the entry holds no ${h} body') } vals[h] = scheme.nil_value() } return PackageMeta{ values: vals } } // run executes a program with literal arguments and the phase's standard streams. pub fn run(argv []string) ! { if argv.len == 0 || argv[0] == '' || argv.any(it.contains('\x00')) { return error('run expects a program and literal arguments') } code := os.system_args(argv) if code != 0 { return error('${argv[0]} exited with status ${code}') } } // pkgdir is the staging directory for the current package. pub fn pkgdir() !string { p := os.getenv('VPAC_PKGDIR') if p == '' { return error('pkgdir: unavailable outside a recipe phase') } return p } // srcdir is where the sources were unpacked, $srcdir pub fn srcdir() !string { p := os.getenv('VPAC_SRCDIR') if p == '' { return error('srcdir: unavailable outside a recipe phase') } return p } // startdir is the build script's own directory, $startdir pub fn startdir() !string { p := os.getenv('VPAC_STARTDIR') if p == '' { return error('startdir: unavailable outside a recipe phase') } return p } // this_pkg is the split a shared package function is packing right now, // $pkgname, exported for the phase as VPAC_PKGNAME pub fn this_pkg() !string { p := os.getenv('VPAC_PKGNAME') if p == '' { return error('this_pkg: unavailable outside a recipe phase') } return p } // glob spills one pattern into a sorted list of matches, empty when nothing // matches pub fn glob(pattern string) ![]string { return os.glob(pattern)! } // a young tree can use the fallback until it has its first matching tag. pub fn git_desc(pattern string, fallback string) !string { res := os.exec(['git', '--no-pager', 'describe', '--long', '--tags', '--match', pattern]) if res.exit_code != 0 { if fallback != '' { return fallback } return error('git-desc: git describe failed for ${pattern}: ${res.output.trim_space()}') } return res.output.trim_space() } // VshEntry supplies the package spec, phase functions and named install hooks. @[params] pub struct VshEntry { pub mut: spec ?fn () !Spec pkgver ?fn () string prepare ?fn () ! build ?fn () ! check ?fn () ! package ?fn () ! hooks map[string]fn ([]string) ! } // vsh_entry dispatches the compiled recipe's metadata, phases and hooks. pub fn vsh_entry(e VshEntry) ! { // VPAC_VSH_PATH carries the script's own path: `v run` does not say which // script called in a way the writer can trust vsh_run(e, os.args#[1..], os.getenv('VPAC_VSH_PATH'))! } // vsh_run is vsh_entry with the command line and the script's path handed // in: args are what follows the script's name, the phase first fn vsh_run(e VshEntry, args []string, script_path string) ! { phase := if args.len > 0 { args[0] } else { '' } match phase { 'generate' { output := if args.len == 2 && args[1] != '' { args[1] } else if args.len == 1 && script_path != '' { os.join_path(os.dir(script_path), 'recipe.json') } else { return error('generate needs an output file or VPAC_VSH_PATH') } spec := e.spec or { return error('pkgbuild: a :spec is required') } s := spec()! // a data package can go straight to its package phase. if e.package == none { return error('pkgbuild: a :package phase is required') } if e.pkgver == none && s.pkgver == '' { return error('pkgbuild: :pkgver is required') } for h in s.hooks { if h !in e.hooks { return error('pkgbuild hooks: the entry holds no ${h} body') } } // hooks in the entry that the spec does not list stay a typo for name in e.hooks.keys() { if name !in s.hooks { return error('pkgbuild hooks: the spec does not list :${name}') } } mut have := map[string]bool{} if e.prepare != none { have['prepare'] = true } if e.pkgver != none { have['pkgver'] = true } for hook in e.hooks.keys() { have[hook] = true } if e.build != none { have['build'] = true } if e.check != none { have['check'] = true } // every recipe has a package phase. have['package'] = true meta := spec_meta(s, have)! desc := native_description(meta)! os.write_file(output, json2.encode(desc))! } '--nspkg-hook' { if args.len < 4 || args[1] !in recipe_hooks || args.len != 3 + recipe_hook_arity(args[1]) { return error('invalid native hook arguments') } os.setenv('VPAC_PKGNAME', args[2], true) if hook := e.hooks[args[1]] { hook(args[3..])! } } 'pkgver' { if f := e.pkgver { out := f() if out == '' { return error('pkgver: it returned an empty version') } println(out) } else { return error('pkgver: the entry holds no pkgver function') } } 'prepare' { if f := e.prepare { f()! } else { return error('prepare: the entry holds no prepare function') } } 'build' { if f := e.build { f()! } else { return error('build: the entry holds no build function') } } 'check' { if f := e.check { f()! } else { return error('check: the entry holds no check function') } } 'package' { if f := e.package { f()! } else { return error('package: the entry holds no package function') } } else { // no argument: the usage, not a call that lost its phase quietly if phase == '' { return error('vsh: give an argument: generate to describe the recipe, or one of the phases (build, check, package, pkgver) or hooks') } if phase in e.hooks { e.hooks[phase](args[1..])! return } return error('vsh: "${phase}" is neither generate, a phase, nor a hook') } } }