blob: 478c02c05bc0b543178de1207f3518e0d7dc1256 [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
25 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070026)
27
Dan Willemsen1e704462016-08-21 15:17:17 -070028func runSoong(ctx Context, config Config) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070029 ctx.BeginTrace("soong")
30 defer ctx.EndTrace()
31
Dan Willemsen99a75cd2017-08-04 16:04:04 -070032 func() {
33 ctx.BeginTrace("blueprint bootstrap")
34 defer ctx.EndTrace()
35
36 cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", "-t")
37 cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint")
38 cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash")
39 cmd.Environment.Set("BUILDDIR", config.SoongOutDir())
Dan Willemsene7945d72018-01-23 22:15:15 -080040 cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag()))
Jeff Gastond3e141d2017-08-08 17:46:01 -070041 cmd.Environment.Set("BLUEPRINT_LIST_FILE", filepath.Join(config.FileListDir(), "Android.bp.list"))
Dan Willemsen99a75cd2017-08-04 16:04:04 -070042 cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir())
43 cmd.Environment.Set("SRCDIR", ".")
44 cmd.Environment.Set("TOPNAME", "Android.bp")
45 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -070046
47 cmd.RunAndPrintOrFatal()
Dan Willemsen99a75cd2017-08-04 16:04:04 -070048 }()
49
50 func() {
51 ctx.BeginTrace("environment check")
52 defer ctx.EndTrace()
53
54 envFile := filepath.Join(config.SoongOutDir(), ".soong.environment")
55 envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env")
56 if _, err := os.Stat(envFile); err == nil {
57 if _, err := os.Stat(envTool); err == nil {
58 cmd := Command(ctx, config, "soong_env", envTool, envFile)
59 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -070060
61 var buf strings.Builder
62 cmd.Stdout = &buf
63 cmd.Stderr = &buf
Dan Willemsen99a75cd2017-08-04 16:04:04 -070064 if err := cmd.Run(); err != nil {
65 ctx.Verboseln("soong_env failed, forcing manifest regeneration")
66 os.Remove(envFile)
67 }
Dan Willemsenb82471a2018-05-17 16:37:09 -070068
69 if buf.Len() > 0 {
70 ctx.Verboseln(buf.String())
71 }
Dan Willemsen99a75cd2017-08-04 16:04:04 -070072 } else {
73 ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration")
74 os.Remove(envFile)
75 }
76 } else if !os.IsNotExist(err) {
77 ctx.Fatalf("Failed to stat %f: %v", envFile, err)
78 }
79 }()
80
Dan Willemsen5af1cbe2018-07-05 21:46:51 -070081 var cfg microfactory.Config
82 cfg.Map("github.com/google/blueprint", "build/blueprint")
83
84 cfg.TrimPath = absPath(ctx, ".")
85
Dan Willemsen99a75cd2017-08-04 16:04:04 -070086 func() {
87 ctx.BeginTrace("minibp")
88 defer ctx.EndTrace()
89
Dan Willemsen99a75cd2017-08-04 16:04:04 -070090 minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp")
91 if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil {
92 ctx.Fatalln("Failed to build minibp:", err)
93 }
94 }()
95
Dan Willemsen5af1cbe2018-07-05 21:46:51 -070096 func() {
97 ctx.BeginTrace("bpglob")
98 defer ctx.EndTrace()
99
100 bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob")
101 if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil {
102 ctx.Fatalln("Failed to build bpglob:", err)
103 }
104 }()
105
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700106 ninja := func(name, file string) {
107 ctx.BeginTrace(name)
108 defer ctx.EndTrace()
109
Dan Willemsenb82471a2018-05-17 16:37:09 -0700110 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
111 status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
112
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700113 cmd := Command(ctx, config, "soong "+name,
114 config.PrebuiltBuildTool("ninja"),
115 "-d", "keepdepfile",
116 "-w", "dupbuild=err",
117 "-j", strconv.Itoa(config.Parallel()),
Dan Willemsen02736672018-07-17 17:54:31 -0700118 "--frontend_file", fifo,
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700119 "-f", filepath.Join(config.SoongOutDir(), file))
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700120 cmd.Sandbox = soongSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -0700121 cmd.RunAndPrintOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700122 }
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700123
124 ninja("minibootstrap", ".minibootstrap/build.ninja")
125 ninja("bootstrap", ".bootstrap/build.ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700126}