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