module vmakepkg import os import blip.vlib.blip // ------------------------------------------------------------ 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('cmake --install build DESTDIR=' + vmakepkg.pkgdir()!)! // } // // 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) } // // `v run PKGBUILD.vsh generate` writes the PKGBUILD (and the .install // beside the script), with the same head validation and phase wrappers the // lip format gets, delegating to `v run PKGBUILD.vsh `. a // `pkgver: fn () string` in the entry makes it a VCS package the same way a // pkgver lambda does for the lip. // // the writers for the phases are the runners' own helpers: vmakepkg.run // shells a line out with makepkg's spellings restored, vmakepkg.pkgdir and // friends read the values makepkg exported for the running phase. // Spec is a vsh package's head, the same fields the lip 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 } // lip_list wraps string items into a blip list, the shape the head reader // walks for the array fields fn lip_list(items []string) blip.Value { mut out := []blip.Value{} for it in items { out << blip.string(it) } return blip.list_from(out) } // spec_meta hands the spec to the same head the lip format walks, as one // struct value: the lists normalised, the sums and fingerprints checked, and // the phases and hooks the script carries named, so lip_generate tells one // truth about both kinds. `have` says which phases and hooks the entry // carries, since the head only prints wrappers the script can answer fn spec_meta(s &Spec, have map[string]bool) !blip.Value { mut vals := map[string]blip.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'] = blip.string(names[0]) vals['pkgname'] = lip_list(names) if s.pkgver != '' { vals['pkgver'] = blip.string(s.pkgver) } vals['pkgrel'] = blip.integer(s.pkgrel) // as the lip'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'] = blip.integer(s.epoch) } if s.pkgdesc == '' { return error('pkgbuild: :pkgdesc is required') } vals['pkgdesc'] = blip.string(s.pkgdesc) if s.arch.len == 0 { return error('pkgbuild: :arch is required') } mut arches := []string{} for a in s.arch { arches << lip_arch(a) } vals['arch'] = lip_list(arches) if s.url != '' { vals['url'] = blip.string(s.url) } if s.license != '' { vals['license'] = blip.string(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] = lip_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'] = lip_list(s.backup) } if s.validpgpkeys.len > 0 { for i, k in s.validpgpkeys { lip_key40(k, i)! } vals['validpgpkeys'] = lip_list(s.validpgpkeys) } if s.b2sums.len > 0 { for it in s.b2sums { lip_hex(it, 128, 'b2sums')! } vals['b2sums'] = lip_list(s.b2sums) } if s.sha256sums.len > 0 { for it in s.sha256sums { lip_hex(it, 64, 'sha256sums')! } vals['sha256sums'] = lip_list(s.sha256sums) } for phase in ['prepare', 'build', 'check', 'package', 'pkgver'] { if have[phase] { vals[phase] = blip.nil_value() } } for h in s.hooks { if h !in lip_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] = blip.nil_value() } return blip.struct_value('pkgbuild', vals.keys(), vals) } // lip_make_from_spec generates the PKGBUILD (and a .install when hooks are // carried) from a PKGBUILD.vsh's spec and its entry's phase inventory, // delegating the phases to `v run` against the script itself pub fn lip_make_from_spec(s &Spec, runner string, script_path string, have map[string]bool) !LipBet { meta := spec_meta(s, have)! return lip_generate(meta, runner, script_path, .vsh)! } // run shells one line out with sh, carrying makepkg's spellings of its state // into the child shell, like the runner's (run) does from the lip side: the // phase exported them for this process under the VPAC_ names pub fn run(cmd string) ! { // the names reach the shell as env's arguments and the line as sh's: no // quoting stands between them and the child. the line itself is shell, // as the script writes it mut args := ['env'] pairs := [['pkgdir', 'VPAC_PKGDIR'], ['srcdir', 'VPAC_SRCDIR'], ['startdir', 'VPAC_STARTDIR'], ['pkgname', 'VPAC_PKGNAME'], ['pkgver', 'VPAC_PKGVER']] for pair in pairs { v := os.getenv(pair[1]) if v != '' { args << '${pair[0]}=${v}' } } args << ['sh', '-c', cmd] res := os.system_args(args) if res != 0 { return error('run: exit code ${res} from: ${cmd}') } } // pkgdir is the directory makepkg is filling during the package phase, the // value of $pkgdir, exported for the phase as VPAC_PKGDIR pub fn pkgdir() !string { p := os.getenv('VPAC_PKGDIR') if p == '' { return error('pkgdir: the runner only exports it inside a makepkg 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: the runner only exports it inside a makepkg 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: the runner only exports it inside a makepkg 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: the runner only exports it inside a makepkg 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)! } // git_desc runs git describe in the cwd the phase runs under (srcdir, where // makepkg runs its phases); the fallback comes out verbatim when describe // finds nothing, so a young tree still has a version before its first 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 is what a PKGBUILD.vsh hands to vsh_entry: the spec, the phase // functions, and the hooks by name. generate needs the spec, and makepkg // needs package (build only when the package has one, as makepkg's build()): // the entry says so if the script leaves one out @[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 is the PKGBUILD.vsh runner, the script's one last line: with the // generate argument it writes the PKGBUILD and the .install beside the // script, and otherwise it runs one phase with the arguments pacman or the // phase wrapper hands it. a failing phase exits nonzero, the way a makepkg // phase would 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' { if script_path == '' { return error("generate: VPAC_VSH_PATH must hold the script's path") } spec := e.spec or { return error('pkgbuild: a :spec is required') } s := spec()! // build is optional, as makepkg's build() is; package is not 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 } // package is makepkg's ground truth, so its wrapper always lands have['package'] = true // the runner the wrappers bake is the persistent v, not this // process's temp binary: the root side carries it as VPAC_V vpath := if os.getenv('VPAC_V') != '' { os.getenv('VPAC_V') } else { os.find_abs_path_of_executable('v') or { return error('generate: the v compiler is not in the path') } } bb := lip_make_from_spec(s, vpath, script_path, have)! out_dir := os.dir(script_path) os.write_file('${out_dir}/PKGBUILD', bb.text)! if bb.install != '' { os.write_file('${out_dir}/${bb.base}.install', bb.install)! } mut wrote := '${out_dir}/PKGBUILD' if bb.install != '' { wrote += ' and ${bb.base}.install' } println('wrote ${wrote}') } '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 write the PKGBUILD, 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') } } }