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