-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.go
executable file
·149 lines (126 loc) · 2.45 KB
/
make.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package vspb
import (
"fmt"
"os"
"os/exec"
"strings"
)
const (
MAKE_TOOL_MESON = iota + 1
MAKE_TOOL_CMAKE
MAKE_TOOL_AUTOMAKE
MAKE_TOOL_AUTOMAKE_GEN
)
func MatchMakeTool(dir string) (int, error) {
files, err := os.ReadDir(dir)
if err != nil {
return -1, err
}
builder := 7
for _, file := range files {
switch file.Name() {
case "meson.build":
if builder > MAKE_TOOL_MESON {
builder = MAKE_TOOL_MESON
}
case "CMakeLists.txt":
if builder > MAKE_TOOL_CMAKE {
builder = MAKE_TOOL_CMAKE
}
case "configure":
if builder > MAKE_TOOL_AUTOMAKE {
builder = MAKE_TOOL_AUTOMAKE
}
case "autogen.sh":
if builder > MAKE_TOOL_AUTOMAKE_GEN {
builder = MAKE_TOOL_AUTOMAKE_GEN
}
default:
continue
}
}
return builder, nil
}
type CmdRunner struct {
Env map[string]string
Dir string
Cmds []string
Verbose bool
}
func (c *CmdRunner) SetEnv(key, value string) {
c.Env[key] = value
}
func (cr *CmdRunner) Run() error {
if len(cr.Cmds) == 0 {
return fmt.Errorf("empty cmds")
}
// TODO: choose a shell
shell := "bash"
fmt.Println("exec cmds: ")
for _, cmd := range cr.Cmds {
fmt.Printf(" %s", cmd)
// check if a command is a cd command
// if it is, change the dirs
if strings.HasPrefix(cmd, "cd") {
args := strings.SplitN(cmd, " ", 2)
if len(args) != 2 {
return fmt.Errorf("'cd' error: '%s'", cmd)
}
cr.Dir = cr.Dir + "/" + args[1]
continue
}
c := exec.Command(shell, "-c", cmd)
c.Env = os.Environ()
for key, value := range cr.Env {
c.Env = append(c.Env, fmt.Sprintf("%s=%s", key, value))
}
c.Dir = cr.Dir
if cr.Verbose {
fmt.Println(":")
c.Stdout = os.Stdout
// TODO:
// maybe should collect stderr even it is not verbose
c.Stderr = os.Stderr
}
if err := c.Run(); err != nil {
return fmt.Errorf("Exec error: %w", err)
}
if !cr.Verbose {
fmt.Println(" -OK")
}
}
return nil
}
func DefaultMaker(maketool int) []string {
cmds := make([]string, 0)
if maketool == MAKE_TOOL_MESON {
cmds = []string{
"meson setup build",
"ninja -C build",
}
}
if maketool == MAKE_TOOL_CMAKE {
cmds = []string{
"cmake -S . -B build",
"cmake --build build",
}
}
if maketool == MAKE_TOOL_AUTOMAKE {
cmds = []string{
"mkdir build",
"cd build",
"../configure",
"make",
}
}
if maketool == MAKE_TOOL_AUTOMAKE_GEN {
cmds = []string{
"./autogen.sh",
"mkdir build",
"cd build",
"../configure",
"make",
}
}
return cmds
}