-
Notifications
You must be signed in to change notification settings - Fork 4
/
crun.go
216 lines (194 loc) · 4.39 KB
/
crun.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
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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
)
var (
buildOnly = flag.Bool("b", false, "build only")
useDebugger = flag.Bool("d", false, "run in debugger")
outputFile = flag.String("o", "", "output file")
format = flag.Bool("f", false, "format source file")
pkgcfg = flag.String("p", "", "use pkg-config for package flags")
exflags = flag.String("x", "", "pass extra flags")
verbose = flag.Bool("v", false, "be verbose")
optimize = flag.Bool("z", false, "turn optimization on")
stdc = flag.String("stdc", "c2x", "standard c version")
stdcpp = flag.String("stdcpp", "c++20", "standard cpp version")
isCpp = false
)
func main() {
log.SetFlags(0)
log.SetPrefix("crun: ")
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
usage()
}
files, args := splitArgs(flag.Args())
if len(files) == 0 {
log.Fatal("no C source file specified")
}
crun(files, args)
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: [options] file ... [args]")
flag.PrintDefaults()
os.Exit(2)
}
func splitArgs(args []string) ([]string, []string) {
for i := range args {
arg := strings.ToLower(args[i])
if strings.HasSuffix(arg, ".cpp") || strings.HasSuffix(arg, ".cc") || strings.HasSuffix(arg, ".cxx") {
isCpp = true
}
if !strings.HasSuffix(arg, ".c") && !isCpp {
return args[:i], args[i:]
}
}
return args, nil
}
func crun(files, args []string) {
cc := os.Getenv("CC")
if cc == "" {
cc = "cc"
}
if isCpp {
cc = os.Getenv("CPP")
if cc == "" {
cc = "c++"
}
}
sharedFlags := "-Wall -Wextra -pedantic -ggdb -g3 -lm -lpthread -lrt -ldl"
if *optimize {
sharedFlags += " -march=native -O2"
}
if runtime.GOOS == "linux" && (runtime.GOARCH == "amd64" || runtime.GOARCH == "386") && !*useDebugger && !*optimize {
sharedFlags += " -fsanitize=address"
}
*pkgcfg += os.Getenv("CRUN_PKG_CONFIG")
if *pkgcfg != "" {
pw := new(bytes.Buffer)
cmd := exec.Command("pkg-config", "--cflags", "--libs", *pkgcfg)
cmd.Stderr = pw
cmd.Stdout = pw
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", pw.Bytes())
log.Fatal(err)
}
sharedFlags += " " + strings.TrimSpace(pw.String())
}
cflags := os.Getenv("CFLAGS")
if cflags == "" {
cflags = fmt.Sprintf("-std=%v %v", *stdc, sharedFlags)
}
if isCpp {
cflags = os.Getenv("CPPFLAGS")
if cflags == "" {
cflags = fmt.Sprintf("-std=%v %v", *stdcpp, sharedFlags)
}
}
binName := tempName(files)
wd, _ := os.Getwd()
if *format {
var fargs []string
fargs = append(fargs, "-i")
fargs = append(fargs, files...)
cmd := exec.Command("clang-format", fargs...)
cmd.Dir = wd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
if *outputFile != "" {
binName = *outputFile
*buildOnly = true
}
var cargs []string
cargs = append(cargs, "-o")
cargs = append(cargs, binName)
cargs = append(cargs, files...)
cargs = append(cargs, strings.Split(cflags, " ")...)
if *exflags != "" {
cargs = append(cargs, *exflags)
}
if *verbose {
fmt.Printf("%s %v\n", cc, cargs)
}
cmd := exec.Command(cc, cargs...)
cmd.Dir = wd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
if *buildOnly {
return
}
donech := make(chan error)
sigch := make(chan os.Signal)
signal.Notify(sigch)
exePath := filepath.Join(wd, binName)
exe := filepath.Join(exePath)
if *useDebugger {
dbg := os.Getenv("CRUNDBG")
if dbg == "" {
dbg = "gdb --args"
}
toks := strings.Split(dbg, " ")
dbgargs := append(toks[1:], []string{exe}...)
dbgargs = append(dbgargs, args...)
args = dbgargs
exe = toks[0]
}
cmd = exec.Command(exe, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
go func() {
donech <- cmd.Run()
}()
loop:
for {
select {
case sig := <-sigch:
if sig != syscall.SIGCHLD && sig != syscall.SIGURG {
fmt.Printf("signal: %v\n", sig)
break loop
}
case err = <-donech:
break loop
}
}
os.Remove(exePath)
if err != nil {
log.Fatal(err)
}
}
func tempName(files []string) string {
name := "crun"
if len(files) > 0 {
name = stripExt(files[0])
}
return fmt.Sprintf("%s-%d", name, rand.Int())
}
func stripExt(str string) string {
ext := filepath.Ext(str)
return str[:len(str)-len(ext)]
}