Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 4 | "fmt" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 5 | "reflect" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 6 | "strings" |
| 7 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 8 | "android/soong/android" |
| 9 | "android/soong/cc/config" |
| 10 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 11 | "github.com/google/blueprint/proptools" |
| 12 | ) |
| 13 | |
| 14 | type BazelFile struct { |
| 15 | Dir string |
| 16 | Basename string |
| 17 | Contents string |
| 18 | } |
| 19 | |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 20 | func CreateSoongInjectionFiles(metrics CodegenMetrics) []BazelFile { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 21 | var files []BazelFile |
| 22 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 23 | files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package. |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 24 | files = append(files, newFile("cc_toolchain", "constants.bzl", config.BazelCcToolchainVars())) |
| 25 | |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 26 | files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n"))) |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 27 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 28 | return files |
| 29 | } |
| 30 | |
Jingwen Chen | 6117450 | 2021-09-17 08:40:45 +0000 | [diff] [blame] | 31 | func convertedModules(convertedModules []string) string { |
| 32 | return strings.Join(convertedModules, "\n") |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 35 | func CreateBazelFiles( |
| 36 | ruleShims map[string]RuleShim, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 37 | buildToTargets map[string]BazelTargets, |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 38 | mode CodegenMode) []BazelFile { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 39 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 40 | var files []BazelFile |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 41 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 42 | if mode == QueryView { |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 43 | // Write top level WORKSPACE. |
| 44 | files = append(files, newFile("", "WORKSPACE", "")) |
| 45 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 46 | // Used to denote that the top level directory is a package. |
| 47 | files = append(files, newFile("", GeneratedBuildFileName, "")) |
| 48 | |
| 49 | files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, "")) |
| 50 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 51 | // These files are only used for queryview. |
| 52 | files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl)) |
| 53 | |
| 54 | for bzlFileName, ruleShim := range ruleShims { |
| 55 | files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content)) |
| 56 | } |
| 57 | files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims))) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 58 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 59 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 60 | files = append(files, createBuildFiles(buildToTargets, mode)...) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 61 | |
| 62 | return files |
| 63 | } |
| 64 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 65 | func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 66 | files := make([]BazelFile, 0, len(buildToTargets)) |
| 67 | for _, dir := range android.SortedStringKeys(buildToTargets) { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 68 | if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 69 | fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir) |
| 70 | continue |
| 71 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 72 | targets := buildToTargets[dir] |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 73 | targets.sort() |
| 74 | |
| 75 | var content string |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 76 | if mode == Bp2Build { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 77 | content = `# READ THIS FIRST: |
| 78 | # This file was automatically generated by bp2build for the Bazel migration project. |
| 79 | # Feel free to edit or test it, but do *not* check it into your version control system. |
| 80 | ` |
| 81 | if targets.hasHandcraftedTargets() { |
| 82 | // For BUILD files with both handcrafted and generated targets, |
| 83 | // don't hardcode actual content, like package() declarations. |
| 84 | // Leave that responsibility to the checked-in BUILD file |
| 85 | // instead. |
| 86 | content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.` |
| 87 | } else { |
| 88 | // For fully-generated BUILD files, hardcode the default visibility. |
| 89 | content += "package(default_visibility = [\"//visibility:public\"])" |
| 90 | } |
| 91 | content += "\n" |
Jingwen Chen | 1c23173 | 2021-02-05 09:38:15 -0500 | [diff] [blame] | 92 | content += targets.LoadStatements() |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 93 | } else if mode == QueryView { |
| 94 | content = soongModuleLoad |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 95 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 96 | if content != "" { |
| 97 | // If there are load statements, add a couple of newlines. |
| 98 | content += "\n\n" |
| 99 | } |
| 100 | content += targets.String() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 101 | files = append(files, newFile(dir, GeneratedBuildFileName, content)) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 102 | } |
| 103 | return files |
| 104 | } |
| 105 | |
| 106 | func newFile(dir, basename, content string) BazelFile { |
| 107 | return BazelFile{ |
| 108 | Dir: dir, |
| 109 | Basename: basename, |
| 110 | Contents: content, |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | const ( |
| 115 | bazelRulesSubDir = "build/bazel/queryview_rules" |
| 116 | |
| 117 | // additional files: |
| 118 | // * workspace file |
| 119 | // * base BUILD file |
| 120 | // * rules BUILD file |
| 121 | // * rules providers.bzl file |
| 122 | // * rules soong_module.bzl file |
| 123 | numAdditionalFiles = 5 |
| 124 | ) |
| 125 | |
| 126 | var ( |
| 127 | // Certain module property names are blocklisted/ignored here, for the reasons commented. |
| 128 | ignoredPropNames = map[string]bool{ |
| 129 | "name": true, // redundant, since this is explicitly generated for every target |
| 130 | "from": true, // reserved keyword |
| 131 | "in": true, // reserved keyword |
Jingwen Chen | 88ae408 | 2021-02-24 19:55:50 -0500 | [diff] [blame] | 132 | "size": true, // reserved for tests |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 133 | "arch": true, // interface prop type is not supported yet. |
| 134 | "multilib": true, // interface prop type is not supported yet. |
| 135 | "target": true, // interface prop type is not supported yet. |
| 136 | "visibility": true, // Bazel has native visibility semantics. Handle later. |
| 137 | "features": true, // There is already a built-in attribute 'features' which cannot be overridden. |
| 138 | } |
| 139 | ) |
| 140 | |
| 141 | func shouldGenerateAttribute(prop string) bool { |
| 142 | return !ignoredPropNames[prop] |
| 143 | } |
| 144 | |
| 145 | func shouldSkipStructField(field reflect.StructField) bool { |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 146 | if field.PkgPath != "" && !field.Anonymous { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 147 | // Skip unexported fields. Some properties are |
| 148 | // internal to Soong only, and these fields do not have PkgPath. |
| 149 | return true |
| 150 | } |
| 151 | // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc |
| 152 | // but cannot be set in a .bp file |
| 153 | if proptools.HasTag(field, "blueprint", "mutated") { |
| 154 | return true |
| 155 | } |
| 156 | return false |
| 157 | } |
| 158 | |
| 159 | // FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as |
| 160 | // testonly = True, forcing other rules that depend on _test rules to also be |
| 161 | // marked as testonly = True. This semantic constraint is not present in Soong. |
| 162 | // To work around, rename "*_test" rules to "*_test_". |
| 163 | func canonicalizeModuleType(moduleName string) string { |
| 164 | if strings.HasSuffix(moduleName, "_test") { |
| 165 | return moduleName + "_" |
| 166 | } |
| 167 | |
| 168 | return moduleName |
| 169 | } |