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