blob: 91e614d23ff4da0c4d4f206e447c26d1d2f4c57d [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"
10 "android/soong/cc/config"
11
Liz Kammer2dd9ca42020-11-25 16:06:39 -080012 "github.com/google/blueprint/proptools"
13)
14
15type BazelFile struct {
16 Dir string
17 Basename string
18 Contents string
19}
20
Chris Parsons3b1f83d2021-10-14 14:08:38 -040021func CreateSoongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000022 var files []BazelFile
23
Jingwen Chenc63677b2021-06-17 05:43:19 +000024 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Chris Parsons3b1f83d2021-10-14 14:08:38 -040025 files = append(files, newFile("cc_toolchain", "constants.bzl", config.BazelCcToolchainVars(cfg)))
Jingwen Chenbf61afb2021-05-06 13:31:18 +000026
Jingwen Chen61174502021-09-17 08:40:45 +000027 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000028
Jingwen Chen01812022021-11-19 14:29:43 +000029 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
30
Liz Kammere8303bd2022-02-16 09:02:48 -050031 files = append(files, newFile("product_config", "arch_configuration.bzl", android.StarlarkArchConfigurations()))
32
Jingwen Chen0ee88a62022-01-07 14:55:29 +000033 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
34 if err != nil {
35 panic(err)
36 }
37 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
38 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
Yu Liufc603162022-03-01 15:44:08 -080039 files = append(files, newFile("api_levels", "api_levels.bzl", android.StarlarkApiLevelConfigs(cfg)))
Jingwen Chen0ee88a62022-01-07 14:55:29 +000040
Jingwen Chenbf61afb2021-05-06 13:31:18 +000041 return files
42}
43
Jingwen Chen61174502021-09-17 08:40:45 +000044func convertedModules(convertedModules []string) string {
45 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000046}
47
Liz Kammer2dd9ca42020-11-25 16:06:39 -080048func CreateBazelFiles(
49 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050050 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050051 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080052
Jingwen Chen6c309cd2021-04-01 07:11:11 +000053 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080054
Jingwen Chen33832f92021-01-24 22:55:54 -050055 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000056 // Write top level WORKSPACE.
57 files = append(files, newFile("", "WORKSPACE", ""))
58
Jingwen Chen12b4c272021-03-10 02:05:59 -050059 // Used to denote that the top level directory is a package.
60 files = append(files, newFile("", GeneratedBuildFileName, ""))
61
62 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
63
Jingwen Chen73850672020-12-14 08:25:34 -050064 // These files are only used for queryview.
65 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
66
67 for bzlFileName, ruleShim := range ruleShims {
68 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
69 }
70 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080072
Jingwen Chen33832f92021-01-24 22:55:54 -050073 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080074
75 return files
76}
77
Jingwen Chen40067de2021-01-26 21:58:43 -050078func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080079 files := make([]BazelFile, 0, len(buildToTargets))
80 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040081 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040082 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
83 continue
84 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080085 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000086 targets.sort()
87
88 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -050089 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +000090 content = `# READ THIS FIRST:
91# This file was automatically generated by bp2build for the Bazel migration project.
92# Feel free to edit or test it, but do *not* check it into your version control system.
93`
94 if targets.hasHandcraftedTargets() {
95 // For BUILD files with both handcrafted and generated targets,
96 // don't hardcode actual content, like package() declarations.
97 // Leave that responsibility to the checked-in BUILD file
98 // instead.
99 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
100 } else {
101 // For fully-generated BUILD files, hardcode the default visibility.
102 content += "package(default_visibility = [\"//visibility:public\"])"
103 }
104 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -0500105 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +0000106 } else if mode == QueryView {
107 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800108 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500109 if content != "" {
110 // If there are load statements, add a couple of newlines.
111 content += "\n\n"
112 }
113 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500114 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800115 }
116 return files
117}
118
119func newFile(dir, basename, content string) BazelFile {
120 return BazelFile{
121 Dir: dir,
122 Basename: basename,
123 Contents: content,
124 }
125}
126
127const (
128 bazelRulesSubDir = "build/bazel/queryview_rules"
129
130 // additional files:
131 // * workspace file
132 // * base BUILD file
133 // * rules BUILD file
134 // * rules providers.bzl file
135 // * rules soong_module.bzl file
136 numAdditionalFiles = 5
137)
138
139var (
140 // Certain module property names are blocklisted/ignored here, for the reasons commented.
141 ignoredPropNames = map[string]bool{
142 "name": true, // redundant, since this is explicitly generated for every target
143 "from": true, // reserved keyword
144 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500145 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800146 "arch": true, // interface prop type is not supported yet.
147 "multilib": true, // interface prop type is not supported yet.
148 "target": true, // interface prop type is not supported yet.
149 "visibility": true, // Bazel has native visibility semantics. Handle later.
150 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
151 }
152)
153
154func shouldGenerateAttribute(prop string) bool {
155 return !ignoredPropNames[prop]
156}
157
158func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400159 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800160 // Skip unexported fields. Some properties are
161 // internal to Soong only, and these fields do not have PkgPath.
162 return true
163 }
164 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
165 // but cannot be set in a .bp file
166 if proptools.HasTag(field, "blueprint", "mutated") {
167 return true
168 }
169 return false
170}
171
172// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
173// testonly = True, forcing other rules that depend on _test rules to also be
174// marked as testonly = True. This semantic constraint is not present in Soong.
175// To work around, rename "*_test" rules to "*_test_".
176func canonicalizeModuleType(moduleName string) string {
177 if strings.HasSuffix(moduleName, "_test") {
178 return moduleName + "_"
179 }
180
181 return moduleName
182}