-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
310 lines (280 loc) · 8.9 KB
/
node.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"fmt"
)
// OutputType specifies the output type of target being defined.
type OutputType int
const (
// OutputTypeUnknown indicates the output type is undefined.
OutputTypeUnknown OutputType = iota
// OutputTypeExecutable indicates the output type is executable.
OutputTypeExecutable
// OutputTypeStaticLibrary indicates the output type is static library.
OutputTypeStaticLibrary
// OutputTypeDynamicLibrary indicates the output type is dynamic library.
OutputTypeDynamicLibrary
)
// Node represents a node in a dependency graph.
type Node struct {
Name string
Type OutputType
Headers []string
Sources []string
IncludeDirs []string
LibDirs []string
Defines []string
CompilerFlags []string
CompilerFlagsC []string
CompilerFlagsCC []string
LinkerFlags []string
MSBuildSettings MSBuildSettings
MSBuildProject MSBuildProject
Templates Templates
Dependencies []*Node
Configs []*Node
Tagged map[string]*Node
}
// GetHeaders gets the paths of the header files.
func (node *Node) GetHeaders(env *Environment) (result []string) {
result = append(result, node.Headers...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.Headers...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetHeaders(env)...)
}
return result
}
// GetSources gets the paths of the source files.
func (node *Node) GetSources(env *Environment) (result []string) {
result = append(result, node.Sources...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.Sources...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetSources(env)...)
}
return result
}
// GetIncludeDirs gets the directories referred to as the header/include search paths.
func (node *Node) GetIncludeDirs(env *Environment) (result []string) {
result = append(result, node.IncludeDirs...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.IncludeDirs...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetIncludeDirs(env)...)
}
return result
}
// GetLibDirs gets the directories referred to as the library search paths.
func (node *Node) GetLibDirs(env *Environment) (result []string) {
result = append(result, node.LibDirs...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.LibDirs...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetLibDirs(env)...)
}
return result
}
// GetDefines gets a set of the preprocessor macros defined.
func (node *Node) GetDefines(env *Environment) (result []string) {
result = append(result, node.Defines...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.Defines...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetDefines(env)...)
}
return result
}
// GetCompilerFlags gets a set of the compiler flags.
func (node *Node) GetCompilerFlags(env *Environment) (result []string) {
result = append(result, node.CompilerFlags...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.CompilerFlags...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetCompilerFlags(env)...)
}
return result
}
// GetCompilerFlagsC gets a set of the C compiler flags.
func (node *Node) GetCompilerFlagsC(env *Environment) (result []string) {
result = append(result, node.CompilerFlagsC...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.CompilerFlagsC...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetCompilerFlagsC(env)...)
}
return result
}
// GetCompilerFlagsCC gets a set of the C++ compiler flags.
func (node *Node) GetCompilerFlagsCC(env *Environment) (result []string) {
result = append(result, node.CompilerFlagsCC...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.CompilerFlagsCC...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetCompilerFlagsCC(env)...)
}
return result
}
// GetLinkerFlags gets a set of the linker flags.
func (node *Node) GetLinkerFlags(env *Environment) (result []string) {
result = append(result, node.LinkerFlags...)
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
result = append(result, tagged.LinkerFlags...)
}
}
for _, c := range node.Configs {
result = append(result, c.GetLinkerFlags(env)...)
}
return result
}
func copyMSBuildProjectConfiguration(dst, src *MSBuildProjectConfiguration) {
dst.Configuration = src.Configuration
dst.Platform = src.Platform
dst.ExecutableExtension = src.ExecutableExtension
dst.StaticLibraryExtension = src.StaticLibraryExtension
dst.DynamicLibraryExtension = src.DynamicLibraryExtension
dst.Tags = make([]string, len(src.Tags))
copy(dst.Tags, src.Tags)
}
// GetMSBuildProject gets configuration details of MSBuild.
func (node *Node) GetMSBuildProject(env *Environment) MSBuildProject {
result := MSBuildProject{}
for _, v := range node.MSBuildProject.Configurations {
c := MSBuildProjectConfiguration{}
copyMSBuildProjectConfiguration(&c, &v)
result.Configurations = append(result.Configurations, c)
}
result.ExtensionSettings = append(result.ExtensionSettings, node.MSBuildProject.ExtensionSettings...)
result.ExtensionTargets = append(result.ExtensionTargets, node.MSBuildProject.ExtensionTargets...)
configs := map[string]*MSBuildProjectConfiguration{}
for _, v := range result.Configurations {
key := fmt.Sprintln(v.Configuration, "|", v.Platform)
configs[key] = &v
}
for _, c := range node.Configs {
other := c.GetMSBuildProject(env)
for _, v := range other.Configurations {
key := fmt.Sprintln(v.Configuration, "|", v.Platform)
if conf, ok := configs[key]; ok {
conf.Tags = append(conf.Tags, v.Tags...)
} else {
c := MSBuildProjectConfiguration{}
copyMSBuildProjectConfiguration(&c, &v)
result.Configurations = append(result.Configurations, c)
}
}
result.ExtensionSettings = append(result.ExtensionSettings, other.ExtensionSettings...)
result.ExtensionTargets = append(result.ExtensionTargets, other.ExtensionTargets...)
}
return result
}
func mergeMSBuildSettingsMap(a, b *map[string]string) {
if (*b) == nil {
return
}
for k, v := range *b {
if _, ok := (*a)[k]; !ok {
(*a)[k] = v
}
}
}
func mergeMSBuildSettings(a, b *MSBuildSettings) {
mergeMSBuildSettingsMap(&a.ClCompile, &b.ClCompile)
mergeMSBuildSettingsMap(&a.Link, &b.Link)
mergeMSBuildSettingsMap(&a.Lib, &b.Lib)
mergeMSBuildSettingsMap(&a.Globals, &b.Globals)
mergeMSBuildSettingsMap(&a.Configuration, &b.Configuration)
mergeMSBuildSettingsMap(&a.User, &b.User)
mergeMSBuildSettingsMap(&a.General, &b.General)
}
func copyStringMap(s map[string]string) map[string]string {
result := make(map[string]string)
for k, v := range s {
result[k] = v
}
return result
}
func copyMSBuildSettings(dst, src *MSBuildSettings) {
dst.ClCompile = copyStringMap(src.ClCompile)
dst.Link = copyStringMap(src.Link)
dst.Lib = copyStringMap(src.Lib)
dst.Globals = copyStringMap(src.Globals)
dst.User = copyStringMap(src.User)
dst.General = copyStringMap(src.General)
dst.Configuration = copyStringMap(src.Configuration)
}
// GetMSBuildSettings gets configuration settings of MSBuild.
func (node *Node) GetMSBuildSettings(env *Environment) MSBuildSettings {
result := MSBuildSettings{}
copyMSBuildSettings(&result, &node.MSBuildSettings)
for _, tag := range env.Tags {
if tagged, ok := node.Tagged[tag]; ok && tagged != nil {
mergeMSBuildSettings(&result, &tagged.MSBuildSettings)
}
}
for _, c := range node.Configs {
other := c.GetMSBuildSettings(env)
mergeMSBuildSettings(&result, &other)
}
return result
}
// GetTemplates gets a set of the template files.
func (node *Node) GetTemplates(env *Environment) Templates {
result := Templates{
MSBuildProject: node.Templates.MSBuildProject,
MSBuildSolution: node.Templates.MSBuildSolution,
Ninja: node.Templates.Ninja,
XcodeProject: node.Templates.XcodeProject,
}
copyIfEmpty := func(dst, src *Templates) {
type t struct {
dst *string
src string
}
pairs := []t{
t{dst: &dst.MSBuildProject, src: src.MSBuildProject},
t{dst: &dst.MSBuildSolution, src: src.MSBuildSolution},
t{dst: &dst.Ninja, src: src.Ninja},
t{dst: &dst.XcodeProject, src: src.XcodeProject},
}
for _, p := range pairs {
if len(*p.dst) == 0 {
*p.dst = p.src
}
}
}
for _, tag := range env.Tags {
if tagged := node.Tagged[tag]; tagged != nil {
copyIfEmpty(&result, &tagged.Templates)
}
}
for _, c := range node.Configs {
temp := c.GetTemplates(env)
copyIfEmpty(&result, &temp)
}
return result
}