blob: 6eb93bcd5be8b660e021276a39357ac2ce0ad324 [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
Jingwen Chenbf61afb2021-05-06 13:31:18 +000058 return files
59}
60
Liz Kammer2dd9ca42020-11-25 16:06:39 -080061func CreateBazelFiles(
Sasha Smundak0fd93e02022-05-19 19:34:31 -070062 cfg android.Config,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080063 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050064 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050065 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080066
Jingwen Chen6c309cd2021-04-01 07:11:11 +000067 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068
Jingwen Chen33832f92021-01-24 22:55:54 -050069 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000070 // Write top level WORKSPACE.
71 files = append(files, newFile("", "WORKSPACE", ""))
72
Jingwen Chen12b4c272021-03-10 02:05:59 -050073 // Used to denote that the top level directory is a package.
74 files = append(files, newFile("", GeneratedBuildFileName, ""))
75
76 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
77
Jingwen Chen73850672020-12-14 08:25:34 -050078 // These files are only used for queryview.
79 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
80
81 for bzlFileName, ruleShim := range ruleShims {
82 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
83 }
84 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080085 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086
Cole Faust324a92e2022-08-23 15:29:05 -070087 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080088
89 return files
90}
91
Cole Faust324a92e2022-08-23 15:29:05 -070092func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080093 files := make([]BazelFile, 0, len(buildToTargets))
94 for _, dir := range android.SortedStringKeys(buildToTargets) {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080095 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000096 targets.sort()
97
98 var content string
Spandan Das5af0bd32022-09-28 20:43:08 +000099 if mode == Bp2Build || mode == ApiBp2build {
Jingwen Chen49109762021-05-25 05:16:48 +0000100 content = `# READ THIS FIRST:
101# This file was automatically generated by bp2build for the Bazel migration project.
102# Feel free to edit or test it, but do *not* check it into your version control system.
103`
Jingwen Chen1c231732021-02-05 09:38:15 -0500104 content += targets.LoadStatements()
Sasha Smundak8bea2672022-08-04 13:31:14 -0700105 content += "\n\n"
106 // Get package rule from the handcrafted BUILD file, otherwise emit the default one.
107 prText := "package(default_visibility = [\"//visibility:public\"])\n"
108 if pr := targets.packageRule(); pr != nil {
109 prText = pr.content
110 }
111 content += prText
Jingwen Chen49109762021-05-25 05:16:48 +0000112 } else if mode == QueryView {
113 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800114 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500115 if content != "" {
116 // If there are load statements, add a couple of newlines.
117 content += "\n\n"
118 }
119 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500120 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800121 }
122 return files
123}
124
125func newFile(dir, basename, content string) BazelFile {
126 return BazelFile{
127 Dir: dir,
128 Basename: basename,
129 Contents: content,
130 }
131}
132
133const (
134 bazelRulesSubDir = "build/bazel/queryview_rules"
135
136 // additional files:
137 // * workspace file
138 // * base BUILD file
139 // * rules BUILD file
140 // * rules providers.bzl file
141 // * rules soong_module.bzl file
142 numAdditionalFiles = 5
143)
144
145var (
146 // Certain module property names are blocklisted/ignored here, for the reasons commented.
147 ignoredPropNames = map[string]bool{
Sam Delmerico263efde2022-09-08 10:43:42 -0400148 "name": true, // redundant, since this is explicitly generated for every target
149 "from": true, // reserved keyword
150 "in": true, // reserved keyword
151 "size": true, // reserved for tests
152 "arch": true, // interface prop type is not supported yet.
153 "multilib": true, // interface prop type is not supported yet.
154 "target": true, // interface prop type is not supported yet.
155 "visibility": true, // Bazel has native visibility semantics. Handle later.
156 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
157 "for": true, // reserved keyword, b/233579439
158 "versions_with_info": true, // TODO(b/245730552) struct properties not fully supported
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800159 }
160)
161
162func shouldGenerateAttribute(prop string) bool {
163 return !ignoredPropNames[prop]
164}
165
166func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400167 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800168 // Skip unexported fields. Some properties are
169 // internal to Soong only, and these fields do not have PkgPath.
170 return true
171 }
Sasha Smundak8bea2672022-08-04 13:31:14 -0700172 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc.
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800173 // but cannot be set in a .bp file
174 if proptools.HasTag(field, "blueprint", "mutated") {
175 return true
176 }
177 return false
178}
179
180// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
181// testonly = True, forcing other rules that depend on _test rules to also be
182// marked as testonly = True. This semantic constraint is not present in Soong.
183// To work around, rename "*_test" rules to "*_test_".
184func canonicalizeModuleType(moduleName string) string {
185 if strings.HasSuffix(moduleName, "_test") {
186 return moduleName + "_"
187 }
188
189 return moduleName
190}