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