aboutsummaryrefslogtreecommitdiff
path: root/cmd/soong_build
diff options
context:
space:
mode:
authorJingwen Chen <jingwen@google.com>2020-12-14 02:58:54 -0500
committerJingwen Chen <jingwen@google.com>2021-01-19 11:03:36 -0500
commitdaa54bcbba9dbea37ddf28feb948a112e3f4c95d (patch)
treed72e4954f212f95daee42d66e572041ac558979b /cmd/soong_build
parent4d31a041c75525ffb526376792760622b081e9e1 (diff)
downloadsoong-daa54bcbba9dbea37ddf28feb948a112e3f4c95d.tar.gz
Implement code-generation step for bp2build.
Implement bp2build codegen as a discrete step that runs after an alternatively registered pipeline of mutators, instead of a presingleton. bp2build codegen requires a Context that supports VisitAllModules and PathContext, so this CL also makes a BpToBuildWrapperContext that conforms to PathContext by adding two method implementations. Test: GENERATE_BAZEL_FILES=true m nothing && bazel query //... --config=bp2build | wc -l # 31433 Test: m queryview && bazel query //... --config=queryview # 63638 Change-Id: I0dd359746584b228046d2d0ff00895f28f9bdfc3
Diffstat (limited to 'cmd/soong_build')
-rw-r--r--cmd/soong_build/main.go44
-rw-r--r--cmd/soong_build/queryview.go39
2 files changed, 33 insertions, 50 deletions
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index 907bed3c7..1d5e7b3af 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -23,6 +23,7 @@ import (
"github.com/google/blueprint/bootstrap"
"android/soong/android"
+ "android/soong/bp2build"
)
var (
@@ -54,18 +55,12 @@ func newNameResolver(config android.Config) *android.NameResolver {
// bazelConversionRequested checks that the user is intending to convert
// Blueprint to Bazel BUILD files.
func bazelConversionRequested(configuration android.Config) bool {
- return configuration.IsEnvTrue("CONVERT_TO_BAZEL")
+ return configuration.IsEnvTrue("GENERATE_BAZEL_FILES")
}
-func newContext(srcDir string, configuration android.Config) *android.Context {
+func newContext(configuration android.Config) *android.Context {
ctx := android.NewContext(configuration)
- if bazelConversionRequested(configuration) {
- // Register an alternate set of singletons and mutators for bazel
- // conversion for Bazel conversion.
- ctx.RegisterForBazelConversion()
- } else {
- ctx.Register()
- }
+ ctx.Register()
if !shouldPrepareBuildActions(configuration) {
configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
}
@@ -100,13 +95,22 @@ func main() {
// enabled even if it completed successfully.
extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve"))
}
+
+ if bazelConversionRequested(configuration) {
+ // Run the alternate pipeline of bp2build mutators and singleton to convert Blueprint to BUILD files
+ // before everything else.
+ runBp2Build(configuration, extraNinjaDeps)
+ // Short-circuit and return.
+ return
+ }
+
if configuration.BazelContext.BazelEnabled() {
// Bazel-enabled mode. Soong runs in two passes.
// First pass: Analyze the build tree, but only store all bazel commands
// needed to correctly evaluate the tree in the second pass.
// TODO(cparsons): Don't output any ninja file, as the second pass will overwrite
// the incorrect results from the first pass, and file I/O is expensive.
- firstCtx := newContext(srcDir, configuration)
+ firstCtx := newContext(configuration)
configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja)
bootstrap.Main(firstCtx.Context, configuration, extraNinjaDeps...)
// Invoke bazel commands and save results for second pass.
@@ -120,10 +124,10 @@ func main() {
fmt.Fprintf(os.Stderr, "%s", err)
os.Exit(1)
}
- ctx = newContext(srcDir, secondPassConfig)
+ ctx = newContext(secondPassConfig)
bootstrap.Main(ctx.Context, secondPassConfig, extraNinjaDeps...)
} else {
- ctx = newContext(srcDir, configuration)
+ ctx = newContext(configuration)
bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...)
}
@@ -154,6 +158,22 @@ func main() {
}
}
+// 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) {
+ // Register an alternate set of singletons and mutators for bazel
+ // conversion for Bazel conversion.
+ bp2buildCtx := android.NewContext(configuration)
+ bp2buildCtx.RegisterForBazelConversion()
+ configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
+ bp2buildCtx.SetNameInterface(newNameResolver(configuration))
+ bootstrap.Main(bp2buildCtx.Context, configuration, extraNinjaDeps...)
+
+ codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx)
+ bp2build.Codegen(codegenContext)
+}
+
// shouldPrepareBuildActions reads configuration and flags if build actions
// should be generated.
func shouldPrepareBuildActions(configuration android.Config) bool {
diff --git a/cmd/soong_build/queryview.go b/cmd/soong_build/queryview.go
index 3657ea8ef..79ea94af7 100644
--- a/cmd/soong_build/queryview.go
+++ b/cmd/soong_build/queryview.go
@@ -20,48 +20,11 @@ import (
"io/ioutil"
"os"
"path/filepath"
-
- "github.com/google/blueprint"
)
-type queryviewContext struct {
- bpCtx *blueprint.Context
-}
-
-func (ctx *queryviewContext) ModuleName(module blueprint.Module) string {
- return ctx.bpCtx.ModuleName(module)
-}
-
-func (ctx *queryviewContext) ModuleDir(module blueprint.Module) string {
- return ctx.bpCtx.ModuleDir(module)
-}
-
-func (ctx *queryviewContext) ModuleSubDir(module blueprint.Module) string {
- return ctx.bpCtx.ModuleSubDir(module)
-}
-
-func (ctx *queryviewContext) ModuleType(module blueprint.Module) string {
- return ctx.bpCtx.ModuleType(module)
-}
-
-func (ctx *queryviewContext) VisitAllModulesBlueprint(visit func(blueprint.Module)) {
- ctx.bpCtx.VisitAllModules(visit)
-}
-
-func (ctx *queryviewContext) VisitDirectDeps(module android.Module, visit func(android.Module)) {
- ctx.bpCtx.VisitDirectDeps(module, func(m blueprint.Module) {
- if aModule, ok := m.(android.Module); ok {
- visit(aModule)
- }
- })
-}
-
func createBazelQueryView(ctx *android.Context, bazelQueryViewDir string) error {
- qvCtx := queryviewContext{
- bpCtx: ctx.Context,
- }
ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories())
- buildToTargets := bp2build.GenerateSoongModuleTargets(&qvCtx)
+ buildToTargets := bp2build.GenerateSoongModuleTargets(*ctx)
filesToWrite := bp2build.CreateBazelFiles(ruleShims, buildToTargets)
for _, f := range filesToWrite {