/
standalone.go
270 lines (237 loc) · 9.44 KB
/
standalone.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
/*
* This file is included when Bob is being run as a standalone binary, i.e. for
* the Ninja generator.
*/
package core
import (
"io/ioutil"
"os"
"github.com/google/blueprint"
"github.com/google/blueprint/bootstrap"
"github.com/ARM-software/bob-build/core/backend"
"github.com/ARM-software/bob-build/core/config"
"github.com/ARM-software/bob-build/core/file"
"github.com/ARM-software/bob-build/core/toolchain"
"github.com/ARM-software/bob-build/internal/graph"
"github.com/ARM-software/bob-build/internal/utils"
"github.com/ARM-software/bob-build/internal/warnings"
)
type PropertyProvider interface {
GetProperties() interface{}
}
// configProvider allows the retrieval of configuration
type configProvider interface {
Config() interface{}
}
func projectModuleDir(ctx blueprint.BaseModuleContext) string {
return ctx.ModuleDir()
}
func getConfig(ctx configProvider) *BobConfig {
return ctx.Config().(*BobConfig)
}
func getBuildDir() string {
if bootstrap.BuildDir == "" {
panic("bootstrap.BuildDir was not initialized!")
}
return bootstrap.BuildDir
}
func getSourceDir() string {
// TODO: This should be part of the backend.
return config.GetEnvironmentVariables().SrcDir
}
func getBobDir() string {
// TODO: This should be part of the backend.
return config.GetEnvironmentVariables().BobDir
}
var logger *warnings.WarningLogger = nil
var loggerFile *os.File = nil
func GetLogger() *warnings.WarningLogger {
return logger
}
func SetupLogger(env *config.EnvironmentVariables) {
if env == nil {
// ioutil.Discard has been deprecated since 1.16, but Bob supports as old as 1.11, move to io.Discard
// once ready
logger = warnings.New(ioutil.Discard, "")
} else {
f, err := os.OpenFile(env.LogWarningsFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
utils.Die("Failed to setup logger, error opening '%s' file: %v", env.LogWarningsFile, err)
}
loggerFile = f
logger = warnings.New(loggerFile, env.LogWarnings)
}
}
func TearDownLogger() {
if loggerFile != nil {
errCnt := logger.ErrorWarnings()
loggerFile.Close()
if errCnt > 0 {
utils.Die("%d error(s) ocurred!\n\n%s\n", errCnt, logger.InfoMessage())
}
}
}
// Main is the entry point for the bob primary builder.
//
// It loads the configuration from .bob.config.json, registers the module type
// and mutators, initializes the backend, and finally calls into Blueprint.
func Main() {
// Load the config first. This is needed because some of the module
// types' definitions contain a struct-per-feature, and features are
// specified in the cfg.
cfg := &BobConfig{}
env := config.GetEnvironmentVariables()
err := cfg.Properties.LoadConfig(env.ConfigJSON)
if err != nil {
utils.Die("%v", err)
}
builder_ninja := cfg.Properties.GetBool("builder_ninja")
builder_android_bp := cfg.Properties.GetBool("builder_android_bp")
// Depend on the config file
pctx.AddNinjaFileDeps(env.ConfigJSON, getPathInBuildDir(".env.hash"))
var ctx = blueprint.NewContext()
RegisterModuleTypes(func(name string, mf FactoryWithConfig) {
// Create a closure passing the config to a module factory so
// that the module factories can access the cfg.
factory := func() (blueprint.Module, []interface{}) {
return mf(cfg)
}
ctx.RegisterModuleType(name, factory)
})
// Note that the order of mutators is important, since the
// contents of each module will be rewritten. The following
// describes the required orderring of mutators dealing with
// property propagation.
//
// On reading build.bp, the various properties will be set
// according to the build.bp structure:
//
// .props.propA
// .props.feature1.propA
// .Host.props.propA
// .Host.props.feature1.propA
// .Target.props.propA
// .Target.props.feature1.propA
//
// default.props.propA
// default.props.feature1.propA
// default.Host.props.propA
// default.Host.props.feature1.propA
// default.Target.props.propA
// default.Target.props.feature1.propA
//
// Merge feature-specific values to the level above in each
// module. This must be before defaults so that a feature-specific
// option set in a default does not override an option set in a
// module. Do this before templates so templates only need to
// operate on one level. The properties we care about are then:
//
// .props.propA
// .Host.props.propA
// .Target.props.propA
//
// default.props.propA
// default.Host.props.propA
// default.Target.props.propA
//
// Evaluate templates next, including in defaults. This avoids us
// having to re-evaluate templates after they have been copied
// around by defaults.
//
// The supported_variants mutator runs next. This just propagates the
// host_supported and target_supported properties through the
// defaults, allowing us to identify whether each module supports
// host and target, and split the modules early.
//
// Then split the libraries into host-specific and target-specific
// modules.
//
// After the libraries are split we can apply target-specific
// options, flattening the properties further:
//
// .props.propA
//
// default.props.propA
//
// Finally apply the remaining properties from defaults. This
// leaves the main property structure on each module holding all
// the settings for each property:
//
// .props.propA
//
// The depender mutator adds the dependencies between binaries and libraries.
//
// The generated depender mutator add dependencies to generated source modules.
ctx.RegisterEarlyMutator("register_toolchains", RegisterToolchainModules)
ctx.RegisterBottomUpMutator("default_deps1", DefaultDepsStage1Mutator).Parallel()
ctx.RegisterBottomUpMutator("default_deps2", DefaultDepsStage2Mutator).Parallel()
ctx.RegisterTopDownMutator("features_applier", featureApplierMutator).Parallel()
ctx.RegisterBottomUpMutator("check_lib_fields", checkLibraryFieldsMutator).Parallel()
ctx.RegisterBottomUpMutator("check_genrule_fields", checkGenruleFieldsMutator).Parallel()
ctx.RegisterBottomUpMutator("strip_empty_components", stripEmptyComponentsMutator).Parallel()
ctx.RegisterBottomUpMutator("supported_variants", supportedVariantsMutator).Parallel()
ctx.RegisterBottomUpMutator(splitterMutatorName, splitterMutator).Parallel()
ctx.RegisterTopDownMutator("target", targetMutator).Parallel()
// `pathMutator` has to run before `DefaultApplierMutator`. This is because paths declared in the module
// are relative to their module scope, whereas paths declared in the defaults are not.
ctx.RegisterBottomUpMutator("process_paths", pathMutator).Parallel()
ctx.RegisterBottomUpMutator("default_applier", DefaultApplierMutator).Parallel()
ctx.RegisterBottomUpMutator("depender", dependerMutator).Parallel()
ctx.RegisterBottomUpMutator("generic_depender", ResolveGenericDepsMutator).Parallel()
// First resolve providers which are not dependant on other modules.
ctx.RegisterBottomUpMutator("resolve_files", file.ResolveFilesMutator).Parallel()
// Now we can resolve remaining, dynamic file providers.
ctx.RegisterBottomUpMutator("resolve_dynamic_src_outputs", resolveDynamicFileOutputs) // Cannot be parallel.
ctx.RegisterBottomUpMutator("alias", aliasMutator).Parallel()
ctx.RegisterBottomUpMutator("generated", generatedDependerMutator).Parallel()
ctx.RegisterBottomUpMutator("collect_metadata", metaDataCollector).Parallel()
if handler := initGrapvizHandler(); handler != nil {
ctx.RegisterBottomUpMutator("graphviz_output", handler.graphvizMutator)
// Singleton for stop tool and don't overwrite build.bp
ctx.RegisterSingletonType("quit_singleton", handler.quitSingletonFactory)
} else {
ctx.RegisterTopDownMutator("export_lib_flags", exportLibFlagsMutator).Parallel()
dependencyGraphHandler := graphMutatorHandler{
map[toolchain.TgtType]graph.Graph{
toolchain.TgtTypeHost: graph.NewGraph("All"),
toolchain.TgtTypeTarget: graph.NewGraph("All"),
},
}
ctx.RegisterBottomUpMutator("sort_resolved_static_libs",
dependencyGraphHandler.ResolveDependencySortMutator) // This can't be parallel
ctx.RegisterTopDownMutator("find_required_modules",
findRequiredModulesMutator).Parallel()
ctx.RegisterBottomUpMutator("check_disabled_modules",
checkDisabledMutator).Parallel()
ctx.RegisterTopDownMutator("check_reexport_libs",
checkReexportLibsMutator).Parallel()
ctx.RegisterTopDownMutator("collect_reexport_lib_dependencies",
collectReexportLibsDependenciesMutator).Parallel()
ctx.RegisterBottomUpMutator("apply_reexport_lib_dependencies",
applyReexportLibsDependenciesMutator).Parallel()
ctx.RegisterTopDownMutator("install_group_mutator", installGroupMutator).Parallel()
ctx.RegisterTopDownMutator("debug_info_mutator", debugInfoMutator).Parallel()
if !builder_android_bp {
// The android_bp backend's escape function is a no-op,
// so optimize by skipping the mutator
ctx.RegisterTopDownMutator("escape_mutator", escapeMutator).Parallel()
}
ctx.RegisterTopDownMutator("late_template_mutator", lateTemplateMutator).Parallel()
}
SetupLogger(env)
defer TearDownLogger()
defer MetaDataWriteToFile(env.BuildMetaFile)
if builder_ninja {
cfg.Generator = &linuxGenerator{}
} else if builder_android_bp {
cfg.Generator = &androidBpGenerator{}
// Do not run in parallel to avoid locking issues on the map
ctx.RegisterBottomUpMutator("collect_buildbp", collectBuildBpFilesMutator)
ctx.RegisterSingletonType("androidbp_singleton", androidBpSingletonFactory)
} else {
utils.Die("Unknown builder backend")
}
// It is safe to call `backend.Get()` after this call.
backend.Setup(env, &cfg.Properties)
bootstrap.Main(ctx, cfg)
}