-
Notifications
You must be signed in to change notification settings - Fork 0
/
deps.go
94 lines (84 loc) · 2.35 KB
/
deps.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
// MaxDepth Max depth in search tree
const MaxDepth = 2
func findIncludes(file string, treated *map[string]bool, depth int) {
var myList []string
file1 := file
f, err := os.Open(file1)
if err != nil {
file1 = strings.TrimPrefix(file, "src/")
for _, pref := range []string{"/usr/local/include/", "usr/include/"} {
f, err = os.Open(pref + file1)
if err == nil {
file1 = pref + file1
if !(*treated)[file1] {
(*treated)[file1] = true
}
break
}
}
} else {
(*treated)[file1] = true
}
defer f.Close()
depth++
if depth > MaxDepth {
return
}
scanner := bufio.NewScanner(f)
r, _ := regexp.Compile("^#\\s*include\\s*([<\"])(.*)[>\"]")
for scanner.Scan() {
line := scanner.Text()
match := r.FindStringSubmatch(line)
if len(match) > 0 {
/* match[0] is the global match, match[1] is '<' or '"' and match[2] is the file to include */
if match[1] == "\"" {
fmt.Printf(" \"%s\" -> \"%s\";\n", file1, "src/"+match[2])
myList = append(myList, "src/"+match[2])
} else {
fmt.Printf(" \"%s\" -> \"%s\";\n", file1, match[2])
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
for _, file2 := range myList {
if !(*treated)[file2] {
findIncludes(file2, treated, depth)
}
}
}
func main() {
args := os.Args[1:]
var fileList []string
if len(args) == 0 {
for _, searchDir := range []string{"src"} {
filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
if strings.HasSuffix(path, ".c") || strings.HasSuffix(path, ".h") {
fileList = append(fileList, path)
}
return nil
})
}
} else {
fileList = append(fileList, args[0])
}
fmt.Println("digraph graphname {")
var treated = make(map[string]bool)
for _, file := range fileList {
if !treated[file] {
findIncludes(file, &treated, 0)
}
}
fmt.Println("}")
}