Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 | package main |
| 16 | |
| 17 | import ( |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 18 | "bufio" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 19 | "context" |
| 20 | "flag" |
| 21 | "fmt" |
Dan Willemsen | 4153838 | 2018-08-31 19:51:35 -0700 | [diff] [blame] | 22 | "io" |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 23 | "io/ioutil" |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 24 | "log" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 25 | "os" |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 26 | "os/exec" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 27 | "path/filepath" |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 28 | "regexp" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 29 | "runtime" |
| 30 | "strings" |
| 31 | "sync" |
Dan Willemsen | 22de216 | 2017-12-11 14:35:23 -0800 | [diff] [blame] | 32 | "syscall" |
Steven Moreland | 552432e | 2017-03-29 19:26:09 -0700 | [diff] [blame] | 33 | "time" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 34 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 35 | "android/soong/ui/logger" |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 36 | "android/soong/ui/signal" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 37 | "android/soong/ui/status" |
| 38 | "android/soong/ui/terminal" |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 39 | "android/soong/ui/tracer" |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 40 | "android/soong/zip" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 41 | ) |
| 42 | |
Dan Willemsen | 1bdbdec | 2019-12-27 09:54:11 -0800 | [diff] [blame] | 43 | var numJobs = flag.Int("j", 0, "number of parallel jobs [0=autodetect]") |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 44 | |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 45 | var keepArtifacts = flag.Bool("keep", false, "keep archives of artifacts") |
Dan Willemsen | 4153838 | 2018-08-31 19:51:35 -0700 | [diff] [blame] | 46 | var incremental = flag.Bool("incremental", false, "run in incremental mode (saving intermediates)") |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 47 | |
| 48 | var outDir = flag.String("out", "", "path to store output directories (defaults to tmpdir under $OUT when empty)") |
Dan Willemsen | f624fb9 | 2017-05-19 16:39:04 -0700 | [diff] [blame] | 49 | var alternateResultDir = flag.Bool("dist", false, "write select results to $DIST_DIR (or <out>/dist when empty)") |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 50 | |
| 51 | var onlyConfig = flag.Bool("only-config", false, "Only run product config (not Soong or Kati)") |
| 52 | var onlySoong = flag.Bool("only-soong", false, "Only run product config and Soong (not Kati)") |
| 53 | |
Dan Willemsen | 5ed900b | 2017-05-07 11:40:30 -0700 | [diff] [blame] | 54 | var buildVariant = flag.String("variant", "eng", "build variant to use") |
| 55 | |
Dan Willemsen | 9609ad9 | 2019-12-05 15:22:41 -0800 | [diff] [blame] | 56 | var shardCount = flag.Int("shard-count", 1, "split the products into multiple shards (to spread the build onto multiple machines, etc)") |
| 57 | var shard = flag.Int("shard", 1, "1-indexed shard to execute") |
| 58 | |
Colin Cross | f2f3d31 | 2020-12-17 11:29:31 -0800 | [diff] [blame] | 59 | var skipProducts multipleStringArg |
| 60 | var includeProducts multipleStringArg |
| 61 | |
| 62 | func init() { |
| 63 | flag.Var(&skipProducts, "skip-products", "comma-separated list of products to skip (known failures, etc)") |
| 64 | flag.Var(&includeProducts, "products", "comma-separated list of products to build") |
| 65 | } |
| 66 | |
| 67 | // multipleStringArg is a flag.Value that takes comma separated lists and converts them to a |
| 68 | // []string. The argument can be passed multiple times to append more values. |
| 69 | type multipleStringArg []string |
| 70 | |
| 71 | func (m *multipleStringArg) String() string { |
| 72 | return strings.Join(*m, `, `) |
| 73 | } |
| 74 | |
| 75 | func (m *multipleStringArg) Set(s string) error { |
| 76 | *m = append(*m, strings.Split(s, ",")...) |
| 77 | return nil |
| 78 | } |
| 79 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 80 | const errorLeadingLines = 20 |
| 81 | const errorTrailingLines = 20 |
| 82 | |
| 83 | func errMsgFromLog(filename string) string { |
| 84 | if filename == "" { |
| 85 | return "" |
| 86 | } |
| 87 | |
| 88 | data, err := ioutil.ReadFile(filename) |
| 89 | if err != nil { |
| 90 | return "" |
| 91 | } |
| 92 | |
| 93 | lines := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 94 | if len(lines) > errorLeadingLines+errorTrailingLines+1 { |
| 95 | lines[errorLeadingLines] = fmt.Sprintf("... skipping %d lines ...", |
| 96 | len(lines)-errorLeadingLines-errorTrailingLines) |
| 97 | |
| 98 | lines = append(lines[:errorLeadingLines+1], |
| 99 | lines[len(lines)-errorTrailingLines:]...) |
| 100 | } |
| 101 | var buf strings.Builder |
| 102 | for _, line := range lines { |
| 103 | buf.WriteString("> ") |
| 104 | buf.WriteString(line) |
| 105 | buf.WriteString("\n") |
| 106 | } |
| 107 | return buf.String() |
| 108 | } |
| 109 | |
Dan Willemsen | 22de216 | 2017-12-11 14:35:23 -0800 | [diff] [blame] | 110 | // TODO(b/70370883): This tool uses a lot of open files -- over the default |
| 111 | // soft limit of 1024 on some systems. So bump up to the hard limit until I fix |
| 112 | // the algorithm. |
| 113 | func setMaxFiles(log logger.Logger) { |
| 114 | var limits syscall.Rlimit |
| 115 | |
| 116 | err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits) |
| 117 | if err != nil { |
| 118 | log.Println("Failed to get file limit:", err) |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | log.Verbosef("Current file limits: %d soft, %d hard", limits.Cur, limits.Max) |
| 123 | if limits.Cur == limits.Max { |
| 124 | return |
| 125 | } |
| 126 | |
| 127 | limits.Cur = limits.Max |
| 128 | err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits) |
| 129 | if err != nil { |
| 130 | log.Println("Failed to increase file limit:", err) |
| 131 | } |
| 132 | } |
| 133 | |
Jeff Gaston | b61e3f7 | 2017-10-25 15:02:45 -0700 | [diff] [blame] | 134 | func inList(str string, list []string) bool { |
| 135 | for _, other := range list { |
| 136 | if str == other { |
| 137 | return true |
| 138 | } |
| 139 | } |
| 140 | return false |
| 141 | } |
| 142 | |
Dan Willemsen | 4153838 | 2018-08-31 19:51:35 -0700 | [diff] [blame] | 143 | func copyFile(from, to string) error { |
| 144 | fromFile, err := os.Open(from) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | defer fromFile.Close() |
| 149 | |
| 150 | toFile, err := os.Create(to) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | defer toFile.Close() |
| 155 | |
| 156 | _, err = io.Copy(toFile, fromFile) |
| 157 | return err |
| 158 | } |
| 159 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 160 | type mpContext struct { |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 161 | Logger logger.Logger |
| 162 | Status status.ToolStatus |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 163 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 164 | SoongUi string |
| 165 | MainOutDir string |
| 166 | MainLogsDir string |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 169 | func findNamedProducts(soongUi string, log logger.Logger) []string { |
| 170 | cmd := exec.Command(soongUi, "--dumpvars-mode", "--vars=all_named_products") |
| 171 | output, err := cmd.Output() |
| 172 | if err != nil { |
| 173 | log.Fatalf("Cannot determine named products: %v", err) |
| 174 | } |
| 175 | |
| 176 | rx := regexp.MustCompile(`^all_named_products='(.*)'$`) |
| 177 | match := rx.FindStringSubmatch(strings.TrimSpace(string(output))) |
| 178 | return strings.Fields(match[1]) |
| 179 | } |
| 180 | |
| 181 | // ensureEmptyFileExists ensures that the containing directory exists, and the |
| 182 | // specified file exists. If it doesn't exist, it will write an empty file. |
| 183 | func ensureEmptyFileExists(file string, log logger.Logger) { |
| 184 | if _, err := os.Stat(file); os.IsNotExist(err) { |
| 185 | f, err := os.Create(file) |
| 186 | if err != nil { |
| 187 | log.Fatalf("Error creating %s: %q\n", file, err) |
| 188 | } |
| 189 | f.Close() |
| 190 | } else if err != nil { |
| 191 | log.Fatalf("Error checking %s: %q\n", file, err) |
| 192 | } |
| 193 | } |
| 194 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 195 | func outDirBase() string { |
| 196 | outDirBase := os.Getenv("OUT_DIR") |
| 197 | if outDirBase == "" { |
| 198 | return "out" |
| 199 | } else { |
| 200 | return outDirBase |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func distDir(outDir string) string { |
| 205 | if distDir := os.Getenv("DIST_DIR"); distDir != "" { |
| 206 | return filepath.Clean(distDir) |
| 207 | } else { |
| 208 | return filepath.Join(outDir, "dist") |
| 209 | } |
| 210 | } |
| 211 | |
Colin Cross | 3c0fe0e | 2021-02-10 13:11:18 -0800 | [diff] [blame] | 212 | func forceAnsiOutput() bool { |
| 213 | value := os.Getenv("SOONG_UI_ANSI_OUTPUT") |
| 214 | return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true" |
| 215 | } |
| 216 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 217 | func main() { |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 218 | stdio := terminal.StdioImpl{} |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 219 | |
Colin Cross | 3c0fe0e | 2021-02-10 13:11:18 -0800 | [diff] [blame] | 220 | output := terminal.NewStatusOutput(stdio.Stdout(), "", false, false, |
| 221 | forceAnsiOutput()) |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 222 | log := logger.New(output) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 223 | defer log.Cleanup() |
| 224 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 225 | for _, v := range os.Environ() { |
| 226 | log.Println("Environment: " + v) |
| 227 | } |
| 228 | |
| 229 | log.Printf("Argv: %v\n", os.Args) |
| 230 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 231 | flag.Parse() |
| 232 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 233 | _, cancel := context.WithCancel(context.Background()) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 234 | defer cancel() |
| 235 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 236 | trace := tracer.New(log) |
| 237 | defer trace.Close() |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 238 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 239 | stat := &status.Status{} |
| 240 | defer stat.Finish() |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 241 | stat.AddOutput(output) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 242 | |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 243 | var failures failureCount |
| 244 | stat.AddOutput(&failures) |
| 245 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 246 | signal.SetupSignals(log, cancel, func() { |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 247 | trace.Close() |
| 248 | log.Cleanup() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 249 | stat.Finish() |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 250 | }) |
| 251 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 252 | soongUi := "build/soong/soong_ui.bash" |
| 253 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 254 | var outputDir string |
| 255 | if *outDir != "" { |
| 256 | outputDir = *outDir |
| 257 | } else { |
Dan Willemsen | 4153838 | 2018-08-31 19:51:35 -0700 | [diff] [blame] | 258 | name := "multiproduct" |
| 259 | if !*incremental { |
| 260 | name += "-" + time.Now().Format("20060102150405") |
| 261 | } |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 262 | outputDir = filepath.Join(outDirBase(), name) |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | log.Println("Output directory:", outputDir) |
| 266 | |
| 267 | // The ninja_build file is used by our buildbots to understand that the output |
| 268 | // can be parsed as ninja output. |
| 269 | if err := os.MkdirAll(outputDir, 0777); err != nil { |
| 270 | log.Fatalf("Failed to create output directory: %v", err) |
| 271 | } |
| 272 | ensureEmptyFileExists(filepath.Join(outputDir, "ninja_build"), log) |
| 273 | |
| 274 | logsDir := filepath.Join(outputDir, "logs") |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 275 | os.MkdirAll(logsDir, 0777) |
| 276 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 277 | var configLogsDir string |
| 278 | if *alternateResultDir { |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 279 | configLogsDir = filepath.Join(distDir(outDirBase()), "logs") |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 280 | } else { |
| 281 | configLogsDir = outputDir |
| 282 | } |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 283 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 284 | log.Println("Logs dir: " + configLogsDir) |
| 285 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 286 | os.MkdirAll(configLogsDir, 0777) |
| 287 | log.SetOutput(filepath.Join(configLogsDir, "soong.log")) |
| 288 | trace.SetOutput(filepath.Join(configLogsDir, "build.trace")) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 289 | |
Dan Willemsen | 1bdbdec | 2019-12-27 09:54:11 -0800 | [diff] [blame] | 290 | var jobs = *numJobs |
| 291 | if jobs < 1 { |
| 292 | jobs = runtime.NumCPU() / 4 |
| 293 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 294 | ramGb := int(detectTotalRAM() / (1024 * 1024 * 1024)) |
Colin Cross | 5cb7366 | 2021-10-18 09:54:11 -0700 | [diff] [blame] | 295 | if ramJobs := ramGb / 30; ramGb > 0 && jobs > ramJobs { |
Dan Willemsen | 1bdbdec | 2019-12-27 09:54:11 -0800 | [diff] [blame] | 296 | jobs = ramJobs |
| 297 | } |
| 298 | |
| 299 | if jobs < 1 { |
| 300 | jobs = 1 |
| 301 | } |
| 302 | } |
| 303 | log.Verbosef("Using %d parallel jobs", jobs) |
| 304 | |
Dan Willemsen | 22de216 | 2017-12-11 14:35:23 -0800 | [diff] [blame] | 305 | setMaxFiles(log) |
| 306 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 307 | allProducts := findNamedProducts(soongUi, log) |
Jeff Gaston | b61e3f7 | 2017-10-25 15:02:45 -0700 | [diff] [blame] | 308 | var productsList []string |
Jeff Gaston | b61e3f7 | 2017-10-25 15:02:45 -0700 | [diff] [blame] | 309 | |
Colin Cross | f2f3d31 | 2020-12-17 11:29:31 -0800 | [diff] [blame] | 310 | if len(includeProducts) > 0 { |
| 311 | var missingProducts []string |
| 312 | for _, product := range includeProducts { |
Jeff Gaston | b61e3f7 | 2017-10-25 15:02:45 -0700 | [diff] [blame] | 313 | if inList(product, allProducts) { |
| 314 | productsList = append(productsList, product) |
| 315 | } else { |
| 316 | missingProducts = append(missingProducts, product) |
| 317 | } |
| 318 | } |
| 319 | if len(missingProducts) > 0 { |
| 320 | log.Fatalf("Products don't exist: %s\n", missingProducts) |
| 321 | } |
| 322 | } else { |
| 323 | productsList = allProducts |
| 324 | } |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 325 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 326 | finalProductsList := make([]string, 0, len(productsList)) |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 327 | skipProduct := func(p string) bool { |
Colin Cross | f2f3d31 | 2020-12-17 11:29:31 -0800 | [diff] [blame] | 328 | for _, s := range skipProducts { |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 329 | if p == s { |
| 330 | return true |
| 331 | } |
| 332 | } |
| 333 | return false |
| 334 | } |
| 335 | for _, product := range productsList { |
| 336 | if !skipProduct(product) { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 337 | finalProductsList = append(finalProductsList, product) |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 338 | } else { |
| 339 | log.Verbose("Skipping: ", product) |
| 340 | } |
| 341 | } |
| 342 | |
Dan Willemsen | 9609ad9 | 2019-12-05 15:22:41 -0800 | [diff] [blame] | 343 | if *shard < 1 { |
| 344 | log.Fatalf("--shard value must be >= 1, not %d\n", *shard) |
| 345 | } else if *shardCount < 1 { |
| 346 | log.Fatalf("--shard-count value must be >= 1, not %d\n", *shardCount) |
| 347 | } else if *shard > *shardCount { |
| 348 | log.Fatalf("--shard (%d) must not be greater than --shard-count (%d)\n", *shard, |
| 349 | *shardCount) |
| 350 | } else if *shardCount > 1 { |
| 351 | finalProductsList = splitList(finalProductsList, *shardCount)[*shard-1] |
| 352 | } |
| 353 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 354 | log.Verbose("Got product list: ", finalProductsList) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 355 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 356 | s := stat.StartTool() |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 357 | s.SetTotalActions(len(finalProductsList)) |
Dan Willemsen | a4e43a7 | 2017-05-06 16:58:26 -0700 | [diff] [blame] | 358 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 359 | mpCtx := &mpContext{ |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 360 | Logger: log, |
| 361 | Status: s, |
| 362 | SoongUi: soongUi, |
| 363 | MainOutDir: outputDir, |
| 364 | MainLogsDir: logsDir, |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 365 | } |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 366 | |
| 367 | products := make(chan string, len(productsList)) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 368 | go func() { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 369 | defer close(products) |
| 370 | for _, product := range finalProductsList { |
| 371 | products <- product |
| 372 | } |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 373 | }() |
| 374 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 375 | var wg sync.WaitGroup |
Dan Willemsen | 1bdbdec | 2019-12-27 09:54:11 -0800 | [diff] [blame] | 376 | for i := 0; i < jobs; i++ { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 377 | wg.Add(1) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 378 | go func() { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 379 | defer wg.Done() |
| 380 | for { |
| 381 | select { |
| 382 | case product := <-products: |
| 383 | if product == "" { |
| 384 | return |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 385 | } |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 386 | runSoongUiForProduct(mpCtx, product) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 387 | } |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 388 | } |
| 389 | }() |
| 390 | } |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 391 | wg.Wait() |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 392 | |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 393 | if *alternateResultDir { |
| 394 | args := zip.ZipArgs{ |
| 395 | FileArgs: []zip.FileArg{ |
| 396 | {GlobDir: logsDir, SourcePrefixToStrip: logsDir}, |
| 397 | }, |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 398 | OutputFilePath: filepath.Join(distDir(outDirBase()), "logs.zip"), |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 399 | NumParallelJobs: runtime.NumCPU(), |
| 400 | CompressionLevel: 5, |
| 401 | } |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 402 | log.Printf("Logs zip: %v\n", args.OutputFilePath) |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 403 | if err := zip.Zip(args); err != nil { |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 404 | log.Fatalf("Error zipping logs: %v", err) |
| 405 | } |
| 406 | } |
| 407 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 408 | s.Finish() |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 409 | |
Liz Kammer | 124967f | 2022-02-14 15:42:53 -0500 | [diff] [blame^] | 410 | if failures.count == 1 { |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 411 | log.Fatal("1 failure") |
Liz Kammer | 124967f | 2022-02-14 15:42:53 -0500 | [diff] [blame^] | 412 | } else if failures.count > 1 { |
| 413 | log.Fatalf("%d failures %q", failures.count, failures.fails) |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 414 | } else { |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 415 | fmt.Fprintln(output, "Success") |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 416 | } |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 417 | } |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 418 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 419 | func cleanupAfterProduct(outDir, productZip string) { |
| 420 | if *keepArtifacts { |
| 421 | args := zip.ZipArgs{ |
| 422 | FileArgs: []zip.FileArg{ |
| 423 | { |
| 424 | GlobDir: outDir, |
| 425 | SourcePrefixToStrip: outDir, |
| 426 | }, |
| 427 | }, |
| 428 | OutputFilePath: productZip, |
| 429 | NumParallelJobs: runtime.NumCPU(), |
| 430 | CompressionLevel: 5, |
| 431 | } |
| 432 | if err := zip.Zip(args); err != nil { |
| 433 | log.Fatalf("Error zipping artifacts: %v", err) |
| 434 | } |
| 435 | } |
| 436 | if !*incremental { |
| 437 | os.RemoveAll(outDir) |
| 438 | } |
| 439 | } |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 440 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 441 | func runSoongUiForProduct(mpctx *mpContext, product string) { |
| 442 | outDir := filepath.Join(mpctx.MainOutDir, product) |
| 443 | logsDir := filepath.Join(mpctx.MainLogsDir, product) |
| 444 | productZip := filepath.Join(mpctx.MainOutDir, product+".zip") |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 445 | consoleLogPath := filepath.Join(logsDir, "std.log") |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 446 | |
| 447 | if err := os.MkdirAll(outDir, 0777); err != nil { |
| 448 | mpctx.Logger.Fatalf("Error creating out directory: %v", err) |
| 449 | } |
| 450 | if err := os.MkdirAll(logsDir, 0777); err != nil { |
| 451 | mpctx.Logger.Fatalf("Error creating log directory: %v", err) |
| 452 | } |
| 453 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 454 | consoleLogFile, err := os.Create(consoleLogPath) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 455 | if err != nil { |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 456 | mpctx.Logger.Fatalf("Error creating console log file: %v", err) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 457 | } |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 458 | defer consoleLogFile.Close() |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 459 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 460 | consoleLogWriter := bufio.NewWriter(consoleLogFile) |
| 461 | defer consoleLogWriter.Flush() |
| 462 | |
| 463 | args := []string{"--make-mode", "--skip-soong-tests", "--skip-ninja"} |
| 464 | |
| 465 | if !*keepArtifacts { |
| 466 | args = append(args, "--empty-ninja-file") |
| 467 | } |
| 468 | |
| 469 | if *onlyConfig { |
| 470 | args = append(args, "--config-only") |
| 471 | } else if *onlySoong { |
| 472 | args = append(args, "--soong-only") |
| 473 | } |
| 474 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 475 | cmd := exec.Command(mpctx.SoongUi, args...) |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 476 | cmd.Stdout = consoleLogWriter |
| 477 | cmd.Stderr = consoleLogWriter |
| 478 | cmd.Env = append(os.Environ(), |
| 479 | "OUT_DIR="+outDir, |
| 480 | "TARGET_PRODUCT="+product, |
| 481 | "TARGET_BUILD_VARIANT="+*buildVariant, |
| 482 | "TARGET_BUILD_TYPE=release", |
| 483 | "TARGET_BUILD_APPS=", |
| 484 | "TARGET_BUILD_UNBUNDLED=") |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 485 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 486 | if *alternateResultDir { |
| 487 | cmd.Env = append(cmd.Env, |
| 488 | "DIST_DIR="+filepath.Join(distDir(outDirBase()), "products/"+product)) |
| 489 | } |
| 490 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 491 | action := &status.Action{ |
| 492 | Description: product, |
| 493 | Outputs: []string{product}, |
| 494 | } |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 495 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 496 | mpctx.Status.StartAction(action) |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 497 | defer cleanupAfterProduct(outDir, productZip) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 498 | |
| 499 | before := time.Now() |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 500 | err = cmd.Run() |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 501 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 502 | if !*onlyConfig && !*onlySoong { |
| 503 | katiBuildNinjaFile := filepath.Join(outDir, "build-"+product+".ninja") |
| 504 | if after, err := os.Stat(katiBuildNinjaFile); err == nil && after.ModTime().After(before) { |
| 505 | err := copyFile(consoleLogPath, filepath.Join(filepath.Dir(consoleLogPath), "std_full.log")) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 506 | if err != nil { |
| 507 | log.Fatalf("Error copying log file: %s", err) |
| 508 | } |
| 509 | } |
| 510 | } |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 511 | var errOutput string |
| 512 | if err == nil { |
| 513 | errOutput = "" |
| 514 | } else { |
| 515 | errOutput = errMsgFromLog(consoleLogPath) |
| 516 | } |
| 517 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 518 | mpctx.Status.FinishAction(status.ActionResult{ |
| 519 | Action: action, |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 520 | Error: err, |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 521 | Output: errOutput, |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 522 | }) |
| 523 | } |
| 524 | |
Liz Kammer | 124967f | 2022-02-14 15:42:53 -0500 | [diff] [blame^] | 525 | type failureCount struct { |
| 526 | count int |
| 527 | fails []string |
| 528 | } |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 529 | |
| 530 | func (f *failureCount) StartAction(action *status.Action, counts status.Counts) {} |
| 531 | |
| 532 | func (f *failureCount) FinishAction(result status.ActionResult, counts status.Counts) { |
| 533 | if result.Error != nil { |
Liz Kammer | 124967f | 2022-02-14 15:42:53 -0500 | [diff] [blame^] | 534 | f.count += 1 |
| 535 | f.fails = append(f.fails, result.Action.Description) |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | func (f *failureCount) Message(level status.MsgLevel, message string) { |
| 540 | if level >= status.ErrorLvl { |
Liz Kammer | 124967f | 2022-02-14 15:42:53 -0500 | [diff] [blame^] | 541 | f.count += 1 |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | func (f *failureCount) Flush() {} |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 546 | |
| 547 | func (f *failureCount) Write(p []byte) (int, error) { |
| 548 | // discard writes |
| 549 | return len(p), nil |
| 550 | } |
Dan Willemsen | 9609ad9 | 2019-12-05 15:22:41 -0800 | [diff] [blame] | 551 | |
| 552 | func splitList(list []string, shardCount int) (ret [][]string) { |
| 553 | each := len(list) / shardCount |
| 554 | extra := len(list) % shardCount |
| 555 | for i := 0; i < shardCount; i++ { |
| 556 | count := each |
| 557 | if extra > 0 { |
| 558 | count += 1 |
| 559 | extra -= 1 |
| 560 | } |
| 561 | ret = append(ret, list[:count]) |
| 562 | list = list[count:] |
| 563 | } |
| 564 | return |
| 565 | } |