blob: b940202e0b35bcc9a10a1ee687465edb9b46ba28 [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
Jingwen Chen4133ce62020-12-02 04:34:15 -050031// BazelQueryViewSingleton is the singleton responsible for registering the
32// soong_build build statement that will convert the Soong module graph after
33// applying *all* mutators, enabing the feature to query the final state of the
34// Soong graph. This mode is meant for querying the build graph state, and not meant
35// for generating BUILD files to be checked in.
Jingwen Chen50f93d22020-11-05 07:42:11 -050036func BazelQueryViewSingleton() Singleton {
37 return &bazelQueryViewSingleton{}
38}
39
Jingwen Chen4133ce62020-12-02 04:34:15 -050040// BazelConverterSingleton is the singleton responsible for registering the soong_build
41// build statement that will convert the Soong module graph by applying an alternate
42// pipeline of mutators, with the goal of reaching semantic equivalence between the original
43// Blueprint and final BUILD files. Using this mode, the goal is to be able to
44// build with these BUILD files directly in the source tree.
45func BazelConverterSingleton() Singleton {
46 return &bazelConverterSingleton{}
47}
Jingwen Chen50f93d22020-11-05 07:42:11 -050048
Jingwen Chen4133ce62020-12-02 04:34:15 -050049type bazelQueryViewSingleton struct{}
50type bazelConverterSingleton struct{}
51
52func generateBuildActionsForBazelConversion(ctx SingletonContext, converterMode bool) {
53 name := "queryview"
Jingwen Chen4133ce62020-12-02 04:34:15 -050054 descriptionTemplate := "[EXPERIMENTAL, PRE-PRODUCTION] Creating the Bazel QueryView workspace with %s at $outDir"
Jingwen Chen4133ce62020-12-02 04:34:15 -050055
Jingwen Chen50f93d22020-11-05 07:42:11 -050056 // Create a build and rule statement, using the Bazel QueryView's WORKSPACE
57 // file as the output file marker.
58 var deps Paths
59 moduleListFilePath := pathForBuildToolDep(ctx, ctx.Config().moduleListFile)
60 deps = append(deps, moduleListFilePath)
61 deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().ProductVariablesFileName))
62
Jingwen Chen4133ce62020-12-02 04:34:15 -050063 bazelQueryViewDirectory := PathForOutput(ctx, name)
Jingwen Chen50f93d22020-11-05 07:42:11 -050064 bazelQueryViewWorkspaceFile := bazelQueryViewDirectory.Join(ctx, "WORKSPACE")
65 primaryBuilder := primaryBuilderPath(ctx)
66 bazelQueryView := ctx.Rule(pctx, "bazelQueryView",
67 blueprint.RuleParams{
68 Command: fmt.Sprintf(
Jingwen Chenb05d62f2020-11-09 08:22:25 -050069 "rm -rf ${outDir}/* && "+
Lukacs T. Berki89071b02021-03-08 13:02:10 +010070 "BUILDER=\"%s\" && "+
71 "cd $$(dirname \"$$BUILDER\") && "+
72 "ABSBUILDER=\"$$PWD/$$(basename \"$$BUILDER\")\" && "+
73 "cd / && "+
74 "env -i \"$$ABSBUILDER\" --bazel_queryview_dir ${outDir} \"%s\" && "+
Jingwen Chenb05d62f2020-11-09 08:22:25 -050075 "echo WORKSPACE: `cat %s` > ${outDir}/.queryview-depfile.d",
Jingwen Chen50f93d22020-11-05 07:42:11 -050076 primaryBuilder.String(),
Lukacs T. Berki89071b02021-03-08 13:02:10 +010077 strings.Join(os.Args[1:], "\" \""),
Jingwen Chen50f93d22020-11-05 07:42:11 -050078 moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile.
79 ),
80 CommandDeps: []string{primaryBuilder.String()},
81 Description: fmt.Sprintf(
Jingwen Chen4133ce62020-12-02 04:34:15 -050082 descriptionTemplate,
Jingwen Chen50f93d22020-11-05 07:42:11 -050083 primaryBuilder.Base()),
84 Deps: blueprint.DepsGCC,
85 Depfile: "${outDir}/.queryview-depfile.d",
86 },
87 "outDir")
88
89 ctx.Build(pctx, BuildParams{
90 Rule: bazelQueryView,
91 Output: bazelQueryViewWorkspaceFile,
92 Inputs: deps,
93 Args: map[string]string{
94 "outDir": bazelQueryViewDirectory.String(),
95 },
96 })
97
Jingwen Chen4133ce62020-12-02 04:34:15 -050098 // Add a phony target for generating the workspace
99 ctx.Phony(name, bazelQueryViewWorkspaceFile)
100}
101
102func (c *bazelQueryViewSingleton) GenerateBuildActions(ctx SingletonContext) {
103 generateBuildActionsForBazelConversion(ctx, false)
104}
105
106func (c *bazelConverterSingleton) GenerateBuildActions(ctx SingletonContext) {
107 generateBuildActionsForBazelConversion(ctx, true)
Jingwen Chen50f93d22020-11-05 07:42:11 -0500108}