-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
198 lines (170 loc) · 5.19 KB
/
graph.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
)
// Graph represents a dependency graph.
type Graph struct {
Nodes []*Node
Sources []*Node
}
func normalizePathList(base string, paths []string) (result []string) {
for _, filename := range paths {
result = append(result, filepath.Clean(filepath.Join(base, filename)))
}
return result
}
func normalizeConfigFile(filename string) (string, error) {
if !filepath.IsAbs(filename) {
abs, err := filepath.Abs(filename)
if err != nil {
return filename, err
}
filename = abs
}
filename = filepath.Clean(filename)
return filename, nil
}
func splitManifestTarget(str string) (manifestFile, targetName string) {
s := strings.Split(str, ":")
if len(s) == 0 {
return manifestFile, targetName
}
manifestFile = strings.Join(s[:len(s)-1], ":")
targetName = s[len(s)-1]
return manifestFile, targetName
}
func parseGraph(manifestFile string) (*Graph, error) {
if len(manifestFile) == 0 {
log.Fatalln("error: Please specify a manifest file.")
}
manifestMap := map[string]*Manifest{}
targetNames := []string{}
nodes := map[string]*Node{}
targets := map[string]Target{}
manifestFiles := []string{manifestFile}
for len(manifestFiles) > 0 {
manifestFile, manifestFiles = manifestFiles[0], manifestFiles[1:]
normalized, err := normalizeConfigFile(manifestFile)
if err != nil {
return nil, err
}
if _, ok := manifestMap[normalized]; ok {
// NOTE: Skip text that are already read.
continue
}
if _, err := os.Stat(manifestFile); os.IsNotExist(err) {
log.Fatalf("error: %s does not exist.", manifestFile)
}
var manifest Manifest
if _, err := toml.DecodeFile(manifestFile, &manifest); err != nil {
return nil, err
}
baseDir := filepath.Dir(manifestFile)
requiredManifests := []string{}
for _, target := range manifest.Targets {
outputType := func() OutputType {
switch target.Type {
case "executable":
return OutputTypeExecutable
case "static_library":
return OutputTypeStaticLibrary
}
if len(target.Type) > 0 {
fmt.Println("warning: Unknown type", target.Type)
}
return OutputTypeUnknown
}()
for _, conf := range target.Configs {
configFile, _ := splitManifestTarget(conf)
if len(configFile) > 0 {
requiredManifests = append(requiredManifests, configFile)
}
}
for _, conf := range target.Dependencies {
configFile, _ := splitManifestTarget(conf)
if len(configFile) > 0 {
requiredManifests = append(requiredManifests, configFile)
}
}
node := &Node{
Name: target.Name,
Type: outputType,
Headers: normalizePathList(baseDir, target.Headers),
Sources: normalizePathList(baseDir, target.Sources),
IncludeDirs: normalizePathList(baseDir, target.IncludeDirs),
LibDirs: normalizePathList(baseDir, target.LibDirs),
Defines: target.Defines,
CompilerFlags: target.CompilerFlags,
CompilerFlagsC: target.CompilerFlagsC,
CompilerFlagsCC: target.CompilerFlagsCC,
LinkerFlags: target.LinkerFlags,
MSBuildSettings: target.MSBuildSettings,
MSBuildProject: target.MSBuildProject,
Templates: target.Templates,
}
node.Tagged = map[string]*Node{}
for tag, tagged := range target.Tagged {
node.Tagged[tag] = &Node{
Headers: normalizePathList(baseDir, tagged.Headers),
Sources: normalizePathList(baseDir, tagged.Sources),
IncludeDirs: normalizePathList(baseDir, tagged.IncludeDirs),
LibDirs: normalizePathList(baseDir, tagged.LibDirs),
Defines: tagged.Defines,
CompilerFlags: tagged.CompilerFlags,
CompilerFlagsC: tagged.CompilerFlagsC,
CompilerFlagsCC: tagged.CompilerFlagsCC,
LinkerFlags: tagged.LinkerFlags,
MSBuildSettings: tagged.MSBuildSettings,
Templates: tagged.Templates,
}
}
targetNames = append(targetNames, target.Name)
nodes[target.Name] = node
targets[target.Name] = target
}
manifestMap[normalized] = &manifest
requiredManifests = normalizePathList(baseDir, requiredManifests)
manifestFiles = append(requiredManifests, manifestFiles...)
}
depNodes := map[string]*Node{}
for _, name := range targetNames {
node := nodes[name]
target := targets[name]
node.Dependencies = make([]*Node, 0, len(target.Dependencies))
for _, v := range target.Dependencies {
_, depName := splitManifestTarget(v)
dep := nodes[depName]
node.Dependencies = append(node.Dependencies, dep)
depNodes[dep.Name] = dep
}
node.Configs = make([]*Node, 0, len(target.Configs))
for _, v := range target.Configs {
_, configName := splitManifestTarget(v)
config := nodes[configName]
node.Configs = append(node.Configs, config)
}
}
sourceNodes := []*Node{}
for name, node := range nodes {
if _, ok := depNodes[name]; ok {
// NOTE: This node is not source.
continue
}
sourceNodes = append(sourceNodes, node)
}
orderedNodes := make([]*Node, 0, len(nodes))
for _, name := range targetNames {
node := nodes[name]
orderedNodes = append(orderedNodes, node)
}
graph := &Graph{
Nodes: orderedNodes,
Sources: sourceNodes,
}
return graph, nil
}