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