Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [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 android |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "errors" |
| 20 | "fmt" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 21 | "io/ioutil" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 22 | "os" |
| 23 | "os/exec" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 24 | "path/filepath" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 25 | "runtime" |
| 26 | "strings" |
| 27 | "sync" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 28 | |
| 29 | "github.com/google/blueprint/bootstrap" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 32 | type CqueryRequestType int |
| 33 | |
| 34 | const ( |
| 35 | getAllFiles CqueryRequestType = iota |
| 36 | ) |
| 37 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 38 | // Map key to describe bazel cquery requests. |
| 39 | type cqueryKey struct { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 40 | label string |
| 41 | requestType CqueryRequestType |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | type BazelContext interface { |
| 45 | // The below methods involve queuing cquery requests to be later invoked |
| 46 | // by bazel. If any of these methods return (_, false), then the request |
| 47 | // has been queued to be run later. |
| 48 | |
| 49 | // Returns result files built by building the given bazel target label. |
| 50 | GetAllFiles(label string) ([]string, bool) |
| 51 | |
| 52 | // TODO(cparsons): Other cquery-related methods should be added here. |
| 53 | // ** End cquery methods |
| 54 | |
| 55 | // Issues commands to Bazel to receive results for all cquery requests |
| 56 | // queued in the BazelContext. |
| 57 | InvokeBazel() error |
| 58 | |
| 59 | // Returns true if bazel is enabled for the given configuration. |
| 60 | BazelEnabled() bool |
| 61 | } |
| 62 | |
| 63 | // A context object which tracks queued requests that need to be made to Bazel, |
| 64 | // and their results after the requests have been made. |
| 65 | type bazelContext struct { |
| 66 | homeDir string |
| 67 | bazelPath string |
| 68 | outputBase string |
| 69 | workspaceDir string |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 70 | buildDir string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 71 | |
| 72 | requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel |
| 73 | requestMutex sync.Mutex // requests can be written in parallel |
| 74 | |
| 75 | results map[cqueryKey]string // Results of cquery requests after Bazel invocations |
| 76 | } |
| 77 | |
| 78 | var _ BazelContext = &bazelContext{} |
| 79 | |
| 80 | // A bazel context to use when Bazel is disabled. |
| 81 | type noopBazelContext struct{} |
| 82 | |
| 83 | var _ BazelContext = noopBazelContext{} |
| 84 | |
| 85 | // A bazel context to use for tests. |
| 86 | type MockBazelContext struct { |
| 87 | AllFiles map[string][]string |
| 88 | } |
| 89 | |
| 90 | func (m MockBazelContext) GetAllFiles(label string) ([]string, bool) { |
| 91 | result, ok := m.AllFiles[label] |
| 92 | return result, ok |
| 93 | } |
| 94 | |
| 95 | func (m MockBazelContext) InvokeBazel() error { |
| 96 | panic("unimplemented") |
| 97 | } |
| 98 | |
| 99 | func (m MockBazelContext) BazelEnabled() bool { |
| 100 | return true |
| 101 | } |
| 102 | |
| 103 | var _ BazelContext = MockBazelContext{} |
| 104 | |
| 105 | func (bazelCtx *bazelContext) GetAllFiles(label string) ([]string, bool) { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 106 | result, ok := bazelCtx.cquery(label, getAllFiles) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 107 | if ok { |
| 108 | bazelOutput := strings.TrimSpace(result) |
| 109 | return strings.Split(bazelOutput, ", "), true |
| 110 | } else { |
| 111 | return nil, false |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func (n noopBazelContext) GetAllFiles(label string) ([]string, bool) { |
| 116 | panic("unimplemented") |
| 117 | } |
| 118 | |
| 119 | func (n noopBazelContext) InvokeBazel() error { |
| 120 | panic("unimplemented") |
| 121 | } |
| 122 | |
| 123 | func (n noopBazelContext) BazelEnabled() bool { |
| 124 | return false |
| 125 | } |
| 126 | |
| 127 | func NewBazelContext(c *config) (BazelContext, error) { |
Chris Parsons | 8b77a00 | 2020-10-27 18:59:25 -0400 | [diff] [blame] | 128 | // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds" |
| 129 | // are production ready. |
| 130 | if c.Getenv("USE_BAZEL_ANALYSIS") != "1" { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 131 | return noopBazelContext{}, nil |
| 132 | } |
| 133 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 134 | bazelCtx := bazelContext{buildDir: c.buildDir, requests: make(map[cqueryKey]bool)} |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 135 | missingEnvVars := []string{} |
| 136 | if len(c.Getenv("BAZEL_HOME")) > 1 { |
| 137 | bazelCtx.homeDir = c.Getenv("BAZEL_HOME") |
| 138 | } else { |
| 139 | missingEnvVars = append(missingEnvVars, "BAZEL_HOME") |
| 140 | } |
| 141 | if len(c.Getenv("BAZEL_PATH")) > 1 { |
| 142 | bazelCtx.bazelPath = c.Getenv("BAZEL_PATH") |
| 143 | } else { |
| 144 | missingEnvVars = append(missingEnvVars, "BAZEL_PATH") |
| 145 | } |
| 146 | if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 { |
| 147 | bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE") |
| 148 | } else { |
| 149 | missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE") |
| 150 | } |
| 151 | if len(c.Getenv("BAZEL_WORKSPACE")) > 1 { |
| 152 | bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE") |
| 153 | } else { |
| 154 | missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE") |
| 155 | } |
| 156 | if len(missingEnvVars) > 0 { |
| 157 | return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars)) |
| 158 | } else { |
| 159 | return &bazelCtx, nil |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func (context *bazelContext) BazelEnabled() bool { |
| 164 | return true |
| 165 | } |
| 166 | |
| 167 | // Adds a cquery request to the Bazel request queue, to be later invoked, or |
| 168 | // returns the result of the given request if the request was already made. |
| 169 | // If the given request was already made (and the results are available), then |
| 170 | // returns (result, true). If the request is queued but no results are available, |
| 171 | // then returns ("", false). |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 172 | func (context *bazelContext) cquery(label string, requestType CqueryRequestType) (string, bool) { |
| 173 | key := cqueryKey{label, requestType} |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 174 | if result, ok := context.results[key]; ok { |
| 175 | return result, true |
| 176 | } else { |
| 177 | context.requestMutex.Lock() |
| 178 | defer context.requestMutex.Unlock() |
| 179 | context.requests[key] = true |
| 180 | return "", false |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func pwdPrefix() string { |
| 185 | // Darwin doesn't have /proc |
| 186 | if runtime.GOOS != "darwin" { |
| 187 | return "PWD=/proc/self/cwd" |
| 188 | } |
| 189 | return "" |
| 190 | } |
| 191 | |
| 192 | func (context *bazelContext) issueBazelCommand(command string, labels []string, |
| 193 | extraFlags ...string) (string, error) { |
| 194 | |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 195 | cmdFlags := []string{"--output_base=" + context.outputBase, command} |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 196 | cmdFlags = append(cmdFlags, labels...) |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 197 | cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir()) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 198 | cmdFlags = append(cmdFlags, extraFlags...) |
| 199 | |
| 200 | bazelCmd := exec.Command(context.bazelPath, cmdFlags...) |
| 201 | bazelCmd.Dir = context.workspaceDir |
| 202 | bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix()) |
| 203 | |
Colin Cross | ff0278b | 2020-10-09 19:24:15 -0700 | [diff] [blame] | 204 | stderr := &bytes.Buffer{} |
| 205 | bazelCmd.Stderr = stderr |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 206 | |
| 207 | if output, err := bazelCmd.Output(); err != nil { |
| 208 | return "", fmt.Errorf("bazel command failed. command: [%s], error [%s]", bazelCmd, stderr) |
| 209 | } else { |
| 210 | return string(output), nil |
| 211 | } |
| 212 | } |
| 213 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 214 | // Returns the string contents of a workspace file that should be output |
| 215 | // adjacent to the main bzl file and build file. |
| 216 | // This workspace file allows, via local_repository rule, sourcetree-level |
| 217 | // BUILD targets to be referenced via @sourceroot. |
| 218 | func (context *bazelContext) workspaceFileContents() []byte { |
| 219 | formatString := ` |
| 220 | # This file is generated by soong_build. Do not edit. |
| 221 | local_repository( |
| 222 | name = "sourceroot", |
| 223 | path = "%s", |
| 224 | ) |
| 225 | ` |
| 226 | return []byte(fmt.Sprintf(formatString, context.workspaceDir)) |
| 227 | } |
| 228 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 229 | func (context *bazelContext) mainBzlFileContents() []byte { |
| 230 | contents := ` |
| 231 | # This file is generated by soong_build. Do not edit. |
| 232 | def _mixed_build_root_impl(ctx): |
| 233 | return [DefaultInfo(files = depset(ctx.files.deps))] |
| 234 | |
| 235 | mixed_build_root = rule( |
| 236 | implementation = _mixed_build_root_impl, |
| 237 | attrs = {"deps" : attr.label_list()}, |
| 238 | ) |
| 239 | ` |
| 240 | return []byte(contents) |
| 241 | } |
| 242 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 243 | // Returns a "canonicalized" corresponding to the given sourcetree-level label. |
| 244 | // This abstraction is required because a sourcetree label such as //foo/bar:baz |
| 245 | // must be referenced via the local repository prefix, such as |
| 246 | // @sourceroot//foo/bar:baz. |
| 247 | func canonicalizeLabel(label string) string { |
| 248 | if strings.HasPrefix(label, "//") { |
| 249 | return "@sourceroot" + label |
| 250 | } else { |
| 251 | return "@sourceroot//" + label |
| 252 | } |
| 253 | } |
| 254 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 255 | func (context *bazelContext) mainBuildFileContents() []byte { |
| 256 | formatString := ` |
| 257 | # This file is generated by soong_build. Do not edit. |
| 258 | load(":main.bzl", "mixed_build_root") |
| 259 | |
| 260 | mixed_build_root(name = "buildroot", |
| 261 | deps = [%s], |
| 262 | ) |
| 263 | ` |
| 264 | var buildRootDeps []string = nil |
| 265 | for val, _ := range context.requests { |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 266 | buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 267 | } |
| 268 | buildRootDepsString := strings.Join(buildRootDeps, ",\n ") |
| 269 | |
| 270 | return []byte(fmt.Sprintf(formatString, buildRootDepsString)) |
| 271 | } |
| 272 | |
| 273 | func (context *bazelContext) cqueryStarlarkFileContents() []byte { |
| 274 | formatString := ` |
| 275 | # This file is generated by soong_build. Do not edit. |
| 276 | getAllFilesLabels = { |
| 277 | %s |
| 278 | } |
| 279 | |
| 280 | def format(target): |
| 281 | if str(target.label) in getAllFilesLabels: |
| 282 | return str(target.label) + ">>" + ', '.join([f.path for f in target.files.to_list()]) |
| 283 | else: |
| 284 | # This target was not requested via cquery, and thus must be a dependency |
| 285 | # of a requested target. |
| 286 | return "" |
| 287 | ` |
| 288 | var buildRootDeps []string = nil |
| 289 | // TODO(cparsons): Sort by request type instead of assuming all requests |
| 290 | // are of GetAllFiles type. |
| 291 | for val, _ := range context.requests { |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 292 | buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\" : True", canonicalizeLabel(val.label))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 293 | } |
| 294 | buildRootDepsString := strings.Join(buildRootDeps, ",\n ") |
| 295 | |
| 296 | return []byte(fmt.Sprintf(formatString, buildRootDepsString)) |
| 297 | } |
| 298 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 299 | // Returns a workspace-relative path containing build-related metadata required |
| 300 | // for interfacing with Bazel. Example: out/soong/bazel. |
| 301 | func (context *bazelContext) intermediatesDir() string { |
| 302 | return filepath.Join(context.buildDir, "bazel") |
| 303 | } |
| 304 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 305 | // Issues commands to Bazel to receive results for all cquery requests |
| 306 | // queued in the BazelContext. |
| 307 | func (context *bazelContext) InvokeBazel() error { |
| 308 | context.results = make(map[cqueryKey]string) |
| 309 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 310 | var cqueryOutput string |
| 311 | var err error |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 312 | |
| 313 | err = os.Mkdir(absolutePath(context.intermediatesDir()), 0777) |
| 314 | if err != nil { |
| 315 | return err |
| 316 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 317 | err = ioutil.WriteFile( |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 318 | absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 319 | context.mainBzlFileContents(), 0666) |
| 320 | if err != nil { |
| 321 | return err |
| 322 | } |
| 323 | err = ioutil.WriteFile( |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 324 | absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 325 | context.mainBuildFileContents(), 0666) |
| 326 | if err != nil { |
| 327 | return err |
| 328 | } |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 329 | cquery_file_relpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery") |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 330 | err = ioutil.WriteFile( |
| 331 | absolutePath(cquery_file_relpath), |
| 332 | context.cqueryStarlarkFileContents(), 0666) |
| 333 | if err != nil { |
| 334 | return err |
| 335 | } |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 336 | workspace_file_relpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel") |
| 337 | err = ioutil.WriteFile( |
| 338 | absolutePath(workspace_file_relpath), |
| 339 | context.workspaceFileContents(), 0666) |
| 340 | if err != nil { |
| 341 | return err |
| 342 | } |
| 343 | buildroot_label := "//:buildroot" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 344 | cqueryOutput, err = context.issueBazelCommand("cquery", |
| 345 | []string{fmt.Sprintf("deps(%s)", buildroot_label)}, |
| 346 | "--output=starlark", |
| 347 | "--starlark:file="+cquery_file_relpath) |
| 348 | |
| 349 | if err != nil { |
| 350 | return err |
| 351 | } |
| 352 | |
| 353 | cqueryResults := map[string]string{} |
| 354 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 355 | if strings.Contains(outputLine, ">>") { |
| 356 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 357 | cqueryResults[splitLine[0]] = splitLine[1] |
| 358 | } |
| 359 | } |
| 360 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 361 | for val, _ := range context.requests { |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 362 | if cqueryResult, ok := cqueryResults[canonicalizeLabel(val.label)]; ok { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 363 | context.results[val] = string(cqueryResult) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 364 | } else { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 365 | return fmt.Errorf("missing result for bazel target %s", val.label) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | // Issue a build command. |
| 370 | // TODO(cparsons): Invoking bazel execution during soong_build should be avoided; |
| 371 | // bazel actions should either be added to the Ninja file and executed later, |
| 372 | // or bazel should handle execution. |
| 373 | // TODO(cparsons): Use --target_pattern_file to avoid command line limits. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 374 | _, err = context.issueBazelCommand("build", []string{buildroot_label}) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 375 | |
| 376 | if err != nil { |
| 377 | return err |
| 378 | } |
| 379 | |
| 380 | // Clear requests. |
| 381 | context.requests = map[cqueryKey]bool{} |
| 382 | return nil |
| 383 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 384 | |
| 385 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 386 | // for correctness of builds which use Bazel. |
| 387 | func BazelSingleton() Singleton { |
| 388 | return &bazelSingleton{} |
| 389 | } |
| 390 | |
| 391 | type bazelSingleton struct{} |
| 392 | |
| 393 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 394 | if ctx.Config().BazelContext.BazelEnabled() { |
| 395 | bazelBuildList := absolutePath(filepath.Join( |
| 396 | filepath.Dir(bootstrap.ModuleListFile), "bazel.list")) |
| 397 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 398 | |
| 399 | data, err := ioutil.ReadFile(bazelBuildList) |
| 400 | if err != nil { |
| 401 | ctx.Errorf(err.Error()) |
| 402 | } |
| 403 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 404 | for _, file := range files { |
| 405 | ctx.AddNinjaFileDeps(file) |
| 406 | } |
| 407 | } |
| 408 | } |