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