blob: 03803689e33accea9f83f609db5ae1504d1871b4 [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"
Dan Willemsen1e704462016-08-21 15:17:17 -070021 "os"
22 "path/filepath"
23 "strconv"
24 "strings"
25 "time"
26
27 "android/soong/ui/build"
28 "android/soong/ui/logger"
Nan Zhang17f27672018-12-12 16:01:49 -080029 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070030 "android/soong/ui/status"
31 "android/soong/ui/terminal"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070032 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070033)
34
35func indexList(s string, list []string) int {
36 for i, l := range list {
37 if l == s {
38 return i
39 }
40 }
41
42 return -1
43}
44
45func inList(s string, list []string) bool {
46 return indexList(s, list) != -1
47}
48
49func main() {
Dan Willemsenc35b3812018-07-16 19:59:10 -070050 var stdio terminal.StdioInterface
51 stdio = terminal.StdioImpl{}
52
53 // dumpvar uses stdout, everything else should be in stderr
54 if os.Args[1] == "--dumpvar-mode" || os.Args[1] == "--dumpvars-mode" {
55 stdio = terminal.NewCustomStdio(os.Stdin, os.Stderr, os.Stderr)
56 }
57
58 writer := terminal.NewWriter(stdio)
Dan Willemsenb82471a2018-05-17 16:37:09 -070059 defer writer.Finish()
60
61 log := logger.New(writer)
Dan Willemsen1e704462016-08-21 15:17:17 -070062 defer log.Cleanup()
63
Dan Willemsen051133b2017-07-14 11:29:29 -070064 if len(os.Args) < 2 || !(inList("--make-mode", os.Args) ||
65 os.Args[1] == "--dumpvars-mode" ||
66 os.Args[1] == "--dumpvar-mode") {
67
Dan Willemsen1e704462016-08-21 15:17:17 -070068 log.Fatalln("The `soong` native UI is not yet available.")
69 }
70
Dan Willemsen1e704462016-08-21 15:17:17 -070071 ctx, cancel := context.WithCancel(context.Background())
72 defer cancel()
73
Dan Willemsend9f6fa22016-08-21 15:17:17 -070074 trace := tracer.New(log)
75 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -070076
Nan Zhang17f27672018-12-12 16:01:49 -080077 met := metrics.New()
78
Dan Willemsenb82471a2018-05-17 16:37:09 -070079 stat := &status.Status{}
80 defer stat.Finish()
81 stat.AddOutput(terminal.NewStatusOutput(writer, os.Getenv("NINJA_STATUS")))
82 stat.AddOutput(trace.StatusTracer())
83
Dan Willemsend9f6fa22016-08-21 15:17:17 -070084 build.SetupSignals(log, cancel, func() {
85 trace.Close()
86 log.Cleanup()
Dan Willemsenb82471a2018-05-17 16:37:09 -070087 stat.Finish()
Dan Willemsend9f6fa22016-08-21 15:17:17 -070088 })
89
Dan Willemsen59339a22018-07-22 21:18:45 -070090 buildCtx := build.Context{ContextImpl: &build.ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -070091 Context: ctx,
92 Logger: log,
Nan Zhang17f27672018-12-12 16:01:49 -080093 Metrics: met,
Dan Willemsenb82471a2018-05-17 16:37:09 -070094 Tracer: trace,
95 Writer: writer,
96 Status: stat,
Dan Willemsend9f6fa22016-08-21 15:17:17 -070097 }}
Dan Willemsen051133b2017-07-14 11:29:29 -070098 var config build.Config
99 if os.Args[1] == "--dumpvars-mode" || os.Args[1] == "--dumpvar-mode" {
100 config = build.NewConfig(buildCtx)
101 } else {
102 config = build.NewConfig(buildCtx, os.Args[1:]...)
103 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700104
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700105 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -0800106
Dan Willemsenb82471a2018-05-17 16:37:09 -0700107 logsDir := config.OutDir()
Dan Willemsen8a073a82017-02-04 17:30:44 -0800108 if config.Dist() {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700109 logsDir = filepath.Join(config.DistDir(), "logs")
Dan Willemsen8a073a82017-02-04 17:30:44 -0800110 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700111
Dan Willemsenb82471a2018-05-17 16:37:09 -0700112 os.MkdirAll(logsDir, 0777)
113 log.SetOutput(filepath.Join(logsDir, "soong.log"))
114 trace.SetOutput(filepath.Join(logsDir, "build.trace"))
115 stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, "verbose.log")))
116 stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, "error.log")))
117
Nan Zhangd50f53b2019-01-07 20:26:51 -0800118 defer met.Dump(filepath.Join(logsDir, "build_metrics"))
119
Dan Willemsen1e704462016-08-21 15:17:17 -0700120 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
121 if !strings.HasSuffix(start, "N") {
122 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
123 log.Verbosef("Took %dms to start up.",
124 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Nan Zhang17f27672018-12-12 16:01:49 -0800125 buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700126 }
127 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700128
129 if executable, err := os.Executable(); err == nil {
130 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
131 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700132 }
133
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700134 f := build.NewSourceFinder(buildCtx, config)
135 defer f.Shutdown()
136 build.FindSources(buildCtx, config, f)
137
Dan Willemsen051133b2017-07-14 11:29:29 -0700138 if os.Args[1] == "--dumpvar-mode" {
139 dumpVar(buildCtx, config, os.Args[2:])
140 } else if os.Args[1] == "--dumpvars-mode" {
141 dumpVars(buildCtx, config, os.Args[2:])
142 } else {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700143 if config.IsVerbose() {
144 writer.Print("! The argument `showcommands` is no longer supported.")
145 writer.Print("! Instead, the verbose log is always written to a compressed file in the output dir:")
146 writer.Print("!")
147 writer.Print(fmt.Sprintf("! gzip -cd %s/verbose.log.gz | less -R", logsDir))
148 writer.Print("!")
149 writer.Print("! Older versions are saved in verbose.log.#.gz files")
150 writer.Print("")
151 time.Sleep(5 * time.Second)
152 }
153
Colin Cross37193492017-11-16 17:55:00 -0800154 toBuild := build.BuildAll
155 if config.Checkbuild() {
156 toBuild |= build.RunBuildTests
157 }
158 build.Build(buildCtx, config, toBuild)
Dan Willemsen051133b2017-07-14 11:29:29 -0700159 }
160}
161
162func dumpVar(ctx build.Context, config build.Config, args []string) {
163 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
164 flags.Usage = func() {
165 fmt.Fprintf(os.Stderr, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
166 fmt.Fprintln(os.Stderr, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
167 fmt.Fprintln(os.Stderr, "")
168
169 fmt.Fprintln(os.Stderr, "'report_config' is a special case that prints the human-readable config banner")
170 fmt.Fprintln(os.Stderr, "from the beginning of the build.")
171 fmt.Fprintln(os.Stderr, "")
172 flags.PrintDefaults()
173 }
174 abs := flags.Bool("abs", false, "Print the absolute path of the value")
175 flags.Parse(args)
176
177 if flags.NArg() != 1 {
178 flags.Usage()
179 os.Exit(1)
180 }
181
182 varName := flags.Arg(0)
183 if varName == "report_config" {
184 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
185 if err != nil {
186 ctx.Fatal(err)
187 }
188
189 fmt.Println(build.Banner(varData))
190 } else {
191 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
192 if err != nil {
193 ctx.Fatal(err)
194 }
195
196 if *abs {
197 var res []string
198 for _, path := range strings.Fields(varData[varName]) {
199 if abs, err := filepath.Abs(path); err == nil {
200 res = append(res, abs)
201 } else {
202 ctx.Fatalln("Failed to get absolute path of", path, err)
203 }
204 }
205 fmt.Println(strings.Join(res, " "))
206 } else {
207 fmt.Println(varData[varName])
208 }
209 }
210}
211
212func dumpVars(ctx build.Context, config build.Config, args []string) {
213 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
214 flags.Usage = func() {
215 fmt.Fprintf(os.Stderr, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
216 fmt.Fprintln(os.Stderr, "In dumpvars mode, dump the values of one or more legacy make variables, in")
217 fmt.Fprintln(os.Stderr, "shell syntax. The resulting output may be sourced directly into a shell to")
218 fmt.Fprintln(os.Stderr, "set corresponding shell variables.")
219 fmt.Fprintln(os.Stderr, "")
220
221 fmt.Fprintln(os.Stderr, "'report_config' is a special case that dumps a variable containing the")
222 fmt.Fprintln(os.Stderr, "human-readable config banner from the beginning of the build.")
223 fmt.Fprintln(os.Stderr, "")
224 flags.PrintDefaults()
225 }
226
227 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
228 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
229
230 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
231 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
232
233 flags.Parse(args)
234
235 if flags.NArg() != 0 {
236 flags.Usage()
237 os.Exit(1)
238 }
239
240 vars := strings.Fields(*varsStr)
241 absVars := strings.Fields(*absVarsStr)
242
243 allVars := append([]string{}, vars...)
244 allVars = append(allVars, absVars...)
245
246 if i := indexList("report_config", allVars); i != -1 {
247 allVars = append(allVars[:i], allVars[i+1:]...)
248 allVars = append(allVars, build.BannerVars...)
249 }
250
251 if len(allVars) == 0 {
252 return
253 }
254
255 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
256 if err != nil {
257 ctx.Fatal(err)
258 }
259
260 for _, name := range vars {
261 if name == "report_config" {
262 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
263 } else {
264 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
265 }
266 }
267 for _, name := range absVars {
268 var res []string
269 for _, path := range strings.Fields(varData[name]) {
270 abs, err := filepath.Abs(path)
271 if err != nil {
272 ctx.Fatalln("Failed to get absolute path of", path, err)
273 }
274 res = append(res, abs)
275 }
276 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
277 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700278}