platform_build_soong/bp2build/metrics.go
Chris Parsons 91b81f0b50 Print number of converted modules
Previously this was equal to the number of generated targets, but with
recent cc_library changes, we now sometimes generate more than one BUILD
target for a single module. Thus, converted module count is a more
useful metric.

At time of writing, this outputs:
`Converted 390 Android.bp modules to 453 total generated BUILD targets.
Included 4 handcrafted BUILD targets. There are 45093 total Android.bp
modules.`

Test: m bp2build
Change-Id: I7d68880dd4c6bf649ca753837a7f3a9c0d73753a
2021-12-08 11:39:07 -05:00

79 lines
2.3 KiB
Go

package bp2build
import (
"fmt"
"strings"
"android/soong/android"
)
// Simple metrics struct to collect information about a Blueprint to BUILD
// conversion process.
type CodegenMetrics struct {
// Total number of Soong modules converted to generated targets
generatedModuleCount int
// Total number of Soong modules converted to handcrafted targets
handCraftedModuleCount int
// Total number of unconverted Soong modules
unconvertedModuleCount int
// Counts of generated Bazel targets per Bazel rule class
ruleClassCount map[string]int
moduleWithUnconvertedDepsMsgs []string
convertedModules []string
}
// Print the codegen metrics to stdout.
func (metrics *CodegenMetrics) Print() {
generatedTargetCount := 0
for _, ruleClass := range android.SortedStringKeys(metrics.ruleClassCount) {
count := metrics.ruleClassCount[ruleClass]
fmt.Printf("[bp2build] %s: %d targets\n", ruleClass, count)
generatedTargetCount += count
}
fmt.Printf(
"[bp2build] Converted %d Android.bp modules to %d total generated BUILD targets. Included %d handcrafted BUILD targets. There are %d total Android.bp modules.\n%d converted modules have unconverted deps: \n\t%s",
metrics.generatedModuleCount,
generatedTargetCount,
metrics.handCraftedModuleCount,
metrics.TotalModuleCount(),
len(metrics.moduleWithUnconvertedDepsMsgs),
strings.Join(metrics.moduleWithUnconvertedDepsMsgs, "\n\t"))
}
func (metrics *CodegenMetrics) IncrementRuleClassCount(ruleClass string) {
metrics.ruleClassCount[ruleClass] += 1
}
func (metrics *CodegenMetrics) IncrementUnconvertedCount() {
metrics.unconvertedModuleCount += 1
}
func (metrics *CodegenMetrics) TotalModuleCount() int {
return metrics.handCraftedModuleCount +
metrics.generatedModuleCount +
metrics.unconvertedModuleCount
}
type ConversionType int
const (
Generated ConversionType = iota
Handcrafted
)
func (metrics *CodegenMetrics) AddConvertedModule(moduleName string, conversionType ConversionType) {
// Undo prebuilt_ module name prefix modifications
moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
metrics.convertedModules = append(metrics.convertedModules, moduleName)
if conversionType == Handcrafted {
metrics.handCraftedModuleCount += 1
} else if conversionType == Generated {
metrics.generatedModuleCount += 1
}
}