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 | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 347 | def _config_node_transition_impl(settings, attr): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 348 | return { |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 349 | "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_%s" % attr.arch, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 350 | } |
| 351 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 352 | _config_node_transition = transition( |
| 353 | implementation = _config_node_transition_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 354 | inputs = [], |
| 355 | outputs = [ |
| 356 | "//command_line_option:platforms", |
| 357 | ], |
| 358 | ) |
| 359 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 360 | def _passthrough_rule_impl(ctx): |
| 361 | return [DefaultInfo(files = depset(ctx.files.deps))] |
| 362 | |
| 363 | config_node = rule( |
| 364 | implementation = _passthrough_rule_impl, |
| 365 | attrs = { |
| 366 | "arch" : attr.string(mandatory = True), |
| 367 | "deps" : attr.label_list(cfg = _config_node_transition), |
| 368 | "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 369 | }, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 370 | ) |
| 371 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 372 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 373 | # Rule representing the root of the build, to depend on all Bazel targets that |
| 374 | # are required for the build. Building this target will build the entire Bazel |
| 375 | # build tree. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 376 | mixed_build_root = rule( |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 377 | implementation = _passthrough_rule_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 378 | attrs = { |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 379 | "deps" : attr.label_list(), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 380 | }, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 381 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 382 | |
| 383 | def _phony_root_impl(ctx): |
| 384 | return [] |
| 385 | |
| 386 | # Rule to depend on other targets but build nothing. |
| 387 | # This is useful as follows: building a target of this rule will generate |
| 388 | # symlink forests for all dependencies of the target, without executing any |
| 389 | # actions of the build. |
| 390 | phony_root = rule( |
| 391 | implementation = _phony_root_impl, |
| 392 | attrs = {"deps" : attr.label_list()}, |
| 393 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 394 | ` |
| 395 | return []byte(contents) |
| 396 | } |
| 397 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 398 | // Returns a "canonicalized" corresponding to the given sourcetree-level label. |
| 399 | // This abstraction is required because a sourcetree label such as //foo/bar:baz |
| 400 | // must be referenced via the local repository prefix, such as |
| 401 | // @sourceroot//foo/bar:baz. |
| 402 | func canonicalizeLabel(label string) string { |
| 403 | if strings.HasPrefix(label, "//") { |
| 404 | return "@sourceroot" + label |
| 405 | } else { |
| 406 | return "@sourceroot//" + label |
| 407 | } |
| 408 | } |
| 409 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 410 | func (context *bazelContext) mainBuildFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 411 | // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded |
| 412 | // architecture mapping. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 413 | formatString := ` |
| 414 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 415 | load(":main.bzl", "config_node", "mixed_build_root", "phony_root") |
| 416 | |
| 417 | %s |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 418 | |
| 419 | mixed_build_root(name = "buildroot", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 420 | deps = [%s], |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 421 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 422 | |
| 423 | phony_root(name = "phonyroot", |
| 424 | deps = [":buildroot"], |
| 425 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 426 | ` |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 427 | configNodeFormatString := ` |
| 428 | config_node(name = "%s", |
| 429 | arch = "%s", |
| 430 | deps = [%s], |
| 431 | ) |
| 432 | ` |
| 433 | |
| 434 | configNodesSection := "" |
| 435 | |
| 436 | labelsByArch := map[string][]string{} |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 437 | for val, _ := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 438 | labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label)) |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 439 | archString := getArchString(val) |
| 440 | labelsByArch[archString] = append(labelsByArch[archString], labelString) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 441 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 442 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame^] | 443 | configNodeLabels := []string{} |
| 444 | for archString, labels := range labelsByArch { |
| 445 | configNodeLabels = append(configNodeLabels, fmt.Sprintf("\":%s\"", archString)) |
| 446 | labelsString := strings.Join(labels, ",\n ") |
| 447 | configNodesSection += fmt.Sprintf(configNodeFormatString, archString, archString, labelsString) |
| 448 | } |
| 449 | |
| 450 | return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(configNodeLabels, ",\n "))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 451 | } |
| 452 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 453 | func indent(original string) string { |
| 454 | result := "" |
| 455 | for _, line := range strings.Split(original, "\n") { |
| 456 | result += " " + line + "\n" |
| 457 | } |
| 458 | return result |
| 459 | } |
| 460 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 461 | // Returns the file contents of the buildroot.cquery file that should be used for the cquery |
| 462 | // expression in order to obtain information about buildroot and its dependencies. |
| 463 | // The contents of this file depend on the bazelContext's requests; requests are enumerated |
| 464 | // and grouped by their request type. The data retrieved for each label depends on its |
| 465 | // request type. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 466 | func (context *bazelContext) cqueryStarlarkFileContents() []byte { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 467 | requestTypeToCqueryIdEntries := map[cquery.RequestType][]string{} |
| 468 | for val, _ := range context.requests { |
| 469 | cqueryId := getCqueryId(val) |
| 470 | mapEntryString := fmt.Sprintf("%q : True", cqueryId) |
| 471 | requestTypeToCqueryIdEntries[val.requestType] = |
| 472 | append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString) |
| 473 | } |
| 474 | labelRegistrationMapSection := "" |
| 475 | functionDefSection := "" |
| 476 | mainSwitchSection := "" |
| 477 | |
| 478 | mapDeclarationFormatString := ` |
| 479 | %s = { |
| 480 | %s |
| 481 | } |
| 482 | ` |
| 483 | functionDefFormatString := ` |
| 484 | def %s(target): |
| 485 | %s |
| 486 | ` |
| 487 | mainSwitchSectionFormatString := ` |
| 488 | if id_string in %s: |
| 489 | return id_string + ">>" + %s(target) |
| 490 | ` |
| 491 | |
| 492 | for _, requestType := range cquery.RequestTypes { |
| 493 | labelMapName := requestType.Name() + "_Labels" |
| 494 | functionName := requestType.Name() + "_Fn" |
| 495 | labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString, |
| 496 | labelMapName, |
| 497 | strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n ")) |
| 498 | functionDefSection += fmt.Sprintf(functionDefFormatString, |
| 499 | functionName, |
| 500 | indent(requestType.StarlarkFunctionBody())) |
| 501 | mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString, |
| 502 | labelMapName, functionName) |
| 503 | } |
| 504 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 505 | formatString := ` |
| 506 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 507 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 508 | # Label Map Section |
| 509 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 510 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 511 | # Function Def Section |
| 512 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 513 | |
| 514 | def get_arch(target): |
| 515 | buildoptions = build_options(target) |
| 516 | platforms = build_options(target)["//command_line_option:platforms"] |
| 517 | if len(platforms) != 1: |
| 518 | # An individual configured target should have only one platform architecture. |
| 519 | # Note that it's fine for there to be multiple architectures for the same label, |
| 520 | # but each is its own configured target. |
| 521 | fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms)) |
| 522 | platform_name = build_options(target)["//command_line_option:platforms"][0].name |
| 523 | if platform_name == "host": |
| 524 | return "HOST" |
| 525 | elif not platform_name.startswith("generic_"): |
| 526 | fail("expected platform name of the form 'generic_<arch>', but was " + str(platforms)) |
| 527 | return "UNKNOWN" |
| 528 | return platform_name[len("generic_"):] |
| 529 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 530 | def format(target): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 531 | id_string = str(target.label) + "|" + get_arch(target) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 532 | |
| 533 | # Main switch section |
| 534 | %s |
| 535 | # This target was not requested via cquery, and thus must be a dependency |
| 536 | # of a requested target. |
| 537 | return id_string + ">>NONE" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 538 | ` |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 539 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 540 | return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection, |
| 541 | mainSwitchSection)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 542 | } |
| 543 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 544 | // Returns a workspace-relative path containing build-related metadata required |
| 545 | // for interfacing with Bazel. Example: out/soong/bazel. |
| 546 | func (context *bazelContext) intermediatesDir() string { |
| 547 | return filepath.Join(context.buildDir, "bazel") |
| 548 | } |
| 549 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 550 | // Issues commands to Bazel to receive results for all cquery requests |
| 551 | // queued in the BazelContext. |
| 552 | func (context *bazelContext) InvokeBazel() error { |
| 553 | context.results = make(map[cqueryKey]string) |
| 554 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 555 | var cqueryOutput string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 556 | var cqueryErr string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 557 | var err error |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 558 | |
Chris Parsons | 07c1e4a | 2021-01-19 17:19:16 -0500 | [diff] [blame] | 559 | intermediatesDirPath := absolutePath(context.intermediatesDir()) |
| 560 | if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) { |
| 561 | err = os.Mkdir(intermediatesDirPath, 0777) |
| 562 | } |
| 563 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 564 | if err != nil { |
| 565 | return err |
| 566 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 567 | err = ioutil.WriteFile( |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 568 | absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 569 | context.mainBzlFileContents(), 0666) |
| 570 | if err != nil { |
| 571 | return err |
| 572 | } |
| 573 | err = ioutil.WriteFile( |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 574 | absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 575 | context.mainBuildFileContents(), 0666) |
| 576 | if err != nil { |
| 577 | return err |
| 578 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 579 | cqueryFileRelpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery") |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 580 | err = ioutil.WriteFile( |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 581 | absolutePath(cqueryFileRelpath), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 582 | context.cqueryStarlarkFileContents(), 0666) |
| 583 | if err != nil { |
| 584 | return err |
| 585 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 586 | workspaceFileRelpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel") |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 587 | err = ioutil.WriteFile( |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 588 | absolutePath(workspaceFileRelpath), |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 589 | context.workspaceFileContents(), 0666) |
| 590 | if err != nil { |
| 591 | return err |
| 592 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 593 | buildrootLabel := "//:buildroot" |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 594 | cqueryOutput, cqueryErr, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery", |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 595 | []string{fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)}, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 596 | "--output=starlark", |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 597 | "--starlark:file="+cqueryFileRelpath) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 598 | err = ioutil.WriteFile( |
| 599 | absolutePath(filepath.Join(context.intermediatesDir(), "cquery.out")), |
| 600 | []byte(cqueryOutput), 0666) |
| 601 | if err != nil { |
| 602 | return err |
| 603 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 604 | |
| 605 | if err != nil { |
| 606 | return err |
| 607 | } |
| 608 | |
| 609 | cqueryResults := map[string]string{} |
| 610 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 611 | if strings.Contains(outputLine, ">>") { |
| 612 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 613 | cqueryResults[splitLine[0]] = splitLine[1] |
| 614 | } |
| 615 | } |
| 616 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 617 | for val, _ := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 618 | if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 619 | context.results[val] = string(cqueryResult) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 620 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 621 | return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]", |
| 622 | getCqueryId(val), cqueryOutput, cqueryErr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 626 | // Issue an aquery command to retrieve action information about the bazel build tree. |
| 627 | // |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 628 | // TODO(cparsons): Use --target_pattern_file to avoid command line limits. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 629 | var aqueryOutput string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 630 | aqueryOutput, _, err = context.issueBazelCommand(bazel.AqueryBuildRootRunName, "aquery", |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 631 | []string{fmt.Sprintf("deps(%s)", buildrootLabel), |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 632 | // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's |
| 633 | // proto sources, which would add a number of unnecessary dependencies. |
| 634 | "--output=jsonproto"}) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 635 | |
| 636 | if err != nil { |
| 637 | return err |
| 638 | } |
| 639 | |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 640 | context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput)) |
| 641 | if err != nil { |
| 642 | return err |
| 643 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 644 | |
| 645 | // Issue a build command of the phony root to generate symlink forests for dependencies of the |
| 646 | // Bazel build. This is necessary because aquery invocations do not generate this symlink forest, |
| 647 | // 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] | 648 | _, _, err = context.issueBazelCommand(bazel.BazelBuildPhonyRootRunName, "build", |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 649 | []string{"//:phonyroot"}) |
| 650 | |
| 651 | if err != nil { |
| 652 | return err |
| 653 | } |
| 654 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 655 | // Clear requests. |
| 656 | context.requests = map[cqueryKey]bool{} |
| 657 | return nil |
| 658 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 659 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 660 | func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 661 | return context.buildStatements |
| 662 | } |
| 663 | |
| 664 | func (context *bazelContext) OutputBase() string { |
| 665 | return context.outputBase |
| 666 | } |
| 667 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 668 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 669 | // for correctness of builds which use Bazel. |
| 670 | func BazelSingleton() Singleton { |
| 671 | return &bazelSingleton{} |
| 672 | } |
| 673 | |
| 674 | type bazelSingleton struct{} |
| 675 | |
| 676 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 677 | // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled. |
| 678 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 679 | return |
| 680 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 681 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 682 | // Add ninja file dependencies for files which all bazel invocations require. |
| 683 | bazelBuildList := absolutePath(filepath.Join( |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 684 | filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list")) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 685 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 686 | |
| 687 | data, err := ioutil.ReadFile(bazelBuildList) |
| 688 | if err != nil { |
| 689 | ctx.Errorf(err.Error()) |
| 690 | } |
| 691 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 692 | for _, file := range files { |
| 693 | ctx.AddNinjaFileDeps(file) |
| 694 | } |
| 695 | |
| 696 | // Register bazel-owned build statements (obtained from the aquery invocation). |
| 697 | for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 698 | if len(buildStatement.Command) < 1 { |
| 699 | panic(fmt.Sprintf("unhandled build statement: %s", buildStatement)) |
| 700 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 701 | rule := NewRuleBuilder(pctx, ctx) |
| 702 | cmd := rule.Command() |
| 703 | cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s", |
| 704 | ctx.Config().BazelContext.OutputBase(), buildStatement.Command)) |
| 705 | |
| 706 | for _, outputPath := range buildStatement.OutputPaths { |
| 707 | cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath)) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 708 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 709 | for _, inputPath := range buildStatement.InputPaths { |
| 710 | cmd.Implicit(PathForBazelOut(ctx, inputPath)) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 711 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 712 | |
| 713 | // This is required to silence warnings pertaining to unexpected timestamps. Particularly, |
| 714 | // some Bazel builtins (such as files in the bazel_tools directory) have far-future |
| 715 | // timestamps. Without restat, Ninja would emit warnings that the input files of a |
| 716 | // build statement have later timestamps than the outputs. |
| 717 | rule.Restat() |
| 718 | |
Liz Kammer | 13548d7 | 2020-12-16 11:13:30 -0800 | [diff] [blame] | 719 | rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 720 | } |
| 721 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 722 | |
| 723 | func getCqueryId(key cqueryKey) string { |
| 724 | return canonicalizeLabel(key.label) + "|" + getArchString(key) |
| 725 | } |
| 726 | |
| 727 | func getArchString(key cqueryKey) string { |
| 728 | arch := key.archType.Name |
| 729 | if len(arch) > 0 { |
| 730 | return arch |
| 731 | } else { |
| 732 | return "x86_64" |
| 733 | } |
| 734 | } |