aboutsummaryrefslogtreecommitdiffhomepage
path: root/cmd/mbf/main.go
blob: 9828a8f1cb442ce03ca97f561956272aa0f1c926 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"
	"os/signal"
	"path/filepath"
	"runtime"
	"syscall"
	"unique"

	"hakurei.app/command"
	"hakurei.app/container"
	"hakurei.app/container/check"
	"hakurei.app/internal/pkg"
	"hakurei.app/internal/rosa"
	"hakurei.app/message"
)

func main() {
	container.TryArgv0(nil)

	log.SetFlags(0)
	log.SetPrefix("mbf: ")
	msg := message.New(log.Default())

	if os.Geteuid() == 0 {
		log.Fatal("this program must not run as root")
	}

	var cache *pkg.Cache
	ctx, stop := signal.NotifyContext(context.Background(),
		syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
	defer stop()
	defer func() {
		if cache != nil {
			cache.Close()
		}

		if r := recover(); r != nil {
			fmt.Println(r)
			log.Fatal("consider scrubbing the on-disk cache")
		}
	}()

	var (
		flagQuiet  bool
		flagCures  int
		flagBase   string
		flagTShift int
	)
	c := command.New(os.Stderr, log.Printf, "mbf", func([]string) (err error) {
		msg.SwapVerbose(!flagQuiet)

		var base *check.Absolute
		if flagBase, err = filepath.Abs(flagBase); err != nil {
			return
		} else if base, err = check.NewAbs(flagBase); err != nil {
			return
		}
		if cache, err = pkg.Open(ctx, msg, flagCures, base); err == nil {
			if flagTShift < 0 {
				cache.SetThreshold(0)
			} else if flagTShift > 31 {
				cache.SetThreshold(1 << 31)
			} else {
				cache.SetThreshold(1 << flagTShift)
			}
		}
		return
	}).Flag(
		&flagQuiet,
		"q", command.BoolFlag(false),
		"Do not print cure messages",
	).Flag(
		&flagCures,
		"cures", command.IntFlag(0),
		"Maximum number of dependencies to cure at any given time",
	).Flag(
		&flagBase,
		"d", command.StringFlag("cache"),
		"Directory to store cured artifacts",
	).Flag(
		&flagTShift,
		"tshift", command.IntFlag(-1),
		"Dependency graph size exponent, to the power of 2",
	)

	{
		var flagShifts int
		c.NewCommand(
			"scrub", "Examine the on-disk cache for errors",
			func(args []string) error {
				if len(args) > 0 {
					return errors.New("scrub expects no arguments")
				}
				if flagShifts < 0 || flagShifts > 31 {
					flagShifts = 12
				}
				return cache.Scrub(runtime.NumCPU() << flagShifts)
			},
		).Flag(
			&flagShifts,
			"shift", command.IntFlag(12),
			"Scrub parallelism size exponent, to the power of 2",
		)
	}

	{
		var (
			flagGentoo   string
			flagChecksum string

			flagStage0 bool
		)
		c.NewCommand(
			"stage3",
			"Check for toolchain 3-stage non-determinism",
			func(args []string) (err error) {
				std := rosa.Std
				if flagGentoo != "" {
					std -= 3 // magic number to discourage misuse

					var checksum pkg.Checksum
					if len(flagChecksum) != 0 {
						if err = pkg.Decode(&checksum, flagChecksum); err != nil {
							return
						}
					}
					rosa.SetGentooStage3(flagGentoo, checksum)
				}

				_, _, _, stage1 := (std - 2).NewLLVM()
				_, _, _, stage2 := (std - 1).NewLLVM()
				_, _, _, stage3 := std.NewLLVM()
				var (
					pathname *check.Absolute
					checksum [2]unique.Handle[pkg.Checksum]
				)

				if pathname, _, err = cache.Cure(stage1); err != nil {
					return err
				}
				log.Println("stage1:", pathname)

				if pathname, checksum[0], err = cache.Cure(stage2); err != nil {
					return err
				}
				log.Println("stage2:", pathname)
				if pathname, checksum[1], err = cache.Cure(stage3); err != nil {
					return err
				}
				log.Println("stage3:", pathname)

				if checksum[0] != checksum[1] {
					err = &pkg.ChecksumMismatchError{
						Got:  checksum[0].Value(),
						Want: checksum[1].Value(),
					}
				} else {
					log.Println(
						"stage2 is identical to stage3",
						"("+pkg.Encode(checksum[0].Value())+")",
					)
				}

				if flagStage0 {
					if pathname, _, err = cache.Cure(
						std.Load(rosa.Stage0),
					); err != nil {
						return err
					}
					log.Println(pathname)
				}

				return
			},
		).
			Flag(
				&flagGentoo,
				"gentoo", command.StringFlag(""),
				"Bootstrap from a Gentoo stage3 tarball",
			).
			Flag(
				&flagChecksum,
				"checksum", command.StringFlag(""),
				"Checksum of Gentoo stage3 tarball",
			).
			Flag(
				&flagStage0,
				"stage0", command.BoolFlag(false),
				"Create bootstrap stage0 tarball",
			)
	}

	{
		var (
			flagDump string
		)
		c.NewCommand(
			"cure",
			"Cure the named artifact and show its path",
			func(args []string) error {
				if len(args) != 1 {
					return errors.New("cure requires 1 argument")
				}
				if p, ok := rosa.ResolveName(args[0]); !ok {
					return fmt.Errorf("unsupported artifact %q", args[0])
				} else if flagDump == "" {
					pathname, _, err := cache.Cure(rosa.Std.Load(p))
					if err == nil {
						log.Println(pathname)
					}
					return err
				} else {
					f, err := os.OpenFile(
						flagDump,
						os.O_WRONLY|os.O_CREATE|os.O_EXCL,
						0644,
					)
					if err != nil {
						return err
					}

					if err = cache.EncodeAll(f, rosa.Std.Load(p)); err != nil {
						_ = f.Close()
						return err
					}

					return f.Close()
				}
			},
		).
			Flag(
				&flagDump,
				"dump", command.StringFlag(""),
				"Write IR to specified pathname and terminate",
			)
	}

	c.MustParse(os.Args[1:], func(err error) {
		if cache != nil {
			cache.Close()
		}
		log.Fatal(err)
	})
}