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 ( |
Cole Faust | c9508aa | 2023-02-07 11:38:27 -0800 | [diff] [blame^] | 18 | "android/soong/starlark_import" |
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" |
Chris Parsons | 520e88b | 2023-02-09 17:54:00 -0500 | [diff] [blame] | 21 | "path/filepath" |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 22 | "strings" |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
| 25 | "android/soong/bazel" |
Chris Parsons | 520e88b | 2023-02-09 17:54:00 -0500 | [diff] [blame] | 26 | "android/soong/shared" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Chris Parsons | 520e88b | 2023-02-09 17:54:00 -0500 | [diff] [blame] | 29 | func deleteFilesExcept(ctx *CodegenContext, rootOutputPath android.OutputPath, except []BazelFile) { |
| 30 | // Delete files that should no longer be present. |
| 31 | bp2buildDirAbs := shared.JoinPath(ctx.topDir, rootOutputPath.String()) |
| 32 | |
| 33 | filesToDelete := make(map[string]struct{}) |
| 34 | err := filepath.Walk(bp2buildDirAbs, |
| 35 | func(path string, info os.FileInfo, err error) error { |
| 36 | if err != nil { |
| 37 | return err |
| 38 | } |
| 39 | if !info.IsDir() { |
| 40 | relPath, err := filepath.Rel(bp2buildDirAbs, path) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | filesToDelete[relPath] = struct{}{} |
| 45 | } |
| 46 | return nil |
| 47 | }) |
| 48 | if err != nil { |
| 49 | fmt.Printf("ERROR reading %s: %s", bp2buildDirAbs, err) |
| 50 | os.Exit(1) |
| 51 | } |
| 52 | |
| 53 | for _, bazelFile := range except { |
| 54 | filePath := filepath.Join(bazelFile.Dir, bazelFile.Basename) |
| 55 | delete(filesToDelete, filePath) |
| 56 | } |
| 57 | for f, _ := range filesToDelete { |
| 58 | absPath := shared.JoinPath(bp2buildDirAbs, f) |
| 59 | if err := os.RemoveAll(absPath); err != nil { |
| 60 | fmt.Printf("ERROR deleting %s: %s", absPath, err) |
| 61 | os.Exit(1) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 66 | // Codegen is the backend of bp2build. The code generator is responsible for |
| 67 | // writing .bzl files that are equivalent to Android.bp files that are capable |
| 68 | // of being built with Bazel. |
usta | 4f5d2c1 | 2022-10-28 23:32:01 -0400 | [diff] [blame] | 69 | func Codegen(ctx *CodegenContext) *CodegenMetrics { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 70 | // This directory stores BUILD files that could be eventually checked-in. |
| 71 | bp2buildDir := android.PathForOutput(ctx, "bp2build") |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 72 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 73 | res, errs := GenerateBazelTargets(ctx, true) |
| 74 | if len(errs) > 0 { |
| 75 | errMsgs := make([]string, len(errs)) |
| 76 | for i, err := range errs { |
| 77 | errMsgs[i] = fmt.Sprintf("%q", err) |
| 78 | } |
| 79 | fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n")) |
| 80 | os.Exit(1) |
| 81 | } |
Sasha Smundak | 0fd93e0 | 2022-05-19 19:34:31 -0700 | [diff] [blame] | 82 | bp2buildFiles := CreateBazelFiles(ctx.Config(), nil, res.buildFileToTargets, ctx.mode) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 83 | writeFiles(ctx, bp2buildDir, bp2buildFiles) |
Chris Parsons | 520e88b | 2023-02-09 17:54:00 -0500 | [diff] [blame] | 84 | // Delete files under the bp2build root which weren't just written. An |
| 85 | // alternative would have been to delete the whole directory and write these |
| 86 | // files. However, this would regenerate files which were otherwise unchanged |
| 87 | // since the last bp2build run, which would have negative incremental |
| 88 | // performance implications. |
| 89 | deleteFilesExcept(ctx, bp2buildDir, bp2buildFiles) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 90 | |
Cole Faust | 9e384e2 | 2023-02-08 17:43:09 -0800 | [diff] [blame] | 91 | injectionFiles, err := CreateSoongInjectionDirFiles(ctx, res.metrics) |
| 92 | if err != nil { |
| 93 | fmt.Printf("%s\n", err.Error()) |
| 94 | os.Exit(1) |
| 95 | } |
| 96 | writeFiles(ctx, android.PathForOutput(ctx, bazel.SoongInjectionDirName), injectionFiles) |
Cole Faust | c9508aa | 2023-02-07 11:38:27 -0800 | [diff] [blame^] | 97 | starlarkDeps, err := starlark_import.GetNinjaDeps() |
| 98 | if err != nil { |
| 99 | fmt.Fprintf(os.Stderr, "%s\n", err) |
| 100 | os.Exit(1) |
| 101 | } |
| 102 | ctx.AddNinjaFileDeps(starlarkDeps...) |
Spandan Das | 83e787e | 2023-01-11 02:50:00 +0000 | [diff] [blame] | 103 | return &res.metrics |
| 104 | } |
| 105 | |
| 106 | // Wrapper function that will be responsible for all files in soong_injection directory |
| 107 | // This includes |
| 108 | // 1. config value(s) that are hardcoded in Soong |
| 109 | // 2. product_config variables |
Cole Faust | 9e384e2 | 2023-02-08 17:43:09 -0800 | [diff] [blame] | 110 | func CreateSoongInjectionDirFiles(ctx *CodegenContext, metrics CodegenMetrics) ([]BazelFile, error) { |
Spandan Das | 83e787e | 2023-01-11 02:50:00 +0000 | [diff] [blame] | 111 | var ret []BazelFile |
| 112 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 113 | productConfigFiles, err := CreateProductConfigFiles(ctx) |
| 114 | if err != nil { |
Cole Faust | 9e384e2 | 2023-02-08 17:43:09 -0800 | [diff] [blame] | 115 | return nil, err |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 116 | } |
Spandan Das | 83e787e | 2023-01-11 02:50:00 +0000 | [diff] [blame] | 117 | ret = append(ret, productConfigFiles...) |
Cole Faust | 9e384e2 | 2023-02-08 17:43:09 -0800 | [diff] [blame] | 118 | injectionFiles, err := soongInjectionFiles(ctx.Config(), metrics) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | ret = append(ret, injectionFiles...) |
| 123 | return ret, nil |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 126 | // Get the output directory and create it if it doesn't exist. |
| 127 | func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath { |
| 128 | dirPath := outputDir.Join(ctx, dir) |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 129 | if err := android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm); err != nil { |
| 130 | fmt.Printf("ERROR: path %s: %s", dirPath, err.Error()) |
| 131 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 132 | return dirPath |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 135 | // writeFiles materializes a list of BazelFile rooted at outputDir. |
| 136 | func writeFiles(ctx android.PathContext, outputDir android.OutputPath, files []BazelFile) { |
| 137 | for _, f := range files { |
| 138 | p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename) |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 139 | if err := writeFile(p, f.Contents); err != nil { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 140 | panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 145 | func writeFile(pathToFile android.OutputPath, content string) error { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 146 | // These files are made editable to allow users to modify and iterate on them |
| 147 | // in the source tree. |
| 148 | return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 149 | } |