blob: 47682ff56e96f071431dc5801e67c746de58c15b [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"
Dan Willemsenb82471a2018-05-17 16:37:09 -070029 "android/soong/ui/status"
30 "android/soong/ui/terminal"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070031 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070032)
33
34func indexList(s string, list []string) int {
35 for i, l := range list {
36 if l == s {
37 return i
38 }
39 }
40
41 return -1
42}
43
44func inList(s string, list []string) bool {
45 return indexList(s, list) != -1
46}
47
48func main() {
Dan Willemsenc35b3812018-07-16 19:59:10 -070049 var stdio terminal.StdioInterface
50 stdio = terminal.StdioImpl{}
51
52 // dumpvar uses stdout, everything else should be in stderr
53 if os.Args[1] == "--dumpvar-mode" || os.Args[1] == "--dumpvars-mode" {
54 stdio = terminal.NewCustomStdio(os.Stdin, os.Stderr, os.Stderr)
55 }
56
57 writer := terminal.NewWriter(stdio)
Dan Willemsenb82471a2018-05-17 16:37:09 -070058 defer writer.Finish()
59
60 log := logger.New(writer)
Dan Willemsen1e704462016-08-21 15:17:17 -070061 defer log.Cleanup()
62
Dan Willemsen051133b2017-07-14 11:29:29 -070063 if len(os.Args) < 2 || !(inList("--make-mode", os.Args) ||
64 os.Args[1] == "--dumpvars-mode" ||
65 os.Args[1] == "--dumpvar-mode") {
66
Dan Willemsen1e704462016-08-21 15:17:17 -070067 log.Fatalln("The `soong` native UI is not yet available.")
68 }
69
Dan Willemsen1e704462016-08-21 15:17:17 -070070 ctx, cancel := context.WithCancel(context.Background())
71 defer cancel()
72
Dan Willemsend9f6fa22016-08-21 15:17:17 -070073 trace := tracer.New(log)
74 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -070075
Dan Willemsenb82471a2018-05-17 16:37:09 -070076 stat := &status.Status{}
77 defer stat.Finish()
78 stat.AddOutput(terminal.NewStatusOutput(writer, os.Getenv("NINJA_STATUS")))
79 stat.AddOutput(trace.StatusTracer())
80
Dan Willemsend9f6fa22016-08-21 15:17:17 -070081 build.SetupSignals(log, cancel, func() {
82 trace.Close()
83 log.Cleanup()
Dan Willemsenb82471a2018-05-17 16:37:09 -070084 stat.Finish()
Dan Willemsend9f6fa22016-08-21 15:17:17 -070085 })
86
87 buildCtx := build.Context{&build.ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -070088 Context: ctx,
89 Logger: log,
90 Tracer: trace,
91 Writer: writer,
92 Status: stat,
Dan Willemsend9f6fa22016-08-21 15:17:17 -070093 }}
Dan Willemsen051133b2017-07-14 11:29:29 -070094 var config build.Config
95 if os.Args[1] == "--dumpvars-mode" || os.Args[1] == "--dumpvar-mode" {
96 config = build.NewConfig(buildCtx)
97 } else {
98 config = build.NewConfig(buildCtx, os.Args[1:]...)
99 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700100
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700101 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -0800102
Dan Willemsenb82471a2018-05-17 16:37:09 -0700103 logsDir := config.OutDir()
Dan Willemsen8a073a82017-02-04 17:30:44 -0800104 if config.Dist() {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700105 logsDir = filepath.Join(config.DistDir(), "logs")
Dan Willemsen8a073a82017-02-04 17:30:44 -0800106 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700107
Dan Willemsenb82471a2018-05-17 16:37:09 -0700108 os.MkdirAll(logsDir, 0777)
109 log.SetOutput(filepath.Join(logsDir, "soong.log"))
110 trace.SetOutput(filepath.Join(logsDir, "build.trace"))
111 stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, "verbose.log")))
112 stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, "error.log")))
113
Dan Willemsen1e704462016-08-21 15:17:17 -0700114 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
115 if !strings.HasSuffix(start, "N") {
116 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
117 log.Verbosef("Took %dms to start up.",
118 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700119 buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700120 }
121 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700122
123 if executable, err := os.Executable(); err == nil {
124 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
125 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700126 }
127
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700128 f := build.NewSourceFinder(buildCtx, config)
129 defer f.Shutdown()
130 build.FindSources(buildCtx, config, f)
131
Dan Willemsen051133b2017-07-14 11:29:29 -0700132 if os.Args[1] == "--dumpvar-mode" {
133 dumpVar(buildCtx, config, os.Args[2:])
134 } else if os.Args[1] == "--dumpvars-mode" {
135 dumpVars(buildCtx, config, os.Args[2:])
136 } else {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700137 if config.IsVerbose() {
138 writer.Print("! The argument `showcommands` is no longer supported.")
139 writer.Print("! Instead, the verbose log is always written to a compressed file in the output dir:")
140 writer.Print("!")
141 writer.Print(fmt.Sprintf("! gzip -cd %s/verbose.log.gz | less -R", logsDir))
142 writer.Print("!")
143 writer.Print("! Older versions are saved in verbose.log.#.gz files")
144 writer.Print("")
145 time.Sleep(5 * time.Second)
146 }
147
Colin Cross37193492017-11-16 17:55:00 -0800148 toBuild := build.BuildAll
149 if config.Checkbuild() {
150 toBuild |= build.RunBuildTests
151 }
152 build.Build(buildCtx, config, toBuild)
Dan Willemsen051133b2017-07-14 11:29:29 -0700153 }
154}
155
156func dumpVar(ctx build.Context, config build.Config, args []string) {
157 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
158 flags.Usage = func() {
159 fmt.Fprintf(os.Stderr, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
160 fmt.Fprintln(os.Stderr, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
161 fmt.Fprintln(os.Stderr, "")
162
163 fmt.Fprintln(os.Stderr, "'report_config' is a special case that prints the human-readable config banner")
164 fmt.Fprintln(os.Stderr, "from the beginning of the build.")
165 fmt.Fprintln(os.Stderr, "")
166 flags.PrintDefaults()
167 }
168 abs := flags.Bool("abs", false, "Print the absolute path of the value")
169 flags.Parse(args)
170
171 if flags.NArg() != 1 {
172 flags.Usage()
173 os.Exit(1)
174 }
175
176 varName := flags.Arg(0)
177 if varName == "report_config" {
178 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
179 if err != nil {
180 ctx.Fatal(err)
181 }
182
183 fmt.Println(build.Banner(varData))
184 } else {
185 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
186 if err != nil {
187 ctx.Fatal(err)
188 }
189
190 if *abs {
191 var res []string
192 for _, path := range strings.Fields(varData[varName]) {
193 if abs, err := filepath.Abs(path); err == nil {
194 res = append(res, abs)
195 } else {
196 ctx.Fatalln("Failed to get absolute path of", path, err)
197 }
198 }
199 fmt.Println(strings.Join(res, " "))
200 } else {
201 fmt.Println(varData[varName])
202 }
203 }
204}
205
206func dumpVars(ctx build.Context, config build.Config, args []string) {
207 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
208 flags.Usage = func() {
209 fmt.Fprintf(os.Stderr, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
210 fmt.Fprintln(os.Stderr, "In dumpvars mode, dump the values of one or more legacy make variables, in")
211 fmt.Fprintln(os.Stderr, "shell syntax. The resulting output may be sourced directly into a shell to")
212 fmt.Fprintln(os.Stderr, "set corresponding shell variables.")
213 fmt.Fprintln(os.Stderr, "")
214
215 fmt.Fprintln(os.Stderr, "'report_config' is a special case that dumps a variable containing the")
216 fmt.Fprintln(os.Stderr, "human-readable config banner from the beginning of the build.")
217 fmt.Fprintln(os.Stderr, "")
218 flags.PrintDefaults()
219 }
220
221 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
222 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
223
224 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
225 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
226
227 flags.Parse(args)
228
229 if flags.NArg() != 0 {
230 flags.Usage()
231 os.Exit(1)
232 }
233
234 vars := strings.Fields(*varsStr)
235 absVars := strings.Fields(*absVarsStr)
236
237 allVars := append([]string{}, vars...)
238 allVars = append(allVars, absVars...)
239
240 if i := indexList("report_config", allVars); i != -1 {
241 allVars = append(allVars[:i], allVars[i+1:]...)
242 allVars = append(allVars, build.BannerVars...)
243 }
244
245 if len(allVars) == 0 {
246 return
247 }
248
249 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
250 if err != nil {
251 ctx.Fatal(err)
252 }
253
254 for _, name := range vars {
255 if name == "report_config" {
256 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
257 } else {
258 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
259 }
260 }
261 for _, name := range absVars {
262 var res []string
263 for _, path := range strings.Fields(varData[name]) {
264 abs, err := filepath.Abs(path)
265 if err != nil {
266 ctx.Fatalln("Failed to get absolute path of", path, err)
267 }
268 res = append(res, abs)
269 }
270 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
271 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700272}