blob: b3d5afb7451fcdcf4fe2a9819e9ed022f06837a6 [file] [log] [blame]
Jingwen Chen164e0862021-02-19 00:48:40 -05001package bp2build
2
3import (
Jingwen Chen164e0862021-02-19 00:48:40 -05004 "fmt"
Liz Kammer6eff3232021-08-26 08:37:59 -04005 "strings"
Chris Parsons91b81f02021-12-08 11:19:06 -05006
7 "android/soong/android"
Jingwen Chen164e0862021-02-19 00:48:40 -05008)
9
10// Simple metrics struct to collect information about a Blueprint to BUILD
11// conversion process.
12type CodegenMetrics struct {
Jingwen Chen310bc8f2021-09-20 10:54:27 +000013 // Total number of Soong modules converted to generated targets
14 generatedModuleCount int
15
16 // Total number of Soong modules converted to handcrafted targets
17 handCraftedModuleCount int
18
19 // Total number of unconverted Soong modules
20 unconvertedModuleCount int
Jingwen Chen164e0862021-02-19 00:48:40 -050021
22 // Counts of generated Bazel targets per Bazel rule class
Jingwen Chen310bc8f2021-09-20 10:54:27 +000023 ruleClassCount map[string]int
Liz Kammer6eff3232021-08-26 08:37:59 -040024
25 moduleWithUnconvertedDepsMsgs []string
Jingwen Chen61174502021-09-17 08:40:45 +000026
27 convertedModules []string
Jingwen Chen164e0862021-02-19 00:48:40 -050028}
29
30// Print the codegen metrics to stdout.
Jingwen Chenafb84bd2021-09-20 10:31:46 +000031func (metrics *CodegenMetrics) Print() {
Jingwen Chen164e0862021-02-19 00:48:40 -050032 generatedTargetCount := 0
Jingwen Chen310bc8f2021-09-20 10:54:27 +000033 for _, ruleClass := range android.SortedStringKeys(metrics.ruleClassCount) {
34 count := metrics.ruleClassCount[ruleClass]
Jingwen Chen164e0862021-02-19 00:48:40 -050035 fmt.Printf("[bp2build] %s: %d targets\n", ruleClass, count)
36 generatedTargetCount += count
37 }
38 fmt.Printf(
Chris Parsons91b81f02021-12-08 11:19:06 -050039 "[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",
40 metrics.generatedModuleCount,
Jingwen Chen164e0862021-02-19 00:48:40 -050041 generatedTargetCount,
Jingwen Chen310bc8f2021-09-20 10:54:27 +000042 metrics.handCraftedModuleCount,
43 metrics.TotalModuleCount(),
Liz Kammer6eff3232021-08-26 08:37:59 -040044 len(metrics.moduleWithUnconvertedDepsMsgs),
45 strings.Join(metrics.moduleWithUnconvertedDepsMsgs, "\n\t"))
Jingwen Chen164e0862021-02-19 00:48:40 -050046}
Jingwen Chen61174502021-09-17 08:40:45 +000047
Jingwen Chen310bc8f2021-09-20 10:54:27 +000048func (metrics *CodegenMetrics) IncrementRuleClassCount(ruleClass string) {
49 metrics.ruleClassCount[ruleClass] += 1
50}
51
52func (metrics *CodegenMetrics) IncrementUnconvertedCount() {
53 metrics.unconvertedModuleCount += 1
54}
55
56func (metrics *CodegenMetrics) TotalModuleCount() int {
57 return metrics.handCraftedModuleCount +
58 metrics.generatedModuleCount +
59 metrics.unconvertedModuleCount
60}
61
62type ConversionType int
63
64const (
65 Generated ConversionType = iota
66 Handcrafted
67)
68
69func (metrics *CodegenMetrics) AddConvertedModule(moduleName string, conversionType ConversionType) {
Jingwen Chen61174502021-09-17 08:40:45 +000070 // Undo prebuilt_ module name prefix modifications
71 moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName)
72 metrics.convertedModules = append(metrics.convertedModules, moduleName)
Jingwen Chen310bc8f2021-09-20 10:54:27 +000073
74 if conversionType == Handcrafted {
75 metrics.handCraftedModuleCount += 1
76 } else if conversionType == Generated {
77 metrics.generatedModuleCount += 1
78 }
Jingwen Chen61174502021-09-17 08:40:45 +000079}