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
|
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bootstrap
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"runtime/pprof"
"runtime/trace"
"github.com/google/blueprint"
)
type Args struct {
ModuleListFile string
OutFile string
EmptyNinjaFile bool
NoGC bool
Cpuprofile string
Memprofile string
TraceFile string
}
// RegisterGoModuleTypes adds module types to build tools written in golang
func RegisterGoModuleTypes(ctx *blueprint.Context) {
ctx.RegisterModuleType("bootstrap_go_package", newGoPackageModuleFactory())
ctx.RegisterModuleType("blueprint_go_binary", newGoBinaryModuleFactory())
}
// RunBlueprint emits `args.OutFile` (a Ninja file) and returns the list of
// its dependencies. These can be written to a `${args.OutFile}.d` file
// so that it is correctly rebuilt when needed in case Blueprint is itself
// invoked from Ninja
func RunBlueprint(args Args, stopBefore StopBefore, ctx *blueprint.Context, config interface{}) ([]string, error) {
runtime.GOMAXPROCS(runtime.NumCPU())
if args.NoGC {
debug.SetGCPercent(-1)
}
if args.Cpuprofile != "" {
f, err := os.Create(joinPath(ctx.SrcDir(), args.Cpuprofile))
if err != nil {
return nil, fmt.Errorf("error opening cpuprofile: %s", err)
}
pprof.StartCPUProfile(f)
defer f.Close()
defer pprof.StopCPUProfile()
}
if args.TraceFile != "" {
f, err := os.Create(joinPath(ctx.SrcDir(), args.TraceFile))
if err != nil {
return nil, fmt.Errorf("error opening trace: %s", err)
}
trace.Start(f)
defer f.Close()
defer trace.Stop()
}
if args.ModuleListFile == "" {
return nil, fmt.Errorf("-l <moduleListFile> is required and must be nonempty")
}
ctx.SetModuleListFile(args.ModuleListFile)
var ninjaDeps []string
ninjaDeps = append(ninjaDeps, args.ModuleListFile)
ctx.BeginEvent("list_modules")
var filesToParse []string
if f, err := ctx.ListModulePaths("."); err != nil {
return nil, fmt.Errorf("could not enumerate files: %v\n", err.Error())
} else {
filesToParse = f
}
ctx.EndEvent("list_modules")
ctx.RegisterBottomUpMutator("bootstrap_plugin_deps", pluginDeps)
ctx.RegisterSingletonType("bootstrap", newSingletonFactory(), false)
RegisterGoModuleTypes(ctx)
blueprint.RegisterPackageIncludesModuleType(ctx)
ctx.BeginEvent("parse_bp")
if blueprintFiles, errs := ctx.ParseFileList(".", filesToParse, config); len(errs) > 0 {
return nil, fatalErrors(errs)
} else {
ctx.EndEvent("parse_bp")
ninjaDeps = append(ninjaDeps, blueprintFiles...)
}
if resolvedDeps, errs := ctx.ResolveDependencies(config); len(errs) > 0 {
return nil, fatalErrors(errs)
} else {
ninjaDeps = append(ninjaDeps, resolvedDeps...)
}
if stopBefore == StopBeforePrepareBuildActions {
return ninjaDeps, nil
}
if ctx.BeforePrepareBuildActionsHook != nil {
if err := ctx.BeforePrepareBuildActionsHook(); err != nil {
return nil, fatalErrors([]error{err})
}
}
if buildActionsDeps, errs := ctx.PrepareBuildActions(config); len(errs) > 0 {
return nil, fatalErrors(errs)
} else {
ninjaDeps = append(ninjaDeps, buildActionsDeps...)
}
if stopBefore == StopBeforeWriteNinja {
return ninjaDeps, nil
}
const outFilePermissions = 0666
var out blueprint.StringWriterWriter
var f *os.File
var buf *bufio.Writer
ctx.BeginEvent("write_files")
defer ctx.EndEvent("write_files")
if args.EmptyNinjaFile {
if err := os.WriteFile(joinPath(ctx.SrcDir(), args.OutFile), []byte(nil), outFilePermissions); err != nil {
return nil, fmt.Errorf("error writing empty Ninja file: %s", err)
}
out = io.Discard.(blueprint.StringWriterWriter)
} else {
f, err := os.OpenFile(joinPath(ctx.SrcDir(), args.OutFile), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, outFilePermissions)
if err != nil {
return nil, fmt.Errorf("error opening Ninja file: %s", err)
}
defer f.Close()
buf = bufio.NewWriterSize(f, 16*1024*1024)
out = buf
}
if err := ctx.WriteBuildFile(out); err != nil {
return nil, fmt.Errorf("error writing Ninja file contents: %s", err)
}
if buf != nil {
if err := buf.Flush(); err != nil {
return nil, fmt.Errorf("error flushing Ninja file contents: %s", err)
}
}
if f != nil {
if err := f.Close(); err != nil {
return nil, fmt.Errorf("error closing Ninja file: %s", err)
}
}
if args.Memprofile != "" {
f, err := os.Create(joinPath(ctx.SrcDir(), args.Memprofile))
if err != nil {
return nil, fmt.Errorf("error opening memprofile: %s", err)
}
defer f.Close()
pprof.WriteHeapProfile(f)
}
return ninjaDeps, nil
}
func fatalErrors(errs []error) error {
red := "\x1b[31m"
unred := "\x1b[0m"
for _, err := range errs {
switch err := err.(type) {
case *blueprint.BlueprintError,
*blueprint.ModuleError,
*blueprint.PropertyError:
fmt.Printf("%serror:%s %s\n", red, unred, err.Error())
default:
fmt.Printf("%sinternal error:%s %s\n", red, unred, err)
}
}
return errors.New("fatal errors encountered")
}
func joinPath(base, path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(base, path)
}
|