blob: dff00f6868f7f143089e8f783316a7019bdb3622 [file] [log] [blame]
Jingwen Chen50f93d22020-11-05 07:42:11 -05001// 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 "fmt"
19 "os"
20 "strings"
21
22 "github.com/google/blueprint"
23)
24
25// The Bazel QueryView singleton is responsible for generating the Ninja actions
26// for calling the soong_build primary builder in the main build.ninja file.
27func init() {
28 RegisterSingletonType("bazel_queryView", BazelQueryViewSingleton)
29}
30
31func BazelQueryViewSingleton() Singleton {
32 return &bazelQueryViewSingleton{}
33}
34
35type bazelQueryViewSingleton struct{}
36
37func (c *bazelQueryViewSingleton) GenerateBuildActions(ctx SingletonContext) {
38 // Create a build and rule statement, using the Bazel QueryView's WORKSPACE
39 // file as the output file marker.
40 var deps Paths
41 moduleListFilePath := pathForBuildToolDep(ctx, ctx.Config().moduleListFile)
42 deps = append(deps, moduleListFilePath)
43 deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().ProductVariablesFileName))
44
45 bazelQueryViewDirectory := PathForOutput(ctx, "queryview")
46 bazelQueryViewWorkspaceFile := bazelQueryViewDirectory.Join(ctx, "WORKSPACE")
47 primaryBuilder := primaryBuilderPath(ctx)
48 bazelQueryView := ctx.Rule(pctx, "bazelQueryView",
49 blueprint.RuleParams{
50 Command: fmt.Sprintf(
51 "rm -rf ${outDir}/* && %s --bazel_queryview_dir ${outDir} %s && echo WORKSPACE: `cat %s` > ${outDir}/.queryview-depfile.d",
52 primaryBuilder.String(),
53 strings.Join(os.Args[1:], " "),
54 moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile.
55 ),
56 CommandDeps: []string{primaryBuilder.String()},
57 Description: fmt.Sprintf(
58 "Creating the Bazel QueryView workspace with %s at $outDir",
59 primaryBuilder.Base()),
60 Deps: blueprint.DepsGCC,
61 Depfile: "${outDir}/.queryview-depfile.d",
62 },
63 "outDir")
64
65 ctx.Build(pctx, BuildParams{
66 Rule: bazelQueryView,
67 Output: bazelQueryViewWorkspaceFile,
68 Inputs: deps,
69 Args: map[string]string{
70 "outDir": bazelQueryViewDirectory.String(),
71 },
72 })
73
74 // Add a phony target for building the Bazel QueryView
75 ctx.Phony("queryview", bazelQueryViewWorkspaceFile)
76}