blob: e15dd59251f381e6ae6454f2723d0b6156f498ec [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
Chris Parsons3b1f83d2021-10-14 14:08:38 -040023func CreateSoongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000024 var files []BazelFile
25
Sam Delmerico46d08b42022-11-15 15:51:04 -050026 files = append(files, newFile("android", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
27 files = append(files, newFile("android", "constants.bzl", android.BazelCcToolchainVars(cfg)))
28
Jingwen Chenc63677b2021-06-17 05:43:19 +000029 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Sam Delmerico932c01c2022-03-25 16:33:26 +000030 files = append(files, newFile("cc_toolchain", "constants.bzl", cc_config.BazelCcToolchainVars(cfg)))
31
32 files = append(files, newFile("java_toolchain", GeneratedBuildFileName, "")) // Creates a //java_toolchain package.
33 files = append(files, newFile("java_toolchain", "constants.bzl", java_config.BazelJavaToolchainVars(cfg)))
Jingwen Chenbf61afb2021-05-06 13:31:18 +000034
Jingwen Chen7810e172022-07-29 02:25:34 +000035 files = append(files, newFile("apex_toolchain", GeneratedBuildFileName, "")) // Creates a //apex_toolchain package.
36 files = append(files, newFile("apex_toolchain", "constants.bzl", apex.BazelApexToolchainVars()))
37
usta4f5d2c12022-10-28 23:32:01 -040038 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.Serialize().ConvertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000039
Kevin Dagostino60f562a2022-09-20 03:54:47 +000040 convertedModulePathMap, err := json.MarshalIndent(metrics.convertedModulePathMap, "", "\t")
41 if err != nil {
42 panic(err)
43 }
44 files = append(files, newFile("metrics", "converted_modules_path_map.json", string(convertedModulePathMap)))
45
Jingwen Chen01812022021-11-19 14:29:43 +000046 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
47
Liz Kammere8303bd2022-02-16 09:02:48 -050048 files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
49
Jingwen Chen0ee88a62022-01-07 14:55:29 +000050 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
51 if err != nil {
52 panic(err)
53 }
54 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
55 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
Yu Liufc603162022-03-01 15:44:08 -080056 files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
Jingwen Chen0ee88a62022-01-07 14:55:29 +000057
Cole Faust705968d2022-12-14 11:32:05 -080058 // TODO(b/262781701): Create an alternate soong_build entrypoint for writing out these files only when requested
59 files = append(files, newFile("allowlists", "mixed_build_prod_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelProdMode), "\n")+"\n"))
60 files = append(files, newFile("allowlists", "mixed_build_staging_allowlist.txt", strings.Join(android.GetBazelEnabledModules(android.BazelStagingMode), "\n")+"\n"))
61
Jingwen Chenbf61afb2021-05-06 13:31:18 +000062 return files
63}
64
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065func CreateBazelFiles(
Sasha Smundak0fd93e02022-05-19 19:34:31 -070066 cfg android.Config,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080067 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050068 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050069 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080070
Jingwen Chen6c309cd2021-04-01 07:11:11 +000071 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080072
Jingwen Chen33832f92021-01-24 22:55:54 -050073 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000074 // Write top level WORKSPACE.
75 files = append(files, newFile("", "WORKSPACE", ""))
76
Jingwen Chen12b4c272021-03-10 02:05:59 -050077 // Used to denote that the top level directory is a package.
78 files = append(files, newFile("", GeneratedBuildFileName, ""))
79
80 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
81
Jingwen Chen73850672020-12-14 08:25:34 -050082 // These files are only used for queryview.
83 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
84
85 for bzlFileName, ruleShim := range ruleShims {
86 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
87 }
88 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080090
Cole Faust324a92e2022-08-23 15:29:05 -070091 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080092
93 return files
94}
95
Cole Faust324a92e2022-08-23 15:29:05 -070096func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080097 files := make([]BazelFile, 0, len(buildToTargets))
98 for _, dir := range android.SortedStringKeys(buildToTargets) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080099 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +0000100 targets.sort()
101
102 var content string
Spandan Das5af0bd32022-09-28 20:43:08 +0000103 if mode == Bp2Build || mode == ApiBp2build {
Jingwen Chen49109762021-05-25 05:16:48 +0000104 content = `# READ THIS FIRST:
105# This file was automatically generated by bp2build for the Bazel migration project.
106# Feel free to edit or test it, but do *not* check it into your version control system.
107`
Jingwen Chen1c231732021-02-05 09:38:15 -0500108 content += targets.LoadStatements()
Sasha Smundak8bea2672022-08-04 13:31:14 -0700109 content += "\n\n"
110 // Get package rule from the handcrafted BUILD file, otherwise emit the default one.
111 prText := "package(default_visibility = [\"//visibility:public\"])\n"
112 if pr := targets.packageRule(); pr != nil {
113 prText = pr.content
114 }
115 content += prText
Jingwen Chen49109762021-05-25 05:16:48 +0000116 } else if mode == QueryView {
117 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800118 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500119 if content != "" {
120 // If there are load statements, add a couple of newlines.
121 content += "\n\n"
122 }
123 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500124 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800125 }
126 return files
127}
128
129func newFile(dir, basename, content string) BazelFile {
130 return BazelFile{
131 Dir: dir,
132 Basename: basename,
133 Contents: content,
134 }
135}
136
137const (
138 bazelRulesSubDir = "build/bazel/queryview_rules"
139
140 // additional files:
141 // * workspace file
142 // * base BUILD file
143 // * rules BUILD file
144 // * rules providers.bzl file
145 // * rules soong_module.bzl file
146 numAdditionalFiles = 5
147)
148
149var (
150 // Certain module property names are blocklisted/ignored here, for the reasons commented.
151 ignoredPropNames = map[string]bool{
Sam Delmerico263efde2022-09-08 10:43:42 -0400152 "name": true, // redundant, since this is explicitly generated for every target
153 "from": true, // reserved keyword
154 "in": true, // reserved keyword
155 "size": true, // reserved for tests
156 "arch": true, // interface prop type is not supported yet.
157 "multilib": true, // interface prop type is not supported yet.
158 "target": true, // interface prop type is not supported yet.
159 "visibility": true, // Bazel has native visibility semantics. Handle later.
160 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
161 "for": true, // reserved keyword, b/233579439
162 "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800163 }
164)
165
166func shouldGenerateAttribute(prop string) bool {
167 return !ignoredPropNames[prop]
168}
169
170func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400171 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800172 // Skip unexported fields. Some properties are
173 // internal to Soong only, and these fields do not have PkgPath.
174 return true
175 }
Sasha Smundak8bea2672022-08-04 13:31:14 -0700176 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc.
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800177 // but cannot be set in a .bp file
178 if proptools.HasTag(field, "blueprint", "mutated") {
179 return true
180 }
181 return false
182}
183
184// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
185// testonly = True, forcing other rules that depend on _test rules to also be
186// marked as testonly = True. This semantic constraint is not present in Soong.
187// To work around, rename "*_test" rules to "*_test_".
188func canonicalizeModuleType(moduleName string) string {
189 if strings.HasSuffix(moduleName, "_test") {
190 return moduleName + "_"
191 }
192
193 return moduleName
194}