Stop creating unnecessary Context objects
Previously, the Context object created in the main() method was
initialized (ctx.Register()) for the main soong build. Build modes
that did not want that build modes that did not want that (symlink,
bp2build, apiBp2build) had to create their own context, or in the
case of runSymlinkForestCreation its own EventHandler. That was
very confusing.
This change avoids that by simply pushing the call to ctx.Register()
into the build modes that require it and that allows the other build
modes to use the main context and its EventHandler.
One point to note is that to ensure the exact same initialization as
before the runApiBp2build has to explicitly call the following:
ctx.SetAllowMissingDependencies(false)
That is because the main context could have that set to true.
Bug: 257650737
Test: m nothing
Change-Id: Iee239fb87edf443fed65156fa14b8a30c89a2328
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index 9b25c3d..99f0e3b 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -123,7 +123,6 @@
func newContext(configuration android.Config) *android.Context {
ctx := android.NewContext(configuration)
- ctx.Register()
ctx.SetNameInterface(newNameResolver(configuration))
ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
return ctx
@@ -207,12 +206,11 @@
// Run the code-generation phase to convert API contributions to BUILD files.
// Return marker file for the new synthetic workspace
-func runApiBp2build(configuration android.Config, extraNinjaDeps []string) string {
- // Create a new context and register mutators that are only meaningful to API export
- ctx := android.NewContext(configuration)
+func runApiBp2build(configuration android.Config, ctx *android.Context, extraNinjaDeps []string) string {
ctx.EventHandler.Begin("api_bp2build")
defer ctx.EventHandler.End("api_bp2build")
- ctx.SetNameInterface(newNameResolver(configuration))
+ // Do not allow missing dependencies.
+ ctx.SetAllowMissingDependencies(false)
ctx.RegisterForApiBazelConversion()
// Register the Android.bp files in the tree
@@ -350,18 +348,19 @@
// output file of the specific activity.
func doChosenActivity(ctx *android.Context, configuration android.Config, extraNinjaDeps []string, metricsDir string) string {
if configuration.BuildMode == android.SymlinkForest {
- runSymlinkForestCreation(configuration, extraNinjaDeps, metricsDir)
+ runSymlinkForestCreation(configuration, ctx, extraNinjaDeps, metricsDir)
return symlinkForestMarker
} else if configuration.BuildMode == android.Bp2build {
// Run the alternate pipeline of bp2build mutators and singleton to convert
// Blueprint to BUILD files before everything else.
- runBp2Build(configuration, extraNinjaDeps, metricsDir)
+ runBp2Build(configuration, ctx, extraNinjaDeps, metricsDir)
return bp2buildMarker
} else if configuration.BuildMode == android.ApiBp2build {
- outputFile := runApiBp2build(configuration, extraNinjaDeps)
+ outputFile := runApiBp2build(configuration, ctx, extraNinjaDeps)
writeMetrics(configuration, ctx.EventHandler, metricsDir)
return outputFile
} else {
+ ctx.Register()
var outputFile string
if configuration.IsMixedBuildsEnabled() {
@@ -628,9 +627,7 @@
// Ideally, bp2build would write a file that contains instructions to the
// symlink tree creation binary. Then the latter would not need to depend on
// the very heavy-weight machinery of soong_build .
-func runSymlinkForestCreation(configuration android.Config, extraNinjaDeps []string, metricsDir string) {
- eventHandler := &metrics.EventHandler{}
-
+func runSymlinkForestCreation(configuration android.Config, ctx *android.Context, extraNinjaDeps []string, metricsDir string) {
var ninjaDeps []string
ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
@@ -657,13 +654,13 @@
// Such a directory SHOULD be added to `ninjaDeps` so that a child directory
// or file created/deleted under it would trigger an update of the symlink
// forest.
- eventHandler.Do("symlink_forest", func() {
+ ctx.EventHandler.Do("symlink_forest", func() {
symlinkForestDeps := bp2build.PlantSymlinkForest(
configuration.IsEnvTrue("BP2BUILD_VERBOSE"), topDir, workspaceRoot, generatedRoot, excludes)
ninjaDeps = append(ninjaDeps, symlinkForestDeps...)
})
- writeDepFile(symlinkForestMarker, eventHandler, ninjaDeps)
+ writeDepFile(symlinkForestMarker, ctx.EventHandler, ninjaDeps)
touch(shared.JoinPath(topDir, symlinkForestMarker))
codegenMetrics := bp2build.ReadCodegenMetrics(metricsDir)
if codegenMetrics == nil {
@@ -673,27 +670,22 @@
//TODO (usta) we cannot determine if we loaded a stale file, i.e. from an unrelated prior
//invocation of codegen. We should simply use a separate .pb file
}
- writeBp2BuildMetrics(codegenMetrics, eventHandler, metricsDir)
+ writeBp2BuildMetrics(codegenMetrics, ctx.EventHandler, metricsDir)
}
// Run Soong in the bp2build mode. This creates a standalone context that registers
// an alternate pipeline of mutators and singletons specifically for generating
// Bazel BUILD files instead of Ninja files.
-func runBp2Build(configuration android.Config, extraNinjaDeps []string, metricsDir string) {
+func runBp2Build(configuration android.Config, ctx *android.Context, extraNinjaDeps []string, metricsDir string) {
var codegenMetrics *bp2build.CodegenMetrics
- eventHandler := &metrics.EventHandler{}
- eventHandler.Do("bp2build", func() {
-
- // Register an alternate set of singletons and mutators for bazel
- // conversion for Bazel conversion.
- bp2buildCtx := android.NewContext(configuration)
+ ctx.EventHandler.Do("bp2build", func() {
// Propagate "allow misssing dependencies" bit. This is normally set in
- // newContext(), but we create bp2buildCtx without calling that method.
- bp2buildCtx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
- bp2buildCtx.SetNameInterface(newNameResolver(configuration))
- bp2buildCtx.RegisterForBazelConversion()
- bp2buildCtx.SetModuleListFile(cmdlineArgs.ModuleListFile)
+ // newContext(), but we create ctx without calling that method.
+ ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
+ ctx.SetNameInterface(newNameResolver(configuration))
+ ctx.RegisterForBazelConversion()
+ ctx.SetModuleListFile(cmdlineArgs.ModuleListFile)
var ninjaDeps []string
ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
@@ -701,25 +693,25 @@
// Run the loading and analysis pipeline to prepare the graph of regular
// Modules parsed from Android.bp files, and the BazelTargetModules mapped
// from the regular Modules.
- eventHandler.Do("bootstrap", func() {
+ ctx.EventHandler.Do("bootstrap", func() {
blueprintArgs := cmdlineArgs
- bootstrapDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.StopBeforePrepareBuildActions, bp2buildCtx.Context, configuration)
+ bootstrapDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.StopBeforePrepareBuildActions, ctx.Context, configuration)
ninjaDeps = append(ninjaDeps, bootstrapDeps...)
})
- globListFiles := writeBuildGlobsNinjaFile(bp2buildCtx, configuration.SoongOutDir(), configuration)
+ globListFiles := writeBuildGlobsNinjaFile(ctx, configuration.SoongOutDir(), configuration)
ninjaDeps = append(ninjaDeps, globListFiles...)
// Run the code-generation phase to convert BazelTargetModules to BUILD files
// and print conversion codegenMetrics to the user.
- codegenContext := bp2build.NewCodegenContext(configuration, bp2buildCtx, bp2build.Bp2Build)
- eventHandler.Do("codegen", func() {
+ codegenContext := bp2build.NewCodegenContext(configuration, ctx, bp2build.Bp2Build)
+ ctx.EventHandler.Do("codegen", func() {
codegenMetrics = bp2build.Codegen(codegenContext)
})
ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...)
- writeDepFile(bp2buildMarker, eventHandler, ninjaDeps)
+ writeDepFile(bp2buildMarker, ctx.EventHandler, ninjaDeps)
touch(shared.JoinPath(topDir, bp2buildMarker))
})
@@ -729,7 +721,7 @@
if configuration.IsEnvTrue("BP2BUILD_VERBOSE") {
codegenMetrics.Print()
}
- writeBp2BuildMetrics(codegenMetrics, eventHandler, metricsDir)
+ writeBp2BuildMetrics(codegenMetrics, ctx.EventHandler, metricsDir)
}
// Write Bp2Build metrics into $LOG_DIR