2020-09-29 14:23:17 +08:00
|
|
|
// Copyright 2020 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-13 11:44:08 +08:00
|
|
|
"io/ioutil"
|
2020-09-29 14:23:17 +08:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-10-13 11:44:08 +08:00
|
|
|
"path/filepath"
|
2020-09-29 14:23:17 +08:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-10-13 11:44:08 +08:00
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
"github.com/google/blueprint/bootstrap"
|
|
|
|
|
2020-12-12 14:24:26 +08:00
|
|
|
"android/soong/bazel"
|
|
|
|
"android/soong/shared"
|
2020-09-29 14:23:17 +08:00
|
|
|
)
|
|
|
|
|
2020-10-24 04:48:08 +08:00
|
|
|
type CqueryRequestType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
getAllFiles CqueryRequestType = iota
|
|
|
|
)
|
|
|
|
|
2020-09-29 14:23:17 +08:00
|
|
|
// Map key to describe bazel cquery requests.
|
|
|
|
type cqueryKey struct {
|
2020-10-24 04:48:08 +08:00
|
|
|
label string
|
|
|
|
requestType CqueryRequestType
|
2020-09-29 14:23:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type BazelContext interface {
|
|
|
|
// The below methods involve queuing cquery requests to be later invoked
|
|
|
|
// by bazel. If any of these methods return (_, false), then the request
|
|
|
|
// has been queued to be run later.
|
|
|
|
|
|
|
|
// Returns result files built by building the given bazel target label.
|
|
|
|
GetAllFiles(label string) ([]string, bool)
|
|
|
|
|
|
|
|
// TODO(cparsons): Other cquery-related methods should be added here.
|
|
|
|
// ** End cquery methods
|
|
|
|
|
|
|
|
// Issues commands to Bazel to receive results for all cquery requests
|
|
|
|
// queued in the BazelContext.
|
|
|
|
InvokeBazel() error
|
|
|
|
|
|
|
|
// Returns true if bazel is enabled for the given configuration.
|
|
|
|
BazelEnabled() bool
|
2020-12-11 06:19:18 +08:00
|
|
|
|
|
|
|
// Returns the bazel output base (the root directory for all bazel intermediate outputs).
|
|
|
|
OutputBase() string
|
|
|
|
|
|
|
|
// Returns build statements which should get registered to reflect Bazel's outputs.
|
|
|
|
BuildStatementsToRegister() []bazel.BuildStatement
|
2020-09-29 14:23:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// A context object which tracks queued requests that need to be made to Bazel,
|
|
|
|
// and their results after the requests have been made.
|
|
|
|
type bazelContext struct {
|
|
|
|
homeDir string
|
|
|
|
bazelPath string
|
|
|
|
outputBase string
|
|
|
|
workspaceDir string
|
2020-10-24 04:48:08 +08:00
|
|
|
buildDir string
|
2020-12-12 14:24:26 +08:00
|
|
|
metricsDir string
|
2020-09-29 14:23:17 +08:00
|
|
|
|
|
|
|
requests map[cqueryKey]bool // cquery requests that have not yet been issued to Bazel
|
|
|
|
requestMutex sync.Mutex // requests can be written in parallel
|
|
|
|
|
|
|
|
results map[cqueryKey]string // Results of cquery requests after Bazel invocations
|
2020-12-11 06:19:18 +08:00
|
|
|
|
|
|
|
// Build statements which should get registered to reflect Bazel's outputs.
|
|
|
|
buildStatements []bazel.BuildStatement
|
2020-09-29 14:23:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ BazelContext = &bazelContext{}
|
|
|
|
|
|
|
|
// A bazel context to use when Bazel is disabled.
|
|
|
|
type noopBazelContext struct{}
|
|
|
|
|
|
|
|
var _ BazelContext = noopBazelContext{}
|
|
|
|
|
|
|
|
// A bazel context to use for tests.
|
|
|
|
type MockBazelContext struct {
|
|
|
|
AllFiles map[string][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MockBazelContext) GetAllFiles(label string) ([]string, bool) {
|
|
|
|
result, ok := m.AllFiles[label]
|
|
|
|
return result, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MockBazelContext) InvokeBazel() error {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MockBazelContext) BazelEnabled() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
func (m MockBazelContext) OutputBase() string {
|
|
|
|
return "outputbase"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MockBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
|
|
|
|
return []bazel.BuildStatement{}
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:23:17 +08:00
|
|
|
var _ BazelContext = MockBazelContext{}
|
|
|
|
|
|
|
|
func (bazelCtx *bazelContext) GetAllFiles(label string) ([]string, bool) {
|
2020-10-24 04:48:08 +08:00
|
|
|
result, ok := bazelCtx.cquery(label, getAllFiles)
|
2020-09-29 14:23:17 +08:00
|
|
|
if ok {
|
|
|
|
bazelOutput := strings.TrimSpace(result)
|
|
|
|
return strings.Split(bazelOutput, ", "), true
|
|
|
|
} else {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n noopBazelContext) GetAllFiles(label string) ([]string, bool) {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n noopBazelContext) InvokeBazel() error {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
func (m noopBazelContext) OutputBase() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:23:17 +08:00
|
|
|
func (n noopBazelContext) BazelEnabled() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
func (m noopBazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
|
|
|
|
return []bazel.BuildStatement{}
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:23:17 +08:00
|
|
|
func NewBazelContext(c *config) (BazelContext, error) {
|
2020-10-28 06:59:25 +08:00
|
|
|
// TODO(cparsons): Assess USE_BAZEL=1 instead once "mixed Soong/Bazel builds"
|
|
|
|
// are production ready.
|
|
|
|
if c.Getenv("USE_BAZEL_ANALYSIS") != "1" {
|
2020-09-29 14:23:17 +08:00
|
|
|
return noopBazelContext{}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-24 04:48:08 +08:00
|
|
|
bazelCtx := bazelContext{buildDir: c.buildDir, requests: make(map[cqueryKey]bool)}
|
2020-09-29 14:23:17 +08:00
|
|
|
missingEnvVars := []string{}
|
|
|
|
if len(c.Getenv("BAZEL_HOME")) > 1 {
|
|
|
|
bazelCtx.homeDir = c.Getenv("BAZEL_HOME")
|
|
|
|
} else {
|
|
|
|
missingEnvVars = append(missingEnvVars, "BAZEL_HOME")
|
|
|
|
}
|
|
|
|
if len(c.Getenv("BAZEL_PATH")) > 1 {
|
|
|
|
bazelCtx.bazelPath = c.Getenv("BAZEL_PATH")
|
|
|
|
} else {
|
|
|
|
missingEnvVars = append(missingEnvVars, "BAZEL_PATH")
|
|
|
|
}
|
|
|
|
if len(c.Getenv("BAZEL_OUTPUT_BASE")) > 1 {
|
|
|
|
bazelCtx.outputBase = c.Getenv("BAZEL_OUTPUT_BASE")
|
|
|
|
} else {
|
|
|
|
missingEnvVars = append(missingEnvVars, "BAZEL_OUTPUT_BASE")
|
|
|
|
}
|
|
|
|
if len(c.Getenv("BAZEL_WORKSPACE")) > 1 {
|
|
|
|
bazelCtx.workspaceDir = c.Getenv("BAZEL_WORKSPACE")
|
|
|
|
} else {
|
|
|
|
missingEnvVars = append(missingEnvVars, "BAZEL_WORKSPACE")
|
|
|
|
}
|
2020-12-12 14:24:26 +08:00
|
|
|
if len(c.Getenv("BAZEL_METRICS_DIR")) > 1 {
|
|
|
|
bazelCtx.metricsDir = c.Getenv("BAZEL_METRICS_DIR")
|
|
|
|
} else {
|
|
|
|
missingEnvVars = append(missingEnvVars, "BAZEL_METRICS_DIR")
|
|
|
|
}
|
2020-09-29 14:23:17 +08:00
|
|
|
if len(missingEnvVars) > 0 {
|
|
|
|
return nil, errors.New(fmt.Sprintf("missing required env vars to use bazel: %s", missingEnvVars))
|
|
|
|
} else {
|
|
|
|
return &bazelCtx, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:24:26 +08:00
|
|
|
func (context *bazelContext) BazelMetricsDir() string {
|
|
|
|
return context.metricsDir
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:23:17 +08:00
|
|
|
func (context *bazelContext) BazelEnabled() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a cquery request to the Bazel request queue, to be later invoked, or
|
|
|
|
// returns the result of the given request if the request was already made.
|
|
|
|
// If the given request was already made (and the results are available), then
|
|
|
|
// returns (result, true). If the request is queued but no results are available,
|
|
|
|
// then returns ("", false).
|
2020-10-24 04:48:08 +08:00
|
|
|
func (context *bazelContext) cquery(label string, requestType CqueryRequestType) (string, bool) {
|
|
|
|
key := cqueryKey{label, requestType}
|
2020-09-29 14:23:17 +08:00
|
|
|
if result, ok := context.results[key]; ok {
|
|
|
|
return result, true
|
|
|
|
} else {
|
|
|
|
context.requestMutex.Lock()
|
|
|
|
defer context.requestMutex.Unlock()
|
|
|
|
context.requests[key] = true
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pwdPrefix() string {
|
|
|
|
// Darwin doesn't have /proc
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
return "PWD=/proc/self/cwd"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:24:26 +08:00
|
|
|
func (context *bazelContext) issueBazelCommand(runName bazel.RunName, command string, labels []string,
|
2020-09-29 14:23:17 +08:00
|
|
|
extraFlags ...string) (string, error) {
|
|
|
|
|
2020-11-02 15:56:20 +08:00
|
|
|
cmdFlags := []string{"--output_base=" + context.outputBase, command}
|
2020-09-29 14:23:17 +08:00
|
|
|
cmdFlags = append(cmdFlags, labels...)
|
2020-11-18 04:41:01 +08:00
|
|
|
cmdFlags = append(cmdFlags, "--package_path=%workspace%/"+context.intermediatesDir())
|
2020-12-12 14:24:26 +08:00
|
|
|
cmdFlags = append(cmdFlags, "--profile="+shared.BazelMetricsFilename(context, runName))
|
2020-09-29 14:23:17 +08:00
|
|
|
cmdFlags = append(cmdFlags, extraFlags...)
|
|
|
|
|
|
|
|
bazelCmd := exec.Command(context.bazelPath, cmdFlags...)
|
|
|
|
bazelCmd.Dir = context.workspaceDir
|
|
|
|
bazelCmd.Env = append(os.Environ(), "HOME="+context.homeDir, pwdPrefix())
|
|
|
|
|
2020-10-10 10:24:15 +08:00
|
|
|
stderr := &bytes.Buffer{}
|
|
|
|
bazelCmd.Stderr = stderr
|
2020-09-29 14:23:17 +08:00
|
|
|
|
|
|
|
if output, err := bazelCmd.Output(); err != nil {
|
|
|
|
return "", fmt.Errorf("bazel command failed. command: [%s], error [%s]", bazelCmd, stderr)
|
|
|
|
} else {
|
|
|
|
return string(output), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-18 04:41:01 +08:00
|
|
|
// Returns the string contents of a workspace file that should be output
|
|
|
|
// adjacent to the main bzl file and build file.
|
|
|
|
// This workspace file allows, via local_repository rule, sourcetree-level
|
|
|
|
// BUILD targets to be referenced via @sourceroot.
|
|
|
|
func (context *bazelContext) workspaceFileContents() []byte {
|
|
|
|
formatString := `
|
|
|
|
# This file is generated by soong_build. Do not edit.
|
|
|
|
local_repository(
|
|
|
|
name = "sourceroot",
|
|
|
|
path = "%s",
|
|
|
|
)
|
|
|
|
`
|
|
|
|
return []byte(fmt.Sprintf(formatString, context.workspaceDir))
|
|
|
|
}
|
|
|
|
|
2020-10-24 04:48:08 +08:00
|
|
|
func (context *bazelContext) mainBzlFileContents() []byte {
|
|
|
|
contents := `
|
2020-12-11 06:19:18 +08:00
|
|
|
#####################################################
|
2020-10-24 04:48:08 +08:00
|
|
|
# This file is generated by soong_build. Do not edit.
|
2020-12-11 06:19:18 +08:00
|
|
|
#####################################################
|
|
|
|
|
2020-10-24 04:48:08 +08:00
|
|
|
def _mixed_build_root_impl(ctx):
|
|
|
|
return [DefaultInfo(files = depset(ctx.files.deps))]
|
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
# Rule representing the root of the build, to depend on all Bazel targets that
|
|
|
|
# are required for the build. Building this target will build the entire Bazel
|
|
|
|
# build tree.
|
2020-10-24 04:48:08 +08:00
|
|
|
mixed_build_root = rule(
|
|
|
|
implementation = _mixed_build_root_impl,
|
|
|
|
attrs = {"deps" : attr.label_list()},
|
|
|
|
)
|
2020-12-11 06:19:18 +08:00
|
|
|
|
|
|
|
def _phony_root_impl(ctx):
|
|
|
|
return []
|
|
|
|
|
|
|
|
# Rule to depend on other targets but build nothing.
|
|
|
|
# This is useful as follows: building a target of this rule will generate
|
|
|
|
# symlink forests for all dependencies of the target, without executing any
|
|
|
|
# actions of the build.
|
|
|
|
phony_root = rule(
|
|
|
|
implementation = _phony_root_impl,
|
|
|
|
attrs = {"deps" : attr.label_list()},
|
|
|
|
)
|
2020-10-24 04:48:08 +08:00
|
|
|
`
|
|
|
|
return []byte(contents)
|
|
|
|
}
|
|
|
|
|
2020-11-18 04:41:01 +08:00
|
|
|
// Returns a "canonicalized" corresponding to the given sourcetree-level label.
|
|
|
|
// This abstraction is required because a sourcetree label such as //foo/bar:baz
|
|
|
|
// must be referenced via the local repository prefix, such as
|
|
|
|
// @sourceroot//foo/bar:baz.
|
|
|
|
func canonicalizeLabel(label string) string {
|
|
|
|
if strings.HasPrefix(label, "//") {
|
|
|
|
return "@sourceroot" + label
|
|
|
|
} else {
|
|
|
|
return "@sourceroot//" + label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 04:48:08 +08:00
|
|
|
func (context *bazelContext) mainBuildFileContents() []byte {
|
|
|
|
formatString := `
|
|
|
|
# This file is generated by soong_build. Do not edit.
|
2020-12-11 06:19:18 +08:00
|
|
|
load(":main.bzl", "mixed_build_root", "phony_root")
|
2020-10-24 04:48:08 +08:00
|
|
|
|
|
|
|
mixed_build_root(name = "buildroot",
|
|
|
|
deps = [%s],
|
|
|
|
)
|
2020-12-11 06:19:18 +08:00
|
|
|
|
|
|
|
phony_root(name = "phonyroot",
|
|
|
|
deps = [":buildroot"],
|
|
|
|
)
|
2020-10-24 04:48:08 +08:00
|
|
|
`
|
|
|
|
var buildRootDeps []string = nil
|
|
|
|
for val, _ := range context.requests {
|
2020-11-18 04:41:01 +08:00
|
|
|
buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\"", canonicalizeLabel(val.label)))
|
2020-10-24 04:48:08 +08:00
|
|
|
}
|
|
|
|
buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
|
|
|
|
|
|
|
|
return []byte(fmt.Sprintf(formatString, buildRootDepsString))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *bazelContext) cqueryStarlarkFileContents() []byte {
|
|
|
|
formatString := `
|
|
|
|
# This file is generated by soong_build. Do not edit.
|
|
|
|
getAllFilesLabels = {
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
|
|
|
|
def format(target):
|
|
|
|
if str(target.label) in getAllFilesLabels:
|
|
|
|
return str(target.label) + ">>" + ', '.join([f.path for f in target.files.to_list()])
|
|
|
|
else:
|
|
|
|
# This target was not requested via cquery, and thus must be a dependency
|
|
|
|
# of a requested target.
|
|
|
|
return ""
|
|
|
|
`
|
|
|
|
var buildRootDeps []string = nil
|
|
|
|
// TODO(cparsons): Sort by request type instead of assuming all requests
|
|
|
|
// are of GetAllFiles type.
|
|
|
|
for val, _ := range context.requests {
|
2020-11-18 04:41:01 +08:00
|
|
|
buildRootDeps = append(buildRootDeps, fmt.Sprintf("\"%s\" : True", canonicalizeLabel(val.label)))
|
2020-10-24 04:48:08 +08:00
|
|
|
}
|
|
|
|
buildRootDepsString := strings.Join(buildRootDeps, ",\n ")
|
|
|
|
|
|
|
|
return []byte(fmt.Sprintf(formatString, buildRootDepsString))
|
|
|
|
}
|
|
|
|
|
2020-11-18 04:41:01 +08:00
|
|
|
// Returns a workspace-relative path containing build-related metadata required
|
|
|
|
// for interfacing with Bazel. Example: out/soong/bazel.
|
|
|
|
func (context *bazelContext) intermediatesDir() string {
|
|
|
|
return filepath.Join(context.buildDir, "bazel")
|
|
|
|
}
|
|
|
|
|
2020-09-29 14:23:17 +08:00
|
|
|
// Issues commands to Bazel to receive results for all cquery requests
|
|
|
|
// queued in the BazelContext.
|
|
|
|
func (context *bazelContext) InvokeBazel() error {
|
|
|
|
context.results = make(map[cqueryKey]string)
|
|
|
|
|
|
|
|
var cqueryOutput string
|
|
|
|
var err error
|
2020-11-18 04:41:01 +08:00
|
|
|
|
|
|
|
err = os.Mkdir(absolutePath(context.intermediatesDir()), 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-24 04:48:08 +08:00
|
|
|
err = ioutil.WriteFile(
|
2020-11-18 04:41:01 +08:00
|
|
|
absolutePath(filepath.Join(context.intermediatesDir(), "main.bzl")),
|
2020-10-24 04:48:08 +08:00
|
|
|
context.mainBzlFileContents(), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(
|
2020-11-18 04:41:01 +08:00
|
|
|
absolutePath(filepath.Join(context.intermediatesDir(), "BUILD.bazel")),
|
2020-10-24 04:48:08 +08:00
|
|
|
context.mainBuildFileContents(), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-22 01:11:10 +08:00
|
|
|
cqueryFileRelpath := filepath.Join(context.intermediatesDir(), "buildroot.cquery")
|
2020-10-24 04:48:08 +08:00
|
|
|
err = ioutil.WriteFile(
|
2020-12-22 01:11:10 +08:00
|
|
|
absolutePath(cqueryFileRelpath),
|
2020-10-24 04:48:08 +08:00
|
|
|
context.cqueryStarlarkFileContents(), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-22 01:11:10 +08:00
|
|
|
workspaceFileRelpath := filepath.Join(context.intermediatesDir(), "WORKSPACE.bazel")
|
2020-11-18 04:41:01 +08:00
|
|
|
err = ioutil.WriteFile(
|
2020-12-22 01:11:10 +08:00
|
|
|
absolutePath(workspaceFileRelpath),
|
2020-11-18 04:41:01 +08:00
|
|
|
context.workspaceFileContents(), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-22 01:11:10 +08:00
|
|
|
buildrootLabel := "//:buildroot"
|
2020-12-12 14:24:26 +08:00
|
|
|
cqueryOutput, err = context.issueBazelCommand(bazel.CqueryBuildRootRunName, "cquery",
|
2020-12-22 01:11:10 +08:00
|
|
|
[]string{fmt.Sprintf("deps(%s)", buildrootLabel)},
|
2020-10-24 04:48:08 +08:00
|
|
|
"--output=starlark",
|
2020-12-22 01:11:10 +08:00
|
|
|
"--starlark:file="+cqueryFileRelpath)
|
2020-09-29 14:23:17 +08:00
|
|
|
|
2020-10-24 04:48:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-29 14:23:17 +08:00
|
|
|
|
2020-10-24 04:48:08 +08:00
|
|
|
cqueryResults := map[string]string{}
|
|
|
|
for _, outputLine := range strings.Split(cqueryOutput, "\n") {
|
|
|
|
if strings.Contains(outputLine, ">>") {
|
|
|
|
splitLine := strings.SplitN(outputLine, ">>", 2)
|
|
|
|
cqueryResults[splitLine[0]] = splitLine[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for val, _ := range context.requests {
|
2020-11-18 04:41:01 +08:00
|
|
|
if cqueryResult, ok := cqueryResults[canonicalizeLabel(val.label)]; ok {
|
2020-10-24 04:48:08 +08:00
|
|
|
context.results[val] = string(cqueryResult)
|
2020-09-29 14:23:17 +08:00
|
|
|
} else {
|
2020-10-24 04:48:08 +08:00
|
|
|
return fmt.Errorf("missing result for bazel target %s", val.label)
|
2020-09-29 14:23:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
// Issue an aquery command to retrieve action information about the bazel build tree.
|
|
|
|
//
|
2020-09-29 14:23:17 +08:00
|
|
|
// TODO(cparsons): Use --target_pattern_file to avoid command line limits.
|
2020-12-11 06:19:18 +08:00
|
|
|
var aqueryOutput string
|
|
|
|
aqueryOutput, err = context.issueBazelCommand(bazel.AqueryBuildRootRunName, "aquery",
|
2020-12-22 01:11:10 +08:00
|
|
|
[]string{fmt.Sprintf("deps(%s)", buildrootLabel),
|
2020-12-11 06:19:18 +08:00
|
|
|
// Use jsonproto instead of proto; actual proto parsing would require a dependency on Bazel's
|
|
|
|
// proto sources, which would add a number of unnecessary dependencies.
|
|
|
|
"--output=jsonproto"})
|
2020-09-29 14:23:17 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
context.buildStatements = bazel.AqueryBuildStatements([]byte(aqueryOutput))
|
|
|
|
|
|
|
|
// Issue a build command of the phony root to generate symlink forests for dependencies of the
|
|
|
|
// Bazel build. This is necessary because aquery invocations do not generate this symlink forest,
|
|
|
|
// but some of symlinks may be required to resolve source dependencies of the build.
|
|
|
|
_, err = context.issueBazelCommand(bazel.BazelBuildPhonyRootRunName, "build",
|
|
|
|
[]string{"//:phonyroot"})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Build statements %s", context.buildStatements)
|
2020-09-29 14:23:17 +08:00
|
|
|
// Clear requests.
|
|
|
|
context.requests = map[cqueryKey]bool{}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-13 11:44:08 +08:00
|
|
|
|
2020-12-11 06:19:18 +08:00
|
|
|
func (context *bazelContext) BuildStatementsToRegister() []bazel.BuildStatement {
|
|
|
|
return context.buildStatements
|
|
|
|
}
|
|
|
|
|
|
|
|
func (context *bazelContext) OutputBase() string {
|
|
|
|
return context.outputBase
|
|
|
|
}
|
|
|
|
|
2020-10-13 11:44:08 +08:00
|
|
|
// Singleton used for registering BUILD file ninja dependencies (needed
|
|
|
|
// for correctness of builds which use Bazel.
|
|
|
|
func BazelSingleton() Singleton {
|
|
|
|
return &bazelSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bazelSingleton struct{}
|
|
|
|
|
|
|
|
func (c *bazelSingleton) GenerateBuildActions(ctx SingletonContext) {
|
2020-12-11 06:19:18 +08:00
|
|
|
// bazelSingleton is a no-op if mixed-soong-bazel-builds are disabled.
|
|
|
|
if !ctx.Config().BazelContext.BazelEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add ninja file dependencies for files which all bazel invocations require.
|
|
|
|
bazelBuildList := absolutePath(filepath.Join(
|
|
|
|
filepath.Dir(bootstrap.ModuleListFile), "bazel.list"))
|
|
|
|
ctx.AddNinjaFileDeps(bazelBuildList)
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(bazelBuildList)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
files := strings.Split(strings.TrimSpace(string(data)), "\n")
|
|
|
|
for _, file := range files {
|
|
|
|
ctx.AddNinjaFileDeps(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register bazel-owned build statements (obtained from the aquery invocation).
|
|
|
|
for index, buildStatement := range ctx.Config().BazelContext.BuildStatementsToRegister() {
|
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
|
|
|
cmd := rule.Command()
|
|
|
|
cmd.Text(fmt.Sprintf("cd %s/execroot/__main__ && %s",
|
|
|
|
ctx.Config().BazelContext.OutputBase(), buildStatement.Command))
|
|
|
|
|
|
|
|
for _, outputPath := range buildStatement.OutputPaths {
|
|
|
|
cmd.ImplicitOutput(PathForBazelOut(ctx, outputPath))
|
2020-10-13 11:44:08 +08:00
|
|
|
}
|
2020-12-11 06:19:18 +08:00
|
|
|
for _, inputPath := range buildStatement.InputPaths {
|
|
|
|
cmd.Implicit(PathForBazelOut(ctx, inputPath))
|
2020-10-13 11:44:08 +08:00
|
|
|
}
|
2020-12-11 06:19:18 +08:00
|
|
|
|
|
|
|
// This is required to silence warnings pertaining to unexpected timestamps. Particularly,
|
|
|
|
// some Bazel builtins (such as files in the bazel_tools directory) have far-future
|
|
|
|
// timestamps. Without restat, Ninja would emit warnings that the input files of a
|
|
|
|
// build statement have later timestamps than the outputs.
|
|
|
|
rule.Restat()
|
|
|
|
|
2020-12-17 03:13:30 +08:00
|
|
|
rule.Build(fmt.Sprintf("bazel %d", index), buildStatement.Mnemonic)
|
2020-10-13 11:44:08 +08:00
|
|
|
}
|
|
|
|
}
|