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