2020-07-15 12:06:41 +02: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 (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
)
|
|
|
|
|
2020-11-05 13:42:11 +01:00
|
|
|
// The Bazel QueryView singleton is responsible for generating the Ninja actions
|
2020-07-15 12:06:41 +02:00
|
|
|
// for calling the soong_build primary builder in the main build.ninja file.
|
|
|
|
func init() {
|
2020-11-09 14:22:25 +01:00
|
|
|
RegisterSingletonType("bazel_queryview", BazelQueryViewSingleton)
|
2020-07-15 12:06:41 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 10:34:15 +01:00
|
|
|
// BazelQueryViewSingleton is the singleton responsible for registering the
|
|
|
|
// soong_build build statement that will convert the Soong module graph after
|
|
|
|
// applying *all* mutators, enabing the feature to query the final state of the
|
|
|
|
// Soong graph. This mode is meant for querying the build graph state, and not meant
|
|
|
|
// for generating BUILD files to be checked in.
|
2020-11-05 13:42:11 +01:00
|
|
|
func BazelQueryViewSingleton() Singleton {
|
|
|
|
return &bazelQueryViewSingleton{}
|
2020-07-15 12:06:41 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 10:34:15 +01:00
|
|
|
// BazelConverterSingleton is the singleton responsible for registering the soong_build
|
|
|
|
// build statement that will convert the Soong module graph by applying an alternate
|
|
|
|
// pipeline of mutators, with the goal of reaching semantic equivalence between the original
|
|
|
|
// Blueprint and final BUILD files. Using this mode, the goal is to be able to
|
|
|
|
// build with these BUILD files directly in the source tree.
|
|
|
|
func BazelConverterSingleton() Singleton {
|
|
|
|
return &bazelConverterSingleton{}
|
|
|
|
}
|
|
|
|
|
2020-11-05 13:42:11 +01:00
|
|
|
type bazelQueryViewSingleton struct{}
|
2020-12-02 10:34:15 +01:00
|
|
|
type bazelConverterSingleton struct{}
|
|
|
|
|
|
|
|
func generateBuildActionsForBazelConversion(ctx SingletonContext, converterMode bool) {
|
|
|
|
name := "queryview"
|
|
|
|
descriptionTemplate := "[EXPERIMENTAL, PRE-PRODUCTION] Creating the Bazel QueryView workspace with %s at $outDir"
|
2020-07-15 12:06:41 +02:00
|
|
|
|
2020-11-05 13:42:11 +01:00
|
|
|
// Create a build and rule statement, using the Bazel QueryView's WORKSPACE
|
2020-07-15 12:06:41 +02:00
|
|
|
// file as the output file marker.
|
|
|
|
var deps Paths
|
|
|
|
moduleListFilePath := pathForBuildToolDep(ctx, ctx.Config().moduleListFile)
|
|
|
|
deps = append(deps, moduleListFilePath)
|
|
|
|
deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().ProductVariablesFileName))
|
|
|
|
|
2020-12-02 10:34:15 +01:00
|
|
|
bazelQueryViewDirectory := PathForOutput(ctx, name)
|
2020-11-05 13:42:11 +01:00
|
|
|
bazelQueryViewWorkspaceFile := bazelQueryViewDirectory.Join(ctx, "WORKSPACE")
|
2020-07-15 12:06:41 +02:00
|
|
|
primaryBuilder := primaryBuilderPath(ctx)
|
2020-11-05 13:42:11 +01:00
|
|
|
bazelQueryView := ctx.Rule(pctx, "bazelQueryView",
|
2020-07-15 12:06:41 +02:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: fmt.Sprintf(
|
2020-11-09 14:22:25 +01:00
|
|
|
"rm -rf ${outDir}/* && "+
|
2021-03-08 13:02:10 +01:00
|
|
|
"BUILDER=\"%s\" && "+
|
|
|
|
"cd $$(dirname \"$$BUILDER\") && "+
|
|
|
|
"ABSBUILDER=\"$$PWD/$$(basename \"$$BUILDER\")\" && "+
|
|
|
|
"cd / && "+
|
|
|
|
"env -i \"$$ABSBUILDER\" --bazel_queryview_dir ${outDir} \"%s\" && "+
|
2020-11-09 14:22:25 +01:00
|
|
|
"echo WORKSPACE: `cat %s` > ${outDir}/.queryview-depfile.d",
|
2020-07-15 12:06:41 +02:00
|
|
|
primaryBuilder.String(),
|
2021-03-08 13:02:10 +01:00
|
|
|
strings.Join(os.Args[1:], "\" \""),
|
2020-07-15 12:06:41 +02:00
|
|
|
moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile.
|
|
|
|
),
|
|
|
|
CommandDeps: []string{primaryBuilder.String()},
|
|
|
|
Description: fmt.Sprintf(
|
2020-12-02 10:34:15 +01:00
|
|
|
descriptionTemplate,
|
2020-07-15 12:06:41 +02:00
|
|
|
primaryBuilder.Base()),
|
|
|
|
Deps: blueprint.DepsGCC,
|
2020-11-05 13:42:11 +01:00
|
|
|
Depfile: "${outDir}/.queryview-depfile.d",
|
2020-07-15 12:06:41 +02:00
|
|
|
},
|
|
|
|
"outDir")
|
|
|
|
|
|
|
|
ctx.Build(pctx, BuildParams{
|
2020-11-05 13:42:11 +01:00
|
|
|
Rule: bazelQueryView,
|
|
|
|
Output: bazelQueryViewWorkspaceFile,
|
2020-07-15 12:06:41 +02:00
|
|
|
Inputs: deps,
|
|
|
|
Args: map[string]string{
|
2020-11-05 13:42:11 +01:00
|
|
|
"outDir": bazelQueryViewDirectory.String(),
|
2020-07-15 12:06:41 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-12-02 10:34:15 +01:00
|
|
|
// Add a phony target for generating the workspace
|
|
|
|
ctx.Phony(name, bazelQueryViewWorkspaceFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bazelQueryViewSingleton) GenerateBuildActions(ctx SingletonContext) {
|
|
|
|
generateBuildActionsForBazelConversion(ctx, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bazelConverterSingleton) GenerateBuildActions(ctx SingletonContext) {
|
|
|
|
generateBuildActionsForBazelConversion(ctx, true)
|
2020-07-15 12:06:41 +02:00
|
|
|
}
|