blob: 994c54dc079a353d252f8ee6b2e78049757f58dc [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"
27)
28
29func indexList(s string, list []string) int {
30 for i, l := range list {
31 if l == s {
32 return i
33 }
34 }
35
36 return -1
37}
38
39func inList(s string, list []string) bool {
40 return indexList(s, list) != -1
41}
42
43func main() {
44 log := logger.New(os.Stderr)
45 defer log.Cleanup()
46
47 if len(os.Args) < 2 || !inList("--make-mode", os.Args) {
48 log.Fatalln("The `soong` native UI is not yet available.")
49 }
50
51 // Precondition: the current directory is the top of the source tree
52 if _, err := os.Stat("build/soong/root.bp"); err != nil {
53 if os.IsNotExist(err) {
54 log.Fatalln("soong_ui should run from the root of the source directory: build/soong/root.bp not found")
55 }
56 log.Fatalln("Error verifying tree state:", err)
57 }
58
59 ctx, cancel := context.WithCancel(context.Background())
60 defer cancel()
61
62 build.SetupSignals(log, cancel, log.Cleanup)
63
64 buildCtx := &build.ContextImpl{
65 Context: ctx,
66 Logger: log,
67 StdioInterface: build.StdioImpl{},
68 }
69 config := build.NewConfig(buildCtx, os.Args[1:]...)
70
71 log.SetVerbose(config.IsVerbose())
72 if err := os.MkdirAll(config.OutDir(), 0777); err != nil {
73 log.Fatalf("Error creating out directory: %v", err)
74 }
75 log.SetOutput(filepath.Join(config.OutDir(), "build.log"))
76
77 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
78 if !strings.HasSuffix(start, "N") {
79 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
80 log.Verbosef("Took %dms to start up.",
81 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
82 }
83 }
84 }
85
86 build.Build(buildCtx, config, build.BuildAll)
87}