blob: 73df6755561c9ec7a0e2ceff4b1ea9e9e1457c72 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001package bp2build
2
3import (
Jingwen Chen0ee88a62022-01-07 14:55:29 +00004 "encoding/json"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08005 "reflect"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08006 "strings"
7
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00008 "android/soong/android"
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +00009 "android/soong/cc"
Sam Delmerico932c01c2022-03-25 16:33:26 +000010 cc_config "android/soong/cc/config"
11 java_config "android/soong/java/config"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000012
Jingwen Chen7810e172022-07-29 02:25:34 +000013 "android/soong/apex"
14
Liz Kammer2dd9ca42020-11-25 16:06:39 -080015 "github.com/google/blueprint/proptools"
16)
17
18type BazelFile struct {
19 Dir string
20 Basename string
21 Contents string
22}
23
Spandan Das83e787e2023-01-11 02:50:00 +000024// PRIVATE: Use CreateSoongInjectionDirFiles instead
Cole Faust9e384e22023-02-08 17:43:09 -080025func soongInjectionFiles(cfg android.Config, metrics CodegenMetrics) ([]BazelFile, error) {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000026 var files []BazelFile
27
Sam Delmerico46d08b42022-11-15 15:51:04 -050028 files = append(files, newFile("android", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
29 files = append(files, newFile("android", "constants.bzl", android.BazelCcToolchainVars(cfg)))
30
Jingwen Chenc63677b2021-06-17 05:43:19 +000031 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Trevor Radcliffe4f95ee92023-01-19 16:02:47 +000032 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 Delmerico932c01c2022-03-25 16:33:26 +000034
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 Chenbf61afb2021-05-06 13:31:18 +000037
Jingwen Chen7810e172022-07-29 02:25:34 +000038 files = append(files, newFile("apex_toolchain", GeneratedBuildFileName, "")) // Creates a //apex_toolchain package.
Cole Faust9e384e22023-02-08 17:43:09 -080039 apexToolchainVars, err := apex.BazelApexToolchainVars()
40 if err != nil {
41 return nil, err
42 }
43 files = append(files, newFile("apex_toolchain", "constants.bzl", apexToolchainVars))
Jingwen Chen7810e172022-07-29 02:25:34 +000044
usta4f5d2c12022-10-28 23:32:01 -040045 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.Serialize().ConvertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000046
Kevin Dagostino60f562a2022-09-20 03:54:47 +000047 convertedModulePathMap, err := json.MarshalIndent(metrics.convertedModulePathMap, "", "\t")
48 if err != nil {
49 panic(err)
50 }
51 files = append(files, newFile("metrics", "converted_modules_path_map.json", string(convertedModulePathMap)))
52
Jingwen Chen01812022021-11-19 14:29:43 +000053 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
54
Liz Kammere8303bd2022-02-16 09:02:48 -050055 files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
56
Jingwen Chen0ee88a62022-01-07 14:55:29 +000057 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
58 if err != nil {
Cole Faust9e384e22023-02-08 17:43:09 -080059 return nil, err
Jingwen Chen0ee88a62022-01-07 14:55:29 +000060 }
61 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
62 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
Yu Liufc603162022-03-01 15:44:08 -080063 files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
Jingwen Chen0ee88a62022-01-07 14:55:29 +000064
Sam Delmericocb3c52c2023-02-03 17:40:08 -050065 files = append(files, newFile("allowlists", GeneratedBuildFileName, ""))
66 files = append(files, newFile("allowlists", "env.bzl", android.EnvironmentVarsFile(cfg)))
Cole Faust705968d2022-12-14 11:32:05 -080067 // TODO(b/262781701): Create an alternate soong_build entrypoint for writing out these files only when requested
68 files = append(files, newFile("allowlists", "mixed_build_prod_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelProdMode), "\n")+"\n"))
69 files = append(files, newFile("allowlists", "mixed_build_staging_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelStagingMode), "\n")+"\n"))
70
Cole Faust9e384e22023-02-08 17:43:09 -080071 return files, nil
Jingwen Chenbf61afb2021-05-06 13:31:18 +000072}
73
Liz Kammer2dd9ca42020-11-25 16:06:39 -080074func CreateBazelFiles(
Sasha Smundak0fd93e02022-05-19 19:34:31 -070075 cfg android.Config,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080076 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050077 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050078 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080079
Jingwen Chen6c309cd2021-04-01 07:11:11 +000080 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081
Jingwen Chen33832f92021-01-24 22:55:54 -050082 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000083 // Write top level WORKSPACE.
84 files = append(files, newFile("", "WORKSPACE", ""))
85
Jingwen Chen12b4c272021-03-10 02:05:59 -050086 // Used to denote that the top level directory is a package.
87 files = append(files, newFile("", GeneratedBuildFileName, ""))
88
89 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
90
Jingwen Chen73850672020-12-14 08:25:34 -050091 // These files are only used for queryview.
92 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
93
94 for bzlFileName, ruleShim := range ruleShims {
95 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
96 }
97 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080099
Cole Faust324a92e2022-08-23 15:29:05 -0700100 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101
102 return files
103}
104
Cole Faust324a92e2022-08-23 15:29:05 -0700105func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800106 files := make([]BazelFile, 0, len(buildToTargets))
107 for _, dir := range android.SortedStringKeys(buildToTargets) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800108 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +0000109 targets.sort()
110
111 var content string
Spandan Das5af0bd32022-09-28 20:43:08 +0000112 if mode == Bp2Build || mode == ApiBp2build {
Jingwen Chen49109762021-05-25 05:16:48 +0000113 content = `# READ THIS FIRST:
114# This file was automatically generated by bp2build for the Bazel migration project.
115# Feel free to edit or test it, but do *not* check it into your version control system.
116`
Jingwen Chen1c231732021-02-05 09:38:15 -0500117 content += targets.LoadStatements()
Sasha Smundak8bea2672022-08-04 13:31:14 -0700118 content += "\n\n"
119 // Get package rule from the handcrafted BUILD file, otherwise emit the default one.
120 prText := "package(default_visibility = [\"//visibility:public\"])\n"
121 if pr := targets.packageRule(); pr != nil {
122 prText = pr.content
123 }
124 content += prText
Jingwen Chen49109762021-05-25 05:16:48 +0000125 } else if mode == QueryView {
126 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800127 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500128 if content != "" {
129 // If there are load statements, add a couple of newlines.
130 content += "\n\n"
131 }
132 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500133 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800134 }
135 return files
136}
137
138func newFile(dir, basename, content string) BazelFile {
139 return BazelFile{
140 Dir: dir,
141 Basename: basename,
142 Contents: content,
143 }
144}
145
146const (
147 bazelRulesSubDir = "build/bazel/queryview_rules"
148
149 // additional files:
150 // * workspace file
151 // * base BUILD file
152 // * rules BUILD file
153 // * rules providers.bzl file
154 // * rules soong_module.bzl file
155 numAdditionalFiles = 5
156)
157
158var (
159 // Certain module property names are blocklisted/ignored here, for the reasons commented.
160 ignoredPropNames = map[string]bool{
Sam Delmerico263efde2022-09-08 10:43:42 -0400161 "name": true, // redundant, since this is explicitly generated for every target
162 "from": true, // reserved keyword
163 "in": true, // reserved keyword
164 "size": true, // reserved for tests
165 "arch": true, // interface prop type is not supported yet.
166 "multilib": true, // interface prop type is not supported yet.
167 "target": true, // interface prop type is not supported yet.
168 "visibility": true, // Bazel has native visibility semantics. Handle later.
169 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
170 "for": true, // reserved keyword, b/233579439
171 "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172 }
173)
174
175func shouldGenerateAttribute(prop string) bool {
176 return !ignoredPropNames[prop]
177}
178
179func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400180 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800181 // Skip unexported fields. Some properties are
182 // internal to Soong only, and these fields do not have PkgPath.
183 return true
184 }
Sasha Smundak8bea2672022-08-04 13:31:14 -0700185 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc.
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800186 // but cannot be set in a .bp file
187 if proptools.HasTag(field, "blueprint", "mutated") {
188 return true
189 }
190 return false
191}
192
193// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
194// testonly = True, forcing other rules that depend on _test rules to also be
195// marked as testonly = True. This semantic constraint is not present in Soong.
196// To work around, rename "*_test" rules to "*_test_".
197func canonicalizeModuleType(moduleName string) string {
198 if strings.HasSuffix(moduleName, "_test") {
199 return moduleName + "_"
200 }
201
202 return moduleName
203}