blob: bdd4b2a751a4feb1bb43c005cde9c68c9c3aa32a [file] [log] [blame]
Dan Willemsen01f0a052019-02-05 13:44:20 -08001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// This is a script that can be used to analyze the results from
16// build/soong/build_test.bash and recommend what devices need changes to their
17// BUILD_BROKEN_* flags.
18//
19// To use, download the logs.zip from one or more branches, and extract them
20// into subdirectories of the current directory. So for example, I have:
21//
22// ./aosp-master/aosp_arm/std_full.log
23// ./aosp-master/aosp_arm64/std_full.log
24// ./aosp-master/...
25// ./internal-master/aosp_arm/std_full.log
26// ./internal-master/aosp_arm64/std_full.log
27// ./internal-master/...
28//
29// Then I use `go run path/to/build_broken_logs.go *`
30package main
31
32import (
33 "fmt"
34 "io/ioutil"
35 "log"
36 "os"
37 "path/filepath"
38 "sort"
39 "strings"
40)
41
42func main() {
43 for _, branch := range os.Args[1:] {
44 fmt.Printf("\nBranch %s:\n", branch)
45 PrintResults(ParseBranch(branch))
46 }
47}
48
49type BuildBrokenBehavior int
50
51const (
52 DefaultFalse BuildBrokenBehavior = iota
53 DefaultTrue
54 DefaultDeprecated
55)
56
57var buildBrokenSettings = []struct {
58 name string
59 behavior BuildBrokenBehavior
60 warnings []string
61}{
62 {
Dan Willemsen01f0a052019-02-05 13:44:20 -080063 name: "BUILD_BROKEN_DUP_RULES",
64 behavior: DefaultFalse,
65 warnings: []string{"overriding commands for target"},
66 },
67 {
68 name: "BUILD_BROKEN_ANDROIDMK_EXPORTS",
Dan Willemsen60977462019-04-18 09:40:15 -070069 behavior: DefaultDeprecated,
Dan Willemsen01f0a052019-02-05 13:44:20 -080070 warnings: []string{"export_keyword"},
71 },
72 {
Dan Willemsen25e6f092019-04-09 10:22:43 -070073 name: "BUILD_BROKEN_USES_NETWORK",
74 behavior: DefaultDeprecated,
75 },
Dan Willemsen01f0a052019-02-05 13:44:20 -080076}
77
78type ProductBranch struct {
79 Branch string
80 Name string
81}
82
83type ProductLog struct {
84 ProductBranch
85 Log
86 Device string
87}
88
89type Log struct {
90 BuildBroken []*bool
91 HasBroken []bool
92}
93
94func Merge(l, l2 Log) Log {
95 if len(l.BuildBroken) == 0 {
96 l.BuildBroken = make([]*bool, len(buildBrokenSettings))
97 }
98 if len(l.HasBroken) == 0 {
99 l.HasBroken = make([]bool, len(buildBrokenSettings))
100 }
101
102 if len(l.BuildBroken) != len(l2.BuildBroken) || len(l.HasBroken) != len(l2.HasBroken) {
103 panic("mis-matched logs")
104 }
105
106 for i, v := range l.BuildBroken {
107 if v == nil {
108 l.BuildBroken[i] = l2.BuildBroken[i]
109 }
110 }
111 for i := range l.HasBroken {
112 l.HasBroken[i] = l.HasBroken[i] || l2.HasBroken[i]
113 }
114
115 return l
116}
117
118func PrintResults(products []ProductLog) {
119 devices := map[string]Log{}
120 deviceNames := []string{}
121
122 for _, product := range products {
123 device := product.Device
124 if _, ok := devices[device]; !ok {
125 deviceNames = append(deviceNames, device)
126 }
127 devices[device] = Merge(devices[device], product.Log)
128 }
129
130 sort.Strings(deviceNames)
131
132 for i, setting := range buildBrokenSettings {
133 printed := false
134
135 for _, device := range deviceNames {
136 log := devices[device]
137
138 if setting.behavior == DefaultTrue {
139 if log.BuildBroken[i] == nil || *log.BuildBroken[i] == false {
140 if log.HasBroken[i] {
141 printed = true
142 fmt.Printf(" %s needs to set %s := true\n", device, setting.name)
143 }
144 } else if !log.HasBroken[i] {
145 printed = true
146 fmt.Printf(" %s sets %s := true, but does not need it\n", device, setting.name)
147 }
148 } else if setting.behavior == DefaultFalse {
149 if log.BuildBroken[i] == nil {
150 // Nothing to be done
151 } else if *log.BuildBroken[i] == false {
152 printed = true
153 fmt.Printf(" %s sets %s := false, which is the default and can be removed\n", device, setting.name)
154 } else if !log.HasBroken[i] {
155 printed = true
156 fmt.Printf(" %s sets %s := true, but does not need it\n", device, setting.name)
157 }
158 } else if setting.behavior == DefaultDeprecated {
159 if log.BuildBroken[i] != nil {
160 printed = true
161 if log.HasBroken[i] {
162 fmt.Printf(" %s sets %s := %v, which is deprecated, but has failures\n", device, setting.name, *log.BuildBroken[i])
163 } else {
164 fmt.Printf(" %s sets %s := %v, which is deprecated and can be removed\n", device, setting.name, *log.BuildBroken[i])
165 }
166 }
167 }
168 }
169
170 if printed {
171 fmt.Println()
172 }
173 }
174}
175
176func ParseBranch(name string) []ProductLog {
177 products, err := filepath.Glob(filepath.Join(name, "*"))
178 if err != nil {
179 log.Fatal(err)
180 }
181
182 ret := []ProductLog{}
183 for _, product := range products {
184 product = filepath.Base(product)
185
186 ret = append(ret, ParseProduct(ProductBranch{Branch: name, Name: product}))
187 }
188 return ret
189}
190
191func ParseProduct(p ProductBranch) ProductLog {
192 soongLog, err := ioutil.ReadFile(filepath.Join(p.Branch, p.Name, "soong.log"))
193 if err != nil {
194 log.Fatal(err)
195 }
196
197 ret := ProductLog{
198 ProductBranch: p,
199 Log: Log{
200 BuildBroken: make([]*bool, len(buildBrokenSettings)),
201 HasBroken: make([]bool, len(buildBrokenSettings)),
202 },
203 }
204
205 lines := strings.Split(string(soongLog), "\n")
206 for _, line := range lines {
207 fields := strings.Split(line, " ")
208 if len(fields) != 5 {
209 continue
210 }
211
212 if fields[3] == "TARGET_DEVICE" {
213 ret.Device = fields[4]
214 }
215
216 if strings.HasPrefix(fields[3], "BUILD_BROKEN_") {
217 for i, setting := range buildBrokenSettings {
218 if setting.name == fields[3] {
219 ret.BuildBroken[i] = ParseBoolPtr(fields[4])
220 }
221 }
222 }
223 }
224
225 stdLog, err := ioutil.ReadFile(filepath.Join(p.Branch, p.Name, "std_full.log"))
226 if err != nil {
227 log.Fatal(err)
228 }
229 stdStr := string(stdLog)
230
231 for i, setting := range buildBrokenSettings {
232 for _, warning := range setting.warnings {
233 if strings.Contains(stdStr, warning) {
234 ret.HasBroken[i] = true
235 }
236 }
237 }
238
239 return ret
240}
241
242func ParseBoolPtr(str string) *bool {
243 var ret *bool
244 if str != "" {
245 b := str == "true"
246 ret = &b
247 }
248 return ret
249}