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