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 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 "android/soong/ui/tracer" 28) 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 52 ctx, cancel := context.WithCancel(context.Background()) 53 defer cancel() 54 55 trace := tracer.New(log) 56 defer trace.Close() 57 58 build.SetupSignals(log, cancel, func() { 59 trace.Close() 60 log.Cleanup() 61 }) 62 63 buildCtx := build.Context{&build.ContextImpl{ 64 Context: ctx, 65 Logger: log, 66 Tracer: trace, 67 StdioInterface: build.StdioImpl{}, 68 }} 69 config := build.NewConfig(buildCtx, os.Args[1:]...) 70 71 log.SetVerbose(config.IsVerbose()) 72 build.SetupOutDir(buildCtx, config) 73 74 if config.Dist() { 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")) 79 } else { 80 log.SetOutput(filepath.Join(config.OutDir(), "soong.log")) 81 trace.SetOutput(filepath.Join(config.OutDir(), "build.trace")) 82 } 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()) 89 buildCtx.CompleteTrace("startup", start_time, uint64(time.Now().UnixNano())) 90 } 91 } 92 } 93 94 build.Build(buildCtx, config, build.BuildAll) 95} 96