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