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