blob: c89f0d52894f3fb6ef97653945435635f0151298 [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 (
Dan Willemsen99a75cd2017-08-04 16:04:04 -070018 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070019 "path/filepath"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070020 "strconv"
Dan Willemsenb82471a2018-05-17 16:37:09 -070021 "strings"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070022
23 "github.com/google/blueprint/microfactory"
Dan Willemsenb82471a2018-05-17 16:37:09 -070024
Nan Zhang17f27672018-12-12 16:01:49 -080025 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070026 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070027)
28
Dan Willemsen1e704462016-08-21 15:17:17 -070029func runSoong(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -080030 ctx.BeginTrace(metrics.RunSoong, "soong")
Dan Willemsend9f6fa22016-08-21 15:17:17 -070031 defer ctx.EndTrace()
32
Dan Willemsen99a75cd2017-08-04 16:04:04 -070033 func() {
Nan Zhang17f27672018-12-12 16:01:49 -080034 ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
Dan Willemsen99a75cd2017-08-04 16:04:04 -070035 defer ctx.EndTrace()
36
37 cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", "-t")
38 cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint")
39 cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash")
40 cmd.Environment.Set("BUILDDIR", config.SoongOutDir())
Dan Willemsene7945d72018-01-23 22:15:15 -080041 cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag()))
Jeff Gastond3e141d2017-08-08 17:46:01 -070042 cmd.Environment.Set("BLUEPRINT_LIST_FILE", filepath.Join(config.FileListDir(), "Android.bp.list"))
Dan Willemsen99a75cd2017-08-04 16:04:04 -070043 cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir())
44 cmd.Environment.Set("SRCDIR", ".")
45 cmd.Environment.Set("TOPNAME", "Android.bp")
46 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -070047
48 cmd.RunAndPrintOrFatal()
Dan Willemsen99a75cd2017-08-04 16:04:04 -070049 }()
50
51 func() {
Nan Zhang17f27672018-12-12 16:01:49 -080052 ctx.BeginTrace(metrics.RunSoong, "environment check")
Dan Willemsen99a75cd2017-08-04 16:04:04 -070053 defer ctx.EndTrace()
54
55 envFile := filepath.Join(config.SoongOutDir(), ".soong.environment")
56 envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env")
57 if _, err := os.Stat(envFile); err == nil {
58 if _, err := os.Stat(envTool); err == nil {
59 cmd := Command(ctx, config, "soong_env", envTool, envFile)
60 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -070061
62 var buf strings.Builder
63 cmd.Stdout = &buf
64 cmd.Stderr = &buf
Dan Willemsen99a75cd2017-08-04 16:04:04 -070065 if err := cmd.Run(); err != nil {
66 ctx.Verboseln("soong_env failed, forcing manifest regeneration")
67 os.Remove(envFile)
68 }
Dan Willemsenb82471a2018-05-17 16:37:09 -070069
70 if buf.Len() > 0 {
71 ctx.Verboseln(buf.String())
72 }
Dan Willemsen99a75cd2017-08-04 16:04:04 -070073 } else {
74 ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration")
75 os.Remove(envFile)
76 }
77 } else if !os.IsNotExist(err) {
78 ctx.Fatalf("Failed to stat %f: %v", envFile, err)
79 }
80 }()
81
Dan Willemsen5af1cbe2018-07-05 21:46:51 -070082 var cfg microfactory.Config
83 cfg.Map("github.com/google/blueprint", "build/blueprint")
84
85 cfg.TrimPath = absPath(ctx, ".")
86
Dan Willemsen99a75cd2017-08-04 16:04:04 -070087 func() {
Nan Zhang17f27672018-12-12 16:01:49 -080088 ctx.BeginTrace(metrics.RunSoong, "minibp")
Dan Willemsen99a75cd2017-08-04 16:04:04 -070089 defer ctx.EndTrace()
90
Dan Willemsen99a75cd2017-08-04 16:04:04 -070091 minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp")
92 if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil {
93 ctx.Fatalln("Failed to build minibp:", err)
94 }
95 }()
96
Dan Willemsen5af1cbe2018-07-05 21:46:51 -070097 func() {
Nan Zhang17f27672018-12-12 16:01:49 -080098 ctx.BeginTrace(metrics.RunSoong, "bpglob")
Dan Willemsen5af1cbe2018-07-05 21:46:51 -070099 defer ctx.EndTrace()
100
101 bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob")
102 if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil {
103 ctx.Fatalln("Failed to build bpglob:", err)
104 }
105 }()
106
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700107 ninja := func(name, file string) {
Nan Zhang17f27672018-12-12 16:01:49 -0800108 ctx.BeginTrace(metrics.RunSoong, name)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700109 defer ctx.EndTrace()
110
Dan Willemsenb82471a2018-05-17 16:37:09 -0700111 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
112 status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
113
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700114 cmd := Command(ctx, config, "soong "+name,
115 config.PrebuiltBuildTool("ninja"),
116 "-d", "keepdepfile",
117 "-w", "dupbuild=err",
118 "-j", strconv.Itoa(config.Parallel()),
Dan Willemsen02736672018-07-17 17:54:31 -0700119 "--frontend_file", fifo,
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700120 "-f", filepath.Join(config.SoongOutDir(), file))
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700121 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -0700122 cmd.RunAndPrintOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700123 }
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700124
125 ninja("minibootstrap", ".minibootstrap/build.ninja")
126 ninja("bootstrap", ".bootstrap/build.ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700127}