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