blob: 01a39a216e2e36a0606e161e3763f507e911703c [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 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 "flag"
19 "fmt"
20 "os"
21 "path/filepath"
22
Colin Cross70b40592015-03-23 12:57:34 -070023 "github.com/google/blueprint/bootstrap"
Colin Cross3f40fa42015-01-30 17:27:36 -080024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -080026)
27
Colin Crosse87040b2017-12-11 15:52:26 -080028var (
Jingwen Chen5ba7e472020-07-15 10:06:41 +000029 docFile string
30 bazelOverlayDir string
Colin Crosse87040b2017-12-11 15:52:26 -080031)
32
33func init() {
34 flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output")
Jingwen Chen5ba7e472020-07-15 10:06:41 +000035 flag.StringVar(&bazelOverlayDir, "bazel_overlay_dir", "", "path to the bazel overlay directory")
Colin Crosse87040b2017-12-11 15:52:26 -080036}
37
Jeff Gaston088e29e2017-11-29 16:47:17 -080038func newNameResolver(config android.Config) *android.NameResolver {
39 namespacePathsToExport := make(map[string]bool)
40
Dan Willemsen3fb1fae2018-03-12 15:30:26 -070041 for _, namespaceName := range config.ExportedNamespaces() {
Jeff Gaston088e29e2017-11-29 16:47:17 -080042 namespacePathsToExport[namespaceName] = true
43 }
44
45 namespacePathsToExport["."] = true // always export the root namespace
46
47 exportFilter := func(namespace *android.Namespace) bool {
48 return namespacePathsToExport[namespace.Path]
49 }
50
51 return android.NewNameResolver(exportFilter)
52}
53
Colin Cross3f40fa42015-01-30 17:27:36 -080054func main() {
Lukacs T. Berkia5e0f712020-05-18 09:50:18 +020055 android.ReexecWithDelveMaybe()
Colin Cross3f40fa42015-01-30 17:27:36 -080056 flag.Parse()
57
58 // The top-level Blueprints file is passed as the first argument.
59 srcDir := filepath.Dir(flag.Arg(0))
60
Colin Cross798bfce2016-10-12 14:28:16 -070061 ctx := android.NewContext()
Colin Crosscec81712017-07-13 14:43:27 -070062 ctx.Register()
Colin Cross3f40fa42015-01-30 17:27:36 -080063
Chris Parsons8f232a22020-06-23 17:37:05 -040064 configuration, err := android.NewConfig(srcDir, bootstrap.BuildDir, bootstrap.ModuleListFile)
Colin Cross3f40fa42015-01-30 17:27:36 -080065 if err != nil {
66 fmt.Fprintf(os.Stderr, "%s", err)
67 os.Exit(1)
68 }
69
Jingwen Chen5ba7e472020-07-15 10:06:41 +000070 if !shouldPrepareBuildActions() {
Colin Crosse87040b2017-12-11 15:52:26 -080071 configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
72 }
73
Jeff Gaston088e29e2017-11-29 16:47:17 -080074 ctx.SetNameInterface(newNameResolver(configuration))
Colin Cross3f40fa42015-01-30 17:27:36 -080075
Colin Cross6ff51382015-12-17 16:39:19 -080076 ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
77
Colin Crossaa812d12019-06-19 13:33:24 -070078 extraNinjaDeps := []string{configuration.ConfigFileName, configuration.ProductVariablesFileName}
79
80 // Read the SOONG_DELVE again through configuration so that there is a dependency on the environment variable
81 // and soong_build will rerun when it is set for the first time.
82 if listen := configuration.Getenv("SOONG_DELVE"); listen != "" {
83 // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is
84 // enabled even if it completed successfully.
85 extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve"))
86 }
87
88 bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...)
Colin Crosse87040b2017-12-11 15:52:26 -080089
Jingwen Chen5ba7e472020-07-15 10:06:41 +000090 if bazelOverlayDir != "" {
91 if err := createBazelOverlay(ctx, bazelOverlayDir); err != nil {
92 fmt.Fprintf(os.Stderr, "%s", err)
93 os.Exit(1)
94 }
95 }
96
Colin Crosse87040b2017-12-11 15:52:26 -080097 if docFile != "" {
Sasha Smundakff483392019-02-07 12:10:56 -080098 if err := writeDocs(ctx, docFile); err != nil {
Colin Cross7089c272019-01-25 22:43:35 -080099 fmt.Fprintf(os.Stderr, "%s", err)
100 os.Exit(1)
101 }
Colin Crosse87040b2017-12-11 15:52:26 -0800102 }
Colin Crossb72c9092020-02-10 11:23:49 -0800103
104 // TODO(ccross): make this a command line argument. Requires plumbing through blueprint
105 // to affect the command line of the primary builder.
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000106 if shouldPrepareBuildActions() {
Colin Crossb72c9092020-02-10 11:23:49 -0800107 metricsFile := filepath.Join(bootstrap.BuildDir, "soong_build_metrics.pb")
108 err = android.WriteMetrics(configuration, metricsFile)
109 if err != nil {
110 fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err)
111 os.Exit(1)
112 }
113 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800114}
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000115
116func shouldPrepareBuildActions() bool {
117 // If we're writing soong_docs or bazel_overlay, don't write build.ninja or
118 // collect metrics.
119 return docFile == "" && bazelOverlayDir == ""
120}