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