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