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" |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 24 | "path" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 25 | "path/filepath" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 26 | "runtime" |
| 27 | "strings" |
| 28 | "sync" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 29 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 30 | "android/soong/bazel/cquery" |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 31 | "android/soong/shared" |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 32 | "github.com/google/blueprint" |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame] | 33 | |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 34 | "android/soong/bazel" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 35 | ) |
| 36 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame^] | 37 | var ( |
| 38 | writeBazelFile = pctx.AndroidStaticRule("bazelWriteFileRule", blueprint.RuleParams{ |
| 39 | Command: `sed "s/\\\\n/\n/g" ${out}.rsp >${out}`, |
| 40 | Rspfile: "${out}.rsp", |
| 41 | RspfileContent: "${content}", |
| 42 | }, "content") |
| 43 | ) |
| 44 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 45 | func init() { |
| 46 | RegisterMixedBuildsMutator(InitRegistrationContext) |
| 47 | } |
| 48 | |
| 49 | func RegisterMixedBuildsMutator(ctx RegistrationContext) { |
| 50 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 51 | ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel() |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) { |
| 56 | if m := ctx.Module(); m.Enabled() { |
| 57 | if mixedBuildMod, ok := m.(MixedBuildBuildable); ok { |
| 58 | if mixedBuildMod.IsMixedBuildSupported(ctx) && MixedBuildsEnabled(ctx) { |
| 59 | mixedBuildMod.QueueBazelCall(ctx) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 65 | type cqueryRequest interface { |
| 66 | // Name returns a string name for this request type. Such request type names must be unique, |
| 67 | // and must only consist of alphanumeric characters. |
| 68 | Name() string |
| 69 | |
| 70 | // StarlarkFunctionBody returns a starlark function body to process this request type. |
| 71 | // The returned string is the body of a Starlark function which obtains |
| 72 | // all request-relevant information about a target and returns a string containing |
| 73 | // this information. |
| 74 | // The function should have the following properties: |
| 75 | // - `target` is the only parameter to this function (a configured target). |
| 76 | // - The return value must be a string. |
| 77 | // - The function body should not be indented outside of its own scope. |
| 78 | StarlarkFunctionBody() string |
| 79 | } |
| 80 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 81 | // Portion of cquery map key to describe target configuration. |
| 82 | type configKey struct { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 83 | arch string |
| 84 | osType OsType |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 87 | // Map key to describe bazel cquery requests. |
| 88 | type cqueryKey struct { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 89 | label string |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 90 | requestType cqueryRequest |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 91 | configKey configKey |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 94 | // BazelContext is a context object useful for interacting with Bazel during |
| 95 | // the course of a build. Use of Bazel to evaluate part of the build graph |
| 96 | // is referred to as a "mixed build". (Some modules are managed by Soong, |
| 97 | // some are managed by Bazel). To facilitate interop between these build |
| 98 | // subgraphs, Soong may make requests to Bazel and evaluate their responses |
| 99 | // so that Soong modules may accurately depend on Bazel targets. |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 100 | type BazelContext interface { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 101 | // Add a cquery request to the bazel request queue. All queued requests |
| 102 | // will be sent to Bazel on a subsequent invocation of InvokeBazel. |
| 103 | QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) |
| 104 | |
| 105 | // ** Cquery Results Retrieval Functions |
| 106 | // The below functions pertain to retrieving cquery results from a prior |
| 107 | // InvokeBazel function call and parsing the results. |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 108 | |
| 109 | // Returns result files built by building the given bazel target label. |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 110 | GetOutputFiles(label string, cfgKey configKey) ([]string, error) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 111 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 112 | // Returns the results of GetOutputFiles and GetCcObjectFiles in a single query (in that order). |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 113 | GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 114 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 115 | // Returns the executable binary resultant from building together the python sources |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 116 | // TODO(b/232976601): Remove. |
| 117 | GetPythonBinary(label string, cfgKey configKey) (string, error) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 118 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 119 | // ** end Cquery Results Retrieval Functions |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 120 | |
| 121 | // Issues commands to Bazel to receive results for all cquery requests |
| 122 | // queued in the BazelContext. |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 123 | InvokeBazel(config Config) error |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 124 | |
| 125 | // Returns true if bazel is enabled for the given configuration. |
| 126 | BazelEnabled() bool |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 127 | |
| 128 | // Returns the bazel output base (the root directory for all bazel intermediate outputs). |
| 129 | OutputBase() string |
| 130 | |
| 131 | // Returns build statements which should get registered to reflect Bazel's outputs. |
| 132 | BuildStatementsToRegister() []bazel.BuildStatement |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 133 | |
| 134 | // Returns the depsets defined in Bazel's aquery response. |
| 135 | AqueryDepsets() []bazel.AqueryDepset |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 138 | type bazelRunner interface { |
| 139 | issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error) |
| 140 | } |
| 141 | |
| 142 | type bazelPaths struct { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 143 | homeDir string |
| 144 | bazelPath string |
| 145 | outputBase string |
| 146 | workspaceDir string |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 147 | soongOutDir string |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 148 | metricsDir string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 149 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 150 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 151 | // A context object which tracks queued requests that need to be made to Bazel, |
| 152 | // and their results after the requests have been made. |
| 153 | type bazelContext struct { |
| 154 | bazelRunner |
| 155 | paths *bazelPaths |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 156 | requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel |
| 157 | requestMutex sync.Mutex // requests can be written in parallel |
| 158 | |
| 159 | results map[cqueryKey]string // Results of cquery requests after Bazel invocations |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 160 | |
| 161 | // Build statements which should get registered to reflect Bazel's outputs. |
| 162 | buildStatements []bazel.BuildStatement |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 163 | |
| 164 | // Depsets which should be used for Bazel's build statements. |
| 165 | depsets []bazel.AqueryDepset |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | var _ BazelContext = &bazelContext{} |
| 169 | |
| 170 | // A bazel context to use when Bazel is disabled. |
| 171 | type noopBazelContext struct{} |
| 172 | |
| 173 | var _ BazelContext = noopBazelContext{} |
| 174 | |
| 175 | // A bazel context to use for tests. |
| 176 | type MockBazelContext struct { |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 177 | OutputBaseDir string |
| 178 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 179 | LabelToOutputFiles map[string][]string |
| 180 | LabelToCcInfo map[string]cquery.CcInfo |
| 181 | LabelToPythonBinary map[string]string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 184 | func (m MockBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
| 185 | panic("unimplemented") |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 188 | func (m MockBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { |
| 189 | result, _ := m.LabelToOutputFiles[label] |
| 190 | return result, nil |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 193 | func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
| 194 | result, _ := m.LabelToCcInfo[label] |
| 195 | return result, nil |
| 196 | } |
| 197 | |
| 198 | func (m MockBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { |
| 199 | result, _ := m.LabelToPythonBinary[label] |
| 200 | return result, nil |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 203 | func (m MockBazelContext) InvokeBazel(config Config) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 204 | panic("unimplemented") |
| 205 | } |
| 206 | |
| 207 | func (m MockBazelContext) BazelEnabled() bool { |
| 208 | return true |
| 209 | } |
| 210 | |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 211 | func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 212 | |
| 213 | func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 214 | return []bazel.BuildStatement{} |
| 215 | } |
| 216 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 217 | func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 218 | return []bazel.AqueryDepset{} |
| 219 | } |
| 220 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 221 | var _ BazelContext = MockBazelContext{} |
| 222 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 223 | func (bazelCtx *bazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
| 224 | key := cqueryKey{label, requestType, cfgKey} |
| 225 | bazelCtx.requestMutex.Lock() |
| 226 | defer bazelCtx.requestMutex.Unlock() |
| 227 | bazelCtx.requests[key] = true |
| 228 | } |
| 229 | |
| 230 | func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { |
| 231 | key := cqueryKey{label, cquery.GetOutputFiles, cfgKey} |
| 232 | if rawString, ok := bazelCtx.results[key]; ok { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 233 | bazelOutput := strings.TrimSpace(rawString) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 234 | return cquery.GetOutputFiles.ParseResult(bazelOutput), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 235 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 236 | return nil, fmt.Errorf("no bazel response found for %v", key) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 239 | func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
| 240 | key := cqueryKey{label, cquery.GetCcInfo, cfgKey} |
| 241 | if rawString, ok := bazelCtx.results[key]; ok { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 242 | bazelOutput := strings.TrimSpace(rawString) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 243 | return cquery.GetCcInfo.ParseResult(bazelOutput) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 244 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 245 | return cquery.CcInfo{}, fmt.Errorf("no bazel response found for %v", key) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 248 | func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { |
| 249 | key := cqueryKey{label, cquery.GetPythonBinary, cfgKey} |
| 250 | if rawString, ok := bazelCtx.results[key]; ok { |
| 251 | bazelOutput := strings.TrimSpace(rawString) |
| 252 | return cquery.GetPythonBinary.ParseResult(bazelOutput), nil |
| 253 | } |
| 254 | return "", fmt.Errorf("no bazel response found for %v", key) |
| 255 | } |
| 256 | |
| 257 | func (n noopBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 258 | panic("unimplemented") |
| 259 | } |
| 260 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 261 | func (n noopBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 262 | panic("unimplemented") |
| 263 | } |
| 264 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 265 | func (n noopBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
| 266 | panic("unimplemented") |
| 267 | } |
| 268 | |
| 269 | func (n noopBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 270 | panic("unimplemented") |
| 271 | } |
| 272 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 273 | func (n noopBazelContext) InvokeBazel(config Config) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 274 | panic("unimplemented") |
| 275 | } |
| 276 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 277 | func (m noopBazelContext) OutputBase() string { |
| 278 | return "" |
| 279 | } |
| 280 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 281 | func (n noopBazelContext) BazelEnabled() bool { |
| 282 | return false |
| 283 | } |
| 284 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 285 | func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 286 | return []bazel.BuildStatement{} |
| 287 | } |
| 288 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 289 | func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 290 | return []bazel.AqueryDepset{} |
| 291 | } |
| 292 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 293 | func NewBazelContext(c *config) (BazelContext, error) { |
Chris Parsons | 8b77a00 | 2020-10-27 18:59:25 -0400 | [diff] [blame] | 294 | // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds" |
| 295 | // are production ready. |
Jingwen Chen | 442b1a4 | 2021-06-17 07:02:15 +0000 | [diff] [blame] | 296 | if !c.IsEnvTrue("USE_BAZEL_ANALYSIS") { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 297 | return noopBazelContext{}, nil |
| 298 | } |
| 299 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 300 | p, err := bazelPathsFromConfig(c) |
| 301 | if err != nil { |
| 302 | return nil, err |
| 303 | } |
| 304 | return &bazelContext{ |
| 305 | bazelRunner: &builtinBazelRunner{}, |
| 306 | paths: p, |
| 307 | requests: make(map[cqueryKey]bool), |
| 308 | }, nil |
| 309 | } |
| 310 | |
| 311 | func bazelPathsFromConfig(c *config) (*bazelPaths, error) { |
| 312 | p := bazelPaths{ |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 313 | soongOutDir: c.soongOutDir, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 314 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 315 | missingEnvVars := []string{} |
| 316 | if len(c.Getenv("BAZEL_HOME")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 317 | p.homeDir = c.Getenv("BAZEL_HOME") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 318 | } else { |
| 319 | missingEnvVars = append(missingEnvVars, "BAZEL_HOME") |
| 320 | } |
| 321 | if len(c.Getenv("BAZEL_PATH")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 322 | p.bazelPath = c.Getenv("BAZEL_PATH") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 323 | } else { |
| 324 | missingEnvVars = append(missingEnvVars, "BAZEL_PATH") |
| 325 | } |
| 326 | if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 327 | p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 328 | } else { |
| 329 | missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE") |
| 330 | } |
| 331 | if len(c.Getenv("BAZEL_WORKSPACE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 332 | p.workspaceDir = c.Getenv("BAZEL_WORKSPACE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 333 | } else { |
| 334 | missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE") |
| 335 | } |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 336 | if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 337 | p.metricsDir = c.Getenv("BAZEL_METRICS_DIR") |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 338 | } else { |
| 339 | missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR") |
| 340 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 341 | if len(missingEnvVars) > 0 { |
| 342 | return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars)) |
| 343 | } else { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 344 | return &p, nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 348 | func (p *bazelPaths) BazelMetricsDir() string { |
| 349 | return p.metricsDir |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 352 | func (context *bazelContext) BazelEnabled() bool { |
| 353 | return true |
| 354 | } |
| 355 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 356 | func pwdPrefix() string { |
| 357 | // Darwin doesn't have /proc |
| 358 | if runtime.GOOS != "darwin" { |
| 359 | return "PWD=/proc/self/cwd" |
| 360 | } |
| 361 | return "" |
| 362 | } |
| 363 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 364 | type bazelCommand struct { |
| 365 | command string |
| 366 | // query or label |
| 367 | expression string |
| 368 | } |
| 369 | |
| 370 | type mockBazelRunner struct { |
| 371 | bazelCommandResults map[bazelCommand]string |
| 372 | commands []bazelCommand |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 373 | extraFlags []string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths, |
| 377 | runName bazel.RunName, |
| 378 | command bazelCommand, |
| 379 | extraFlags ...string) (string, string, error) { |
| 380 | r.commands = append(r.commands, command) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 381 | r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " ")) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 382 | if ret, ok := r.bazelCommandResults[command]; ok { |
| 383 | return ret, "", nil |
| 384 | } |
| 385 | return "", "", nil |
| 386 | } |
| 387 | |
| 388 | type builtinBazelRunner struct{} |
| 389 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 390 | // Issues the given bazel command with given build label and additional flags. |
| 391 | // Returns (stdout, stderr, error). The first and second return values are strings |
| 392 | // containing the stdout and stderr of the run command, and an error is returned if |
| 393 | // the invocation returned an error code. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 394 | func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 395 | extraFlags ...string) (string, string, error) { |
Romain Jobredeaux | 41fd5e4 | 2021-08-27 15:59:39 +0000 | [diff] [blame] | 396 | cmdFlags := []string{ |
| 397 | // --noautodetect_server_javabase has the practical consequence of preventing Bazel from |
| 398 | // attempting to download rules_java, which is incompatible with |
| 399 | // --experimental_repository_disable_download set further below. |
| 400 | // rules_java is also not needed until mixed builds start building java targets. |
| 401 | // TODO(b/197958133): Once rules_java is pulled into AOSP, remove this flag. |
| 402 | "--noautodetect_server_javabase", |
| 403 | "--output_base=" + absolutePath(paths.outputBase), |
| 404 | command.command, |
| 405 | } |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 406 | cmdFlags = append(cmdFlags, command.expression) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 407 | cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName)) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 408 | |
| 409 | // Set default platforms to canonicalized values for mixed builds requests. |
| 410 | // If these are set in the bazelrc, they will have values that are |
| 411 | // non-canonicalized to @sourceroot labels, and thus be invalid when |
| 412 | // referenced from the buildroot. |
| 413 | // |
| 414 | // The actual platform values here may be overridden by configuration |
| 415 | // transitions from the buildroot. |
Chris Parsons | ee423b0 | 2021-02-08 23:04:59 -0500 | [diff] [blame] | 416 | cmdFlags = append(cmdFlags, |
Liz Kammer | c0c6609 | 2021-07-26 17:38:47 -0400 | [diff] [blame] | 417 | fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target")) |
Chris Parsons | ee423b0 | 2021-02-08 23:04:59 -0500 | [diff] [blame] | 418 | cmdFlags = append(cmdFlags, |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 419 | fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all")) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 420 | // This should be parameterized on the host OS, but let's restrict to linux |
| 421 | // to keep things simple for now. |
| 422 | cmdFlags = append(cmdFlags, |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 423 | fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64")) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 424 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 425 | // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network. |
| 426 | cmdFlags = append(cmdFlags, "--experimental_repository_disable_download") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 427 | cmdFlags = append(cmdFlags, extraFlags...) |
| 428 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 429 | bazelCmd := exec.Command(paths.bazelPath, cmdFlags...) |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 430 | bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir()) |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 431 | bazelCmd.Env = append(os.Environ(), |
| 432 | "HOME="+paths.homeDir, |
| 433 | pwdPrefix(), |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 434 | "BUILD_DIR="+absolutePath(paths.soongOutDir), |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 435 | // Make OUT_DIR absolute here so tools/bazel.sh uses the correct |
| 436 | // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out. |
| 437 | "OUT_DIR="+absolutePath(paths.outDir()), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 438 | // Disables local host detection of gcc; toolchain information is defined |
| 439 | // explicitly in BUILD files. |
| 440 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1") |
Colin Cross | ff0278b | 2020-10-09 19:24:15 -0700 | [diff] [blame] | 441 | stderr := &bytes.Buffer{} |
| 442 | bazelCmd.Stderr = stderr |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 443 | |
| 444 | if output, err := bazelCmd.Output(); err != nil { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 445 | return "", string(stderr.Bytes()), |
| 446 | 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] | 447 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 448 | return string(output), string(stderr.Bytes()), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 452 | func (context *bazelContext) mainBzlFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 453 | // TODO(cparsons): Define configuration transitions programmatically based |
| 454 | // on available archs. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 455 | contents := ` |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 456 | ##################################################### |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 457 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 458 | ##################################################### |
| 459 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 460 | def _config_node_transition_impl(settings, attr): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 461 | return { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 462 | "//command_line_option:platforms": "@//build/bazel/platforms:%s_%s" % (attr.os, attr.arch), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 463 | } |
| 464 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 465 | _config_node_transition = transition( |
| 466 | implementation = _config_node_transition_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 467 | inputs = [], |
| 468 | outputs = [ |
| 469 | "//command_line_option:platforms", |
| 470 | ], |
| 471 | ) |
| 472 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 473 | def _passthrough_rule_impl(ctx): |
| 474 | return [DefaultInfo(files = depset(ctx.files.deps))] |
| 475 | |
| 476 | config_node = rule( |
| 477 | implementation = _passthrough_rule_impl, |
| 478 | attrs = { |
| 479 | "arch" : attr.string(mandatory = True), |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 480 | "os" : attr.string(mandatory = True), |
| 481 | "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True), |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 482 | "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 483 | }, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 484 | ) |
| 485 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 486 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 487 | # Rule representing the root of the build, to depend on all Bazel targets that |
| 488 | # are required for the build. Building this target will build the entire Bazel |
| 489 | # build tree. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 490 | mixed_build_root = rule( |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 491 | implementation = _passthrough_rule_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 492 | attrs = { |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 493 | "deps" : attr.label_list(), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 494 | }, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 495 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 496 | |
| 497 | def _phony_root_impl(ctx): |
| 498 | return [] |
| 499 | |
| 500 | # Rule to depend on other targets but build nothing. |
| 501 | # This is useful as follows: building a target of this rule will generate |
| 502 | # symlink forests for all dependencies of the target, without executing any |
| 503 | # actions of the build. |
| 504 | phony_root = rule( |
| 505 | implementation = _phony_root_impl, |
| 506 | attrs = {"deps" : attr.label_list()}, |
| 507 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 508 | ` |
| 509 | return []byte(contents) |
| 510 | } |
| 511 | |
| 512 | func (context *bazelContext) mainBuildFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 513 | // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded |
| 514 | // architecture mapping. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 515 | formatString := ` |
| 516 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 517 | load(":main.bzl", "config_node", "mixed_build_root", "phony_root") |
| 518 | |
| 519 | %s |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 520 | |
| 521 | mixed_build_root(name = "buildroot", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 522 | deps = [%s], |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 523 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 524 | |
| 525 | phony_root(name = "phonyroot", |
| 526 | deps = [":buildroot"], |
| 527 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 528 | ` |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 529 | configNodeFormatString := ` |
| 530 | config_node(name = "%s", |
| 531 | arch = "%s", |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 532 | os = "%s", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 533 | deps = [%s], |
| 534 | ) |
| 535 | ` |
| 536 | |
| 537 | configNodesSection := "" |
| 538 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 539 | labelsByConfig := map[string][]string{} |
Usta Shrestha | 2bc1cd9 | 2022-06-23 13:45:24 -0400 | [diff] [blame] | 540 | for val := range context.requests { |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 541 | labelString := fmt.Sprintf("\"@%s\"", val.label) |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 542 | configString := getConfigString(val) |
| 543 | labelsByConfig[configString] = append(labelsByConfig[configString], labelString) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 544 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 545 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 546 | allLabels := []string{} |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 547 | for configString, labels := range labelsByConfig { |
| 548 | configTokens := strings.Split(configString, "|") |
| 549 | if len(configTokens) != 2 { |
| 550 | panic(fmt.Errorf("Unexpected config string format: %s", configString)) |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 551 | } |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 552 | archString := configTokens[0] |
| 553 | osString := configTokens[1] |
| 554 | targetString := fmt.Sprintf("%s_%s", osString, archString) |
| 555 | allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString)) |
| 556 | labelsString := strings.Join(labels, ",\n ") |
| 557 | configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, labelsString) |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 560 | return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n "))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 561 | } |
| 562 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 563 | func indent(original string) string { |
| 564 | result := "" |
| 565 | for _, line := range strings.Split(original, "\n") { |
| 566 | result += " " + line + "\n" |
| 567 | } |
| 568 | return result |
| 569 | } |
| 570 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 571 | // Returns the file contents of the buildroot.cquery file that should be used for the cquery |
| 572 | // expression in order to obtain information about buildroot and its dependencies. |
| 573 | // The contents of this file depend on the bazelContext's requests; requests are enumerated |
| 574 | // and grouped by their request type. The data retrieved for each label depends on its |
| 575 | // request type. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 576 | func (context *bazelContext) cqueryStarlarkFileContents() []byte { |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 577 | requestTypeToCqueryIdEntries := map[cqueryRequest][]string{} |
Usta Shrestha | 2bc1cd9 | 2022-06-23 13:45:24 -0400 | [diff] [blame] | 578 | for val := range context.requests { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 579 | cqueryId := getCqueryId(val) |
| 580 | mapEntryString := fmt.Sprintf("%q : True", cqueryId) |
| 581 | requestTypeToCqueryIdEntries[val.requestType] = |
| 582 | append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString) |
| 583 | } |
| 584 | labelRegistrationMapSection := "" |
| 585 | functionDefSection := "" |
| 586 | mainSwitchSection := "" |
| 587 | |
| 588 | mapDeclarationFormatString := ` |
| 589 | %s = { |
| 590 | %s |
| 591 | } |
| 592 | ` |
| 593 | functionDefFormatString := ` |
| 594 | def %s(target): |
| 595 | %s |
| 596 | ` |
| 597 | mainSwitchSectionFormatString := ` |
| 598 | if id_string in %s: |
| 599 | return id_string + ">>" + %s(target) |
| 600 | ` |
| 601 | |
Usta Shrestha | 0b52d83 | 2022-02-04 21:37:39 -0500 | [diff] [blame] | 602 | for requestType := range requestTypeToCqueryIdEntries { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 603 | labelMapName := requestType.Name() + "_Labels" |
| 604 | functionName := requestType.Name() + "_Fn" |
| 605 | labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString, |
| 606 | labelMapName, |
| 607 | strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n ")) |
| 608 | functionDefSection += fmt.Sprintf(functionDefFormatString, |
| 609 | functionName, |
| 610 | indent(requestType.StarlarkFunctionBody())) |
| 611 | mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString, |
| 612 | labelMapName, functionName) |
| 613 | } |
| 614 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 615 | formatString := ` |
| 616 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 617 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 618 | # Label Map Section |
| 619 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 620 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 621 | # Function Def Section |
| 622 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 623 | |
| 624 | def get_arch(target): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 625 | # TODO(b/199363072): filegroups and file targets aren't associated with any |
| 626 | # specific platform architecture in mixed builds. This is consistent with how |
| 627 | # Soong treats filegroups, but it may not be the case with manually-written |
| 628 | # filegroup BUILD targets. |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 629 | buildoptions = build_options(target) |
Jingwen Chen | 8f22274 | 2021-10-07 12:02:23 +0000 | [diff] [blame] | 630 | if buildoptions == None: |
| 631 | # File targets do not have buildoptions. File targets aren't associated with |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 632 | # any specific platform architecture in mixed builds, so use the host. |
| 633 | return "x86_64|linux" |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 634 | platforms = build_options(target)["//command_line_option:platforms"] |
| 635 | if len(platforms) != 1: |
| 636 | # An individual configured target should have only one platform architecture. |
| 637 | # Note that it's fine for there to be multiple architectures for the same label, |
| 638 | # but each is its own configured target. |
| 639 | fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms)) |
| 640 | platform_name = build_options(target)["//command_line_option:platforms"][0].name |
| 641 | if platform_name == "host": |
| 642 | return "HOST" |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 643 | elif platform_name.startswith("android_"): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 644 | return platform_name[len("android_"):] + "|" + platform_name[:len("android_")-1] |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 645 | elif platform_name.startswith("linux_"): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 646 | return platform_name[len("linux_"):] + "|" + platform_name[:len("linux_")-1] |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 647 | else: |
| 648 | 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] | 649 | return "UNKNOWN" |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 650 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 651 | def format(target): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 652 | id_string = str(target.label) + "|" + get_arch(target) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 653 | |
| 654 | # Main switch section |
| 655 | %s |
| 656 | # This target was not requested via cquery, and thus must be a dependency |
| 657 | # of a requested target. |
| 658 | return id_string + ">>NONE" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 659 | ` |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 660 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 661 | return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection, |
| 662 | mainSwitchSection)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 663 | } |
| 664 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 665 | // Returns a path containing build-related metadata required for interfacing |
| 666 | // with Bazel. Example: out/soong/bazel. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 667 | func (p *bazelPaths) intermediatesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 668 | return filepath.Join(p.soongOutDir, "bazel") |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 669 | } |
| 670 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 671 | // Returns the path where the contents of the @soong_injection repository live. |
| 672 | // It is used by Soong to tell Bazel things it cannot over the command line. |
| 673 | func (p *bazelPaths) injectedFilesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 674 | return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName) |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | // Returns the path of the synthetic Bazel workspace that contains a symlink |
| 678 | // forest composed the whole source tree and BUILD files generated by bp2build. |
| 679 | func (p *bazelPaths) syntheticWorkspaceDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 680 | return filepath.Join(p.soongOutDir, "workspace") |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 683 | // Returns the path to the top level out dir ($OUT_DIR). |
| 684 | func (p *bazelPaths) outDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 685 | return filepath.Dir(p.soongOutDir) |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 688 | // Issues commands to Bazel to receive results for all cquery requests |
| 689 | // queued in the BazelContext. |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 690 | func (context *bazelContext) InvokeBazel(config Config) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 691 | context.results = make(map[cqueryKey]string) |
| 692 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 693 | var cqueryOutput string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 694 | var cqueryErr string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 695 | var err error |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 696 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 697 | soongInjectionPath := absolutePath(context.paths.injectedFilesDir()) |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 698 | mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds") |
| 699 | if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) { |
| 700 | err = os.MkdirAll(mixedBuildsPath, 0777) |
Chris Parsons | 07c1e4a | 2021-01-19 17:19:16 -0500 | [diff] [blame] | 701 | } |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 702 | if err != nil { |
| 703 | return err |
| 704 | } |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 705 | if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" { |
| 706 | err = os.MkdirAll(metricsDir, 0777) |
| 707 | if err != nil { |
| 708 | return err |
| 709 | } |
| 710 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 711 | err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666) |
| 712 | if err != nil { |
| 713 | return err |
| 714 | } |
| 715 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 716 | err = ioutil.WriteFile( |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 717 | filepath.Join(mixedBuildsPath, "main.bzl"), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 718 | context.mainBzlFileContents(), 0666) |
| 719 | if err != nil { |
| 720 | return err |
| 721 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 722 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 723 | err = ioutil.WriteFile( |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 724 | filepath.Join(mixedBuildsPath, "BUILD.bazel"), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 725 | context.mainBuildFileContents(), 0666) |
| 726 | if err != nil { |
| 727 | return err |
| 728 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 729 | cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery") |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 730 | err = ioutil.WriteFile( |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 731 | absolutePath(cqueryFileRelpath), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 732 | context.cqueryStarlarkFileContents(), 0666) |
| 733 | if err != nil { |
| 734 | return err |
| 735 | } |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 736 | |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 737 | buildrootLabel := "@soong_injection//mixed_builds:buildroot" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 738 | cqueryOutput, cqueryErr, err = context.issueBazelCommand( |
| 739 | context.paths, |
| 740 | bazel.CqueryBuildRootRunName, |
Liz Kammer | c19d5cd | 2021-10-06 18:16:58 -0400 | [diff] [blame] | 741 | bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 742 | "--output=starlark", |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 743 | "--starlark:file="+absolutePath(cqueryFileRelpath)) |
| 744 | err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 745 | []byte(cqueryOutput), 0666) |
| 746 | if err != nil { |
| 747 | return err |
| 748 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 749 | |
| 750 | if err != nil { |
| 751 | return err |
| 752 | } |
| 753 | |
| 754 | cqueryResults := map[string]string{} |
| 755 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 756 | if strings.Contains(outputLine, ">>") { |
| 757 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 758 | cqueryResults[splitLine[0]] = splitLine[1] |
| 759 | } |
| 760 | } |
| 761 | |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 762 | for val := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 763 | if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 764 | context.results[val] = cqueryResult |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 765 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 766 | return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]", |
| 767 | getCqueryId(val), cqueryOutput, cqueryErr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 771 | // Issue an aquery command to retrieve action information about the bazel build tree. |
| 772 | // |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 773 | var aqueryOutput string |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 774 | var coverageFlags []string |
| 775 | if Bool(config.productVariables.ClangCoverage) { |
| 776 | coverageFlags = append(coverageFlags, "--collect_code_coverage") |
| 777 | if len(config.productVariables.NativeCoveragePaths) > 0 || |
| 778 | len(config.productVariables.NativeCoverageExcludePaths) > 0 { |
| 779 | includePaths := JoinWithPrefixAndSeparator(config.productVariables.NativeCoveragePaths, "+", ",") |
| 780 | excludePaths := JoinWithPrefixAndSeparator(config.productVariables.NativeCoverageExcludePaths, "-", ",") |
| 781 | if len(includePaths) > 0 && len(excludePaths) > 0 { |
| 782 | includePaths += "," |
| 783 | } |
| 784 | coverageFlags = append(coverageFlags, fmt.Sprintf(`--instrumentation_filter=%s`, |
| 785 | includePaths+excludePaths)) |
| 786 | } |
| 787 | } |
| 788 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame^] | 789 | extraFlags := append([]string{"--output=jsonproto", "--include_file_write_contents"}, coverageFlags...) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 790 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 791 | aqueryOutput, _, err = context.issueBazelCommand( |
| 792 | context.paths, |
| 793 | bazel.AqueryBuildRootRunName, |
| 794 | bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}, |
| 795 | // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's |
| 796 | // proto sources, which would add a number of unnecessary dependencies. |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 797 | extraFlags...) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 798 | |
| 799 | if err != nil { |
| 800 | return err |
| 801 | } |
| 802 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 803 | context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput)) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 804 | if err != nil { |
| 805 | return err |
| 806 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 807 | |
| 808 | // Issue a build command of the phony root to generate symlink forests for dependencies of the |
| 809 | // Bazel build. This is necessary because aquery invocations do not generate this symlink forest, |
| 810 | // 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] | 811 | _, _, err = context.issueBazelCommand( |
| 812 | context.paths, |
| 813 | bazel.BazelBuildPhonyRootRunName, |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 814 | bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 815 | |
| 816 | if err != nil { |
| 817 | return err |
| 818 | } |
| 819 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 820 | // Clear requests. |
| 821 | context.requests = map[cqueryKey]bool{} |
| 822 | return nil |
| 823 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 824 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 825 | func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 826 | return context.buildStatements |
| 827 | } |
| 828 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 829 | func (context *bazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 830 | return context.depsets |
| 831 | } |
| 832 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 833 | func (context *bazelContext) OutputBase() string { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 834 | return context.paths.outputBase |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 835 | } |
| 836 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 837 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 838 | // for correctness of builds which use Bazel. |
| 839 | func BazelSingleton() Singleton { |
| 840 | return &bazelSingleton{} |
| 841 | } |
| 842 | |
| 843 | type bazelSingleton struct{} |
| 844 | |
| 845 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 846 | // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled. |
| 847 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 848 | return |
| 849 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 850 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 851 | // Add ninja file dependencies for files which all bazel invocations require. |
| 852 | bazelBuildList := absolutePath(filepath.Join( |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 853 | filepath.Dir(ctx.Config().moduleListFile), "bazel.list")) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 854 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 855 | |
| 856 | data, err := ioutil.ReadFile(bazelBuildList) |
| 857 | if err != nil { |
| 858 | ctx.Errorf(err.Error()) |
| 859 | } |
| 860 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 861 | for _, file := range files { |
| 862 | ctx.AddNinjaFileDeps(file) |
| 863 | } |
| 864 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 865 | for _, depset := range ctx.Config().BazelContext.AqueryDepsets() { |
| 866 | var outputs []Path |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 867 | for _, depsetDepHash := range depset.TransitiveDepSetHashes { |
| 868 | otherDepsetName := bazelDepsetName(depsetDepHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 869 | outputs = append(outputs, PathForPhony(ctx, otherDepsetName)) |
| 870 | } |
| 871 | for _, artifactPath := range depset.DirectArtifacts { |
| 872 | outputs = append(outputs, PathForBazelOut(ctx, artifactPath)) |
| 873 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 874 | thisDepsetName := bazelDepsetName(depset.ContentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 875 | ctx.Build(pctx, BuildParams{ |
| 876 | Rule: blueprint.Phony, |
| 877 | Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)}, |
| 878 | Implicits: outputs, |
| 879 | }) |
| 880 | } |
| 881 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 882 | executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__") |
| 883 | bazelOutDir := path.Join(executionRoot, "bazel-out") |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 884 | for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() { |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame^] | 885 | if len(buildStatement.Command) > 0 { |
| 886 | rule := NewRuleBuilder(pctx, ctx) |
| 887 | createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx) |
| 888 | desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths) |
| 889 | rule.Build(fmt.Sprintf("bazel %d", index), desc) |
| 890 | continue |
| 891 | } |
| 892 | // Certain actions returned by aquery (for instance FileWrite) do not contain a command |
| 893 | // and thus require special treatment. If BuildStatement were an interface implementing |
| 894 | // buildRule(ctx) function, the code here would just call it. |
| 895 | // Unfortunately, the BuildStatement is defined in |
| 896 | // the 'bazel' package, which cannot depend on 'android' package where ctx is defined, |
| 897 | // because this would cause circular dependency. So, until we move aquery processing |
| 898 | // to the 'android' package, we need to handle special cases here. |
| 899 | if buildStatement.Mnemonic == "FileWrite" || buildStatement.Mnemonic == "SourceSymlinkManifest" { |
| 900 | // Pass file contents as the value of the rule's "content" argument. |
| 901 | // Escape newlines and $ in the contents (the action "writeBazelFile" restores "\\n" |
| 902 | // back to the newline, and Ninja reads $$ as $. |
| 903 | escaped := strings.ReplaceAll(strings.ReplaceAll(buildStatement.FileContents, "\n", "\\n"), |
| 904 | "$", "$$") |
| 905 | ctx.Build(pctx, BuildParams{ |
| 906 | Rule: writeBazelFile, |
| 907 | Output: PathForBazelOut(ctx, buildStatement.OutputPaths[0]), |
| 908 | Description: fmt.Sprintf("%s %s", buildStatement.Mnemonic, buildStatement.OutputPaths[0]), |
| 909 | Args: map[string]string{ |
| 910 | "content": escaped, |
| 911 | }, |
| 912 | }) |
| 913 | } else { |
Rupert Shuttleworth | a29903f | 2021-04-06 16:17:33 +0000 | [diff] [blame] | 914 | panic(fmt.Sprintf("unhandled build statement: %v", buildStatement)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 915 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 916 | } |
| 917 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 918 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 919 | // Register bazel-owned build statements (obtained from the aquery invocation). |
| 920 | func createCommand(cmd *RuleBuilderCommand, buildStatement bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx PathContext) { |
| 921 | // executionRoot is the action cwd. |
| 922 | cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot)) |
| 923 | |
| 924 | // Remove old outputs, as some actions might not rerun if the outputs are detected. |
| 925 | if len(buildStatement.OutputPaths) > 0 { |
| 926 | cmd.Text("rm -f") |
| 927 | for _, outputPath := range buildStatement.OutputPaths { |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 928 | cmd.Text(fmt.Sprintf("'%s'", outputPath)) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 929 | } |
| 930 | cmd.Text("&&") |
| 931 | } |
| 932 | |
| 933 | for _, pair := range buildStatement.Env { |
| 934 | // Set per-action env variables, if any. |
| 935 | cmd.Flag(pair.Key + "=" + pair.Value) |
| 936 | } |
| 937 | |
| 938 | // The actual Bazel action. |
| 939 | cmd.Text(buildStatement.Command) |
| 940 | |
| 941 | for _, outputPath := range buildStatement.OutputPaths { |
| 942 | cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath)) |
| 943 | } |
| 944 | for _, inputPath := range buildStatement.InputPaths { |
| 945 | cmd.Implicit(PathForBazelOut(ctx, inputPath)) |
| 946 | } |
| 947 | for _, inputDepsetHash := range buildStatement.InputDepsetHashes { |
| 948 | otherDepsetName := bazelDepsetName(inputDepsetHash) |
| 949 | cmd.Implicit(PathForPhony(ctx, otherDepsetName)) |
| 950 | } |
| 951 | |
| 952 | if depfile := buildStatement.Depfile; depfile != nil { |
| 953 | // The paths in depfile are relative to `executionRoot`. |
| 954 | // Hence, they need to be corrected by replacing "bazel-out" |
| 955 | // with the full `bazelOutDir`. |
| 956 | // Otherwise, implicit outputs and implicit inputs under "bazel-out/" |
| 957 | // would be deemed missing. |
| 958 | // (Note: The regexp uses a capture group because the version of sed |
| 959 | // does not support a look-behind pattern.) |
| 960 | replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`, |
| 961 | bazelOutDir, *depfile) |
| 962 | cmd.Text(replacement) |
| 963 | cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile)) |
| 964 | } |
| 965 | |
| 966 | for _, symlinkPath := range buildStatement.SymlinkPaths { |
| 967 | cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath)) |
| 968 | } |
| 969 | } |
| 970 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 971 | func getCqueryId(key cqueryKey) string { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 972 | return key.label + "|" + getConfigString(key) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 973 | } |
| 974 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 975 | func getConfigString(key cqueryKey) string { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 976 | arch := key.configKey.arch |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 977 | if len(arch) == 0 || arch == "common" { |
Sasha Smundak | 9d46dcf | 2022-06-08 12:10:36 -0700 | [diff] [blame] | 978 | if key.configKey.osType.Class == Device { |
| 979 | // For the generic Android, the expected result is "target|android", which |
| 980 | // corresponds to the product_variable_config named "android_target" in |
| 981 | // build/bazel/platforms/BUILD.bazel. |
| 982 | arch = "target" |
| 983 | } else { |
| 984 | // Use host platform, which is currently hardcoded to be x86_64. |
| 985 | arch = "x86_64" |
| 986 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 987 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 988 | osName := key.configKey.osType.Name |
| 989 | if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 990 | // Use host OS, which is currently hardcoded to be linux. |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 991 | osName = "linux" |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 992 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 993 | return arch + "|" + osName |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 994 | } |
| 995 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 996 | func GetConfigKey(ctx BaseModuleContext) configKey { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 997 | return configKey{ |
| 998 | // use string because Arch is not a valid key in go |
| 999 | arch: ctx.Arch().String(), |
| 1000 | osType: ctx.Os(), |
| 1001 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1002 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1003 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 1004 | func bazelDepsetName(contentHash string) string { |
| 1005 | return fmt.Sprintf("bazel_depset_%s", contentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1006 | } |