2021-02-19 06:48:40 +01:00
package bp2build
import (
"android/soong/android"
"fmt"
2021-08-26 14:37:59 +02:00
"strings"
2021-02-19 06:48:40 +01:00
)
// Simple metrics struct to collect information about a Blueprint to BUILD
// conversion process.
type CodegenMetrics struct {
2021-09-20 12:54:27 +02:00
// Total number of Soong modules converted to generated targets
generatedModuleCount int
2021-02-19 06:48:40 +01:00
2021-09-20 12:54:27 +02:00
// Total number of Soong modules converted to handcrafted targets
handCraftedModuleCount int
// Total number of unconverted Soong modules
unconvertedModuleCount int
2021-02-17 19:22:03 +01:00
2021-09-20 12:54:27 +02:00
// Counts of generated Bazel targets per Bazel rule class
ruleClassCount map [ string ] int
2021-08-26 14:37:59 +02:00
moduleWithUnconvertedDepsMsgs [ ] string
2021-09-17 10:40:45 +02:00
convertedModules [ ] string
2021-02-19 06:48:40 +01:00
}
// Print the codegen metrics to stdout.
2021-09-20 12:31:46 +02:00
func ( metrics * CodegenMetrics ) Print ( ) {
2021-02-19 06:48:40 +01:00
generatedTargetCount := 0
2021-09-20 12:54:27 +02:00
for _ , ruleClass := range android . SortedStringKeys ( metrics . ruleClassCount ) {
count := metrics . ruleClassCount [ ruleClass ]
2021-02-19 06:48:40 +01:00
fmt . Printf ( "[bp2build] %s: %d targets\n" , ruleClass , count )
generatedTargetCount += count
}
fmt . Printf (
2021-08-26 14:37:59 +02:00
"[bp2build] Generated %d total BUILD targets and included %d handcrafted BUILD targets from %d Android.bp modules.\n With %d modules with unconverted deps \n\t%s" ,
2021-02-19 06:48:40 +01:00
generatedTargetCount ,
2021-09-20 12:54:27 +02:00
metrics . handCraftedModuleCount ,
metrics . TotalModuleCount ( ) ,
2021-08-26 14:37:59 +02:00
len ( metrics . moduleWithUnconvertedDepsMsgs ) ,
strings . Join ( metrics . moduleWithUnconvertedDepsMsgs , "\n\t" ) )
2021-02-19 06:48:40 +01:00
}
2021-09-17 10:40:45 +02:00
2021-09-20 12:54:27 +02:00
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 ) {
2021-09-17 10:40:45 +02:00
// Undo prebuilt_ module name prefix modifications
moduleName = android . RemoveOptionalPrebuiltPrefix ( moduleName )
metrics . convertedModules = append ( metrics . convertedModules , moduleName )
2021-09-20 12:54:27 +02:00
if conversionType == Handcrafted {
metrics . handCraftedModuleCount += 1
} else if conversionType == Generated {
metrics . generatedModuleCount += 1
}
2021-09-17 10:40:45 +02:00
}