blob: 8a26171e642e29c0ffcd1691af2f424ec00ba119 [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"
19 "os"
20 "path/filepath"
21 "strconv"
22 "strings"
23 "time"
24
25 "android/soong/ui/build"
26 "android/soong/ui/logger"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070027 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070028)
29
30func 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
40func inList(s string, list []string) bool {
41 return indexList(s, list) != -1
42}
43
44func 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 Willemsen1e704462016-08-21 15:17:17 -070052 ctx, cancel := context.WithCancel(context.Background())
53 defer cancel()
54
Dan Willemsend9f6fa22016-08-21 15:17:17 -070055 trace := tracer.New(log)
56 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -070057
Dan Willemsend9f6fa22016-08-21 15:17:17 -070058 build.SetupSignals(log, cancel, func() {
59 trace.Close()
60 log.Cleanup()
61 })
62
63 buildCtx := build.Context{&build.ContextImpl{
Dan Willemsen1e704462016-08-21 15:17:17 -070064 Context: ctx,
65 Logger: log,
Dan Willemsend9f6fa22016-08-21 15:17:17 -070066 Tracer: trace,
Dan Willemsen1e704462016-08-21 15:17:17 -070067 StdioInterface: build.StdioImpl{},
Dan Willemsend9f6fa22016-08-21 15:17:17 -070068 }}
Dan Willemsen1e704462016-08-21 15:17:17 -070069 config := build.NewConfig(buildCtx, os.Args[1:]...)
70
71 log.SetVerbose(config.IsVerbose())
Dan Willemsend9f6fa22016-08-21 15:17:17 -070072 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -080073
74 if config.Dist() {
Dan Willemsenabc56d42017-03-01 17:38:41 -080075 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 Willemsen8a073a82017-02-04 17:30:44 -080079 } else {
80 log.SetOutput(filepath.Join(config.OutDir(), "soong.log"))
81 trace.SetOutput(filepath.Join(config.OutDir(), "build.trace"))
82 }
Dan Willemsen1e704462016-08-21 15:17:17 -070083
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 Willemsend9f6fa22016-08-21 15:17:17 -070089 buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -070090 }
91 }
Dan Willemsencae59bc2017-07-13 14:27:31 -070092
93 if executable, err := os.Executable(); err == nil {
94 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
95 }
Dan Willemsen1e704462016-08-21 15:17:17 -070096 }
97
Jeff Gastonb64fc1c2017-08-04 12:30:12 -070098 f := build.NewSourceFinder(buildCtx, config)
99 defer f.Shutdown()
100 build.FindSources(buildCtx, config, f)
101
Dan Willemsen1e704462016-08-21 15:17:17 -0700102 build.Build(buildCtx, config, build.BuildAll)
103}