blob: b06e4fe73a85e052ac0471aa6cd2fbb8a54b70a5 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// 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
15package main
16
17import (
18 "context"
Dan Willemsen051133b2017-07-14 11:29:29 -070019 "flag"
20 "fmt"
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +000021 "io/ioutil"
Dan Willemsen1e704462016-08-21 15:17:17 -070022 "os"
23 "path/filepath"
24 "strconv"
25 "strings"
Liz Kammera7541782022-02-07 13:38:52 -050026 "syscall"
Dan Willemsen1e704462016-08-21 15:17:17 -070027 "time"
28
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +010029 "android/soong/shared"
Dan Willemsen1e704462016-08-21 15:17:17 -070030 "android/soong/ui/build"
31 "android/soong/ui/logger"
Nan Zhang17f27672018-12-12 16:01:49 -080032 "android/soong/ui/metrics"
Lukacs T. Berkif656b842021-08-11 11:10:28 +020033 "android/soong/ui/signal"
Dan Willemsenb82471a2018-05-17 16:37:09 -070034 "android/soong/ui/status"
35 "android/soong/ui/terminal"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070036 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070037)
38
Patrice Arrudaa5c25422019-04-09 18:49:49 -070039// A command represents an operation to be executed in the soong build
40// system.
41type command struct {
Patrice Arrudaf445ba12020-07-28 17:49:01 +000042 // The flag name (must have double dashes).
Patrice Arrudaa5c25422019-04-09 18:49:49 -070043 flag string
44
Patrice Arrudaf445ba12020-07-28 17:49:01 +000045 // Description for the flag (to display when running help).
Patrice Arrudaa5c25422019-04-09 18:49:49 -070046 description string
47
Patrice Arrudaf445ba12020-07-28 17:49:01 +000048 // Stream the build status output into the simple terminal mode.
49 simpleOutput bool
Colin Crossc0b9f6b2019-09-23 12:44:54 -070050
51 // Sets a prefix string to use for filenames of log files.
52 logsPrefix string
53
Patrice Arrudaa5c25422019-04-09 18:49:49 -070054 // Creates the build configuration based on the args and build context.
55 config func(ctx build.Context, args ...string) build.Config
56
57 // Returns what type of IO redirection this Command requires.
58 stdio func() terminal.StdioInterface
59
60 // run the command
61 run func(ctx build.Context, config build.Config, args []string, logsDir string)
62}
63
Patrice Arrudaa5c25422019-04-09 18:49:49 -070064// list of supported commands (flags) supported by soong ui
Usta6feae382021-12-13 12:31:50 -050065var commands = []command{
Patrice Arrudaa5c25422019-04-09 18:49:49 -070066 {
Anton Hansson5a7861a2021-06-04 10:09:01 +010067 flag: "--make-mode",
Patrice Arrudaa5c25422019-04-09 18:49:49 -070068 description: "build the modules by the target name (i.e. soong_docs)",
Usta Shrestha59417a12022-08-05 17:14:49 -040069 config: build.NewConfig,
70 stdio: stdio,
71 run: runMake,
Patrice Arrudaa5c25422019-04-09 18:49:49 -070072 }, {
Patrice Arrudaf445ba12020-07-28 17:49:01 +000073 flag: "--dumpvar-mode",
74 description: "print the value of the legacy make variable VAR to stdout",
75 simpleOutput: true,
76 logsPrefix: "dumpvars-",
77 config: dumpVarConfig,
78 stdio: customStdio,
79 run: dumpVar,
Patrice Arrudaa5c25422019-04-09 18:49:49 -070080 }, {
Patrice Arrudaf445ba12020-07-28 17:49:01 +000081 flag: "--dumpvars-mode",
82 description: "dump the values of one or more legacy make variables, in shell syntax",
83 simpleOutput: true,
84 logsPrefix: "dumpvars-",
85 config: dumpVarConfig,
86 stdio: customStdio,
87 run: dumpVars,
Patrice Arrudab7b22822019-05-21 17:46:23 -070088 }, {
89 flag: "--build-mode",
90 description: "build modules based on the specified build action",
91 config: buildActionConfig,
92 stdio: stdio,
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010093 run: runMake,
Patrice Arrudaa5c25422019-04-09 18:49:49 -070094 },
95}
96
97// indexList returns the index of first found s. -1 is return if s is not
98// found.
Dan Willemsen1e704462016-08-21 15:17:17 -070099func indexList(s string, list []string) int {
100 for i, l := range list {
101 if l == s {
102 return i
103 }
104 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700105 return -1
106}
107
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700108// inList returns true if one or more of s is in the list.
Dan Willemsen1e704462016-08-21 15:17:17 -0700109func inList(s string, list []string) bool {
110 return indexList(s, list) != -1
111}
112
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700113// Main execution of soong_ui. The command format is as follows:
114//
Usta Shrestha59417a12022-08-05 17:14:49 -0400115// soong_ui <command> [<arg 1> <arg 2> ... <arg n>]
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700116//
117// Command is the type of soong_ui execution. Only one type of
118// execution is specified. The args are specific to the command.
Dan Willemsen1e704462016-08-21 15:17:17 -0700119func main() {
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +0100120 shared.ReexecWithDelveMaybe(os.Getenv("SOONG_UI_DELVE"), shared.ResolveDelveBinary())
121
Patrice Arruda73c790f2020-07-13 23:01:18 +0000122 buildStarted := time.Now()
Patrice Arruda219eef32020-06-01 17:29:30 +0000123
Liz Kammer0e7993e2020-10-15 11:07:13 -0700124 c, args, err := getCommand(os.Args)
125 if err != nil {
126 fmt.Fprintf(os.Stderr, "Error parsing `soong` args: %s.\n", err)
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700127 os.Exit(1)
Dan Willemsenc35b3812018-07-16 19:59:10 -0700128 }
129
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800130 // Create a terminal output that mimics Ninja's.
Patrice Arrudaf445ba12020-07-28 17:49:01 +0000131 output := terminal.NewStatusOutput(c.stdio().Stdout(), os.Getenv("NINJA_STATUS"), c.simpleOutput,
Colin Cross3c0fe0e2021-02-10 13:11:18 -0800132 build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD"),
133 build.OsEnvironment().IsEnvTrue("SOONG_UI_ANSI_OUTPUT"))
Dan Willemsenb82471a2018-05-17 16:37:09 -0700134
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800135 // Attach a new logger instance to the terminal output.
Colin Crosse0df1a32019-06-09 19:40:08 -0700136 log := logger.New(output)
Dan Willemsen1e704462016-08-21 15:17:17 -0700137 defer log.Cleanup()
138
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800139 // Create a context to simplify the program termination process.
Dan Willemsen1e704462016-08-21 15:17:17 -0700140 ctx, cancel := context.WithCancel(context.Background())
141 defer cancel()
142
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800143 // Create a new trace file writer, making it log events to the log instance.
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700144 trace := tracer.New(log)
145 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -0700146
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800147 // Create and start a new metric record.
Nan Zhang17f27672018-12-12 16:01:49 -0800148 met := metrics.New()
Patrice Arruda73c790f2020-07-13 23:01:18 +0000149 met.SetBuildDateTime(buildStarted)
Patrice Arrudae92c30d2020-10-29 11:01:32 -0700150 met.SetBuildCommand(os.Args)
Nan Zhang17f27672018-12-12 16:01:49 -0800151
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800152 // Create a new Status instance, which manages action counts and event output channels.
Dan Willemsenb82471a2018-05-17 16:37:09 -0700153 stat := &status.Status{}
154 defer stat.Finish()
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800155 // Hook up the terminal output and tracer to Status.
Colin Crosse0df1a32019-06-09 19:40:08 -0700156 stat.AddOutput(output)
Dan Willemsenb82471a2018-05-17 16:37:09 -0700157 stat.AddOutput(trace.StatusTracer())
158
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800159 // Set up a cleanup procedure in case the normal termination process doesn't work.
Lukacs T. Berkif656b842021-08-11 11:10:28 +0200160 signal.SetupSignals(log, cancel, func() {
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700161 trace.Close()
162 log.Cleanup()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700163 stat.Finish()
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700164 })
165
Dan Willemsen59339a22018-07-22 21:18:45 -0700166 buildCtx := build.Context{ContextImpl: &build.ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -0700167 Context: ctx,
168 Logger: log,
Nan Zhang17f27672018-12-12 16:01:49 -0800169 Metrics: met,
Dan Willemsenb82471a2018-05-17 16:37:09 -0700170 Tracer: trace,
Colin Crosse0df1a32019-06-09 19:40:08 -0700171 Writer: output,
Dan Willemsenb82471a2018-05-17 16:37:09 -0700172 Status: stat,
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700173 }}
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700174
Kousik Kumar7b7dca42022-01-14 00:22:32 -0500175 config := c.config(buildCtx, args...)
176
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700177 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -0800178
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800179 // Set up files to be outputted in the log directory.
Patrice Arruda83842d72020-12-08 19:42:08 +0000180 logsDir := config.LogsDir()
Dan Willemsen1e704462016-08-21 15:17:17 -0700181
Patrice Arruda40564022020-12-10 00:42:58 +0000182 // Common list of metric file definition.
Patrice Arruda219eef32020-06-01 17:29:30 +0000183 buildErrorFile := filepath.Join(logsDir, c.logsPrefix+"build_error")
184 rbeMetricsFile := filepath.Join(logsDir, c.logsPrefix+"rbe_metrics.pb")
185 soongMetricsFile := filepath.Join(logsDir, c.logsPrefix+"soong_metrics")
Patrice Arruda40564022020-12-10 00:42:58 +0000186
Kousik Kumara0a44a82020-10-08 02:33:29 -0400187 build.PrintOutDirWarning(buildCtx, config)
Patrice Arruda219eef32020-06-01 17:29:30 +0000188
Dan Willemsenb82471a2018-05-17 16:37:09 -0700189 os.MkdirAll(logsDir, 0777)
Colin Crossc0b9f6b2019-09-23 12:44:54 -0700190 log.SetOutput(filepath.Join(logsDir, c.logsPrefix+"soong.log"))
191 trace.SetOutput(filepath.Join(logsDir, c.logsPrefix+"build.trace"))
192 stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, c.logsPrefix+"verbose.log")))
193 stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, c.logsPrefix+"error.log")))
Patrice Arruda219eef32020-06-01 17:29:30 +0000194 stat.AddOutput(status.NewProtoErrorLog(log, buildErrorFile))
Colin Cross7b624532019-06-21 15:08:30 -0700195 stat.AddOutput(status.NewCriticalPath(log))
Patrice Arruda74b43992020-03-11 08:21:05 -0700196 stat.AddOutput(status.NewBuildProgressLog(log, filepath.Join(logsDir, c.logsPrefix+"build_progress.pb")))
Dan Willemsenb82471a2018-05-17 16:37:09 -0700197
Colin Cross8b8bec32019-11-15 13:18:43 -0800198 buildCtx.Verbosef("Detected %.3v GB total RAM", float32(config.TotalRAM())/(1024*1024*1024))
199 buildCtx.Verbosef("Parallelism (local/remote/highmem): %v/%v/%v",
200 config.Parallel(), config.RemoteParallel(), config.HighmemParallel())
201
Liz Kammer4ae119c2022-02-09 10:54:05 -0500202 setMaxFiles(buildCtx)
Liz Kammera7541782022-02-07 13:38:52 -0500203
204 {
Patrice Arruda40564022020-12-10 00:42:58 +0000205 // The order of the function calls is important. The last defer function call
206 // is the first one that is executed to save the rbe metrics to a protobuf
207 // file. The soong metrics file is then next. Bazel profiles are written
208 // before the uploadMetrics is invoked. The written files are then uploaded
209 // if the uploading of the metrics is enabled.
210 files := []string{
211 buildErrorFile, // build error strings
212 rbeMetricsFile, // high level metrics related to remote build execution.
213 soongMetricsFile, // high level metrics related to this build system.
214 config.BazelMetricsDir(), // directory that contains a set of bazel metrics.
215 }
216 defer build.UploadMetrics(buildCtx, config, c.simpleOutput, buildStarted, files...)
217 defer met.Dump(soongMetricsFile)
Kousik Kumar7bc78192022-04-27 14:52:56 -0400218 defer build.CheckProdCreds(buildCtx, config)
Patrice Arruda40564022020-12-10 00:42:58 +0000219 }
Nan Zhangd50f53b2019-01-07 20:26:51 -0800220
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800221 // Read the time at the starting point.
Dan Willemsen1e704462016-08-21 15:17:17 -0700222 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800223 // soong_ui.bash uses the date command's %N (nanosec) flag when getting the start time,
224 // which Darwin doesn't support. Check if it was executed properly before parsing the value.
Dan Willemsen1e704462016-08-21 15:17:17 -0700225 if !strings.HasSuffix(start, "N") {
226 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
227 log.Verbosef("Took %dms to start up.",
228 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Nan Zhang17f27672018-12-12 16:01:49 -0800229 buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700230 }
231 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700232
233 if executable, err := os.Executable(); err == nil {
234 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
235 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700236 }
237
Dan Willemsen6b783c82019-03-08 11:42:28 -0800238 // Fix up the source tree due to a repo bug where it doesn't remove
239 // linkfiles that have been removed
240 fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.bp")
241 fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.mk")
242
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800243 // Create a source finder.
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700244 f := build.NewSourceFinder(buildCtx, config)
245 defer f.Shutdown()
246 build.FindSources(buildCtx, config, f)
247
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700248 c.run(buildCtx, config, args, logsDir)
Dan Willemsen051133b2017-07-14 11:29:29 -0700249}
250
Dan Willemsen6b783c82019-03-08 11:42:28 -0800251func fixBadDanglingLink(ctx build.Context, name string) {
252 _, err := os.Lstat(name)
253 if err != nil {
254 return
255 }
256 _, err = os.Stat(name)
257 if os.IsNotExist(err) {
258 err = os.Remove(name)
259 if err != nil {
260 ctx.Fatalf("Failed to remove dangling link %q: %v", name, err)
261 }
262 }
263}
264
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700265func dumpVar(ctx build.Context, config build.Config, args []string, _ string) {
Dan Willemsen051133b2017-07-14 11:29:29 -0700266 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
Usta Shrestha675564d2022-08-09 18:03:23 -0400267 flags.SetOutput(ctx.Writer)
268
Dan Willemsen051133b2017-07-14 11:29:29 -0700269 flags.Usage = func() {
Patrice Arrudadb4c2f12019-06-17 17:27:09 -0700270 fmt.Fprintf(ctx.Writer, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
271 fmt.Fprintln(ctx.Writer, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
272 fmt.Fprintln(ctx.Writer, "")
Dan Willemsen051133b2017-07-14 11:29:29 -0700273
Patrice Arrudadb4c2f12019-06-17 17:27:09 -0700274 fmt.Fprintln(ctx.Writer, "'report_config' is a special case that prints the human-readable config banner")
275 fmt.Fprintln(ctx.Writer, "from the beginning of the build.")
276 fmt.Fprintln(ctx.Writer, "")
Dan Willemsen051133b2017-07-14 11:29:29 -0700277 flags.PrintDefaults()
278 }
279 abs := flags.Bool("abs", false, "Print the absolute path of the value")
280 flags.Parse(args)
281
282 if flags.NArg() != 1 {
283 flags.Usage()
284 os.Exit(1)
285 }
286
287 varName := flags.Arg(0)
288 if varName == "report_config" {
289 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
290 if err != nil {
291 ctx.Fatal(err)
292 }
293
294 fmt.Println(build.Banner(varData))
295 } else {
296 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
297 if err != nil {
298 ctx.Fatal(err)
299 }
300
301 if *abs {
302 var res []string
303 for _, path := range strings.Fields(varData[varName]) {
304 if abs, err := filepath.Abs(path); err == nil {
305 res = append(res, abs)
306 } else {
307 ctx.Fatalln("Failed to get absolute path of", path, err)
308 }
309 }
310 fmt.Println(strings.Join(res, " "))
311 } else {
312 fmt.Println(varData[varName])
313 }
314 }
315}
316
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700317func dumpVars(ctx build.Context, config build.Config, args []string, _ string) {
Dan Willemsen051133b2017-07-14 11:29:29 -0700318 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
Usta Shrestha675564d2022-08-09 18:03:23 -0400319 flags.SetOutput(ctx.Writer)
320
Dan Willemsen051133b2017-07-14 11:29:29 -0700321 flags.Usage = func() {
Patrice Arrudadb4c2f12019-06-17 17:27:09 -0700322 fmt.Fprintf(ctx.Writer, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
323 fmt.Fprintln(ctx.Writer, "In dumpvars mode, dump the values of one or more legacy make variables, in")
324 fmt.Fprintln(ctx.Writer, "shell syntax. The resulting output may be sourced directly into a shell to")
325 fmt.Fprintln(ctx.Writer, "set corresponding shell variables.")
326 fmt.Fprintln(ctx.Writer, "")
Dan Willemsen051133b2017-07-14 11:29:29 -0700327
Patrice Arrudadb4c2f12019-06-17 17:27:09 -0700328 fmt.Fprintln(ctx.Writer, "'report_config' is a special case that dumps a variable containing the")
329 fmt.Fprintln(ctx.Writer, "human-readable config banner from the beginning of the build.")
330 fmt.Fprintln(ctx.Writer, "")
Dan Willemsen051133b2017-07-14 11:29:29 -0700331 flags.PrintDefaults()
332 }
333
334 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
335 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
336
337 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
338 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
339
340 flags.Parse(args)
341
342 if flags.NArg() != 0 {
343 flags.Usage()
344 os.Exit(1)
345 }
346
347 vars := strings.Fields(*varsStr)
348 absVars := strings.Fields(*absVarsStr)
349
350 allVars := append([]string{}, vars...)
351 allVars = append(allVars, absVars...)
352
353 if i := indexList("report_config", allVars); i != -1 {
354 allVars = append(allVars[:i], allVars[i+1:]...)
355 allVars = append(allVars, build.BannerVars...)
356 }
357
358 if len(allVars) == 0 {
359 return
360 }
361
362 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
363 if err != nil {
364 ctx.Fatal(err)
365 }
366
367 for _, name := range vars {
368 if name == "report_config" {
369 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
370 } else {
371 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
372 }
373 }
374 for _, name := range absVars {
375 var res []string
376 for _, path := range strings.Fields(varData[name]) {
377 abs, err := filepath.Abs(path)
378 if err != nil {
379 ctx.Fatalln("Failed to get absolute path of", path, err)
380 }
381 res = append(res, abs)
382 }
383 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
384 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700385}
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700386
Patrice Arrudab7b22822019-05-21 17:46:23 -0700387func stdio() terminal.StdioInterface {
388 return terminal.StdioImpl{}
389}
390
Jaewoong Jung9f98d3f2020-11-17 18:20:14 -0800391// dumpvar and dumpvars use stdout to output variable values, so use stderr instead of stdout when
392// reporting events to keep stdout clean from noise.
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700393func customStdio() terminal.StdioInterface {
394 return terminal.NewCustomStdio(os.Stdin, os.Stderr, os.Stderr)
395}
396
397// dumpVarConfig does not require any arguments to be parsed by the NewConfig.
398func dumpVarConfig(ctx build.Context, args ...string) build.Config {
399 return build.NewConfig(ctx)
400}
401
Patrice Arrudab7b22822019-05-21 17:46:23 -0700402func buildActionConfig(ctx build.Context, args ...string) build.Config {
403 flags := flag.NewFlagSet("build-mode", flag.ContinueOnError)
Usta Shrestha675564d2022-08-09 18:03:23 -0400404 flags.SetOutput(ctx.Writer)
405
Patrice Arrudab7b22822019-05-21 17:46:23 -0700406 flags.Usage = func() {
407 fmt.Fprintf(ctx.Writer, "usage: %s --build-mode --dir=<path> <build action> [<build arg 1> <build arg 2> ...]\n\n", os.Args[0])
408 fmt.Fprintln(ctx.Writer, "In build mode, build the set of modules based on the specified build")
409 fmt.Fprintln(ctx.Writer, "action. The --dir flag is required to determine what is needed to")
410 fmt.Fprintln(ctx.Writer, "build in the source tree based on the build action. See below for")
411 fmt.Fprintln(ctx.Writer, "the list of acceptable build action flags.")
412 fmt.Fprintln(ctx.Writer, "")
413 flags.PrintDefaults()
414 }
415
416 buildActionFlags := []struct {
Dan Willemsence41e942019-07-29 23:39:30 -0700417 name string
418 description string
419 action build.BuildAction
420 set bool
Patrice Arrudab7b22822019-05-21 17:46:23 -0700421 }{{
Dan Willemsence41e942019-07-29 23:39:30 -0700422 name: "all-modules",
423 description: "Build action: build from the top of the source tree.",
424 action: build.BUILD_MODULES,
Patrice Arrudab7b22822019-05-21 17:46:23 -0700425 }, {
Dan Willemsence41e942019-07-29 23:39:30 -0700426 // This is redirecting to mma build command behaviour. Once it has soaked for a
427 // while, the build command is deleted from here once it has been removed from the
428 // envsetup.sh.
429 name: "modules-in-a-dir-no-deps",
430 description: "Build action: builds all of the modules in the current directory without their dependencies.",
431 action: build.BUILD_MODULES_IN_A_DIRECTORY,
Patrice Arrudab7b22822019-05-21 17:46:23 -0700432 }, {
Dan Willemsence41e942019-07-29 23:39:30 -0700433 // This is redirecting to mmma build command behaviour. Once it has soaked for a
434 // while, the build command is deleted from here once it has been removed from the
435 // envsetup.sh.
436 name: "modules-in-dirs-no-deps",
437 description: "Build action: builds all of the modules in the supplied directories without their dependencies.",
438 action: build.BUILD_MODULES_IN_DIRECTORIES,
Patrice Arrudab7b22822019-05-21 17:46:23 -0700439 }, {
Dan Willemsence41e942019-07-29 23:39:30 -0700440 name: "modules-in-a-dir",
441 description: "Build action: builds all of the modules in the current directory and their dependencies.",
442 action: build.BUILD_MODULES_IN_A_DIRECTORY,
Patrice Arrudab7b22822019-05-21 17:46:23 -0700443 }, {
Dan Willemsence41e942019-07-29 23:39:30 -0700444 name: "modules-in-dirs",
445 description: "Build action: builds all of the modules in the supplied directories and their dependencies.",
446 action: build.BUILD_MODULES_IN_DIRECTORIES,
Patrice Arrudab7b22822019-05-21 17:46:23 -0700447 }}
448 for i, flag := range buildActionFlags {
449 flags.BoolVar(&buildActionFlags[i].set, flag.name, false, flag.description)
450 }
451 dir := flags.String("dir", "", "Directory of the executed build command.")
452
453 // Only interested in the first two args which defines the build action and the directory.
454 // The remaining arguments are passed down to the config.
455 const numBuildActionFlags = 2
456 if len(args) < numBuildActionFlags {
457 flags.Usage()
Usta Shrestha675564d2022-08-09 18:03:23 -0400458 ctx.Fatalln("Improper build action arguments: too few arguments")
Patrice Arrudab7b22822019-05-21 17:46:23 -0700459 }
Usta Shrestha675564d2022-08-09 18:03:23 -0400460 parseError := flags.Parse(args[0:numBuildActionFlags])
Patrice Arrudab7b22822019-05-21 17:46:23 -0700461
462 // The next block of code is to validate that exactly one build action is set and the dir flag
463 // is specified.
Usta Shrestha675564d2022-08-09 18:03:23 -0400464 buildActionFound := false
Patrice Arrudab7b22822019-05-21 17:46:23 -0700465 var buildAction build.BuildAction
Usta Shrestha675564d2022-08-09 18:03:23 -0400466 for _, f := range buildActionFlags {
467 if f.set {
468 if buildActionFound {
469 if parseError == nil {
470 //otherwise Parse() already called Usage()
471 flags.Usage()
472 }
473 ctx.Fatalf("Build action already specified, omit: --%s\n", f.name)
474 }
475 buildActionFound = true
476 buildAction = f.action
Patrice Arrudab7b22822019-05-21 17:46:23 -0700477 }
478 }
Usta Shrestha675564d2022-08-09 18:03:23 -0400479 if !buildActionFound {
480 if parseError == nil {
481 //otherwise Parse() already called Usage()
482 flags.Usage()
483 }
Patrice Arrudab7b22822019-05-21 17:46:23 -0700484 ctx.Fatalln("Build action not defined.")
485 }
486 if *dir == "" {
487 ctx.Fatalln("-dir not specified.")
488 }
489
490 // Remove the build action flags from the args as they are not recognized by the config.
491 args = args[numBuildActionFlags:]
Dan Willemsence41e942019-07-29 23:39:30 -0700492 return build.NewBuildActionConfig(buildAction, *dir, ctx, args...)
Patrice Arrudab7b22822019-05-21 17:46:23 -0700493}
494
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100495func runMake(ctx build.Context, config build.Config, _ []string, logsDir string) {
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700496 if config.IsVerbose() {
497 writer := ctx.Writer
Colin Cross097ed2a2019-06-08 21:48:58 -0700498 fmt.Fprintln(writer, "! The argument `showcommands` is no longer supported.")
499 fmt.Fprintln(writer, "! Instead, the verbose log is always written to a compressed file in the output dir:")
500 fmt.Fprintln(writer, "!")
501 fmt.Fprintf(writer, "! gzip -cd %s/verbose.log.gz | less -R\n", logsDir)
502 fmt.Fprintln(writer, "!")
503 fmt.Fprintln(writer, "! Older versions are saved in verbose.log.#.gz files")
504 fmt.Fprintln(writer, "")
Usta Shrestha96ff7222022-08-09 17:41:15 -0400505 ctx.Fatal("done")
Dan Willemsenc6360832019-07-25 14:07:36 -0700506 }
507
508 if _, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok {
509 writer := ctx.Writer
Dan Willemsence41e942019-07-29 23:39:30 -0700510 fmt.Fprintln(writer, "! The variable `ONE_SHOT_MAKEFILE` is obsolete.")
Dan Willemsenc6360832019-07-25 14:07:36 -0700511 fmt.Fprintln(writer, "!")
512 fmt.Fprintln(writer, "! If you're using `mm`, you'll need to run `source build/envsetup.sh` to update.")
513 fmt.Fprintln(writer, "!")
514 fmt.Fprintln(writer, "! Otherwise, either specify a module name with m, or use mma / MODULES-IN-...")
515 fmt.Fprintln(writer, "")
Dan Willemsence41e942019-07-29 23:39:30 -0700516 ctx.Fatal("done")
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700517 }
518
Anton Hansson5a7861a2021-06-04 10:09:01 +0100519 build.Build(ctx, config)
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700520}
521
522// getCommand finds the appropriate command based on args[1] flag. args[0]
523// is the soong_ui filename.
Liz Kammer0e7993e2020-10-15 11:07:13 -0700524func getCommand(args []string) (*command, []string, error) {
Usta Shrestha675564d2022-08-09 18:03:23 -0400525 listFlags := func() []string {
526 flags := make([]string, len(commands))
527 for i, c := range commands {
528 flags[i] = c.flag
529 }
530 return flags
531 }
532
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700533 if len(args) < 2 {
Usta Shrestha675564d2022-08-09 18:03:23 -0400534 return nil, nil, fmt.Errorf("Too few arguments: %q\nUse one of these: %q", args, listFlags())
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700535 }
536
537 for _, c := range commands {
538 if c.flag == args[1] {
Liz Kammer0e7993e2020-10-15 11:07:13 -0700539 return &c, args[2:], nil
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700540 }
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700541 }
Usta Shrestha675564d2022-08-09 18:03:23 -0400542 return nil, nil, fmt.Errorf("Command not found: %q\nDid you mean one of these: %q", args[1], listFlags())
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700543}
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000544
545// For Bazel support, this moves files and directories from e.g. out/dist/$f to DIST_DIR/$f if necessary.
546func populateExternalDistDir(ctx build.Context, config build.Config) {
547 // Make sure that internalDistDirPath and externalDistDirPath are both absolute paths, so we can compare them
548 var err error
549 var internalDistDirPath string
550 var externalDistDirPath string
551 if internalDistDirPath, err = filepath.Abs(config.DistDir()); err != nil {
552 ctx.Fatalf("Unable to find absolute path of %s: %s", internalDistDirPath, err)
553 }
554 if externalDistDirPath, err = filepath.Abs(config.RealDistDir()); err != nil {
555 ctx.Fatalf("Unable to find absolute path of %s: %s", externalDistDirPath, err)
556 }
557 if externalDistDirPath == internalDistDirPath {
558 return
559 }
560
Rupert Shuttleworth534f1572020-12-16 23:07:06 +0000561 // Make sure the internal DIST_DIR actually exists before trying to read from it
562 if _, err = os.Stat(internalDistDirPath); os.IsNotExist(err) {
563 ctx.Println("Skipping Bazel dist dir migration - nothing to do!")
564 return
565 }
566
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000567 // Make sure the external DIST_DIR actually exists before trying to write to it
568 if err = os.MkdirAll(externalDistDirPath, 0755); err != nil {
569 ctx.Fatalf("Unable to make directory %s: %s", externalDistDirPath, err)
570 }
571
572 ctx.Println("Populating external DIST_DIR...")
573
574 populateExternalDistDirHelper(ctx, config, internalDistDirPath, externalDistDirPath)
575}
576
577func populateExternalDistDirHelper(ctx build.Context, config build.Config, internalDistDirPath string, externalDistDirPath string) {
578 files, err := ioutil.ReadDir(internalDistDirPath)
579 if err != nil {
580 ctx.Fatalf("Can't read internal distdir %s: %s", internalDistDirPath, err)
581 }
582 for _, f := range files {
583 internalFilePath := filepath.Join(internalDistDirPath, f.Name())
584 externalFilePath := filepath.Join(externalDistDirPath, f.Name())
585
586 if f.IsDir() {
587 // Moving a directory - check if there is an existing directory to merge with
588 externalLstat, err := os.Lstat(externalFilePath)
589 if err != nil {
590 if !os.IsNotExist(err) {
591 ctx.Fatalf("Can't lstat external %s: %s", externalDistDirPath, err)
592 }
593 // Otherwise, if the error was os.IsNotExist, that's fine and we fall through to the rename at the bottom
594 } else {
595 if externalLstat.IsDir() {
596 // Existing dir - try to merge the directories?
597 populateExternalDistDirHelper(ctx, config, internalFilePath, externalFilePath)
598 continue
599 } else {
600 // Existing file being replaced with a directory. Delete the existing file...
601 if err := os.RemoveAll(externalFilePath); err != nil {
602 ctx.Fatalf("Unable to remove existing %s: %s", externalFilePath, err)
603 }
604 }
605 }
606 } else {
607 // Moving a file (not a dir) - delete any existing file or directory
608 if err := os.RemoveAll(externalFilePath); err != nil {
609 ctx.Fatalf("Unable to remove existing %s: %s", externalFilePath, err)
610 }
611 }
612
613 // The actual move - do a rename instead of a copy in order to save disk space.
614 if err := os.Rename(internalFilePath, externalFilePath); err != nil {
615 ctx.Fatalf("Unable to rename %s -> %s due to error %s", internalFilePath, externalFilePath, err)
616 }
617 }
618}
Liz Kammer4ae119c2022-02-09 10:54:05 -0500619
620func setMaxFiles(ctx build.Context) {
621 var limits syscall.Rlimit
622
623 err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits)
624 if err != nil {
625 ctx.Println("Failed to get file limit:", err)
626 return
627 }
628
629 ctx.Verbosef("Current file limits: %d soft, %d hard", limits.Cur, limits.Max)
630 if limits.Cur == limits.Max {
631 return
632 }
633
634 limits.Cur = limits.Max
635 err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limits)
636 if err != nil {
637 ctx.Println("Failed to increase file limit:", err)
638 }
639}