blob: 9f40e33ef26168cacc0e135d828950526093e740 [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()
Sasha Smundakc0c9ef92019-01-23 09:52:57 -080081 stat.AddOutput(terminal.NewStatusOutput(writer, os.Getenv("NINJA_STATUS"),
82 build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD")))
Dan Willemsenb82471a2018-05-17 16:37:09 -070083 stat.AddOutput(trace.StatusTracer())
84
Dan Willemsend9f6fa22016-08-21 15:17:17 -070085 build.SetupSignals(log, cancel, func() {
86 trace.Close()
87 log.Cleanup()
Dan Willemsenb82471a2018-05-17 16:37:09 -070088 stat.Finish()
Dan Willemsend9f6fa22016-08-21 15:17:17 -070089 })
90
Dan Willemsen59339a22018-07-22 21:18:45 -070091 buildCtx := build.Context{ContextImpl: &build.ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -070092 Context: ctx,
93 Logger: log,
Nan Zhang17f27672018-12-12 16:01:49 -080094 Metrics: met,
Dan Willemsenb82471a2018-05-17 16:37:09 -070095 Tracer: trace,
96 Writer: writer,
97 Status: stat,
Dan Willemsend9f6fa22016-08-21 15:17:17 -070098 }}
Dan Willemsen051133b2017-07-14 11:29:29 -070099 var config build.Config
100 if os.Args[1] == "--dumpvars-mode" || os.Args[1] == "--dumpvar-mode" {
101 config = build.NewConfig(buildCtx)
102 } else {
103 config = build.NewConfig(buildCtx, os.Args[1:]...)
104 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700105
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700106 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -0800107
Dan Willemsenb82471a2018-05-17 16:37:09 -0700108 logsDir := config.OutDir()
Dan Willemsen8a073a82017-02-04 17:30:44 -0800109 if config.Dist() {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700110 logsDir = filepath.Join(config.DistDir(), "logs")
Dan Willemsen8a073a82017-02-04 17:30:44 -0800111 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700112
Dan Willemsenb82471a2018-05-17 16:37:09 -0700113 os.MkdirAll(logsDir, 0777)
114 log.SetOutput(filepath.Join(logsDir, "soong.log"))
115 trace.SetOutput(filepath.Join(logsDir, "build.trace"))
116 stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, "verbose.log")))
117 stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, "error.log")))
118
Nan Zhangd50f53b2019-01-07 20:26:51 -0800119 defer met.Dump(filepath.Join(logsDir, "build_metrics"))
120
Dan Willemsen1e704462016-08-21 15:17:17 -0700121 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
122 if !strings.HasSuffix(start, "N") {
123 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
124 log.Verbosef("Took %dms to start up.",
125 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Nan Zhang17f27672018-12-12 16:01:49 -0800126 buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700127 }
128 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700129
130 if executable, err := os.Executable(); err == nil {
131 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
132 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700133 }
134
Dan Willemsen6b783c82019-03-08 11:42:28 -0800135 // Fix up the source tree due to a repo bug where it doesn't remove
136 // linkfiles that have been removed
137 fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.bp")
138 fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.mk")
139
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700140 f := build.NewSourceFinder(buildCtx, config)
141 defer f.Shutdown()
142 build.FindSources(buildCtx, config, f)
143
Dan Willemsen051133b2017-07-14 11:29:29 -0700144 if os.Args[1] == "--dumpvar-mode" {
145 dumpVar(buildCtx, config, os.Args[2:])
146 } else if os.Args[1] == "--dumpvars-mode" {
147 dumpVars(buildCtx, config, os.Args[2:])
148 } else {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700149 if config.IsVerbose() {
150 writer.Print("! The argument `showcommands` is no longer supported.")
151 writer.Print("! Instead, the verbose log is always written to a compressed file in the output dir:")
152 writer.Print("!")
153 writer.Print(fmt.Sprintf("! gzip -cd %s/verbose.log.gz | less -R", logsDir))
154 writer.Print("!")
155 writer.Print("! Older versions are saved in verbose.log.#.gz files")
156 writer.Print("")
157 time.Sleep(5 * time.Second)
158 }
159
Colin Cross37193492017-11-16 17:55:00 -0800160 toBuild := build.BuildAll
161 if config.Checkbuild() {
162 toBuild |= build.RunBuildTests
163 }
164 build.Build(buildCtx, config, toBuild)
Dan Willemsen051133b2017-07-14 11:29:29 -0700165 }
166}
167
Dan Willemsen6b783c82019-03-08 11:42:28 -0800168func fixBadDanglingLink(ctx build.Context, name string) {
169 _, err := os.Lstat(name)
170 if err != nil {
171 return
172 }
173 _, err = os.Stat(name)
174 if os.IsNotExist(err) {
175 err = os.Remove(name)
176 if err != nil {
177 ctx.Fatalf("Failed to remove dangling link %q: %v", name, err)
178 }
179 }
180}
181
Dan Willemsen051133b2017-07-14 11:29:29 -0700182func dumpVar(ctx build.Context, config build.Config, args []string) {
183 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
184 flags.Usage = func() {
185 fmt.Fprintf(os.Stderr, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
186 fmt.Fprintln(os.Stderr, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
187 fmt.Fprintln(os.Stderr, "")
188
189 fmt.Fprintln(os.Stderr, "'report_config' is a special case that prints the human-readable config banner")
190 fmt.Fprintln(os.Stderr, "from the beginning of the build.")
191 fmt.Fprintln(os.Stderr, "")
192 flags.PrintDefaults()
193 }
194 abs := flags.Bool("abs", false, "Print the absolute path of the value")
195 flags.Parse(args)
196
197 if flags.NArg() != 1 {
198 flags.Usage()
199 os.Exit(1)
200 }
201
202 varName := flags.Arg(0)
203 if varName == "report_config" {
204 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
205 if err != nil {
206 ctx.Fatal(err)
207 }
208
209 fmt.Println(build.Banner(varData))
210 } else {
211 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
212 if err != nil {
213 ctx.Fatal(err)
214 }
215
216 if *abs {
217 var res []string
218 for _, path := range strings.Fields(varData[varName]) {
219 if abs, err := filepath.Abs(path); err == nil {
220 res = append(res, abs)
221 } else {
222 ctx.Fatalln("Failed to get absolute path of", path, err)
223 }
224 }
225 fmt.Println(strings.Join(res, " "))
226 } else {
227 fmt.Println(varData[varName])
228 }
229 }
230}
231
232func dumpVars(ctx build.Context, config build.Config, args []string) {
233 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
234 flags.Usage = func() {
235 fmt.Fprintf(os.Stderr, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
236 fmt.Fprintln(os.Stderr, "In dumpvars mode, dump the values of one or more legacy make variables, in")
237 fmt.Fprintln(os.Stderr, "shell syntax. The resulting output may be sourced directly into a shell to")
238 fmt.Fprintln(os.Stderr, "set corresponding shell variables.")
239 fmt.Fprintln(os.Stderr, "")
240
241 fmt.Fprintln(os.Stderr, "'report_config' is a special case that dumps a variable containing the")
242 fmt.Fprintln(os.Stderr, "human-readable config banner from the beginning of the build.")
243 fmt.Fprintln(os.Stderr, "")
244 flags.PrintDefaults()
245 }
246
247 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
248 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
249
250 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
251 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
252
253 flags.Parse(args)
254
255 if flags.NArg() != 0 {
256 flags.Usage()
257 os.Exit(1)
258 }
259
260 vars := strings.Fields(*varsStr)
261 absVars := strings.Fields(*absVarsStr)
262
263 allVars := append([]string{}, vars...)
264 allVars = append(allVars, absVars...)
265
266 if i := indexList("report_config", allVars); i != -1 {
267 allVars = append(allVars[:i], allVars[i+1:]...)
268 allVars = append(allVars, build.BannerVars...)
269 }
270
271 if len(allVars) == 0 {
272 return
273 }
274
275 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
276 if err != nil {
277 ctx.Fatal(err)
278 }
279
280 for _, name := range vars {
281 if name == "report_config" {
282 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
283 } else {
284 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
285 }
286 }
287 for _, name := range absVars {
288 var res []string
289 for _, path := range strings.Fields(varData[name]) {
290 abs, err := filepath.Abs(path)
291 if err != nil {
292 ctx.Fatalln("Failed to get absolute path of", path, err)
293 }
294 res = append(res, abs)
295 }
296 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
297 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700298}