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