Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 1 | // Copyright 2020 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 ( |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 20 | "path/filepath" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | func runBazel(ctx Context, config Config) { |
| 25 | // "droid" is the default ninja target. |
Jingwen Chen | 8024c95 | 2020-11-08 23:57:56 -0500 | [diff] [blame] | 26 | // TODO(b/160568333): stop hardcoding 'droid' to support building any |
| 27 | // Ninja target. |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 28 | outputGroups := "droid" |
| 29 | if len(config.ninjaArgs) > 0 { |
| 30 | // At this stage, the residue slice of args passed to ninja |
| 31 | // are the ninja targets to build, which can correspond directly |
| 32 | // to ninja_build's output_groups. |
| 33 | outputGroups = strings.Join(config.ninjaArgs, ",") |
| 34 | } |
| 35 | |
Jingwen Chen | a26ac3c | 2020-11-10 08:17:59 -0500 | [diff] [blame] | 36 | config.environ.Set("COMBINED_NINJA", config.CombinedNinjaFile()) |
| 37 | config.environ.Set("KATI_NINJA", config.KatiBuildNinjaFile()) |
| 38 | config.environ.Set("PACKAGE_NINJA", config.KatiPackageNinjaFile()) |
| 39 | config.environ.Set("SOONG_NINJA", config.SoongNinjaFile()) |
| 40 | |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 41 | bazelExecutable := filepath.Join("tools", "bazel") |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 42 | cmd := Command(ctx, config, "bazel", bazelExecutable) |
| 43 | |
| 44 | if extra_startup_args, ok := cmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok { |
| 45 | cmd.Args = append(cmd.Args, strings.Fields(extra_startup_args)...) |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 48 | cmd.Args = append(cmd.Args, |
| 49 | "build", |
| 50 | "--output_groups="+outputGroups, |
| 51 | ) |
| 52 | |
| 53 | if extra_build_args, ok := cmd.Environment.Get("BAZEL_BUILD_ARGS"); ok { |
| 54 | cmd.Args = append(cmd.Args, strings.Fields(extra_build_args)...) |
| 55 | } |
| 56 | |
| 57 | cmd.Args = append(cmd.Args, |
| 58 | "//:"+config.TargetProduct()+"-"+config.TargetBuildVariant(), |
| 59 | ) |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 60 | |
| 61 | cmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 62 | cmd.Environment.Set("SHELL", "/bin/bash") |
| 63 | |
| 64 | ctx.Println(cmd.Cmd) |
| 65 | cmd.Dir = filepath.Join(config.OutDir(), "..") |
| 66 | ctx.Status.Status("Starting Bazel..") |
| 67 | cmd.RunAndStreamOrFatal() |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 68 | |
| 69 | // Obtain the Bazel output directory for ninja_build. |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 70 | infoCmd := Command(ctx, config, "bazel", bazelExecutable) |
| 71 | |
| 72 | if extra_startup_args, ok := infoCmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok { |
| 73 | infoCmd.Args = append(infoCmd.Args, strings.Fields(extra_startup_args)...) |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 76 | infoCmd.Args = append(infoCmd.Args, |
| 77 | "info", |
| 78 | "output_path", |
| 79 | ) |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 80 | |
| 81 | infoCmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 82 | infoCmd.Environment.Set("SHELL", "/bin/bash") |
| 83 | infoCmd.Dir = filepath.Join(config.OutDir(), "..") |
| 84 | ctx.Status.Status("Getting Bazel Info..") |
| 85 | outputBasePath := string(infoCmd.OutputOrFatal()) |
| 86 | // TODO: Don't hardcode out/ as the bazel output directory. This is |
| 87 | // currently hardcoded as ninja_build.output_root. |
| 88 | bazelNinjaBuildOutputRoot := filepath.Join(outputBasePath, "..", "out") |
| 89 | |
| 90 | symlinkOutdir(ctx, config, bazelNinjaBuildOutputRoot, ".") |
| 91 | } |
| 92 | |
| 93 | // For all files F recursively under rootPath/relativePath, creates symlinks |
| 94 | // such that OutDir/F resolves to rootPath/F via symlinks. |
| 95 | func symlinkOutdir(ctx Context, config Config, rootPath string, relativePath string) { |
| 96 | destDir := filepath.Join(rootPath, relativePath) |
| 97 | os.MkdirAll(destDir, 0755) |
| 98 | files, err := ioutil.ReadDir(destDir) |
| 99 | if err != nil { |
| 100 | ctx.Fatal(err) |
| 101 | } |
| 102 | for _, f := range files { |
| 103 | destPath := filepath.Join(destDir, f.Name()) |
| 104 | srcPath := filepath.Join(config.OutDir(), relativePath, f.Name()) |
| 105 | if statResult, err := os.Stat(srcPath); err == nil { |
| 106 | if statResult.Mode().IsDir() && f.IsDir() { |
| 107 | // Directory under OutDir already exists, so recurse on its contents. |
| 108 | symlinkOutdir(ctx, config, rootPath, filepath.Join(relativePath, f.Name())) |
| 109 | } else if !statResult.Mode().IsDir() && !f.IsDir() { |
| 110 | // File exists both in source and destination, and it's not a directory |
| 111 | // in either location. Do nothing. |
| 112 | // This can arise for files which are generated under OutDir outside of |
| 113 | // soong_build, such as .bootstrap files. |
| 114 | } else { |
| 115 | // File is a directory in one location but not the other. Raise an error. |
| 116 | ctx.Fatalf("Could not link %s to %s due to conflict", srcPath, destPath) |
| 117 | } |
| 118 | } else if os.IsNotExist(err) { |
| 119 | // Create symlink srcPath -> fullDestPath. |
| 120 | os.Symlink(destPath, srcPath) |
| 121 | } else { |
| 122 | ctx.Fatalf("Unable to stat %s: %s", srcPath, err) |
| 123 | } |
| 124 | } |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 125 | } |