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