| 1 | module vmakepkg |
| 2 | |
| 3 | import os |
| 4 | import blip.vlib.blip |
| 5 | |
| 6 | // ------------------------------------------------------------ PKGBUILD.vsh |
| 7 | // |
| 8 | // PKGBUILD.vsh is the same package written as a plain V script, with the |
| 9 | // phases as ordinary V functions: |
| 10 | // |
| 11 | // import vmakepkg |
| 12 | // import os |
| 13 | // |
| 14 | // fn spec() !vmakepkg.Spec { |
| 15 | // return vmakepkg.Spec{ |
| 16 | // pkgname: 'foobar' |
| 17 | // pkgver: '2.5' |
| 18 | // pkgrel: 2 |
| 19 | // pkgdesc: 'Text editor for X11' |
| 20 | // arch: ['x86_64'] |
| 21 | // url: 'https://example.com/foobar/' |
| 22 | // license: 'Apache-2.0' |
| 23 | // depends: ['glibc'] |
| 24 | // options: ['!strip' 'docs'] |
| 25 | // source: ['foobar-2.5.tar.gz::https://example.com/2.5.tar.gz'] |
| 26 | // b2sums: ['...'] |
| 27 | // hooks: ['post-install'] |
| 28 | // } |
| 29 | // } |
| 30 | // |
| 31 | // fn build() ! { |
| 32 | // vmakepkg.run('cmake -B build -DCMAKE_INSTALL_PREFIX=/usr')! |
| 33 | // vmakepkg.run('cmake --build build')! |
| 34 | // } |
| 35 | // |
| 36 | // fn package() ! { |
| 37 | // vmakepkg.run('cmake --install build DESTDIR=' + vmakepkg.pkgdir()!)! |
| 38 | // } |
| 39 | // |
| 40 | // fn post_install(v []string) ! { |
| 41 | // println('hint: run foobar-setup once') |
| 42 | // } |
| 43 | // |
| 44 | // vmakepkg.vsh_entry( |
| 45 | // spec: spec |
| 46 | // build: build |
| 47 | // package: package |
| 48 | // hooks: {'post-install': post_install} |
| 49 | // ) or { eprintln(err); exit(1) } |
| 50 | // |
| 51 | // `v run PKGBUILD.vsh generate` writes the PKGBUILD (and the .install |
| 52 | // beside the script), with the same head validation and phase wrappers the |
| 53 | // lip format gets, delegating to `v run PKGBUILD.vsh <phase>`. a |
| 54 | // `pkgver: fn () string` in the entry makes it a VCS package the same way a |
| 55 | // pkgver lambda does for the lip. |
| 56 | // |
| 57 | // the writers for the phases are the runners' own helpers: vmakepkg.run |
| 58 | // shells a line out with makepkg's spellings restored, vmakepkg.pkgdir and |
| 59 | // friends read the values makepkg exported for the running phase. |
| 60 | |
| 61 | // Spec is a vsh package's head, the same fields the lip format reads |
| 62 | pub struct Spec { |
| 63 | pub mut: |
| 64 | pkgname string |
| 65 | pkgnames []string |
| 66 | pkgver string |
| 67 | pkgrel i64 |
| 68 | epoch i64 |
| 69 | pkgdesc string |
| 70 | arch []string |
| 71 | url string |
| 72 | license string |
| 73 | depends []string |
| 74 | makedepends []string |
| 75 | checkdepends []string |
| 76 | optdepends []string |
| 77 | provides []string |
| 78 | conflicts []string |
| 79 | backup []string |
| 80 | options []string |
| 81 | source []string |
| 82 | validpgpkeys []string |
| 83 | b2sums []string |
| 84 | sha256sums []string |
| 85 | hooks []string |
| 86 | } |
| 87 | |
| 88 | // spec_names reads pkgname from a spec: the bare name or the split list. |
| 89 | // both spellings together are one intent, so the same name is not repeated, |
| 90 | // the order of first mention kept |
| 91 | fn spec_names(s &Spec) ![]string { |
| 92 | mut named := []string{} |
| 93 | if s.pkgname != '' { |
| 94 | named << s.pkgname |
| 95 | } |
| 96 | named << s.pkgnames |
| 97 | mut out := []string{} |
| 98 | for it in named { |
| 99 | if it in out { |
| 100 | continue |
| 101 | } |
| 102 | out << it |
| 103 | } |
| 104 | if out.len == 0 { |
| 105 | return error('pkgbuild: :pkgname is required') |
| 106 | } |
| 107 | for it in out { |
| 108 | if it.contains(' ') { |
| 109 | return error('pkgbuild :pkgname: "${it}" has whitespace in it') |
| 110 | } |
| 111 | } |
| 112 | return out |
| 113 | } |
| 114 | |
| 115 | // lip_list wraps string items into a blip list, the shape the head reader |
| 116 | // walks for the array fields |
| 117 | fn lip_list(items []string) blip.Value { |
| 118 | mut out := []blip.Value{} |
| 119 | for it in items { |
| 120 | out << blip.string(it) |
| 121 | } |
| 122 | return blip.list_from(out) |
| 123 | } |
| 124 | |
| 125 | // spec_meta hands the spec to the same head the lip format walks, as one |
| 126 | // struct value: the lists normalised, the sums and fingerprints checked, and |
| 127 | // the phases and hooks the script carries named, so lip_generate tells one |
| 128 | // truth about both kinds. `have` says which phases and hooks the entry |
| 129 | // carries, since the head only prints wrappers the script can answer |
| 130 | fn spec_meta(s &Spec, have map[string]bool) !blip.Value { |
| 131 | mut vals := map[string]blip.Value{} |
| 132 | names := spec_names(s)! |
| 133 | // name is the pkgbase, the canonical first of the names: for a |
| 134 | // split-only spec the bare pkgname spelling is empty and this is what |
| 135 | // carries one |
| 136 | vals['name'] = blip.string(names[0]) |
| 137 | vals['pkgname'] = lip_list(names) |
| 138 | if s.pkgver != '' { |
| 139 | vals['pkgver'] = blip.string(s.pkgver) |
| 140 | } |
| 141 | vals['pkgrel'] = blip.integer(s.pkgrel) |
| 142 | // as the lip's :epoch: 0 means absent, and a negative one is a mistake |
| 143 | // to say, not an epoch to leave out |
| 144 | if s.epoch < 0 { |
| 145 | return error('pkgbuild :epoch is negative; 0 means absent') |
| 146 | } |
| 147 | if s.epoch > 0 { |
| 148 | vals['epoch'] = blip.integer(s.epoch) |
| 149 | } |
| 150 | if s.pkgdesc == '' { |
| 151 | return error('pkgbuild: :pkgdesc is required') |
| 152 | } |
| 153 | vals['pkgdesc'] = blip.string(s.pkgdesc) |
| 154 | if s.arch.len == 0 { |
| 155 | return error('pkgbuild: :arch is required') |
| 156 | } |
| 157 | mut arches := []string{} |
| 158 | for a in s.arch { |
| 159 | arches << lip_arch(a) |
| 160 | } |
| 161 | vals['arch'] = lip_list(arches) |
| 162 | if s.url != '' { |
| 163 | vals['url'] = blip.string(s.url) |
| 164 | } |
| 165 | if s.license != '' { |
| 166 | vals['license'] = blip.string(s.license) |
| 167 | } |
| 168 | map2 := { |
| 169 | 'depends': s.depends |
| 170 | 'makedepends': s.makedepends |
| 171 | 'checkdepends': s.checkdepends |
| 172 | 'optdepends': s.optdepends |
| 173 | 'provides': s.provides |
| 174 | 'conflicts': s.conflicts |
| 175 | 'options': s.options |
| 176 | 'source': s.source |
| 177 | } |
| 178 | for key, items in map2 { |
| 179 | if items.len > 0 { |
| 180 | vals[key] = lip_list(items) |
| 181 | } |
| 182 | } |
| 183 | if s.backup.len > 0 { |
| 184 | for b in s.backup { |
| 185 | if b.starts_with('/') { |
| 186 | return error('pkgbuild :backup: "${b}" is absolute; backup paths are relative, no leading slash') |
| 187 | } |
| 188 | } |
| 189 | vals['backup'] = lip_list(s.backup) |
| 190 | } |
| 191 | if s.validpgpkeys.len > 0 { |
| 192 | for i, k in s.validpgpkeys { |
| 193 | lip_key40(k, i)! |
| 194 | } |
| 195 | vals['validpgpkeys'] = lip_list(s.validpgpkeys) |
| 196 | } |
| 197 | if s.b2sums.len > 0 { |
| 198 | for it in s.b2sums { |
| 199 | lip_hex(it, 128, 'b2sums')! |
| 200 | } |
| 201 | vals['b2sums'] = lip_list(s.b2sums) |
| 202 | } |
| 203 | if s.sha256sums.len > 0 { |
| 204 | for it in s.sha256sums { |
| 205 | lip_hex(it, 64, 'sha256sums')! |
| 206 | } |
| 207 | vals['sha256sums'] = lip_list(s.sha256sums) |
| 208 | } |
| 209 | for phase in ['prepare', 'build', 'check', 'package', 'pkgver'] { |
| 210 | if have[phase] { |
| 211 | vals[phase] = blip.nil_value() |
| 212 | } |
| 213 | } |
| 214 | for h in s.hooks { |
| 215 | if h !in lip_hooks { |
| 216 | return error('pkgbuild hooks: :${h} is not a hook makepkg knows') |
| 217 | } |
| 218 | if !have[h] { |
| 219 | return error('pkgbuild hooks: the entry holds no ${h} body') |
| 220 | } |
| 221 | vals[h] = blip.nil_value() |
| 222 | } |
| 223 | return blip.struct_value('pkgbuild', vals.keys(), vals) |
| 224 | } |
| 225 | |
| 226 | // lip_make_from_spec generates the PKGBUILD (and a .install when hooks are |
| 227 | // carried) from a PKGBUILD.vsh's spec and its entry's phase inventory, |
| 228 | // delegating the phases to `v run` against the script itself |
| 229 | pub fn lip_make_from_spec(s &Spec, runner string, script_path string, have map[string]bool) !LipBet { |
| 230 | meta := spec_meta(s, have)! |
| 231 | return lip_generate(meta, runner, script_path, .vsh)! |
| 232 | } |
| 233 | |
| 234 | // run shells one line out with sh, carrying makepkg's spellings of its state |
| 235 | // into the child shell, like the runner's (run) does from the lip side: the |
| 236 | // phase exported them for this process under the VPAC_ names |
| 237 | pub fn run(cmd string) ! { |
| 238 | // the names reach the shell as env's arguments and the line as sh's: no |
| 239 | // quoting stands between them and the child. the line itself is shell, |
| 240 | // as the script writes it |
| 241 | mut args := ['env'] |
| 242 | pairs := [['pkgdir', 'VPAC_PKGDIR'], ['srcdir', 'VPAC_SRCDIR'], ['startdir', 'VPAC_STARTDIR'], |
| 243 | ['pkgname', 'VPAC_PKGNAME'], ['pkgver', 'VPAC_PKGVER']] |
| 244 | for pair in pairs { |
| 245 | v := os.getenv(pair[1]) |
| 246 | if v != '' { |
| 247 | args << '${pair[0]}=${v}' |
| 248 | } |
| 249 | } |
| 250 | args << ['sh', '-c', cmd] |
| 251 | res := os.system_args(args) |
| 252 | if res != 0 { |
| 253 | return error('run: exit code ${res} from: ${cmd}') |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // pkgdir is the directory makepkg is filling during the package phase, the |
| 258 | // value of $pkgdir, exported for the phase as VPAC_PKGDIR |
| 259 | pub fn pkgdir() !string { |
| 260 | p := os.getenv('VPAC_PKGDIR') |
| 261 | if p == '' { |
| 262 | return error('pkgdir: the runner only exports it inside a makepkg phase') |
| 263 | } |
| 264 | return p |
| 265 | } |
| 266 | |
| 267 | // srcdir is where the sources were unpacked, $srcdir |
| 268 | pub fn srcdir() !string { |
| 269 | p := os.getenv('VPAC_SRCDIR') |
| 270 | if p == '' { |
| 271 | return error('srcdir: the runner only exports it inside a makepkg phase') |
| 272 | } |
| 273 | return p |
| 274 | } |
| 275 | |
| 276 | // startdir is the build script's own directory, $startdir |
| 277 | pub fn startdir() !string { |
| 278 | p := os.getenv('VPAC_STARTDIR') |
| 279 | if p == '' { |
| 280 | return error('startdir: the runner only exports it inside a makepkg phase') |
| 281 | } |
| 282 | return p |
| 283 | } |
| 284 | |
| 285 | // this_pkg is the split a shared package function is packing right now, |
| 286 | // $pkgname, exported for the phase as VPAC_PKGNAME |
| 287 | pub fn this_pkg() !string { |
| 288 | p := os.getenv('VPAC_PKGNAME') |
| 289 | if p == '' { |
| 290 | return error('this_pkg: the runner only exports it inside a makepkg phase') |
| 291 | } |
| 292 | return p |
| 293 | } |
| 294 | |
| 295 | // glob spills one pattern into a sorted list of matches, empty when nothing |
| 296 | // matches |
| 297 | pub fn glob(pattern string) ![]string { |
| 298 | return os.glob(pattern)! |
| 299 | } |
| 300 | |
| 301 | // git_desc runs git describe in the cwd the phase runs under (srcdir, where |
| 302 | // makepkg runs its phases); the fallback comes out verbatim when describe |
| 303 | // finds nothing, so a young tree still has a version before its first tag |
| 304 | pub fn git_desc(pattern string, fallback string) !string { |
| 305 | res := os.exec(['git', '--no-pager', 'describe', '--long', '--tags', '--match', pattern]) |
| 306 | if res.exit_code != 0 { |
| 307 | if fallback != '' { |
| 308 | return fallback |
| 309 | } |
| 310 | return error('git-desc: git describe failed for ${pattern}: ${res.output.trim_space()}') |
| 311 | } |
| 312 | return res.output.trim_space() |
| 313 | } |
| 314 | |
| 315 | // VshEntry is what a PKGBUILD.vsh hands to vsh_entry: the spec, the phase |
| 316 | // functions, and the hooks by name. generate needs the spec, and makepkg |
| 317 | // needs package (build only when the package has one, as makepkg's build()): |
| 318 | // the entry says so if the script leaves one out |
| 319 | @[params] |
| 320 | pub struct VshEntry { |
| 321 | pub mut: |
| 322 | spec ?fn () !Spec |
| 323 | pkgver ?fn () string |
| 324 | prepare ?fn () ! |
| 325 | build ?fn () ! |
| 326 | check ?fn () ! |
| 327 | package ?fn () ! |
| 328 | hooks map[string]fn ([]string) ! |
| 329 | } |
| 330 | |
| 331 | // vsh_entry is the PKGBUILD.vsh runner, the script's one last line: with the |
| 332 | // generate argument it writes the PKGBUILD and the .install beside the |
| 333 | // script, and otherwise it runs one phase with the arguments pacman or the |
| 334 | // phase wrapper hands it. a failing phase exits nonzero, the way a makepkg |
| 335 | // phase would |
| 336 | pub fn vsh_entry(e VshEntry) ! { |
| 337 | // VPAC_VSH_PATH carries the script's own path: `v run` does not say which |
| 338 | // script called in a way the writer can trust |
| 339 | vsh_run(e, os.args#[1..], os.getenv('VPAC_VSH_PATH'))! |
| 340 | } |
| 341 | |
| 342 | // vsh_run is vsh_entry with the command line and the script's path handed |
| 343 | // in: args are what follows the script's name, the phase first |
| 344 | fn vsh_run(e VshEntry, args []string, script_path string) ! { |
| 345 | phase := if args.len > 0 { args[0] } else { '' } |
| 346 | match phase { |
| 347 | 'generate' { |
| 348 | if script_path == '' { |
| 349 | return error("generate: VPAC_VSH_PATH must hold the script's path") |
| 350 | } |
| 351 | spec := e.spec or { return error('pkgbuild: a :spec is required') } |
| 352 | s := spec()! |
| 353 | // build is optional, as makepkg's build() is; package is not |
| 354 | if e.package == none { |
| 355 | return error('pkgbuild: a :package phase is required') |
| 356 | } |
| 357 | if e.pkgver == none && s.pkgver == '' { |
| 358 | return error('pkgbuild: :pkgver is required') |
| 359 | } |
| 360 | for h in s.hooks { |
| 361 | if h !in e.hooks { |
| 362 | return error('pkgbuild hooks: the entry holds no ${h} body') |
| 363 | } |
| 364 | } |
| 365 | // hooks in the entry that the spec does not list stay a typo |
| 366 | for name in e.hooks.keys() { |
| 367 | if name !in s.hooks { |
| 368 | return error('pkgbuild hooks: the spec does not list :${name}') |
| 369 | } |
| 370 | } |
| 371 | mut have := map[string]bool{} |
| 372 | if e.prepare != none { |
| 373 | have['prepare'] = true |
| 374 | } |
| 375 | if e.pkgver != none { |
| 376 | have['pkgver'] = true |
| 377 | } |
| 378 | for hook in e.hooks.keys() { |
| 379 | have[hook] = true |
| 380 | } |
| 381 | if e.build != none { |
| 382 | have['build'] = true |
| 383 | } |
| 384 | if e.check != none { |
| 385 | have['check'] = true |
| 386 | } |
| 387 | // package is makepkg's ground truth, so its wrapper always lands |
| 388 | have['package'] = true |
| 389 | // the runner the wrappers bake is the persistent v, not this |
| 390 | // process's temp binary: the root side carries it as VPAC_V |
| 391 | vpath := if os.getenv('VPAC_V') != '' { |
| 392 | os.getenv('VPAC_V') |
| 393 | } else { |
| 394 | os.find_abs_path_of_executable('v') or { |
| 395 | return error('generate: the v compiler is not in the path') |
| 396 | } |
| 397 | } |
| 398 | bb := lip_make_from_spec(s, vpath, script_path, have)! |
| 399 | out_dir := os.dir(script_path) |
| 400 | os.write_file('${out_dir}/PKGBUILD', bb.text)! |
| 401 | if bb.install != '' { |
| 402 | os.write_file('${out_dir}/${bb.base}.install', bb.install)! |
| 403 | } |
| 404 | mut wrote := '${out_dir}/PKGBUILD' |
| 405 | if bb.install != '' { |
| 406 | wrote += ' and ${bb.base}.install' |
| 407 | } |
| 408 | println('wrote ${wrote}') |
| 409 | } |
| 410 | 'pkgver' { |
| 411 | if f := e.pkgver { |
| 412 | out := f() |
| 413 | if out == '' { |
| 414 | return error('pkgver: it returned an empty version') |
| 415 | } |
| 416 | println(out) |
| 417 | } else { |
| 418 | return error('pkgver: the entry holds no pkgver function') |
| 419 | } |
| 420 | } |
| 421 | 'prepare' { |
| 422 | if f := e.prepare { |
| 423 | f()! |
| 424 | } else { |
| 425 | return error('prepare: the entry holds no prepare function') |
| 426 | } |
| 427 | } |
| 428 | 'build' { |
| 429 | if f := e.build { |
| 430 | f()! |
| 431 | } else { |
| 432 | return error('build: the entry holds no build function') |
| 433 | } |
| 434 | } |
| 435 | 'check' { |
| 436 | if f := e.check { |
| 437 | f()! |
| 438 | } else { |
| 439 | return error('check: the entry holds no check function') |
| 440 | } |
| 441 | } |
| 442 | 'package' { |
| 443 | if f := e.package { |
| 444 | f()! |
| 445 | } else { |
| 446 | return error('package: the entry holds no package function') |
| 447 | } |
| 448 | } |
| 449 | else { |
| 450 | // no argument: the usage, not a call that lost its phase quietly |
| 451 | if phase == '' { |
| 452 | return error('vsh: give an argument: generate to write the PKGBUILD, or one of the phases (build, check, package, pkgver) or hooks') |
| 453 | } |
| 454 | if phase in e.hooks { |
| 455 | e.hooks[phase](args[1..])! |
| 456 | return |
| 457 | } |
| 458 | return error('vsh: "${phase}" is neither generate, a phase, nor a hook') |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | |