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" |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 19 | "fmt" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 20 | "os" |
| 21 | ) |
| 22 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 23 | // The Bazel bp2build code generator is responsible for writing .bzl files that are equivalent to |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 24 | // Android.bp files that are capable of being built with Bazel. |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 25 | func Codegen(ctx CodegenContext) { |
| 26 | outputDir := android.PathForOutput(ctx, "bp2build") |
| 27 | android.RemoveAllOutputDir(outputDir) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 28 | |
| 29 | ruleShims := CreateRuleShims(android.ModuleTypeFactories()) |
| 30 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 31 | buildToTargets := GenerateSoongModuleTargets(ctx.Context()) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 32 | |
| 33 | filesToWrite := CreateBazelFiles(ruleShims, buildToTargets) |
| 34 | for _, f := range filesToWrite { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 35 | if err := writeFile(outputDir, ctx, f); err != nil { |
| 36 | fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 41 | func writeFile(outputDir android.OutputPath, ctx android.PathContext, f BazelFile) error { |
| 42 | return writeReadOnlyFile(ctx, getOutputPath(outputDir, ctx, f.Dir), f.Basename, f.Contents) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 45 | func getOutputPath(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath { |
| 46 | return outputDir.Join(ctx, dir) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // The auto-conversion directory should be read-only, sufficient for bazel query. The files |
| 50 | // are not intended to be edited by end users. |
| 51 | func writeReadOnlyFile(ctx android.PathContext, dir android.OutputPath, baseName, content string) error { |
| 52 | android.CreateOutputDirIfNonexistent(dir, os.ModePerm) |
| 53 | pathToFile := dir.Join(ctx, baseName) |
| 54 | |
| 55 | // 0444 is read-only |
| 56 | err := android.WriteFileToOutputDir(pathToFile, []byte(content), 0444) |
| 57 | |
| 58 | return err |
| 59 | } |