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" |
| 19 | "errors" |
| 20 | "fmt" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 21 | "io/ioutil" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 22 | "os" |
| 23 | "os/exec" |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame^] | 24 | "path" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 25 | "path/filepath" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 26 | "runtime" |
| 27 | "strings" |
| 28 | "sync" |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 29 | |
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" |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 32 | "github.com/google/blueprint" |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame] | 33 | |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 34 | "android/soong/bazel" |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 35 | ) |
| 36 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 37 | func init() { |
| 38 | RegisterMixedBuildsMutator(InitRegistrationContext) |
| 39 | } |
| 40 | |
| 41 | func RegisterMixedBuildsMutator(ctx RegistrationContext) { |
| 42 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 43 | ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel() |
| 44 | }) |
| 45 | } |
| 46 | |
| 47 | func mixedBuildsPrepareMutator(ctx BottomUpMutatorContext) { |
| 48 | if m := ctx.Module(); m.Enabled() { |
| 49 | if mixedBuildMod, ok := m.(MixedBuildBuildable); ok { |
| 50 | if mixedBuildMod.IsMixedBuildSupported(ctx) && MixedBuildsEnabled(ctx) { |
| 51 | mixedBuildMod.QueueBazelCall(ctx) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 57 | type cqueryRequest interface { |
| 58 | // Name returns a string name for this request type. Such request type names must be unique, |
| 59 | // and must only consist of alphanumeric characters. |
| 60 | Name() string |
| 61 | |
| 62 | // StarlarkFunctionBody returns a starlark function body to process this request type. |
| 63 | // The returned string is the body of a Starlark function which obtains |
| 64 | // all request-relevant information about a target and returns a string containing |
| 65 | // this information. |
| 66 | // The function should have the following properties: |
| 67 | // - `target` is the only parameter to this function (a configured target). |
| 68 | // - The return value must be a string. |
| 69 | // - The function body should not be indented outside of its own scope. |
| 70 | StarlarkFunctionBody() string |
| 71 | } |
| 72 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 73 | // Portion of cquery map key to describe target configuration. |
| 74 | type configKey struct { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 75 | arch string |
| 76 | osType OsType |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 79 | // Map key to describe bazel cquery requests. |
| 80 | type cqueryKey struct { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 81 | label string |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 82 | requestType cqueryRequest |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 83 | configKey configKey |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 86 | // BazelContext is a context object useful for interacting with Bazel during |
| 87 | // the course of a build. Use of Bazel to evaluate part of the build graph |
| 88 | // is referred to as a "mixed build". (Some modules are managed by Soong, |
| 89 | // some are managed by Bazel). To facilitate interop between these build |
| 90 | // subgraphs, Soong may make requests to Bazel and evaluate their responses |
| 91 | // so that Soong modules may accurately depend on Bazel targets. |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 92 | type BazelContext interface { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 93 | // Add a cquery request to the bazel request queue. All queued requests |
| 94 | // will be sent to Bazel on a subsequent invocation of InvokeBazel. |
| 95 | QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) |
| 96 | |
| 97 | // ** Cquery Results Retrieval Functions |
| 98 | // The below functions pertain to retrieving cquery results from a prior |
| 99 | // InvokeBazel function call and parsing the results. |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 100 | |
| 101 | // Returns result files built by building the given bazel target label. |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 102 | GetOutputFiles(label string, cfgKey configKey) ([]string, error) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 103 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 104 | // 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] | 105 | GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 106 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 107 | // Returns the executable binary resultant from building together the python sources |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 108 | // TODO(b/232976601): Remove. |
| 109 | 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] | 110 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 111 | // ** end Cquery Results Retrieval Functions |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 112 | |
| 113 | // Issues commands to Bazel to receive results for all cquery requests |
| 114 | // queued in the BazelContext. |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 115 | InvokeBazel(config Config) error |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 116 | |
| 117 | // Returns true if bazel is enabled for the given configuration. |
| 118 | BazelEnabled() bool |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 119 | |
| 120 | // Returns the bazel output base (the root directory for all bazel intermediate outputs). |
| 121 | OutputBase() string |
| 122 | |
| 123 | // Returns build statements which should get registered to reflect Bazel's outputs. |
| 124 | BuildStatementsToRegister() []bazel.BuildStatement |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 125 | |
| 126 | // Returns the depsets defined in Bazel's aquery response. |
| 127 | AqueryDepsets() []bazel.AqueryDepset |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 130 | type bazelRunner interface { |
| 131 | issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, extraFlags ...string) (string, string, error) |
| 132 | } |
| 133 | |
| 134 | type bazelPaths struct { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 135 | homeDir string |
| 136 | bazelPath string |
| 137 | outputBase string |
| 138 | workspaceDir string |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 139 | soongOutDir string |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 140 | metricsDir string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 141 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 142 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 143 | // A context object which tracks queued requests that need to be made to Bazel, |
| 144 | // and their results after the requests have been made. |
| 145 | type bazelContext struct { |
| 146 | bazelRunner |
| 147 | paths *bazelPaths |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 148 | requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel |
| 149 | requestMutex sync.Mutex // requests can be written in parallel |
| 150 | |
| 151 | results map[cqueryKey]string // Results of cquery requests after Bazel invocations |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 152 | |
| 153 | // Build statements which should get registered to reflect Bazel's outputs. |
| 154 | buildStatements []bazel.BuildStatement |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 155 | |
| 156 | // Depsets which should be used for Bazel's build statements. |
| 157 | depsets []bazel.AqueryDepset |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | var _ BazelContext = &bazelContext{} |
| 161 | |
| 162 | // A bazel context to use when Bazel is disabled. |
| 163 | type noopBazelContext struct{} |
| 164 | |
| 165 | var _ BazelContext = noopBazelContext{} |
| 166 | |
| 167 | // A bazel context to use for tests. |
| 168 | type MockBazelContext struct { |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 169 | OutputBaseDir string |
| 170 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a05a255 | 2021-08-11 16:48:30 +0000 | [diff] [blame] | 171 | LabelToOutputFiles map[string][]string |
| 172 | LabelToCcInfo map[string]cquery.CcInfo |
| 173 | LabelToPythonBinary map[string]string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 176 | func (m MockBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
| 177 | panic("unimplemented") |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 180 | func (m MockBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { |
| 181 | result, _ := m.LabelToOutputFiles[label] |
| 182 | return result, nil |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 185 | func (m MockBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
| 186 | result, _ := m.LabelToCcInfo[label] |
| 187 | return result, nil |
| 188 | } |
| 189 | |
| 190 | func (m MockBazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { |
| 191 | result, _ := m.LabelToPythonBinary[label] |
| 192 | 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] | 193 | } |
| 194 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 195 | func (m MockBazelContext) InvokeBazel(config Config) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 196 | panic("unimplemented") |
| 197 | } |
| 198 | |
| 199 | func (m MockBazelContext) BazelEnabled() bool { |
| 200 | return true |
| 201 | } |
| 202 | |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 203 | func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 204 | |
| 205 | func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 206 | return []bazel.BuildStatement{} |
| 207 | } |
| 208 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 209 | func (m MockBazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 210 | return []bazel.AqueryDepset{} |
| 211 | } |
| 212 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 213 | var _ BazelContext = MockBazelContext{} |
| 214 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 215 | func (bazelCtx *bazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
| 216 | key := cqueryKey{label, requestType, cfgKey} |
| 217 | bazelCtx.requestMutex.Lock() |
| 218 | defer bazelCtx.requestMutex.Unlock() |
| 219 | bazelCtx.requests[key] = true |
| 220 | } |
| 221 | |
| 222 | func (bazelCtx *bazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { |
| 223 | key := cqueryKey{label, cquery.GetOutputFiles, cfgKey} |
| 224 | if rawString, ok := bazelCtx.results[key]; ok { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 225 | bazelOutput := strings.TrimSpace(rawString) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 226 | return cquery.GetOutputFiles.ParseResult(bazelOutput), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 227 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 228 | return nil, fmt.Errorf("no bazel response found for %v", key) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 231 | func (bazelCtx *bazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
| 232 | key := cqueryKey{label, cquery.GetCcInfo, cfgKey} |
| 233 | 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] | 234 | bazelOutput := strings.TrimSpace(rawString) |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 235 | 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] | 236 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 237 | 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] | 238 | } |
| 239 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 240 | func (bazelCtx *bazelContext) GetPythonBinary(label string, cfgKey configKey) (string, error) { |
| 241 | key := cqueryKey{label, cquery.GetPythonBinary, cfgKey} |
| 242 | if rawString, ok := bazelCtx.results[key]; ok { |
| 243 | bazelOutput := strings.TrimSpace(rawString) |
| 244 | return cquery.GetPythonBinary.ParseResult(bazelOutput), nil |
| 245 | } |
| 246 | return "", fmt.Errorf("no bazel response found for %v", key) |
| 247 | } |
| 248 | |
| 249 | func (n noopBazelContext) QueueBazelRequest(label string, requestType cqueryRequest, cfgKey configKey) { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 250 | panic("unimplemented") |
| 251 | } |
| 252 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 253 | func (n noopBazelContext) GetOutputFiles(label string, cfgKey configKey) ([]string, error) { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 254 | panic("unimplemented") |
| 255 | } |
| 256 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 257 | func (n noopBazelContext) GetCcInfo(label string, cfgKey configKey) (cquery.CcInfo, error) { |
| 258 | panic("unimplemented") |
| 259 | } |
| 260 | |
| 261 | func (n noopBazelContext) 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] | 262 | panic("unimplemented") |
| 263 | } |
| 264 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 265 | func (n noopBazelContext) InvokeBazel(config Config) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 266 | panic("unimplemented") |
| 267 | } |
| 268 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 269 | func (m noopBazelContext) OutputBase() string { |
| 270 | return "" |
| 271 | } |
| 272 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 273 | func (n noopBazelContext) BazelEnabled() bool { |
| 274 | return false |
| 275 | } |
| 276 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 277 | func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 278 | return []bazel.BuildStatement{} |
| 279 | } |
| 280 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 281 | func (m noopBazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 282 | return []bazel.AqueryDepset{} |
| 283 | } |
| 284 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 285 | func NewBazelContext(c *config) (BazelContext, error) { |
Chris Parsons | 8b77a00 | 2020-10-27 18:59:25 -0400 | [diff] [blame] | 286 | // TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds" |
| 287 | // are production ready. |
Jingwen Chen | 442b1a4 | 2021-06-17 07:02:15 +0000 | [diff] [blame] | 288 | if !c.IsEnvTrue("USE_BAZEL_ANALYSIS") { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 289 | return noopBazelContext{}, nil |
| 290 | } |
| 291 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 292 | p, err := bazelPathsFromConfig(c) |
| 293 | if err != nil { |
| 294 | return nil, err |
| 295 | } |
| 296 | return &bazelContext{ |
| 297 | bazelRunner: &builtinBazelRunner{}, |
| 298 | paths: p, |
| 299 | requests: make(map[cqueryKey]bool), |
| 300 | }, nil |
| 301 | } |
| 302 | |
| 303 | func bazelPathsFromConfig(c *config) (*bazelPaths, error) { |
| 304 | p := bazelPaths{ |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 305 | soongOutDir: c.soongOutDir, |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 306 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 307 | missingEnvVars := []string{} |
| 308 | if len(c.Getenv("BAZEL_HOME")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 309 | p.homeDir = c.Getenv("BAZEL_HOME") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 310 | } else { |
| 311 | missingEnvVars = append(missingEnvVars, "BAZEL_HOME") |
| 312 | } |
| 313 | if len(c.Getenv("BAZEL_PATH")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 314 | p.bazelPath = c.Getenv("BAZEL_PATH") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 315 | } else { |
| 316 | missingEnvVars = append(missingEnvVars, "BAZEL_PATH") |
| 317 | } |
| 318 | if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 319 | p.outputBase = c.Getenv("BAZEL_OUTPUT_BASE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 320 | } else { |
| 321 | missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE") |
| 322 | } |
| 323 | if len(c.Getenv("BAZEL_WORKSPACE")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 324 | p.workspaceDir = c.Getenv("BAZEL_WORKSPACE") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 325 | } else { |
| 326 | missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE") |
| 327 | } |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 328 | if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 329 | p.metricsDir = c.Getenv("BAZEL_METRICS_DIR") |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 330 | } else { |
| 331 | missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR") |
| 332 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 333 | if len(missingEnvVars) > 0 { |
| 334 | return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars)) |
| 335 | } else { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 336 | return &p, nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 340 | func (p *bazelPaths) BazelMetricsDir() string { |
| 341 | return p.metricsDir |
Patrice Arruda | 05ab2d0 | 2020-12-12 06:24:26 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 344 | func (context *bazelContext) BazelEnabled() bool { |
| 345 | return true |
| 346 | } |
| 347 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 348 | func pwdPrefix() string { |
| 349 | // Darwin doesn't have /proc |
| 350 | if runtime.GOOS != "darwin" { |
| 351 | return "PWD=/proc/self/cwd" |
| 352 | } |
| 353 | return "" |
| 354 | } |
| 355 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 356 | type bazelCommand struct { |
| 357 | command string |
| 358 | // query or label |
| 359 | expression string |
| 360 | } |
| 361 | |
| 362 | type mockBazelRunner struct { |
| 363 | bazelCommandResults map[bazelCommand]string |
| 364 | commands []bazelCommand |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 365 | extraFlags []string |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | func (r *mockBazelRunner) issueBazelCommand(paths *bazelPaths, |
| 369 | runName bazel.RunName, |
| 370 | command bazelCommand, |
| 371 | extraFlags ...string) (string, string, error) { |
| 372 | r.commands = append(r.commands, command) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 373 | r.extraFlags = append(r.extraFlags, strings.Join(extraFlags, " ")) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 374 | if ret, ok := r.bazelCommandResults[command]; ok { |
| 375 | return ret, "", nil |
| 376 | } |
| 377 | return "", "", nil |
| 378 | } |
| 379 | |
| 380 | type builtinBazelRunner struct{} |
| 381 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 382 | // Issues the given bazel command with given build label and additional flags. |
| 383 | // Returns (stdout, stderr, error). The first and second return values are strings |
| 384 | // containing the stdout and stderr of the run command, and an error is returned if |
| 385 | // the invocation returned an error code. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 386 | func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.RunName, command bazelCommand, |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 387 | extraFlags ...string) (string, string, error) { |
Romain Jobredeaux | 41fd5e4 | 2021-08-27 15:59:39 +0000 | [diff] [blame] | 388 | cmdFlags := []string{ |
| 389 | // --noautodetect_server_javabase has the practical consequence of preventing Bazel from |
| 390 | // attempting to download rules_java, which is incompatible with |
| 391 | // --experimental_repository_disable_download set further below. |
| 392 | // rules_java is also not needed until mixed builds start building java targets. |
| 393 | // TODO(b/197958133): Once rules_java is pulled into AOSP, remove this flag. |
| 394 | "--noautodetect_server_javabase", |
| 395 | "--output_base=" + absolutePath(paths.outputBase), |
| 396 | command.command, |
| 397 | } |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 398 | cmdFlags = append(cmdFlags, command.expression) |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 399 | cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(paths, runName)) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 400 | |
| 401 | // Set default platforms to canonicalized values for mixed builds requests. |
| 402 | // If these are set in the bazelrc, they will have values that are |
| 403 | // non-canonicalized to @sourceroot labels, and thus be invalid when |
| 404 | // referenced from the buildroot. |
| 405 | // |
| 406 | // The actual platform values here may be overridden by configuration |
| 407 | // transitions from the buildroot. |
Chris Parsons | ee423b0 | 2021-02-08 23:04:59 -0500 | [diff] [blame] | 408 | cmdFlags = append(cmdFlags, |
Liz Kammer | c0c6609 | 2021-07-26 17:38:47 -0400 | [diff] [blame] | 409 | fmt.Sprintf("--platforms=%s", "//build/bazel/platforms:android_target")) |
Chris Parsons | ee423b0 | 2021-02-08 23:04:59 -0500 | [diff] [blame] | 410 | cmdFlags = append(cmdFlags, |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 411 | fmt.Sprintf("--extra_toolchains=%s", "//prebuilts/clang/host/linux-x86:all")) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 412 | // This should be parameterized on the host OS, but let's restrict to linux |
| 413 | // to keep things simple for now. |
| 414 | cmdFlags = append(cmdFlags, |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 415 | fmt.Sprintf("--host_platform=%s", "//build/bazel/platforms:linux_x86_64")) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 416 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 417 | // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network. |
| 418 | cmdFlags = append(cmdFlags, "--experimental_repository_disable_download") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 419 | cmdFlags = append(cmdFlags, extraFlags...) |
| 420 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 421 | bazelCmd := exec.Command(paths.bazelPath, cmdFlags...) |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 422 | bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir()) |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 423 | bazelCmd.Env = append(os.Environ(), |
| 424 | "HOME="+paths.homeDir, |
| 425 | pwdPrefix(), |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 426 | "BUILD_DIR="+absolutePath(paths.soongOutDir), |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 427 | // Make OUT_DIR absolute here so tools/bazel.sh uses the correct |
| 428 | // OUT_DIR at <root>/out, instead of <root>/out/soong/workspace/out. |
| 429 | "OUT_DIR="+absolutePath(paths.outDir()), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 430 | // Disables local host detection of gcc; toolchain information is defined |
| 431 | // explicitly in BUILD files. |
| 432 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1") |
Colin Cross | ff0278b | 2020-10-09 19:24:15 -0700 | [diff] [blame] | 433 | stderr := &bytes.Buffer{} |
| 434 | bazelCmd.Stderr = stderr |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 435 | |
| 436 | if output, err := bazelCmd.Output(); err != nil { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 437 | return "", string(stderr.Bytes()), |
| 438 | fmt.Errorf("bazel command failed. command: [%s], env: [%s], error [%s]", bazelCmd, bazelCmd.Env, stderr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 439 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 440 | return string(output), string(stderr.Bytes()), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 444 | func (context *bazelContext) mainBzlFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 445 | // TODO(cparsons): Define configuration transitions programmatically based |
| 446 | // on available archs. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 447 | contents := ` |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 448 | ##################################################### |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 449 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 450 | ##################################################### |
| 451 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 452 | def _config_node_transition_impl(settings, attr): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 453 | return { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 454 | "//command_line_option:platforms": "@//build/bazel/platforms:%s_%s" % (attr.os, attr.arch), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 455 | } |
| 456 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 457 | _config_node_transition = transition( |
| 458 | implementation = _config_node_transition_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 459 | inputs = [], |
| 460 | outputs = [ |
| 461 | "//command_line_option:platforms", |
| 462 | ], |
| 463 | ) |
| 464 | |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 465 | def _passthrough_rule_impl(ctx): |
| 466 | return [DefaultInfo(files = depset(ctx.files.deps))] |
| 467 | |
| 468 | config_node = rule( |
| 469 | implementation = _passthrough_rule_impl, |
| 470 | attrs = { |
| 471 | "arch" : attr.string(mandatory = True), |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 472 | "os" : attr.string(mandatory = True), |
| 473 | "deps" : attr.label_list(cfg = _config_node_transition, allow_files = True), |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 474 | "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 475 | }, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 476 | ) |
| 477 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 478 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 479 | # Rule representing the root of the build, to depend on all Bazel targets that |
| 480 | # are required for the build. Building this target will build the entire Bazel |
| 481 | # build tree. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 482 | mixed_build_root = rule( |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 483 | implementation = _passthrough_rule_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 484 | attrs = { |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 485 | "deps" : attr.label_list(), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 486 | }, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 487 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 488 | |
| 489 | def _phony_root_impl(ctx): |
| 490 | return [] |
| 491 | |
| 492 | # Rule to depend on other targets but build nothing. |
| 493 | # This is useful as follows: building a target of this rule will generate |
| 494 | # symlink forests for all dependencies of the target, without executing any |
| 495 | # actions of the build. |
| 496 | phony_root = rule( |
| 497 | implementation = _phony_root_impl, |
| 498 | attrs = {"deps" : attr.label_list()}, |
| 499 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 500 | ` |
| 501 | return []byte(contents) |
| 502 | } |
| 503 | |
| 504 | func (context *bazelContext) mainBuildFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 505 | // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded |
| 506 | // architecture mapping. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 507 | formatString := ` |
| 508 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 509 | load(":main.bzl", "config_node", "mixed_build_root", "phony_root") |
| 510 | |
| 511 | %s |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 512 | |
| 513 | mixed_build_root(name = "buildroot", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 514 | deps = [%s], |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 515 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 516 | |
| 517 | phony_root(name = "phonyroot", |
| 518 | deps = [":buildroot"], |
| 519 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 520 | ` |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 521 | configNodeFormatString := ` |
| 522 | config_node(name = "%s", |
| 523 | arch = "%s", |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 524 | os = "%s", |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 525 | deps = [%s], |
| 526 | ) |
| 527 | ` |
| 528 | |
| 529 | configNodesSection := "" |
| 530 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 531 | labelsByConfig := map[string][]string{} |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 532 | for val, _ := range context.requests { |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 533 | labelString := fmt.Sprintf("\"@%s\"", val.label) |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 534 | configString := getConfigString(val) |
| 535 | labelsByConfig[configString] = append(labelsByConfig[configString], labelString) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 536 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 537 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 538 | allLabels := []string{} |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 539 | for configString, labels := range labelsByConfig { |
| 540 | configTokens := strings.Split(configString, "|") |
| 541 | if len(configTokens) != 2 { |
| 542 | panic(fmt.Errorf("Unexpected config string format: %s", configString)) |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 543 | } |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 544 | archString := configTokens[0] |
| 545 | osString := configTokens[1] |
| 546 | targetString := fmt.Sprintf("%s_%s", osString, archString) |
| 547 | allLabels = append(allLabels, fmt.Sprintf("\":%s\"", targetString)) |
| 548 | labelsString := strings.Join(labels, ",\n ") |
| 549 | configNodesSection += fmt.Sprintf(configNodeFormatString, targetString, archString, osString, labelsString) |
Chris Parsons | ad0b5ba | 2021-03-29 21:09:24 -0400 | [diff] [blame] | 550 | } |
| 551 | |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 552 | return []byte(fmt.Sprintf(formatString, configNodesSection, strings.Join(allLabels, ",\n "))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 553 | } |
| 554 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 555 | func indent(original string) string { |
| 556 | result := "" |
| 557 | for _, line := range strings.Split(original, "\n") { |
| 558 | result += " " + line + "\n" |
| 559 | } |
| 560 | return result |
| 561 | } |
| 562 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 563 | // Returns the file contents of the buildroot.cquery file that should be used for the cquery |
| 564 | // expression in order to obtain information about buildroot and its dependencies. |
| 565 | // The contents of this file depend on the bazelContext's requests; requests are enumerated |
| 566 | // and grouped by their request type. The data retrieved for each label depends on its |
| 567 | // request type. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 568 | func (context *bazelContext) cqueryStarlarkFileContents() []byte { |
Liz Kammer | f29df7c | 2021-04-02 13:37:39 -0400 | [diff] [blame] | 569 | requestTypeToCqueryIdEntries := map[cqueryRequest][]string{} |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 570 | for val, _ := range context.requests { |
| 571 | cqueryId := getCqueryId(val) |
| 572 | mapEntryString := fmt.Sprintf("%q : True", cqueryId) |
| 573 | requestTypeToCqueryIdEntries[val.requestType] = |
| 574 | append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString) |
| 575 | } |
| 576 | labelRegistrationMapSection := "" |
| 577 | functionDefSection := "" |
| 578 | mainSwitchSection := "" |
| 579 | |
| 580 | mapDeclarationFormatString := ` |
| 581 | %s = { |
| 582 | %s |
| 583 | } |
| 584 | ` |
| 585 | functionDefFormatString := ` |
| 586 | def %s(target): |
| 587 | %s |
| 588 | ` |
| 589 | mainSwitchSectionFormatString := ` |
| 590 | if id_string in %s: |
| 591 | return id_string + ">>" + %s(target) |
| 592 | ` |
| 593 | |
Usta Shrestha | 0b52d83 | 2022-02-04 21:37:39 -0500 | [diff] [blame] | 594 | for requestType := range requestTypeToCqueryIdEntries { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 595 | labelMapName := requestType.Name() + "_Labels" |
| 596 | functionName := requestType.Name() + "_Fn" |
| 597 | labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString, |
| 598 | labelMapName, |
| 599 | strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n ")) |
| 600 | functionDefSection += fmt.Sprintf(functionDefFormatString, |
| 601 | functionName, |
| 602 | indent(requestType.StarlarkFunctionBody())) |
| 603 | mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString, |
| 604 | labelMapName, functionName) |
| 605 | } |
| 606 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 607 | formatString := ` |
| 608 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 609 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 610 | # Label Map Section |
| 611 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 612 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 613 | # Function Def Section |
| 614 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 615 | |
| 616 | def get_arch(target): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 617 | # TODO(b/199363072): filegroups and file targets aren't associated with any |
| 618 | # specific platform architecture in mixed builds. This is consistent with how |
| 619 | # Soong treats filegroups, but it may not be the case with manually-written |
| 620 | # filegroup BUILD targets. |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 621 | buildoptions = build_options(target) |
Jingwen Chen | 8f22274 | 2021-10-07 12:02:23 +0000 | [diff] [blame] | 622 | if buildoptions == None: |
| 623 | # File targets do not have buildoptions. File targets aren't associated with |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 624 | # any specific platform architecture in mixed builds, so use the host. |
| 625 | return "x86_64|linux" |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 626 | platforms = build_options(target)["//command_line_option:platforms"] |
| 627 | if len(platforms) != 1: |
| 628 | # An individual configured target should have only one platform architecture. |
| 629 | # Note that it's fine for there to be multiple architectures for the same label, |
| 630 | # but each is its own configured target. |
| 631 | fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms)) |
| 632 | platform_name = build_options(target)["//command_line_option:platforms"][0].name |
| 633 | if platform_name == "host": |
| 634 | return "HOST" |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 635 | elif platform_name.startswith("android_"): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 636 | return platform_name[len("android_"):] + "|" + platform_name[:len("android_")-1] |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 637 | elif platform_name.startswith("linux_"): |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 638 | return platform_name[len("linux_"):] + "|" + platform_name[:len("linux_")-1] |
Chris Parsons | 94a0bba | 2021-06-04 15:03:47 -0400 | [diff] [blame] | 639 | else: |
| 640 | fail("expected platform name of the form 'android_<arch>' or 'linux_<arch>', but was " + str(platforms)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 641 | return "UNKNOWN" |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 642 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 643 | def format(target): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 644 | id_string = str(target.label) + "|" + get_arch(target) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 645 | |
| 646 | # Main switch section |
| 647 | %s |
| 648 | # This target was not requested via cquery, and thus must be a dependency |
| 649 | # of a requested target. |
| 650 | return id_string + ">>NONE" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 651 | ` |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 652 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 653 | return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection, |
| 654 | mainSwitchSection)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 657 | // Returns a path containing build-related metadata required for interfacing |
| 658 | // with Bazel. Example: out/soong/bazel. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 659 | func (p *bazelPaths) intermediatesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 660 | return filepath.Join(p.soongOutDir, "bazel") |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 661 | } |
| 662 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 663 | // Returns the path where the contents of the @soong_injection repository live. |
| 664 | // It is used by Soong to tell Bazel things it cannot over the command line. |
| 665 | func (p *bazelPaths) injectedFilesDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 666 | return filepath.Join(p.soongOutDir, bazel.SoongInjectionDirName) |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | // Returns the path of the synthetic Bazel workspace that contains a symlink |
| 670 | // forest composed the whole source tree and BUILD files generated by bp2build. |
| 671 | func (p *bazelPaths) syntheticWorkspaceDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 672 | return filepath.Join(p.soongOutDir, "workspace") |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 675 | // Returns the path to the top level out dir ($OUT_DIR). |
| 676 | func (p *bazelPaths) outDir() string { |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 677 | return filepath.Dir(p.soongOutDir) |
Jingwen Chen | 8c52358 | 2021-06-01 11:19:53 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 680 | // Issues commands to Bazel to receive results for all cquery requests |
| 681 | // queued in the BazelContext. |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 682 | func (context *bazelContext) InvokeBazel(config Config) error { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 683 | context.results = make(map[cqueryKey]string) |
| 684 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 685 | var cqueryOutput string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 686 | var cqueryErr string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 687 | var err error |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 688 | |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 689 | soongInjectionPath := absolutePath(context.paths.injectedFilesDir()) |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 690 | mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds") |
| 691 | if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) { |
| 692 | err = os.MkdirAll(mixedBuildsPath, 0777) |
Chris Parsons | 07c1e4a | 2021-01-19 17:19:16 -0500 | [diff] [blame] | 693 | } |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 694 | if err != nil { |
| 695 | return err |
| 696 | } |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 697 | if metricsDir := context.paths.BazelMetricsDir(); metricsDir != "" { |
| 698 | err = os.MkdirAll(metricsDir, 0777) |
| 699 | if err != nil { |
| 700 | return err |
| 701 | } |
| 702 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 703 | err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "WORKSPACE.bazel"), []byte{}, 0666) |
| 704 | if err != nil { |
| 705 | return err |
| 706 | } |
| 707 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 708 | err = ioutil.WriteFile( |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 709 | filepath.Join(mixedBuildsPath, "main.bzl"), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 710 | context.mainBzlFileContents(), 0666) |
| 711 | if err != nil { |
| 712 | return err |
| 713 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 714 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 715 | err = ioutil.WriteFile( |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 716 | filepath.Join(mixedBuildsPath, "BUILD.bazel"), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 717 | context.mainBuildFileContents(), 0666) |
| 718 | if err != nil { |
| 719 | return err |
| 720 | } |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 721 | cqueryFileRelpath := filepath.Join(context.paths.injectedFilesDir(), "buildroot.cquery") |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 722 | err = ioutil.WriteFile( |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 723 | absolutePath(cqueryFileRelpath), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 724 | context.cqueryStarlarkFileContents(), 0666) |
| 725 | if err != nil { |
| 726 | return err |
| 727 | } |
Jingwen Chen | 1e34786 | 2021-09-02 12:11:49 +0000 | [diff] [blame] | 728 | |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 729 | buildrootLabel := "@soong_injection//mixed_builds:buildroot" |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 730 | cqueryOutput, cqueryErr, err = context.issueBazelCommand( |
| 731 | context.paths, |
| 732 | bazel.CqueryBuildRootRunName, |
Liz Kammer | c19d5cd | 2021-10-06 18:16:58 -0400 | [diff] [blame] | 733 | bazelCommand{"cquery", fmt.Sprintf("deps(%s, 2)", buildrootLabel)}, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 734 | "--output=starlark", |
Lukacs T. Berki | d6cd813 | 2021-04-20 13:01:07 +0200 | [diff] [blame] | 735 | "--starlark:file="+absolutePath(cqueryFileRelpath)) |
| 736 | err = ioutil.WriteFile(filepath.Join(soongInjectionPath, "cquery.out"), |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 737 | []byte(cqueryOutput), 0666) |
| 738 | if err != nil { |
| 739 | return err |
| 740 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 741 | |
| 742 | if err != nil { |
| 743 | return err |
| 744 | } |
| 745 | |
| 746 | cqueryResults := map[string]string{} |
| 747 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 748 | if strings.Contains(outputLine, ">>") { |
| 749 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 750 | cqueryResults[splitLine[0]] = splitLine[1] |
| 751 | } |
| 752 | } |
| 753 | |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 754 | for val := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 755 | if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { |
Usta Shrestha | 902fd17 | 2022-03-02 15:27:49 -0500 | [diff] [blame] | 756 | context.results[val] = cqueryResult |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 757 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 758 | return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]", |
| 759 | getCqueryId(val), cqueryOutput, cqueryErr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 763 | // Issue an aquery command to retrieve action information about the bazel build tree. |
| 764 | // |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 765 | var aqueryOutput string |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 766 | var coverageFlags []string |
| 767 | if Bool(config.productVariables.ClangCoverage) { |
| 768 | coverageFlags = append(coverageFlags, "--collect_code_coverage") |
| 769 | if len(config.productVariables.NativeCoveragePaths) > 0 || |
| 770 | len(config.productVariables.NativeCoverageExcludePaths) > 0 { |
| 771 | includePaths := JoinWithPrefixAndSeparator(config.productVariables.NativeCoveragePaths, "+", ",") |
| 772 | excludePaths := JoinWithPrefixAndSeparator(config.productVariables.NativeCoverageExcludePaths, "-", ",") |
| 773 | if len(includePaths) > 0 && len(excludePaths) > 0 { |
| 774 | includePaths += "," |
| 775 | } |
| 776 | coverageFlags = append(coverageFlags, fmt.Sprintf(`--instrumentation_filter=%s`, |
| 777 | includePaths+excludePaths)) |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | extraFlags := append([]string{"--output=jsonproto"}, coverageFlags...) |
| 782 | |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 783 | aqueryOutput, _, err = context.issueBazelCommand( |
| 784 | context.paths, |
| 785 | bazel.AqueryBuildRootRunName, |
| 786 | bazelCommand{"aquery", fmt.Sprintf("deps(%s)", buildrootLabel)}, |
| 787 | // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's |
| 788 | // proto sources, which would add a number of unnecessary dependencies. |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 789 | extraFlags...) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 790 | |
| 791 | if err != nil { |
| 792 | return err |
| 793 | } |
| 794 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 795 | context.buildStatements, context.depsets, err = bazel.AqueryBuildStatements([]byte(aqueryOutput)) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 796 | if err != nil { |
| 797 | return err |
| 798 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 799 | |
| 800 | // Issue a build command of the phony root to generate symlink forests for dependencies of the |
| 801 | // Bazel build. This is necessary because aquery invocations do not generate this symlink forest, |
| 802 | // but some of symlinks may be required to resolve source dependencies of the build. |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 803 | _, _, err = context.issueBazelCommand( |
| 804 | context.paths, |
| 805 | bazel.BazelBuildPhonyRootRunName, |
Lukacs T. Berki | 3069dd9 | 2021-05-11 16:54:29 +0200 | [diff] [blame] | 806 | bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"}) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 807 | |
| 808 | if err != nil { |
| 809 | return err |
| 810 | } |
| 811 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 812 | // Clear requests. |
| 813 | context.requests = map[cqueryKey]bool{} |
| 814 | return nil |
| 815 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 816 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 817 | func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 818 | return context.buildStatements |
| 819 | } |
| 820 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 821 | func (context *bazelContext) AqueryDepsets() []bazel.AqueryDepset { |
| 822 | return context.depsets |
| 823 | } |
| 824 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 825 | func (context *bazelContext) OutputBase() string { |
Liz Kammer | 8d62a4f | 2021-04-08 09:47:28 -0400 | [diff] [blame] | 826 | return context.paths.outputBase |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 827 | } |
| 828 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 829 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 830 | // for correctness of builds which use Bazel. |
| 831 | func BazelSingleton() Singleton { |
| 832 | return &bazelSingleton{} |
| 833 | } |
| 834 | |
| 835 | type bazelSingleton struct{} |
| 836 | |
| 837 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 838 | // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled. |
| 839 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 840 | return |
| 841 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 842 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 843 | // Add ninja file dependencies for files which all bazel invocations require. |
| 844 | bazelBuildList := absolutePath(filepath.Join( |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 845 | filepath.Dir(ctx.Config().moduleListFile), "bazel.list")) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 846 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 847 | |
| 848 | data, err := ioutil.ReadFile(bazelBuildList) |
| 849 | if err != nil { |
| 850 | ctx.Errorf(err.Error()) |
| 851 | } |
| 852 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 853 | for _, file := range files { |
| 854 | ctx.AddNinjaFileDeps(file) |
| 855 | } |
| 856 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 857 | for _, depset := range ctx.Config().BazelContext.AqueryDepsets() { |
| 858 | var outputs []Path |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 859 | for _, depsetDepHash := range depset.TransitiveDepSetHashes { |
| 860 | otherDepsetName := bazelDepsetName(depsetDepHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 861 | outputs = append(outputs, PathForPhony(ctx, otherDepsetName)) |
| 862 | } |
| 863 | for _, artifactPath := range depset.DirectArtifacts { |
| 864 | outputs = append(outputs, PathForBazelOut(ctx, artifactPath)) |
| 865 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 866 | thisDepsetName := bazelDepsetName(depset.ContentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 867 | ctx.Build(pctx, BuildParams{ |
| 868 | Rule: blueprint.Phony, |
| 869 | Outputs: []WritablePath{PathForPhony(ctx, thisDepsetName)}, |
| 870 | Implicits: outputs, |
| 871 | }) |
| 872 | } |
| 873 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame^] | 874 | executionRoot := path.Join(ctx.Config().BazelContext.OutputBase(), "execroot", "__main__") |
| 875 | bazelOutDir := path.Join(executionRoot, "bazel-out") |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 876 | for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 877 | if len(buildStatement.Command) < 1 { |
Rupert Shuttleworth | a29903f | 2021-04-06 16:17:33 +0000 | [diff] [blame] | 878 | panic(fmt.Sprintf("unhandled build statement: %v", buildStatement)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 879 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 880 | rule := NewRuleBuilder(pctx, ctx) |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame^] | 881 | createCommand(rule.Command(), buildStatement, executionRoot, bazelOutDir, ctx) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 882 | // This is required to silence warnings pertaining to unexpected timestamps. Particularly, |
| 883 | // some Bazel builtins (such as files in the bazel_tools directory) have far-future |
| 884 | // timestamps. Without restat, Ninja would emit warnings that the input files of a |
| 885 | // build statement have later timestamps than the outputs. |
| 886 | rule.Restat() |
| 887 | |
Chris Parsons | 155d768 | 2022-02-18 16:32:24 -0500 | [diff] [blame] | 888 | desc := fmt.Sprintf("%s: %s", buildStatement.Mnemonic, buildStatement.OutputPaths) |
| 889 | rule.Build(fmt.Sprintf("bazel %d", index), desc) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 890 | } |
| 891 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 892 | |
Usta Shrestha | acd5a0c | 2022-06-22 11:20:50 -0400 | [diff] [blame^] | 893 | // Register bazel-owned build statements (obtained from the aquery invocation). |
| 894 | func createCommand(cmd *RuleBuilderCommand, buildStatement bazel.BuildStatement, executionRoot string, bazelOutDir string, ctx PathContext) { |
| 895 | // executionRoot is the action cwd. |
| 896 | cmd.Text(fmt.Sprintf("cd '%s' &&", executionRoot)) |
| 897 | |
| 898 | // Remove old outputs, as some actions might not rerun if the outputs are detected. |
| 899 | if len(buildStatement.OutputPaths) > 0 { |
| 900 | cmd.Text("rm -f") |
| 901 | for _, outputPath := range buildStatement.OutputPaths { |
| 902 | cmd.Text(outputPath) |
| 903 | } |
| 904 | cmd.Text("&&") |
| 905 | } |
| 906 | |
| 907 | for _, pair := range buildStatement.Env { |
| 908 | // Set per-action env variables, if any. |
| 909 | cmd.Flag(pair.Key + "=" + pair.Value) |
| 910 | } |
| 911 | |
| 912 | // The actual Bazel action. |
| 913 | cmd.Text(buildStatement.Command) |
| 914 | |
| 915 | for _, outputPath := range buildStatement.OutputPaths { |
| 916 | cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath)) |
| 917 | } |
| 918 | for _, inputPath := range buildStatement.InputPaths { |
| 919 | cmd.Implicit(PathForBazelOut(ctx, inputPath)) |
| 920 | } |
| 921 | for _, inputDepsetHash := range buildStatement.InputDepsetHashes { |
| 922 | otherDepsetName := bazelDepsetName(inputDepsetHash) |
| 923 | cmd.Implicit(PathForPhony(ctx, otherDepsetName)) |
| 924 | } |
| 925 | |
| 926 | if depfile := buildStatement.Depfile; depfile != nil { |
| 927 | // The paths in depfile are relative to `executionRoot`. |
| 928 | // Hence, they need to be corrected by replacing "bazel-out" |
| 929 | // with the full `bazelOutDir`. |
| 930 | // Otherwise, implicit outputs and implicit inputs under "bazel-out/" |
| 931 | // would be deemed missing. |
| 932 | // (Note: The regexp uses a capture group because the version of sed |
| 933 | // does not support a look-behind pattern.) |
| 934 | replacement := fmt.Sprintf(`&& sed -i'' -E 's@(^|\s|")bazel-out/@\1%s/@g' '%s'`, |
| 935 | bazelOutDir, *depfile) |
| 936 | cmd.Text(replacement) |
| 937 | cmd.ImplicitDepFile(PathForBazelOut(ctx, *depfile)) |
| 938 | } |
| 939 | |
| 940 | for _, symlinkPath := range buildStatement.SymlinkPaths { |
| 941 | cmd.ImplicitSymlinkOutput(PathForBazelOut(ctx, symlinkPath)) |
| 942 | } |
| 943 | } |
| 944 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 945 | func getCqueryId(key cqueryKey) string { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 946 | return key.label + "|" + getConfigString(key) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 947 | } |
| 948 | |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 949 | func getConfigString(key cqueryKey) string { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 950 | arch := key.configKey.arch |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 951 | if len(arch) == 0 || arch == "common" { |
| 952 | // Use host platform, which is currently hardcoded to be x86_64. |
| 953 | arch = "x86_64" |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 954 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 955 | osName := key.configKey.osType.Name |
| 956 | if len(osName) == 0 || osName == "common_os" || osName == "linux_glibc" { |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 957 | // Use host OS, which is currently hardcoded to be linux. |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 958 | osName = "linux" |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 959 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 960 | return arch + "|" + osName |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 961 | } |
| 962 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 963 | func GetConfigKey(ctx BaseModuleContext) configKey { |
Liz Kammer | 0940b89 | 2022-03-18 15:55:04 -0400 | [diff] [blame] | 964 | return configKey{ |
| 965 | // use string because Arch is not a valid key in go |
| 966 | arch: ctx.Arch().String(), |
| 967 | osType: ctx.Os(), |
| 968 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 969 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 970 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 971 | func bazelDepsetName(contentHash string) string { |
| 972 | return fmt.Sprintf("bazel_depset_%s", contentHash) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 973 | } |