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