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