blob: 0619b5ca7c9ee3f2cec6a68cda5125b696aaa475 [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 Willemsend9f6fa22016-08-21 15:17:17 -070029 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070030)
31
32func indexList(s string, list []string) int {
33 for i, l := range list {
34 if l == s {
35 return i
36 }
37 }
38
39 return -1
40}
41
42func inList(s string, list []string) bool {
43 return indexList(s, list) != -1
44}
45
46func main() {
47 log := logger.New(os.Stderr)
48 defer log.Cleanup()
49
Dan Willemsen051133b2017-07-14 11:29:29 -070050 if len(os.Args) < 2 || !(inList("--make-mode", os.Args) ||
51 os.Args[1] == "--dumpvars-mode" ||
52 os.Args[1] == "--dumpvar-mode") {
53
Dan Willemsen1e704462016-08-21 15:17:17 -070054 log.Fatalln("The `soong` native UI is not yet available.")
55 }
56
Dan Willemsen1e704462016-08-21 15:17:17 -070057 ctx, cancel := context.WithCancel(context.Background())
58 defer cancel()
59
Dan Willemsend9f6fa22016-08-21 15:17:17 -070060 trace := tracer.New(log)
61 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -070062
Dan Willemsend9f6fa22016-08-21 15:17:17 -070063 build.SetupSignals(log, cancel, func() {
64 trace.Close()
65 log.Cleanup()
66 })
67
68 buildCtx := build.Context{&build.ContextImpl{
Dan Willemsen1e704462016-08-21 15:17:17 -070069 Context: ctx,
70 Logger: log,
Dan Willemsend9f6fa22016-08-21 15:17:17 -070071 Tracer: trace,
Dan Willemsen1e704462016-08-21 15:17:17 -070072 StdioInterface: build.StdioImpl{},
Dan Willemsend9f6fa22016-08-21 15:17:17 -070073 }}
Dan Willemsen051133b2017-07-14 11:29:29 -070074 var config build.Config
75 if os.Args[1] == "--dumpvars-mode" || os.Args[1] == "--dumpvar-mode" {
76 config = build.NewConfig(buildCtx)
77 } else {
78 config = build.NewConfig(buildCtx, os.Args[1:]...)
79 }
Dan Willemsen1e704462016-08-21 15:17:17 -070080
81 log.SetVerbose(config.IsVerbose())
Dan Willemsend9f6fa22016-08-21 15:17:17 -070082 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -080083
84 if config.Dist() {
Dan Willemsenabc56d42017-03-01 17:38:41 -080085 logsDir := filepath.Join(config.DistDir(), "logs")
86 os.MkdirAll(logsDir, 0777)
87 log.SetOutput(filepath.Join(logsDir, "soong.log"))
88 trace.SetOutput(filepath.Join(logsDir, "build.trace"))
Dan Willemsen8a073a82017-02-04 17:30:44 -080089 } else {
90 log.SetOutput(filepath.Join(config.OutDir(), "soong.log"))
91 trace.SetOutput(filepath.Join(config.OutDir(), "build.trace"))
92 }
Dan Willemsen1e704462016-08-21 15:17:17 -070093
94 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
95 if !strings.HasSuffix(start, "N") {
96 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
97 log.Verbosef("Took %dms to start up.",
98 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Dan Willemsend9f6fa22016-08-21 15:17:17 -070099 buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700100 }
101 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700102
103 if executable, err := os.Executable(); err == nil {
104 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
105 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700106 }
107
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700108 f := build.NewSourceFinder(buildCtx, config)
109 defer f.Shutdown()
110 build.FindSources(buildCtx, config, f)
111
Dan Willemsen051133b2017-07-14 11:29:29 -0700112 if os.Args[1] == "--dumpvar-mode" {
113 dumpVar(buildCtx, config, os.Args[2:])
114 } else if os.Args[1] == "--dumpvars-mode" {
115 dumpVars(buildCtx, config, os.Args[2:])
116 } else {
Colin Crossfb941912017-11-13 23:38:57 +0000117 build.Build(buildCtx, config, build.BuildAll)
Dan Willemsen051133b2017-07-14 11:29:29 -0700118 }
119}
120
121func dumpVar(ctx build.Context, config build.Config, args []string) {
122 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
123 flags.Usage = func() {
124 fmt.Fprintf(os.Stderr, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
125 fmt.Fprintln(os.Stderr, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
126 fmt.Fprintln(os.Stderr, "")
127
128 fmt.Fprintln(os.Stderr, "'report_config' is a special case that prints the human-readable config banner")
129 fmt.Fprintln(os.Stderr, "from the beginning of the build.")
130 fmt.Fprintln(os.Stderr, "")
131 flags.PrintDefaults()
132 }
133 abs := flags.Bool("abs", false, "Print the absolute path of the value")
134 flags.Parse(args)
135
136 if flags.NArg() != 1 {
137 flags.Usage()
138 os.Exit(1)
139 }
140
141 varName := flags.Arg(0)
142 if varName == "report_config" {
143 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
144 if err != nil {
145 ctx.Fatal(err)
146 }
147
148 fmt.Println(build.Banner(varData))
149 } else {
150 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
151 if err != nil {
152 ctx.Fatal(err)
153 }
154
155 if *abs {
156 var res []string
157 for _, path := range strings.Fields(varData[varName]) {
158 if abs, err := filepath.Abs(path); err == nil {
159 res = append(res, abs)
160 } else {
161 ctx.Fatalln("Failed to get absolute path of", path, err)
162 }
163 }
164 fmt.Println(strings.Join(res, " "))
165 } else {
166 fmt.Println(varData[varName])
167 }
168 }
169}
170
171func dumpVars(ctx build.Context, config build.Config, args []string) {
172 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
173 flags.Usage = func() {
174 fmt.Fprintf(os.Stderr, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
175 fmt.Fprintln(os.Stderr, "In dumpvars mode, dump the values of one or more legacy make variables, in")
176 fmt.Fprintln(os.Stderr, "shell syntax. The resulting output may be sourced directly into a shell to")
177 fmt.Fprintln(os.Stderr, "set corresponding shell variables.")
178 fmt.Fprintln(os.Stderr, "")
179
180 fmt.Fprintln(os.Stderr, "'report_config' is a special case that dumps a variable containing the")
181 fmt.Fprintln(os.Stderr, "human-readable config banner from the beginning of the build.")
182 fmt.Fprintln(os.Stderr, "")
183 flags.PrintDefaults()
184 }
185
186 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
187 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
188
189 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
190 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
191
192 flags.Parse(args)
193
194 if flags.NArg() != 0 {
195 flags.Usage()
196 os.Exit(1)
197 }
198
199 vars := strings.Fields(*varsStr)
200 absVars := strings.Fields(*absVarsStr)
201
202 allVars := append([]string{}, vars...)
203 allVars = append(allVars, absVars...)
204
205 if i := indexList("report_config", allVars); i != -1 {
206 allVars = append(allVars[:i], allVars[i+1:]...)
207 allVars = append(allVars, build.BannerVars...)
208 }
209
210 if len(allVars) == 0 {
211 return
212 }
213
214 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
215 if err != nil {
216 ctx.Fatal(err)
217 }
218
219 for _, name := range vars {
220 if name == "report_config" {
221 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
222 } else {
223 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
224 }
225 }
226 for _, name := range absVars {
227 var res []string
228 for _, path := range strings.Fields(varData[name]) {
229 abs, err := filepath.Abs(path)
230 if err != nil {
231 ctx.Fatalln("Failed to get absolute path of", path, err)
232 }
233 res = append(res, abs)
234 }
235 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
236 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700237}