blob: 4246f7da3e36d325332e18cc056a87ad086721a9 [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
Liz Kammer2dd9ca42020-11-25 16:06:39 -080013 "github.com/google/blueprint/proptools"
14)
15
16type BazelFile struct {
17 Dir string
18 Basename string
19 Contents string
20}
21
Chris Parsons3b1f83d2021-10-14 14:08:38 -040022func CreateSoongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000023 var files []BazelFile
24
Jingwen Chenc63677b2021-06-17 05:43:19 +000025 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Sam Delmerico932c01c2022-03-25 16:33:26 +000026 files = append(files, newFile("cc_toolchain", "constants.bzl", cc_config.BazelCcToolchainVars(cfg)))
27
28 files = append(files, newFile("java_toolchain", GeneratedBuildFileName, "")) // Creates a //java_toolchain package.
29 files = append(files, newFile("java_toolchain", "constants.bzl", java_config.BazelJavaToolchainVars(cfg)))
Jingwen Chenbf61afb2021-05-06 13:31:18 +000030
Jingwen Chen61174502021-09-17 08:40:45 +000031 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000032
Jingwen Chen01812022021-11-19 14:29:43 +000033 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
34
Liz Kammere8303bd2022-02-16 09:02:48 -050035 files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
36
Jingwen Chen0ee88a62022-01-07 14:55:29 +000037 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
38 if err != nil {
39 panic(err)
40 }
41 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
42 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
Yu Liufc603162022-03-01 15:44:08 -080043 files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
Jingwen Chen0ee88a62022-01-07 14:55:29 +000044
Jingwen Chenbf61afb2021-05-06 13:31:18 +000045 return files
46}
47
Jingwen Chen61174502021-09-17 08:40:45 +000048func convertedModules(convertedModules []string) string {
49 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000050}
51
Liz Kammer2dd9ca42020-11-25 16:06:39 -080052func CreateBazelFiles(
Sasha Smundak0fd93e02022-05-19 19:34:31 -070053 cfg android.Config,
Liz Kammer2dd9ca42020-11-25 16:06:39 -080054 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050055 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050056 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057
Jingwen Chen6c309cd2021-04-01 07:11:11 +000058 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059
Jingwen Chen33832f92021-01-24 22:55:54 -050060 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000061 // Write top level WORKSPACE.
62 files = append(files, newFile("", "WORKSPACE", ""))
63
Jingwen Chen12b4c272021-03-10 02:05:59 -050064 // Used to denote that the top level directory is a package.
65 files = append(files, newFile("", GeneratedBuildFileName, ""))
66
67 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
68
Jingwen Chen73850672020-12-14 08:25:34 -050069 // These files are only used for queryview.
70 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
71
72 for bzlFileName, ruleShim := range ruleShims {
73 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
74 }
75 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080076 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080077
Sasha Smundak0fd93e02022-05-19 19:34:31 -070078 files = append(files, createBuildFiles(cfg, buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080079
80 return files
81}
82
Sasha Smundak0fd93e02022-05-19 19:34:31 -070083func createBuildFiles(cfg android.Config, buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080084 files := make([]BazelFile, 0, len(buildToTargets))
Sasha Smundak0fd93e02022-05-19 19:34:31 -070085 warnNotWriting := cfg.IsEnvTrue("BP2BUILD_VERBOSE")
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040087 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Sasha Smundak0fd93e02022-05-19 19:34:31 -070088 if warnNotWriting {
89 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
90 }
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040091 continue
92 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080093 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000094 targets.sort()
95
96 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -050097 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +000098 content = `# READ THIS FIRST:
99# This file was automatically generated by bp2build for the Bazel migration project.
100# Feel free to edit or test it, but do *not* check it into your version control system.
101`
102 if targets.hasHandcraftedTargets() {
103 // For BUILD files with both handcrafted and generated targets,
104 // don't hardcode actual content, like package() declarations.
105 // Leave that responsibility to the checked-in BUILD file
106 // instead.
107 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
108 } else {
109 // For fully-generated BUILD files, hardcode the default visibility.
110 content += "package(default_visibility = [\"//visibility:public\"])"
111 }
112 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -0500113 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +0000114 } else if mode == QueryView {
115 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800116 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500117 if content != "" {
118 // If there are load statements, add a couple of newlines.
119 content += "\n\n"
120 }
121 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500122 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800123 }
124 return files
125}
126
127func newFile(dir, basename, content string) BazelFile {
128 return BazelFile{
129 Dir: dir,
130 Basename: basename,
131 Contents: content,
132 }
133}
134
135const (
136 bazelRulesSubDir = "build/bazel/queryview_rules"
137
138 // additional files:
139 // * workspace file
140 // * base BUILD file
141 // * rules BUILD file
142 // * rules providers.bzl file
143 // * rules soong_module.bzl file
144 numAdditionalFiles = 5
145)
146
147var (
148 // Certain module property names are blocklisted/ignored here, for the reasons commented.
149 ignoredPropNames = map[string]bool{
150 "name": true, // redundant, since this is explicitly generated for every target
151 "from": true, // reserved keyword
152 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500153 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800154 "arch": true, // interface prop type is not supported yet.
155 "multilib": true, // interface prop type is not supported yet.
156 "target": true, // interface prop type is not supported yet.
157 "visibility": true, // Bazel has native visibility semantics. Handle later.
158 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
Jingwen Chend9d096e2022-05-23 09:38:39 +0000159 "for": true, // reserved keyword, b/233579439
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 }
173 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
174 // 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}