blob: 04fac441ed035d07b1e08baf5d1e346b80b25819 [file] [log] [blame]
Jingwen Chen164e0862021-02-19 00:48:40 -05001package bp2build
2
3import (
Jingwen Chen164e0862021-02-19 00:48:40 -05004 "fmt"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05005 "os"
6 "path/filepath"
Liz Kammer6eff3232021-08-26 08:37:59 -04007 "strings"
Chris Parsons91b81f02021-12-08 11:19:06 -05008
9 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050010 "android/soong/shared"
11 "android/soong/ui/metrics/bp2build_metrics_proto"
Chris Parsons492bd912022-01-20 12:55:05 -050012 "github.com/google/blueprint"
Jingwen Chen164e0862021-02-19 00:48:40 -050013)
14
15// Simple metrics struct to collect information about a Blueprint to BUILD
16// conversion process.
17type CodegenMetrics struct {
Jingwen Chen310bc8f2021-09-20 10:54:27 +000018 // Total number of Soong modules converted to generated targets
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050019 generatedModuleCount uint64
Jingwen Chen310bc8f2021-09-20 10:54:27 +000020
21 // Total number of Soong modules converted to handcrafted targets
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050022 handCraftedModuleCount uint64
Jingwen Chen310bc8f2021-09-20 10:54:27 +000023
24 // Total number of unconverted Soong modules
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050025 unconvertedModuleCount uint64
Jingwen Chen164e0862021-02-19 00:48:40 -050026
27 // Counts of generated Bazel targets per Bazel rule class
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050028 ruleClassCount map[string]uint64
Liz Kammer6eff3232021-08-26 08:37:59 -040029
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050030 // List of modules with unconverted deps
31 // NOTE: NOT in the .proto
Liz Kammer6eff3232021-08-26 08:37:59 -040032 moduleWithUnconvertedDepsMsgs []string
Jingwen Chen61174502021-09-17 08:40:45 +000033
Liz Kammerdaa09ef2021-12-15 15:35:38 -050034 // List of modules with missing deps
35 // NOTE: NOT in the .proto
36 moduleWithMissingDepsMsgs []string
37
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050038 // List of converted modules
Jingwen Chen61174502021-09-17 08:40:45 +000039 convertedModules []string
Chris Parsons492bd912022-01-20 12:55:05 -050040
41 // Counts of converted modules by module type.
42 convertedModuleTypeCount map[string]uint64
43
44 // Counts of total modules by module type.
45 totalModuleTypeCount map[string]uint64
Chris Parsons715b08f2022-03-22 19:23:40 -040046
47 Events []*bp2build_metrics_proto.Event
Jingwen Chen164e0862021-02-19 00:48:40 -050048}
49
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050050// Serialize returns the protoized version of CodegenMetrics: bp2build_metrics_proto.Bp2BuildMetrics
51func (metrics *CodegenMetrics) Serialize() bp2build_metrics_proto.Bp2BuildMetrics {
52 return bp2build_metrics_proto.Bp2BuildMetrics{
Chris Parsons492bd912022-01-20 12:55:05 -050053 GeneratedModuleCount: metrics.generatedModuleCount,
54 HandCraftedModuleCount: metrics.handCraftedModuleCount,
55 UnconvertedModuleCount: metrics.unconvertedModuleCount,
56 RuleClassCount: metrics.ruleClassCount,
57 ConvertedModules: metrics.convertedModules,
58 ConvertedModuleTypeCount: metrics.convertedModuleTypeCount,
59 TotalModuleTypeCount: metrics.totalModuleTypeCount,
Chris Parsons715b08f2022-03-22 19:23:40 -040060 Events: metrics.Events,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050061 }
62}
63
Jingwen Chen164e0862021-02-19 00:48:40 -050064// Print the codegen metrics to stdout.
Jingwen Chenafb84bd2021-09-20 10:31:46 +000065func (metrics *CodegenMetrics) Print() {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050066 generatedTargetCount := uint64(0)
Jingwen Chen310bc8f2021-09-20 10:54:27 +000067 for _, ruleClass := range android.SortedStringKeys(metrics.ruleClassCount) {
68 count := metrics.ruleClassCount[ruleClass]
Jingwen Chen164e0862021-02-19 00:48:40 -050069 fmt.Printf("[bp2build] %s: %d targets\n", ruleClass, count)
70 generatedTargetCount += count
71 }
72 fmt.Printf(
Liz Kammerdaa09ef2021-12-15 15:35:38 -050073 `[bp2build] Converted %d Android.bp modules to %d total generated BUILD targets. Included %d handcrafted BUILD targets. There are %d total Android.bp modules.
74%d converted modules have unconverted deps:
75 %s
76%d converted modules have missing deps:
77 %s
78`,
Chris Parsons91b81f02021-12-08 11:19:06 -050079 metrics.generatedModuleCount,
Jingwen Chen164e0862021-02-19 00:48:40 -050080 generatedTargetCount,
Jingwen Chen310bc8f2021-09-20 10:54:27 +000081 metrics.handCraftedModuleCount,
82 metrics.TotalModuleCount(),
Liz Kammer6eff3232021-08-26 08:37:59 -040083 len(metrics.moduleWithUnconvertedDepsMsgs),
Liz Kammerdaa09ef2021-12-15 15:35:38 -050084 strings.Join(metrics.moduleWithUnconvertedDepsMsgs, "\n\t"),
85 len(metrics.moduleWithMissingDepsMsgs),
86 strings.Join(metrics.moduleWithMissingDepsMsgs, "\n\t"),
87 )
Jingwen Chen164e0862021-02-19 00:48:40 -050088}
Jingwen Chen61174502021-09-17 08:40:45 +000089
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -050090const bp2buildMetricsFilename = "bp2build_metrics.pb"
91
92// fail prints $PWD to stderr, followed by the given printf string and args (vals),
93// then the given alert, and then exits with 1 for failure
94func fail(err error, alertFmt string, vals ...interface{}) {
95 cwd, wderr := os.Getwd()
96 if wderr != nil {
97 cwd = "FAILED TO GET $PWD: " + wderr.Error()
98 }
99 fmt.Fprintf(os.Stderr, "\nIn "+cwd+":\n"+alertFmt+"\n"+err.Error()+"\n", vals...)
100 os.Exit(1)
101}
102
103// Write the bp2build-protoized codegen metrics into the given directory
104func (metrics *CodegenMetrics) Write(dir string) {
105 if _, err := os.Stat(dir); os.IsNotExist(err) {
106 // The metrics dir doesn't already exist, so create it (and parents)
107 if err := os.MkdirAll(dir, 0755); err != nil { // rx for all; w for user
108 fail(err, "Failed to `mkdir -p` %s", dir)
109 }
110 } else if err != nil {
111 fail(err, "Failed to `stat` %s", dir)
112 }
113 metricsFile := filepath.Join(dir, bp2buildMetricsFilename)
114 if err := metrics.dump(metricsFile); err != nil {
115 fail(err, "Error outputting %s", metricsFile)
116 }
117 if _, err := os.Stat(metricsFile); err != nil {
118 fail(err, "MISSING BP2BUILD METRICS OUTPUT: Failed to `stat` %s", metricsFile)
119 } else {
120 fmt.Printf("\nWrote bp2build metrics to: %s\n", metricsFile)
121 }
122}
123
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000124func (metrics *CodegenMetrics) IncrementRuleClassCount(ruleClass string) {
125 metrics.ruleClassCount[ruleClass] += 1
126}
127
Chris Parsons492bd912022-01-20 12:55:05 -0500128func (metrics *CodegenMetrics) AddUnconvertedModule(moduleType string) {
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000129 metrics.unconvertedModuleCount += 1
Chris Parsons492bd912022-01-20 12:55:05 -0500130 metrics.totalModuleTypeCount[moduleType] += 1
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000131}
132
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -0500133func (metrics *CodegenMetrics) TotalModuleCount() uint64 {
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000134 return metrics.handCraftedModuleCount +
135 metrics.generatedModuleCount +
136 metrics.unconvertedModuleCount
137}
138
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -0500139// Dump serializes the metrics to the given filename
140func (metrics *CodegenMetrics) dump(filename string) (err error) {
141 ser := metrics.Serialize()
142 return shared.Save(&ser, filename)
143}
144
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000145type ConversionType int
146
147const (
148 Generated ConversionType = iota
149 Handcrafted
150)
151
Chris Parsons492bd912022-01-20 12:55:05 -0500152func (metrics *CodegenMetrics) AddConvertedModule(m blueprint.Module, moduleType string, conversionType ConversionType) {
Jingwen Chen61174502021-09-17 08:40:45 +0000153 // Undo prebuilt_ module name prefix modifications
Chris Parsons492bd912022-01-20 12:55:05 -0500154 moduleName := android.RemoveOptionalPrebuiltPrefix(m.Name())
Jingwen Chen61174502021-09-17 08:40:45 +0000155 metrics.convertedModules = append(metrics.convertedModules, moduleName)
Chris Parsons492bd912022-01-20 12:55:05 -0500156 metrics.convertedModuleTypeCount[moduleType] += 1
157 metrics.totalModuleTypeCount[moduleType] += 1
Jingwen Chen310bc8f2021-09-20 10:54:27 +0000158
159 if conversionType == Handcrafted {
160 metrics.handCraftedModuleCount += 1
161 } else if conversionType == Generated {
162 metrics.generatedModuleCount += 1
163 }
Jingwen Chen61174502021-09-17 08:40:45 +0000164}