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 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 33 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 34 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 35 | defer ctx.EndTrace() |
| 36 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 37 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 38 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 39 | defer ctx.EndTrace() |
| 40 | |
| 41 | cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", "-t") |
| 42 | cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint") |
| 43 | cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash") |
| 44 | cmd.Environment.Set("BUILDDIR", config.SoongOutDir()) |
Dan Willemsen | e7945d7 | 2018-01-23 22:15:15 -0800 | [diff] [blame] | 45 | cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag())) |
Jeff Gaston | d3e141d | 2017-08-08 17:46:01 -0700 | [diff] [blame] | 46 | 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] | 47 | cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir()) |
| 48 | cmd.Environment.Set("SRCDIR", ".") |
| 49 | cmd.Environment.Set("TOPNAME", "Android.bp") |
| 50 | cmd.Sandbox = soongSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 51 | |
| 52 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 53 | }() |
| 54 | |
| 55 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 56 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 57 | defer ctx.EndTrace() |
| 58 | |
| 59 | envFile := filepath.Join(config.SoongOutDir(), ".soong.environment") |
| 60 | envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env") |
| 61 | if _, err := os.Stat(envFile); err == nil { |
| 62 | if _, err := os.Stat(envTool); err == nil { |
| 63 | cmd := Command(ctx, config, "soong_env", envTool, envFile) |
| 64 | cmd.Sandbox = soongSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 65 | |
| 66 | var buf strings.Builder |
| 67 | cmd.Stdout = &buf |
| 68 | cmd.Stderr = &buf |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 69 | if err := cmd.Run(); err != nil { |
| 70 | ctx.Verboseln("soong_env failed, forcing manifest regeneration") |
| 71 | os.Remove(envFile) |
| 72 | } |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 73 | |
| 74 | if buf.Len() > 0 { |
| 75 | ctx.Verboseln(buf.String()) |
| 76 | } |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 77 | } else { |
| 78 | ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration") |
| 79 | os.Remove(envFile) |
| 80 | } |
| 81 | } else if !os.IsNotExist(err) { |
| 82 | ctx.Fatalf("Failed to stat %f: %v", envFile, err) |
| 83 | } |
| 84 | }() |
| 85 | |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 86 | var cfg microfactory.Config |
| 87 | cfg.Map("github.com/google/blueprint", "build/blueprint") |
| 88 | |
| 89 | cfg.TrimPath = absPath(ctx, ".") |
| 90 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 91 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 92 | ctx.BeginTrace(metrics.RunSoong, "minibp") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 93 | defer ctx.EndTrace() |
| 94 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 95 | minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp") |
| 96 | if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil { |
| 97 | ctx.Fatalln("Failed to build minibp:", err) |
| 98 | } |
| 99 | }() |
| 100 | |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 101 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 102 | ctx.BeginTrace(metrics.RunSoong, "bpglob") |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 103 | defer ctx.EndTrace() |
| 104 | |
| 105 | bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob") |
| 106 | if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil { |
| 107 | ctx.Fatalln("Failed to build bpglob:", err) |
| 108 | } |
| 109 | }() |
| 110 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 111 | ninja := func(name, file string) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 112 | ctx.BeginTrace(metrics.RunSoong, name) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 113 | defer ctx.EndTrace() |
| 114 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 115 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 116 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 117 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 118 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 119 | cmd := Command(ctx, config, "soong "+name, |
| 120 | config.PrebuiltBuildTool("ninja"), |
| 121 | "-d", "keepdepfile", |
| 122 | "-w", "dupbuild=err", |
| 123 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 124 | "--frontend_file", fifo, |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 125 | "-f", filepath.Join(config.SoongOutDir(), file)) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 126 | cmd.Environment.Set("SOONG_SANDBOX_SOONG_BUILD", "true") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 127 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 128 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 129 | } |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 130 | |
| 131 | ninja("minibootstrap", ".minibootstrap/build.ninja") |
| 132 | ninja("bootstrap", ".bootstrap/build.ninja") |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame^] | 133 | |
| 134 | soongBuildMetrics := loadSoongBuildMetrics(ctx, config) |
| 135 | logSoongBuildMetrics(ctx, soongBuildMetrics) |
| 136 | |
| 137 | if ctx.Metrics != nil { |
| 138 | ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics { |
| 143 | soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb") |
| 144 | buf, err := ioutil.ReadFile(soongBuildMetricsFile) |
| 145 | if err != nil { |
| 146 | ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err) |
| 147 | } |
| 148 | soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{} |
| 149 | err = proto.Unmarshal(buf, soongBuildMetrics) |
| 150 | if err != nil { |
| 151 | ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 152 | } |
| 153 | return soongBuildMetrics |
| 154 | } |
| 155 | |
| 156 | func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 157 | ctx.Verbosef("soong_build metrics:") |
| 158 | ctx.Verbosef(" modules: %v", metrics.GetModules()) |
| 159 | ctx.Verbosef(" variants: %v", metrics.GetVariants()) |
| 160 | ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6) |
| 161 | ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount()) |
| 162 | ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6) |
| 163 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 164 | } |