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 | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame^] | 346 | if !c.IsMixedBuildsEnabled() { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 347 | return noopBazelContext{}, nil |
| 348 | } |
| 349 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 350 | p, err := bazelPathsFromConfig(c) |
| 351 | if err != nil { |
| 352 | return nil, err |
| 353 | } |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame^] | 354 | |
| 355 | // TODO(cparsons): Use a different allowlist depending on prod vs. dev |
| 356 | // bazel mode. |
| 357 | disabledModules := map[string]bool{} |
| 358 | // Don't use partially-converted cc_library targets in mixed builds, |
| 359 | // since mixed builds would generally rely on both static and shared |
| 360 | // variants of a cc_library. |
| 361 | for staticOnlyModule, _ := range GetBp2BuildAllowList().ccLibraryStaticOnly { |
| 362 | disabledModules[staticOnlyModule] = true |
| 363 | } |
| 364 | for _, disabledDevModule := range allowlists.MixedBuildsDisabledList { |
| 365 | disabledModules[disabledDevModule] = true |
| 366 | } |
| 367 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 368 | return &bazelContext{ |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame^] | 369 | bazelRunner: &builtinBazelRunner{}, |
| 370 | paths: p, |
| 371 | requests: make(map[cqueryKey]bool), |
| 372 | modulesDefaultToBazel: true, |
| 373 | bazelDisabledModules: disabledModules, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 374 | }, nil |
| 375 | } |
| 376 | |
| 377 | func bazelPathsFromConfig(c *config) (*bazelPaths, error) { |
| 378 | p := bazelPaths{ |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 379 | soongOutDir: c.soongOutDir, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 380 | } |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 381 | var missingEnvVars []string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 382 | if len(c.Getenv("BAZEL_HOME")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 383 | p.homeDir = c.Getenv("BAZEL_HOME") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 384 | } else { |
| 385 | missingEnvVars = append(missingEnvVars, "BAZEL_HOME") |
| 386 | } |
| 387 | if len(c.Getenv("BAZEL_PATH")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 388 | p.bazelPath = c.Getenv("BAZEL_PATH") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 389 | } else { |
| 390 | missingEnvVars = append(missingEnvVars, "BAZEL_PATH") |
| 391 | } |
| 392 | if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 393 | p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 394 | } else { |
| 395 | missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE") |
| 396 | } |
| 397 | if len(c.Getenv("BAZEL_WORKSPACE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 398 | p.workspaceDir = c.Getenv("BAZEL_WORKSPACE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 399 | } else { |
| 400 | missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE") |
| 401 | } |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 402 | if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 403 | p.metricsDir = c.Getenv("BAZEL_METRICS_DIR") |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 404 | } else { |
| 405 | missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR") |
| 406 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 407 | if len(missingEnvVars) > 0 { |
| 408 | return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars)) |
| 409 | } else { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 410 | return &p, nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 414 | func (p *bazelPaths) BazelMetricsDir() string { |
| 415 | return p.metricsDir |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame^] | 418 | func (context *bazelContext) BazelAllowlisted(moduleName string) bool { |
| 419 | if context.bazelDisabledModules[moduleName] { |
| 420 | return false |
| 421 | } |
| 422 | if context.bazelEnabledModules[moduleName] { |
| 423 | return true |
| 424 | } |
| 425 | return context.modulesDefaultToBazel |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 428 | func pwdPrefix() string { |
| 429 | // Darwin doesn't have /proc |
| 430 | if runtime.GOOS != "darwin" { |
| 431 | return "PWD=/proc/self/cwd" |
| 432 | } |
| 433 | return "" |
| 434 | } |
| 435 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 436 | type bazelCommand struct { |
| 437 | command string |
| 438 | // query or label |
| 439 | expression string |
| 440 | } |
| 441 | |
| 442 | type mockBazelRunner struct { |
| 443 | bazelCommandResults map[bazelCommand]string |
| 444 | commands []bazelCommand |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 445 | extraFlags []string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 448 | func (r *mockBazelRunner) issueBazelCommand(_ *bazelPaths, _ bazel.RunName, |
| 449 | command bazelCommand, extraFlags ...string) (string, string, error) { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 450 | r.commands = append(r.commands, command) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 451 | r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " ")) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 452 | if ret, ok := r.bazelCommandResults[command]; ok { |
| 453 | return ret, "", nil |
| 454 | } |
| 455 | return "", "", nil |
| 456 | } |
| 457 | |
| 458 | type builtinBazelRunner struct{} |
| 459 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 460 | // Issues the given bazel command with given build label and additional flags. |
| 461 | // Returns (stdout, stderr, error). The first and second return values are strings |
| 462 | // containing the stdout and stderr of the run command, and an error is returned if |
| 463 | // the invocation returned an error code. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 464 | func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 465 | extraFlags ...string) (string, string, error) { |
Romain Jobredeaux | 41fd5e4 | 2021-08-27 15:59:39 +0000 | [diff] [blame] | 466 | cmdFlags := []string{ |
Romain Jobredeaux | 41fd5e4 | 2021-08-27 15:59:39 +0000 | [diff] [blame] | 467 | "--output_base=" + absolutePath(paths.outputBase), |
| 468 | command.command, |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 469 | command.expression, |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 470 | // TODO(asmundak): is it needed in every build? |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 471 | "--profile=" + shared.BazelMetricsFilename(paths, runName), |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 472 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 473 | // Set default platforms to canonicalized values for mixed builds requests. |
| 474 | // If these are set in the bazelrc, they will have values that are |
| 475 | // non-canonicalized to @sourceroot labels, and thus be invalid when |
| 476 | // referenced from the buildroot. |
| 477 | // |
| 478 | // The actual platform values here may be overridden by configuration |
| 479 | // transitions from the buildroot. |
| 480 | fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target"), |
| 481 | fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all"), |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 482 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 483 | // This should be parameterized on the host OS, but let's restrict to linux |
| 484 | // to keep things simple for now. |
| 485 | fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64"), |
| 486 | |
| 487 | // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network. |
| 488 | "--experimental_repository_disable_download", |
| 489 | |
| 490 | // Suppress noise |
| 491 | "--ui_event_filters=-INFO", |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 492 | "--noshow_progress"} |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 493 | cmdFlags = append(cmdFlags, extraFlags...) |
| 494 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 495 | bazelCmd := exec.Command(paths.bazelPath, cmdFlags...) |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 496 | bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir()) |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 497 | extraEnv := []string{ |
| 498 | "HOME=" + paths.homeDir, |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 499 | pwdPrefix(), |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 500 | "BUILD_DIR=" + absolutePath(paths.soongOutDir), |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 501 | // Make OUT_DIR absolute here so tools/bazel.sh uses the correct |
| 502 | // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out. |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 503 | "OUT_DIR=" + absolutePath(paths.outDir()), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 504 | // Disables local host detection of gcc; toolchain information is defined |
| 505 | // explicitly in BUILD files. |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 506 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1", |
| 507 | } |
| 508 | bazelCmd.Env = append(os.Environ(), extraEnv...) |
Colin Cross | ff0278b | 2020-10-09 19:24:15 -0700 | [diff] [blame] | 509 | stderr := &bytes.Buffer{} |
| 510 | bazelCmd.Stderr = stderr |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 511 | |
| 512 | if output, err := bazelCmd.Output(); err != nil { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 513 | return "", string(stderr.Bytes()), |
| 514 | 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] | 515 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 516 | return string(output), string(stderr.Bytes()), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 520 | func (context *bazelContext) mainBzlFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 521 | // TODO(cparsons): Define configuration transitions programmatically based |
| 522 | // on available archs. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 523 | contents := ` |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 524 | ##################################################### |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 525 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 526 | ##################################################### |
| 527 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 528 | def _config_node_transition_impl(settings, attr): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 529 | return { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 530 | "//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] | 531 | } |
| 532 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 533 | _config_node_transition = transition( |
| 534 | implementation = _config_node_transition_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 535 | inputs = [], |
| 536 | outputs = [ |
| 537 | "//command_line_option:platforms", |
| 538 | ], |
| 539 | ) |
| 540 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 541 | def _passthrough_rule_impl(ctx): |
| 542 | return [DefaultInfo(files = depset(ctx.files.deps))] |
| 543 | |
| 544 | config_node = rule( |
| 545 | implementation = _passthrough_rule_impl, |
| 546 | attrs = { |
| 547 | "arch" : attr.string(mandatory = True), |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 548 | "os" : attr.string(mandatory = True), |
| 549 | "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True), |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 550 | "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 551 | }, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 552 | ) |
| 553 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 554 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 555 | # Rule representing the root of the build, to depend on all Bazel targets that |
| 556 | # are required for the build. Building this target will build the entire Bazel |
| 557 | # build tree. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 558 | mixed_build_root = rule( |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 559 | implementation = _passthrough_rule_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 560 | attrs = { |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 561 | "deps" : attr.label_list(), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 562 | }, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 563 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 564 | |
| 565 | def _phony_root_impl(ctx): |
| 566 | return [] |
| 567 | |
| 568 | # Rule to depend on other targets but build nothing. |
| 569 | # This is useful as follows: building a target of this rule will generate |
| 570 | # symlink forests for all dependencies of the target, without executing any |
| 571 | # actions of the build. |
| 572 | phony_root = rule( |
| 573 | implementation = _phony_root_impl, |
| 574 | attrs = {"deps" : attr.label_list()}, |
| 575 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 576 | ` |
| 577 | return []byte(contents) |
| 578 | } |
| 579 | |
| 580 | func (context *bazelContext) mainBuildFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 581 | // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded |
| 582 | // architecture mapping. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 583 | formatString := ` |
| 584 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 585 | load(":main.bzl", "config_node", "mixed_build_root", "phony_root") |
| 586 | |
| 587 | %s |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 588 | |
| 589 | mixed_build_root(name = "buildroot", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 590 | deps = [%s], |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 591 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 592 | |
| 593 | phony_root(name = "phonyroot", |
| 594 | deps = [":buildroot"], |
| 595 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 596 | ` |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 597 | configNodeFormatString := ` |
| 598 | config_node(name = "%s", |
| 599 | arch = "%s", |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 600 | os = "%s", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 601 | deps = [%s], |
| 602 | ) |
| 603 | ` |
| 604 | |
| 605 | configNodesSection := "" |
| 606 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 607 | labelsByConfig := map[string][]string{} |
Usta Shrestha | 2bc1cd9 | 2022-06-23 13:45:24 -0400 | [diff] [blame] | 608 | for val := range context.requests { |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 609 | labelString := fmt.Sprintf("\"@%s\"", val.label) |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 610 | configString := getConfigString(val) |
| 611 | labelsByConfig[configString] = append(labelsByConfig[configString], labelString) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 612 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 613 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 614 | allLabels := []string{} |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 615 | for configString, labels := range labelsByConfig { |
| 616 | configTokens := strings.Split(configString, "|") |
| 617 | if len(configTokens) != 2 { |
| 618 | panic(fmt.Errorf("Unexpected config string format: %s", configString)) |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 619 | } |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 620 | archString := configTokens[0] |
| 621 | osString := configTokens[1] |
| 622 | targetString := fmt.Sprintf("%s_%s", osString, archString) |
| 623 | allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString)) |
| 624 | labelsString := strings.Join(labels, ",\n ") |
| 625 | configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, labelsString) |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 626 | } |
| 627 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 628 | return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n "))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 629 | } |
| 630 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 631 | func indent(original string) string { |
| 632 | result := "" |
| 633 | for _, line := range strings.Split(original, "\n") { |
| 634 | result += " " + line + "\n" |
| 635 | } |
| 636 | return result |
| 637 | } |
| 638 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 639 | // Returns the file contents of the buildroot.cquery file that should be used for the cquery |
| 640 | // expression in order to obtain information about buildroot and its dependencies. |
| 641 | // The contents of this file depend on the bazelContext's requests; requests are enumerated |
| 642 | // and grouped by their request type. The data retrieved for each label depends on its |
| 643 | // request type. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 644 | func (context *bazelContext) cqueryStarlarkFileContents() []byte { |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 645 | requestTypeToCqueryIdEntries := map[cqueryRequest][]string{} |
Usta Shrestha | 2bc1cd9 | 2022-06-23 13:45:24 -0400 | [diff] [blame] | 646 | for val := range context.requests { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 647 | cqueryId := getCqueryId(val) |
| 648 | mapEntryString := fmt.Sprintf("%q : True", cqueryId) |
| 649 | requestTypeToCqueryIdEntries[val.requestType] = |
| 650 | append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString) |
| 651 | } |
| 652 | labelRegistrationMapSection := "" |
| 653 | functionDefSection := "" |
| 654 | mainSwitchSection := "" |
| 655 | |
| 656 | mapDeclarationFormatString := ` |
| 657 | %s = { |
| 658 | %s |
| 659 | } |
| 660 | ` |
| 661 | functionDefFormatString := ` |
| 662 | def %s(target): |
| 663 | %s |
| 664 | ` |
| 665 | mainSwitchSectionFormatString := ` |
| 666 | if id_string in %s: |
| 667 | return id_string + ">>" + %s(target) |
| 668 | ` |
| 669 | |
Usta Shrestha | 0b52d83 | 2022-02-04 21:37:39 -0500 | [diff] [blame] | 670 | for requestType := range requestTypeToCqueryIdEntries { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 671 | labelMapName := requestType.Name() + "_Labels" |
| 672 | functionName := requestType.Name() + "_Fn" |
| 673 | labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString, |
| 674 | labelMapName, |
| 675 | strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n ")) |
| 676 | functionDefSection += fmt.Sprintf(functionDefFormatString, |
| 677 | functionName, |
| 678 | indent(requestType.StarlarkFunctionBody())) |
| 679 | mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString, |
| 680 | labelMapName, functionName) |
| 681 | } |
| 682 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 683 | formatString := ` |
| 684 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 685 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 686 | # Label Map Section |
| 687 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 688 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 689 | # Function Def Section |
| 690 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 691 | |
| 692 | def get_arch(target): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 693 | # TODO(b/199363072): filegroups and file targets aren't associated with any |
| 694 | # specific platform architecture in mixed builds. This is consistent with how |
| 695 | # Soong treats filegroups, but it may not be the case with manually-written |
| 696 | # filegroup BUILD targets. |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 697 | buildoptions = build_options(target) |
Jingwen Chen | 8f22274 | 2021-10-07 12:02:23 +0000 | [diff] [blame] | 698 | if buildoptions == None: |
| 699 | # File targets do not have buildoptions. File targets aren't associated with |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 700 | # any specific platform architecture in mixed builds, so use the host. |
| 701 | return "x86_64|linux" |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 702 | platforms = build_options(target)["//command_line_option:platforms"] |
| 703 | if len(platforms) != 1: |
| 704 | # An individual configured target should have only one platform architecture. |
| 705 | # Note that it's fine for there to be multiple architectures for the same label, |
| 706 | # but each is its own configured target. |
| 707 | fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms)) |
| 708 | platform_name = build_options(target)["//command_line_option:platforms"][0].name |
| 709 | if platform_name == "host": |
| 710 | return "HOST" |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 711 | elif platform_name.startswith("android_"): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 712 | return platform_name[len("android_"):] + "|" + platform_name[:len("android_")-1] |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 713 | elif platform_name.startswith("linux_"): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 714 | return platform_name[len("linux_"):] + "|" + platform_name[:len("linux_")-1] |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 715 | else: |
| 716 | 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] | 717 | return "UNKNOWN" |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 718 | |
Sasha Smundak | fe9a5b8 | 2022-07-27 14:51:45 -0700 | [diff] [blame] | 719 | def json_for_file(key, file): |
| 720 | return '"' + key + '":"' + file.path + '"' |
| 721 | |
| 722 | def json_for_files(key, files): |
| 723 | return '"' + key + '":[' + ",".join(['"' + f.path + '"' for f in files]) + ']' |
| 724 | |
| 725 | def json_for_labels(key, ll): |
| 726 | return '"' + key + '":[' + ",".join(['"' + str(x) + '"' for x in ll]) + ']' |
| 727 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 728 | def format(target): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 729 | id_string = str(target.label) + "|" + get_arch(target) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 730 | |
| 731 | # Main switch section |
| 732 | %s |
| 733 | # This target was not requested via cquery, and thus must be a dependency |
| 734 | # of a requested target. |
| 735 | return id_string + ">>NONE" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 736 | ` |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 737 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 738 | return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection, |
| 739 | mainSwitchSection)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 740 | } |
| 741 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 742 | // Returns a path containing build-related metadata required for interfacing |
| 743 | // with Bazel. Example: out/soong/bazel. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 744 | func (p *bazelPaths) intermediatesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 745 | return filepath.Join(p.soongOutDir, "bazel") |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 746 | } |
| 747 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 748 | // Returns the path where the contents of the @soong_injection repository live. |
| 749 | // It is used by Soong to tell Bazel things it cannot over the command line. |
| 750 | func (p *bazelPaths) injectedFilesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 751 | return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName) |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // Returns the path of the synthetic Bazel workspace that contains a symlink |
| 755 | // forest composed the whole source tree and BUILD files generated by bp2build. |
| 756 | func (p *bazelPaths) syntheticWorkspaceDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 757 | return filepath.Join(p.soongOutDir, "workspace") |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 760 | // Returns the path to the top level out dir ($OUT_DIR). |
| 761 | func (p *bazelPaths) outDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 762 | return filepath.Dir(p.soongOutDir) |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 765 | // Issues commands to Bazel to receive results for all cquery requests |
| 766 | // queued in the BazelContext. |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 767 | func (context *bazelContext) InvokeBazel(config Config) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 768 | context.results = make(map[cqueryKey]string) |
| 769 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 770 | var err error |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 771 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 772 | soongInjectionPath := absolutePath(context.paths.injectedFilesDir()) |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 773 | mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds") |
| 774 | if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) { |
| 775 | err = os.MkdirAll(mixedBuildsPath, 0777) |
Chris Parsons | 07c1e4a | 2021-01-19 17:19:16 -0500 | [diff] [blame] | 776 | } |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 777 | if err != nil { |
| 778 | return err |
| 779 | } |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 780 | if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" { |
| 781 | err = os.MkdirAll(metricsDir, 0777) |
| 782 | if err != nil { |
| 783 | return err |
| 784 | } |
| 785 | } |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 786 | 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] | 787 | return err |
| 788 | } |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 789 | 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] | 790 | return err |
| 791 | } |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 792 | 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] | 793 | return err |
| 794 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 795 | cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery") |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 796 | if err = ioutil.WriteFile(absolutePath(cqueryFileRelpath), context.cqueryStarlarkFileContents(), 0666); err != nil { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 797 | return err |
| 798 | } |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 799 | |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 800 | const buildrootLabel = "@soong_injection//mixed_builds:buildroot" |
| 801 | cqueryCmd := bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)} |
| 802 | cqueryOutput, cqueryErr, err := context.issueBazelCommand(context.paths, bazel.CqueryBuildRootRunName, cqueryCmd, |
| 803 | "--output=starlark", "--starlark:file="+absolutePath(cqueryFileRelpath)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 804 | if err != nil { |
Chris Parsons | 429f540 | 2022-08-11 17:02:41 -0400 | [diff] [blame] | 805 | return err |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 806 | } |
Chris Parsons | 429f540 | 2022-08-11 17:02:41 -0400 | [diff] [blame] | 807 | 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] | 808 | return err |
| 809 | } |
| 810 | |
| 811 | cqueryResults := map[string]string{} |
| 812 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 813 | if strings.Contains(outputLine, ">>") { |
| 814 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 815 | cqueryResults[splitLine[0]] = splitLine[1] |
| 816 | } |
| 817 | } |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 818 | for val := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 819 | if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 820 | context.results[val] = cqueryResult |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 821 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 822 | return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]", |
| 823 | getCqueryId(val), cqueryOutput, cqueryErr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 827 | // Issue an aquery command to retrieve action information about the bazel build tree. |
| 828 | // |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 829 | // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's |
| 830 | // proto sources, which would add a number of unnecessary dependencies. |
| 831 | extraFlags := []string{"--output=jsonproto", "--include_file_write_contents"} |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 832 | if Bool(config.productVariables.ClangCoverage) { |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 833 | extraFlags = append(extraFlags, "--collect_code_coverage") |
| 834 | paths := make([]string, 0, 2) |
| 835 | if p := config.productVariables.NativeCoveragePaths; len(p) > 0 { |
| 836 | paths = append(paths, JoinWithPrefixAndSeparator(p, "+", ",")) |
| 837 | } |
| 838 | if p := config.productVariables.NativeCoverageExcludePaths; len(p) > 0 { |
| 839 | paths = append(paths, JoinWithPrefixAndSeparator(p, "-", ",")) |
| 840 | } |
| 841 | if len(paths) > 0 { |
| 842 | extraFlags = append(extraFlags, "--instrumentation_filter="+strings.Join(paths, ",")) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
Sasha Smundak | b43ae1e | 2022-07-03 15:57:36 -0700 | [diff] [blame] | 845 | aqueryCmd := bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)} |
| 846 | if aqueryOutput, _, err := context.issueBazelCommand(context.paths, bazel.AqueryBuildRootRunName, aqueryCmd, |
| 847 | extraFlags...); err == nil { |
| 848 | context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput)) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 849 | } |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 850 | if err != nil { |
| 851 | return err |
| 852 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 853 | |
| 854 | // Issue a build command of the phony root to generate symlink forests for dependencies of the |
| 855 | // Bazel build. This is necessary because aquery invocations do not generate this symlink forest, |
| 856 | // 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] | 857 | buildCmd := bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"} |
| 858 | if _, _, err = context.issueBazelCommand(context.paths, bazel.BazelBuildPhonyRootRunName, buildCmd); err != nil { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 859 | return err |
| 860 | } |
| 861 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 862 | // Clear requests. |
| 863 | context.requests = map[cqueryKey]bool{} |
| 864 | return nil |
| 865 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 866 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 867 | func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 868 | return context.buildStatements |
| 869 | } |
| 870 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 871 | func (context *bazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 872 | return context.depsets |
| 873 | } |
| 874 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 875 | func (context *bazelContext) OutputBase() string { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 876 | return context.paths.outputBase |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 877 | } |
| 878 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 879 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 880 | // for correctness of builds which use Bazel. |
| 881 | func BazelSingleton() Singleton { |
| 882 | return &bazelSingleton{} |
| 883 | } |
| 884 | |
| 885 | type bazelSingleton struct{} |
| 886 | |
| 887 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 888 | // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled. |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame^] | 889 | if !ctx.Config().IsMixedBuildsEnabled() { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 890 | return |
| 891 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 892 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 893 | // Add ninja file dependencies for files which all bazel invocations require. |
| 894 | bazelBuildList := absolutePath(filepath.Join( |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 895 | filepath.Dir(ctx.Config().moduleListFile), "bazel.list")) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 896 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 897 | |
| 898 | data, err := ioutil.ReadFile(bazelBuildList) |
| 899 | if err != nil { |
| 900 | ctx.Errorf(err.Error()) |
| 901 | } |
| 902 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 903 | for _, file := range files { |
| 904 | ctx.AddNinjaFileDeps(file) |
| 905 | } |
| 906 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 907 | for _, depset := range ctx.Config().BazelContext.AqueryDepsets() { |
| 908 | var outputs []Path |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 909 | for _, depsetDepHash := range depset.TransitiveDepSetHashes { |
| 910 | otherDepsetName := bazelDepsetName(depsetDepHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 911 | outputs = append(outputs, PathForPhony(ctx, otherDepsetName)) |
| 912 | } |
| 913 | for _, artifactPath := range depset.DirectArtifacts { |
| 914 | outputs = append(outputs, PathForBazelOut(ctx, artifactPath)) |
| 915 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 916 | thisDepsetName := bazelDepsetName(depset.ContentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 917 | ctx.Build(pctx, BuildParams{ |
| 918 | Rule: blueprint.Phony, |
| 919 | Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)}, |
| 920 | Implicits: outputs, |
| 921 | }) |
| 922 | } |
| 923 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 924 | executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__") |
| 925 | bazelOutDir := path.Join(executionRoot, "bazel-out") |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 926 | for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() { |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 927 | if len(buildStatement.Command) > 0 { |
| 928 | rule := NewRuleBuilder(pctx, ctx) |
| 929 | createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx) |
| 930 | desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths) |
| 931 | rule.Build(fmt.Sprintf("bazel %d", index), desc) |
| 932 | continue |
| 933 | } |
| 934 | // Certain actions returned by aquery (for instance FileWrite) do not contain a command |
| 935 | // and thus require special treatment. If BuildStatement were an interface implementing |
| 936 | // buildRule(ctx) function, the code here would just call it. |
| 937 | // Unfortunately, the BuildStatement is defined in |
| 938 | // the 'bazel' package, which cannot depend on 'android' package where ctx is defined, |
| 939 | // because this would cause circular dependency. So, until we move aquery processing |
| 940 | // to the 'android' package, we need to handle special cases here. |
| 941 | if buildStatement.Mnemonic == "FileWrite" || buildStatement.Mnemonic == "SourceSymlinkManifest" { |
| 942 | // Pass file contents as the value of the rule's "content" argument. |
| 943 | // Escape newlines and $ in the contents (the action "writeBazelFile" restores "\\n" |
| 944 | // back to the newline, and Ninja reads $$ as $. |
| 945 | escaped := strings.ReplaceAll(strings.ReplaceAll(buildStatement.FileContents, "\n", "\\n"), |
| 946 | "$", "$$") |
| 947 | ctx.Build(pctx, BuildParams{ |
| 948 | Rule: writeBazelFile, |
| 949 | Output: PathForBazelOut(ctx, buildStatement.OutputPaths[0]), |
| 950 | Description: fmt.Sprintf("%s %s", buildStatement.Mnemonic, buildStatement.OutputPaths[0]), |
| 951 | Args: map[string]string{ |
| 952 | "content": escaped, |
| 953 | }, |
| 954 | }) |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 955 | } else if buildStatement.Mnemonic == "SymlinkTree" { |
| 956 | // build-runfiles arguments are the manifest file and the target directory |
| 957 | // where it creates the symlink tree according to this manifest (and then |
| 958 | // writes the MANIFEST file to it). |
| 959 | outManifest := PathForBazelOut(ctx, buildStatement.OutputPaths[0]) |
| 960 | outManifestPath := outManifest.String() |
| 961 | if !strings.HasSuffix(outManifestPath, "MANIFEST") { |
| 962 | panic("the base name of the symlink tree action should be MANIFEST, got " + outManifestPath) |
| 963 | } |
| 964 | outDir := filepath.Dir(outManifestPath) |
| 965 | ctx.Build(pctx, BuildParams{ |
| 966 | Rule: buildRunfilesRule, |
| 967 | Output: outManifest, |
| 968 | Inputs: []Path{PathForBazelOut(ctx, buildStatement.InputPaths[0])}, |
| 969 | Description: "symlink tree for " + outDir, |
| 970 | Args: map[string]string{ |
| 971 | "outDir": outDir, |
| 972 | }, |
| 973 | }) |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 974 | } else { |
Rupert Shuttleworth | a29903f | 2021-04-06 16:17:33 +0000 | [diff] [blame] | 975 | panic(fmt.Sprintf("unhandled build statement: %v", buildStatement)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 976 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 977 | } |
| 978 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 979 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 980 | // Register bazel-owned build statements (obtained from the aquery invocation). |
| 981 | func createCommand(cmd *RuleBuilderCommand, buildStatement bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx PathContext) { |
| 982 | // executionRoot is the action cwd. |
| 983 | cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot)) |
| 984 | |
| 985 | // Remove old outputs, as some actions might not rerun if the outputs are detected. |
| 986 | if len(buildStatement.OutputPaths) > 0 { |
| 987 | cmd.Text("rm -f") |
| 988 | for _, outputPath := range buildStatement.OutputPaths { |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 989 | cmd.Text(fmt.Sprintf("'%s'", outputPath)) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame] | 990 | } |
| 991 | cmd.Text("&&") |
| 992 | } |
| 993 | |
| 994 | for _, pair := range buildStatement.Env { |
| 995 | // Set per-action env variables, if any. |
| 996 | cmd.Flag(pair.Key + "=" + pair.Value) |
| 997 | } |
| 998 | |
| 999 | // The actual Bazel action. |
| 1000 | cmd.Text(buildStatement.Command) |
| 1001 | |
| 1002 | for _, outputPath := range buildStatement.OutputPaths { |
| 1003 | cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath)) |
| 1004 | } |
| 1005 | for _, inputPath := range buildStatement.InputPaths { |
| 1006 | cmd.Implicit(PathForBazelOut(ctx, inputPath)) |
| 1007 | } |
| 1008 | for _, inputDepsetHash := range buildStatement.InputDepsetHashes { |
| 1009 | otherDepsetName := bazelDepsetName(inputDepsetHash) |
| 1010 | cmd.Implicit(PathForPhony(ctx, otherDepsetName)) |
| 1011 | } |
| 1012 | |
| 1013 | if depfile := buildStatement.Depfile; depfile != nil { |
| 1014 | // The paths in depfile are relative to `executionRoot`. |
| 1015 | // Hence, they need to be corrected by replacing "bazel-out" |
| 1016 | // with the full `bazelOutDir`. |
| 1017 | // Otherwise, implicit outputs and implicit inputs under "bazel-out/" |
| 1018 | // would be deemed missing. |
| 1019 | // (Note: The regexp uses a capture group because the version of sed |
| 1020 | // does not support a look-behind pattern.) |
| 1021 | replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`, |
| 1022 | bazelOutDir, *depfile) |
| 1023 | cmd.Text(replacement) |
| 1024 | cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile)) |
| 1025 | } |
| 1026 | |
| 1027 | for _, symlinkPath := range buildStatement.SymlinkPaths { |
| 1028 | cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath)) |
| 1029 | } |
| 1030 | } |
| 1031 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1032 | func getCqueryId(key cqueryKey) string { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1033 | return key.label + "|" + getConfigString(key) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1034 | } |
| 1035 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1036 | func getConfigString(key cqueryKey) string { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 1037 | arch := key.configKey.arch |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1038 | if len(arch) == 0 || arch == "common" { |
Sasha Smundak | 9d46dcf | 2022-06-08 12:10:36 -0700 | [diff] [blame] | 1039 | if key.configKey.osType.Class == Device { |
| 1040 | // For the generic Android, the expected result is "target|android", which |
| 1041 | // corresponds to the product_variable_config named "android_target" in |
| 1042 | // build/bazel/platforms/BUILD.bazel. |
| 1043 | arch = "target" |
| 1044 | } else { |
| 1045 | // Use host platform, which is currently hardcoded to be x86_64. |
| 1046 | arch = "x86_64" |
| 1047 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1048 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 1049 | osName := key.configKey.osType.Name |
| 1050 | if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1051 | // Use host OS, which is currently hardcoded to be linux. |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 1052 | osName = "linux" |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1053 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 1054 | return arch + "|" + osName |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 1055 | } |
| 1056 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 1057 | func GetConfigKey(ctx BaseModuleContext) configKey { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 1058 | return configKey{ |
| 1059 | // use string because Arch is not a valid key in go |
| 1060 | arch: ctx.Arch().String(), |
| 1061 | osType: ctx.Os(), |
| 1062 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 1063 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1064 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 1065 | func bazelDepsetName(contentHash string) string { |
| 1066 | return fmt.Sprintf("bazel_depset_%s", contentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 1067 | } |