blob: 8cf9beaf43faa723f2f116b9f48d0ab74400a2e8 [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001package bp2build
2
3import (
Jingwen Chen0ee88a62022-01-07 14:55:29 +00004 "encoding/json"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -04005 "fmt"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08006 "reflect"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08007 "strings"
8
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00009 "android/soong/android"
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
Chris Parsons3b1f83d2021-10-14 14:08:38 -040024func CreateSoongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000025 var files []BazelFile
26
Jingwen Chenc63677b2021-06-17 05:43:19 +000027 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Sam Delmerico932c01c2022-03-25 16:33:26 +000028 files = append(files, newFile("cc_toolchain", "constants.bzl", cc_config.BazelCcToolchainVars(cfg)))
29
30 files = append(files, newFile("java_toolchain", GeneratedBuildFileName, "")) // Creates a //java_toolchain package.
31 files = append(files, newFile("java_toolchain", "constants.bzl", java_config.BazelJavaToolchainVars(cfg)))
Jingwen Chenbf61afb2021-05-06 13:31:18 +000032
Jingwen Chen7810e172022-07-29 02:25:34 +000033 files = append(files, newFile("apex_toolchain", GeneratedBuildFileName, "")) // Creates a //apex_toolchain package.
34 files = append(files, newFile("apex_toolchain", "constants.bzl", apex.BazelApexToolchainVars()))
35
Jingwen Chen61174502021-09-17 08:40:45 +000036 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000037
Jingwen Chen01812022021-11-19 14:29:43 +000038 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
39
Liz Kammere8303bd2022-02-16 09:02:48 -050040 files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
41
Jingwen Chen0ee88a62022-01-07 14:55:29 +000042 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
43 if err != nil {
44 panic(err)
45 }
46 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
47 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
Yu Liufc603162022-03-01 15:44:08 -080048 files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
Jingwen Chen0ee88a62022-01-07 14:55:29 +000049
Jingwen Chenbf61afb2021-05-06 13:31:18 +000050 return files
51}
52
Jingwen Chen61174502021-09-17 08:40:45 +000053func convertedModules(convertedModules []string) string {
54 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000055}
56
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057func CreateBazelFiles(
Sasha Smundak0fd93e02022-05-19 19:34:31 -070058 cfg android.Config,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050060 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050061 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080062
Jingwen Chen6c309cd2021-04-01 07:11:11 +000063 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080064
Jingwen Chen33832f92021-01-24 22:55:54 -050065 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000066 // Write top level WORKSPACE.
67 files = append(files, newFile("", "WORKSPACE", ""))
68
Jingwen Chen12b4c272021-03-10 02:05:59 -050069 // Used to denote that the top level directory is a package.
70 files = append(files, newFile("", GeneratedBuildFileName, ""))
71
72 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
73
Jingwen Chen73850672020-12-14 08:25:34 -050074 // These files are only used for queryview.
75 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
76
77 for bzlFileName, ruleShim := range ruleShims {
78 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
79 }
80 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080082
Sasha Smundak0fd93e02022-05-19 19:34:31 -070083 files = append(files, createBuildFiles(cfg, buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080084
85 return files
86}
87
Sasha Smundak0fd93e02022-05-19 19:34:31 -070088func createBuildFiles(cfg android.Config, buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089 files := make([]BazelFile, 0, len(buildToTargets))
Sasha Smundak0fd93e02022-05-19 19:34:31 -070090 warnNotWriting := cfg.IsEnvTrue("BP2BUILD_VERBOSE")
Liz Kammer2dd9ca42020-11-25 16:06:39 -080091 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040092 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Sasha Smundak0fd93e02022-05-19 19:34:31 -070093 if warnNotWriting {
94 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
95 }
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040096 continue
97 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000099 targets.sort()
100
101 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -0500102 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +0000103 content = `# READ THIS FIRST:
104# This file was automatically generated by bp2build for the Bazel migration project.
105# Feel free to edit or test it, but do *not* check it into your version control system.
106`
107 if targets.hasHandcraftedTargets() {
108 // For BUILD files with both handcrafted and generated targets,
109 // don't hardcode actual content, like package() declarations.
110 // Leave that responsibility to the checked-in BUILD file
111 // instead.
112 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
113 } else {
114 // For fully-generated BUILD files, hardcode the default visibility.
115 content += "package(default_visibility = [\"//visibility:public\"])"
116 }
117 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -0500118 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +0000119 } else if mode == QueryView {
120 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800121 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500122 if content != "" {
123 // If there are load statements, add a couple of newlines.
124 content += "\n\n"
125 }
126 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500127 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800128 }
129 return files
130}
131
132func newFile(dir, basename, content string) BazelFile {
133 return BazelFile{
134 Dir: dir,
135 Basename: basename,
136 Contents: content,
137 }
138}
139
140const (
141 bazelRulesSubDir = "build/bazel/queryview_rules"
142
143 // additional files:
144 // * workspace file
145 // * base BUILD file
146 // * rules BUILD file
147 // * rules providers.bzl file
148 // * rules soong_module.bzl file
149 numAdditionalFiles = 5
150)
151
152var (
153 // Certain module property names are blocklisted/ignored here, for the reasons commented.
154 ignoredPropNames = map[string]bool{
155 "name": true, // redundant, since this is explicitly generated for every target
156 "from": true, // reserved keyword
157 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500158 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800159 "arch": true, // interface prop type is not supported yet.
160 "multilib": true, // interface prop type is not supported yet.
161 "target": true, // interface prop type is not supported yet.
162 "visibility": true, // Bazel has native visibility semantics. Handle later.
163 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
Jingwen Chend9d096e2022-05-23 09:38:39 +0000164 "for": true, // reserved keyword, b/233579439
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800165 }
166)
167
168func shouldGenerateAttribute(prop string) bool {
169 return !ignoredPropNames[prop]
170}
171
172func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400173 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800174 // Skip unexported fields. Some properties are
175 // internal to Soong only, and these fields do not have PkgPath.
176 return true
177 }
178 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
179 // but cannot be set in a .bp file
180 if proptools.HasTag(field, "blueprint", "mutated") {
181 return true
182 }
183 return false
184}
185
186// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
187// testonly = True, forcing other rules that depend on _test rules to also be
188// marked as testonly = True. This semantic constraint is not present in Soong.
189// To work around, rename "*_test" rules to "*_test_".
190func canonicalizeModuleType(moduleName string) string {
191 if strings.HasSuffix(moduleName, "_test") {
192 return moduleName + "_"
193 }
194
195 return moduleName
196}