blob: b20237cf1f79b2cd8e37972af971b98e4ed64549 [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 build
16
17import (
Colin Crossb72c9092020-02-10 11:23:49 -080018 "io/ioutil"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070019 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "path/filepath"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070021 "strconv"
Dan Willemsenb82471a2018-05-17 16:37:09 -070022 "strings"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070023
Colin Crossb72c9092020-02-10 11:23:49 -080024 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
25
26 "github.com/golang/protobuf/proto"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070027 "github.com/google/blueprint/microfactory"
Dan Willemsenb82471a2018-05-17 16:37:09 -070028
Nan Zhang17f27672018-12-12 16:01:49 -080029 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070030 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070031)
32
Dan Willemsen1e704462016-08-21 15:17:17 -070033func runSoong(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -080034 ctx.BeginTrace(metrics.RunSoong, "soong")
Dan Willemsend9f6fa22016-08-21 15:17:17 -070035 defer ctx.EndTrace()
36
Dan Willemsen99a75cd2017-08-04 16:04:04 -070037 func() {
Nan Zhang17f27672018-12-12 16:01:49 -080038 ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
Dan Willemsen99a75cd2017-08-04 16:04:04 -070039 defer ctx.EndTrace()
40
Colin Cross00a8a3f2020-10-29 14:08:31 -070041 args := []string{"-n"}
42 if !config.skipSoongTests {
43 args = append(args, "-t")
44 }
45
46 cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", args...)
Dan Willemsen99a75cd2017-08-04 16:04:04 -070047 cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint")
48 cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash")
49 cmd.Environment.Set("BUILDDIR", config.SoongOutDir())
Dan Willemsene7945d72018-01-23 22:15:15 -080050 cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag()))
Jeff Gastond3e141d2017-08-08 17:46:01 -070051 cmd.Environment.Set("BLUEPRINT_LIST_FILE", filepath.Join(config.FileListDir(), "Android.bp.list"))
Dan Willemsen99a75cd2017-08-04 16:04:04 -070052 cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir())
53 cmd.Environment.Set("SRCDIR", ".")
54 cmd.Environment.Set("TOPNAME", "Android.bp")
55 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -070056
57 cmd.RunAndPrintOrFatal()
Dan Willemsen99a75cd2017-08-04 16:04:04 -070058 }()
59
60 func() {
Nan Zhang17f27672018-12-12 16:01:49 -080061 ctx.BeginTrace(metrics.RunSoong, "environment check")
Dan Willemsen99a75cd2017-08-04 16:04:04 -070062 defer ctx.EndTrace()
63
64 envFile := filepath.Join(config.SoongOutDir(), ".soong.environment")
65 envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env")
66 if _, err := os.Stat(envFile); err == nil {
67 if _, err := os.Stat(envTool); err == nil {
68 cmd := Command(ctx, config, "soong_env", envTool, envFile)
69 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -070070
71 var buf strings.Builder
72 cmd.Stdout = &buf
73 cmd.Stderr = &buf
Dan Willemsen99a75cd2017-08-04 16:04:04 -070074 if err := cmd.Run(); err != nil {
75 ctx.Verboseln("soong_env failed, forcing manifest regeneration")
76 os.Remove(envFile)
77 }
Dan Willemsenb82471a2018-05-17 16:37:09 -070078
79 if buf.Len() > 0 {
80 ctx.Verboseln(buf.String())
81 }
Dan Willemsen99a75cd2017-08-04 16:04:04 -070082 } else {
83 ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration")
84 os.Remove(envFile)
85 }
86 } else if !os.IsNotExist(err) {
87 ctx.Fatalf("Failed to stat %f: %v", envFile, err)
88 }
89 }()
90
Dan Willemsen5af1cbe2018-07-05 21:46:51 -070091 var cfg microfactory.Config
92 cfg.Map("github.com/google/blueprint", "build/blueprint")
93
94 cfg.TrimPath = absPath(ctx, ".")
95
Dan Willemsen99a75cd2017-08-04 16:04:04 -070096 func() {
Nan Zhang17f27672018-12-12 16:01:49 -080097 ctx.BeginTrace(metrics.RunSoong, "minibp")
Dan Willemsen99a75cd2017-08-04 16:04:04 -070098 defer ctx.EndTrace()
99
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700100 minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp")
101 if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil {
102 ctx.Fatalln("Failed to build minibp:", err)
103 }
104 }()
105
Dan Willemsen5af1cbe2018-07-05 21:46:51 -0700106 func() {
Nan Zhang17f27672018-12-12 16:01:49 -0800107 ctx.BeginTrace(metrics.RunSoong, "bpglob")
Dan Willemsen5af1cbe2018-07-05 21:46:51 -0700108 defer ctx.EndTrace()
109
110 bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob")
111 if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil {
112 ctx.Fatalln("Failed to build bpglob:", err)
113 }
114 }()
115
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700116 ninja := func(name, file string) {
Nan Zhang17f27672018-12-12 16:01:49 -0800117 ctx.BeginTrace(metrics.RunSoong, name)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700118 defer ctx.EndTrace()
119
Dan Willemsenb82471a2018-05-17 16:37:09 -0700120 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
Colin Crossb98d3bc2019-03-21 16:02:58 -0700121 nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
122 defer nr.Close()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700123
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700124 cmd := Command(ctx, config, "soong "+name,
125 config.PrebuiltBuildTool("ninja"),
126 "-d", "keepdepfile",
Dan Willemsen08218222020-05-18 14:02:02 -0700127 "-d", "stats",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700128 "-o", "usesphonyoutputs=yes",
129 "-o", "preremoveoutputs=yes",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700130 "-w", "dupbuild=err",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700131 "-w", "outputdir=err",
132 "-w", "missingoutfile=err",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700133 "-j", strconv.Itoa(config.Parallel()),
Dan Willemsen02736672018-07-17 17:54:31 -0700134 "--frontend_file", fifo,
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700135 "-f", filepath.Join(config.SoongOutDir(), file))
Colin Cross988414c2020-01-11 01:11:46 +0000136 cmd.Environment.Set("SOONG_SANDBOX_SOONG_BUILD", "true")
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700137 cmd.Sandbox = soongSandbox
Colin Cross7b97ecd2019-06-19 13:17:59 -0700138 cmd.RunAndStreamOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700139 }
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700140
141 ninja("minibootstrap", ".minibootstrap/build.ninja")
142 ninja("bootstrap", ".bootstrap/build.ninja")
Colin Crossb72c9092020-02-10 11:23:49 -0800143
144 soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
145 logSoongBuildMetrics(ctx, soongBuildMetrics)
146
Colin Cross8ba7d472020-06-25 11:27:52 -0700147 distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
148
149 if !config.SkipMake() {
150 distGzipFile(ctx, config, config.SoongAndroidMk(), "soong")
151 distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong")
152 }
153
Colin Crossb72c9092020-02-10 11:23:49 -0800154 if ctx.Metrics != nil {
155 ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
156 }
157}
158
159func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics {
160 soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb")
161 buf, err := ioutil.ReadFile(soongBuildMetricsFile)
162 if err != nil {
163 ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err)
164 }
165 soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{}
166 err = proto.Unmarshal(buf, soongBuildMetrics)
167 if err != nil {
168 ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err)
169 }
170 return soongBuildMetrics
171}
172
173func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) {
174 ctx.Verbosef("soong_build metrics:")
175 ctx.Verbosef(" modules: %v", metrics.GetModules())
176 ctx.Verbosef(" variants: %v", metrics.GetVariants())
177 ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6)
178 ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount())
179 ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6)
180
Dan Willemsen1e704462016-08-21 15:17:17 -0700181}