blob: 96c12d3e13442cdf0e6c9d53336f6437e0afe7d4 [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
Jingwen Chen0ee88a62022-01-07 14:55:29 +000031 apiLevelsContent, err := json.Marshal(android.GetApiLevelsMap(cfg))
32 if err != nil {
33 panic(err)
34 }
35 files = append(files, newFile("api_levels", GeneratedBuildFileName, `exports_files(["api_levels.json"])`))
36 files = append(files, newFile("api_levels", "api_levels.json", string(apiLevelsContent)))
37
Jingwen Chenbf61afb2021-05-06 13:31:18 +000038 return files
39}
40
Jingwen Chen61174502021-09-17 08:40:45 +000041func convertedModules(convertedModules []string) string {
42 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000043}
44
Liz Kammer2dd9ca42020-11-25 16:06:39 -080045func CreateBazelFiles(
46 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050047 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050048 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080049
Jingwen Chen6c309cd2021-04-01 07:11:11 +000050 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080051
Jingwen Chen33832f92021-01-24 22:55:54 -050052 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000053 // Write top level WORKSPACE.
54 files = append(files, newFile("", "WORKSPACE", ""))
55
Jingwen Chen12b4c272021-03-10 02:05:59 -050056 // Used to denote that the top level directory is a package.
57 files = append(files, newFile("", GeneratedBuildFileName, ""))
58
59 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
60
Jingwen Chen73850672020-12-14 08:25:34 -050061 // These files are only used for queryview.
62 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
63
64 for bzlFileName, ruleShim := range ruleShims {
65 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
66 }
67 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080069
Jingwen Chen33832f92021-01-24 22:55:54 -050070 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071
72 return files
73}
74
Jingwen Chen40067de2021-01-26 21:58:43 -050075func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080076 files := make([]BazelFile, 0, len(buildToTargets))
77 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040078 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040079 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
80 continue
81 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080082 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000083 targets.sort()
84
85 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -050086 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +000087 content = `# READ THIS FIRST:
88# This file was automatically generated by bp2build for the Bazel migration project.
89# Feel free to edit or test it, but do *not* check it into your version control system.
90`
91 if targets.hasHandcraftedTargets() {
92 // For BUILD files with both handcrafted and generated targets,
93 // don't hardcode actual content, like package() declarations.
94 // Leave that responsibility to the checked-in BUILD file
95 // instead.
96 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
97 } else {
98 // For fully-generated BUILD files, hardcode the default visibility.
99 content += "package(default_visibility = [\"//visibility:public\"])"
100 }
101 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -0500102 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +0000103 } else if mode == QueryView {
104 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800105 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500106 if content != "" {
107 // If there are load statements, add a couple of newlines.
108 content += "\n\n"
109 }
110 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500111 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800112 }
113 return files
114}
115
116func newFile(dir, basename, content string) BazelFile {
117 return BazelFile{
118 Dir: dir,
119 Basename: basename,
120 Contents: content,
121 }
122}
123
124const (
125 bazelRulesSubDir = "build/bazel/queryview_rules"
126
127 // additional files:
128 // * workspace file
129 // * base BUILD file
130 // * rules BUILD file
131 // * rules providers.bzl file
132 // * rules soong_module.bzl file
133 numAdditionalFiles = 5
134)
135
136var (
137 // Certain module property names are blocklisted/ignored here, for the reasons commented.
138 ignoredPropNames = map[string]bool{
139 "name": true, // redundant, since this is explicitly generated for every target
140 "from": true, // reserved keyword
141 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500142 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800143 "arch": true, // interface prop type is not supported yet.
144 "multilib": true, // interface prop type is not supported yet.
145 "target": true, // interface prop type is not supported yet.
146 "visibility": true, // Bazel has native visibility semantics. Handle later.
147 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
148 }
149)
150
151func shouldGenerateAttribute(prop string) bool {
152 return !ignoredPropNames[prop]
153}
154
155func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400156 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800157 // Skip unexported fields. Some properties are
158 // internal to Soong only, and these fields do not have PkgPath.
159 return true
160 }
161 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
162 // but cannot be set in a .bp file
163 if proptools.HasTag(field, "blueprint", "mutated") {
164 return true
165 }
166 return false
167}
168
169// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
170// testonly = True, forcing other rules that depend on _test rules to also be
171// marked as testonly = True. This semantic constraint is not present in Soong.
172// To work around, rename "*_test" rules to "*_test_".
173func canonicalizeModuleType(moduleName string) string {
174 if strings.HasSuffix(moduleName, "_test") {
175 return moduleName + "_"
176 }
177
178 return moduleName
179}