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)) |
Chris Parsons | ee423b0 | 2021-02-08 23:04:59 -0500 | [diff] [blame] | 273 | // Set default platforms to canonicalized values for mixed builds requests. If these are set |
| 274 | // in the bazelrc, they will have values that are non-canonicalized, and thus be invalid. |
| 275 | // The actual platform values here may be overridden by configuration transitions from the buildroot. |
| 276 | cmdFlags = append(cmdFlags, |
| 277 | fmt.Sprintf("--platforms=%s", canonicalizeLabel("//build/bazel/platforms:generic_x86_64"))) |
| 278 | cmdFlags = append(cmdFlags, |
| 279 | fmt.Sprintf("--extra_toolchains=%s", canonicalizeLabel("//prebuilts/clang/host/linux-x86:all"))) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 280 | // Explicitly disable downloading rules (such as canonical C++ and Java rules) from the network. |
| 281 | cmdFlags = append(cmdFlags, "--experimental_repository_disable_download") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 282 | cmdFlags = append(cmdFlags, extraFlags...) |
| 283 | |
| 284 | bazelCmd := exec.Command(context.bazelPath, cmdFlags...) |
| 285 | bazelCmd.Dir = context.workspaceDir |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 286 | bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix(), |
| 287 | // Disables local host detection of gcc; toolchain information is defined |
| 288 | // explicitly in BUILD files. |
| 289 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1") |
Colin Cross | ff0278b | 2020-10-09 19:24:15 -0700 | [diff] [blame] | 290 | stderr := &bytes.Buffer{} |
| 291 | bazelCmd.Stderr = stderr |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 292 | |
| 293 | if output, err := bazelCmd.Output(); err != nil { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 294 | return "", string(stderr.Bytes()), |
| 295 | 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] | 296 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 297 | return string(output), string(stderr.Bytes()), nil |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 301 | // Returns the string contents of a workspace file that should be output |
| 302 | // adjacent to the main bzl file and build file. |
| 303 | // This workspace file allows, via local_repository rule, sourcetree-level |
| 304 | // BUILD targets to be referenced via @sourceroot. |
| 305 | func (context *bazelContext) workspaceFileContents() []byte { |
| 306 | formatString := ` |
| 307 | # This file is generated by soong_build. Do not edit. |
| 308 | local_repository( |
| 309 | name = "sourceroot", |
| 310 | path = "%s", |
| 311 | ) |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame^] | 312 | |
| 313 | local_repository( |
| 314 | name = "rules_cc", |
| 315 | path = "%s/build/bazel/rules_cc", |
| 316 | ) |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 317 | ` |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame^] | 318 | return []byte(fmt.Sprintf(formatString, context.workspaceDir, context.workspaceDir)) |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 319 | } |
| 320 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 321 | func (context *bazelContext) mainBzlFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 322 | // TODO(cparsons): Define configuration transitions programmatically based |
| 323 | // on available archs. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 324 | contents := ` |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 325 | ##################################################### |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 326 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 327 | ##################################################### |
| 328 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 329 | def _x86_64_transition_impl(settings, attr): |
| 330 | return { |
| 331 | "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86_64", |
| 332 | } |
| 333 | |
| 334 | def _x86_transition_impl(settings, attr): |
| 335 | return { |
| 336 | "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_x86", |
| 337 | } |
| 338 | |
| 339 | def _arm64_transition_impl(settings, attr): |
| 340 | return { |
| 341 | "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm64", |
| 342 | } |
| 343 | |
| 344 | def _arm_transition_impl(settings, attr): |
| 345 | return { |
| 346 | "//command_line_option:platforms": "@sourceroot//build/bazel/platforms:generic_arm", |
| 347 | } |
| 348 | |
| 349 | x86_64_transition = transition( |
| 350 | implementation = _x86_64_transition_impl, |
| 351 | inputs = [], |
| 352 | outputs = [ |
| 353 | "//command_line_option:platforms", |
| 354 | ], |
| 355 | ) |
| 356 | |
| 357 | x86_transition = transition( |
| 358 | implementation = _x86_transition_impl, |
| 359 | inputs = [], |
| 360 | outputs = [ |
| 361 | "//command_line_option:platforms", |
| 362 | ], |
| 363 | ) |
| 364 | |
| 365 | arm64_transition = transition( |
| 366 | implementation = _arm64_transition_impl, |
| 367 | inputs = [], |
| 368 | outputs = [ |
| 369 | "//command_line_option:platforms", |
| 370 | ], |
| 371 | ) |
| 372 | |
| 373 | arm_transition = transition( |
| 374 | implementation = _arm_transition_impl, |
| 375 | inputs = [], |
| 376 | outputs = [ |
| 377 | "//command_line_option:platforms", |
| 378 | ], |
| 379 | ) |
| 380 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 381 | def _mixed_build_root_impl(ctx): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 382 | all_files = ctx.files.deps_x86_64 + ctx.files.deps_x86 + ctx.files.deps_arm64 + ctx.files.deps_arm |
| 383 | return [DefaultInfo(files = depset(all_files))] |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 384 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 385 | # Rule representing the root of the build, to depend on all Bazel targets that |
| 386 | # are required for the build. Building this target will build the entire Bazel |
| 387 | # build tree. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 388 | mixed_build_root = rule( |
| 389 | implementation = _mixed_build_root_impl, |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 390 | attrs = { |
| 391 | "deps_x86_64" : attr.label_list(cfg = x86_64_transition), |
| 392 | "deps_x86" : attr.label_list(cfg = x86_transition), |
| 393 | "deps_arm64" : attr.label_list(cfg = arm64_transition), |
| 394 | "deps_arm" : attr.label_list(cfg = arm_transition), |
| 395 | "_allowlist_function_transition": attr.label(default = "@bazel_tools//tools/allowlists/function_transition_allowlist"), |
| 396 | }, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 397 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 398 | |
| 399 | def _phony_root_impl(ctx): |
| 400 | return [] |
| 401 | |
| 402 | # Rule to depend on other targets but build nothing. |
| 403 | # This is useful as follows: building a target of this rule will generate |
| 404 | # symlink forests for all dependencies of the target, without executing any |
| 405 | # actions of the build. |
| 406 | phony_root = rule( |
| 407 | implementation = _phony_root_impl, |
| 408 | attrs = {"deps" : attr.label_list()}, |
| 409 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 410 | ` |
| 411 | return []byte(contents) |
| 412 | } |
| 413 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 414 | // Returns a "canonicalized" corresponding to the given sourcetree-level label. |
| 415 | // This abstraction is required because a sourcetree label such as //foo/bar:baz |
| 416 | // must be referenced via the local repository prefix, such as |
| 417 | // @sourceroot//foo/bar:baz. |
| 418 | func canonicalizeLabel(label string) string { |
| 419 | if strings.HasPrefix(label, "//") { |
| 420 | return "@sourceroot" + label |
| 421 | } else { |
| 422 | return "@sourceroot//" + label |
| 423 | } |
| 424 | } |
| 425 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 426 | func (context *bazelContext) mainBuildFileContents() []byte { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 427 | // TODO(cparsons): Map label to attribute programmatically; don't use hard-coded |
| 428 | // architecture mapping. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 429 | formatString := ` |
| 430 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 431 | load(":main.bzl", "mixed_build_root", "phony_root") |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 432 | |
| 433 | mixed_build_root(name = "buildroot", |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 434 | deps_x86_64 = [%s], |
| 435 | deps_x86 = [%s], |
| 436 | deps_arm64 = [%s], |
| 437 | deps_arm = [%s], |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 438 | ) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 439 | |
| 440 | phony_root(name = "phonyroot", |
| 441 | deps = [":buildroot"], |
| 442 | ) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 443 | ` |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 444 | var deps_x86_64 []string = nil |
| 445 | var deps_x86 []string = nil |
| 446 | var deps_arm64 []string = nil |
| 447 | var deps_arm []string = nil |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 448 | for val, _ := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 449 | labelString := fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label)) |
| 450 | switch getArchString(val) { |
| 451 | case "x86_64": |
| 452 | deps_x86_64 = append(deps_x86_64, labelString) |
| 453 | case "x86": |
| 454 | deps_x86 = append(deps_x86, labelString) |
| 455 | case "arm64": |
| 456 | deps_arm64 = append(deps_arm64, labelString) |
| 457 | case "arm": |
| 458 | deps_arm = append(deps_arm, labelString) |
| 459 | default: |
Liz Kammer | 15b04e2 | 2021-03-04 09:45:21 -0500 | [diff] [blame] | 460 | panic(fmt.Sprintf("unhandled architecture %s for %v", getArchString(val), val)) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 461 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 462 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 463 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 464 | return []byte(fmt.Sprintf(formatString, |
| 465 | strings.Join(deps_x86_64, ",\n "), |
| 466 | strings.Join(deps_x86, ",\n "), |
| 467 | strings.Join(deps_arm64, ",\n "), |
| 468 | strings.Join(deps_arm, ",\n "))) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 469 | } |
| 470 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 471 | func indent(original string) string { |
| 472 | result := "" |
| 473 | for _, line := range strings.Split(original, "\n") { |
| 474 | result += " " + line + "\n" |
| 475 | } |
| 476 | return result |
| 477 | } |
| 478 | |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 479 | // Returns the file contents of the buildroot.cquery file that should be used for the cquery |
| 480 | // expression in order to obtain information about buildroot and its dependencies. |
| 481 | // The contents of this file depend on the bazelContext's requests; requests are enumerated |
| 482 | // and grouped by their request type. The data retrieved for each label depends on its |
| 483 | // request type. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 484 | func (context *bazelContext) cqueryStarlarkFileContents() []byte { |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 485 | requestTypeToCqueryIdEntries := map[cquery.RequestType][]string{} |
| 486 | for val, _ := range context.requests { |
| 487 | cqueryId := getCqueryId(val) |
| 488 | mapEntryString := fmt.Sprintf("%q : True", cqueryId) |
| 489 | requestTypeToCqueryIdEntries[val.requestType] = |
| 490 | append(requestTypeToCqueryIdEntries[val.requestType], mapEntryString) |
| 491 | } |
| 492 | labelRegistrationMapSection := "" |
| 493 | functionDefSection := "" |
| 494 | mainSwitchSection := "" |
| 495 | |
| 496 | mapDeclarationFormatString := ` |
| 497 | %s = { |
| 498 | %s |
| 499 | } |
| 500 | ` |
| 501 | functionDefFormatString := ` |
| 502 | def %s(target): |
| 503 | %s |
| 504 | ` |
| 505 | mainSwitchSectionFormatString := ` |
| 506 | if id_string in %s: |
| 507 | return id_string + ">>" + %s(target) |
| 508 | ` |
| 509 | |
| 510 | for _, requestType := range cquery.RequestTypes { |
| 511 | labelMapName := requestType.Name() + "_Labels" |
| 512 | functionName := requestType.Name() + "_Fn" |
| 513 | labelRegistrationMapSection += fmt.Sprintf(mapDeclarationFormatString, |
| 514 | labelMapName, |
| 515 | strings.Join(requestTypeToCqueryIdEntries[requestType], ",\n ")) |
| 516 | functionDefSection += fmt.Sprintf(functionDefFormatString, |
| 517 | functionName, |
| 518 | indent(requestType.StarlarkFunctionBody())) |
| 519 | mainSwitchSection += fmt.Sprintf(mainSwitchSectionFormatString, |
| 520 | labelMapName, functionName) |
| 521 | } |
| 522 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 523 | formatString := ` |
| 524 | # This file is generated by soong_build. Do not edit. |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 525 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 526 | # Label Map Section |
| 527 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 528 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 529 | # Function Def Section |
| 530 | %s |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 531 | |
| 532 | def get_arch(target): |
| 533 | buildoptions = build_options(target) |
| 534 | platforms = build_options(target)["//command_line_option:platforms"] |
| 535 | if len(platforms) != 1: |
| 536 | # An individual configured target should have only one platform architecture. |
| 537 | # Note that it's fine for there to be multiple architectures for the same label, |
| 538 | # but each is its own configured target. |
| 539 | fail("expected exactly 1 platform for " + str(target.label) + " but got " + str(platforms)) |
| 540 | platform_name = build_options(target)["//command_line_option:platforms"][0].name |
| 541 | if platform_name == "host": |
| 542 | return "HOST" |
| 543 | elif not platform_name.startswith("generic_"): |
| 544 | fail("expected platform name of the form 'generic_<arch>', but was " + str(platforms)) |
| 545 | return "UNKNOWN" |
| 546 | return platform_name[len("generic_"):] |
| 547 | |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 548 | def format(target): |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 549 | id_string = str(target.label) + "|" + get_arch(target) |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 550 | |
| 551 | # Main switch section |
| 552 | %s |
| 553 | # This target was not requested via cquery, and thus must be a dependency |
| 554 | # of a requested target. |
| 555 | return id_string + ">>NONE" |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 556 | ` |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 557 | |
Chris Parsons | 944e7d0 | 2021-03-11 11:08:46 -0500 | [diff] [blame] | 558 | return []byte(fmt.Sprintf(formatString, labelRegistrationMapSection, functionDefSection, |
| 559 | mainSwitchSection)) |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 562 | // Returns a workspace-relative path containing build-related metadata required |
| 563 | // for interfacing with Bazel. Example: out/soong/bazel. |
| 564 | func (context *bazelContext) intermediatesDir() string { |
| 565 | return filepath.Join(context.buildDir, "bazel") |
| 566 | } |
| 567 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 568 | // Issues commands to Bazel to receive results for all cquery requests |
| 569 | // queued in the BazelContext. |
| 570 | func (context *bazelContext) InvokeBazel() error { |
| 571 | context.results = make(map[cqueryKey]string) |
| 572 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 573 | var cqueryOutput string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 574 | var cqueryErr string |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 575 | var err error |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 576 | |
Chris Parsons | 07c1e4a | 2021-01-19 17:19:16 -0500 | [diff] [blame] | 577 | intermediatesDirPath := absolutePath(context.intermediatesDir()) |
| 578 | if _, err := os.Stat(intermediatesDirPath); os.IsNotExist(err) { |
| 579 | err = os.Mkdir(intermediatesDirPath, 0777) |
| 580 | } |
| 581 | |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 582 | if err != nil { |
| 583 | return err |
| 584 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 585 | err = ioutil.WriteFile( |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 586 | absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 587 | context.mainBzlFileContents(), 0666) |
| 588 | if err != nil { |
| 589 | return err |
| 590 | } |
| 591 | err = ioutil.WriteFile( |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 592 | absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 593 | context.mainBuildFileContents(), 0666) |
| 594 | if err != nil { |
| 595 | return err |
| 596 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 597 | cqueryFileRelpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery") |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 598 | err = ioutil.WriteFile( |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 599 | absolutePath(cqueryFileRelpath), |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 600 | context.cqueryStarlarkFileContents(), 0666) |
| 601 | if err != nil { |
| 602 | return err |
| 603 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 604 | workspaceFileRelpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel") |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 605 | err = ioutil.WriteFile( |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 606 | absolutePath(workspaceFileRelpath), |
Chris Parsons | 8ccdb63 | 2020-11-17 15:41:01 -0500 | [diff] [blame] | 607 | context.workspaceFileContents(), 0666) |
| 608 | if err != nil { |
| 609 | return err |
| 610 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 611 | buildrootLabel := "//:buildroot" |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 612 | cqueryOutput, cqueryErr, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery", |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 613 | []string{fmt.Sprintf("kind(rule, deps(%s))", buildrootLabel)}, |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 614 | "--output=starlark", |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 615 | "--starlark:file="+cqueryFileRelpath) |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 616 | err = ioutil.WriteFile( |
| 617 | absolutePath(filepath.Join(context.intermediatesDir(), "cquery.out")), |
| 618 | []byte(cqueryOutput), 0666) |
| 619 | if err != nil { |
| 620 | return err |
| 621 | } |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 622 | |
| 623 | if err != nil { |
| 624 | return err |
| 625 | } |
| 626 | |
| 627 | cqueryResults := map[string]string{} |
| 628 | for _, outputLine := range strings.Split(cqueryOutput, "\n") { |
| 629 | if strings.Contains(outputLine, ">>") { |
| 630 | splitLine := strings.SplitN(outputLine, ">>", 2) |
| 631 | cqueryResults[splitLine[0]] = splitLine[1] |
| 632 | } |
| 633 | } |
| 634 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 635 | for val, _ := range context.requests { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 636 | if cqueryResult, ok := cqueryResults[getCqueryId(val)]; ok { |
Chris Parsons | b0f8ac4 | 2020-10-23 16:48:08 -0400 | [diff] [blame] | 637 | context.results[val] = string(cqueryResult) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 638 | } else { |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 639 | return fmt.Errorf("missing result for bazel target %s. query output: [%s], cquery err: [%s]", |
| 640 | getCqueryId(val), cqueryOutput, cqueryErr) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 644 | // Issue an aquery command to retrieve action information about the bazel build tree. |
| 645 | // |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 646 | // TODO(cparsons): Use --target_pattern_file to avoid command line limits. |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 647 | var aqueryOutput string |
Chris Parsons | 808d84c | 2021-03-09 20:43:32 -0500 | [diff] [blame] | 648 | aqueryOutput, _, err = context.issueBazelCommand(bazel.AqueryBuildRootRunName, "aquery", |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 649 | []string{fmt.Sprintf("deps(%s)", buildrootLabel), |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 650 | // Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's |
| 651 | // proto sources, which would add a number of unnecessary dependencies. |
| 652 | "--output=jsonproto"}) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 653 | |
| 654 | if err != nil { |
| 655 | return err |
| 656 | } |
| 657 | |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 658 | context.buildStatements, err = bazel.AqueryBuildStatements([]byte(aqueryOutput)) |
| 659 | if err != nil { |
| 660 | return err |
| 661 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 662 | |
| 663 | // Issue a build command of the phony root to generate symlink forests for dependencies of the |
| 664 | // Bazel build. This is necessary because aquery invocations do not generate this symlink forest, |
| 665 | // 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] | 666 | _, _, err = context.issueBazelCommand(bazel.BazelBuildPhonyRootRunName, "build", |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 667 | []string{"//:phonyroot"}) |
| 668 | |
| 669 | if err != nil { |
| 670 | return err |
| 671 | } |
| 672 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 673 | // Clear requests. |
| 674 | context.requests = map[cqueryKey]bool{} |
| 675 | return nil |
| 676 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 677 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 678 | func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement { |
| 679 | return context.buildStatements |
| 680 | } |
| 681 | |
| 682 | func (context *bazelContext) OutputBase() string { |
| 683 | return context.outputBase |
| 684 | } |
| 685 | |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 686 | // Singleton used for registering BUILD file ninja dependencies (needed |
| 687 | // for correctness of builds which use Bazel. |
| 688 | func BazelSingleton() Singleton { |
| 689 | return &bazelSingleton{} |
| 690 | } |
| 691 | |
| 692 | type bazelSingleton struct{} |
| 693 | |
| 694 | func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 695 | // bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled. |
| 696 | if !ctx.Config().BazelContext.BazelEnabled() { |
| 697 | return |
| 698 | } |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 699 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 700 | // Add ninja file dependencies for files which all bazel invocations require. |
| 701 | bazelBuildList := absolutePath(filepath.Join( |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 702 | filepath.Dir(bootstrap.CmdlineModuleListFile()), "bazel.list")) |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 703 | ctx.AddNinjaFileDeps(bazelBuildList) |
| 704 | |
| 705 | data, err := ioutil.ReadFile(bazelBuildList) |
| 706 | if err != nil { |
| 707 | ctx.Errorf(err.Error()) |
| 708 | } |
| 709 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 710 | for _, file := range files { |
| 711 | ctx.AddNinjaFileDeps(file) |
| 712 | } |
| 713 | |
| 714 | // Register bazel-owned build statements (obtained from the aquery invocation). |
| 715 | for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() { |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 716 | if len(buildStatement.Command) < 1 { |
| 717 | panic(fmt.Sprintf("unhandled build statement: %s", buildStatement)) |
| 718 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 719 | rule := NewRuleBuilder(pctx, ctx) |
| 720 | cmd := rule.Command() |
| 721 | cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s", |
| 722 | ctx.Config().BazelContext.OutputBase(), buildStatement.Command)) |
| 723 | |
| 724 | for _, outputPath := range buildStatement.OutputPaths { |
| 725 | cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath)) |
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 | for _, inputPath := range buildStatement.InputPaths { |
| 728 | cmd.Implicit(PathForBazelOut(ctx, inputPath)) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 729 | } |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 730 | |
| 731 | // This is required to silence warnings pertaining to unexpected timestamps. Particularly, |
| 732 | // some Bazel builtins (such as files in the bazel_tools directory) have far-future |
| 733 | // timestamps. Without restat, Ninja would emit warnings that the input files of a |
| 734 | // build statement have later timestamps than the outputs. |
| 735 | rule.Restat() |
| 736 | |
Liz Kammer | 13548d7 | 2020-12-16 11:13:30 -0800 | [diff] [blame] | 737 | rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic) |
Chris Parsons | a798d96 | 2020-10-12 23:44:08 -0400 | [diff] [blame] | 738 | } |
| 739 | } |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 740 | |
| 741 | func getCqueryId(key cqueryKey) string { |
| 742 | return canonicalizeLabel(key.label) + "|" + getArchString(key) |
| 743 | } |
| 744 | |
| 745 | func getArchString(key cqueryKey) string { |
| 746 | arch := key.archType.Name |
| 747 | if len(arch) > 0 { |
| 748 | return arch |
| 749 | } else { |
| 750 | return "x86_64" |
| 751 | } |
| 752 | } |