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 ( |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 18 | "fmt" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 19 | "os" |
Chris Parsons | 520e88b | 2023-02-09 17:54:00 -0500 | [diff] [blame] | 20 | "path/filepath" |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 21 | "strings" |
Chris Parsons | 3b1f83d | 2021-10-14 14:08:38 -0400 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
| 24 | "android/soong/bazel" |
Chris Parsons | 520e88b | 2023-02-09 17:54:00 -0500 | [diff] [blame] | 25 | "android/soong/shared" |
usta | aaf2fd1 | 2023-07-01 11:40:36 -0400 | [diff] [blame] | 26 | "android/soong/starlark_import" |
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 { |
usta | aaf2fd1 | 2023-07-01 11:40:36 -0400 | [diff] [blame] | 70 | ctx.Context().BeginEvent("Codegen") |
| 71 | defer ctx.Context().EndEvent("Codegen") |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 72 | // This directory stores BUILD files that could be eventually checked-in. |
| 73 | bp2buildDir := android.PathForOutput(ctx, "bp2build") |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 74 | |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 75 | res, errs := GenerateBazelTargets(ctx, true) |
| 76 | if len(errs) > 0 { |
| 77 | errMsgs := make([]string, len(errs)) |
| 78 | for i, err := range errs { |
| 79 | errMsgs[i] = fmt.Sprintf("%q", err) |
| 80 | } |
| 81 | fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n")) |
| 82 | os.Exit(1) |
| 83 | } |
usta | aaf2fd1 | 2023-07-01 11:40:36 -0400 | [diff] [blame] | 84 | var bp2buildFiles []BazelFile |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 85 | productConfig, err := createProductConfigFiles(ctx, res.metrics) |
usta | aaf2fd1 | 2023-07-01 11:40:36 -0400 | [diff] [blame] | 86 | ctx.Context().EventHandler.Do("CreateBazelFile", func() { |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 87 | allTargets := make(map[string]BazelTargets) |
| 88 | for k, v := range res.buildFileToTargets { |
| 89 | allTargets[k] = append(allTargets[k], v...) |
| 90 | } |
| 91 | for k, v := range productConfig.bp2buildTargets { |
| 92 | allTargets[k] = append(allTargets[k], v...) |
| 93 | } |
| 94 | bp2buildFiles = CreateBazelFiles(nil, allTargets, ctx.mode) |
usta | aaf2fd1 | 2023-07-01 11:40:36 -0400 | [diff] [blame] | 95 | }) |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 96 | bp2buildFiles = append(bp2buildFiles, productConfig.bp2buildFiles...) |
| 97 | injectionFiles, err := createSoongInjectionDirFiles(ctx, res.metrics) |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 98 | if err != nil { |
| 99 | fmt.Printf("%s\n", err.Error()) |
| 100 | os.Exit(1) |
| 101 | } |
Cole Faust | 6054cdf | 2023-09-12 10:07:07 -0700 | [diff] [blame] | 102 | injectionFiles = append(injectionFiles, productConfig.injectionFiles...) |
| 103 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 104 | writeFiles(ctx, bp2buildDir, bp2buildFiles) |
Chris Parsons | 520e88b | 2023-02-09 17:54:00 -0500 | [diff] [blame] | 105 | // Delete files under the bp2build root which weren't just written. An |
| 106 | // alternative would have been to delete the whole directory and write these |
| 107 | // files. However, this would regenerate files which were otherwise unchanged |
| 108 | // since the last bp2build run, which would have negative incremental |
| 109 | // performance implications. |
| 110 | deleteFilesExcept(ctx, bp2buildDir, bp2buildFiles) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 111 | |
Cole Faust | 9e384e2 | 2023-02-08 17:43:09 -0800 | [diff] [blame] | 112 | writeFiles(ctx, android.PathForOutput(ctx, bazel.SoongInjectionDirName), injectionFiles) |
Cole Faust | c9508aa | 2023-02-07 11:38:27 -0800 | [diff] [blame] | 113 | starlarkDeps, err := starlark_import.GetNinjaDeps() |
| 114 | if err != nil { |
| 115 | fmt.Fprintf(os.Stderr, "%s\n", err) |
| 116 | os.Exit(1) |
| 117 | } |
| 118 | ctx.AddNinjaFileDeps(starlarkDeps...) |
Spandan Das | 83e787e | 2023-01-11 02:50:00 +0000 | [diff] [blame] | 119 | return &res.metrics |
| 120 | } |
| 121 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 122 | // Get the output directory and create it if it doesn't exist. |
| 123 | func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath { |
| 124 | dirPath := outputDir.Join(ctx, dir) |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 125 | if err := android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm); err != nil { |
| 126 | fmt.Printf("ERROR: path %s: %s", dirPath, err.Error()) |
| 127 | } |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 128 | return dirPath |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 131 | // writeFiles materializes a list of BazelFile rooted at outputDir. |
| 132 | func writeFiles(ctx android.PathContext, outputDir android.OutputPath, files []BazelFile) { |
| 133 | for _, f := range files { |
| 134 | p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename) |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 135 | if err := writeFile(p, f.Contents); err != nil { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 136 | panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 141 | func writeFile(pathToFile android.OutputPath, content string) error { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 142 | // These files are made editable to allow users to modify and iterate on them |
| 143 | // in the source tree. |
| 144 | return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 145 | } |