Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "os" |
| 20 | "path/filepath" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | "time" |
| 24 | |
| 25 | "android/soong/ui/build" |
| 26 | "android/soong/ui/logger" |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 27 | "android/soong/ui/tracer" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func indexList(s string, list []string) int { |
| 31 | for i, l := range list { |
| 32 | if l == s { |
| 33 | return i |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | return -1 |
| 38 | } |
| 39 | |
| 40 | func inList(s string, list []string) bool { |
| 41 | return indexList(s, list) != -1 |
| 42 | } |
| 43 | |
| 44 | func main() { |
| 45 | log := logger.New(os.Stderr) |
| 46 | defer log.Cleanup() |
| 47 | |
| 48 | if len(os.Args) < 2 || !inList("--make-mode", os.Args) { |
| 49 | log.Fatalln("The `soong` native UI is not yet available.") |
| 50 | } |
| 51 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 52 | ctx, cancel := context.WithCancel(context.Background()) |
| 53 | defer cancel() |
| 54 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 55 | trace := tracer.New(log) |
| 56 | defer trace.Close() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 57 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 58 | build.SetupSignals(log, cancel, func() { |
| 59 | trace.Close() |
| 60 | log.Cleanup() |
| 61 | }) |
| 62 | |
| 63 | buildCtx := build.Context{&build.ContextImpl{ |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 64 | Context: ctx, |
| 65 | Logger: log, |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 66 | Tracer: trace, |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 67 | StdioInterface: build.StdioImpl{}, |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 68 | }} |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 69 | config := build.NewConfig(buildCtx, os.Args[1:]...) |
| 70 | |
| 71 | log.SetVerbose(config.IsVerbose()) |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 72 | build.SetupOutDir(buildCtx, config) |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 73 | |
| 74 | if config.Dist() { |
Dan Willemsen | abc56d4 | 2017-03-01 17:38:41 -0800 | [diff] [blame] | 75 | logsDir := filepath.Join(config.DistDir(), "logs") |
| 76 | os.MkdirAll(logsDir, 0777) |
| 77 | log.SetOutput(filepath.Join(logsDir, "soong.log")) |
| 78 | trace.SetOutput(filepath.Join(logsDir, "build.trace")) |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 79 | } else { |
| 80 | log.SetOutput(filepath.Join(config.OutDir(), "soong.log")) |
| 81 | trace.SetOutput(filepath.Join(config.OutDir(), "build.trace")) |
| 82 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 83 | |
| 84 | if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok { |
| 85 | if !strings.HasSuffix(start, "N") { |
| 86 | if start_time, err := strconv.ParseUint(start, 10, 64); err == nil { |
| 87 | log.Verbosef("Took %dms to start up.", |
| 88 | time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds()) |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 89 | buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano())) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
Dan Willemsen | cae59bc | 2017-07-13 14:27:31 -0700 | [diff] [blame] | 92 | |
| 93 | if executable, err := os.Executable(); err == nil { |
| 94 | trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace")) |
| 95 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame^] | 98 | f := build.NewSourceFinder(buildCtx, config) |
| 99 | defer f.Shutdown() |
| 100 | build.FindSources(buildCtx, config, f) |
| 101 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 102 | build.Build(buildCtx, config, build.BuildAll) |
| 103 | } |