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