blob: e2f25b8f297609774bffe8dd4f473dd6c30f2b31 [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 Willemsenb82471a2018-05-17 16:37:09 -070049 writer := terminal.NewWriter(terminal.StdioImpl{})
50 defer writer.Finish()
51
52 log := logger.New(writer)
Dan Willemsen1e704462016-08-21 15:17:17 -070053 defer log.Cleanup()
54
Dan Willemsen051133b2017-07-14 11:29:29 -070055 if len(os.Args) < 2 || !(inList("--make-mode", os.Args) ||
56 os.Args[1] == "--dumpvars-mode" ||
57 os.Args[1] == "--dumpvar-mode") {
58
Dan Willemsen1e704462016-08-21 15:17:17 -070059 log.Fatalln("The `soong` native UI is not yet available.")
60 }
61
Dan Willemsen1e704462016-08-21 15:17:17 -070062 ctx, cancel := context.WithCancel(context.Background())
63 defer cancel()
64
Dan Willemsend9f6fa22016-08-21 15:17:17 -070065 trace := tracer.New(log)
66 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -070067
Dan Willemsenb82471a2018-05-17 16:37:09 -070068 stat := &status.Status{}
69 defer stat.Finish()
70 stat.AddOutput(terminal.NewStatusOutput(writer, os.Getenv("NINJA_STATUS")))
71 stat.AddOutput(trace.StatusTracer())
72
Dan Willemsend9f6fa22016-08-21 15:17:17 -070073 build.SetupSignals(log, cancel, func() {
74 trace.Close()
75 log.Cleanup()
Dan Willemsenb82471a2018-05-17 16:37:09 -070076 stat.Finish()
Dan Willemsend9f6fa22016-08-21 15:17:17 -070077 })
78
79 buildCtx := build.Context{&build.ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -070080 Context: ctx,
81 Logger: log,
82 Tracer: trace,
83 Writer: writer,
84 Status: stat,
Dan Willemsend9f6fa22016-08-21 15:17:17 -070085 }}
Dan Willemsen051133b2017-07-14 11:29:29 -070086 var config build.Config
87 if os.Args[1] == "--dumpvars-mode" || os.Args[1] == "--dumpvar-mode" {
88 config = build.NewConfig(buildCtx)
89 } else {
90 config = build.NewConfig(buildCtx, os.Args[1:]...)
91 }
Dan Willemsen1e704462016-08-21 15:17:17 -070092
Dan Willemsend9f6fa22016-08-21 15:17:17 -070093 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -080094
Dan Willemsenb82471a2018-05-17 16:37:09 -070095 logsDir := config.OutDir()
Dan Willemsen8a073a82017-02-04 17:30:44 -080096 if config.Dist() {
Dan Willemsenb82471a2018-05-17 16:37:09 -070097 logsDir = filepath.Join(config.DistDir(), "logs")
Dan Willemsen8a073a82017-02-04 17:30:44 -080098 }
Dan Willemsen1e704462016-08-21 15:17:17 -070099
Dan Willemsenb82471a2018-05-17 16:37:09 -0700100 os.MkdirAll(logsDir, 0777)
101 log.SetOutput(filepath.Join(logsDir, "soong.log"))
102 trace.SetOutput(filepath.Join(logsDir, "build.trace"))
103 stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, "verbose.log")))
104 stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, "error.log")))
105
Dan Willemsen1e704462016-08-21 15:17:17 -0700106 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
107 if !strings.HasSuffix(start, "N") {
108 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
109 log.Verbosef("Took %dms to start up.",
110 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700111 buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700112 }
113 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700114
115 if executable, err := os.Executable(); err == nil {
116 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
117 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700118 }
119
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700120 f := build.NewSourceFinder(buildCtx, config)
121 defer f.Shutdown()
122 build.FindSources(buildCtx, config, f)
123
Dan Willemsen051133b2017-07-14 11:29:29 -0700124 if os.Args[1] == "--dumpvar-mode" {
125 dumpVar(buildCtx, config, os.Args[2:])
126 } else if os.Args[1] == "--dumpvars-mode" {
127 dumpVars(buildCtx, config, os.Args[2:])
128 } else {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700129 if config.IsVerbose() {
130 writer.Print("! The argument `showcommands` is no longer supported.")
131 writer.Print("! Instead, the verbose log is always written to a compressed file in the output dir:")
132 writer.Print("!")
133 writer.Print(fmt.Sprintf("! gzip -cd %s/verbose.log.gz | less -R", logsDir))
134 writer.Print("!")
135 writer.Print("! Older versions are saved in verbose.log.#.gz files")
136 writer.Print("")
137 time.Sleep(5 * time.Second)
138 }
139
Colin Cross37193492017-11-16 17:55:00 -0800140 toBuild := build.BuildAll
141 if config.Checkbuild() {
142 toBuild |= build.RunBuildTests
143 }
144 build.Build(buildCtx, config, toBuild)
Dan Willemsen051133b2017-07-14 11:29:29 -0700145 }
146}
147
148func dumpVar(ctx build.Context, config build.Config, args []string) {
149 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
150 flags.Usage = func() {
151 fmt.Fprintf(os.Stderr, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
152 fmt.Fprintln(os.Stderr, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
153 fmt.Fprintln(os.Stderr, "")
154
155 fmt.Fprintln(os.Stderr, "'report_config' is a special case that prints the human-readable config banner")
156 fmt.Fprintln(os.Stderr, "from the beginning of the build.")
157 fmt.Fprintln(os.Stderr, "")
158 flags.PrintDefaults()
159 }
160 abs := flags.Bool("abs", false, "Print the absolute path of the value")
161 flags.Parse(args)
162
163 if flags.NArg() != 1 {
164 flags.Usage()
165 os.Exit(1)
166 }
167
168 varName := flags.Arg(0)
169 if varName == "report_config" {
170 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
171 if err != nil {
172 ctx.Fatal(err)
173 }
174
175 fmt.Println(build.Banner(varData))
176 } else {
177 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
178 if err != nil {
179 ctx.Fatal(err)
180 }
181
182 if *abs {
183 var res []string
184 for _, path := range strings.Fields(varData[varName]) {
185 if abs, err := filepath.Abs(path); err == nil {
186 res = append(res, abs)
187 } else {
188 ctx.Fatalln("Failed to get absolute path of", path, err)
189 }
190 }
191 fmt.Println(strings.Join(res, " "))
192 } else {
193 fmt.Println(varData[varName])
194 }
195 }
196}
197
198func dumpVars(ctx build.Context, config build.Config, args []string) {
199 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
200 flags.Usage = func() {
201 fmt.Fprintf(os.Stderr, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
202 fmt.Fprintln(os.Stderr, "In dumpvars mode, dump the values of one or more legacy make variables, in")
203 fmt.Fprintln(os.Stderr, "shell syntax. The resulting output may be sourced directly into a shell to")
204 fmt.Fprintln(os.Stderr, "set corresponding shell variables.")
205 fmt.Fprintln(os.Stderr, "")
206
207 fmt.Fprintln(os.Stderr, "'report_config' is a special case that dumps a variable containing the")
208 fmt.Fprintln(os.Stderr, "human-readable config banner from the beginning of the build.")
209 fmt.Fprintln(os.Stderr, "")
210 flags.PrintDefaults()
211 }
212
213 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
214 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
215
216 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
217 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
218
219 flags.Parse(args)
220
221 if flags.NArg() != 0 {
222 flags.Usage()
223 os.Exit(1)
224 }
225
226 vars := strings.Fields(*varsStr)
227 absVars := strings.Fields(*absVarsStr)
228
229 allVars := append([]string{}, vars...)
230 allVars = append(allVars, absVars...)
231
232 if i := indexList("report_config", allVars); i != -1 {
233 allVars = append(allVars[:i], allVars[i+1:]...)
234 allVars = append(allVars, build.BannerVars...)
235 }
236
237 if len(allVars) == 0 {
238 return
239 }
240
241 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
242 if err != nil {
243 ctx.Fatal(err)
244 }
245
246 for _, name := range vars {
247 if name == "report_config" {
248 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
249 } else {
250 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
251 }
252 }
253 for _, name := range absVars {
254 var res []string
255 for _, path := range strings.Fields(varData[name]) {
256 abs, err := filepath.Abs(path)
257 if err != nil {
258 ctx.Fatalln("Failed to get absolute path of", path, err)
259 }
260 res = append(res, abs)
261 }
262 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
263 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700264}