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" |
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" |
Trevor Radcliffe | 4f95ee9 | 2023-01-19 16:02:47 +0000 | [diff] [blame^] | 9 | "android/soong/cc" |
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 | |
Jingwen Chen | 7810e17 | 2022-07-29 02:25:34 +0000 | [diff] [blame] | 13 | "android/soong/apex" |
| 14 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 15 | "github.com/google/blueprint/proptools" |
| 16 | ) |
| 17 | |
| 18 | type BazelFile struct { |
| 19 | Dir string |
| 20 | Basename string |
| 21 | Contents string |
| 22 | } |
| 23 | |
Spandan Das | 83e787e | 2023-01-11 02:50:00 +0000 | [diff] [blame] | 24 | // PRIVATE: Use CreateSoongInjectionDirFiles instead |
| 25 | func soongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile { |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 26 | var files []BazelFile |
| 27 | |
Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 28 | files = append(files, newFile("android", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package. |
| 29 | files = append(files, newFile("android", "constants.bzl", android.BazelCcToolchainVars(cfg))) |
| 30 | |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 31 | files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package. |
Trevor Radcliffe | 4f95ee9 | 2023-01-19 16:02:47 +0000 | [diff] [blame^] | 32 | files = append(files, newFile("cc_toolchain", "config_constants.bzl", cc_config.BazelCcToolchainVars(cfg))) |
| 33 | files = append(files, newFile("cc_toolchain", "sanitizer_constants.bzl", cc.BazelCcSanitizerToolchainVars(cfg))) |
Sam Delmerico | 932c01c | 2022-03-25 16:33:26 +0000 | [diff] [blame] | 34 | |
| 35 | files = append(files, newFile("java_toolchain", GeneratedBuildFileName, "")) // Creates a //java_toolchain package. |
| 36 | files = append(files, newFile("java_toolchain", "constants.bzl", java_config.BazelJavaToolchainVars(cfg))) |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 37 | |
Jingwen Chen | 7810e17 | 2022-07-29 02:25:34 +0000 | [diff] [blame] | 38 | files = append(files, newFile("apex_toolchain", GeneratedBuildFileName, "")) // Creates a //apex_toolchain package. |
| 39 | files = append(files, newFile("apex_toolchain", "constants.bzl", apex.BazelApexToolchainVars())) |
| 40 | |
usta | 4f5d2c1 | 2022-10-28 23:32:01 -0400 | [diff] [blame] | 41 | files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.Serialize().ConvertedModules, "\n"))) |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 42 | |
Kevin Dagostino | 60f562a | 2022-09-20 03:54:47 +0000 | [diff] [blame] | 43 | convertedModulePathMap, err := json.MarshalIndent(metrics.convertedModulePathMap, "", "\t") |
| 44 | if err != nil { |
| 45 | panic(err) |
| 46 | } |
| 47 | files = append(files, newFile("metrics", "converted_modules_path_map.json", string(convertedModulePathMap))) |
| 48 | |
Jingwen Chen | 0181202 | 2021-11-19 14:29:43 +0000 | [diff] [blame] | 49 | files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String())) |
| 50 | |
Liz Kammer | e8303bd | 2022-02-16 09:02:48 -0500 | [diff] [blame] | 51 | files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations())) |
| 52 | |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 53 | apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg)) |
| 54 | if err != nil { |
| 55 | panic(err) |
| 56 | } |
| 57 | files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`)) |
| 58 | files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent))) |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 59 | files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg))) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 60 | |
Cole Faust | 705968d | 2022-12-14 11:32:05 -0800 | [diff] [blame] | 61 | // TODO(b/262781701): Create an alternate soong_build entrypoint for writing out these files only when requested |
| 62 | files = append(files, newFile("allowlists", "mixed_build_prod_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelProdMode), "\n")+"\n")) |
| 63 | files = append(files, newFile("allowlists", "mixed_build_staging_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelStagingMode), "\n")+"\n")) |
| 64 | |
Jingwen Chen | bf61afb | 2021-05-06 13:31:18 +0000 | [diff] [blame] | 65 | return files |
| 66 | } |
| 67 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 68 | func CreateBazelFiles( |
Sasha Smundak | 0fd93e0 | 2022-05-19 19:34:31 -0700 | [diff] [blame] | 69 | cfg android.Config, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 70 | ruleShims map[string]RuleShim, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 71 | buildToTargets map[string]BazelTargets, |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 72 | mode CodegenMode) []BazelFile { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 73 | |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 74 | var files []BazelFile |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 75 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 76 | if mode == QueryView { |
Jingwen Chen | 6c309cd | 2021-04-01 07:11:11 +0000 | [diff] [blame] | 77 | // Write top level WORKSPACE. |
| 78 | files = append(files, newFile("", "WORKSPACE", "")) |
| 79 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 80 | // Used to denote that the top level directory is a package. |
| 81 | files = append(files, newFile("", GeneratedBuildFileName, "")) |
| 82 | |
| 83 | files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, "")) |
| 84 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 85 | // These files are only used for queryview. |
| 86 | files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl)) |
| 87 | |
| 88 | for bzlFileName, ruleShim := range ruleShims { |
| 89 | files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content)) |
| 90 | } |
| 91 | files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims))) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 92 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 93 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 94 | files = append(files, createBuildFiles(buildToTargets, mode)...) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 95 | |
| 96 | return files |
| 97 | } |
| 98 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 99 | func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 100 | files := make([]BazelFile, 0, len(buildToTargets)) |
| 101 | for _, dir := range android.SortedStringKeys(buildToTargets) { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 102 | targets := buildToTargets[dir] |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 103 | targets.sort() |
| 104 | |
| 105 | var content string |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 106 | if mode == Bp2Build || mode == ApiBp2build { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 107 | content = `# READ THIS FIRST: |
| 108 | # This file was automatically generated by bp2build for the Bazel migration project. |
| 109 | # Feel free to edit or test it, but do *not* check it into your version control system. |
| 110 | ` |
Jingwen Chen | 1c23173 | 2021-02-05 09:38:15 -0500 | [diff] [blame] | 111 | content += targets.LoadStatements() |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 112 | content += "\n\n" |
| 113 | // Get package rule from the handcrafted BUILD file, otherwise emit the default one. |
| 114 | prText := "package(default_visibility = [\"//visibility:public\"])\n" |
| 115 | if pr := targets.packageRule(); pr != nil { |
| 116 | prText = pr.content |
| 117 | } |
| 118 | content += prText |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 119 | } else if mode == QueryView { |
| 120 | content = soongModuleLoad |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 121 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 122 | if content != "" { |
| 123 | // If there are load statements, add a couple of newlines. |
| 124 | content += "\n\n" |
| 125 | } |
| 126 | content += targets.String() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 127 | files = append(files, newFile(dir, GeneratedBuildFileName, content)) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 128 | } |
| 129 | return files |
| 130 | } |
| 131 | |
| 132 | func newFile(dir, basename, content string) BazelFile { |
| 133 | return BazelFile{ |
| 134 | Dir: dir, |
| 135 | Basename: basename, |
| 136 | Contents: content, |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | const ( |
| 141 | bazelRulesSubDir = "build/bazel/queryview_rules" |
| 142 | |
| 143 | // additional files: |
| 144 | // * workspace file |
| 145 | // * base BUILD file |
| 146 | // * rules BUILD file |
| 147 | // * rules providers.bzl file |
| 148 | // * rules soong_module.bzl file |
| 149 | numAdditionalFiles = 5 |
| 150 | ) |
| 151 | |
| 152 | var ( |
| 153 | // Certain module property names are blocklisted/ignored here, for the reasons commented. |
| 154 | ignoredPropNames = map[string]bool{ |
Sam Delmerico | 263efde | 2022-09-08 10:43:42 -0400 | [diff] [blame] | 155 | "name": true, // redundant, since this is explicitly generated for every target |
| 156 | "from": true, // reserved keyword |
| 157 | "in": true, // reserved keyword |
| 158 | "size": true, // reserved for tests |
| 159 | "arch": true, // interface prop type is not supported yet. |
| 160 | "multilib": true, // interface prop type is not supported yet. |
| 161 | "target": true, // interface prop type is not supported yet. |
| 162 | "visibility": true, // Bazel has native visibility semantics. Handle later. |
| 163 | "features": true, // There is already a built-in attribute 'features' which cannot be overridden. |
| 164 | "for": true, // reserved keyword, b/233579439 |
| 165 | "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 166 | } |
| 167 | ) |
| 168 | |
| 169 | func shouldGenerateAttribute(prop string) bool { |
| 170 | return !ignoredPropNames[prop] |
| 171 | } |
| 172 | |
| 173 | func shouldSkipStructField(field reflect.StructField) bool { |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 174 | if field.PkgPath != "" && !field.Anonymous { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 175 | // Skip unexported fields. Some properties are |
| 176 | // internal to Soong only, and these fields do not have PkgPath. |
| 177 | return true |
| 178 | } |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 179 | // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc. |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 180 | // but cannot be set in a .bp file |
| 181 | if proptools.HasTag(field, "blueprint", "mutated") { |
| 182 | return true |
| 183 | } |
| 184 | return false |
| 185 | } |
| 186 | |
| 187 | // FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as |
| 188 | // testonly = True, forcing other rules that depend on _test rules to also be |
| 189 | // marked as testonly = True. This semantic constraint is not present in Soong. |
| 190 | // To work around, rename "*_test" rules to "*_test_". |
| 191 | func canonicalizeModuleType(moduleName string) string { |
| 192 | if strings.HasSuffix(moduleName, "_test") { |
| 193 | return moduleName + "_" |
| 194 | } |
| 195 | |
| 196 | return moduleName |
| 197 | } |