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