Merge "Stop creating unnecessary Context objects"

This commit is contained in:
Paul Duffin
2022-11-14 22:30:53 +00:00
committed by Gerrit Code Review

View File

@@ -111,7 +111,6 @@ func newNameResolver(config android.Config) *android.NameResolver {
func newContext(configuration android.Config) *android.Context { func newContext(configuration android.Config) *android.Context {
ctx := android.NewContext(configuration) ctx := android.NewContext(configuration)
ctx.Register()
ctx.SetNameInterface(newNameResolver(configuration)) ctx.SetNameInterface(newNameResolver(configuration))
ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
return ctx return ctx
@@ -195,12 +194,11 @@ func runQueryView(queryviewDir, queryviewMarker string, configuration android.Co
// Run the code-generation phase to convert API contributions to BUILD files. // Run the code-generation phase to convert API contributions to BUILD files.
// Return marker file for the new synthetic workspace // Return marker file for the new synthetic workspace
func runApiBp2build(configuration android.Config, extraNinjaDeps []string) string { func runApiBp2build(configuration android.Config, ctx *android.Context, extraNinjaDeps []string) string {
// Create a new context and register mutators that are only meaningful to API export
ctx := android.NewContext(configuration)
ctx.EventHandler.Begin("api_bp2build") ctx.EventHandler.Begin("api_bp2build")
defer ctx.EventHandler.End("api_bp2build") defer ctx.EventHandler.End("api_bp2build")
ctx.SetNameInterface(newNameResolver(configuration)) // Do not allow missing dependencies.
ctx.SetAllowMissingDependencies(false)
ctx.RegisterForApiBazelConversion() ctx.RegisterForApiBazelConversion()
// Register the Android.bp files in the tree // Register the Android.bp files in the tree
@@ -338,18 +336,19 @@ func writeDepFile(outputFile string, eventHandler *metrics.EventHandler, ninjaDe
// output file of the specific activity. // output file of the specific activity.
func doChosenActivity(ctx *android.Context, configuration android.Config, extraNinjaDeps []string, metricsDir string) string { func doChosenActivity(ctx *android.Context, configuration android.Config, extraNinjaDeps []string, metricsDir string) string {
if configuration.BuildMode == android.SymlinkForest { if configuration.BuildMode == android.SymlinkForest {
runSymlinkForestCreation(configuration, extraNinjaDeps, metricsDir) runSymlinkForestCreation(configuration, ctx, extraNinjaDeps, metricsDir)
return symlinkForestMarker return symlinkForestMarker
} else if configuration.BuildMode == android.Bp2build { } else if configuration.BuildMode == android.Bp2build {
// Run the alternate pipeline of bp2build mutators and singleton to convert // Run the alternate pipeline of bp2build mutators and singleton to convert
// Blueprint to BUILD files before everything else. // Blueprint to BUILD files before everything else.
runBp2Build(configuration, extraNinjaDeps, metricsDir) runBp2Build(configuration, ctx, extraNinjaDeps, metricsDir)
return bp2buildMarker return bp2buildMarker
} else if configuration.BuildMode == android.ApiBp2build { } else if configuration.BuildMode == android.ApiBp2build {
outputFile := runApiBp2build(configuration, extraNinjaDeps) outputFile := runApiBp2build(configuration, ctx, extraNinjaDeps)
writeMetrics(configuration, ctx.EventHandler, metricsDir) writeMetrics(configuration, ctx.EventHandler, metricsDir)
return outputFile return outputFile
} else { } else {
ctx.Register()
var outputFile string var outputFile string
if configuration.IsMixedBuildsEnabled() { if configuration.IsMixedBuildsEnabled() {
@@ -616,9 +615,7 @@ func bazelArtifacts() []string {
// Ideally, bp2build would write a file that contains instructions to the // Ideally, bp2build would write a file that contains instructions to the
// symlink tree creation binary. Then the latter would not need to depend on // symlink tree creation binary. Then the latter would not need to depend on
// the very heavy-weight machinery of soong_build . // the very heavy-weight machinery of soong_build .
func runSymlinkForestCreation(configuration android.Config, extraNinjaDeps []string, metricsDir string) { func runSymlinkForestCreation(configuration android.Config, ctx *android.Context, extraNinjaDeps []string, metricsDir string) {
eventHandler := &metrics.EventHandler{}
var ninjaDeps []string var ninjaDeps []string
ninjaDeps = append(ninjaDeps, extraNinjaDeps...) ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
@@ -645,13 +642,13 @@ func runSymlinkForestCreation(configuration android.Config, extraNinjaDeps []str
// Such a directory SHOULD be added to `ninjaDeps` so that a child directory // 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 // or file created/deleted under it would trigger an update of the symlink
// forest. // forest.
eventHandler.Do("symlink_forest", func() { ctx.EventHandler.Do("symlink_forest", func() {
symlinkForestDeps := bp2build.PlantSymlinkForest( symlinkForestDeps := bp2build.PlantSymlinkForest(
configuration.IsEnvTrue("BP2BUILD_VERBOSE"), topDir, workspaceRoot, generatedRoot, excludes) configuration.IsEnvTrue("BP2BUILD_VERBOSE"), topDir, workspaceRoot, generatedRoot, excludes)
ninjaDeps = append(ninjaDeps, symlinkForestDeps...) ninjaDeps = append(ninjaDeps, symlinkForestDeps...)
}) })
writeDepFile(symlinkForestMarker, eventHandler, ninjaDeps) writeDepFile(symlinkForestMarker, ctx.EventHandler, ninjaDeps)
touch(shared.JoinPath(topDir, symlinkForestMarker)) touch(shared.JoinPath(topDir, symlinkForestMarker))
codegenMetrics := bp2build.ReadCodegenMetrics(metricsDir) codegenMetrics := bp2build.ReadCodegenMetrics(metricsDir)
if codegenMetrics == nil { if codegenMetrics == nil {
@@ -661,27 +658,22 @@ func runSymlinkForestCreation(configuration android.Config, extraNinjaDeps []str
//TODO (usta) we cannot determine if we loaded a stale file, i.e. from an unrelated prior //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 //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 // Run Soong in the bp2build mode. This creates a standalone context that registers
// an alternate pipeline of mutators and singletons specifically for generating // an alternate pipeline of mutators and singletons specifically for generating
// Bazel BUILD files instead of Ninja files. // 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 var codegenMetrics *bp2build.CodegenMetrics
eventHandler := &metrics.EventHandler{} ctx.EventHandler.Do("bp2build", func() {
eventHandler.Do("bp2build", func() {
// Register an alternate set of singletons and mutators for bazel
// conversion for Bazel conversion.
bp2buildCtx := android.NewContext(configuration)
// Propagate "allow misssing dependencies" bit. This is normally set in // Propagate "allow misssing dependencies" bit. This is normally set in
// newContext(), but we create bp2buildCtx without calling that method. // newContext(), but we create ctx without calling that method.
bp2buildCtx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
bp2buildCtx.SetNameInterface(newNameResolver(configuration)) ctx.SetNameInterface(newNameResolver(configuration))
bp2buildCtx.RegisterForBazelConversion() ctx.RegisterForBazelConversion()
bp2buildCtx.SetModuleListFile(cmdlineArgs.ModuleListFile) ctx.SetModuleListFile(cmdlineArgs.ModuleListFile)
var ninjaDeps []string var ninjaDeps []string
ninjaDeps = append(ninjaDeps, extraNinjaDeps...) ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
@@ -689,25 +681,25 @@ func runBp2Build(configuration android.Config, extraNinjaDeps []string, metricsD
// Run the loading and analysis pipeline to prepare the graph of regular // Run the loading and analysis pipeline to prepare the graph of regular
// Modules parsed from Android.bp files, and the BazelTargetModules mapped // Modules parsed from Android.bp files, and the BazelTargetModules mapped
// from the regular Modules. // from the regular Modules.
eventHandler.Do("bootstrap", func() { ctx.EventHandler.Do("bootstrap", func() {
blueprintArgs := cmdlineArgs blueprintArgs := cmdlineArgs
bootstrapDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.StopBeforePrepareBuildActions, bp2buildCtx.Context, configuration) bootstrapDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.StopBeforePrepareBuildActions, ctx.Context, configuration)
ninjaDeps = append(ninjaDeps, bootstrapDeps...) ninjaDeps = append(ninjaDeps, bootstrapDeps...)
}) })
globListFiles := writeBuildGlobsNinjaFile(bp2buildCtx, configuration.SoongOutDir(), configuration) globListFiles := writeBuildGlobsNinjaFile(ctx, configuration.SoongOutDir(), configuration)
ninjaDeps = append(ninjaDeps, globListFiles...) ninjaDeps = append(ninjaDeps, globListFiles...)
// Run the code-generation phase to convert BazelTargetModules to BUILD files // Run the code-generation phase to convert BazelTargetModules to BUILD files
// and print conversion codegenMetrics to the user. // and print conversion codegenMetrics to the user.
codegenContext := bp2build.NewCodegenContext(configuration, bp2buildCtx, bp2build.Bp2Build) codegenContext := bp2build.NewCodegenContext(configuration, ctx, bp2build.Bp2Build)
eventHandler.Do("codegen", func() { ctx.EventHandler.Do("codegen", func() {
codegenMetrics = bp2build.Codegen(codegenContext) codegenMetrics = bp2build.Codegen(codegenContext)
}) })
ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...) ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...)
writeDepFile(bp2buildMarker, eventHandler, ninjaDeps) writeDepFile(bp2buildMarker, ctx.EventHandler, ninjaDeps)
touch(shared.JoinPath(topDir, bp2buildMarker)) touch(shared.JoinPath(topDir, bp2buildMarker))
}) })
@@ -717,7 +709,7 @@ func runBp2Build(configuration android.Config, extraNinjaDeps []string, metricsD
if configuration.IsEnvTrue("BP2BUILD_VERBOSE") { if configuration.IsEnvTrue("BP2BUILD_VERBOSE") {
codegenMetrics.Print() codegenMetrics.Print()
} }
writeBp2BuildMetrics(codegenMetrics, eventHandler, metricsDir) writeBp2BuildMetrics(codegenMetrics, ctx.EventHandler, metricsDir)
} }
// Write Bp2Build metrics into $LOG_DIR // Write Bp2Build metrics into $LOG_DIR