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