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 | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 22 | |
Lukacs T. Berki | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 23 | "android/soong/shared" |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 24 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 25 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
| 26 | |
| 27 | "github.com/golang/protobuf/proto" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/microfactory" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 29 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 30 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 31 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 34 | func writeEnvironmentFile(ctx Context, envFile string, envDeps map[string]string) error { |
| 35 | data, err := shared.EnvFileContents(envDeps) |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | return ioutil.WriteFile(envFile, data, 0644) |
| 41 | } |
| 42 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 43 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 44 | // |
| 45 | // However, the execution of <builddir>/build.ninja happens later in build/soong/ui/build/build.go#Build() |
| 46 | // |
| 47 | // We want to rely on as few prebuilts as possible, so there is some bootstrapping here. |
| 48 | // |
| 49 | // "Microfactory" is a tool for compiling Go code. We use it to build two other tools: |
| 50 | // - minibp, used to generate build.ninja files. This is really build/blueprint/bootstrap/command.go#Main() |
| 51 | // - bpglob, used during incremental builds to identify files in a glob that have changed |
| 52 | // |
| 53 | // In reality, several build.ninja files are generated and/or used during the bootstrapping and build process. |
| 54 | // See build/blueprint/bootstrap/doc.go for more information. |
| 55 | // |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 56 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 57 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 58 | defer ctx.EndTrace() |
| 59 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 60 | // We have two environment files: .available is the one with every variable, |
| 61 | // .used with the ones that were actually used. The latter is used to |
| 62 | // determine whether Soong needs to be re-run since why re-run it if only |
| 63 | // unused variables were changed? |
| 64 | envFile := filepath.Join(config.SoongOutDir(), "soong.environment.available") |
| 65 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 66 | // 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] | 67 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 68 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 69 | defer ctx.EndTrace() |
| 70 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 71 | // Use validations to depend on tests. |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 72 | args := []string{"-n"} |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 73 | |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 74 | if !config.skipSoongTests { |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 75 | // Run tests. |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 76 | args = append(args, "-t") |
| 77 | } |
| 78 | |
| 79 | cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", args...) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 80 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 81 | cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint") |
| 82 | cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash") |
| 83 | cmd.Environment.Set("BUILDDIR", config.SoongOutDir()) |
Dan Willemsen | e7945d7 | 2018-01-23 22:15:15 -0800 | [diff] [blame] | 84 | cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag())) |
Jeff Gaston | d3e141d | 2017-08-08 17:46:01 -0700 | [diff] [blame] | 85 | 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] | 86 | cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir()) |
| 87 | cmd.Environment.Set("SRCDIR", ".") |
| 88 | cmd.Environment.Set("TOPNAME", "Android.bp") |
| 89 | cmd.Sandbox = soongSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 90 | |
| 91 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 92 | }() |
| 93 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 94 | soongBuildEnv := config.Environment().Copy() |
| 95 | soongBuildEnv.Set("TOP", os.Getenv("TOP")) |
| 96 | // These two dependencies are read from bootstrap.go, but also need to be here |
| 97 | // so that soong_build can declare a dependency on them |
| 98 | soongBuildEnv.Set("SOONG_DELVE", os.Getenv("SOONG_DELVE")) |
| 99 | soongBuildEnv.Set("SOONG_DELVE_PATH", os.Getenv("SOONG_DELVE_PATH")) |
| 100 | soongBuildEnv.Set("SOONG_OUTDIR", config.SoongOutDir()) |
| 101 | // For Bazel mixed builds. |
| 102 | soongBuildEnv.Set("BAZEL_PATH", "./tools/bazel") |
| 103 | soongBuildEnv.Set("BAZEL_HOME", filepath.Join(config.BazelOutDir(), "bazelhome")) |
| 104 | soongBuildEnv.Set("BAZEL_OUTPUT_BASE", filepath.Join(config.BazelOutDir(), "output")) |
| 105 | soongBuildEnv.Set("BAZEL_WORKSPACE", absPath(ctx, ".")) |
| 106 | soongBuildEnv.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir()) |
| 107 | |
Paul Duffin | 5e85c66 | 2021-03-05 12:26:14 +0000 | [diff] [blame] | 108 | err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap()) |
| 109 | if err != nil { |
| 110 | ctx.Fatalf("failed to write environment file %s: %s", envFile, err) |
| 111 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 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, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 115 | defer ctx.EndTrace() |
| 116 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 117 | envFile := filepath.Join(config.SoongOutDir(), "soong.environment.used") |
Lukacs T. Berki | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 118 | getenv := func(k string) string { |
Lukacs T. Berki | b4ced9d | 2021-03-10 15:43:06 +0100 | [diff] [blame^] | 119 | v, _ := soongBuildEnv.Get(k) |
Lukacs T. Berki | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 120 | return v |
| 121 | } |
| 122 | if stale, _ := shared.StaleEnvFile(envFile, getenv); stale { |
| 123 | os.Remove(envFile) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 124 | } |
| 125 | }() |
| 126 | |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 127 | var cfg microfactory.Config |
| 128 | cfg.Map("github.com/google/blueprint", "build/blueprint") |
| 129 | |
| 130 | cfg.TrimPath = absPath(ctx, ".") |
| 131 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 132 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 133 | ctx.BeginTrace(metrics.RunSoong, "minibp") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 134 | defer ctx.EndTrace() |
| 135 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 136 | minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp") |
| 137 | if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil { |
| 138 | ctx.Fatalln("Failed to build minibp:", err) |
| 139 | } |
| 140 | }() |
| 141 | |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 142 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 143 | ctx.BeginTrace(metrics.RunSoong, "bpglob") |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 144 | defer ctx.EndTrace() |
| 145 | |
| 146 | bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob") |
| 147 | if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil { |
| 148 | ctx.Fatalln("Failed to build bpglob:", err) |
| 149 | } |
| 150 | }() |
| 151 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 152 | ninja := func(name, file string) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 153 | ctx.BeginTrace(metrics.RunSoong, name) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 154 | defer ctx.EndTrace() |
| 155 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 156 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 157 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 158 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 159 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 160 | cmd := Command(ctx, config, "soong "+name, |
| 161 | config.PrebuiltBuildTool("ninja"), |
| 162 | "-d", "keepdepfile", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 163 | "-d", "stats", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 164 | "-o", "usesphonyoutputs=yes", |
| 165 | "-o", "preremoveoutputs=yes", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 166 | "-w", "dupbuild=err", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 167 | "-w", "outputdir=err", |
| 168 | "-w", "missingoutfile=err", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 169 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 170 | "--frontend_file", fifo, |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 171 | "-f", filepath.Join(config.SoongOutDir(), file)) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 172 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 173 | var ninjaEnv Environment |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 174 | |
| 175 | // This is currently how the command line to invoke soong_build finds the |
| 176 | // root of the source tree and the output root |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 177 | ninjaEnv.Set("TOP", os.Getenv("TOP")) |
| 178 | ninjaEnv.Set("SOONG_OUTDIR", config.SoongOutDir()) |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 179 | |
| 180 | // Needed for NonHermeticHostSystemTool() and that, only in tests. We should |
| 181 | // probably find a better way of running tests other than making $PATH |
| 182 | // available also to production builds. Note that this is not get same as |
| 183 | // os.Getenv("PATH"): config.Environment() contains the $PATH that redirects |
| 184 | // every binary through the path interposer. |
| 185 | configPath, _ := config.Environment().Get("PATH") |
| 186 | ninjaEnv.Set("PATH", configPath) |
| 187 | |
| 188 | // For debugging |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 189 | if os.Getenv("SOONG_DELVE") != "" { |
Lukacs T. Berki | b4ced9d | 2021-03-10 15:43:06 +0100 | [diff] [blame^] | 190 | ninjaEnv.Set("SOONG_DELVE", os.Getenv("SOONG_DELVE")) |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 191 | ninjaEnv.Set("SOONG_DELVE_PATH", shared.ResolveDelveBinary()) |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 194 | cmd.Environment = &ninjaEnv |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 195 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 196 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 197 | } |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 198 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 199 | // 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] | 200 | ninja("minibootstrap", ".minibootstrap/build.ninja") |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 201 | |
| 202 | // 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] | 203 | ninja("bootstrap", ".bootstrap/build.ninja") |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 204 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 205 | var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics |
| 206 | if shouldCollectBuildSoongMetrics(config) { |
| 207 | soongBuildMetrics := loadSoongBuildMetrics(ctx, config) |
| 208 | logSoongBuildMetrics(ctx, soongBuildMetrics) |
| 209 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 210 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 211 | distGzipFile(ctx, config, config.SoongNinjaFile(), "soong") |
| 212 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 213 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 214 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 215 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 216 | } |
| 217 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 218 | if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 219 | ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics) |
| 220 | } |
| 221 | } |
| 222 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 223 | func shouldCollectBuildSoongMetrics(config Config) bool { |
| 224 | // Do not collect metrics protobuf if the soong_build binary ran as the bp2build converter. |
| 225 | return config.Environment().IsFalse("GENERATE_BAZEL_FILES") |
| 226 | } |
| 227 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 228 | func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics { |
| 229 | soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb") |
| 230 | buf, err := ioutil.ReadFile(soongBuildMetricsFile) |
| 231 | if err != nil { |
| 232 | ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err) |
| 233 | } |
| 234 | soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{} |
| 235 | err = proto.Unmarshal(buf, soongBuildMetrics) |
| 236 | if err != nil { |
| 237 | ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 238 | } |
| 239 | return soongBuildMetrics |
| 240 | } |
| 241 | |
| 242 | func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 243 | ctx.Verbosef("soong_build metrics:") |
| 244 | ctx.Verbosef(" modules: %v", metrics.GetModules()) |
| 245 | ctx.Verbosef(" variants: %v", metrics.GetVariants()) |
| 246 | ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6) |
| 247 | ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount()) |
| 248 | ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6) |
| 249 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 250 | } |