Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [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 bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Liz Kammer | 09f947d | 2021-05-12 14:51:49 -0400 | [diff] [blame] | 19 | "android/soong/bazel" |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 20 | "fmt" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 21 | "os" |
| 22 | ) |
| 23 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 24 | // Codegen is the backend of bp2build. The code generator is responsible for |
| 25 | // writing .bzl files that are equivalent to Android.bp files that are capable |
| 26 | // of being built with Bazel. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 27 | func Codegen(ctx *CodegenContext) CodegenMetrics { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 28 | // This directory stores BUILD files that could be eventually checked-in. |
| 29 | bp2buildDir := android.PathForOutput(ctx, "bp2build") |
| 30 | android.RemoveAllOutputDir(bp2buildDir) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 31 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 32 | buildToTargets, metrics, compatLayer := GenerateBazelTargets(ctx, true) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 33 | bp2buildFiles := CreateBazelFiles(nil, buildToTargets, ctx.mode) |
| 34 | writeFiles(ctx, bp2buildDir, bp2buildFiles) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 35 | |
Liz Kammer | 09f947d | 2021-05-12 14:51:49 -0400 | [diff] [blame] | 36 | soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName) |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 37 | writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(compatLayer)) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 38 | |
| 39 | return metrics |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 42 | // Get the output directory and create it if it doesn't exist. |
| 43 | func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath { |
| 44 | dirPath := outputDir.Join(ctx, dir) |
| 45 | android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm) |
| 46 | return dirPath |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 49 | // writeFiles materializes a list of BazelFile rooted at outputDir. |
| 50 | func writeFiles(ctx android.PathContext, outputDir android.OutputPath, files []BazelFile) { |
| 51 | for _, f := range files { |
| 52 | p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename) |
| 53 | if err := writeFile(ctx, p, f.Contents); err != nil { |
| 54 | panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 59 | func writeFile(ctx android.PathContext, pathToFile android.OutputPath, content string) error { |
| 60 | // These files are made editable to allow users to modify and iterate on them |
| 61 | // in the source tree. |
| 62 | return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 63 | } |