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