diff --git a/bp2build/Android.bp b/bp2build/Android.bp index 1d52a7040..40526a623 100644 --- a/bp2build/Android.bp +++ b/bp2build/Android.bp @@ -11,7 +11,6 @@ bootstrap_go_package { "build_conversion.go", "bzl_conversion.go", "configurability.go", - "compatibility.go", "constants.go", "conversion.go", "metrics.go", diff --git a/bp2build/bp2build.go b/bp2build/bp2build.go index 48b294529..45a3cb6fe 100644 --- a/bp2build/bp2build.go +++ b/bp2build/bp2build.go @@ -43,7 +43,7 @@ func Codegen(ctx *CodegenContext) CodegenMetrics { writeFiles(ctx, bp2buildDir, bp2buildFiles) soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName) - writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(res.compatLayer)) + writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(res.metrics)) return res.metrics } diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go index 2cbb5579e..f7b392b04 100644 --- a/bp2build/build_conversion.go +++ b/bp2build/build_conversion.go @@ -249,7 +249,6 @@ func propsToAttributes(props map[string]string) string { type conversionResults struct { buildFileToTargets map[string]BazelTargets metrics CodegenMetrics - compatLayer CodegenCompatLayer } func (r conversionResults) BuildDirToTargets() map[string]BazelTargets { @@ -265,10 +264,6 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers RuleClassCount: make(map[string]int), } - compatLayer := CodegenCompatLayer{ - NameToLabelMap: make(map[string]string), - } - dirs := make(map[string]bool) var errs []error @@ -285,7 +280,7 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() { metrics.handCraftedTargetCount += 1 metrics.TotalModuleCount += 1 - compatLayer.AddNameToLabelEntry(m.Name(), b.HandcraftedLabel()) + metrics.AddConvertedModule(m.Name()) pathToBuildFile := getBazelPackagePath(b) // We are using the entire contents of handcrafted build file, so if multiple targets within // a package have handcrafted targets, we only want to include the contents one time. @@ -314,10 +309,8 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers } targets = generateBazelTargets(bpCtx, aModule) for _, t := range targets { - if t.name == m.Name() { - // only add targets that exist in Soong to compatibility layer - compatLayer.AddNameToLabelEntry(m.Name(), t.Label()) - } + // only add targets that exist in Soong to compatibility layer + metrics.AddConvertedModule(m.Name()) metrics.RuleClassCount[t.ruleClass] += 1 } } else { @@ -360,7 +353,6 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers return conversionResults{ buildFileToTargets: buildFileToTargets, metrics: metrics, - compatLayer: compatLayer, }, errs } diff --git a/bp2build/compatibility.go b/bp2build/compatibility.go deleted file mode 100644 index 01dbdb9df..000000000 --- a/bp2build/compatibility.go +++ /dev/null @@ -1,27 +0,0 @@ -package bp2build - -import ( - "fmt" -) - -// Data from the code generation process that is used to improve compatibility -// between build systems. -type CodegenCompatLayer struct { - // A map from the original module name to the generated/handcrafted Bazel - // label for legacy build systems to be able to build a fully-qualified - // Bazel target from an unique module name. - NameToLabelMap map[string]string -} - -// Log an entry of module name -> Bazel target label. -func (compatLayer CodegenCompatLayer) AddNameToLabelEntry(name, label string) { - if existingLabel, ok := compatLayer.NameToLabelMap[name]; ok { - panic(fmt.Errorf( - "Module '%s' maps to more than one Bazel target label: %s, %s. "+ - "This shouldn't happen. It probably indicates a bug with the bp2build internals.", - name, - existingLabel, - label)) - } - compatLayer.NameToLabelMap[name] = label -} diff --git a/bp2build/conversion.go b/bp2build/conversion.go index 75bc2b415..354abf6b7 100644 --- a/bp2build/conversion.go +++ b/bp2build/conversion.go @@ -16,29 +16,19 @@ type BazelFile struct { Contents string } -func CreateSoongInjectionFiles(compatLayer CodegenCompatLayer) []BazelFile { +func CreateSoongInjectionFiles(metrics CodegenMetrics) []BazelFile { var files []BazelFile files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package. files = append(files, newFile("cc_toolchain", "constants.bzl", config.BazelCcToolchainVars())) - files = append(files, newFile("module_name_to_label", GeneratedBuildFileName, nameToLabelAliases(compatLayer.NameToLabelMap))) + files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n"))) return files } -func nameToLabelAliases(nameToLabelMap map[string]string) string { - ret := make([]string, len(nameToLabelMap)) - - for k, v := range nameToLabelMap { - // v is the fully qualified label rooted at '//' - ret = append(ret, fmt.Sprintf( - `alias( - name = "%s", - actual = "@%s", -)`, k, v)) - } - return strings.Join(ret, "\n\n") +func convertedModules(convertedModules []string) string { + return strings.Join(convertedModules, "\n") } func CreateBazelFiles( diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go index 56ea589bb..dfa1a9eb4 100644 --- a/bp2build/conversion_test.go +++ b/bp2build/conversion_test.go @@ -80,7 +80,7 @@ func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { } func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { - files := CreateSoongInjectionFiles(CodegenCompatLayer{}) + files := CreateSoongInjectionFiles(CodegenMetrics{}) expectedFilePaths := []bazelFilepath{ { @@ -92,8 +92,8 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { basename: "constants.bzl", }, { - dir: "module_name_to_label", - basename: GeneratedBuildFileName, + dir: "metrics", + basename: "converted_modules.txt", }, } @@ -107,9 +107,5 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename { t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.Basename) } - - if expectedFile.basename != GeneratedBuildFileName && actualFile.Contents == "" { - t.Errorf("Contents of %s unexpected empty.", actualFile) - } } } diff --git a/bp2build/metrics.go b/bp2build/metrics.go index 645ef2d19..55b928b70 100644 --- a/bp2build/metrics.go +++ b/bp2build/metrics.go @@ -19,6 +19,8 @@ type CodegenMetrics struct { handCraftedTargetCount int moduleWithUnconvertedDepsMsgs []string + + convertedModules []string } // Print the codegen metrics to stdout. @@ -37,3 +39,9 @@ func (metrics CodegenMetrics) Print() { len(metrics.moduleWithUnconvertedDepsMsgs), strings.Join(metrics.moduleWithUnconvertedDepsMsgs, "\n\t")) } + +func (metrics CodegenMetrics) AddConvertedModule(moduleName string) { + // Undo prebuilt_ module name prefix modifications + moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName) + metrics.convertedModules = append(metrics.convertedModules, moduleName) +}