blob: 4d8b8a450638156dad344728e91ef3c8f11e2330 [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
Jingwen Chenc63677b2021-06-17 05:43:19 +000026 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Sam Delmerico932c01c2022-03-25 16:33:26 +000027 files = append(files, newFile("cc_toolchain", "constants.bzl", cc_config.BazelCcToolchainVars(cfg)))
28
29 files = append(files, newFile("java_toolchain", GeneratedBuildFileName, "")) // Creates a //java_toolchain package.
30 files = append(files, newFile("java_toolchain", "constants.bzl", java_config.BazelJavaToolchainVars(cfg)))
Jingwen Chenbf61afb2021-05-06 13:31:18 +000031
Jingwen Chen7810e172022-07-29 02:25:34 +000032 files = append(files, newFile("apex_toolchain", GeneratedBuildFileName, "")) // Creates a //apex_toolchain package.
33 files = append(files, newFile("apex_toolchain", "constants.bzl", apex.BazelApexToolchainVars()))
34
Jingwen Chen61174502021-09-17 08:40:45 +000035 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000036
Kevin Dagostino60f562a2022-09-20 03:54:47 +000037 convertedModulePathMap, err := json.MarshalIndent(metrics.convertedModulePathMap, "", "\t")
38 if err != nil {
39 panic(err)
40 }
41 files = append(files, newFile("metrics", "converted_modules_path_map.json", string(convertedModulePathMap)))
42
Jingwen Chen01812022021-11-19 14:29:43 +000043 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
44
Liz Kammere8303bd2022-02-16 09:02:48 -050045 files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
46
Jingwen Chen0ee88a62022-01-07 14:55:29 +000047 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
48 if err != nil {
49 panic(err)
50 }
51 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
52 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
Yu Liufc603162022-03-01 15:44:08 -080053 files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
Jingwen Chen0ee88a62022-01-07 14:55:29 +000054
Jingwen Chenbf61afb2021-05-06 13:31:18 +000055 return files
56}
57
Jingwen Chen61174502021-09-17 08:40:45 +000058func convertedModules(convertedModules []string) string {
59 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000060}
61
Liz Kammer2dd9ca42020-11-25 16:06:39 -080062func CreateBazelFiles(
Sasha Smundak0fd93e02022-05-19 19:34:31 -070063 cfg android.Config,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080064 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050065 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050066 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080067
Jingwen Chen6c309cd2021-04-01 07:11:11 +000068 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080069
Jingwen Chen33832f92021-01-24 22:55:54 -050070 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000071 // Write top level WORKSPACE.
72 files = append(files, newFile("", "WORKSPACE", ""))
73
Jingwen Chen12b4c272021-03-10 02:05:59 -050074 // Used to denote that the top level directory is a package.
75 files = append(files, newFile("", GeneratedBuildFileName, ""))
76
77 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
78
Jingwen Chen73850672020-12-14 08:25:34 -050079 // These files are only used for queryview.
80 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
81
82 for bzlFileName, ruleShim := range ruleShims {
83 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
84 }
85 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087
Cole Faust324a92e2022-08-23 15:29:05 -070088 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089
90 return files
91}
92
Cole Faust324a92e2022-08-23 15:29:05 -070093func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080094 files := make([]BazelFile, 0, len(buildToTargets))
95 for _, dir := range android.SortedStringKeys(buildToTargets) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080096 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000097 targets.sort()
98
99 var content string
Spandan Das5af0bd32022-09-28 20:43:08 +0000100 if mode == Bp2Build || mode == ApiBp2build {
Jingwen Chen49109762021-05-25 05:16:48 +0000101 content = `# READ THIS FIRST:
102# This file was automatically generated by bp2build for the Bazel migration project.
103# Feel free to edit or test it, but do *not* check it into your version control system.
104`
Jingwen Chen1c231732021-02-05 09:38:15 -0500105 content += targets.LoadStatements()
Sasha Smundak8bea2672022-08-04 13:31:14 -0700106 content += "\n\n"
107 // Get package rule from the handcrafted BUILD file, otherwise emit the default one.
108 prText := "package(default_visibility = [\"//visibility:public\"])\n"
109 if pr := targets.packageRule(); pr != nil {
110 prText = pr.content
111 }
112 content += prText
Jingwen Chen49109762021-05-25 05:16:48 +0000113 } else if mode == QueryView {
114 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800115 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500116 if content != "" {
117 // If there are load statements, add a couple of newlines.
118 content += "\n\n"
119 }
120 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500121 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800122 }
123 return files
124}
125
126func newFile(dir, basename, content string) BazelFile {
127 return BazelFile{
128 Dir: dir,
129 Basename: basename,
130 Contents: content,
131 }
132}
133
134const (
135 bazelRulesSubDir = "build/bazel/queryview_rules"
136
137 // additional files:
138 // * workspace file
139 // * base BUILD file
140 // * rules BUILD file
141 // * rules providers.bzl file
142 // * rules soong_module.bzl file
143 numAdditionalFiles = 5
144)
145
146var (
147 // Certain module property names are blocklisted/ignored here, for the reasons commented.
148 ignoredPropNames = map[string]bool{
Sam Delmerico263efde2022-09-08 10:43:42 -0400149 "name": true, // redundant, since this is explicitly generated for every target
150 "from": true, // reserved keyword
151 "in": true, // reserved keyword
152 "size": true, // reserved for tests
153 "arch": true, // interface prop type is not supported yet.
154 "multilib": true, // interface prop type is not supported yet.
155 "target": true, // interface prop type is not supported yet.
156 "visibility": true, // Bazel has native visibility semantics. Handle later.
157 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
158 "for": true, // reserved keyword, b/233579439
159 "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800160 }
161)
162
163func shouldGenerateAttribute(prop string) bool {
164 return !ignoredPropNames[prop]
165}
166
167func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400168 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800169 // Skip unexported fields. Some properties are
170 // internal to Soong only, and these fields do not have PkgPath.
171 return true
172 }
Sasha Smundak8bea2672022-08-04 13:31:14 -0700173 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc.
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800174 // but cannot be set in a .bp file
175 if proptools.HasTag(field, "blueprint", "mutated") {
176 return true
177 }
178 return false
179}
180
181// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
182// testonly = True, forcing other rules that depend on _test rules to also be
183// marked as testonly = True. This semantic constraint is not present in Soong.
184// To work around, rename "*_test" rules to "*_test_".
185func canonicalizeModuleType(moduleName string) string {
186 if strings.HasSuffix(moduleName, "_test") {
187 return moduleName + "_"
188 }
189
190 return moduleName
191}