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