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 ( |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 18 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 19 | "path/filepath" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 20 | "strconv" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 21 | "strings" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint/microfactory" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 24 | |
| 25 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 28 | func runSoong(ctx Context, config Config) { |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 29 | ctx.BeginTrace("soong") |
| 30 | defer ctx.EndTrace() |
| 31 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 32 | 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 Willemsen | e7945d7 | 2018-01-23 22:15:15 -0800 | [diff] [blame] | 40 | cmd.Environment.Set("GOROOT", "./"+filepath.Join("prebuilts/go", config.HostPrebuiltTag())) |
Jeff Gaston | d3e141d | 2017-08-08 17:46:01 -0700 | [diff] [blame] | 41 | 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] | 42 | cmd.Environment.Set("NINJA_BUILDDIR", config.OutDir()) |
| 43 | cmd.Environment.Set("SRCDIR", ".") |
| 44 | cmd.Environment.Set("TOPNAME", "Android.bp") |
| 45 | cmd.Sandbox = soongSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 46 | |
| 47 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 48 | }() |
| 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 Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 60 | |
| 61 | var buf strings.Builder |
| 62 | cmd.Stdout = &buf |
| 63 | cmd.Stderr = &buf |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 64 | if err := cmd.Run(); err != nil { |
| 65 | ctx.Verboseln("soong_env failed, forcing manifest regeneration") |
| 66 | os.Remove(envFile) |
| 67 | } |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 68 | |
| 69 | if buf.Len() > 0 { |
| 70 | ctx.Verboseln(buf.String()) |
| 71 | } |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 72 | } 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 Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 81 | var cfg microfactory.Config |
| 82 | cfg.Map("github.com/google/blueprint", "build/blueprint") |
| 83 | |
| 84 | cfg.TrimPath = absPath(ctx, ".") |
| 85 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 86 | func() { |
| 87 | ctx.BeginTrace("minibp") |
| 88 | defer ctx.EndTrace() |
| 89 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 90 | 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 Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 96 | 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 Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 106 | ninja := func(name, file string) { |
| 107 | ctx.BeginTrace(name) |
| 108 | defer ctx.EndTrace() |
| 109 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 110 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
| 111 | status.NinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 112 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 113 | cmd := Command(ctx, config, "soong "+name, |
| 114 | config.PrebuiltBuildTool("ninja"), |
| 115 | "-d", "keepdepfile", |
| 116 | "-w", "dupbuild=err", |
| 117 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 118 | "--frontend_file", fifo, |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 119 | "-f", filepath.Join(config.SoongOutDir(), file)) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 120 | cmd.Sandbox = soongSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 121 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 122 | } |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 123 | |
| 124 | ninja("minibootstrap", ".minibootstrap/build.ninja") |
| 125 | ninja("bootstrap", ".bootstrap/build.ninja") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 126 | } |