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
diff --git a/bp2build/bp2build.go b/bp2build/bp2build.go
index 30f298a..49729e0 100644
--- a/bp2build/bp2build.go
+++ b/bp2build/bp2build.go
@@ -16,52 +16,34 @@
import (
"android/soong/android"
+ "fmt"
"os"
)
-// The Bazel bp2build singleton is responsible for writing .bzl files that are equivalent to
+// The Bazel bp2build code generator is responsible for writing .bzl files that are equivalent to
// Android.bp files that are capable of being built with Bazel.
-func init() {
- android.RegisterBazelConverterPreSingletonType("androidbp_to_build", AndroidBpToBuildSingleton)
-}
-
-func AndroidBpToBuildSingleton() android.Singleton {
- return &androidBpToBuildSingleton{
- name: "bp2build",
- }
-}
-
-type androidBpToBuildSingleton struct {
- name string
- outputDir android.OutputPath
-}
-
-func (s *androidBpToBuildSingleton) GenerateBuildActions(ctx android.SingletonContext) {
- s.outputDir = android.PathForOutput(ctx, s.name)
- android.RemoveAllOutputDir(s.outputDir)
-
- if !ctx.Config().IsEnvTrue("CONVERT_TO_BAZEL") {
- return
- }
+func Codegen(ctx CodegenContext) {
+ outputDir := android.PathForOutput(ctx, "bp2build")
+ android.RemoveAllOutputDir(outputDir)
ruleShims := CreateRuleShims(android.ModuleTypeFactories())
- buildToTargets := GenerateSoongModuleTargets(ctx)
+ buildToTargets := GenerateSoongModuleTargets(ctx.Context())
filesToWrite := CreateBazelFiles(ruleShims, buildToTargets)
for _, f := range filesToWrite {
- if err := s.writeFile(ctx, f); err != nil {
- ctx.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)
+ if err := writeFile(outputDir, ctx, f); err != nil {
+ fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)
}
}
}
-func (s *androidBpToBuildSingleton) getOutputPath(ctx android.PathContext, dir string) android.OutputPath {
- return s.outputDir.Join(ctx, dir)
+func writeFile(outputDir android.OutputPath, ctx android.PathContext, f BazelFile) error {
+ return writeReadOnlyFile(ctx, getOutputPath(outputDir, ctx, f.Dir), f.Basename, f.Contents)
}
-func (s *androidBpToBuildSingleton) writeFile(ctx android.PathContext, f BazelFile) error {
- return writeReadOnlyFile(ctx, s.getOutputPath(ctx, f.Dir), f.Basename, f.Contents)
+func getOutputPath(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
+ return outputDir.Join(ctx, dir)
}
// The auto-conversion directory should be read-only, sufficient for bazel query. The files