Add bp2build mode to soong_build.
This CL adds a new CONVERT_TO_BAZEL env var, and a bp2build goal. It
reuses the queryview architecture, but registers no mutators before
converting to BUILD files. This gives us a blank slate of the module
graph to work with, and create a shadow/alternate pipeline of mutators
to converge the Bazel BUILD graph to be semantically equivalent with the
Android.bp graph (after apply its current set of mutators).
The command to do so is:
$ CONVERT_TO_BAZEL=true m bp2build
To not clobber with queryview, the generated files are in
out/soong/bp2build.
Test: CONVERT_TO_BAZEL=true m bp2build && bazel query --config=bp2build //...
Test: m queryview && bazel query --config=queryview //..
Test: soong tests
Test: TH presubmit
Fixes: 174465461
Signed-off-by: Jingwen Chen <jingwen@google.com>
Change-Id: I4dd0ccc73abc345d70a50ca2803d6f400cd8c863
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index b88803a..eeee3a8 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -51,10 +51,22 @@
return android.NewNameResolver(exportFilter)
}
+// 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")
+}
+
func newContext(srcDir string, configuration android.Config) *android.Context {
ctx := android.NewContext(configuration)
- ctx.Register()
- if !shouldPrepareBuildActions() {
+ if bazelConversionRequested(configuration) {
+ // Register an alternate set of singletons and mutators for bazel
+ // conversion for Bazel conversion.
+ ctx.RegisterForBazelConversion()
+ } else {
+ ctx.Register()
+ }
+ if !shouldPrepareBuildActions(configuration) {
configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
}
ctx.SetNameInterface(newNameResolver(configuration))
@@ -114,6 +126,8 @@
ctx = newContext(srcDir, configuration)
bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...)
}
+
+ // Convert the Soong module graph into Bazel BUILD files.
if bazelQueryViewDir != "" {
if err := createBazelQueryView(ctx, bazelQueryViewDir); err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
@@ -130,7 +144,7 @@
// TODO(ccross): make this a command line argument. Requires plumbing through blueprint
// to affect the command line of the primary builder.
- if shouldPrepareBuildActions() {
+ if shouldPrepareBuildActions(configuration) {
metricsFile := filepath.Join(bootstrap.BuildDir, "soong_build_metrics.pb")
err := android.WriteMetrics(configuration, metricsFile)
if err != nil {
@@ -140,8 +154,19 @@
}
}
-func shouldPrepareBuildActions() bool {
- // If we're writing soong_docs or queryview, don't write build.ninja or
- // collect metrics.
- return docFile == "" && bazelQueryViewDir == ""
+// shouldPrepareBuildActions reads configuration and flags if build actions
+// should be generated.
+func shouldPrepareBuildActions(configuration android.Config) bool {
+ // Generating Soong docs
+ if docFile != "" {
+ return false
+ }
+
+ // Generating a directory for Soong query (queryview)
+ if bazelQueryViewDir != "" {
+ return false
+ }
+
+ // Generating a directory for converted Bazel BUILD files
+ return !bazelConversionRequested(configuration)
}