blob: e53f29e39bb78245d7c5bd3b5d5ed0c83cb93427 [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"
Sam Delmerico932c01c2022-03-25 16:33:26 +00009 cc_config "android/soong/cc/config"
10 java_config "android/soong/java/config"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000011
Jingwen Chen7810e172022-07-29 02:25:34 +000012 "android/soong/apex"
13
Liz Kammer2dd9ca42020-11-25 16:06:39 -080014 "github.com/google/blueprint/proptools"
15)
16
17type BazelFile struct {
18 Dir string
19 Basename string
20 Contents string
21}
22
Spandan Das83e787e2023-01-11 02:50:00 +000023// PRIVATE: Use CreateSoongInjectionDirFiles instead
24func soongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000025 var files []BazelFile
26
Sam Delmerico46d08b42022-11-15 15:51:04 -050027 files = append(files, newFile("android", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
28 files = append(files, newFile("android", "constants.bzl", android.BazelCcToolchainVars(cfg)))
29
Jingwen Chenc63677b2021-06-17 05:43:19 +000030 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Sam Delmerico932c01c2022-03-25 16:33:26 +000031 files = append(files, newFile("cc_toolchain", "constants.bzl", cc_config.BazelCcToolchainVars(cfg)))
32
33 files = append(files, newFile("java_toolchain", GeneratedBuildFileName, "")) // Creates a //java_toolchain package.
34 files = append(files, newFile("java_toolchain", "constants.bzl", java_config.BazelJavaToolchainVars(cfg)))
Jingwen Chenbf61afb2021-05-06 13:31:18 +000035
Jingwen Chen7810e172022-07-29 02:25:34 +000036 files = append(files, newFile("apex_toolchain", GeneratedBuildFileName, "")) // Creates a //apex_toolchain package.
37 files = append(files, newFile("apex_toolchain", "constants.bzl", apex.BazelApexToolchainVars()))
38
usta4f5d2c12022-10-28 23:32:01 -040039 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.Serialize().ConvertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000040
Kevin Dagostino60f562a2022-09-20 03:54:47 +000041 convertedModulePathMap, err := json.MarshalIndent(metrics.convertedModulePathMap, "", "\t")
42 if err != nil {
43 panic(err)
44 }
45 files = append(files, newFile("metrics", "converted_modules_path_map.json", string(convertedModulePathMap)))
46
Jingwen Chen01812022021-11-19 14:29:43 +000047 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
48
Liz Kammere8303bd2022-02-16 09:02:48 -050049 files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
50
Jingwen Chen0ee88a62022-01-07 14:55:29 +000051 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
52 if err != nil {
53 panic(err)
54 }
55 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
56 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
Yu Liufc603162022-03-01 15:44:08 -080057 files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
Jingwen Chen0ee88a62022-01-07 14:55:29 +000058
Cole Faust705968d2022-12-14 11:32:05 -080059 // TODO(b/262781701): Create an alternate soong_build entrypoint for writing out these files only when requested
60 files = append(files, newFile("allowlists", "mixed_build_prod_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelProdMode), "\n")+"\n"))
61 files = append(files, newFile("allowlists", "mixed_build_staging_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelStagingMode), "\n")+"\n"))
62
Jingwen Chenbf61afb2021-05-06 13:31:18 +000063 return files
64}
65
Liz Kammer2dd9ca42020-11-25 16:06:39 -080066func CreateBazelFiles(
Sasha Smundak0fd93e02022-05-19 19:34:31 -070067 cfg android.Config,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050069 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050070 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071
Jingwen Chen6c309cd2021-04-01 07:11:11 +000072 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080073
Jingwen Chen33832f92021-01-24 22:55:54 -050074 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000075 // Write top level WORKSPACE.
76 files = append(files, newFile("", "WORKSPACE", ""))
77
Jingwen Chen12b4c272021-03-10 02:05:59 -050078 // Used to denote that the top level directory is a package.
79 files = append(files, newFile("", GeneratedBuildFileName, ""))
80
81 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
82
Jingwen Chen73850672020-12-14 08:25:34 -050083 // These files are only used for queryview.
84 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
85
86 for bzlFileName, ruleShim := range ruleShims {
87 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
88 }
89 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080090 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080091
Cole Faust324a92e2022-08-23 15:29:05 -070092 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080093
94 return files
95}
96
Cole Faust324a92e2022-08-23 15:29:05 -070097func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 files := make([]BazelFile, 0, len(buildToTargets))
99 for _, dir := range android.SortedStringKeys(buildToTargets) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800100 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +0000101 targets.sort()
102
103 var content string
Spandan Das5af0bd32022-09-28 20:43:08 +0000104 if mode == Bp2Build || mode == ApiBp2build {
Jingwen Chen49109762021-05-25 05:16:48 +0000105 content = `# READ THIS FIRST:
106# This file was automatically generated by bp2build for the Bazel migration project.
107# Feel free to edit or test it, but do *not* check it into your version control system.
108`
Jingwen Chen1c231732021-02-05 09:38:15 -0500109 content += targets.LoadStatements()
Sasha Smundak8bea2672022-08-04 13:31:14 -0700110 content += "\n\n"
111 // Get package rule from the handcrafted BUILD file, otherwise emit the default one.
112 prText := "package(default_visibility = [\"//visibility:public\"])\n"
113 if pr := targets.packageRule(); pr != nil {
114 prText = pr.content
115 }
116 content += prText
Jingwen Chen49109762021-05-25 05:16:48 +0000117 } else if mode == QueryView {
118 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800119 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500120 if content != "" {
121 // If there are load statements, add a couple of newlines.
122 content += "\n\n"
123 }
124 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500125 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800126 }
127 return files
128}
129
130func newFile(dir, basename, content string) BazelFile {
131 return BazelFile{
132 Dir: dir,
133 Basename: basename,
134 Contents: content,
135 }
136}
137
138const (
139 bazelRulesSubDir = "build/bazel/queryview_rules"
140
141 // additional files:
142 // * workspace file
143 // * base BUILD file
144 // * rules BUILD file
145 // * rules providers.bzl file
146 // * rules soong_module.bzl file
147 numAdditionalFiles = 5
148)
149
150var (
151 // Certain module property names are blocklisted/ignored here, for the reasons commented.
152 ignoredPropNames = map[string]bool{
Sam Delmerico263efde2022-09-08 10:43:42 -0400153 "name": true, // redundant, since this is explicitly generated for every target
154 "from": true, // reserved keyword
155 "in": true, // reserved keyword
156 "size": true, // reserved for tests
157 "arch": true, // interface prop type is not supported yet.
158 "multilib": true, // interface prop type is not supported yet.
159 "target": true, // interface prop type is not supported yet.
160 "visibility": true, // Bazel has native visibility semantics. Handle later.
161 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
162 "for": true, // reserved keyword, b/233579439
163 "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800164 }
165)
166
167func shouldGenerateAttribute(prop string) bool {
168 return !ignoredPropNames[prop]
169}
170
171func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400172 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800173 // Skip unexported fields. Some properties are
174 // internal to Soong only, and these fields do not have PkgPath.
175 return true
176 }
Sasha Smundak8bea2672022-08-04 13:31:14 -0700177 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc.
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800178 // but cannot be set in a .bp file
179 if proptools.HasTag(field, "blueprint", "mutated") {
180 return true
181 }
182 return false
183}
184
185// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
186// testonly = True, forcing other rules that depend on _test rules to also be
187// marked as testonly = True. This semantic constraint is not present in Soong.
188// To work around, rename "*_test" rules to "*_test_".
189func canonicalizeModuleType(moduleName string) string {
190 if strings.HasSuffix(moduleName, "_test") {
191 return moduleName + "_"
192 }
193
194 return moduleName
195}