// vmakepkg holds the build side of blip PKGBUILD.lip files and V-script // PKGBUILD.vsh files. vpac is its one user: the build mode (vpac b) and the // --lip-run entry the generated PKGBUILDs call back through; the rest of // the repo reaches it only by running vpac: // // (defpkgbuild foobar ...) lives under pkgs//PKGBUILD.lip or inside // an arbitrary repo a build was pointed at, and this module: // 1. validates the form and reads it out as a struct value, // 2. lip_generate turns it into a PKGBUILD (plus a .install when // hooks are written) whose phase functions call the runner binary back // with --lip-run, so the lambdas never become shell, // 3. lip_run_phase is the runner: it loads the lip and runs one phase in // a machine where (run) (glob) (pkgdir) (srcdir) (startdir) (this-pkg) // and (git-desc) exist. // makepkg keeps everything else: sources, checksums, PGP, fakeroot, lint, // packaging. unknown options and checksum counts come out of makepkg's own // mouth, since the generated file is a real PKGBUILD. module vmakepkg import os import blip.vlib.blip import blip.vlib.blip.machine import blip.vlib.blip.host import blip.vlib.blip.printer // ----------------------------------------------------------------- lip pkgs // // PKGBUILD.lip files under pkgs/ describe a package in blip: // // (defpkgbuild foobar // ;; — identity — // :pkgname "foobar" ; or '("foo" "bar") for split // :pkgver "2.5" ; or (lambda () ...) for VCS // :pkgrel 2 ; bump on every change // ;; :epoch 1 ; only to break version ties // :pkgdesc "Text editor for X11" // :arch '(:x86-64) ; or '(:any) // :url "https://example.com/foobar/" // :license "Apache-2.0 WITH LLVM-exception" ; one SPDX expression // ;; — dependencies — // :depends '("glibc" "foobar>=1.8.0") // :makedepends '("git") // :checkdepends '("cmocka") // :optdepends '("cups: printing support") // ;; — relations — // :provides '("foobar.so") ; the pkgname itself is implied // :conflicts '("foobar-git") // ;; — packaging — // :backup '("etc/foobar.conf") ; relative, no leading slash // :options '(:no-strip :docs) ; :no-x becomes !x for makepkg // ;; — sources & integrity — // :source '("foobar-2.5.tar.gz::https://example.com/2.5.tar.gz") // :validpgpkeys '("3B2C6CE3A0FE1EC1D41E39A5F5A0A1B3C9D8E7F6") // :b2sums '("...") // ;; — build phases, as code — // :prepare // (lambda () // (run "sed -i 's|/opt|/usr|' src/config.h")) // :build // (lambda () // (run "cmake -B build -DCMAKE_INSTALL_PREFIX=/usr") // (run "cmake --build build")) // :check // (lambda () // (run "ctest --test-dir build")) // :package // (lambda () // (run "cmake --install build DESTDIR=$pkgdir") // ;; blip logic instead of fragile shell; for-each comes with the runner: // (for-each (lambda (doc) // (run "install -Dm644 ~A ~A/doc/foobar/~A" doc (pkgdir) doc)) // (glob "docs/*.html")) // (run "install -Dm644 LICENSE ~A/usr/share/licenses/foobar/LICENSE" (pkgdir))) // ;; — .install hooks, inline; no separate .install file — // :post-install // (lambda (newver) // (echo "Optional: run foobar-setup to configure autosave")) // :post-upgrade // (lambda (newver oldver) // (echo (format "from ~a to ~a" oldver newver)) // (run "systemctl try-restart foobar.service"))) // // the build translates that into a real PKGBUILD (and a .install when // hooks are written) in a fresh work dir, where makepkg keeps everything as // usual. const lip_list_keys = [ 'pkgname', 'arch', 'license', 'depends', 'makedepends', 'checkdepends', 'optdepends', 'provides', 'conflicts', 'backup', 'options', 'source', 'validpgpkeys', 'b2sums', 'sha256sums', ] const lip_phases = ['prepare', 'build', 'check', 'package'] const lip_hooks = ['pre-install', 'post-install', 'pre-upgrade', 'post-upgrade', 'pre-remove', 'post-remove'] // lip_hook_arity is the versions pacman hands a .install scriptlet: the new // version for installs and removes, new and then old for upgrades fn lip_hook_arity(hook string) int { if hook in ['pre-upgrade', 'post-upgrade'] { return 2 } return 1 } // lip_fn_name spells a hook as its .install function name: dashes are valid // in a keyword, underscores are the .install convention fn lip_fn_name(hook string) string { return hook.replace('-', '_') } // shell_quote wraps a value for the shell as one word, whatever it holds: // single quotes around it, each quote inside closed, escaped and reopened. // the one quoting vpac and the generated PKGBUILDs both use pub fn shell_quote(s string) string { return "'" + s.replace("'", "'\\''") + "'" } // lip_tag_name is for error text: 'a keyword' reads better than a dump of the // tag enum fn lip_tag_name(t blip.Tag) string { return match t { .nil { 'nil' } .boolean { 'a boolean' } .integer { 'an integer' } .float { 'a float' } .rune { 'a character' } .string { 'a string' } .symbol { 'a symbol' } .keyword { 'a keyword' } .pair { 'a list' } .emptylist { 'an empty list' } .vector { 'a vector' } .array { 'an array' } .table { 'a table' } .buffer { 'a buffer' } .closure { 'a lambda' } .primitive { 'a primitive' } else { 'a struct' } } } // lip_items_of reads one plain item ("glibc", :any) or a list of them out of // a value: a quoted list '("a" "b"), or a vector ["a" "b"]. lone keywords are // allowed as items, matching the arch and options spellings, and the same // reader takes a single item so a license does not have to hide in a list fn lip_items_of(v blip.Value, what string) ![]string { mut out := []string{} match v.tag { .string, .keyword, .symbol { out << v.as_string() } .emptylist {} .vector, .array { for item in v.as_vector().data { match item.tag { .string, .keyword, .symbol { out << item.as_string() } else { return error('pkgbuild :${what}: items must be strings or keywords, not ${lip_tag_name(item.tag)}') } } } } .pair { mut cur := v for cur.tag == .pair { p := cur.as_pair() match p.car.tag { .string, .keyword, .symbol { out << p.car.as_string() } else { return error('pkgbuild :${what}: items must be strings or keywords, not ${lip_tag_name(p.car.tag)}') } } cur = p.cdr } // a proper list ends in (): a dotted tail is an item the walk // above would drop without a word if cur.tag != .emptylist { return error('pkgbuild :${what}: a list ends in (), not in a dotted ${lip_tag_name(cur.tag)}') } } else { return error('pkgbuild :${what} must be a string or a quoted list') } } return out } // lip_arch spells arches the way makepkg reads them: :x86-64 is x86_64 in the // generated head, :any and everything else pass through fn lip_arch(a string) string { if a == 'x86-64' { return 'x86_64' } return a } // lip_option spells one of :options' entries the way makepkg's options array // wants it: makepkg spells disabled options with a ! in front, and the lip's // keyword for that is no-, so :no-strip becomes !strip; an enabled option and // anything already in makepkg's own spelling pass through fn lip_option(keyword string) string { if keyword.starts_with('no-') { return '!' + keyword[3..] } return keyword } // lip_hex checks a checksum body: full hex for its own length, or SKIP, so a // bad sum is named in the lip, not somewhere inside makepkg's diagnosis fn lip_hex(s string, len int, what string) ! { if s == 'SKIP' { return } if s.len != len { return error('${what}: ${s} is ${s.len} characters; ${what} entries are ${len} hex characters or SKIP') } for c in s { if !c.is_hex_digit() { return error('${what}: ${s} has a character that is not hex (${c.ascii_str()})') } } } // lip_key40 checks a PGP fingerprint: the full 40 uppercase hex characters fn lip_key40(s string, i int) ! { if s.len != 40 { return error('validpgpkeys: entry ${i} is ${s.len} characters; fingerprints are 40: the full uppercase one') } for c in s { if !c.is_hex_digit() { return error('validpgpkeys: entry ${i} has a character that is not hex (${c.ascii_str()})') } if c >= `a` && c <= `f` { return error('validpgpkeys: entry ${i} is lowercase hex (${c.ascii_str()}); uppercase fingerprints only') } } } // prim_defpkgbuild validates one (defpkgbuild ...) form and hands back a // struct value: the scalars checked, the list keys normalised, and the phase // and hook lambdas kept as the closures they evaluated to. the generator and // the runner both read out of that one value. one (defpkgbuild ...) per file // is checked by lip_defpkg_count when the file loads fn prim_defpkgbuild(args []blip.Value) !blip.Value { if args.len == 0 { return error('pkgbuild: (defpkgbuild name ...) needs the name') } // the name can be the bare symbol (the format's spelling, pre-bound to // its own string by the loader), a quoted symbol, or a string if args[0].tag !in [.string, .symbol] { return error('pkgbuild: the name is a symbol or a string, not ${lip_tag_name(args[0].tag)}') } pkgbase_name := args[0].as_string() if (args.len - 1) % 2 != 0 { return error('pkgbuild: :key and value come in pairs') } mut fields := []string{} mut vals := map[string]blip.Value{} fields << 'name' vals['name'] = blip.string(pkgbase_name) mut i := 1 for i < args.len { kv := args[i] if kv.tag != .keyword { return error('pkgbuild: expected a :key, got ${printer.write(kv)}') } key := kv.as_string() val := args[i + 1] if key in fields { return error('pkgbuild :${key} is set twice') } if key in lip_list_keys { mut items := lip_items_of(val, key)! match key { 'pkgname' { if items.len == 0 { return error('pkgbuild :pkgname must not be empty') } for it in items { if it.contains(' ') { return error('pkgbuild :pkgname: "${it}" has whitespace in it') } } } 'arch' { if items.len == 0 { return error('pkgbuild :arch must not be empty') } for j, it in items { items[j] = lip_arch(it) } } 'validpgpkeys' { for j, it in items { lip_key40(it, j)! } } 'b2sums' { for it in items { lip_hex(it, 128, 'b2sums')! } } 'sha256sums' { for it in items { lip_hex(it, 64, 'sha256sums')! } } 'backup' { for it in items { if it.starts_with('/') { return error('pkgbuild :backup: "${it}" is absolute; backup paths are relative, no leading slash') } } } else {} } mut item_vals := []blip.Value{} for it in items { item_vals << blip.string(it) } fields << key vals[key] = blip.list_from(item_vals) i += 2 continue } if key in lip_phases || key in lip_hooks { if val.tag != .closure { return error('pkgbuild :${key} must be a (lambda ...); makepkg runs it as a function') } p := val.as_closure() arity := if key in lip_hooks { lip_hook_arity(key) } else { 0 } if p.params.len != arity { return error('pkgbuild :${key} takes ${arity} parameter${if arity == 1 { '' } else { 's' }}, not ${p.params.len}') } fields << key vals[key] = val i += 2 continue } match key { 'pkgver' { // a static version is written verbatim; a lambda is the pkgver() // makepkg runs after the sources are unpacked, for VCS packages if val.tag != .string && val.tag != .closure { return error('pkgbuild :pkgver must be a string or a (lambda ...)') } fields << key vals[key] = val } 'pkgrel', 'epoch' { if val.tag != .integer { return error('pkgbuild :${key} must be an integer') } if key == 'epoch' && val.as_int() < 0 { return error('pkgbuild :epoch is negative; 0 means absent') } fields << key vals[key] = val } 'pkgdesc', 'url' { if val.tag != .string { return error('pkgbuild :${key} must be a string') } fields << key vals[key] = val } else { // the keys makepkg knows, in the lip's spelling; anything else // is an error here, where the lip names it return error('pkgbuild has no key :${key}') } } i += 2 } // the fields makepkg refuses to do without. build is not one: makepkg // runs build() only when the PKGBUILD has one (a data package, the split // smoke package, has none); package is, the one phase every package runs for req in ['pkgname', 'pkgver', 'pkgrel', 'pkgdesc', 'arch', 'package'] { if req !in fields { return error('pkgbuild: :${req} is required') } } return blip.struct_value('pkgbuild', fields, vals) } // prim_run replaces every ~a in the format with the next argument — // shell-quoted, so a path with spaces survives — and hands the line to the // runner's own run helper fn prim_run(args []blip.Value) !blip.Value { run(lip_fmt(args)!)! return blip.nil_value() } // prim_glob spills the pattern into a blip vector, sorted, empty when nothing // matches: the loop in the format example iterates it directly fn prim_glob(args []blip.Value) !blip.Value { if args.len != 1 || args[0].tag != .string { return error('(glob pattern) takes one string pattern') } matches := glob(args[0].as_string())! mut out := []blip.Value{} for m in matches { out << blip.string(m) } return blip.vector(out) } // prim_pkgdir is (pkgdir): the directory makepkg is filling, which the // generated function exports for the phase fn prim_pkgdir(_ []blip.Value) !blip.Value { p := os.getenv('VPAC_PKGDIR') if p == '' { return error('(pkgdir): only a value inside a pkgbuild phase; let the built PKGBUILD call it') } return blip.string(p) } fn prim_srcdir(_ []blip.Value) !blip.Value { p := os.getenv('VPAC_SRCDIR') if p == '' { return error('(srcdir): only a value inside a pkgbuild phase; let the built PKGBUILD call it') } return blip.string(p) } fn prim_startdir(_ []blip.Value) !blip.Value { p := os.getenv('VPAC_STARTDIR') if p == '' { return error('(startdir): only a value inside a pkgbuild phase; let the built PKGBUILD call it') } return blip.string(p) } // prim_this_pkg is (this-pkg): the name a split lambda runs under — makepkg // rebinds pkgname for every split while packaging fn prim_this_pkg(_ []blip.Value) !blip.Value { p := os.getenv('VPAC_PKGNAME') if p == '' { return error('(this-pkg): only a value inside a pkgbuild phase; let the built PKGBUILD call it') } return blip.string(p) } // prim_git_desc is (git-desc pattern fallback?): 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 fn prim_git_desc(args []blip.Value) !blip.Value { if args.len == 0 || args.len > 2 || args[0].tag != .string { return error('(git-desc pattern fallback?) takes a string pattern and an optional string fallback') } pattern := args[0].as_string() mut fallback := '' if args.len == 2 { if args[1].tag != .string { return error('(git-desc ...): the fallback must be a string') } fallback = args[1].as_string() } return blip.string(git_desc(pattern, fallback)!) } // lip_fmt renders the format for run: every ~a takes the next argument, // shell-quoted, which is what the command needs to survive a path with // spaces. echo needs none of it: blip's own echo prints plainly, and a hook // that wants interpolation echoes (format "..." ~a) fn lip_fmt(args []blip.Value) !string { if args.len == 0 || args[0].tag != .string { return error('run: the first argument is the format string') } // both spellings of the placeholder are welcome; blip's own format uses // the lowercase one fmt_text := args[0].as_string().replace('~A', '~a') parts := fmt_text.split('~a') if parts.len - 1 != args.len - 1 { return error('run: the format has ${parts.len - 1} ~a for ${args.len - 1} argument${if args.len - 1 == 1 { '' } else { 's' }}') } mut out := parts[0] mut j := 1 for j < args.len { arg := args[j] mut rendered := '' match arg.tag { .string { rendered = arg.as_string() } .integer { rendered = arg.as_int().str() } else { return error('run: argument ${j} is ${lip_tag_name(arg.tag)}; for a path call (pkgdir), (srcdir) or (startdir) and pass it as a ~a in the format') } } rendered = shell_quote(rendered) out += rendered out += parts[j] j++ } return out } // lip_new_machine builds a machine with the pkgbuild prims registered // (defpkgbuild among them). the bare package name is spelled the file's own // way: the loader pre-binds it before the file loads, prim arg or not fn lip_new_machine() !&machine.Machine { mut m := machine.new_standalone(&host.ConsoleHost{}) m.prims['defpkgbuild'] = prim_defpkgbuild m.prims['run'] = prim_run m.prims['glob'] = prim_glob m.prims['pkgdir'] = prim_pkgdir m.prims['srcdir'] = prim_srcdir m.prims['startdir'] = prim_startdir m.prims['this-pkg'] = prim_this_pkg m.prims['git-desc'] = prim_git_desc return m } // lip_delim is true for a byte that ends a symbol: space, a bracket, or a // comment's or string's opening fn lip_delim(c u8) bool { return c in [` `, `\t`, `\n`, `\r`, `(`, `)`, `[`, `]`, `{`, `}`, `;`, `"`] } // lip_defpkg_forms finds the (defpkgbuild ...) forms in a lip's real text, // blip's own spelling of comments and strings applied: `;` ends a line's // text, `#|` opens a block comment nested to its `|#`, and a double-quoted // string hides its contents, so `(defpkgbuild` named in one does not count. // in code the form only opens at a word boundary, so a symbol with a longer // name or one quoted into a string is not a second build of the package. // each form is answered as the offset just past its `(defpkgbuild` fn lip_defpkg_forms(src string) []int { mut forms := []int{} mut i := 0 for i < src.len { c := src[i] if c == `;` { for i < src.len && src[i] != `\n` { i++ } continue } if c == `#` && i + 1 < src.len && src[i + 1] == `|` { mut level := 1 i += 2 for i < src.len && level > 0 { if src[i] == `#` && i + 1 < src.len && src[i + 1] == `|` { level++ i += 2 } else if src[i] == `|` && i + 1 < src.len && src[i + 1] == `#` { level-- i += 2 } else { i++ } } continue } if c == `"` { i++ for i < src.len { if src[i] == `\\` && i + 1 < src.len { i += 2 continue } if src[i] == `"` { i++ break } i++ } continue } if c == `(` && src[i + 1..].starts_with('defpkgbuild') { after := i + 1 + 'defpkgbuild'.len // the symbol ends at space, bracket or end: a same-named text // inside a bigger symbol name does not open a form if after >= src.len || lip_delim(src[after]) { forms << after } i = after continue } i++ } return forms } // lip_defpkg_name reads the (defpkgbuild name ...) symbol out of the file // text. the machine evaluates a bare symbol, so the runner and the generator // pre-bind the name under it before the file loads: the file keeps spelling // the package bare, the format's own way. the form is found the way // lip_defpkg_count finds it, so one named in a comment or a string binds // nothing, and the name runs to the symbol's end, whatever a package name // may hold (a dot, a plus, an at) fn lip_defpkg_name(lip string) string { src := os.read_file(lip) or { return '' } forms := lip_defpkg_forms(src) if forms.len == 0 { return '' } mut j := forms[0] for j < src.len && src[j] in [` `, `\n`, `\r`, `\t`] { j++ } // a quoted or string spelling needs no trick if j >= src.len || src[j] == `'` || src[j] == `"` { return '' } mut end := j for end < src.len && !lip_delim(src[end]) { end++ } return src[j..end] } // lip_defpkg_count counts the (defpkgbuild ...) forms in a lip's real text fn lip_defpkg_count(path string) !int { return lip_defpkg_forms(os.read_file(path)!).len } // lip_load_in loads one lip into a machine that already has the prims, with // the pre-bound name, and checks the last form is the defpkgbuild struct fn lip_load_in(mut m machine.Machine, path string) !blip.Value { // a lip that cannot be read fails here, with the read's own reason: the // one-form check is never skipped on the way to a load that reads it count := lip_defpkg_count(path) or { return error('${path}: ${err.msg()}') } if count > 1 { return error('${path}: one (defpkgbuild ...) form per file, not ${count}') } name := lip_defpkg_name(path) if name != '' { m.envs.define(m.globals, name, blip.string(name)) } v := m.load(path)! if v.tag != .struct_ { return error('${path}: the last form must be a (defpkgbuild ...)') } return v } // lip_load evaluates a PKGBUILD.lip and returns the struct the defpkgbuild // prim ran it into: the last form's value pub fn lip_load(path string) !blip.Value { mut m := lip_new_machine()! return lip_load_in(mut m, path)! } // lip_make is the one-liner callers want: load the lip, hand back everything // the generated package needs (the PKGBUILD text, the .install when hooks // exist, and whether pkgver floats) pub fn lip_make(lip string, runner string) !LipBet { meta := lip_load(lip)! return lip_generate(meta, runner, lip, .lip)! } // lip_run_phase is --lip-run [args...]: it loads the lip, wires // the phase's closure under a fresh name, and calls it with the remaining // arguments as the parameters. pkgver's result is the version, printed for // makepkg's call to read; every other phase's result falls on the floor. a // failure is nonzero, which fails the makepkg phase like a plain script would pub fn lip_run_phase(lip string, phase string, params []string) ! { mut m := lip_new_machine()! // load on this machine, not lip_load's: the bootstrap macro has to be // in the machine that runs the file, with its phases meta := lip_load_in(mut m, lip)! s := meta.as_struct() if !s.has(phase) { return error('${lip}: the pkgbuild has no :${phase}') } cb := s.get(phase) if cb.tag != .closure { return error('${lip}: :${phase} is not a (lambda ...)') } name := m.fresh('PKG-RUN') m.envs.define(m.globals, name, cb) mut arg_nodes := []blip.NodeId{} for arg in params { arg_nodes << m.string_node(arg) } call := m.call_node(m.sym_node(name), arg_nodes) res := m.run([call])! if phase == 'pkgver' { if res.tag != .string { return error('pkgver: the lambda ran into ${lip_tag_name(res.tag)}, not a version string') } println(res.as_string()) } } // lip_array_line writes a makepkg array: one quoted entry per line, in the // order the lip declared them fn lip_array_line(name string, items []string) string { mut out := '${name}=(' for it in items { out += '\n ${shell_quote(it)}' } out += '\n)' return out } // LipKind says which file the phases live in: a lip runs through this // binary's runner, a PKGBUILD.vsh runs through v itself pub enum LipKind { lip vsh } // delegate_line is the one bash line a phase wrapper hands the work to, per // kind, with makepkg's own state exported for the runner fn delegate_line(kind LipKind, phase string, extra string) string { call := if kind == .lip { '"\$VPAC_BLIP_RUN" --lip-run "\$VPAC_PKG_LIP" ${phase}' } else { '"\$VPAC_VSH_RUN" run "\$VPAC_PKG_VSH" ${phase}' } return ' export VPAC_PKGDIR="\$pkgdir" VPAC_SRCDIR="\$srcdir" VPAC_STARTDIR="\$startdir" VPAC_PKGNAME="\$pkgname" VPAC_PKGVER="\$pkgver" ${call}${extra}' } // lip_phase_fn writes one makepkg function that hands the phase to the // runner. makepkg's own pkgdir, srcdir and startdir are readonly in its // shell, so the wrapper exports fresh VPAC_ spellings instead: the runner's // host primitives read those, and (run) re-exposes makepkg's spellings to // every command it passes to sh, so the raw $pkgdir of a phase body keeps // meaning exactly what makepkg says fn lip_phase_fn(fn_name string, phase string, kind LipKind) string { return '${fn_name}() { ${delegate_line(kind, phase, '')} }' } // lip_pkgver_fn writes pkgver() for VCS packages: makepkg captures its stdout // as the new version, and runs it from $srcdir like the other phases fn lip_pkgver_fn(kind LipKind) string { return 'pkgver() { ${delegate_line(kind, 'pkgver', '')} }' } // lip_hook_fn writes one .install function: pacman passes the versions, the // runner takes them as the lambda's parameters. libalpm sources the .install // at install time in a shell of its own, where nothing of the PKGBUILD's — // its VPAC_ variables, makepkg's pkgdir and srcdir — exists, so the runner // and the script are written into the function as they were at the build, // and the package's name is the one thing of makepkg's state handed on. a // runner or a script that is gone by then is said and fails the scriptlet, // where an empty command would have been a silent 127 fn lip_hook_fn(hook string, kind LipKind, runner string, script_path string, pkgname string) string { argv := lip_hook_arity(hook) mut args := '' if argv > 0 { args = ' "\$1"' } if argv > 1 { args += ' "\$2"' } r := shell_quote(runner) s := shell_quote(script_path) call := if kind == .lip { '${r} --lip-run ${s} ${hook}' } else { '${r} run ${s} ${hook}' } gone := shell_quote('${pkgname}: the ${hook} hook needs ${runner} and ${script_path}, and one of them is gone') return '${lip_fn_name(hook)}() { if [ ! -x ${r} ] || [ ! -r ${s} ]; then echo ${gone} >&2 return 1 fi VPAC_PKGNAME=${shell_quote(pkgname)} ${call}${args} }' } // LipBet is one generated package: the PKGBUILD, the .install when hooks are // there, and the base the files follow pub struct LipBet { pub mut: text string install string dynamic bool base string } // lip_generate turns the defpkgbuild struct into the PKGBUILD text (and into // a .install when hooks are written), pointing at this binary and at the // original lip, wherever they were when the package was built fn lip_generate(meta blip.Value, runner string, script_path string, kind LipKind) !LipBet { s := meta.as_struct() mut names := []string{} if s.has('pkgname') { names = lip_items_of(s.get('pkgname'), 'pkgname')! } pkgbase := names[0] mut dynamic := false pkgver := if s.has('pkgver') { v := s.get('pkgver') if v.tag == .string { v.as_string() } else { dynamic = true '0' } } else { // the entry holds the pkgver function for vsh scripts, so the head // gets the same 0 placeholder and pkgver() wrapper a lambda would dynamic = true '0' } rel := s.get('pkgrel').as_int() mut out := []string{} head_label := if kind == .lip { 'from the lip; the lip is the source' } else { 'from PKGBUILD.vsh; the script is the source' } out << '# Generated by the vpac build ${head_label}' if kind == .lip { out << 'VPAC_BLIP_RUN=${shell_quote(runner)}' out << 'VPAC_PKG_LIP=${shell_quote(script_path)}' } else { out << 'VPAC_VSH_RUN=${shell_quote(runner)}' out << 'VPAC_PKG_VSH=${shell_quote(script_path)}' } out << '' if names.len > 1 { out << 'pkgbase=${shell_quote(pkgbase)}' } if names.len == 1 { out << 'pkgname=${shell_quote(pkgbase)}' } else { out << lip_array_line('pkgname', names) } out << 'pkgver=${shell_quote(pkgver)}' out << 'pkgrel=${rel}' if s.has('epoch') && s.get('epoch').as_int() > 0 { out << 'epoch=${s.get('epoch').as_int()}' } pkgdesc := s.get('pkgdesc').as_string() out << 'pkgdesc=${shell_quote(pkgdesc)}' arches := lip_items_of(s.get('arch'), 'arch')! out << lip_array_line('arch', arches) for opt in ['url', 'license'] { if s.has(opt) { items := lip_items_of(s.get(opt), opt)! if opt == 'license' || items.len > 1 { // makepkg says license must be an array: one entry or many out << lip_array_line(opt, items) } else { out << '${opt}=${shell_quote(items[0])}' } } } for k in ['depends', 'makedepends', 'checkdepends', 'optdepends', 'provides', 'conflicts', 'backup', 'options', 'source', 'validpgpkeys'] { if s.has(k) { items := lip_items_of(s.get(k), k)! // the lip spells a disabled option :no-x, makepkg spells !x; a // PKGBUILD.vsh's spec holds makepkg's own spelling already worded := if kind == .lip && k == 'options' { items.map(lip_option) } else { items } out << lip_array_line(k, worded) } } for k in ['b2sums', 'sha256sums'] { if s.has(k) { out << lip_array_line(k, lip_items_of(s.get(k), k)!) } } mut has_hooks := false for h in lip_hooks { if s.has(h) { has_hooks = true } } mut install_name := '' if has_hooks { install_name = '${pkgbase}.install' out << 'install=${shell_quote(install_name)}' } out << '' out << "# the phases run as the runner's own code above, nothing here is shell logic" if dynamic { out << lip_pkgver_fn(kind) } for ph in lip_phases { if ph == 'package' { continue } if s.has(ph) { out << lip_phase_fn(ph, ph, kind) } } // a shared :package lambda runs once per split: makepkg rebinds pkgname // on the way through, so (this-pkg) says which split is being packed for name in names { fn_name := if names.len == 1 { 'package' } else { 'package_${name}' } out << lip_phase_fn(fn_name, 'package', kind) } // the hooks are inline in the lip, and live in the package the world // sees, as pacman's own .install contract expects mut install := '' if has_hooks { mut lines := []string{} lines << '#!/bin/sh' lines << '# Generated by the vpac build: the hooks are the source, run through the runner' for h in lip_hooks { if s.has(h) { lines << lip_hook_fn(h, kind, runner, script_path, pkgbase) } } install = lines.join('\n') + '\n' } return LipBet{ text: out.join('\n') + '\n' install: install dynamic: dynamic base: pkgbase } }