| 1 | module vmakepkg |
| 2 | |
| 3 | import os |
| 4 | import vile.reader |
| 5 | import vile.scheme |
| 6 | |
| 7 | // Scheme and Vile recipes share the same metadata and phase bindings. |
| 8 | const recipe_list_keys = [ |
| 9 | 'pkgname', |
| 10 | 'arch', |
| 11 | 'license', |
| 12 | 'depends', |
| 13 | 'makedepends', |
| 14 | 'checkdepends', |
| 15 | 'optdepends', |
| 16 | 'provides', |
| 17 | 'conflicts', |
| 18 | 'backup', |
| 19 | 'options', |
| 20 | 'source', |
| 21 | 'validpgpkeys', |
| 22 | 'hook-files', |
| 23 | 'b2sums', |
| 24 | 'sha256sums', |
| 25 | ] |
| 26 | |
| 27 | const recipe_phases = ['prepare', 'build', 'check', 'package'] |
| 28 | |
| 29 | const recipe_hooks = ['pre-install', 'post-install', 'pre-upgrade', 'post-upgrade', 'pre-remove', |
| 30 | 'post-remove'] |
| 31 | |
| 32 | // upgrade hooks receive the new and old versions, in that order. |
| 33 | fn recipe_hook_arity(hook string) int { |
| 34 | if hook in ['pre-upgrade', 'post-upgrade'] { |
| 35 | return 2 |
| 36 | } |
| 37 | return 1 |
| 38 | } |
| 39 | |
| 40 | // quote one argument for callers that execute a plain PKGBUILD or elevate through su. |
| 41 | pub fn shell_quote(s string) string { |
| 42 | return "'" + s.replace("'", "'\\''") + "'" |
| 43 | } |
| 44 | |
| 45 | // recipe_tag_name is for error text: 'a keyword' reads better than a dump of the |
| 46 | // tag enum |
| 47 | fn recipe_tag_name(t scheme.Kind) string { |
| 48 | return match t { |
| 49 | .nil { 'an empty list' } |
| 50 | .boolean { 'a boolean' } |
| 51 | .integer { 'an integer' } |
| 52 | .real, .rational { 'a number' } |
| 53 | .character { 'a character' } |
| 54 | .string { 'a string' } |
| 55 | .symbol { 'a symbol' } |
| 56 | .keyword { 'a keyword' } |
| 57 | .pair { 'a list' } |
| 58 | .vector { 'a vector' } |
| 59 | .procedure { 'a procedure' } |
| 60 | else { 'a ${t}' } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | fn recipe_item(v scheme.Value) !string { |
| 65 | return match v.kind() { |
| 66 | .string { v.as_string()! } |
| 67 | .symbol { v.symbol_name()! } |
| 68 | .keyword { v.keyword_name()! } |
| 69 | else { error('items must be strings or keywords, not ${recipe_tag_name(v.kind())}') } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // list_items also rejects dotted and cyclic lists, without dropping a tail. |
| 74 | fn recipe_items_of(v scheme.Value, what string) ![]string { |
| 75 | match v.kind() { |
| 76 | .string, .symbol, .keyword { return [recipe_item(v)!] } |
| 77 | .nil { return []string{} } |
| 78 | .pair, .vector { |
| 79 | items := if v.kind() == .vector { |
| 80 | v.vector_items()! |
| 81 | } else { |
| 82 | v.list_items() or { return error('pkgbuild #:${what}: a list ends in (): ${err.msg()}') } |
| 83 | } |
| 84 | mut out := []string{} |
| 85 | for item in items { |
| 86 | out << recipe_item(item) or { return error('pkgbuild #:${what}: ${err.msg()}') } |
| 87 | } |
| 88 | return out |
| 89 | } |
| 90 | else { return error('pkgbuild #:${what} must be a string or a quoted list') } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // package metadata uses the kernel spelling of x86_64. |
| 95 | fn recipe_arch(a string) string { |
| 96 | if a == 'x86-64' { |
| 97 | return 'x86_64' |
| 98 | } |
| 99 | return a |
| 100 | } |
| 101 | |
| 102 | // Scheme keywords spell a disabled option as #:no-strip and package metadata uses !strip. |
| 103 | fn recipe_option(keyword string) string { |
| 104 | if keyword.starts_with('no-') { |
| 105 | return '!' + keyword[3..] |
| 106 | } |
| 107 | return keyword |
| 108 | } |
| 109 | |
| 110 | // a source checksum is either its full hexadecimal digest or SKIP. |
| 111 | fn recipe_hex(s string, len int, what string) ! { |
| 112 | if s == 'SKIP' { |
| 113 | return |
| 114 | } |
| 115 | if s.len != len { |
| 116 | return error('${what}: ${s} is ${s.len} characters; ${what} entries are ${len} hex characters or SKIP') |
| 117 | } |
| 118 | for c in s { |
| 119 | if !c.is_hex_digit() { |
| 120 | return error('${what}: ${s} has a character that is not hex (${c.ascii_str()})') |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // recipe_key40 checks a PGP fingerprint: the full 40 uppercase hex characters |
| 126 | fn recipe_key40(s string, i int) ! { |
| 127 | if s.len != 40 { |
| 128 | return error('validpgpkeys: entry ${i} is ${s.len} characters; fingerprints are 40: the full uppercase one') |
| 129 | } |
| 130 | for c in s { |
| 131 | if !c.is_hex_digit() { |
| 132 | return error('validpgpkeys: entry ${i} has a character that is not hex (${c.ascii_str()})') |
| 133 | } |
| 134 | if c >= `a` && c <= `f` { |
| 135 | return error('validpgpkeys: entry ${i} is lowercase hex (${c.ascii_str()}); uppercase fingerprints only') |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // keep phase closures in the VM that evaluated the recipe. |
| 141 | fn prim_defpkgbuild(mut vm scheme.VM, input []scheme.Value) !scheme.Value { |
| 142 | scheme.check_arity('vpac-defpkgbuild', input.len, 2, 2)! |
| 143 | forms := input[0].list_items()! |
| 144 | mut args := input[1].list_items()! |
| 145 | if forms.len != args.len { return error('pkgbuild: source and value counts differ') } |
| 146 | if args.len == 0 { |
| 147 | return error('pkgbuild: (defpkgbuild name ...) needs the name') |
| 148 | } |
| 149 | // the macro quotes the name so package names never resolve as variables. |
| 150 | if args[0].kind() == .pair { |
| 151 | quoted := args[0].list_items()! |
| 152 | if quoted.len == 2 && quoted[0].kind() == .symbol && quoted[0].symbol_name()! == 'quote' { |
| 153 | args[0] = quoted[1] |
| 154 | } |
| 155 | } |
| 156 | if args[0].kind() !in [.string, .symbol] { |
| 157 | return error('pkgbuild: the name is a symbol or a string, not ${recipe_tag_name(args[0].kind())}') |
| 158 | } |
| 159 | pkgbase_name := recipe_item(args[0])! |
| 160 | if (args.len - 1) % 2 != 0 { |
| 161 | return error('pkgbuild: #:key and value come in pairs') |
| 162 | } |
| 163 | mut fields := []string{} |
| 164 | mut vals := map[string]scheme.Value{} |
| 165 | fields << 'name' |
| 166 | vals['name'] = scheme.string_value(pkgbase_name) |
| 167 | mut i := 1 |
| 168 | for i < args.len { |
| 169 | kv := args[i] |
| 170 | if kv.kind() != .keyword { |
| 171 | return error('pkgbuild: expected a #:key, got ${scheme.write(kv)!}') |
| 172 | } |
| 173 | key := kv.keyword_name()! |
| 174 | val := args[i + 1] |
| 175 | if key in fields { |
| 176 | return error('pkgbuild #:${key} is set twice') |
| 177 | } |
| 178 | if key in recipe_list_keys { |
| 179 | mut items := recipe_items_of(val, key)! |
| 180 | match key { |
| 181 | 'pkgname' { |
| 182 | if items.len == 0 { |
| 183 | return error('pkgbuild #:pkgname must not be empty') |
| 184 | } |
| 185 | for it in items { |
| 186 | if it.contains(' ') { |
| 187 | return error('pkgbuild #:pkgname: "${it}" has whitespace in it') |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | 'arch' { |
| 192 | if items.len == 0 { |
| 193 | return error('pkgbuild #:arch must not be empty') |
| 194 | } |
| 195 | for j, it in items { |
| 196 | items[j] = recipe_arch(it) |
| 197 | } |
| 198 | } |
| 199 | 'validpgpkeys' { |
| 200 | for j, it in items { |
| 201 | recipe_key40(it, j)! |
| 202 | } |
| 203 | } |
| 204 | 'b2sums' { |
| 205 | for it in items { |
| 206 | recipe_hex(it, 128, 'b2sums')! |
| 207 | } |
| 208 | } |
| 209 | 'sha256sums' { |
| 210 | for it in items { |
| 211 | recipe_hex(it, 64, 'sha256sums')! |
| 212 | } |
| 213 | } |
| 214 | 'backup' { |
| 215 | for it in items { |
| 216 | if it.starts_with('/') { |
| 217 | return error('pkgbuild #:backup: "${it}" is absolute; backup paths are relative, no leading slash') |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | else {} |
| 222 | } |
| 223 | mut item_vals := []scheme.Value{} |
| 224 | for it in items { |
| 225 | item_vals << scheme.string_value(it) |
| 226 | } |
| 227 | fields << key |
| 228 | vals[key] = scheme.list(item_vals) |
| 229 | i += 2 |
| 230 | continue |
| 231 | } |
| 232 | if key in recipe_phases || key in recipe_hooks { |
| 233 | if val.kind() != .procedure { |
| 234 | return error('pkgbuild #:${key} must be a (lambda ...)') |
| 235 | } |
| 236 | arity := if key in recipe_hooks { recipe_hook_arity(key) } else { 0 } |
| 237 | recipe_check_lambda(forms[i + 1], key, arity)! |
| 238 | fields << key |
| 239 | vals[key] = val |
| 240 | i += 2 |
| 241 | continue |
| 242 | } |
| 243 | match key { |
| 244 | 'pkgver' { |
| 245 | // VCS recipes can calculate the version after extracting the sources. |
| 246 | if val.kind() != .string && val.kind() != .procedure { |
| 247 | return error('pkgbuild #:pkgver must be a string or a (lambda ...)') |
| 248 | } |
| 249 | fields << key |
| 250 | vals[key] = val |
| 251 | } |
| 252 | 'pkgrel', 'epoch' { |
| 253 | if val.kind() != .integer { |
| 254 | return error('pkgbuild #:${key} must be an integer') |
| 255 | } |
| 256 | if key == 'epoch' && val.as_integer()! < 0 { |
| 257 | return error('pkgbuild #:epoch is negative; 0 means absent') |
| 258 | } |
| 259 | fields << key |
| 260 | vals[key] = val |
| 261 | } |
| 262 | 'pkgdesc', 'url' { |
| 263 | if val.kind() != .string { |
| 264 | return error('pkgbuild #:${key} must be a string') |
| 265 | } |
| 266 | fields << key |
| 267 | vals[key] = val |
| 268 | } |
| 269 | else { |
| 270 | return error('pkgbuild has no key #:${key}') |
| 271 | } |
| 272 | } |
| 273 | i += 2 |
| 274 | } |
| 275 | // data-only packages can go straight to the package phase. |
| 276 | for req in ['pkgname', 'pkgver', 'pkgrel', 'pkgdesc', 'arch', 'package'] { |
| 277 | if req !in fields { |
| 278 | return error('pkgbuild: #:${req} is required') |
| 279 | } |
| 280 | } |
| 281 | return vm.call(vm.lookup('vpac-make-pkgbuild')!, [ |
| 282 | scheme.vector(fields.map(scheme.string_value(it))), |
| 283 | scheme.vector(fields.map(vals[it])), |
| 284 | ]) |
| 285 | } |
| 286 | |
| 287 | fn prim_exec(mut _vm scheme.VM, args []scheme.Value) !scheme.Value { |
| 288 | mut argv := []string{cap: args.len} |
| 289 | for arg in args { |
| 290 | argv << arg.as_string()! |
| 291 | } |
| 292 | run(argv)! |
| 293 | return scheme.nil_value() |
| 294 | } |
| 295 | |
| 296 | // prim_glob returns a sorted scheme list for for-each, empty when nothing |
| 297 | // matches: the loop in the format example iterates it directly |
| 298 | fn prim_glob(mut _vm scheme.VM, args []scheme.Value) !scheme.Value { |
| 299 | if args.len != 1 || args[0].kind() != .string { |
| 300 | return error('(glob pattern) takes one string pattern') |
| 301 | } |
| 302 | matches := glob(args[0].as_string()!)! |
| 303 | mut out := []scheme.Value{} |
| 304 | for m in matches { |
| 305 | out << scheme.string_value(m) |
| 306 | } |
| 307 | return scheme.list(out) |
| 308 | } |
| 309 | |
| 310 | fn prim_pkgdir(mut _vm scheme.VM, _ []scheme.Value) !scheme.Value { |
| 311 | return scheme.string_value(pkgdir()!) |
| 312 | } |
| 313 | |
| 314 | fn prim_srcdir(mut _vm scheme.VM, _ []scheme.Value) !scheme.Value { |
| 315 | return scheme.string_value(srcdir()!) |
| 316 | } |
| 317 | |
| 318 | fn prim_startdir(mut _vm scheme.VM, _ []scheme.Value) !scheme.Value { |
| 319 | return scheme.string_value(startdir()!) |
| 320 | } |
| 321 | |
| 322 | fn prim_this_pkg(mut _vm scheme.VM, _ []scheme.Value) !scheme.Value { |
| 323 | return scheme.string_value(this_pkg()!) |
| 324 | } |
| 325 | |
| 326 | // a repository can use a fallback version before it has a matching tag. |
| 327 | fn prim_git_desc(mut _vm scheme.VM, args []scheme.Value) !scheme.Value { |
| 328 | if args.len == 0 || args.len > 2 || args[0].kind() != .string { |
| 329 | return error('(git-desc pattern fallback?) takes a string pattern and an optional string fallback') |
| 330 | } |
| 331 | pattern := args[0].as_string()! |
| 332 | mut fallback := '' |
| 333 | if args.len == 2 { |
| 334 | if args[1].kind() != .string { |
| 335 | return error('(git-desc ...): the fallback must be a string') |
| 336 | } |
| 337 | fallback = args[1].as_string()! |
| 338 | } |
| 339 | return scheme.string_value(git_desc(pattern, fallback)!) |
| 340 | } |
| 341 | |
| 342 | // source forms retain literal lambda parameter lists for validation, without |
| 343 | // invoking build phases while reading package metadata. |
| 344 | fn recipe_check_lambda(form scheme.Value, key string, expected int) ! { |
| 345 | if form.kind() != .pair { return } |
| 346 | parts := form.list_items()! |
| 347 | if parts.len < 2 || parts[0].kind() != .symbol || parts[0].symbol_name()! != 'lambda' { return } |
| 348 | params := parts[1].list_items() or { return error('pkgbuild #:${key} needs a fixed parameter list') } |
| 349 | if params.len != expected { |
| 350 | return error('pkgbuild #:${key} takes ${expected} parameter${if expected == 1 { |
| 351 | '' |
| 352 | } else { |
| 353 | 's' |
| 354 | }}, not ${params.len}') |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | fn recipe_new_machine() !&scheme.VM { |
| 359 | mut m := scheme.new_with_options(max_steps: 100_000_000)! |
| 360 | m.register('vpac-defpkgbuild', prim_defpkgbuild)! |
| 361 | m.register('run', prim_exec)! |
| 362 | m.register('glob', prim_glob)! |
| 363 | m.register('pkgdir', prim_pkgdir)! |
| 364 | m.register('srcdir', prim_srcdir)! |
| 365 | m.register('startdir', prim_startdir)! |
| 366 | m.register('this-pkg', prim_this_pkg)! |
| 367 | m.register('git-desc', prim_git_desc)! |
| 368 | m.eval('(define-record-type vpac-pkgbuild (vpac-make-pkgbuild keys values) vpac-pkgbuild? (keys vpac-pkgbuild-keys) (values vpac-pkgbuild-values)) |
| 369 | (define-syntax defpkgbuild |
| 370 | (syntax-rules () |
| 371 | ((_) (vpac-defpkgbuild (quote ()) (quote ()))) |
| 372 | ((_ name arg ...) (vpac-defpkgbuild (quote (name arg ...)) (cons (quote name) (list arg ...))))))')! |
| 373 | return m |
| 374 | } |
| 375 | |
| 376 | // use the evaluator's reader so datum comments, escaped symbols and whitespace |
| 377 | // have the same meaning here. quoted data is not a package declaration. |
| 378 | fn recipe_count_form(arena &reader.DatumArena, id reader.NodeId, mut seen map[reader.NodeId]bool) !int { |
| 379 | first := arena.canonical_id(id)! |
| 380 | node := arena.node(first)! |
| 381 | if node.kind != .pair || first in seen { |
| 382 | return 0 |
| 383 | } |
| 384 | head := arena.node(arena.canonical_id(node.car)!)! |
| 385 | mut count := 0 |
| 386 | if head.kind == .symbol { |
| 387 | name := arena.symbol_name(node.car)! |
| 388 | if name in ['quote', 'quasiquote'] { |
| 389 | return 0 |
| 390 | } |
| 391 | if name == 'defpkgbuild' { |
| 392 | count++ |
| 393 | } |
| 394 | } |
| 395 | mut cursor := first |
| 396 | for cursor !in seen { |
| 397 | seen[cursor] = true |
| 398 | item := arena.node(cursor)! |
| 399 | if item.kind != .pair { |
| 400 | break |
| 401 | } |
| 402 | count += recipe_count_form(arena, item.car, mut seen)! |
| 403 | cursor = arena.canonical_id(item.cdr)! |
| 404 | } |
| 405 | return count |
| 406 | } |
| 407 | |
| 408 | fn recipe_defpkg_count(path string) !int { |
| 409 | source := os.read_file(path)! |
| 410 | doc := if os.file_ext(path) == '.vile' { |
| 411 | reader.read_alt(source)! |
| 412 | } else { |
| 413 | reader.read_all(source)! |
| 414 | } |
| 415 | mut seen := map[reader.NodeId]bool{} |
| 416 | mut count := 0 |
| 417 | for form in doc.forms { |
| 418 | count += recipe_count_form(&doc.arena, form, mut seen)! |
| 419 | } |
| 420 | return count |
| 421 | } |
| 422 | |
| 423 | // recipe_load_in checks that the final value is the package metadata record. |
| 424 | fn recipe_load_in(mut m scheme.VM, path string) !PackageMeta { |
| 425 | // a recipe that cannot be read fails here, with the read's own reason: the |
| 426 | // one-form check is never skipped on the way to a load that reads it |
| 427 | count := recipe_defpkg_count(path) or { return error('${path}: ${err.msg()}') } |
| 428 | if count > 1 { |
| 429 | return error('${path}: one (defpkgbuild ...) form per file, not ${count}') |
| 430 | } |
| 431 | v := m.eval_file(path)! |
| 432 | if v.kind() != .record || v.record_type()!.record_type_name()! != 'vpac-pkgbuild' { |
| 433 | return error('${path}: the last form must be a (defpkgbuild ...)') |
| 434 | } |
| 435 | return package_meta(v)! |
| 436 | } |
| 437 | |
| 438 | // recipe_load reads the final metadata record in the syntax selected by the filename. |
| 439 | pub fn recipe_load(path string) !PackageMeta { |
| 440 | mut m := recipe_new_machine()! |
| 441 | return recipe_load_in(mut m, path)! |
| 442 | } |
| 443 | |
| 444 | // evaluate the phase in the same VM as its metadata and closures. |
| 445 | pub fn recipe_run_phase(recipe string, phase string, params []string) ! { |
| 446 | mut m := recipe_new_machine()! |
| 447 | // load on this machine, not recipe_load's: the bootstrap macro has to be |
| 448 | // in the machine that runs the file, with its phases |
| 449 | meta := recipe_load_in(mut m, recipe)! |
| 450 | if !meta.has(phase) { |
| 451 | return error('${recipe}: the pkgbuild has no #:${phase}') |
| 452 | } |
| 453 | cb := meta.get(phase) |
| 454 | if cb.kind() != .procedure { |
| 455 | return error('${recipe}: #:${phase} is not a (lambda ...)') |
| 456 | } |
| 457 | res := m.call(cb, params.map(scheme.string_value(it)))! |
| 458 | if phase == 'pkgver' { |
| 459 | if res.kind() != .string { |
| 460 | return error('pkgver: the lambda ran into ${recipe_tag_name(res.kind())}, not a version string') |
| 461 | } |
| 462 | println(res.as_string()!) |
| 463 | } |
| 464 | } |
| 465 | |