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