Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 4 | "fmt" |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 5 | "strings" |
Chris Parsons | 91b81f0 | 2021-12-08 11:19:06 -0500 | [diff] [blame^] | 6 | |
| 7 | "android/soong/android" |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // Simple metrics struct to collect information about a Blueprint to BUILD |
| 11 | // conversion process. |
| 12 | type CodegenMetrics struct { |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 13 | // 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 Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 21 | |
| 22 | // Counts of generated Bazel targets per Bazel rule class |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 23 | ruleClassCount map[string]int |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 24 | |
| 25 | moduleWithUnconvertedDepsMsgs []string |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 26 | |
| 27 | convertedModules []string |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // Print the codegen metrics to stdout. |
Jingwen Chen | afb84bd | 2021-09-20 10:31:46 +0000 | [diff] [blame] | 31 | func (metrics *CodegenMetrics) Print() { |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 32 | generatedTargetCount := 0 |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 33 | for _, ruleClass := range android.SortedStringKeys(metrics.ruleClassCount) { |
| 34 | count := metrics.ruleClassCount[ruleClass] |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 35 | fmt.Printf("[bp2build] %s: %d targets\n", ruleClass, count) |
| 36 | generatedTargetCount += count |
| 37 | } |
| 38 | fmt.Printf( |
Chris Parsons | 91b81f0 | 2021-12-08 11:19:06 -0500 | [diff] [blame^] | 39 | "[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 Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 41 | generatedTargetCount, |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 42 | metrics.handCraftedModuleCount, |
| 43 | metrics.TotalModuleCount(), |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 44 | len(metrics.moduleWithUnconvertedDepsMsgs), |
| 45 | strings.Join(metrics.moduleWithUnconvertedDepsMsgs, "\n\t")) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 46 | } |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 47 | |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 48 | func (metrics *CodegenMetrics) IncrementRuleClassCount(ruleClass string) { |
| 49 | metrics.ruleClassCount[ruleClass] += 1 |
| 50 | } |
| 51 | |
| 52 | func (metrics *CodegenMetrics) IncrementUnconvertedCount() { |
| 53 | metrics.unconvertedModuleCount += 1 |
| 54 | } |
| 55 | |
| 56 | func (metrics *CodegenMetrics) TotalModuleCount() int { |
| 57 | return metrics.handCraftedModuleCount + |
| 58 | metrics.generatedModuleCount + |
| 59 | metrics.unconvertedModuleCount |
| 60 | } |
| 61 | |
| 62 | type ConversionType int |
| 63 | |
| 64 | const ( |
| 65 | Generated ConversionType = iota |
| 66 | Handcrafted |
| 67 | ) |
| 68 | |
| 69 | func (metrics *CodegenMetrics) AddConvertedModule(moduleName string, conversionType ConversionType) { |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 70 | // Undo prebuilt_ module name prefix modifications |
| 71 | moduleName = android.RemoveOptionalPrebuiltPrefix(moduleName) |
| 72 | metrics.convertedModules = append(metrics.convertedModules, moduleName) |
Jingwen Chen | 310bc8f | 2021-09-20 10:54:27 +0000 | [diff] [blame] | 73 | |
| 74 | if conversionType == Handcrafted { |
| 75 | metrics.handCraftedModuleCount += 1 |
| 76 | } else if conversionType == Generated { |
| 77 | metrics.generatedModuleCount += 1 |
| 78 | } |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 79 | } |