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 detectTotalRAM() uint64 { |
| 170 | var info syscall.Sysinfo_t |
| 171 | err := syscall.Sysinfo(&info) |
| 172 | if err != nil { |
| 173 | panic(err) |
| 174 | } |
| 175 | return info.Totalram * uint64(info.Unit) |
| 176 | } |
| 177 | |
| 178 | func findNamedProducts(soongUi string, log logger.Logger) []string { |
| 179 | cmd := exec.Command(soongUi, "--dumpvars-mode", "--vars=all_named_products") |
| 180 | output, err := cmd.Output() |
| 181 | if err != nil { |
| 182 | log.Fatalf("Cannot determine named products: %v", err) |
| 183 | } |
| 184 | |
| 185 | rx := regexp.MustCompile(`^all_named_products='(.*)'$`) |
| 186 | match := rx.FindStringSubmatch(strings.TrimSpace(string(output))) |
| 187 | return strings.Fields(match[1]) |
| 188 | } |
| 189 | |
| 190 | // ensureEmptyFileExists ensures that the containing directory exists, and the |
| 191 | // specified file exists. If it doesn't exist, it will write an empty file. |
| 192 | func ensureEmptyFileExists(file string, log logger.Logger) { |
| 193 | if _, err := os.Stat(file); os.IsNotExist(err) { |
| 194 | f, err := os.Create(file) |
| 195 | if err != nil { |
| 196 | log.Fatalf("Error creating %s: %q\n", file, err) |
| 197 | } |
| 198 | f.Close() |
| 199 | } else if err != nil { |
| 200 | log.Fatalf("Error checking %s: %q\n", file, err) |
| 201 | } |
| 202 | } |
| 203 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 204 | func outDirBase() string { |
| 205 | outDirBase := os.Getenv("OUT_DIR") |
| 206 | if outDirBase == "" { |
| 207 | return "out" |
| 208 | } else { |
| 209 | return outDirBase |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | func distDir(outDir string) string { |
| 214 | if distDir := os.Getenv("DIST_DIR"); distDir != "" { |
| 215 | return filepath.Clean(distDir) |
| 216 | } else { |
| 217 | return filepath.Join(outDir, "dist") |
| 218 | } |
| 219 | } |
| 220 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 221 | func main() { |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 222 | stdio := terminal.StdioImpl{} |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 223 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 224 | output := terminal.NewStatusOutput(stdio.Stdout(), "", false, false) |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 225 | log := logger.New(output) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 226 | defer log.Cleanup() |
| 227 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 228 | for _, v := range os.Environ() { |
| 229 | log.Println("Environment: " + v) |
| 230 | } |
| 231 | |
| 232 | log.Printf("Argv: %v\n", os.Args) |
| 233 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 234 | flag.Parse() |
| 235 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 236 | _, cancel := context.WithCancel(context.Background()) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 237 | defer cancel() |
| 238 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 239 | trace := tracer.New(log) |
| 240 | defer trace.Close() |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 241 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 242 | stat := &status.Status{} |
| 243 | defer stat.Finish() |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 244 | stat.AddOutput(output) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 245 | |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 246 | var failures failureCount |
| 247 | stat.AddOutput(&failures) |
| 248 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 249 | signal.SetupSignals(log, cancel, func() { |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 250 | trace.Close() |
| 251 | log.Cleanup() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 252 | stat.Finish() |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 253 | }) |
| 254 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 255 | soongUi := "build/soong/soong_ui.bash" |
| 256 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 257 | var outputDir string |
| 258 | if *outDir != "" { |
| 259 | outputDir = *outDir |
| 260 | } else { |
Dan Willemsen | 4153838 | 2018-08-31 19:51:35 -0700 | [diff] [blame] | 261 | name := "multiproduct" |
| 262 | if !*incremental { |
| 263 | name += "-" + time.Now().Format("20060102150405") |
| 264 | } |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 265 | outputDir = filepath.Join(outDirBase(), name) |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | log.Println("Output directory:", outputDir) |
| 269 | |
| 270 | // The ninja_build file is used by our buildbots to understand that the output |
| 271 | // can be parsed as ninja output. |
| 272 | if err := os.MkdirAll(outputDir, 0777); err != nil { |
| 273 | log.Fatalf("Failed to create output directory: %v", err) |
| 274 | } |
| 275 | ensureEmptyFileExists(filepath.Join(outputDir, "ninja_build"), log) |
| 276 | |
| 277 | logsDir := filepath.Join(outputDir, "logs") |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 278 | os.MkdirAll(logsDir, 0777) |
| 279 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 280 | var configLogsDir string |
| 281 | if *alternateResultDir { |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 282 | configLogsDir = filepath.Join(distDir(outDirBase()), "logs") |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 283 | } else { |
| 284 | configLogsDir = outputDir |
| 285 | } |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 286 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 287 | log.Println("Logs dir: " + configLogsDir) |
| 288 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 289 | os.MkdirAll(configLogsDir, 0777) |
| 290 | log.SetOutput(filepath.Join(configLogsDir, "soong.log")) |
| 291 | trace.SetOutput(filepath.Join(configLogsDir, "build.trace")) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 292 | |
Dan Willemsen | 1bdbdec | 2019-12-27 09:54:11 -0800 | [diff] [blame] | 293 | var jobs = *numJobs |
| 294 | if jobs < 1 { |
| 295 | jobs = runtime.NumCPU() / 4 |
| 296 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 297 | ramGb := int(detectTotalRAM() / (1024 * 1024 * 1024)) |
Colin Cross | f04fe9a | 2021-01-26 14:03:21 -0800 | [diff] [blame] | 298 | if ramJobs := ramGb / 25; ramGb > 0 && jobs > ramJobs { |
Dan Willemsen | 1bdbdec | 2019-12-27 09:54:11 -0800 | [diff] [blame] | 299 | jobs = ramJobs |
| 300 | } |
| 301 | |
| 302 | if jobs < 1 { |
| 303 | jobs = 1 |
| 304 | } |
| 305 | } |
| 306 | log.Verbosef("Using %d parallel jobs", jobs) |
| 307 | |
Dan Willemsen | 22de216 | 2017-12-11 14:35:23 -0800 | [diff] [blame] | 308 | setMaxFiles(log) |
| 309 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 310 | allProducts := findNamedProducts(soongUi, log) |
Jeff Gaston | b61e3f7 | 2017-10-25 15:02:45 -0700 | [diff] [blame] | 311 | var productsList []string |
Jeff Gaston | b61e3f7 | 2017-10-25 15:02:45 -0700 | [diff] [blame] | 312 | |
Colin Cross | f2f3d31 | 2020-12-17 11:29:31 -0800 | [diff] [blame] | 313 | if len(includeProducts) > 0 { |
| 314 | var missingProducts []string |
| 315 | for _, product := range includeProducts { |
Jeff Gaston | b61e3f7 | 2017-10-25 15:02:45 -0700 | [diff] [blame] | 316 | if inList(product, allProducts) { |
| 317 | productsList = append(productsList, product) |
| 318 | } else { |
| 319 | missingProducts = append(missingProducts, product) |
| 320 | } |
| 321 | } |
| 322 | if len(missingProducts) > 0 { |
| 323 | log.Fatalf("Products don't exist: %s\n", missingProducts) |
| 324 | } |
| 325 | } else { |
| 326 | productsList = allProducts |
| 327 | } |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 328 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 329 | finalProductsList := make([]string, 0, len(productsList)) |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 330 | skipProduct := func(p string) bool { |
Colin Cross | f2f3d31 | 2020-12-17 11:29:31 -0800 | [diff] [blame] | 331 | for _, s := range skipProducts { |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 332 | if p == s { |
| 333 | return true |
| 334 | } |
| 335 | } |
| 336 | return false |
| 337 | } |
| 338 | for _, product := range productsList { |
| 339 | if !skipProduct(product) { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 340 | finalProductsList = append(finalProductsList, product) |
Dan Willemsen | 9957b9c | 2017-10-06 15:05:05 -0700 | [diff] [blame] | 341 | } else { |
| 342 | log.Verbose("Skipping: ", product) |
| 343 | } |
| 344 | } |
| 345 | |
Dan Willemsen | 9609ad9 | 2019-12-05 15:22:41 -0800 | [diff] [blame] | 346 | if *shard < 1 { |
| 347 | log.Fatalf("--shard value must be >= 1, not %d\n", *shard) |
| 348 | } else if *shardCount < 1 { |
| 349 | log.Fatalf("--shard-count value must be >= 1, not %d\n", *shardCount) |
| 350 | } else if *shard > *shardCount { |
| 351 | log.Fatalf("--shard (%d) must not be greater than --shard-count (%d)\n", *shard, |
| 352 | *shardCount) |
| 353 | } else if *shardCount > 1 { |
| 354 | finalProductsList = splitList(finalProductsList, *shardCount)[*shard-1] |
| 355 | } |
| 356 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 357 | log.Verbose("Got product list: ", finalProductsList) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 358 | |
Lukacs T. Berki | 2c40569 | 2021-08-11 09:19:27 +0200 | [diff] [blame] | 359 | s := stat.StartTool() |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 360 | s.SetTotalActions(len(finalProductsList)) |
Dan Willemsen | a4e43a7 | 2017-05-06 16:58:26 -0700 | [diff] [blame] | 361 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 362 | mpCtx := &mpContext{ |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 363 | Logger: log, |
| 364 | Status: s, |
| 365 | SoongUi: soongUi, |
| 366 | MainOutDir: outputDir, |
| 367 | MainLogsDir: logsDir, |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 368 | } |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 369 | |
| 370 | products := make(chan string, len(productsList)) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 371 | go func() { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 372 | defer close(products) |
| 373 | for _, product := range finalProductsList { |
| 374 | products <- product |
| 375 | } |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 376 | }() |
| 377 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 378 | var wg sync.WaitGroup |
Dan Willemsen | 1bdbdec | 2019-12-27 09:54:11 -0800 | [diff] [blame] | 379 | for i := 0; i < jobs; i++ { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 380 | wg.Add(1) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 381 | go func() { |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 382 | defer wg.Done() |
| 383 | for { |
| 384 | select { |
| 385 | case product := <-products: |
| 386 | if product == "" { |
| 387 | return |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 388 | } |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 389 | runSoongUiForProduct(mpCtx, product) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 390 | } |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 391 | } |
| 392 | }() |
| 393 | } |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 394 | wg.Wait() |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 395 | |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 396 | if *alternateResultDir { |
| 397 | args := zip.ZipArgs{ |
| 398 | FileArgs: []zip.FileArg{ |
| 399 | {GlobDir: logsDir, SourcePrefixToStrip: logsDir}, |
| 400 | }, |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 401 | OutputFilePath: filepath.Join(distDir(outDirBase()), "logs.zip"), |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 402 | NumParallelJobs: runtime.NumCPU(), |
| 403 | CompressionLevel: 5, |
| 404 | } |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 405 | log.Printf("Logs zip: %v\n", args.OutputFilePath) |
Colin Cross | 05518bc | 2018-09-27 15:06:19 -0700 | [diff] [blame] | 406 | if err := zip.Zip(args); err != nil { |
Dan Willemsen | e348076 | 2017-11-07 11:23:27 -0800 | [diff] [blame] | 407 | log.Fatalf("Error zipping logs: %v", err) |
| 408 | } |
| 409 | } |
| 410 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 411 | s.Finish() |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 412 | |
| 413 | if failures == 1 { |
| 414 | log.Fatal("1 failure") |
| 415 | } else if failures > 1 { |
| 416 | log.Fatalf("%d failures", failures) |
| 417 | } else { |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 418 | fmt.Fprintln(output, "Success") |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 419 | } |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 420 | } |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 421 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 422 | func cleanupAfterProduct(outDir, productZip string) { |
| 423 | if *keepArtifacts { |
| 424 | args := zip.ZipArgs{ |
| 425 | FileArgs: []zip.FileArg{ |
| 426 | { |
| 427 | GlobDir: outDir, |
| 428 | SourcePrefixToStrip: outDir, |
| 429 | }, |
| 430 | }, |
| 431 | OutputFilePath: productZip, |
| 432 | NumParallelJobs: runtime.NumCPU(), |
| 433 | CompressionLevel: 5, |
| 434 | } |
| 435 | if err := zip.Zip(args); err != nil { |
| 436 | log.Fatalf("Error zipping artifacts: %v", err) |
| 437 | } |
| 438 | } |
| 439 | if !*incremental { |
| 440 | os.RemoveAll(outDir) |
| 441 | } |
| 442 | } |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 443 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 444 | func runSoongUiForProduct(mpctx *mpContext, product string) { |
| 445 | outDir := filepath.Join(mpctx.MainOutDir, product) |
| 446 | logsDir := filepath.Join(mpctx.MainLogsDir, product) |
| 447 | productZip := filepath.Join(mpctx.MainOutDir, product+".zip") |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 448 | consoleLogPath := filepath.Join(logsDir, "std.log") |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 449 | |
| 450 | if err := os.MkdirAll(outDir, 0777); err != nil { |
| 451 | mpctx.Logger.Fatalf("Error creating out directory: %v", err) |
| 452 | } |
| 453 | if err := os.MkdirAll(logsDir, 0777); err != nil { |
| 454 | mpctx.Logger.Fatalf("Error creating log directory: %v", err) |
| 455 | } |
| 456 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 457 | consoleLogFile, err := os.Create(consoleLogPath) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 458 | if err != nil { |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 459 | mpctx.Logger.Fatalf("Error creating console log file: %v", err) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 460 | } |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 461 | defer consoleLogFile.Close() |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 462 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 463 | consoleLogWriter := bufio.NewWriter(consoleLogFile) |
| 464 | defer consoleLogWriter.Flush() |
| 465 | |
| 466 | args := []string{"--make-mode", "--skip-soong-tests", "--skip-ninja"} |
| 467 | |
| 468 | if !*keepArtifacts { |
| 469 | args = append(args, "--empty-ninja-file") |
| 470 | } |
| 471 | |
| 472 | if *onlyConfig { |
| 473 | args = append(args, "--config-only") |
| 474 | } else if *onlySoong { |
| 475 | args = append(args, "--soong-only") |
| 476 | } |
| 477 | |
Lukacs T. Berki | f656b84 | 2021-08-11 11:10:28 +0200 | [diff] [blame] | 478 | cmd := exec.Command(mpctx.SoongUi, args...) |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 479 | cmd.Stdout = consoleLogWriter |
| 480 | cmd.Stderr = consoleLogWriter |
| 481 | cmd.Env = append(os.Environ(), |
| 482 | "OUT_DIR="+outDir, |
| 483 | "TARGET_PRODUCT="+product, |
| 484 | "TARGET_BUILD_VARIANT="+*buildVariant, |
| 485 | "TARGET_BUILD_TYPE=release", |
| 486 | "TARGET_BUILD_APPS=", |
| 487 | "TARGET_BUILD_UNBUNDLED=") |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 488 | |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 489 | if *alternateResultDir { |
| 490 | cmd.Env = append(cmd.Env, |
| 491 | "DIST_DIR="+filepath.Join(distDir(outDirBase()), "products/"+product)) |
| 492 | } |
| 493 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 494 | action := &status.Action{ |
| 495 | Description: product, |
| 496 | Outputs: []string{product}, |
| 497 | } |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 498 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 499 | mpctx.Status.StartAction(action) |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 500 | defer cleanupAfterProduct(outDir, productZip) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 501 | |
| 502 | before := time.Now() |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 503 | err = cmd.Run() |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 504 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 505 | if !*onlyConfig && !*onlySoong { |
| 506 | katiBuildNinjaFile := filepath.Join(outDir, "build-"+product+".ninja") |
| 507 | if after, err := os.Stat(katiBuildNinjaFile); err == nil && after.ModTime().After(before) { |
| 508 | err := copyFile(consoleLogPath, filepath.Join(filepath.Dir(consoleLogPath), "std_full.log")) |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 509 | if err != nil { |
| 510 | log.Fatalf("Error copying log file: %s", err) |
| 511 | } |
| 512 | } |
| 513 | } |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 514 | var errOutput string |
| 515 | if err == nil { |
| 516 | errOutput = "" |
| 517 | } else { |
| 518 | errOutput = errMsgFromLog(consoleLogPath) |
| 519 | } |
| 520 | |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 521 | mpctx.Status.FinishAction(status.ActionResult{ |
| 522 | Action: action, |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 523 | Error: err, |
Lukacs T. Berki | bf5bdb2 | 2021-08-24 10:47:13 +0200 | [diff] [blame] | 524 | Output: errOutput, |
Dan Willemsen | bcc1dbf | 2018-09-04 18:09:47 -0700 | [diff] [blame] | 525 | }) |
| 526 | } |
| 527 | |
Dan Willemsen | 34218ec | 2018-07-19 22:57:18 -0700 | [diff] [blame] | 528 | type failureCount int |
| 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 { |
| 534 | *f += 1 |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | func (f *failureCount) Message(level status.MsgLevel, message string) { |
| 539 | if level >= status.ErrorLvl { |
| 540 | *f += 1 |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | func (f *failureCount) Flush() {} |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 545 | |
| 546 | func (f *failureCount) Write(p []byte) (int, error) { |
| 547 | // discard writes |
| 548 | return len(p), nil |
| 549 | } |
Dan Willemsen | 9609ad9 | 2019-12-05 15:22:41 -0800 | [diff] [blame] | 550 | |
| 551 | func splitList(list []string, shardCount int) (ret [][]string) { |
| 552 | each := len(list) / shardCount |
| 553 | extra := len(list) % shardCount |
| 554 | for i := 0; i < shardCount; i++ { |
| 555 | count := each |
| 556 | if extra > 0 { |
| 557 | count += 1 |
| 558 | extra -= 1 |
| 559 | } |
| 560 | ret = append(ret, list[:count]) |
| 561 | list = list[count:] |
| 562 | } |
| 563 | return |
| 564 | } |