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