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" |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame] | 22 | |
| 23 | "android/soong/shared" |
Patrice Arruda | b7cf9ba | 2020-11-13 13:04:17 -0800 | [diff] [blame] | 24 | "android/soong/ui/metrics" |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 27 | // Main entry point to construct the Bazel build command line, environment |
| 28 | // variables and post-processing steps (e.g. converge output directories) |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 29 | func runBazel(ctx Context, config Config) { |
Patrice Arruda | b7cf9ba | 2020-11-13 13:04:17 -0800 | [diff] [blame] | 30 | ctx.BeginTrace(metrics.RunBazel, "bazel") |
| 31 | defer ctx.EndTrace() |
| 32 | |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 33 | // "droid" is the default ninja target. |
Jingwen Chen | 8024c95 | 2020-11-08 23:57:56 -0500 | [diff] [blame] | 34 | // TODO(b/160568333): stop hardcoding 'droid' to support building any |
| 35 | // Ninja target. |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 36 | outputGroups := "droid" |
| 37 | if len(config.ninjaArgs) > 0 { |
| 38 | // At this stage, the residue slice of args passed to ninja |
| 39 | // are the ninja targets to build, which can correspond directly |
| 40 | // to ninja_build's output_groups. |
| 41 | outputGroups = strings.Join(config.ninjaArgs, ",") |
| 42 | } |
| 43 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 44 | // Environment variables are the primary mechanism to pass information from |
| 45 | // soong_ui configuration or context to Bazel. |
| 46 | // |
| 47 | // Use *_NINJA variables to pass the root-relative path of the combined, |
| 48 | // kati-generated, soong-generated, and packaging Ninja files to Bazel. |
| 49 | // Bazel reads these from the lunch() repository rule. |
Jingwen Chen | a26ac3c | 2020-11-10 08:17:59 -0500 | [diff] [blame] | 50 | config.environ.Set("COMBINED_NINJA", config.CombinedNinjaFile()) |
| 51 | config.environ.Set("KATI_NINJA", config.KatiBuildNinjaFile()) |
| 52 | config.environ.Set("PACKAGE_NINJA", config.KatiPackageNinjaFile()) |
| 53 | config.environ.Set("SOONG_NINJA", config.SoongNinjaFile()) |
| 54 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 55 | // `tools/bazel` is the default entry point for executing Bazel in the AOSP |
| 56 | // source tree. |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 57 | bazelExecutable := filepath.Join("tools", "bazel") |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 58 | cmd := Command(ctx, config, "bazel", bazelExecutable) |
| 59 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 60 | // Append custom startup flags to the Bazel command. Startup flags affect |
| 61 | // the Bazel server itself, and any changes to these flags would incur a |
| 62 | // restart of the server, losing much of the in-memory incrementality. |
| 63 | if extraStartupArgs, ok := cmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok { |
| 64 | cmd.Args = append(cmd.Args, strings.Fields(extraStartupArgs)...) |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 67 | // Start constructing the `build` command. |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame] | 68 | actionName := "build" |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 69 | cmd.Args = append(cmd.Args, |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame] | 70 | actionName, |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 71 | // Use output_groups to select the set of outputs to produce from a |
| 72 | // ninja_build target. |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 73 | "--output_groups="+outputGroups, |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 74 | // Generate a performance profile |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame] | 75 | "--profile="+filepath.Join(shared.BazelMetricsFilename(config.OutDir(), actionName)), |
| 76 | "--slim_profile=true", |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 77 | ) |
| 78 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 79 | // Append custom build flags to the Bazel command. Changes to these flags |
| 80 | // may invalidate Bazel's analysis cache. |
| 81 | if extraBuildArgs, ok := cmd.Environment.Get("BAZEL_BUILD_ARGS"); ok { |
| 82 | cmd.Args = append(cmd.Args, strings.Fields(extraBuildArgs)...) |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 85 | // Append the label of the default ninja_build target. |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 86 | cmd.Args = append(cmd.Args, |
| 87 | "//:"+config.TargetProduct()+"-"+config.TargetBuildVariant(), |
| 88 | ) |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 89 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 90 | // Ensure that the PATH environment variable value used in the action |
| 91 | // environment is the restricted set computed from soong_ui, and not a |
| 92 | // user-provided one, for hermeticity reasons. |
Jingwen Chen | 3a4b58d | 2020-11-16 04:19:35 -0500 | [diff] [blame] | 93 | if pathEnvValue, ok := config.environ.Get("PATH"); ok { |
| 94 | cmd.Environment.Set("PATH", pathEnvValue) |
| 95 | cmd.Args = append(cmd.Args, "--action_env=PATH="+pathEnvValue) |
| 96 | } |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 97 | |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 98 | cmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 99 | cmd.Environment.Set("SHELL", "/bin/bash") |
| 100 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 101 | // Print the full command line for debugging purposes. |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 102 | ctx.Println(cmd.Cmd) |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 103 | |
| 104 | // Execute the command at the root of the directory. |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 105 | cmd.Dir = filepath.Join(config.OutDir(), "..") |
| 106 | ctx.Status.Status("Starting Bazel..") |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 107 | |
| 108 | // Execute the build command. |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 109 | cmd.RunAndStreamOrFatal() |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 110 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 111 | // Post-processing steps start here. Once the Bazel build completes, the |
| 112 | // output files are still stored in the execution root, not in $OUT_DIR. |
| 113 | // Ensure that the $OUT_DIR contains the expected set of files by symlinking |
| 114 | // the files from the execution root's output direction into $OUT_DIR. |
| 115 | |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 116 | // Obtain the Bazel output directory for ninja_build. |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 117 | infoCmd := Command(ctx, config, "bazel", bazelExecutable) |
| 118 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 119 | if extraStartupArgs, ok := infoCmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok { |
| 120 | infoCmd.Args = append(infoCmd.Args, strings.Fields(extraStartupArgs)...) |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Jingwen Chen | 7a5391a | 2020-11-17 03:42:12 -0500 | [diff] [blame] | 123 | // Obtain the output directory path in the execution root. |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 124 | infoCmd.Args = append(infoCmd.Args, |
| 125 | "info", |
| 126 | "output_path", |
| 127 | ) |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 128 | |
| 129 | infoCmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 130 | infoCmd.Environment.Set("SHELL", "/bin/bash") |
| 131 | infoCmd.Dir = filepath.Join(config.OutDir(), "..") |
| 132 | ctx.Status.Status("Getting Bazel Info..") |
| 133 | outputBasePath := string(infoCmd.OutputOrFatal()) |
| 134 | // TODO: Don't hardcode out/ as the bazel output directory. This is |
| 135 | // currently hardcoded as ninja_build.output_root. |
| 136 | bazelNinjaBuildOutputRoot := filepath.Join(outputBasePath, "..", "out") |
| 137 | |
| 138 | symlinkOutdir(ctx, config, bazelNinjaBuildOutputRoot, ".") |
| 139 | } |
| 140 | |
| 141 | // For all files F recursively under rootPath/relativePath, creates symlinks |
| 142 | // such that OutDir/F resolves to rootPath/F via symlinks. |
| 143 | func symlinkOutdir(ctx Context, config Config, rootPath string, relativePath string) { |
| 144 | destDir := filepath.Join(rootPath, relativePath) |
| 145 | os.MkdirAll(destDir, 0755) |
| 146 | files, err := ioutil.ReadDir(destDir) |
| 147 | if err != nil { |
| 148 | ctx.Fatal(err) |
| 149 | } |
| 150 | for _, f := range files { |
| 151 | destPath := filepath.Join(destDir, f.Name()) |
| 152 | srcPath := filepath.Join(config.OutDir(), relativePath, f.Name()) |
| 153 | if statResult, err := os.Stat(srcPath); err == nil { |
| 154 | if statResult.Mode().IsDir() && f.IsDir() { |
| 155 | // Directory under OutDir already exists, so recurse on its contents. |
| 156 | symlinkOutdir(ctx, config, rootPath, filepath.Join(relativePath, f.Name())) |
| 157 | } else if !statResult.Mode().IsDir() && !f.IsDir() { |
| 158 | // File exists both in source and destination, and it's not a directory |
| 159 | // in either location. Do nothing. |
| 160 | // This can arise for files which are generated under OutDir outside of |
| 161 | // soong_build, such as .bootstrap files. |
| 162 | } else { |
| 163 | // File is a directory in one location but not the other. Raise an error. |
| 164 | ctx.Fatalf("Could not link %s to %s due to conflict", srcPath, destPath) |
| 165 | } |
| 166 | } else if os.IsNotExist(err) { |
| 167 | // Create symlink srcPath -> fullDestPath. |
| 168 | os.Symlink(destPath, srcPath) |
| 169 | } else { |
| 170 | ctx.Fatalf("Unable to stat %s: %s", srcPath, err) |
| 171 | } |
| 172 | } |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 173 | } |