-
Notifications
You must be signed in to change notification settings - Fork 63
/
build_options.go
78 lines (65 loc) · 1.93 KB
/
build_options.go
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file expect in compliance with the License.
package app
import (
"fmt"
"os"
"kraftkit.sh/exec"
"kraftkit.sh/make"
)
type BuildOptions struct {
mopts []make.MakeOption
onProgress func(progress float64)
noPrepare bool
}
type BuildOption func(opts *BuildOptions) error
// WithBuildMakeOptions allows customization of the invocation of the GNU make
// tool
func WithBuildMakeOptions(mopts ...make.MakeOption) BuildOption {
return func(bo *BuildOptions) error {
bo.mopts = append(bo.mopts, mopts...)
return nil
}
}
// WithBuildProgressFunc sets an optional progress function which is used as a
// callback during the ultimate invocation of make within Unikraft's build
// system
func WithBuildProgressFunc(onProgress func(progress float64)) BuildOption {
return func(bo *BuildOptions) error {
bo.onProgress = onProgress
return nil
}
}
type saveBuildLog struct {
file *os.File
}
func (sbl *saveBuildLog) Write(b []byte) (int, error) {
return sbl.file.Write(b)
}
// WithBuildLogFile specifies a path to a file which will be used to save the
// output from Unikraft's build invocation
func WithBuildLogFile(path string) BuildOption {
return func(bo *BuildOptions) error {
if len(path) == 0 {
return nil
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o666)
if err != nil {
return fmt.Errorf("could not create or open build log file: %v", err)
}
bo.mopts = append(bo.mopts, make.WithExecOptions(
exec.WithStdoutCallback(&saveBuildLog{file}),
))
return nil
}
}
// WithBuildNoPrepare disables calling `make prepare` befere invoking the
// main Unikraft's build invocation.
func WithBuildNoPrepare(noPrepare bool) BuildOption {
return func(bo *BuildOptions) error {
bo.noPrepare = noPrepare
return nil
}
}