-
Notifications
You must be signed in to change notification settings - Fork 0
/
comp.go
156 lines (133 loc) · 2.94 KB
/
comp.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
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
)
func fatal(a ...interface{}) {
fmt.Fprintln(os.Stderr, a...)
os.Exit(1)
}
// Returns true if 'path' exists and is not a directory.
func existsFile(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
func execCmd(prog string, args ...string) []byte {
cmd := exec.Command(prog, args...)
log.Println(cmd)
buf := &bytes.Buffer{}
cmd.Stdout = buf
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fatal(err)
}
return buf.Bytes()
}
type CompCommand struct {
Directory string `json:"directory"`
File string `json:"file"`
Command string `json:"command"`
}
func FindCompDb() (string, string, error) {
wd, err := os.Getwd()
if err != nil {
return "", "", err
}
path := wd
name := "compile_commands.json"
for filepath.Dir(path) != path {
if existsFile(filepath.Join(path, name)) {
p, e := filepath.Rel(wd, path)
return name, p, e
}
path = filepath.Dir(path)
}
return "", "", errors.New("could not find compile_commands.json")
}
func main() {
debug := flag.Bool("debug", false, "show debug information")
knit := flag.Bool("knit", false, "run knit to generate the compdb")
flag.Parse()
args := flag.Args()
if *debug {
log.SetOutput(os.Stdout)
log.SetFlags(0)
log.SetPrefix("[debug] ")
} else {
log.SetOutput(io.Discard)
}
if len(args) <= 0 {
fatal("no file to build")
}
var data []byte
var path string
if *knit {
path = filepath.Dir(string(execCmd("knit", "-t", "path")))
data = execCmd("knit", "comp", "-t", "compdb", "all")
} else {
var name string
var err error
name, path, err = FindCompDb()
if err != nil {
fatal(err)
}
compdb := filepath.Join(path, name)
log.Println("compdb:", compdb)
data, err = os.ReadFile(compdb)
if err != nil {
fatal(err)
}
}
var compcmds []CompCommand
err := json.Unmarshal(data, &compcmds)
if err != nil {
fatal(err)
}
wd, _ := os.Getwd()
target, _ := filepath.Rel(filepath.Join(wd, path), filepath.Join(wd, args[0]))
curdir, _ := filepath.Rel(filepath.Join(wd, path), wd)
lock := &sync.Mutex{}
wg := sync.WaitGroup{}
worker := func(cmdchan chan CompCommand) {
defer wg.Done()
for c := range cmdchan {
buf := &bytes.Buffer{}
cmd := exec.Command("sh", "-c", c.Command)
cmd.Dir = filepath.Join(wd, path, c.Directory)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = buf
cmd.Run()
lock.Lock()
fmt.Print(strings.ReplaceAll(buf.String(), curdir+"/", ""))
lock.Unlock()
}
}
cmdchan := make(chan CompCommand)
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go worker(cmdchan)
}
for _, c := range compcmds {
file := filepath.Join(wd, path, c.Directory, c.File)
frel, _ := filepath.Rel(filepath.Join(wd, path), file)
if target == frel {
log.Println(c.Command)
cmdchan <- c
}
}
close(cmdchan)
wg.Wait()
}