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 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 29 | "android/soong/bazel/cquery" |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame] | 30 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 31 | "github.com/google/blueprint/bootstrap" |
| 32 | |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 33 | "android/soong/bazel" |
| 34 | "android/soong/shared" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 35 | ) |
| 36 | |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 37 | type cqueryRequest interface { |
| 38 | // Name returns a string name for this request type. Such request type names must be unique, |
| 39 | // and must only consist of alphanumeric characters. |
| 40 | Name() string |
| 41 | |
| 42 | // StarlarkFunctionBody returns a starlark function body to process this request type. |
| 43 | // The returned string is the body of a Starlark function which obtains |
| 44 | // all request-relevant information about a target and returns a string containing |
| 45 | // this information. |
| 46 | // The function should have the following properties: |
| 47 | // - `target` is the only parameter to this function (a configured target). |
| 48 | // - The return value must be a string. |
| 49 | // - The function body should not be indented outside of its own scope. |
| 50 | StarlarkFunctionBody() string |
| 51 | } |
| 52 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 53 | // Map key to describe bazel cquery requests. |
| 54 | type cqueryKey struct { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 55 | label string |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 56 | requestType cqueryRequest |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 57 | archType ArchType |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | type BazelContext interface { |
| 61 | // The below methods involve queuing cquery requests to be later invoked |
| 62 | // by bazel. If any of these methods return (_, false), then the request |
| 63 | // has been queued to be run later. |
| 64 | |
| 65 | // Returns result files built by building the given bazel target label. |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 66 | GetOutputFiles(label string, archType ArchType) ([]string, bool) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 67 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 68 | // TODO(cparsons): Other cquery-related methods should be added here. |
| 69 | // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order). |
| 70 | GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 71 | |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 72 | // GetPrebuiltCcStaticLibraryFiles returns paths to prebuilt cc static libraries, and whether the |
| 73 | // results were available |
| 74 | GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) |
| 75 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 76 | // ** End cquery methods |
| 77 | |
| 78 | // Issues commands to Bazel to receive results for all cquery requests |
| 79 | // queued in the BazelContext. |
| 80 | InvokeBazel() error |
| 81 | |
| 82 | // Returns true if bazel is enabled for the given configuration. |
| 83 | BazelEnabled() bool |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 84 | |
| 85 | // Returns the bazel output base (the root directory for all bazel intermediate outputs). |
| 86 | OutputBase() string |
| 87 | |
| 88 | // Returns build statements which should get registered to reflect Bazel's outputs. |
| 89 | BuildStatementsToRegister() []bazel.BuildStatement |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 92 | type bazelRunner interface { |
| 93 | issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error) |
| 94 | } |
| 95 | |
| 96 | type bazelPaths struct { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 97 | homeDir string |
| 98 | bazelPath string |
| 99 | outputBase string |
| 100 | workspaceDir string |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 101 | buildDir string |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 102 | metricsDir string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 103 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 104 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 105 | // A context object which tracks queued requests that need to be made to Bazel, |
| 106 | // and their results after the requests have been made. |
| 107 | type bazelContext struct { |
| 108 | bazelRunner |
| 109 | paths *bazelPaths |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 110 | requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel |
| 111 | requestMutex sync.Mutex // requests can be written in parallel |
| 112 | |
| 113 | results map[cqueryKey]string // Results of cquery requests after Bazel invocations |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 114 | |
| 115 | // Build statements which should get registered to reflect Bazel's outputs. |
| 116 | buildStatements []bazel.BuildStatement |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | var _ BazelContext = &bazelContext{} |
| 120 | |
| 121 | // A bazel context to use when Bazel is disabled. |
| 122 | type noopBazelContext struct{} |
| 123 | |
| 124 | var _ BazelContext = noopBazelContext{} |
| 125 | |
| 126 | // A bazel context to use for tests. |
| 127 | type MockBazelContext struct { |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 128 | OutputBaseDir string |
| 129 | |
| 130 | LabelToOutputFiles map[string][]string |
| 131 | LabelToOutputFilesAndCcObjectFiles map[string]cquery.GetOutputFilesAndCcObjectFiles_Result |
| 132 | LabelToCcStaticLibraryFiles map[string][]string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 135 | func (m MockBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) { |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 136 | result, ok := m.LabelToOutputFiles[label] |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 137 | return result, ok |
| 138 | } |
| 139 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 140 | func (m MockBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) { |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 141 | result, ok := m.LabelToOutputFilesAndCcObjectFiles[label] |
| 142 | return result.OutputFiles, result.CcObjectFiles, ok |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 145 | func (m MockBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) { |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 146 | result, ok := m.LabelToCcStaticLibraryFiles[label] |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 147 | return result, ok |
| 148 | } |
| 149 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 150 | func (m MockBazelContext) InvokeBazel() error { |
| 151 | panic("unimplemented") |
| 152 | } |
| 153 | |
| 154 | func (m MockBazelContext) BazelEnabled() bool { |
| 155 | return true |
| 156 | } |
| 157 | |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 158 | func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 159 | |
| 160 | func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 161 | return []bazel.BuildStatement{} |
| 162 | } |
| 163 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 164 | var _ BazelContext = MockBazelContext{} |
| 165 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 166 | func (bazelCtx *bazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) { |
| 167 | rawString, ok := bazelCtx.cquery(label, cquery.GetOutputFiles, archType) |
| 168 | var ret []string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 169 | if ok { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 170 | bazelOutput := strings.TrimSpace(rawString) |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 171 | ret = cquery.GetOutputFiles.ParseResult(bazelOutput) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 172 | } |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 173 | return ret, ok |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 176 | func (bazelCtx *bazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) { |
| 177 | var outputFiles []string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 178 | var ccObjects []string |
| 179 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 180 | result, ok := bazelCtx.cquery(label, cquery.GetOutputFilesAndCcObjectFiles, archType) |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 181 | if ok { |
| 182 | bazelOutput := strings.TrimSpace(result) |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 183 | returnResult := cquery.GetOutputFilesAndCcObjectFiles.ParseResult(bazelOutput) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 184 | outputFiles = returnResult.OutputFiles |
| 185 | ccObjects = returnResult.CcObjectFiles |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 186 | } |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 187 | |
| 188 | return outputFiles, ccObjects, ok |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 191 | // GetPrebuiltCcStaticLibraryFiles returns a slice of prebuilt static libraries for the given |
| 192 | // label/archType if there are query results; otherwise, it enqueues the query and returns false. |
| 193 | func (bazelCtx *bazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) { |
| 194 | result, ok := bazelCtx.cquery(label, cquery.GetPrebuiltCcStaticLibraryFiles, archType) |
| 195 | if !ok { |
| 196 | return nil, false |
| 197 | } |
| 198 | |
| 199 | bazelOutput := strings.TrimSpace(result) |
| 200 | ret := cquery.GetPrebuiltCcStaticLibraryFiles.ParseResult(bazelOutput) |
| 201 | return ret, ok |
| 202 | } |
| 203 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 204 | func (n noopBazelContext) GetOutputFiles(label string, archType ArchType) ([]string, bool) { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 205 | panic("unimplemented") |
| 206 | } |
| 207 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 208 | func (n noopBazelContext) GetOutputFilesAndCcObjectFiles(label string, archType ArchType) ([]string, []string, bool) { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 209 | panic("unimplemented") |
| 210 | } |
| 211 | |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 212 | func (n noopBazelContext) GetPrebuiltCcStaticLibraryFiles(label string, archType ArchType) ([]string, bool) { |
| 213 | panic("unimplemented") |
| 214 | } |
| 215 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 216 | func (n noopBazelContext) InvokeBazel() error { |
| 217 | panic("unimplemented") |
| 218 | } |
| 219 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 220 | func (m noopBazelContext) OutputBase() string { |
| 221 | return "" |
| 222 | } |
| 223 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 224 | func (n noopBazelContext) BazelEnabled() bool { |
| 225 | return false |
| 226 | } |
| 227 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 228 | func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 229 | return []bazel.BuildStatement{} |
| 230 | } |
| 231 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 232 | func NewBazelContext(c *config) (BazelContext, error) { |
Chris Parsons | 8b77a00 | 2020-10-27 18:59:25 -0400 | [diff] [blame] | 233 | // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds" |
| 234 | // are production ready. |
| 235 | if c.Getenv("USE_BAZEL_ANALYSIS") != "1" { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 236 | return noopBazelContext{}, nil |
| 237 | } |
| 238 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 239 | p, err := bazelPathsFromConfig(c) |
| 240 | if err != nil { |
| 241 | return nil, err |
| 242 | } |
| 243 | return &bazelContext{ |
| 244 | bazelRunner: &builtinBazelRunner{}, |
| 245 | paths: p, |
| 246 | requests: make(map[cqueryKey]bool), |
| 247 | }, nil |
| 248 | } |
| 249 | |
| 250 | func bazelPathsFromConfig(c *config) (*bazelPaths, error) { |
| 251 | p := bazelPaths{ |
| 252 | buildDir: c.buildDir, |
| 253 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 254 | missingEnvVars := []string{} |
| 255 | if len(c.Getenv("BAZEL_HOME")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 256 | p.homeDir = c.Getenv("BAZEL_HOME") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 257 | } else { |
| 258 | missingEnvVars = append(missingEnvVars, "BAZEL_HOME") |
| 259 | } |
| 260 | if len(c.Getenv("BAZEL_PATH")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 261 | p.bazelPath = c.Getenv("BAZEL_PATH") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 262 | } else { |
| 263 | missingEnvVars = append(missingEnvVars, "BAZEL_PATH") |
| 264 | } |
| 265 | if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 266 | p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 267 | } else { |
| 268 | missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE") |
| 269 | } |
| 270 | if len(c.Getenv("BAZEL_WORKSPACE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 271 | p.workspaceDir = c.Getenv("BAZEL_WORKSPACE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 272 | } else { |
| 273 | missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE") |
| 274 | } |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 275 | if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 276 | p.metricsDir = c.Getenv("BAZEL_METRICS_DIR") |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 277 | } else { |
| 278 | missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR") |
| 279 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 280 | if len(missingEnvVars) > 0 { |
| 281 | return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars)) |
| 282 | } else { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 283 | return &p, nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 287 | func (p *bazelPaths) BazelMetricsDir() string { |
| 288 | return p.metricsDir |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 291 | func (context *bazelContext) BazelEnabled() bool { |
| 292 | return true |
| 293 | } |
| 294 | |
| 295 | // Adds a cquery request to the Bazel request queue, to be later invoked, or |
| 296 | // returns the result of the given request if the request was already made. |
| 297 | // If the given request was already made (and the results are available), then |
| 298 | // returns (result, true). If the request is queued but no results are available, |
| 299 | // then returns ("", false). |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 300 | func (context *bazelContext) cquery(label string, requestType cqueryRequest, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 301 | archType ArchType) (string, bool) { |
| 302 | key := cqueryKey{label, requestType, archType} |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 303 | if result, ok := context.results[key]; ok { |
| 304 | return result, true |
| 305 | } else { |
| 306 | context.requestMutex.Lock() |
| 307 | defer context.requestMutex.Unlock() |
| 308 | context.requests[key] = true |
| 309 | return "", false |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func pwdPrefix() string { |
| 314 | // Darwin doesn't have /proc |
| 315 | if runtime.GOOS != "darwin" { |
| 316 | return "PWD=/proc/self/cwd" |
| 317 | } |
| 318 | return "" |
| 319 | } |
| 320 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 321 | type bazelCommand struct { |
| 322 | command string |
| 323 | // query or label |
| 324 | expression string |
| 325 | } |
| 326 | |
| 327 | type mockBazelRunner struct { |
| 328 | bazelCommandResults map[bazelCommand]string |
| 329 | commands []bazelCommand |
| 330 | } |
| 331 | |
| 332 | func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths, |
| 333 | runName bazel.RunName, |
| 334 | command bazelCommand, |
| 335 | extraFlags ...string) (string, string, error) { |
| 336 | r.commands = append(r.commands, command) |
| 337 | if ret, ok := r.bazelCommandResults[command]; ok { |
| 338 | return ret, "", nil |
| 339 | } |
| 340 | return "", "", nil |
| 341 | } |
| 342 | |
| 343 | type builtinBazelRunner struct{} |
| 344 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 345 | // Issues the given bazel command with given build label and additional flags. |
| 346 | // Returns (stdout, stderr, error). The first and second return values are strings |
| 347 | // containing the stdout and stderr of the run command, and an error is returned if |
| 348 | // the invocation returned an error code. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 349 | func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 350 | extraFlags ...string) (string, string, error) { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 351 | cmdFlags := []string{"--output_base=" + paths.outputBase, command.command} |
| 352 | cmdFlags = append(cmdFlags, command.expression) |
| 353 | cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+paths.intermediatesDir()) |
| 354 | cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName)) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 355 | |
| 356 | // Set default platforms to canonicalized values for mixed builds requests. |
| 357 | // If these are set in the bazelrc, they will have values that are |
| 358 | // non-canonicalized to @sourceroot labels, and thus be invalid when |
| 359 | // referenced from the buildroot. |
| 360 | // |
| 361 | // The actual platform values here may be overridden by configuration |
| 362 | // transitions from the buildroot. |
Chris Parsons | ee423b0 | 2021-02-08 23:04:59 -0500 | [diff] [blame] | 363 | cmdFlags = append(cmdFlags, |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 364 | fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:android_x86_64"))) |
Chris Parsons | ee423b0 | 2021-02-08 23:04:59 -0500 | [diff] [blame] | 365 | cmdFlags = append(cmdFlags, |
| 366 | fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all"))) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 367 | // This should be parameterized on the host OS, but let's restrict to linux |
| 368 | // to keep things simple for now. |
| 369 | cmdFlags = append(cmdFlags, |
| 370 | fmt.Sprintf("--host_platform=%s", canonicalizeLabel("//build/bazel/platforms:linux_x86_64"))) |
| 371 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 372 | // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network. |
| 373 | cmdFlags = append(cmdFlags, "--experimental_repository_disable_download") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 374 | cmdFlags = append(cmdFlags, extraFlags...) |
| 375 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 376 | bazelCmd := exec.Command(paths.bazelPath, cmdFlags...) |
| 377 | bazelCmd.Dir = paths.workspaceDir |
| 378 | bazelCmd.Env = append(os.Environ(), "HOME="+paths.homeDir, pwdPrefix(), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 379 | // Disables local host detection of gcc; toolchain information is defined |
| 380 | // explicitly in BUILD files. |
| 381 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1") |
Colin Cross | ff0278b | 2020-10-09 19:24:15 -0700 | [diff] [blame] | 382 | stderr := &bytes.Buffer{} |
| 383 | bazelCmd.Stderr = stderr |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 384 | |
| 385 | if output, err := bazelCmd.Output(); err != nil { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 386 | return "", string(stderr.Bytes()), |
| 387 | fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 388 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 389 | return string(output), string(stderr.Bytes()), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 393 | // Returns the string contents of a workspace file that should be output |
| 394 | // adjacent to the main bzl file and build file. |
| 395 | // This workspace file allows, via local_repository rule, sourcetree-level |
| 396 | // BUILD targets to be referenced via @sourceroot. |
| 397 | func (context *bazelContext) workspaceFileContents() []byte { |
| 398 | formatString := ` |
| 399 | # This file is generated by soong_build. Do not edit. |
| 400 | local_repository( |
| 401 | name = "sourceroot", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 402 | path = "%[1]s", |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 403 | ) |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame] | 404 | |
| 405 | local_repository( |
| 406 | name = "rules_cc", |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 407 | path = "%[1]s/build/bazel/rules_cc", |
| 408 | ) |
| 409 | |
| 410 | local_repository( |
| 411 | name = "bazel_skylib", |
| 412 | path = "%[1]s/build/bazel/bazel_skylib", |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame] | 413 | ) |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 414 | ` |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 415 | return []byte(fmt.Sprintf(formatString, context.paths.workspaceDir)) |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 418 | func (context *bazelContext) mainBzlFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 419 | // TODO(cparsons): Define configuration transitions programmatically based |
| 420 | // on available archs. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 421 | contents := ` |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 422 | ##################################################### |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 423 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 424 | ##################################################### |
| 425 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 426 | def _config_node_transition_impl(settings, attr): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 427 | return { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 428 | "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:android_%s" % attr.arch, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 431 | _config_node_transition = transition( |
| 432 | implementation = _config_node_transition_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 433 | inputs = [], |
| 434 | outputs = [ |
| 435 | "//command_line_option:platforms", |
| 436 | ], |
| 437 | ) |
| 438 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 439 | def _passthrough_rule_impl(ctx): |
| 440 | return [DefaultInfo(files = depset(ctx.files.deps))] |
| 441 | |
| 442 | config_node = rule( |
| 443 | implementation = _passthrough_rule_impl, |
| 444 | attrs = { |
| 445 | "arch" : attr.string(mandatory = True), |
| 446 | "deps" : attr.label_list(cfg = _config_node_transition), |
| 447 | "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 448 | }, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 449 | ) |
| 450 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 451 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 452 | # Rule representing the root of the build, to depend on all Bazel targets that |
| 453 | # are required for the build. Building this target will build the entire Bazel |
| 454 | # build tree. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 455 | mixed_build_root = rule( |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 456 | implementation = _passthrough_rule_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 457 | attrs = { |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 458 | "deps" : attr.label_list(), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 459 | }, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 460 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 461 | |
| 462 | def _phony_root_impl(ctx): |
| 463 | return [] |
| 464 | |
| 465 | # Rule to depend on other targets but build nothing. |
| 466 | # This is useful as follows: building a target of this rule will generate |
| 467 | # symlink forests for all dependencies of the target, without executing any |
| 468 | # actions of the build. |
| 469 | phony_root = rule( |
| 470 | implementation = _phony_root_impl, |
| 471 | attrs = {"deps" : attr.label_list()}, |
| 472 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 473 | ` |
| 474 | return []byte(contents) |
| 475 | } |
| 476 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 477 | // Returns a "canonicalized" corresponding to the given sourcetree-level label. |
| 478 | // This abstraction is required because a sourcetree label such as //foo/bar:baz |
| 479 | // must be referenced via the local repository prefix, such as |
| 480 | // @sourceroot//foo/bar:baz. |
| 481 | func canonicalizeLabel(label string) string { |
| 482 | if strings.HasPrefix(label, "//") { |
| 483 | return "@sourceroot" + label |
| 484 | } else { |
| 485 | return "@sourceroot//" + label |
| 486 | } |
| 487 | } |
| 488 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 489 | func (context *bazelContext) mainBuildFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 490 | // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded |
| 491 | // architecture mapping. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 492 | formatString := ` |
| 493 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 494 | load(":main.bzl", "config_node", "mixed_build_root", "phony_root") |
| 495 | |
| 496 | %s |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 497 | |
| 498 | mixed_build_root(name = "buildroot", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 499 | deps = [%s], |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 500 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 501 | |
| 502 | phony_root(name = "phonyroot", |
| 503 | deps = [":buildroot"], |
| 504 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 505 | ` |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 506 | configNodeFormatString := ` |
| 507 | config_node(name = "%s", |
| 508 | arch = "%s", |
| 509 | deps = [%s], |
| 510 | ) |
| 511 | ` |
| 512 | |
| 513 | configNodesSection := "" |
| 514 | |
| 515 | labelsByArch := map[string][]string{} |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 516 | for val, _ := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 517 | labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label)) |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 518 | archString := getArchString(val) |
| 519 | labelsByArch[archString] = append(labelsByArch[archString], labelString) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 520 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 521 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 522 | configNodeLabels := []string{} |
| 523 | for archString, labels := range labelsByArch { |
| 524 | configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString)) |
| 525 | labelsString := strings.Join(labels, ",\n ") |
| 526 | configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString) |
| 527 | } |
| 528 | |
| 529 | return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n "))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 530 | } |
| 531 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 532 | func indent(original string) string { |
| 533 | result := "" |
| 534 | for _, line := range strings.Split(original, "\n") { |
| 535 | result += " " + line + "\n" |
| 536 | } |
| 537 | return result |
| 538 | } |
| 539 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 540 | // Returns the file contents of the buildroot.cquery file that should be used for the cquery |
| 541 | // expression in order to obtain information about buildroot and its dependencies. |
| 542 | // The contents of this file depend on the bazelContext's requests; requests are enumerated |
| 543 | // and grouped by their request type. The data retrieved for each label depends on its |
| 544 | // request type. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 545 | func (context *bazelContext) cqueryStarlarkFileContents() []byte { |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 546 | requestTypeToCqueryIdEntries := map[cqueryRequest][]string{} |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 547 | for val, _ := range context.requests { |
| 548 | cqueryId := getCqueryId(val) |
| 549 | mapEntryString := fmt.Sprintf("%q : True", cqueryId) |
| 550 | requestTypeToCqueryIdEntries[val.requestType] = |
| 551 | append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString) |
| 552 | } |
| 553 | labelRegistrationMapSection := "" |
| 554 | functionDefSection := "" |
| 555 | mainSwitchSection := "" |
| 556 | |
| 557 | mapDeclarationFormatString := ` |
| 558 | %s = { |
| 559 | %s |
| 560 | } |
| 561 | ` |
| 562 | functionDefFormatString := ` |
| 563 | def %s(target): |
| 564 | %s |
| 565 | ` |
| 566 | mainSwitchSectionFormatString := ` |
| 567 | if id_string in %s: |
| 568 | return id_string + ">>" + %s(target) |
| 569 | ` |
| 570 | |
Liz Kammer | 66ffdb7 | 2021-04-02 13:26:07 -0400 | [diff] [blame] | 571 | for requestType, _ := range requestTypeToCqueryIdEntries { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 572 | labelMapName := requestType.Name() + "_Labels" |
| 573 | functionName := requestType.Name() + "_Fn" |
| 574 | labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString, |
| 575 | labelMapName, |
| 576 | strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n ")) |
| 577 | functionDefSection += fmt.Sprintf(functionDefFormatString, |
| 578 | functionName, |
| 579 | indent(requestType.StarlarkFunctionBody())) |
| 580 | mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString, |
| 581 | labelMapName, functionName) |
| 582 | } |
| 583 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 584 | formatString := ` |
| 585 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 586 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 587 | # Label Map Section |
| 588 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 589 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 590 | # Function Def Section |
| 591 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 592 | |
| 593 | def get_arch(target): |
| 594 | buildoptions = build_options(target) |
| 595 | platforms = build_options(target)["//command_line_option:platforms"] |
| 596 | if len(platforms) != 1: |
| 597 | # An individual configured target should have only one platform architecture. |
| 598 | # Note that it's fine for there to be multiple architectures for the same label, |
| 599 | # but each is its own configured target. |
| 600 | fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms)) |
| 601 | platform_name = build_options(target)["//command_line_option:platforms"][0].name |
| 602 | if platform_name == "host": |
| 603 | return "HOST" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 604 | elif not platform_name.startswith("android_"): |
| 605 | fail("expected platform name of the form 'android_<arch>', but was " + str(platforms)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 606 | return "UNKNOWN" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 607 | return platform_name[len("android_"):] |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 608 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 609 | def format(target): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 610 | id_string = str(target.label) + "|" + get_arch(target) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 611 | |
| 612 | # Main switch section |
| 613 | %s |
| 614 | # This target was not requested via cquery, and thus must be a dependency |
| 615 | # of a requested target. |
| 616 | return id_string + ">>NONE" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 617 | ` |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 618 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 619 | return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection, |
| 620 | mainSwitchSection)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 621 | } |
| 622 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 623 | // Returns a workspace-relative path containing build-related metadata required |
| 624 | // for interfacing with Bazel. Example: out/soong/bazel. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 625 | func (p *bazelPaths) intermediatesDir() string { |
| 626 | return filepath.Join(p.buildDir, "bazel") |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 627 | } |
| 628 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 629 | // Issues commands to Bazel to receive results for all cquery requests |
| 630 | // queued in the BazelContext. |
| 631 | func (context *bazelContext) InvokeBazel() error { |
| 632 | context.results = make(map[cqueryKey]string) |
| 633 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 634 | var cqueryOutput string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 635 | var cqueryErr string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 636 | var err error |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 637 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 638 | intermediatesDirPath := absolutePath(context.paths.intermediatesDir()) |
Chris Parsons | 07c1e4a | 2021-01-19 17:19:16 -0500 | [diff] [blame] | 639 | if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) { |
| 640 | err = os.Mkdir(intermediatesDirPath, 0777) |
| 641 | } |
| 642 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 643 | if err != nil { |
| 644 | return err |
| 645 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 646 | err = ioutil.WriteFile( |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 647 | filepath.Join(intermediatesDirPath, "main.bzl"), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 648 | context.mainBzlFileContents(), 0666) |
| 649 | if err != nil { |
| 650 | return err |
| 651 | } |
| 652 | err = ioutil.WriteFile( |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 653 | filepath.Join(intermediatesDirPath, "BUILD.bazel"), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 654 | context.mainBuildFileContents(), 0666) |
| 655 | if err != nil { |
| 656 | return err |
| 657 | } |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 658 | cqueryFileRelpath := filepath.Join(context.paths.intermediatesDir(), "buildroot.cquery") |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 659 | err = ioutil.WriteFile( |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 660 | absolutePath(cqueryFileRelpath), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 661 | context.cqueryStarlarkFileContents(), 0666) |
| 662 | if err != nil { |
| 663 | return err |
| 664 | } |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 665 | err = ioutil.WriteFile( |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 666 | filepath.Join(intermediatesDirPath, "WORKSPACE.bazel"), |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 667 | context.workspaceFileContents(), 0666) |
| 668 | if err != nil { |
| 669 | return err |
| 670 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 671 | buildrootLabel := "//:buildroot" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 672 | cqueryOutput, cqueryErr, err = context.issueBazelCommand( |
| 673 | context.paths, |
| 674 | bazel.CqueryBuildRootRunName, |
| 675 | bazelCommand{"cquery", fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)}, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 676 | "--output=starlark", |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 677 | "--starlark:file="+cqueryFileRelpath) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 678 | err = ioutil.WriteFile(filepath.Join(intermediatesDirPath, "cquery.out"), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 679 | []byte(cqueryOutput), 0666) |
| 680 | if err != nil { |
| 681 | return err |
| 682 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 683 | |
| 684 | if err != nil { |
| 685 | return err |
| 686 | } |
| 687 | |
| 688 | cqueryResults := map[string]string{} |
| 689 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 690 | if strings.Contains(outputLine, ">>") { |
| 691 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 692 | cqueryResults[splitLine[0]] = splitLine[1] |
| 693 | } |
| 694 | } |
| 695 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 696 | for val, _ := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 697 | if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 698 | context.results[val] = string(cqueryResult) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 699 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 700 | return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]", |
| 701 | getCqueryId(val), cqueryOutput, cqueryErr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 705 | // Issue an aquery command to retrieve action information about the bazel build tree. |
| 706 | // |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 707 | // TODO(cparsons): Use --target_pattern_file to avoid command line limits. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 708 | var aqueryOutput string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 709 | aqueryOutput, _, err = context.issueBazelCommand( |
| 710 | context.paths, |
| 711 | bazel.AqueryBuildRootRunName, |
| 712 | bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}, |
| 713 | // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's |
| 714 | // proto sources, which would add a number of unnecessary dependencies. |
| 715 | "--output=jsonproto") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 716 | |
| 717 | if err != nil { |
| 718 | return err |
| 719 | } |
| 720 | |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 721 | context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput)) |
| 722 | if err != nil { |
| 723 | return err |
| 724 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 725 | |
| 726 | // Issue a build command of the phony root to generate symlink forests for dependencies of the |
| 727 | // Bazel build. This is necessary because aquery invocations do not generate this symlink forest, |
| 728 | // but some of symlinks may be required to resolve source dependencies of the build. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 729 | _, _, err = context.issueBazelCommand( |
| 730 | context.paths, |
| 731 | bazel.BazelBuildPhonyRootRunName, |
| 732 | bazelCommand{"build", "//:phonyroot"}) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 733 | |
| 734 | if err != nil { |
| 735 | return err |
| 736 | } |
| 737 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 738 | // Clear requests. |
| 739 | context.requests = map[cqueryKey]bool{} |
| 740 | return nil |
| 741 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 742 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 743 | func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 744 | return context.buildStatements |
| 745 | } |
| 746 | |
| 747 | func (context *bazelContext) OutputBase() string { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame^] | 748 | return context.paths.outputBase |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 749 | } |
| 750 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 751 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 752 | // for correctness of builds which use Bazel. |
| 753 | func BazelSingleton() Singleton { |
| 754 | return &bazelSingleton{} |
| 755 | } |
| 756 | |
| 757 | type bazelSingleton struct{} |
| 758 | |
| 759 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 760 | // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled. |
| 761 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 762 | return |
| 763 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 764 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 765 | // Add ninja file dependencies for files which all bazel invocations require. |
| 766 | bazelBuildList := absolutePath(filepath.Join( |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 767 | filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list")) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 768 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 769 | |
| 770 | data, err := ioutil.ReadFile(bazelBuildList) |
| 771 | if err != nil { |
| 772 | ctx.Errorf(err.Error()) |
| 773 | } |
| 774 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 775 | for _, file := range files { |
| 776 | ctx.AddNinjaFileDeps(file) |
| 777 | } |
| 778 | |
| 779 | // Register bazel-owned build statements (obtained from the aquery invocation). |
| 780 | for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 781 | if len(buildStatement.Command) < 1 { |
Rupert Shuttleworth | a29903f | 2021-04-06 16:17:33 +0000 | [diff] [blame] | 782 | panic(fmt.Sprintf("unhandled build statement: %v", buildStatement)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 783 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 784 | rule := NewRuleBuilder(pctx, ctx) |
| 785 | cmd := rule.Command() |
| 786 | cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s", |
| 787 | ctx.Config().BazelContext.OutputBase(), buildStatement.Command)) |
| 788 | |
| 789 | for _, outputPath := range buildStatement.OutputPaths { |
| 790 | cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath)) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 791 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 792 | for _, inputPath := range buildStatement.InputPaths { |
| 793 | cmd.Implicit(PathForBazelOut(ctx, inputPath)) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 794 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 795 | |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 796 | if depfile := buildStatement.Depfile; depfile != nil { |
| 797 | cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile)) |
| 798 | } |
| 799 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 800 | // This is required to silence warnings pertaining to unexpected timestamps. Particularly, |
| 801 | // some Bazel builtins (such as files in the bazel_tools directory) have far-future |
| 802 | // timestamps. Without restat, Ninja would emit warnings that the input files of a |
| 803 | // build statement have later timestamps than the outputs. |
| 804 | rule.Restat() |
| 805 | |
Liz Kammer | 13548d7 | 2020-12-16 11:13:30 -0800 | [diff] [blame] | 806 | rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 807 | } |
| 808 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 809 | |
| 810 | func getCqueryId(key cqueryKey) string { |
| 811 | return canonicalizeLabel(key.label) + "|" + getArchString(key) |
| 812 | } |
| 813 | |
| 814 | func getArchString(key cqueryKey) string { |
| 815 | arch := key.archType.Name |
| 816 | if len(arch) > 0 { |
| 817 | return arch |
| 818 | } else { |
| 819 | return "x86_64" |
| 820 | } |
| 821 | } |