blob: f081f262d141f453711786c5443486a28ecf5cd0 [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 {
63 name: "BUILD_BROKEN_DUP_COPY_HEADERS",
64 behavior: DefaultDeprecated,
65 warnings: []string{"Duplicate header copy:"},
66 },
67 {
68 name: "BUILD_BROKEN_DUP_RULES",
69 behavior: DefaultFalse,
70 warnings: []string{"overriding commands for target"},
71 },
72 {
73 name: "BUILD_BROKEN_ANDROIDMK_EXPORTS",
74 behavior: DefaultFalse,
75 warnings: []string{"export_keyword"},
76 },
77 {
78 name: "BUILD_BROKEN_PHONY_TARGETS",
79 behavior: DefaultFalse,
80 warnings: []string{
81 "depends on PHONY target",
82 "looks like a real file",
83 "writing to readonly directory",
84 },
85 },
86 {
87 name: "BUILD_BROKEN_ENG_DEBUG_TAGS",
88 behavior: DefaultTrue,
89 warnings: []string{
90 "Changes.md#LOCAL_MODULE_TAGS",
91 },
92 },
Dan Willemsen25e6f092019-04-09 10:22:43 -070093 {
94 name: "BUILD_BROKEN_USES_NETWORK",
95 behavior: DefaultDeprecated,
96 },
Dan Willemsen01f0a052019-02-05 13:44:20 -080097}
98
99type ProductBranch struct {
100 Branch string
101 Name string
102}
103
104type ProductLog struct {
105 ProductBranch
106 Log
107 Device string
108}
109
110type Log struct {
111 BuildBroken []*bool
112 HasBroken []bool
113}
114
115func Merge(l, l2 Log) Log {
116 if len(l.BuildBroken) == 0 {
117 l.BuildBroken = make([]*bool, len(buildBrokenSettings))
118 }
119 if len(l.HasBroken) == 0 {
120 l.HasBroken = make([]bool, len(buildBrokenSettings))
121 }
122
123 if len(l.BuildBroken) != len(l2.BuildBroken) || len(l.HasBroken) != len(l2.HasBroken) {
124 panic("mis-matched logs")
125 }
126
127 for i, v := range l.BuildBroken {
128 if v == nil {
129 l.BuildBroken[i] = l2.BuildBroken[i]
130 }
131 }
132 for i := range l.HasBroken {
133 l.HasBroken[i] = l.HasBroken[i] || l2.HasBroken[i]
134 }
135
136 return l
137}
138
139func PrintResults(products []ProductLog) {
140 devices := map[string]Log{}
141 deviceNames := []string{}
142
143 for _, product := range products {
144 device := product.Device
145 if _, ok := devices[device]; !ok {
146 deviceNames = append(deviceNames, device)
147 }
148 devices[device] = Merge(devices[device], product.Log)
149 }
150
151 sort.Strings(deviceNames)
152
153 for i, setting := range buildBrokenSettings {
154 printed := false
155
156 for _, device := range deviceNames {
157 log := devices[device]
158
159 if setting.behavior == DefaultTrue {
160 if log.BuildBroken[i] == nil || *log.BuildBroken[i] == false {
161 if log.HasBroken[i] {
162 printed = true
163 fmt.Printf(" %s needs to set %s := true\n", device, setting.name)
164 }
165 } else if !log.HasBroken[i] {
166 printed = true
167 fmt.Printf(" %s sets %s := true, but does not need it\n", device, setting.name)
168 }
169 } else if setting.behavior == DefaultFalse {
170 if log.BuildBroken[i] == nil {
171 // Nothing to be done
172 } else if *log.BuildBroken[i] == false {
173 printed = true
174 fmt.Printf(" %s sets %s := false, which is the default and can be removed\n", device, setting.name)
175 } else if !log.HasBroken[i] {
176 printed = true
177 fmt.Printf(" %s sets %s := true, but does not need it\n", device, setting.name)
178 }
179 } else if setting.behavior == DefaultDeprecated {
180 if log.BuildBroken[i] != nil {
181 printed = true
182 if log.HasBroken[i] {
183 fmt.Printf(" %s sets %s := %v, which is deprecated, but has failures\n", device, setting.name, *log.BuildBroken[i])
184 } else {
185 fmt.Printf(" %s sets %s := %v, which is deprecated and can be removed\n", device, setting.name, *log.BuildBroken[i])
186 }
187 }
188 }
189 }
190
191 if printed {
192 fmt.Println()
193 }
194 }
195}
196
197func ParseBranch(name string) []ProductLog {
198 products, err := filepath.Glob(filepath.Join(name, "*"))
199 if err != nil {
200 log.Fatal(err)
201 }
202
203 ret := []ProductLog{}
204 for _, product := range products {
205 product = filepath.Base(product)
206
207 ret = append(ret, ParseProduct(ProductBranch{Branch: name, Name: product}))
208 }
209 return ret
210}
211
212func ParseProduct(p ProductBranch) ProductLog {
213 soongLog, err := ioutil.ReadFile(filepath.Join(p.Branch, p.Name, "soong.log"))
214 if err != nil {
215 log.Fatal(err)
216 }
217
218 ret := ProductLog{
219 ProductBranch: p,
220 Log: Log{
221 BuildBroken: make([]*bool, len(buildBrokenSettings)),
222 HasBroken: make([]bool, len(buildBrokenSettings)),
223 },
224 }
225
226 lines := strings.Split(string(soongLog), "\n")
227 for _, line := range lines {
228 fields := strings.Split(line, " ")
229 if len(fields) != 5 {
230 continue
231 }
232
233 if fields[3] == "TARGET_DEVICE" {
234 ret.Device = fields[4]
235 }
236
237 if strings.HasPrefix(fields[3], "BUILD_BROKEN_") {
238 for i, setting := range buildBrokenSettings {
239 if setting.name == fields[3] {
240 ret.BuildBroken[i] = ParseBoolPtr(fields[4])
241 }
242 }
243 }
244 }
245
246 stdLog, err := ioutil.ReadFile(filepath.Join(p.Branch, p.Name, "std_full.log"))
247 if err != nil {
248 log.Fatal(err)
249 }
250 stdStr := string(stdLog)
251
252 for i, setting := range buildBrokenSettings {
253 for _, warning := range setting.warnings {
254 if strings.Contains(stdStr, warning) {
255 ret.HasBroken[i] = true
256 }
257 }
258 }
259
260 return ret
261}
262
263func ParseBoolPtr(str string) *bool {
264 var ret *bool
265 if str != "" {
266 b := str == "true"
267 ret = &b
268 }
269 return ret
270}