-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.go
135 lines (116 loc) · 3.07 KB
/
build.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
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
// Copyright (c) 2020 The PKT developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)
// generic stuff
func die(format string, args ...interface{}) {
panic(fmt.Sprintf(format, args...))
}
func assertNil(x interface{}, format string, args ...interface{}) {
if x != nil {
die(format, args...)
}
}
type exeF int
const (
exeEcho exeF = 1 << iota
exeCanFail exeF = 1 << iota
exeNoRedirect exeF = 1 << iota
)
func exe(flags exeF, name string, arg ...string) (int, string, string) {
stdout := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
if flags&exeEcho != 0 {
fmt.Println(strings.Join(append([]string{name}, arg...), " "))
}
cmd := exec.Command(name, arg...)
if flags&exeNoRedirect == 0 {
cmd.Stdout = stdout
cmd.Stderr = stderr
} else {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
err := cmd.Run()
ret := 0
if err != nil {
if ee, ok := err.(*exec.ExitError); ok && flags&exeCanFail != 0 {
ret = ee.ExitCode()
} else {
die("exe(%s, %v) -> %v", name, arg, err)
}
}
return ret, stdout.String(), stderr.String()
}
// build stuff
type config struct {
buildargs []string
bindir string
}
func build(name string, pkg string, conf *config) {
if os.Getenv("GOOS") == "windows" {
name = name + ".exe"
}
fmt.Printf("Building %s\n", name)
args := append([]string{"build", "-o", conf.bindir + "/" + name}, conf.buildargs...)
args = append(args, pkg)
exe(exeNoRedirect|exeEcho, "go", args...)
}
func chkdir() {
info, err := os.Stat("./contrib/build/build.go")
if err != nil || info.IsDir() {
die("this script must be invoked from the project root")
}
}
func buildStr() string {
exe(0, "git", "update-index", "-q", "--refresh")
_, id, _ := exe(0, "git", "describe", "--tags", "HEAD")
id = strings.TrimSpace(id)
if x, _, _ := exe(exeCanFail, "git", "diff", "--quiet"); x != 0 {
if os.Getenv("PKT_FAIL_DIRTY") != "" {
die("Build is dirty, aborting")
}
return id + "-dirty"
}
return id
}
func ldflags() string {
return "-X github.com/pkt-cash/PKT-FullNode/pktconfig/version.appBuild=" + buildStr()
}
func test() {
fmt.Println("Running tests")
exe(exeNoRedirect, "go", "test", "-count=1", "-cover", "-parallel=1", "./...", "-tags=dev")
}
var regex = regexp.MustCompile("[A-Z0-9_]+=.*")
func main() {
chkdir()
conf := config{}
conf.bindir = "./bin"
conf.buildargs = append(conf.buildargs, "-trimpath")
conf.buildargs = append(conf.buildargs, "-ldflags="+ldflags())
assertNil(os.MkdirAll(conf.bindir, 0755), "mkdir bin")
for _, a := range os.Args {
if !regex.MatchString(a) {
continue
}
i := strings.IndexRune(a, '=')
fmt.Printf("env %s=%s\n", a[0:i], a[i+1:])
os.Setenv(a[0:i], a[i+1:])
}
build("pktd", ".", &conf)
build("pktctl", "./cmd/pktctl", &conf)
if strings.Contains(strings.Join(os.Args, "|"), "--test") {
test()
} else {
fmt.Println("Pass the --test flag if you want to run the tests as well")
}
fmt.Println("Everything looks good, type `./bin/pktd` to launch the full node.")
}