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