Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 18 | "io/ioutil" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 19 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "path/filepath" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 21 | "strconv" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 22 | "strings" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 23 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 24 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
| 25 | |
| 26 | "github.com/golang/protobuf/proto" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/microfactory" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 28 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 29 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 30 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 33 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 34 | // |
| 35 | // However, the execution of <builddir>/build.ninja happens later in build/soong/ui/build/build.go#Build() |
| 36 | // |
| 37 | // We want to rely on as few prebuilts as possible, so there is some bootstrapping here. |
| 38 | // |
| 39 | // "Microfactory" is a tool for compiling Go code. We use it to build two other tools: |
| 40 | // - minibp, used to generate build.ninja files. This is really build/blueprint/bootstrap/command.go#Main() |
| 41 | // - bpglob, used during incremental builds to identify files in a glob that have changed |
| 42 | // |
| 43 | // In reality, several build.ninja files are generated and/or used during the bootstrapping and build process. |
| 44 | // See build/blueprint/bootstrap/doc.go for more information. |
| 45 | // |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 46 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 47 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 48 | defer ctx.EndTrace() |
| 49 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 50 | // Use an anonymous inline function for tracing purposes (this pattern is used several times below). |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 51 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 52 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 53 | defer ctx.EndTrace() |
| 54 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 55 | // Use validations to depend on tests. |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 56 | args := []string{"-n"} |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 57 | |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 58 | if !config.skipSoongTests { |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 59 | // Run tests. |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 60 | args = append(args, "-t") |
| 61 | } |
| 62 | |
| 63 | cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", args...) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 64 | cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint") |
| 65 | cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash") |
| 66 | cmd.Environment.Set("BUILDDIR", config.SoongOutDir()) |
Dan Willemsen | e7945d7 | 2018-01-23 22:15:15 -0800 | [diff] [blame] | 67 | cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag())) |
Jeff Gaston | d3e141d | 2017-08-08 17:46:01 -0700 | [diff] [blame] | 68 | cmd.Environment.Set("BLUEPRINT_LIST_FILE", filepath.Join(config.FileListDir(), "Android.bp.list")) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 69 | cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir()) |
| 70 | cmd.Environment.Set("SRCDIR", ".") |
| 71 | cmd.Environment.Set("TOPNAME", "Android.bp") |
| 72 | cmd.Sandbox = soongSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 73 | |
| 74 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 75 | }() |
| 76 | |
| 77 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 78 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 79 | defer ctx.EndTrace() |
| 80 | |
| 81 | envFile := filepath.Join(config.SoongOutDir(), ".soong.environment") |
| 82 | envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env") |
| 83 | if _, err := os.Stat(envFile); err == nil { |
| 84 | if _, err := os.Stat(envTool); err == nil { |
| 85 | cmd := Command(ctx, config, "soong_env", envTool, envFile) |
| 86 | cmd.Sandbox = soongSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 87 | |
| 88 | var buf strings.Builder |
| 89 | cmd.Stdout = &buf |
| 90 | cmd.Stderr = &buf |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 91 | if err := cmd.Run(); err != nil { |
| 92 | ctx.Verboseln("soong_env failed, forcing manifest regeneration") |
| 93 | os.Remove(envFile) |
| 94 | } |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 95 | |
| 96 | if buf.Len() > 0 { |
| 97 | ctx.Verboseln(buf.String()) |
| 98 | } |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 99 | } else { |
| 100 | ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration") |
| 101 | os.Remove(envFile) |
| 102 | } |
| 103 | } else if !os.IsNotExist(err) { |
| 104 | ctx.Fatalf("Failed to stat %f: %v", envFile, err) |
| 105 | } |
| 106 | }() |
| 107 | |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 108 | var cfg microfactory.Config |
| 109 | cfg.Map("github.com/google/blueprint", "build/blueprint") |
| 110 | |
| 111 | cfg.TrimPath = absPath(ctx, ".") |
| 112 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 113 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 114 | ctx.BeginTrace(metrics.RunSoong, "minibp") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 115 | defer ctx.EndTrace() |
| 116 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 117 | minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp") |
| 118 | if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil { |
| 119 | ctx.Fatalln("Failed to build minibp:", err) |
| 120 | } |
| 121 | }() |
| 122 | |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 123 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 124 | ctx.BeginTrace(metrics.RunSoong, "bpglob") |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 125 | defer ctx.EndTrace() |
| 126 | |
| 127 | bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob") |
| 128 | if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil { |
| 129 | ctx.Fatalln("Failed to build bpglob:", err) |
| 130 | } |
| 131 | }() |
| 132 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 133 | ninja := func(name, file string) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 134 | ctx.BeginTrace(metrics.RunSoong, name) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 135 | defer ctx.EndTrace() |
| 136 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 137 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 138 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 139 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 140 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 141 | cmd := Command(ctx, config, "soong "+name, |
| 142 | config.PrebuiltBuildTool("ninja"), |
| 143 | "-d", "keepdepfile", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 144 | "-d", "stats", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 145 | "-o", "usesphonyoutputs=yes", |
| 146 | "-o", "preremoveoutputs=yes", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 147 | "-w", "dupbuild=err", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 148 | "-w", "outputdir=err", |
| 149 | "-w", "missingoutfile=err", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 150 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 151 | "--frontend_file", fifo, |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 152 | "-f", filepath.Join(config.SoongOutDir(), file)) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 153 | |
| 154 | // For Bazel mixed builds. |
| 155 | cmd.Environment.Set("BAZEL_PATH", "./tools/bazel") |
| 156 | cmd.Environment.Set("BAZEL_HOME", filepath.Join(config.BazelOutDir(), "bazelhome")) |
| 157 | cmd.Environment.Set("BAZEL_OUTPUT_BASE", filepath.Join(config.BazelOutDir(), "output")) |
| 158 | cmd.Environment.Set("BAZEL_WORKSPACE", absPath(ctx, ".")) |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame^] | 159 | cmd.Environment.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir()) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 160 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 161 | cmd.Environment.Set("SOONG_SANDBOX_SOONG_BUILD", "true") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 162 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 163 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 164 | } |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 165 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 166 | // This build generates .bootstrap/build.ninja, which is used in the next step. |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 167 | ninja("minibootstrap", ".minibootstrap/build.ninja") |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 168 | |
| 169 | // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build(). |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 170 | ninja("bootstrap", ".bootstrap/build.ninja") |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 171 | |
| 172 | soongBuildMetrics := loadSoongBuildMetrics(ctx, config) |
| 173 | logSoongBuildMetrics(ctx, soongBuildMetrics) |
| 174 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 175 | distGzipFile(ctx, config, config.SoongNinjaFile(), "soong") |
| 176 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 177 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 178 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 179 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 180 | } |
| 181 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 182 | if ctx.Metrics != nil { |
| 183 | ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics { |
| 188 | soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb") |
| 189 | buf, err := ioutil.ReadFile(soongBuildMetricsFile) |
| 190 | if err != nil { |
| 191 | ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err) |
| 192 | } |
| 193 | soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{} |
| 194 | err = proto.Unmarshal(buf, soongBuildMetrics) |
| 195 | if err != nil { |
| 196 | ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 197 | } |
| 198 | return soongBuildMetrics |
| 199 | } |
| 200 | |
| 201 | func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 202 | ctx.Verbosef("soong_build metrics:") |
| 203 | ctx.Verbosef(" modules: %v", metrics.GetModules()) |
| 204 | ctx.Verbosef(" variants: %v", metrics.GetVariants()) |
| 205 | ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6) |
| 206 | ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount()) |
| 207 | ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6) |
| 208 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 209 | } |