vpac / vmakepkg / native_build.v
149 lines · 142 sloc · 4.71 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module vmakepkg
2
3import json2
4import os
5
6fn C.mkdtemp(&char) &char
7
8fn private_dir(parent string) !string {
9 path := os.join_path(parent, '.vpac-XXXXXX').clone()
10 if C.mkdtemp(&char(path.str)) == unsafe { nil } { return os.last_error() }
11 return path
12}
13
14fn command_output(argv []string) !string {
15 result := os.exec(argv)
16 if result.exit_code != 0 {
17 return error('${argv[0]} exited with status ${result.exit_code}: ${result.output.trim_space()}')
18 }
19 return result.output.trim_space()
20}
21
22fn filename_part(value string) bool {
23 return value.len > 0 && value !in ['.', '..'] && !value.starts_with('-')
24 && value.bytes().all(it.is_alnum() || it in [`@`, `_`, `+`, `.`, `-`])
25}
26
27// the builder has a fresh private working directory per invocation. the caller
28// receives the completed split set only after every archive and signature exists.
29pub fn native_build(path string, sign bool) ![]string {
30 if os.geteuid() == 0 { return error('native package builds require an unprivileged user') }
31 recipe := os.abs_path(path)
32 if os.base(recipe) !in ['PKGBUILD.scm', 'PKGBUILD.vile', 'PKGBUILD.vsh'] {
33 return error('native build expects PKGBUILD.scm, PKGBUILD.vile or PKGBUILD.vsh')
34 }
35 start := os.dir(recipe)
36 work := private_dir(start)!
37 mut complete := false
38 defer {
39 if !complete {
40 os.rmdir_all(work) or { eprintln('remove incomplete build: ${err}') }
41 }
42 }
43 src := os.join_path(work, 'src')
44 os.mkdir(src)!
45 mut runner := [os.executable(), '--recipe-run', recipe]
46 mut desc := NativeRecipe{}
47 if os.file_ext(recipe) == '.vsh' {
48 mut compiler := os.getenv('VPAC_V')
49 if compiler == '' { compiler = 'v' }
50 exe := os.join_path(work, 'vsh-runner')
51 mut compile := [compiler]
52 // this executable and its hooks target the builder's ABI. supplying it
53 // keeps V from invoking the host's ldd shell script to guess the libc.
54 $if linux && glibc ? {
55 compile << '-glibc'
56 } $else $if linux && musl ? {
57 compile << '-musl'
58 }
59 compile << ['-skip-running', '-o', exe, recipe]
60 run(compile)!
61 description := os.join_path(work, 'recipe.json')
62 run([exe, 'generate', description])!
63 desc = json2.decode[NativeRecipe](os.read_file(description)!)!
64 runner = [exe]
65 } else {
66 desc = native_description(recipe_load(recipe)!)!
67 }
68 spec := desc.spec
69 if spec.pkgnames.len == 0 || spec.pkgnames.any(!filename_part(it)) || spec.pkgrel < 1 {
70 return error('native recipe needs valid package names and a positive pkgrel')
71 }
72 host_arch := os.uname().machine
73 pkg_arch := if host_arch in spec.arch {
74 host_arch
75 } else if 'any' in spec.arch {
76 if os.file_ext(recipe) == '.vsh' && spec.hooks.len > 0 { host_arch } else { 'any' }
77 } else {
78 return error('package does not support ${host_arch}')
79 }
80 mut deps := spec.depends.clone()
81 deps << spec.makedepends
82 deps << spec.checkdepends
83 if deps.len > 0 {
84 mut query := [os.executable(), '--makepkg-query', '-T']
85 query << deps
86 command_output(query)!
87 }
88 for opt in spec.options {
89 if opt !in ['docs', '!strip', '!debug'] {
90 return error('unsupported native package option: ${opt}')
91 }
92 }
93 native_sources(spec, start, src)!
94 original := os.getwd()
95 os.chdir(src)!
96 defer { os.chdir(original) or { eprintln('restore working directory: ${err}') } }
97 os.setenv('VPAC_STARTDIR', start, true)
98 os.setenv('VPAC_SRCDIR', src, true)
99 os.setenv('VPAC_PKGNAME', spec.pkgnames[0], true)
100 os.setenv('VPAC_PKGVER', spec.pkgver, true)
101 os.unsetenv('VPAC_PKGDIR')
102 if 'prepare' in desc.phases { run(phase_args(runner, 'prepare'))! }
103 version := if desc.dynamic {
104 command_output(phase_args(runner, 'pkgver'))!
105 } else {
106 spec.pkgver
107 }
108 if !filename_part(version) || version.contains('-') {
109 return error('invalid pkgver: ${version}')
110 }
111 os.setenv('VPAC_PKGVER', version, true)
112 for phase in ['build', 'check'] {
113 if phase in desc.phases { run(phase_args(runner, phase))! }
114 }
115 hook := if spec.hooks.len == 0 {
116 ''
117 } else if os.file_ext(recipe) == '.vsh' {
118 runner[0]
119 } else {
120 write_recipe_hook(recipe, work, desc.hook_files)!
121 }
122 pkgs := os.join_path(work, 'pkg')
123 os.mkdir(pkgs)!
124 fullver := if spec.epoch > 0 {
125 '${spec.epoch}:${version}-${spec.pkgrel}'
126 } else {
127 '${version}-${spec.pkgrel}'
128 }
129 mut archives := []string{}
130 for name in spec.pkgnames {
131 pkg := os.join_path(pkgs, name)
132 os.mkdir(pkg)!
133 os.setenv('VPAC_PKGDIR', pkg, true)
134 os.setenv('VPAC_PKGNAME', name, true)
135 run(phase_args(runner, 'package'))!
136 archive := os.join_path(work, '${name}-${version}-${spec.pkgrel}-${pkg_arch}.pkg.tar.zst')
137 native_archive(desc, name, fullver, pkg_arch, pkg, archive, hook, recipe)!
138 if sign { run(['gpg', '--batch', '--detach-sign', '--output', archive + '.sig', archive])! }
139 archives << archive
140 }
141 complete = true
142 return archives
143}
144
145fn phase_args(runner []string, phase string) []string {
146 mut args := runner.clone()
147 args << phase
148 return args
149}
150