blob: 8220597d4f3ce785549c5eb10bf8e567df42f78d [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"
21 "time"
22
23 "github.com/google/blueprint/microfactory"
Dan Willemsen1e704462016-08-21 15:17:17 -070024)
25
Dan Willemsen1e704462016-08-21 15:17:17 -070026func runSoong(ctx Context, config Config) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070027 ctx.BeginTrace("soong")
28 defer ctx.EndTrace()
29
Dan Willemsen99a75cd2017-08-04 16:04:04 -070030 func() {
31 ctx.BeginTrace("blueprint bootstrap")
32 defer ctx.EndTrace()
33
34 cmd := Command(ctx, config, "blueprint bootstrap", "build/blueprint/bootstrap.bash", "-t")
35 cmd.Environment.Set("BLUEPRINTDIR", "./build/blueprint")
36 cmd.Environment.Set("BOOTSTRAP", "./build/blueprint/bootstrap.bash")
37 cmd.Environment.Set("BUILDDIR", config.SoongOutDir())
38 cmd.Environment.Set("GOROOT", filepath.Join("./prebuilts/go", config.HostPrebuiltTag()))
39 cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir())
40 cmd.Environment.Set("SRCDIR", ".")
41 cmd.Environment.Set("TOPNAME", "Android.bp")
42 cmd.Sandbox = soongSandbox
43 cmd.Stdout = ctx.Stdout()
44 cmd.Stderr = ctx.Stderr()
45 cmd.RunOrFatal()
46 }()
47
48 func() {
49 ctx.BeginTrace("environment check")
50 defer ctx.EndTrace()
51
52 envFile := filepath.Join(config.SoongOutDir(), ".soong.environment")
53 envTool := filepath.Join(config.SoongOutDir(), ".bootstrap/bin/soong_env")
54 if _, err := os.Stat(envFile); err == nil {
55 if _, err := os.Stat(envTool); err == nil {
56 cmd := Command(ctx, config, "soong_env", envTool, envFile)
57 cmd.Sandbox = soongSandbox
58 cmd.Stdout = ctx.Stdout()
59 cmd.Stderr = ctx.Stderr()
60 if err := cmd.Run(); err != nil {
61 ctx.Verboseln("soong_env failed, forcing manifest regeneration")
62 os.Remove(envFile)
63 }
64 } else {
65 ctx.Verboseln("Missing soong_env tool, forcing manifest regeneration")
66 os.Remove(envFile)
67 }
68 } else if !os.IsNotExist(err) {
69 ctx.Fatalf("Failed to stat %f: %v", envFile, err)
70 }
71 }()
72
73 func() {
74 ctx.BeginTrace("minibp")
75 defer ctx.EndTrace()
76
77 var cfg microfactory.Config
78 cfg.Map("github.com/google/blueprint", "build/blueprint")
79
80 if absPath, err := filepath.Abs("."); err == nil {
81 cfg.TrimPath = absPath
82 }
83
84 minibp := filepath.Join(config.SoongOutDir(), ".minibootstrap/minibp")
85 if _, err := microfactory.Build(&cfg, minibp, "github.com/google/blueprint/bootstrap/minibp"); err != nil {
86 ctx.Fatalln("Failed to build minibp:", err)
87 }
88 }()
89
90 ninja := func(name, file string) {
91 ctx.BeginTrace(name)
92 defer ctx.EndTrace()
93
94 cmd := Command(ctx, config, "soong "+name,
95 config.PrebuiltBuildTool("ninja"),
96 "-d", "keepdepfile",
97 "-w", "dupbuild=err",
98 "-j", strconv.Itoa(config.Parallel()),
99 "-f", filepath.Join(config.SoongOutDir(), file))
100 if config.IsVerbose() {
101 cmd.Args = append(cmd.Args, "-v")
102 }
103 cmd.Environment.Set("GOROOT", filepath.Join("./prebuilts/go", config.HostPrebuiltTag()))
104 cmd.Sandbox = soongSandbox
105 cmd.Stdin = ctx.Stdin()
106 cmd.Stdout = ctx.Stdout()
107 cmd.Stderr = ctx.Stderr()
108
109 defer ctx.ImportNinjaLog(filepath.Join(config.OutDir(), ".ninja_log"), time.Now())
110 cmd.RunOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700111 }
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700112
113 ninja("minibootstrap", ".minibootstrap/build.ninja")
114 ninja("bootstrap", ".bootstrap/build.ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700115}