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