blob: 970ae01161fe5d2b9ed2a08095f754513e5b4be6 [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() {
Jingwen Chenb05d62f2020-11-09 08:22:25 -050028 RegisterSingletonType("bazel_queryview", BazelQueryViewSingleton)
Jingwen Chen50f93d22020-11-05 07:42:11 -050029}
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(
Jingwen Chenb05d62f2020-11-09 08:22:25 -050051 "rm -rf ${outDir}/* && "+
52 "%s --bazel_queryview_dir ${outDir} %s && "+
53 "echo WORKSPACE: `cat %s` > ${outDir}/.queryview-depfile.d",
Jingwen Chen50f93d22020-11-05 07:42:11 -050054 primaryBuilder.String(),
55 strings.Join(os.Args[1:], " "),
56 moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile.
57 ),
58 CommandDeps: []string{primaryBuilder.String()},
59 Description: fmt.Sprintf(
Jingwen Chen478d0332020-11-13 10:04:17 -050060 "[EXPERIMENTAL, PRE-PRODUCTION] Creating the Bazel QueryView workspace with %s at $outDir",
Jingwen Chen50f93d22020-11-05 07:42:11 -050061 primaryBuilder.Base()),
62 Deps: blueprint.DepsGCC,
63 Depfile: "${outDir}/.queryview-depfile.d",
64 },
65 "outDir")
66
67 ctx.Build(pctx, BuildParams{
68 Rule: bazelQueryView,
69 Output: bazelQueryViewWorkspaceFile,
70 Inputs: deps,
71 Args: map[string]string{
72 "outDir": bazelQueryViewDirectory.String(),
73 },
74 })
75
76 // Add a phony target for building the Bazel QueryView
77 ctx.Phony("queryview", bazelQueryViewWorkspaceFile)
78}