| 1 | // vmakepkg holds the build side of blip PKGBUILD.lip files and V-script |
| 2 | // PKGBUILD.vsh files. vpac is its one user: the build mode (vpac b) and the |
| 3 | // --lip-run entry the generated PKGBUILDs call back through; the rest of |
| 4 | // the repo reaches it only by running vpac: |
| 5 | // |
| 6 | // (defpkgbuild foobar ...) lives under pkgs/<name>/PKGBUILD.lip or inside |
| 7 | // an arbitrary repo a build was pointed at, and this module: |
| 8 | // 1. validates the form and reads it out as a struct value, |
| 9 | // 2. lip_generate turns it into a PKGBUILD (plus a <base>.install when |
| 10 | // hooks are written) whose phase functions call the runner binary back |
| 11 | // with --lip-run, so the lambdas never become shell, |
| 12 | // 3. lip_run_phase is the runner: it loads the lip and runs one phase in |
| 13 | // a machine where (run) (glob) (pkgdir) (srcdir) (startdir) (this-pkg) |
| 14 | // and (git-desc) exist. |
| 15 | // makepkg keeps everything else: sources, checksums, PGP, fakeroot, lint, |
| 16 | // packaging. unknown options and checksum counts come out of makepkg's own |
| 17 | // mouth, since the generated file is a real PKGBUILD. |
| 18 | module vmakepkg |
| 19 | |
| 20 | import os |
| 21 | import blip.vlib.blip |
| 22 | import blip.vlib.blip.machine |
| 23 | import blip.vlib.blip.host |
| 24 | import blip.vlib.blip.printer |
| 25 | |
| 26 | // ----------------------------------------------------------------- lip pkgs |
| 27 | // |
| 28 | // PKGBUILD.lip files under pkgs/ describe a package in blip: |
| 29 | // |
| 30 | // (defpkgbuild foobar |
| 31 | // ;; — identity — |
| 32 | // :pkgname "foobar" ; or '("foo" "bar") for split |
| 33 | // :pkgver "2.5" ; or (lambda () ...) for VCS |
| 34 | // :pkgrel 2 ; bump on every change |
| 35 | // ;; :epoch 1 ; only to break version ties |
| 36 | // :pkgdesc "Text editor for X11" |
| 37 | // :arch '(:x86-64) ; or '(:any) |
| 38 | // :url "https://example.com/foobar/" |
| 39 | // :license "Apache-2.0 WITH LLVM-exception" ; one SPDX expression |
| 40 | // ;; — dependencies — |
| 41 | // :depends '("glibc" "foobar>=1.8.0") |
| 42 | // :makedepends '("git") |
| 43 | // :checkdepends '("cmocka") |
| 44 | // :optdepends '("cups: printing support") |
| 45 | // ;; — relations — |
| 46 | // :provides '("foobar.so") ; the pkgname itself is implied |
| 47 | // :conflicts '("foobar-git") |
| 48 | // ;; — packaging — |
| 49 | // :backup '("etc/foobar.conf") ; relative, no leading slash |
| 50 | // :options '(:no-strip :docs) ; :no-x becomes !x for makepkg |
| 51 | // ;; — sources & integrity — |
| 52 | // :source '("foobar-2.5.tar.gz::https://example.com/2.5.tar.gz") |
| 53 | // :validpgpkeys '("3B2C6CE3A0FE1EC1D41E39A5F5A0A1B3C9D8E7F6") |
| 54 | // :b2sums '("...") |
| 55 | // ;; — build phases, as code — |
| 56 | // :prepare |
| 57 | // (lambda () |
| 58 | // (run "sed -i 's|/opt|/usr|' src/config.h")) |
| 59 | // :build |
| 60 | // (lambda () |
| 61 | // (run "cmake -B build -DCMAKE_INSTALL_PREFIX=/usr") |
| 62 | // (run "cmake --build build")) |
| 63 | // :check |
| 64 | // (lambda () |
| 65 | // (run "ctest --test-dir build")) |
| 66 | // :package |
| 67 | // (lambda () |
| 68 | // (run "cmake --install build DESTDIR=$pkgdir") |
| 69 | // ;; blip logic instead of fragile shell; for-each comes with the runner: |
| 70 | // (for-each (lambda (doc) |
| 71 | // (run "install -Dm644 ~A ~A/doc/foobar/~A" doc (pkgdir) doc)) |
| 72 | // (glob "docs/*.html")) |
| 73 | // (run "install -Dm644 LICENSE ~A/usr/share/licenses/foobar/LICENSE" (pkgdir))) |
| 74 | // ;; — .install hooks, inline; no separate .install file — |
| 75 | // :post-install |
| 76 | // (lambda (newver) |
| 77 | // (echo "Optional: run foobar-setup to configure autosave")) |
| 78 | // :post-upgrade |
| 79 | // (lambda (newver oldver) |
| 80 | // (echo (format "from ~a to ~a" oldver newver)) |
| 81 | // (run "systemctl try-restart foobar.service"))) |
| 82 | // |
| 83 | // the build translates that into a real PKGBUILD (and a <base>.install when |
| 84 | // hooks are written) in a fresh work dir, where makepkg keeps everything as |
| 85 | // usual. |
| 86 | const lip_list_keys = [ |
| 87 | 'pkgname', |
| 88 | 'arch', |
| 89 | 'license', |
| 90 | 'depends', |
| 91 | 'makedepends', |
| 92 | 'checkdepends', |
| 93 | 'optdepends', |
| 94 | 'provides', |
| 95 | 'conflicts', |
| 96 | 'backup', |
| 97 | 'options', |
| 98 | 'source', |
| 99 | 'validpgpkeys', |
| 100 | 'b2sums', |
| 101 | 'sha256sums', |
| 102 | ] |
| 103 | |
| 104 | const lip_phases = ['prepare', 'build', 'check', 'package'] |
| 105 | |
| 106 | const lip_hooks = ['pre-install', 'post-install', 'pre-upgrade', 'post-upgrade', 'pre-remove', |
| 107 | 'post-remove'] |
| 108 | |
| 109 | // lip_hook_arity is the versions pacman hands a .install scriptlet: the new |
| 110 | // version for installs and removes, new and then old for upgrades |
| 111 | fn lip_hook_arity(hook string) int { |
| 112 | if hook in ['pre-upgrade', 'post-upgrade'] { |
| 113 | return 2 |
| 114 | } |
| 115 | return 1 |
| 116 | } |
| 117 | |
| 118 | // lip_fn_name spells a hook as its .install function name: dashes are valid |
| 119 | // in a keyword, underscores are the .install convention |
| 120 | fn lip_fn_name(hook string) string { |
| 121 | return hook.replace('-', '_') |
| 122 | } |
| 123 | |
| 124 | // shell_quote wraps a value for the shell as one word, whatever it holds: |
| 125 | // single quotes around it, each quote inside closed, escaped and reopened. |
| 126 | // the one quoting vpac and the generated PKGBUILDs both use |
| 127 | pub fn shell_quote(s string) string { |
| 128 | return "'" + s.replace("'", "'\\''") + "'" |
| 129 | } |
| 130 | |
| 131 | // lip_tag_name is for error text: 'a keyword' reads better than a dump of the |
| 132 | // tag enum |
| 133 | fn lip_tag_name(t blip.Tag) string { |
| 134 | return match t { |
| 135 | .nil { 'nil' } |
| 136 | .boolean { 'a boolean' } |
| 137 | .integer { 'an integer' } |
| 138 | .float { 'a float' } |
| 139 | .rune { 'a character' } |
| 140 | .string { 'a string' } |
| 141 | .symbol { 'a symbol' } |
| 142 | .keyword { 'a keyword' } |
| 143 | .pair { 'a list' } |
| 144 | .emptylist { 'an empty list' } |
| 145 | .vector { 'a vector' } |
| 146 | .array { 'an array' } |
| 147 | .table { 'a table' } |
| 148 | .buffer { 'a buffer' } |
| 149 | .closure { 'a lambda' } |
| 150 | .primitive { 'a primitive' } |
| 151 | else { 'a struct' } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // lip_items_of reads one plain item ("glibc", :any) or a list of them out of |
| 156 | // a value: a quoted list '("a" "b"), or a vector ["a" "b"]. lone keywords are |
| 157 | // allowed as items, matching the arch and options spellings, and the same |
| 158 | // reader takes a single item so a license does not have to hide in a list |
| 159 | fn lip_items_of(v blip.Value, what string) ![]string { |
| 160 | mut out := []string{} |
| 161 | match v.tag { |
| 162 | .string, .keyword, .symbol { |
| 163 | out << v.as_string() |
| 164 | } |
| 165 | .emptylist {} |
| 166 | .vector, .array { |
| 167 | for item in v.as_vector().data { |
| 168 | match item.tag { |
| 169 | .string, .keyword, .symbol { |
| 170 | out << item.as_string() |
| 171 | } |
| 172 | else { |
| 173 | return error('pkgbuild :${what}: items must be strings or keywords, not ${lip_tag_name(item.tag)}') |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | .pair { |
| 179 | mut cur := v |
| 180 | for cur.tag == .pair { |
| 181 | p := cur.as_pair() |
| 182 | match p.car.tag { |
| 183 | .string, .keyword, .symbol { |
| 184 | out << p.car.as_string() |
| 185 | } |
| 186 | else { |
| 187 | return error('pkgbuild :${what}: items must be strings or keywords, not ${lip_tag_name(p.car.tag)}') |
| 188 | } |
| 189 | } |
| 190 | cur = p.cdr |
| 191 | } |
| 192 | // a proper list ends in (): a dotted tail is an item the walk |
| 193 | // above would drop without a word |
| 194 | if cur.tag != .emptylist { |
| 195 | return error('pkgbuild :${what}: a list ends in (), not in a dotted ${lip_tag_name(cur.tag)}') |
| 196 | } |
| 197 | } |
| 198 | else { |
| 199 | return error('pkgbuild :${what} must be a string or a quoted list') |
| 200 | } |
| 201 | } |
| 202 | return out |
| 203 | } |
| 204 | |
| 205 | // lip_arch spells arches the way makepkg reads them: :x86-64 is x86_64 in the |
| 206 | // generated head, :any and everything else pass through |
| 207 | fn lip_arch(a string) string { |
| 208 | if a == 'x86-64' { |
| 209 | return 'x86_64' |
| 210 | } |
| 211 | return a |
| 212 | } |
| 213 | |
| 214 | // lip_option spells one of :options' entries the way makepkg's options array |
| 215 | // wants it: makepkg spells disabled options with a ! in front, and the lip's |
| 216 | // keyword for that is no-, so :no-strip becomes !strip; an enabled option and |
| 217 | // anything already in makepkg's own spelling pass through |
| 218 | fn lip_option(keyword string) string { |
| 219 | if keyword.starts_with('no-') { |
| 220 | return '!' + keyword[3..] |
| 221 | } |
| 222 | return keyword |
| 223 | } |
| 224 | |
| 225 | // lip_hex checks a checksum body: full hex for its own length, or SKIP, so a |
| 226 | // bad sum is named in the lip, not somewhere inside makepkg's diagnosis |
| 227 | fn lip_hex(s string, len int, what string) ! { |
| 228 | if s == 'SKIP' { |
| 229 | return |
| 230 | } |
| 231 | if s.len != len { |
| 232 | return error('${what}: ${s} is ${s.len} characters; ${what} entries are ${len} hex characters or SKIP') |
| 233 | } |
| 234 | for c in s { |
| 235 | if !c.is_hex_digit() { |
| 236 | return error('${what}: ${s} has a character that is not hex (${c.ascii_str()})') |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // lip_key40 checks a PGP fingerprint: the full 40 uppercase hex characters |
| 242 | fn lip_key40(s string, i int) ! { |
| 243 | if s.len != 40 { |
| 244 | return error('validpgpkeys: entry ${i} is ${s.len} characters; fingerprints are 40: the full uppercase one') |
| 245 | } |
| 246 | for c in s { |
| 247 | if !c.is_hex_digit() { |
| 248 | return error('validpgpkeys: entry ${i} has a character that is not hex (${c.ascii_str()})') |
| 249 | } |
| 250 | if c >= `a` && c <= `f` { |
| 251 | return error('validpgpkeys: entry ${i} is lowercase hex (${c.ascii_str()}); uppercase fingerprints only') |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // prim_defpkgbuild validates one (defpkgbuild ...) form and hands back a |
| 257 | // struct value: the scalars checked, the list keys normalised, and the phase |
| 258 | // and hook lambdas kept as the closures they evaluated to. the generator and |
| 259 | // the runner both read out of that one value. one (defpkgbuild ...) per file |
| 260 | // is checked by lip_defpkg_count when the file loads |
| 261 | fn prim_defpkgbuild(args []blip.Value) !blip.Value { |
| 262 | if args.len == 0 { |
| 263 | return error('pkgbuild: (defpkgbuild name ...) needs the name') |
| 264 | } |
| 265 | // the name can be the bare symbol (the format's spelling, pre-bound to |
| 266 | // its own string by the loader), a quoted symbol, or a string |
| 267 | if args[0].tag !in [.string, .symbol] { |
| 268 | return error('pkgbuild: the name is a symbol or a string, not ${lip_tag_name(args[0].tag)}') |
| 269 | } |
| 270 | pkgbase_name := args[0].as_string() |
| 271 | if (args.len - 1) % 2 != 0 { |
| 272 | return error('pkgbuild: :key and value come in pairs') |
| 273 | } |
| 274 | mut fields := []string{} |
| 275 | mut vals := map[string]blip.Value{} |
| 276 | fields << 'name' |
| 277 | vals['name'] = blip.string(pkgbase_name) |
| 278 | mut i := 1 |
| 279 | for i < args.len { |
| 280 | kv := args[i] |
| 281 | if kv.tag != .keyword { |
| 282 | return error('pkgbuild: expected a :key, got ${printer.write(kv)}') |
| 283 | } |
| 284 | key := kv.as_string() |
| 285 | val := args[i + 1] |
| 286 | if key in fields { |
| 287 | return error('pkgbuild :${key} is set twice') |
| 288 | } |
| 289 | if key in lip_list_keys { |
| 290 | mut items := lip_items_of(val, key)! |
| 291 | match key { |
| 292 | 'pkgname' { |
| 293 | if items.len == 0 { |
| 294 | return error('pkgbuild :pkgname must not be empty') |
| 295 | } |
| 296 | for it in items { |
| 297 | if it.contains(' ') { |
| 298 | return error('pkgbuild :pkgname: "${it}" has whitespace in it') |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | 'arch' { |
| 303 | if items.len == 0 { |
| 304 | return error('pkgbuild :arch must not be empty') |
| 305 | } |
| 306 | for j, it in items { |
| 307 | items[j] = lip_arch(it) |
| 308 | } |
| 309 | } |
| 310 | 'validpgpkeys' { |
| 311 | for j, it in items { |
| 312 | lip_key40(it, j)! |
| 313 | } |
| 314 | } |
| 315 | 'b2sums' { |
| 316 | for it in items { |
| 317 | lip_hex(it, 128, 'b2sums')! |
| 318 | } |
| 319 | } |
| 320 | 'sha256sums' { |
| 321 | for it in items { |
| 322 | lip_hex(it, 64, 'sha256sums')! |
| 323 | } |
| 324 | } |
| 325 | 'backup' { |
| 326 | for it in items { |
| 327 | if it.starts_with('/') { |
| 328 | return error('pkgbuild :backup: "${it}" is absolute; backup paths are relative, no leading slash') |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | else {} |
| 333 | } |
| 334 | mut item_vals := []blip.Value{} |
| 335 | for it in items { |
| 336 | item_vals << blip.string(it) |
| 337 | } |
| 338 | fields << key |
| 339 | vals[key] = blip.list_from(item_vals) |
| 340 | i += 2 |
| 341 | continue |
| 342 | } |
| 343 | if key in lip_phases || key in lip_hooks { |
| 344 | if val.tag != .closure { |
| 345 | return error('pkgbuild :${key} must be a (lambda ...); makepkg runs it as a function') |
| 346 | } |
| 347 | p := val.as_closure() |
| 348 | arity := if key in lip_hooks { lip_hook_arity(key) } else { 0 } |
| 349 | if p.params.len != arity { |
| 350 | return error('pkgbuild :${key} takes ${arity} parameter${if arity == 1 { |
| 351 | '' |
| 352 | } else { |
| 353 | 's' |
| 354 | }}, not ${p.params.len}') |
| 355 | } |
| 356 | fields << key |
| 357 | vals[key] = val |
| 358 | i += 2 |
| 359 | continue |
| 360 | } |
| 361 | match key { |
| 362 | 'pkgver' { |
| 363 | // a static version is written verbatim; a lambda is the pkgver() |
| 364 | // makepkg runs after the sources are unpacked, for VCS packages |
| 365 | if val.tag != .string && val.tag != .closure { |
| 366 | return error('pkgbuild :pkgver must be a string or a (lambda ...)') |
| 367 | } |
| 368 | fields << key |
| 369 | vals[key] = val |
| 370 | } |
| 371 | 'pkgrel', 'epoch' { |
| 372 | if val.tag != .integer { |
| 373 | return error('pkgbuild :${key} must be an integer') |
| 374 | } |
| 375 | if key == 'epoch' && val.as_int() < 0 { |
| 376 | return error('pkgbuild :epoch is negative; 0 means absent') |
| 377 | } |
| 378 | fields << key |
| 379 | vals[key] = val |
| 380 | } |
| 381 | 'pkgdesc', 'url' { |
| 382 | if val.tag != .string { |
| 383 | return error('pkgbuild :${key} must be a string') |
| 384 | } |
| 385 | fields << key |
| 386 | vals[key] = val |
| 387 | } |
| 388 | else { |
| 389 | // the keys makepkg knows, in the lip's spelling; anything else |
| 390 | // is an error here, where the lip names it |
| 391 | return error('pkgbuild has no key :${key}') |
| 392 | } |
| 393 | } |
| 394 | i += 2 |
| 395 | } |
| 396 | // the fields makepkg refuses to do without. build is not one: makepkg |
| 397 | // runs build() only when the PKGBUILD has one (a data package, the split |
| 398 | // smoke package, has none); package is, the one phase every package runs |
| 399 | for req in ['pkgname', 'pkgver', 'pkgrel', 'pkgdesc', 'arch', 'package'] { |
| 400 | if req !in fields { |
| 401 | return error('pkgbuild: :${req} is required') |
| 402 | } |
| 403 | } |
| 404 | return blip.struct_value('pkgbuild', fields, vals) |
| 405 | } |
| 406 | |
| 407 | // prim_run replaces every ~a in the format with the next argument — |
| 408 | // shell-quoted, so a path with spaces survives — and hands the line to the |
| 409 | // runner's own run helper |
| 410 | fn prim_run(args []blip.Value) !blip.Value { |
| 411 | run(lip_fmt(args)!)! |
| 412 | return blip.nil_value() |
| 413 | } |
| 414 | |
| 415 | // prim_glob spills the pattern into a blip vector, sorted, empty when nothing |
| 416 | // matches: the loop in the format example iterates it directly |
| 417 | fn prim_glob(args []blip.Value) !blip.Value { |
| 418 | if args.len != 1 || args[0].tag != .string { |
| 419 | return error('(glob pattern) takes one string pattern') |
| 420 | } |
| 421 | matches := glob(args[0].as_string())! |
| 422 | mut out := []blip.Value{} |
| 423 | for m in matches { |
| 424 | out << blip.string(m) |
| 425 | } |
| 426 | return blip.vector(out) |
| 427 | } |
| 428 | |
| 429 | // prim_pkgdir is (pkgdir): the directory makepkg is filling, which the |
| 430 | // generated function exports for the phase |
| 431 | fn prim_pkgdir(_ []blip.Value) !blip.Value { |
| 432 | p := os.getenv('VPAC_PKGDIR') |
| 433 | if p == '' { |
| 434 | return error('(pkgdir): only a value inside a pkgbuild phase; let the built PKGBUILD call it') |
| 435 | } |
| 436 | return blip.string(p) |
| 437 | } |
| 438 | |
| 439 | fn prim_srcdir(_ []blip.Value) !blip.Value { |
| 440 | p := os.getenv('VPAC_SRCDIR') |
| 441 | if p == '' { |
| 442 | return error('(srcdir): only a value inside a pkgbuild phase; let the built PKGBUILD call it') |
| 443 | } |
| 444 | return blip.string(p) |
| 445 | } |
| 446 | |
| 447 | fn prim_startdir(_ []blip.Value) !blip.Value { |
| 448 | p := os.getenv('VPAC_STARTDIR') |
| 449 | if p == '' { |
| 450 | return error('(startdir): only a value inside a pkgbuild phase; let the built PKGBUILD call it') |
| 451 | } |
| 452 | return blip.string(p) |
| 453 | } |
| 454 | |
| 455 | // prim_this_pkg is (this-pkg): the name a split lambda runs under — makepkg |
| 456 | // rebinds pkgname for every split while packaging |
| 457 | fn prim_this_pkg(_ []blip.Value) !blip.Value { |
| 458 | p := os.getenv('VPAC_PKGNAME') |
| 459 | if p == '' { |
| 460 | return error('(this-pkg): only a value inside a pkgbuild phase; let the built PKGBUILD call it') |
| 461 | } |
| 462 | return blip.string(p) |
| 463 | } |
| 464 | |
| 465 | // prim_git_desc is (git-desc pattern fallback?): git describe in the cwd the |
| 466 | // phase runs under ($srcdir, where makepkg runs its phases). the fallback |
| 467 | // comes out verbatim when describe finds nothing, so a young tree still has a |
| 468 | // version before its first tag |
| 469 | fn prim_git_desc(args []blip.Value) !blip.Value { |
| 470 | if args.len == 0 || args.len > 2 || args[0].tag != .string { |
| 471 | return error('(git-desc pattern fallback?) takes a string pattern and an optional string fallback') |
| 472 | } |
| 473 | pattern := args[0].as_string() |
| 474 | mut fallback := '' |
| 475 | if args.len == 2 { |
| 476 | if args[1].tag != .string { |
| 477 | return error('(git-desc ...): the fallback must be a string') |
| 478 | } |
| 479 | fallback = args[1].as_string() |
| 480 | } |
| 481 | return blip.string(git_desc(pattern, fallback)!) |
| 482 | } |
| 483 | |
| 484 | // lip_fmt renders the format for run: every ~a takes the next argument, |
| 485 | // shell-quoted, which is what the command needs to survive a path with |
| 486 | // spaces. echo needs none of it: blip's own echo prints plainly, and a hook |
| 487 | // that wants interpolation echoes (format "..." ~a) |
| 488 | fn lip_fmt(args []blip.Value) !string { |
| 489 | if args.len == 0 || args[0].tag != .string { |
| 490 | return error('run: the first argument is the format string') |
| 491 | } |
| 492 | // both spellings of the placeholder are welcome; blip's own format uses |
| 493 | // the lowercase one |
| 494 | fmt_text := args[0].as_string().replace('~A', '~a') |
| 495 | parts := fmt_text.split('~a') |
| 496 | if parts.len - 1 != args.len - 1 { |
| 497 | return error('run: the format has ${parts.len - 1} ~a for ${args.len - 1} argument${if args.len - 1 == 1 { |
| 498 | '' |
| 499 | } else { |
| 500 | 's' |
| 501 | }}') |
| 502 | } |
| 503 | mut out := parts[0] |
| 504 | mut j := 1 |
| 505 | for j < args.len { |
| 506 | arg := args[j] |
| 507 | mut rendered := '' |
| 508 | match arg.tag { |
| 509 | .string { |
| 510 | rendered = arg.as_string() |
| 511 | } |
| 512 | .integer { |
| 513 | rendered = arg.as_int().str() |
| 514 | } |
| 515 | else { |
| 516 | 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') |
| 517 | } |
| 518 | } |
| 519 | rendered = shell_quote(rendered) |
| 520 | out += rendered |
| 521 | out += parts[j] |
| 522 | j++ |
| 523 | } |
| 524 | return out |
| 525 | } |
| 526 | |
| 527 | // lip_new_machine builds a machine with the pkgbuild prims registered |
| 528 | // (defpkgbuild among them). the bare package name is spelled the file's own |
| 529 | // way: the loader pre-binds it before the file loads, prim arg or not |
| 530 | fn lip_new_machine() !&machine.Machine { |
| 531 | mut m := machine.new_standalone(&host.ConsoleHost{}) |
| 532 | m.prims['defpkgbuild'] = prim_defpkgbuild |
| 533 | m.prims['run'] = prim_run |
| 534 | m.prims['glob'] = prim_glob |
| 535 | m.prims['pkgdir'] = prim_pkgdir |
| 536 | m.prims['srcdir'] = prim_srcdir |
| 537 | m.prims['startdir'] = prim_startdir |
| 538 | m.prims['this-pkg'] = prim_this_pkg |
| 539 | m.prims['git-desc'] = prim_git_desc |
| 540 | return m |
| 541 | } |
| 542 | |
| 543 | // lip_delim is true for a byte that ends a symbol: space, a bracket, or a |
| 544 | // comment's or string's opening |
| 545 | fn lip_delim(c u8) bool { |
| 546 | return c in [` `, `\t`, `\n`, `\r`, `(`, `)`, `[`, `]`, `{`, `}`, `;`, `"`] |
| 547 | } |
| 548 | |
| 549 | // lip_defpkg_forms finds the (defpkgbuild ...) forms in a lip's real text, |
| 550 | // blip's own spelling of comments and strings applied: `;` ends a line's |
| 551 | // text, `#|` opens a block comment nested to its `|#`, and a double-quoted |
| 552 | // string hides its contents, so `(defpkgbuild` named in one does not count. |
| 553 | // in code the form only opens at a word boundary, so a symbol with a longer |
| 554 | // name or one quoted into a string is not a second build of the package. |
| 555 | // each form is answered as the offset just past its `(defpkgbuild` |
| 556 | fn lip_defpkg_forms(src string) []int { |
| 557 | mut forms := []int{} |
| 558 | mut i := 0 |
| 559 | for i < src.len { |
| 560 | c := src[i] |
| 561 | if c == `;` { |
| 562 | for i < src.len && src[i] != `\n` { |
| 563 | i++ |
| 564 | } |
| 565 | continue |
| 566 | } |
| 567 | if c == `#` && i + 1 < src.len && src[i + 1] == `|` { |
| 568 | mut level := 1 |
| 569 | i += 2 |
| 570 | for i < src.len && level > 0 { |
| 571 | if src[i] == `#` && i + 1 < src.len && src[i + 1] == `|` { |
| 572 | level++ |
| 573 | i += 2 |
| 574 | } else if src[i] == `|` && i + 1 < src.len && src[i + 1] == `#` { |
| 575 | level-- |
| 576 | i += 2 |
| 577 | } else { |
| 578 | i++ |
| 579 | } |
| 580 | } |
| 581 | continue |
| 582 | } |
| 583 | if c == `"` { |
| 584 | i++ |
| 585 | for i < src.len { |
| 586 | if src[i] == `\\` && i + 1 < src.len { |
| 587 | i += 2 |
| 588 | continue |
| 589 | } |
| 590 | if src[i] == `"` { |
| 591 | i++ |
| 592 | break |
| 593 | } |
| 594 | i++ |
| 595 | } |
| 596 | continue |
| 597 | } |
| 598 | if c == `(` && src[i + 1..].starts_with('defpkgbuild') { |
| 599 | after := i + 1 + 'defpkgbuild'.len |
| 600 | // the symbol ends at space, bracket or end: a same-named text |
| 601 | // inside a bigger symbol name does not open a form |
| 602 | if after >= src.len || lip_delim(src[after]) { |
| 603 | forms << after |
| 604 | } |
| 605 | i = after |
| 606 | continue |
| 607 | } |
| 608 | i++ |
| 609 | } |
| 610 | return forms |
| 611 | } |
| 612 | |
| 613 | // lip_defpkg_name reads the (defpkgbuild name ...) symbol out of the file |
| 614 | // text. the machine evaluates a bare symbol, so the runner and the generator |
| 615 | // pre-bind the name under it before the file loads: the file keeps spelling |
| 616 | // the package bare, the format's own way. the form is found the way |
| 617 | // lip_defpkg_count finds it, so one named in a comment or a string binds |
| 618 | // nothing, and the name runs to the symbol's end, whatever a package name |
| 619 | // may hold (a dot, a plus, an at) |
| 620 | fn lip_defpkg_name(lip string) string { |
| 621 | src := os.read_file(lip) or { return '' } |
| 622 | forms := lip_defpkg_forms(src) |
| 623 | if forms.len == 0 { |
| 624 | return '' |
| 625 | } |
| 626 | mut j := forms[0] |
| 627 | for j < src.len && src[j] in [` `, `\n`, `\r`, `\t`] { |
| 628 | j++ |
| 629 | } |
| 630 | // a quoted or string spelling needs no trick |
| 631 | if j >= src.len || src[j] == `'` || src[j] == `"` { |
| 632 | return '' |
| 633 | } |
| 634 | mut end := j |
| 635 | for end < src.len && !lip_delim(src[end]) { |
| 636 | end++ |
| 637 | } |
| 638 | return src[j..end] |
| 639 | } |
| 640 | |
| 641 | // lip_defpkg_count counts the (defpkgbuild ...) forms in a lip's real text |
| 642 | fn lip_defpkg_count(path string) !int { |
| 643 | return lip_defpkg_forms(os.read_file(path)!).len |
| 644 | } |
| 645 | |
| 646 | // lip_load_in loads one lip into a machine that already has the prims, with |
| 647 | // the pre-bound name, and checks the last form is the defpkgbuild struct |
| 648 | fn lip_load_in(mut m machine.Machine, path string) !blip.Value { |
| 649 | // a lip that cannot be read fails here, with the read's own reason: the |
| 650 | // one-form check is never skipped on the way to a load that reads it |
| 651 | count := lip_defpkg_count(path) or { return error('${path}: ${err.msg()}') } |
| 652 | if count > 1 { |
| 653 | return error('${path}: one (defpkgbuild ...) form per file, not ${count}') |
| 654 | } |
| 655 | name := lip_defpkg_name(path) |
| 656 | if name != '' { |
| 657 | m.envs.define(m.globals, name, blip.string(name)) |
| 658 | } |
| 659 | v := m.load(path)! |
| 660 | if v.tag != .struct_ { |
| 661 | return error('${path}: the last form must be a (defpkgbuild ...)') |
| 662 | } |
| 663 | return v |
| 664 | } |
| 665 | |
| 666 | // lip_load evaluates a PKGBUILD.lip and returns the struct the defpkgbuild |
| 667 | // prim ran it into: the last form's value |
| 668 | pub fn lip_load(path string) !blip.Value { |
| 669 | mut m := lip_new_machine()! |
| 670 | return lip_load_in(mut m, path)! |
| 671 | } |
| 672 | |
| 673 | // lip_make is the one-liner callers want: load the lip, hand back everything |
| 674 | // the generated package needs (the PKGBUILD text, the .install when hooks |
| 675 | // exist, and whether pkgver floats) |
| 676 | pub fn lip_make(lip string, runner string) !LipBet { |
| 677 | meta := lip_load(lip)! |
| 678 | return lip_generate(meta, runner, lip, .lip)! |
| 679 | } |
| 680 | |
| 681 | // lip_run_phase is --lip-run <lip> <phase> [args...]: it loads the lip, wires |
| 682 | // the phase's closure under a fresh name, and calls it with the remaining |
| 683 | // arguments as the parameters. pkgver's result is the version, printed for |
| 684 | // makepkg's call to read; every other phase's result falls on the floor. a |
| 685 | // failure is nonzero, which fails the makepkg phase like a plain script would |
| 686 | pub fn lip_run_phase(lip string, phase string, params []string) ! { |
| 687 | mut m := lip_new_machine()! |
| 688 | // load on this machine, not lip_load's: the bootstrap macro has to be |
| 689 | // in the machine that runs the file, with its phases |
| 690 | meta := lip_load_in(mut m, lip)! |
| 691 | s := meta.as_struct() |
| 692 | if !s.has(phase) { |
| 693 | return error('${lip}: the pkgbuild has no :${phase}') |
| 694 | } |
| 695 | cb := s.get(phase) |
| 696 | if cb.tag != .closure { |
| 697 | return error('${lip}: :${phase} is not a (lambda ...)') |
| 698 | } |
| 699 | name := m.fresh('PKG-RUN') |
| 700 | m.envs.define(m.globals, name, cb) |
| 701 | mut arg_nodes := []blip.NodeId{} |
| 702 | for arg in params { |
| 703 | arg_nodes << m.string_node(arg) |
| 704 | } |
| 705 | call := m.call_node(m.sym_node(name), arg_nodes) |
| 706 | res := m.run([call])! |
| 707 | if phase == 'pkgver' { |
| 708 | if res.tag != .string { |
| 709 | return error('pkgver: the lambda ran into ${lip_tag_name(res.tag)}, not a version string') |
| 710 | } |
| 711 | println(res.as_string()) |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // lip_array_line writes a makepkg array: one quoted entry per line, in the |
| 716 | // order the lip declared them |
| 717 | fn lip_array_line(name string, items []string) string { |
| 718 | mut out := '${name}=(' |
| 719 | for it in items { |
| 720 | out += '\n ${shell_quote(it)}' |
| 721 | } |
| 722 | out += '\n)' |
| 723 | return out |
| 724 | } |
| 725 | |
| 726 | // LipKind says which file the phases live in: a lip runs through this |
| 727 | // binary's runner, a PKGBUILD.vsh runs through v itself |
| 728 | pub enum LipKind { |
| 729 | lip |
| 730 | vsh |
| 731 | } |
| 732 | |
| 733 | // delegate_line is the one bash line a phase wrapper hands the work to, per |
| 734 | // kind, with makepkg's own state exported for the runner |
| 735 | fn delegate_line(kind LipKind, phase string, extra string) string { |
| 736 | call := if kind == .lip { |
| 737 | '"\$VPAC_BLIP_RUN" --lip-run "\$VPAC_PKG_LIP" ${phase}' |
| 738 | } else { |
| 739 | '"\$VPAC_VSH_RUN" run "\$VPAC_PKG_VSH" ${phase}' |
| 740 | } |
| 741 | return ' export VPAC_PKGDIR="\$pkgdir" VPAC_SRCDIR="\$srcdir" VPAC_STARTDIR="\$startdir" VPAC_PKGNAME="\$pkgname" VPAC_PKGVER="\$pkgver" |
| 742 | ${call}${extra}' |
| 743 | } |
| 744 | |
| 745 | // lip_phase_fn writes one makepkg function that hands the phase to the |
| 746 | // runner. makepkg's own pkgdir, srcdir and startdir are readonly in its |
| 747 | // shell, so the wrapper exports fresh VPAC_ spellings instead: the runner's |
| 748 | // host primitives read those, and (run) re-exposes makepkg's spellings to |
| 749 | // every command it passes to sh, so the raw $pkgdir of a phase body keeps |
| 750 | // meaning exactly what makepkg says |
| 751 | fn lip_phase_fn(fn_name string, phase string, kind LipKind) string { |
| 752 | return '${fn_name}() { |
| 753 | ${delegate_line(kind, phase, '')} |
| 754 | }' |
| 755 | } |
| 756 | |
| 757 | // lip_pkgver_fn writes pkgver() for VCS packages: makepkg captures its stdout |
| 758 | // as the new version, and runs it from $srcdir like the other phases |
| 759 | fn lip_pkgver_fn(kind LipKind) string { |
| 760 | return 'pkgver() { |
| 761 | ${delegate_line(kind, 'pkgver', '')} |
| 762 | }' |
| 763 | } |
| 764 | |
| 765 | // lip_hook_fn writes one .install function: pacman passes the versions, the |
| 766 | // runner takes them as the lambda's parameters. libalpm sources the .install |
| 767 | // at install time in a shell of its own, where nothing of the PKGBUILD's — |
| 768 | // its VPAC_ variables, makepkg's pkgdir and srcdir — exists, so the runner |
| 769 | // and the script are written into the function as they were at the build, |
| 770 | // and the package's name is the one thing of makepkg's state handed on. a |
| 771 | // runner or a script that is gone by then is said and fails the scriptlet, |
| 772 | // where an empty command would have been a silent 127 |
| 773 | fn lip_hook_fn(hook string, kind LipKind, runner string, script_path string, pkgname string) string { |
| 774 | argv := lip_hook_arity(hook) |
| 775 | mut args := '' |
| 776 | if argv > 0 { |
| 777 | args = ' "\$1"' |
| 778 | } |
| 779 | if argv > 1 { |
| 780 | args += ' "\$2"' |
| 781 | } |
| 782 | r := shell_quote(runner) |
| 783 | s := shell_quote(script_path) |
| 784 | call := if kind == .lip { '${r} --lip-run ${s} ${hook}' } else { '${r} run ${s} ${hook}' } |
| 785 | gone := shell_quote('${pkgname}: the ${hook} hook needs ${runner} and ${script_path}, and one of them is gone') |
| 786 | return '${lip_fn_name(hook)}() { |
| 787 | if [ ! -x ${r} ] || [ ! -r ${s} ]; then |
| 788 | echo ${gone} >&2 |
| 789 | return 1 |
| 790 | fi |
| 791 | VPAC_PKGNAME=${shell_quote(pkgname)} ${call}${args} |
| 792 | }' |
| 793 | } |
| 794 | |
| 795 | // LipBet is one generated package: the PKGBUILD, the .install when hooks are |
| 796 | // there, and the base the files follow |
| 797 | pub struct LipBet { |
| 798 | pub mut: |
| 799 | text string |
| 800 | install string |
| 801 | dynamic bool |
| 802 | base string |
| 803 | } |
| 804 | |
| 805 | // lip_generate turns the defpkgbuild struct into the PKGBUILD text (and into |
| 806 | // a .install when hooks are written), pointing at this binary and at the |
| 807 | // original lip, wherever they were when the package was built |
| 808 | fn lip_generate(meta blip.Value, runner string, script_path string, kind LipKind) !LipBet { |
| 809 | s := meta.as_struct() |
| 810 | mut names := []string{} |
| 811 | if s.has('pkgname') { |
| 812 | names = lip_items_of(s.get('pkgname'), 'pkgname')! |
| 813 | } |
| 814 | pkgbase := names[0] |
| 815 | mut dynamic := false |
| 816 | pkgver := if s.has('pkgver') { |
| 817 | v := s.get('pkgver') |
| 818 | if v.tag == .string { |
| 819 | v.as_string() |
| 820 | } else { |
| 821 | dynamic = true |
| 822 | '0' |
| 823 | } |
| 824 | } else { |
| 825 | // the entry holds the pkgver function for vsh scripts, so the head |
| 826 | // gets the same 0 placeholder and pkgver() wrapper a lambda would |
| 827 | dynamic = true |
| 828 | '0' |
| 829 | } |
| 830 | rel := s.get('pkgrel').as_int() |
| 831 | mut out := []string{} |
| 832 | head_label := if kind == .lip { |
| 833 | 'from the lip; the lip is the source' |
| 834 | } else { |
| 835 | 'from PKGBUILD.vsh; the script is the source' |
| 836 | } |
| 837 | out << '# Generated by the vpac build ${head_label}' |
| 838 | if kind == .lip { |
| 839 | out << 'VPAC_BLIP_RUN=${shell_quote(runner)}' |
| 840 | out << 'VPAC_PKG_LIP=${shell_quote(script_path)}' |
| 841 | } else { |
| 842 | out << 'VPAC_VSH_RUN=${shell_quote(runner)}' |
| 843 | out << 'VPAC_PKG_VSH=${shell_quote(script_path)}' |
| 844 | } |
| 845 | out << '' |
| 846 | if names.len > 1 { |
| 847 | out << 'pkgbase=${shell_quote(pkgbase)}' |
| 848 | } |
| 849 | if names.len == 1 { |
| 850 | out << 'pkgname=${shell_quote(pkgbase)}' |
| 851 | } else { |
| 852 | out << lip_array_line('pkgname', names) |
| 853 | } |
| 854 | out << 'pkgver=${shell_quote(pkgver)}' |
| 855 | out << 'pkgrel=${rel}' |
| 856 | if s.has('epoch') && s.get('epoch').as_int() > 0 { |
| 857 | out << 'epoch=${s.get('epoch').as_int()}' |
| 858 | } |
| 859 | pkgdesc := s.get('pkgdesc').as_string() |
| 860 | out << 'pkgdesc=${shell_quote(pkgdesc)}' |
| 861 | arches := lip_items_of(s.get('arch'), 'arch')! |
| 862 | out << lip_array_line('arch', arches) |
| 863 | for opt in ['url', 'license'] { |
| 864 | if s.has(opt) { |
| 865 | items := lip_items_of(s.get(opt), opt)! |
| 866 | if opt == 'license' || items.len > 1 { |
| 867 | // makepkg says license must be an array: one entry or many |
| 868 | out << lip_array_line(opt, items) |
| 869 | } else { |
| 870 | out << '${opt}=${shell_quote(items[0])}' |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | for k in ['depends', 'makedepends', 'checkdepends', 'optdepends', 'provides', 'conflicts', |
| 875 | 'backup', 'options', 'source', 'validpgpkeys'] { |
| 876 | if s.has(k) { |
| 877 | items := lip_items_of(s.get(k), k)! |
| 878 | // the lip spells a disabled option :no-x, makepkg spells !x; a |
| 879 | // PKGBUILD.vsh's spec holds makepkg's own spelling already |
| 880 | worded := if kind == .lip && k == 'options' { items.map(lip_option) } else { items } |
| 881 | out << lip_array_line(k, worded) |
| 882 | } |
| 883 | } |
| 884 | for k in ['b2sums', 'sha256sums'] { |
| 885 | if s.has(k) { |
| 886 | out << lip_array_line(k, lip_items_of(s.get(k), k)!) |
| 887 | } |
| 888 | } |
| 889 | mut has_hooks := false |
| 890 | for h in lip_hooks { |
| 891 | if s.has(h) { |
| 892 | has_hooks = true |
| 893 | } |
| 894 | } |
| 895 | mut install_name := '' |
| 896 | if has_hooks { |
| 897 | install_name = '${pkgbase}.install' |
| 898 | out << 'install=${shell_quote(install_name)}' |
| 899 | } |
| 900 | out << '' |
| 901 | out << "# the phases run as the runner's own code above, nothing here is shell logic" |
| 902 | if dynamic { |
| 903 | out << lip_pkgver_fn(kind) |
| 904 | } |
| 905 | for ph in lip_phases { |
| 906 | if ph == 'package' { |
| 907 | continue |
| 908 | } |
| 909 | if s.has(ph) { |
| 910 | out << lip_phase_fn(ph, ph, kind) |
| 911 | } |
| 912 | } |
| 913 | // a shared :package lambda runs once per split: makepkg rebinds pkgname |
| 914 | // on the way through, so (this-pkg) says which split is being packed |
| 915 | for name in names { |
| 916 | fn_name := if names.len == 1 { 'package' } else { 'package_${name}' } |
| 917 | out << lip_phase_fn(fn_name, 'package', kind) |
| 918 | } |
| 919 | // the hooks are inline in the lip, and live in the package the world |
| 920 | // sees, as pacman's own .install contract expects |
| 921 | mut install := '' |
| 922 | if has_hooks { |
| 923 | mut lines := []string{} |
| 924 | lines << '#!/bin/sh' |
| 925 | lines << '# Generated by the vpac build: the hooks are the source, run through the runner' |
| 926 | for h in lip_hooks { |
| 927 | if s.has(h) { |
| 928 | lines << lip_hook_fn(h, kind, runner, script_path, pkgbase) |
| 929 | } |
| 930 | } |
| 931 | install = lines.join('\n') + '\n' |
| 932 | } |
| 933 | return LipBet{ |
| 934 | text: out.join('\n') + '\n' |
| 935 | install: install |
| 936 | dynamic: dynamic |
| 937 | base: pkgbase |
| 938 | } |
| 939 | } |
| 940 | |