-
Notifications
You must be signed in to change notification settings - Fork 0
/
ninja.go
227 lines (201 loc) · 5.6 KB
/
ninja.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
package main
import (
"bufio"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// NinjaGenerator generates a ninja file.
type NinjaGenerator struct {
Variables []string
Rules []*NinjaRule
Nodes []*NinjaBuild
}
// AddRule adds the new rule to the ninja definition.
func (gen *NinjaGenerator) AddRule(rule *NinjaRule) {
gen.Rules = append(gen.Rules, rule)
}
// AddNode adds the build node to the ninja graph.
func (gen *NinjaGenerator) AddNode(node *NinjaBuild) {
gen.Nodes = append(gen.Nodes, node)
}
// AddVariable adds the new variable to the ninja definition.
func (gen *NinjaGenerator) AddVariable(key, value string) {
gen.Variables = append(gen.Variables, key+" = "+value)
}
func joinNinjaOptions(prefix string, options []string) string {
str := ""
for i, d := range options {
if i > 0 {
str += " "
}
str += prefix
str += d
}
return str
}
func compileSources(env *Environment, node *Node, generator *NinjaGenerator) (objFiles []string) {
sources := node.GetSources(env)
includeDirs := node.GetIncludeDirs(env)
defines := node.GetDefines(env)
cflags := node.GetCompilerFlags(env)
cflagsC := node.GetCompilerFlagsC(env)
cflagsCC := node.GetCompilerFlagsCC(env)
for _, source := range sources {
sourceFileType := getSourceFileType(source)
obj := filepath.Clean(filepath.Join(env.OutDir, "obj", source+".o"))
objFiles = append(objFiles, obj)
variables := map[string]string{}
if len(includeDirs) > 0 {
variables["include_dirs"] = joinNinjaOptions("-I", includeDirs)
}
if len(defines) > 0 {
variables["defines"] = joinNinjaOptions("-D", defines)
}
variables["cflags"] = strings.Join(cflags, " ")
var compileRule string
switch sourceFileType {
case SourceFileTypeCSource:
compileRule = "compile_c"
variables["cflags_c"] = strings.Join(cflagsC, " ")
case SourceFileTypeCppSource:
compileRule = "compile"
variables["cflags_cc"] = strings.Join(cflagsCC, " ")
default:
continue
}
generator.AddNode(&NinjaBuild{
Rule: compileRule,
Inputs: []string{source},
Outputs: []string{obj},
Variables: variables,
})
}
return objFiles
}
// Generate generates the ninja definitions from graph contains the intermediate nodes.
func (gen *NinjaGenerator) Generate(env *Environment, graph *Graph) {
// $cxx -MMD -MF $out.d $defines $includes $cflags $cflags_cc
gen.AddRule(&NinjaRule{
Name: "compile_c",
Command: "clang -MMD -MF $out.d $defines $include_dirs $cflags $cflags_c -c $in -o $out",
Deps: "gcc",
DepFile: "$out.d",
})
gen.AddRule(&NinjaRule{
Name: "compile",
Command: "clang++ -MMD -MF $out.d $defines $include_dirs $cflags $cflags_cc -c $in -o $out",
Deps: "gcc",
DepFile: "$out.d",
})
gen.AddRule(&NinjaRule{
Name: "link",
Command: "ld $in $ldflags -o $out",
})
gen.AddRule(&NinjaRule{
Name: "archive",
Command: "ar -rc $out $in",
})
gen.AddRule(&NinjaRule{
Name: "archive-static-libs",
Command: "libtool -static -o $out $in",
})
for _, node := range graph.Nodes {
switch node.Type {
case OutputTypeExecutable:
objFiles := compileSources(env, node, gen)
libraryFiles := []string{}
ldflags := []string{
"-L" + filepath.Join(env.OutDir, "bin"),
}
for _, f := range node.GetLinkerFlags(env) {
ldflags = append(ldflags, f)
}
for _, dir := range node.GetLibDirs(env) {
ldflags = append(ldflags, "-L"+dir)
}
for _, dep := range node.Dependencies {
switch dep.Type {
case OutputTypeStaticLibrary:
lib := filepath.Join(env.OutDir, "bin", "lib"+dep.Name+".a")
libraryFiles = append(libraryFiles, lib)
ldflags = append(ldflags, "-l"+dep.Name)
}
}
executableFile := filepath.Join(env.OutDir, "bin", node.Name)
gen.AddNode(&NinjaBuild{
Rule: "link",
Inputs: objFiles,
ImplicitDeps: libraryFiles,
Outputs: []string{executableFile},
Variables: map[string]string{
"ldflags": strings.Join(ldflags, " "),
},
})
case OutputTypeStaticLibrary:
objFiles := compileSources(env, node, gen)
libraryFiles := []string{}
for _, dep := range node.Dependencies {
switch dep.Type {
case OutputTypeStaticLibrary:
lib := filepath.Join(env.OutDir, "bin", "lib"+dep.Name+".a")
libraryFiles = append(libraryFiles, lib)
}
}
libFile := filepath.Join(env.OutDir, "bin", "lib"+node.Name+".a")
gen.AddNode(&NinjaBuild{
Rule: "archive-static-libs",
Inputs: append(objFiles, libraryFiles...),
Outputs: []string{libFile},
})
}
}
}
// WriteFile writes the ninja defintions to the specified file.
func (gen *NinjaGenerator) WriteFile(ninjaFile string) error {
dir := filepath.Dir(ninjaFile)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return errors.Wrapf(err, "Failed to create output directory \"%s\"", dir)
}
}
file, err := os.Create(ninjaFile)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for _, v := range gen.Variables {
if _, err := writer.WriteString(v + "\n"); err != nil {
return err
}
}
if len(gen.Variables) > 0 {
if _, err := writer.WriteString("\n"); err != nil {
return err
}
}
for i, rule := range gen.Rules {
if i > 0 {
if _, err := writer.WriteString("\n"); err != nil {
return err
}
}
if _, err := writer.WriteString(rule.ToString()); err != nil {
return err
}
}
if len(gen.Rules) > 0 {
if _, err := writer.WriteString("\n"); err != nil {
return err
}
}
for _, node := range gen.Nodes {
if _, err := writer.WriteString(node.ToString()); err != nil {
return err
}
}
writer.Flush()
return nil
}