Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [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 Overlay 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() { |
| 28 | RegisterSingletonType("bazel_overlay", BazelOverlaySingleton) |
| 29 | } |
| 30 | |
| 31 | func BazelOverlaySingleton() Singleton { |
| 32 | return &bazelOverlaySingleton{} |
| 33 | } |
| 34 | |
| 35 | type bazelOverlaySingleton struct{} |
| 36 | |
| 37 | func (c *bazelOverlaySingleton) GenerateBuildActions(ctx SingletonContext) { |
| 38 | // Create a build and rule statement, using the Bazel overlay'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 | bazelOverlayDirectory := PathForOutput(ctx, "bazel_overlay") |
| 46 | bazelOverlayWorkspaceFile := bazelOverlayDirectory.Join(ctx, "WORKSPACE") |
| 47 | primaryBuilder := primaryBuilderPath(ctx) |
| 48 | bazelOverlay := ctx.Rule(pctx, "bazelOverlay", |
| 49 | blueprint.RuleParams{ |
| 50 | Command: fmt.Sprintf( |
| 51 | "rm -rf ${outDir}/* && %s --bazel_overlay_dir ${outDir} %s && echo WORKSPACE: `cat %s` > ${outDir}/.overlay-depfile.d", |
| 52 | primaryBuilder.String(), |
| 53 | strings.Join(os.Args[1:], " "), |
| 54 | moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile. |
| 55 | ), |
| 56 | CommandDeps: []string{primaryBuilder.String()}, |
| 57 | Description: fmt.Sprintf( |
| 58 | "Creating the Bazel overlay workspace with %s at $outDir", |
| 59 | primaryBuilder.Base()), |
| 60 | Deps: blueprint.DepsGCC, |
| 61 | Depfile: "${outDir}/.overlay-depfile.d", |
| 62 | }, |
| 63 | "outDir") |
| 64 | |
| 65 | ctx.Build(pctx, BuildParams{ |
| 66 | Rule: bazelOverlay, |
| 67 | Output: bazelOverlayWorkspaceFile, |
| 68 | Inputs: deps, |
| 69 | Args: map[string]string{ |
| 70 | "outDir": bazelOverlayDirectory.String(), |
| 71 | }, |
| 72 | }) |
| 73 | |
| 74 | // Add a phony target for building the bazel overlay |
| 75 | ctx.Phony("bazel_overlay", bazelOverlayWorkspaceFile) |
| 76 | } |