Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 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. |
| 27 | func init() { |
Jingwen Chen | b05d62f | 2020-11-09 08:22:25 -0500 | [diff] [blame] | 28 | RegisterSingletonType("bazel_queryview", BazelQueryViewSingleton) |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 29 | RegisterSingletonType("bazel_converter", BazelConverterSingleton) |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 32 | // BazelQueryViewSingleton is the singleton responsible for registering the |
| 33 | // soong_build build statement that will convert the Soong module graph after |
| 34 | // applying *all* mutators, enabing the feature to query the final state of the |
| 35 | // Soong graph. This mode is meant for querying the build graph state, and not meant |
| 36 | // for generating BUILD files to be checked in. |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 37 | func BazelQueryViewSingleton() Singleton { |
| 38 | return &bazelQueryViewSingleton{} |
| 39 | } |
| 40 | |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 41 | // BazelConverterSingleton is the singleton responsible for registering the soong_build |
| 42 | // build statement that will convert the Soong module graph by applying an alternate |
| 43 | // pipeline of mutators, with the goal of reaching semantic equivalence between the original |
| 44 | // Blueprint and final BUILD files. Using this mode, the goal is to be able to |
| 45 | // build with these BUILD files directly in the source tree. |
| 46 | func BazelConverterSingleton() Singleton { |
| 47 | return &bazelConverterSingleton{} |
| 48 | } |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 49 | |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 50 | type bazelQueryViewSingleton struct{} |
| 51 | type bazelConverterSingleton struct{} |
| 52 | |
| 53 | func generateBuildActionsForBazelConversion(ctx SingletonContext, converterMode bool) { |
| 54 | name := "queryview" |
| 55 | additionalEnvVars := "" |
| 56 | descriptionTemplate := "[EXPERIMENTAL, PRE-PRODUCTION] Creating the Bazel QueryView workspace with %s at $outDir" |
| 57 | if converterMode { |
| 58 | name = "bp2build" |
| 59 | additionalEnvVars = "CONVERT_TO_BAZEL=true" |
| 60 | descriptionTemplate = "[EXPERIMENTAL, PRE-PRODUCTION] Converting all Android.bp to Bazel BUILD files with %s at $outDir" |
| 61 | } |
| 62 | |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 63 | // Create a build and rule statement, using the Bazel QueryView's WORKSPACE |
| 64 | // file as the output file marker. |
| 65 | var deps Paths |
| 66 | moduleListFilePath := pathForBuildToolDep(ctx, ctx.Config().moduleListFile) |
| 67 | deps = append(deps, moduleListFilePath) |
| 68 | deps = append(deps, pathForBuildToolDep(ctx, ctx.Config().ProductVariablesFileName)) |
| 69 | |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 70 | bazelQueryViewDirectory := PathForOutput(ctx, name) |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 71 | bazelQueryViewWorkspaceFile := bazelQueryViewDirectory.Join(ctx, "WORKSPACE") |
| 72 | primaryBuilder := primaryBuilderPath(ctx) |
| 73 | bazelQueryView := ctx.Rule(pctx, "bazelQueryView", |
| 74 | blueprint.RuleParams{ |
| 75 | Command: fmt.Sprintf( |
Jingwen Chen | b05d62f | 2020-11-09 08:22:25 -0500 | [diff] [blame] | 76 | "rm -rf ${outDir}/* && "+ |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 77 | "%s %s --bazel_queryview_dir ${outDir} %s && "+ |
Jingwen Chen | b05d62f | 2020-11-09 08:22:25 -0500 | [diff] [blame] | 78 | "echo WORKSPACE: `cat %s` > ${outDir}/.queryview-depfile.d", |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 79 | additionalEnvVars, |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 80 | primaryBuilder.String(), |
| 81 | strings.Join(os.Args[1:], " "), |
| 82 | moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile. |
| 83 | ), |
| 84 | CommandDeps: []string{primaryBuilder.String()}, |
| 85 | Description: fmt.Sprintf( |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 86 | descriptionTemplate, |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 87 | primaryBuilder.Base()), |
| 88 | Deps: blueprint.DepsGCC, |
| 89 | Depfile: "${outDir}/.queryview-depfile.d", |
| 90 | }, |
| 91 | "outDir") |
| 92 | |
| 93 | ctx.Build(pctx, BuildParams{ |
| 94 | Rule: bazelQueryView, |
| 95 | Output: bazelQueryViewWorkspaceFile, |
| 96 | Inputs: deps, |
| 97 | Args: map[string]string{ |
| 98 | "outDir": bazelQueryViewDirectory.String(), |
| 99 | }, |
| 100 | }) |
| 101 | |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 102 | // Add a phony target for generating the workspace |
| 103 | ctx.Phony(name, bazelQueryViewWorkspaceFile) |
| 104 | } |
| 105 | |
| 106 | func (c *bazelQueryViewSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 107 | generateBuildActionsForBazelConversion(ctx, false) |
| 108 | } |
| 109 | |
| 110 | func (c *bazelConverterSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 111 | generateBuildActionsForBazelConversion(ctx, true) |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 112 | } |