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 | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | func BazelQueryViewSingleton() Singleton { |
| 32 | return &bazelQueryViewSingleton{} |
| 33 | } |
| 34 | |
| 35 | type bazelQueryViewSingleton struct{} |
| 36 | |
| 37 | func (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 Chen | b05d62f | 2020-11-09 08:22:25 -0500 | [diff] [blame] | 51 | "rm -rf ${outDir}/* && "+ |
| 52 | "%s --bazel_queryview_dir ${outDir} %s && "+ |
| 53 | "echo WORKSPACE: `cat %s` > ${outDir}/.queryview-depfile.d", |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 54 | 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 Chen | 478d033 | 2020-11-13 10:04:17 -0500 | [diff] [blame] | 60 | "[EXPERIMENTAL, PRE-PRODUCTION] Creating the Bazel QueryView workspace with %s at $outDir", |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 61 | 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 | } |