Merge "bp2build: add converted modules to codegen metrics, and remove the compat layer."

This commit is contained in:
Jingwen Chen
2021-09-20 08:27:34 +00:00
committed by Gerrit Code Review
7 changed files with 19 additions and 61 deletions

View File

@@ -11,7 +11,6 @@ bootstrap_go_package {
"build_conversion.go", "build_conversion.go",
"bzl_conversion.go", "bzl_conversion.go",
"configurability.go", "configurability.go",
"compatibility.go",
"constants.go", "constants.go",
"conversion.go", "conversion.go",
"metrics.go", "metrics.go",

View File

@@ -43,7 +43,7 @@ func Codegen(ctx *CodegenContext) CodegenMetrics {
writeFiles(ctx, bp2buildDir, bp2buildFiles) writeFiles(ctx, bp2buildDir, bp2buildFiles)
soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName) soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName)
writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(res.compatLayer)) writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(res.metrics))
return res.metrics return res.metrics
} }

View File

@@ -249,7 +249,6 @@ func propsToAttributes(props map[string]string) string {
type conversionResults struct { type conversionResults struct {
buildFileToTargets map[string]BazelTargets buildFileToTargets map[string]BazelTargets
metrics CodegenMetrics metrics CodegenMetrics
compatLayer CodegenCompatLayer
} }
func (r conversionResults) BuildDirToTargets() map[string]BazelTargets { func (r conversionResults) BuildDirToTargets() map[string]BazelTargets {
@@ -265,10 +264,6 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers
RuleClassCount: make(map[string]int), RuleClassCount: make(map[string]int),
} }
compatLayer := CodegenCompatLayer{
NameToLabelMap: make(map[string]string),
}
dirs := make(map[string]bool) dirs := make(map[string]bool)
var errs []error var errs []error
@@ -285,7 +280,7 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers
if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() { if b, ok := m.(android.Bazelable); ok && b.HasHandcraftedLabel() {
metrics.handCraftedTargetCount += 1 metrics.handCraftedTargetCount += 1
metrics.TotalModuleCount += 1 metrics.TotalModuleCount += 1
compatLayer.AddNameToLabelEntry(m.Name(), b.HandcraftedLabel()) metrics.AddConvertedModule(m.Name())
pathToBuildFile := getBazelPackagePath(b) pathToBuildFile := getBazelPackagePath(b)
// We are using the entire contents of handcrafted build file, so if multiple targets within // 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. // 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) targets = generateBazelTargets(bpCtx, aModule)
for _, t := range targets { for _, t := range targets {
if t.name == m.Name() { // only add targets that exist in Soong to compatibility layer
// only add targets that exist in Soong to compatibility layer metrics.AddConvertedModule(m.Name())
compatLayer.AddNameToLabelEntry(m.Name(), t.Label())
}
metrics.RuleClassCount[t.ruleClass] += 1 metrics.RuleClassCount[t.ruleClass] += 1
} }
} else { } else {
@@ -360,7 +353,6 @@ func GenerateBazelTargets(ctx *CodegenContext, generateFilegroups bool) (convers
return conversionResults{ return conversionResults{
buildFileToTargets: buildFileToTargets, buildFileToTargets: buildFileToTargets,
metrics: metrics, metrics: metrics,
compatLayer: compatLayer,
}, errs }, errs
} }

View File

@@ -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
}

View File

@@ -16,29 +16,19 @@ type BazelFile struct {
Contents string Contents string
} }
func CreateSoongInjectionFiles(compatLayer CodegenCompatLayer) []BazelFile { func CreateSoongInjectionFiles(metrics CodegenMetrics) []BazelFile {
var files []BazelFile var files []BazelFile
files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package. 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("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 return files
} }
func nameToLabelAliases(nameToLabelMap map[string]string) string { func convertedModules(convertedModules []string) string {
ret := make([]string, len(nameToLabelMap)) return strings.Join(convertedModules, "\n")
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 CreateBazelFiles( func CreateBazelFiles(

View File

@@ -80,7 +80,7 @@ func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
} }
func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) { func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
files := CreateSoongInjectionFiles(CodegenCompatLayer{}) files := CreateSoongInjectionFiles(CodegenMetrics{})
expectedFilePaths := []bazelFilepath{ expectedFilePaths := []bazelFilepath{
{ {
@@ -92,8 +92,8 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
basename: "constants.bzl", basename: "constants.bzl",
}, },
{ {
dir: "module_name_to_label", dir: "metrics",
basename: GeneratedBuildFileName, basename: "converted_modules.txt",
}, },
} }
@@ -107,9 +107,5 @@ func TestCreateBazelFiles_Bp2Build_CreatesDefaultFiles(t *testing.T) {
if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename { if actualFile.Dir != expectedFile.dir || actualFile.Basename != expectedFile.basename {
t.Errorf("Did not find expected file %s/%s", actualFile.Dir, actualFile.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)
}
} }
} }

View File

@@ -19,6 +19,8 @@ type CodegenMetrics struct {
handCraftedTargetCount int handCraftedTargetCount int
moduleWithUnconvertedDepsMsgs []string moduleWithUnconvertedDepsMsgs []string
convertedModules []string
} }
// Print the codegen metrics to stdout. // Print the codegen metrics to stdout.
@@ -37,3 +39,9 @@ func (metrics CodegenMetrics) Print() {
len(metrics.moduleWithUnconvertedDepsMsgs), len(metrics.moduleWithUnconvertedDepsMsgs),
strings.Join(metrics.moduleWithUnconvertedDepsMsgs, "\n\t")) 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)
}