| 1 | module vmakepkg |
| 2 | |
| 3 | import os |
| 4 | import json2 |
| 5 | import vile.scheme |
| 6 | |
| 7 | // ------------------------------------------------------------ PKGBUILD.vsh |
| 8 | // |
| 9 | // PKGBUILD.vsh is the same package written as a plain V script, with the |
| 10 | // phases as ordinary V functions: |
| 11 | // |
| 12 | // import vmakepkg |
| 13 | // import os |
| 14 | // |
| 15 | // fn spec() !vmakepkg.Spec { |
| 16 | // return vmakepkg.Spec{ |
| 17 | // pkgname: 'foobar' |
| 18 | // pkgver: '2.5' |
| 19 | // pkgrel: 2 |
| 20 | // pkgdesc: 'Text editor for X11' |
| 21 | // arch: ['x86_64'] |
| 22 | // url: 'https://example.com/foobar/' |
| 23 | // license: 'Apache-2.0' |
| 24 | // depends: ['glibc'] |
| 25 | // options: ['!strip' 'docs'] |
| 26 | // source: ['foobar-2.5.tar.gz::https://example.com/2.5.tar.gz'] |
| 27 | // b2sums: ['...'] |
| 28 | // hooks: ['post-install'] |
| 29 | // } |
| 30 | // } |
| 31 | // |
| 32 | // fn build() ! { |
| 33 | // vmakepkg.run(['cmake', '-B', 'build', '-DCMAKE_INSTALL_PREFIX=/usr'])! |
| 34 | // vmakepkg.run(['cmake', '--build', 'build'])! |
| 35 | // } |
| 36 | // |
| 37 | // fn package() ! { |
| 38 | // vmakepkg.run(['env', 'DESTDIR=' + vmakepkg.pkgdir()!, 'cmake', '--install', 'build'])! |
| 39 | // } |
| 40 | // |
| 41 | // fn post_install(v []string) ! { |
| 42 | // println('hint: run foobar-setup once') |
| 43 | // } |
| 44 | // |
| 45 | // vmakepkg.vsh_entry( |
| 46 | // spec: spec |
| 47 | // build: build |
| 48 | // package: package |
| 49 | // hooks: {'post-install': post_install} |
| 50 | // ) or { eprintln(err); exit(1) } |
| 51 | // |
| 52 | // vpac compiles the script once and invokes the executable for metadata, |
| 53 | // phases and packaged install hooks. run takes an argv array. |
| 54 | |
| 55 | // Spec is a vsh package's head, the same fields the recipe format reads |
| 56 | pub struct Spec { |
| 57 | pub mut: |
| 58 | pkgname string |
| 59 | pkgnames []string |
| 60 | pkgver string |
| 61 | pkgrel i64 |
| 62 | epoch i64 |
| 63 | pkgdesc string |
| 64 | arch []string |
| 65 | url string |
| 66 | license string |
| 67 | depends []string |
| 68 | makedepends []string |
| 69 | checkdepends []string |
| 70 | optdepends []string |
| 71 | provides []string |
| 72 | conflicts []string |
| 73 | backup []string |
| 74 | options []string |
| 75 | source []string |
| 76 | validpgpkeys []string |
| 77 | b2sums []string |
| 78 | sha256sums []string |
| 79 | hooks []string |
| 80 | } |
| 81 | |
| 82 | // spec_names reads pkgname from a spec: the bare name or the split list. |
| 83 | // both spellings together are one intent, so the same name is not repeated, |
| 84 | // the order of first mention kept |
| 85 | fn spec_names(s &Spec) ![]string { |
| 86 | mut named := []string{} |
| 87 | if s.pkgname != '' { |
| 88 | named << s.pkgname |
| 89 | } |
| 90 | named << s.pkgnames |
| 91 | mut out := []string{} |
| 92 | for it in named { |
| 93 | if it in out { |
| 94 | continue |
| 95 | } |
| 96 | out << it |
| 97 | } |
| 98 | if out.len == 0 { |
| 99 | return error('pkgbuild: #:pkgname is required') |
| 100 | } |
| 101 | for it in out { |
| 102 | if it.contains(' ') { |
| 103 | return error('pkgbuild #:pkgname: "${it}" has whitespace in it') |
| 104 | } |
| 105 | } |
| 106 | return out |
| 107 | } |
| 108 | |
| 109 | // recipe_list wraps string items into a scheme list, the shape the head reader |
| 110 | // walks for the array fields |
| 111 | fn recipe_list(items []string) scheme.Value { |
| 112 | mut out := []scheme.Value{} |
| 113 | for it in items { |
| 114 | out << scheme.string_value(it) |
| 115 | } |
| 116 | return scheme.list(out) |
| 117 | } |
| 118 | |
| 119 | // spec_meta hands the spec to the same head the recipe format walks, as one |
| 120 | // metadata: the lists normalised, the sums and fingerprints checked, and |
| 121 | // the declared phases and hooks must match functions in the compiled entry. |
| 122 | fn spec_meta(s &Spec, have map[string]bool) !PackageMeta { |
| 123 | mut vals := map[string]scheme.Value{} |
| 124 | names := spec_names(s)! |
| 125 | // name is the pkgbase, the canonical first of the names: for a |
| 126 | // split-only spec the bare pkgname spelling is empty and this is what |
| 127 | // carries one |
| 128 | vals['name'] = scheme.string_value(names[0]) |
| 129 | vals['pkgname'] = recipe_list(names) |
| 130 | if s.pkgver != '' { |
| 131 | vals['pkgver'] = scheme.string_value(s.pkgver) |
| 132 | } |
| 133 | vals['pkgrel'] = scheme.integer(s.pkgrel) |
| 134 | // as the recipe's #:epoch: 0 means absent, and a negative one is a mistake |
| 135 | // to say, not an epoch to leave out |
| 136 | if s.epoch < 0 { |
| 137 | return error('pkgbuild #:epoch is negative; 0 means absent') |
| 138 | } |
| 139 | if s.epoch > 0 { |
| 140 | vals['epoch'] = scheme.integer(s.epoch) |
| 141 | } |
| 142 | if s.pkgdesc == '' { |
| 143 | return error('pkgbuild: #:pkgdesc is required') |
| 144 | } |
| 145 | vals['pkgdesc'] = scheme.string_value(s.pkgdesc) |
| 146 | if s.arch.len == 0 { |
| 147 | return error('pkgbuild: #:arch is required') |
| 148 | } |
| 149 | mut arches := []string{} |
| 150 | for a in s.arch { |
| 151 | arches << recipe_arch(a) |
| 152 | } |
| 153 | vals['arch'] = recipe_list(arches) |
| 154 | if s.license != '' { vals['license'] = recipe_list([s.license]) } |
| 155 | if s.url != '' { |
| 156 | vals['url'] = scheme.string_value(s.url) |
| 157 | } |
| 158 | if s.license != '' { |
| 159 | vals['license'] = scheme.string_value(s.license) |
| 160 | } |
| 161 | map2 := { |
| 162 | 'depends': s.depends |
| 163 | 'makedepends': s.makedepends |
| 164 | 'checkdepends': s.checkdepends |
| 165 | 'optdepends': s.optdepends |
| 166 | 'provides': s.provides |
| 167 | 'conflicts': s.conflicts |
| 168 | 'options': s.options |
| 169 | 'source': s.source |
| 170 | } |
| 171 | for key, items in map2 { |
| 172 | if items.len > 0 { |
| 173 | vals[key] = recipe_list(items) |
| 174 | } |
| 175 | } |
| 176 | if s.backup.len > 0 { |
| 177 | for b in s.backup { |
| 178 | if b.starts_with('/') { |
| 179 | return error('pkgbuild #:backup: "${b}" is absolute; backup paths are relative, no leading slash') |
| 180 | } |
| 181 | } |
| 182 | vals['backup'] = recipe_list(s.backup) |
| 183 | } |
| 184 | if s.validpgpkeys.len > 0 { |
| 185 | for i, k in s.validpgpkeys { |
| 186 | recipe_key40(k, i)! |
| 187 | } |
| 188 | vals['validpgpkeys'] = recipe_list(s.validpgpkeys) |
| 189 | } |
| 190 | if s.b2sums.len > 0 { |
| 191 | for it in s.b2sums { |
| 192 | recipe_hex(it, 128, 'b2sums')! |
| 193 | } |
| 194 | vals['b2sums'] = recipe_list(s.b2sums) |
| 195 | } |
| 196 | if s.sha256sums.len > 0 { |
| 197 | for it in s.sha256sums { |
| 198 | recipe_hex(it, 64, 'sha256sums')! |
| 199 | } |
| 200 | vals['sha256sums'] = recipe_list(s.sha256sums) |
| 201 | } |
| 202 | for phase in ['prepare', 'build', 'check', 'package', 'pkgver'] { |
| 203 | if have[phase] { |
| 204 | vals[phase] = scheme.nil_value() |
| 205 | } |
| 206 | } |
| 207 | for h in s.hooks { |
| 208 | if h !in recipe_hooks { |
| 209 | return error('pkgbuild hooks: #:${h} is not a hook makepkg knows') |
| 210 | } |
| 211 | if !have[h] { |
| 212 | return error('pkgbuild hooks: the entry holds no ${h} body') |
| 213 | } |
| 214 | vals[h] = scheme.nil_value() |
| 215 | } |
| 216 | return PackageMeta{ values: vals } |
| 217 | } |
| 218 | |
| 219 | // run executes a program with literal arguments and the phase's standard streams. |
| 220 | pub fn run(argv []string) ! { |
| 221 | if argv.len == 0 || argv[0] == '' || argv.any(it.contains('\x00')) { |
| 222 | return error('run expects a program and literal arguments') |
| 223 | } |
| 224 | code := os.system_args(argv) |
| 225 | if code != 0 { return error('${argv[0]} exited with status ${code}') } |
| 226 | } |
| 227 | |
| 228 | // pkgdir is the staging directory for the current package. |
| 229 | pub fn pkgdir() !string { |
| 230 | p := os.getenv('VPAC_PKGDIR') |
| 231 | if p == '' { |
| 232 | return error('pkgdir: unavailable outside a recipe phase') |
| 233 | } |
| 234 | return p |
| 235 | } |
| 236 | |
| 237 | // srcdir is where the sources were unpacked, $srcdir |
| 238 | pub fn srcdir() !string { |
| 239 | p := os.getenv('VPAC_SRCDIR') |
| 240 | if p == '' { |
| 241 | return error('srcdir: unavailable outside a recipe phase') |
| 242 | } |
| 243 | return p |
| 244 | } |
| 245 | |
| 246 | // startdir is the build script's own directory, $startdir |
| 247 | pub fn startdir() !string { |
| 248 | p := os.getenv('VPAC_STARTDIR') |
| 249 | if p == '' { |
| 250 | return error('startdir: unavailable outside a recipe phase') |
| 251 | } |
| 252 | return p |
| 253 | } |
| 254 | |
| 255 | // this_pkg is the split a shared package function is packing right now, |
| 256 | // $pkgname, exported for the phase as VPAC_PKGNAME |
| 257 | pub fn this_pkg() !string { |
| 258 | p := os.getenv('VPAC_PKGNAME') |
| 259 | if p == '' { |
| 260 | return error('this_pkg: unavailable outside a recipe phase') |
| 261 | } |
| 262 | return p |
| 263 | } |
| 264 | |
| 265 | // glob spills one pattern into a sorted list of matches, empty when nothing |
| 266 | // matches |
| 267 | pub fn glob(pattern string) ![]string { |
| 268 | return os.glob(pattern)! |
| 269 | } |
| 270 | |
| 271 | // a young tree can use the fallback until it has its first matching tag. |
| 272 | pub fn git_desc(pattern string, fallback string) !string { |
| 273 | res := os.exec(['git', '--no-pager', 'describe', '--long', '--tags', '--match', pattern]) |
| 274 | if res.exit_code != 0 { |
| 275 | if fallback != '' { |
| 276 | return fallback |
| 277 | } |
| 278 | return error('git-desc: git describe failed for ${pattern}: ${res.output.trim_space()}') |
| 279 | } |
| 280 | return res.output.trim_space() |
| 281 | } |
| 282 | |
| 283 | // VshEntry supplies the package spec, phase functions and named install hooks. |
| 284 | @[params] |
| 285 | pub struct VshEntry { |
| 286 | pub mut: |
| 287 | spec ?fn () !Spec |
| 288 | pkgver ?fn () string |
| 289 | prepare ?fn () ! |
| 290 | build ?fn () ! |
| 291 | check ?fn () ! |
| 292 | package ?fn () ! |
| 293 | hooks map[string]fn ([]string) ! |
| 294 | } |
| 295 | |
| 296 | // vsh_entry dispatches the compiled recipe's metadata, phases and hooks. |
| 297 | pub fn vsh_entry(e VshEntry) ! { |
| 298 | // VPAC_VSH_PATH carries the script's own path: `v run` does not say which |
| 299 | // script called in a way the writer can trust |
| 300 | vsh_run(e, os.args#[1..], os.getenv('VPAC_VSH_PATH'))! |
| 301 | } |
| 302 | |
| 303 | // vsh_run is vsh_entry with the command line and the script's path handed |
| 304 | // in: args are what follows the script's name, the phase first |
| 305 | fn vsh_run(e VshEntry, args []string, script_path string) ! { |
| 306 | phase := if args.len > 0 { args[0] } else { '' } |
| 307 | match phase { |
| 308 | 'generate' { |
| 309 | output := if args.len == 2 && args[1] != '' { |
| 310 | args[1] |
| 311 | } else if args.len == 1 && script_path != '' { |
| 312 | os.join_path(os.dir(script_path), 'recipe.json') |
| 313 | } else { |
| 314 | return error('generate needs an output file or VPAC_VSH_PATH') |
| 315 | } |
| 316 | spec := e.spec or { return error('pkgbuild: a :spec is required') } |
| 317 | s := spec()! |
| 318 | // a data package can go straight to its package phase. |
| 319 | if e.package == none { |
| 320 | return error('pkgbuild: a :package phase is required') |
| 321 | } |
| 322 | if e.pkgver == none && s.pkgver == '' { |
| 323 | return error('pkgbuild: :pkgver is required') |
| 324 | } |
| 325 | for h in s.hooks { |
| 326 | if h !in e.hooks { |
| 327 | return error('pkgbuild hooks: the entry holds no ${h} body') |
| 328 | } |
| 329 | } |
| 330 | // hooks in the entry that the spec does not list stay a typo |
| 331 | for name in e.hooks.keys() { |
| 332 | if name !in s.hooks { |
| 333 | return error('pkgbuild hooks: the spec does not list :${name}') |
| 334 | } |
| 335 | } |
| 336 | mut have := map[string]bool{} |
| 337 | if e.prepare != none { |
| 338 | have['prepare'] = true |
| 339 | } |
| 340 | if e.pkgver != none { |
| 341 | have['pkgver'] = true |
| 342 | } |
| 343 | for hook in e.hooks.keys() { |
| 344 | have[hook] = true |
| 345 | } |
| 346 | if e.build != none { |
| 347 | have['build'] = true |
| 348 | } |
| 349 | if e.check != none { |
| 350 | have['check'] = true |
| 351 | } |
| 352 | // every recipe has a package phase. |
| 353 | have['package'] = true |
| 354 | meta := spec_meta(s, have)! |
| 355 | desc := native_description(meta)! |
| 356 | os.write_file(output, json2.encode(desc))! |
| 357 | } |
| 358 | '--nspkg-hook' { |
| 359 | if args.len < 4 || args[1] !in recipe_hooks || args.len != 3 + recipe_hook_arity(args[1]) { |
| 360 | return error('invalid native hook arguments') |
| 361 | } |
| 362 | os.setenv('VPAC_PKGNAME', args[2], true) |
| 363 | if hook := e.hooks[args[1]] { hook(args[3..])! } |
| 364 | } |
| 365 | 'pkgver' { |
| 366 | if f := e.pkgver { |
| 367 | out := f() |
| 368 | if out == '' { |
| 369 | return error('pkgver: it returned an empty version') |
| 370 | } |
| 371 | println(out) |
| 372 | } else { |
| 373 | return error('pkgver: the entry holds no pkgver function') |
| 374 | } |
| 375 | } |
| 376 | 'prepare' { |
| 377 | if f := e.prepare { |
| 378 | f()! |
| 379 | } else { |
| 380 | return error('prepare: the entry holds no prepare function') |
| 381 | } |
| 382 | } |
| 383 | 'build' { |
| 384 | if f := e.build { |
| 385 | f()! |
| 386 | } else { |
| 387 | return error('build: the entry holds no build function') |
| 388 | } |
| 389 | } |
| 390 | 'check' { |
| 391 | if f := e.check { |
| 392 | f()! |
| 393 | } else { |
| 394 | return error('check: the entry holds no check function') |
| 395 | } |
| 396 | } |
| 397 | 'package' { |
| 398 | if f := e.package { |
| 399 | f()! |
| 400 | } else { |
| 401 | return error('package: the entry holds no package function') |
| 402 | } |
| 403 | } |
| 404 | else { |
| 405 | // no argument: the usage, not a call that lost its phase quietly |
| 406 | if phase == '' { |
| 407 | return error('vsh: give an argument: generate to describe the recipe, or one of the phases (build, check, package, pkgver) or hooks') |
| 408 | } |
| 409 | if phase in e.hooks { |
| 410 | e.hooks[phase](args[1..])! |
| 411 | return |
| 412 | } |
| 413 | return error('vsh: "${phase}" is neither generate, a phase, nor a hook') |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |