blob: 0a86a79c5a1f1cf1d00cb066264463edc27ac1ac [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001package bp2build
2
3import (
4 "android/soong/android"
Jingwen Chenbf61afb2021-05-06 13:31:18 +00005 "android/soong/cc/config"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -04006 "fmt"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08007 "reflect"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08008 "strings"
9
10 "github.com/google/blueprint/proptools"
11)
12
13type BazelFile struct {
14 Dir string
15 Basename string
16 Contents string
17}
18
Jingwen Chen61174502021-09-17 08:40:45 +000019func CreateSoongInjectionFiles(metrics CodegenMetrics) []BazelFile {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000020 var files []BazelFile
21
Jingwen Chenc63677b2021-06-17 05:43:19 +000022 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Jingwen Chenbf61afb2021-05-06 13:31:18 +000023 files = append(files, newFile("cc_toolchain", "constants.bzl", config.BazelCcToolchainVars()))
24
Jingwen Chen61174502021-09-17 08:40:45 +000025 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000026
Jingwen Chenbf61afb2021-05-06 13:31:18 +000027 return files
28}
29
Jingwen Chen61174502021-09-17 08:40:45 +000030func convertedModules(convertedModules []string) string {
31 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000032}
33
Liz Kammer2dd9ca42020-11-25 16:06:39 -080034func CreateBazelFiles(
35 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050036 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050037 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080038
Jingwen Chen6c309cd2021-04-01 07:11:11 +000039 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080040
Jingwen Chen33832f92021-01-24 22:55:54 -050041 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000042 // Write top level WORKSPACE.
43 files = append(files, newFile("", "WORKSPACE", ""))
44
Jingwen Chen12b4c272021-03-10 02:05:59 -050045 // Used to denote that the top level directory is a package.
46 files = append(files, newFile("", GeneratedBuildFileName, ""))
47
48 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
49
Jingwen Chen73850672020-12-14 08:25:34 -050050 // These files are only used for queryview.
51 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
52
53 for bzlFileName, ruleShim := range ruleShims {
54 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
55 }
56 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080058
Jingwen Chen33832f92021-01-24 22:55:54 -050059 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080060
61 return files
62}
63
Jingwen Chen40067de2021-01-26 21:58:43 -050064func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065 files := make([]BazelFile, 0, len(buildToTargets))
66 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040067 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040068 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
69 continue
70 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000072 targets.sort()
73
74 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -050075 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +000076 content = `# READ THIS FIRST:
77# This file was automatically generated by bp2build for the Bazel migration project.
78# Feel free to edit or test it, but do *not* check it into your version control system.
79`
80 if targets.hasHandcraftedTargets() {
81 // For BUILD files with both handcrafted and generated targets,
82 // don't hardcode actual content, like package() declarations.
83 // Leave that responsibility to the checked-in BUILD file
84 // instead.
85 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
86 } else {
87 // For fully-generated BUILD files, hardcode the default visibility.
88 content += "package(default_visibility = [\"//visibility:public\"])"
89 }
90 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -050091 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +000092 } else if mode == QueryView {
93 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -080094 }
Jingwen Chen40067de2021-01-26 21:58:43 -050095 if content != "" {
96 // If there are load statements, add a couple of newlines.
97 content += "\n\n"
98 }
99 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500100 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800101 }
102 return files
103}
104
105func newFile(dir, basename, content string) BazelFile {
106 return BazelFile{
107 Dir: dir,
108 Basename: basename,
109 Contents: content,
110 }
111}
112
113const (
114 bazelRulesSubDir = "build/bazel/queryview_rules"
115
116 // additional files:
117 // * workspace file
118 // * base BUILD file
119 // * rules BUILD file
120 // * rules providers.bzl file
121 // * rules soong_module.bzl file
122 numAdditionalFiles = 5
123)
124
125var (
126 // Certain module property names are blocklisted/ignored here, for the reasons commented.
127 ignoredPropNames = map[string]bool{
128 "name": true, // redundant, since this is explicitly generated for every target
129 "from": true, // reserved keyword
130 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500131 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800132 "arch": true, // interface prop type is not supported yet.
133 "multilib": true, // interface prop type is not supported yet.
134 "target": true, // interface prop type is not supported yet.
135 "visibility": true, // Bazel has native visibility semantics. Handle later.
136 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
137 }
138)
139
140func shouldGenerateAttribute(prop string) bool {
141 return !ignoredPropNames[prop]
142}
143
144func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400145 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800146 // Skip unexported fields. Some properties are
147 // internal to Soong only, and these fields do not have PkgPath.
148 return true
149 }
150 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
151 // but cannot be set in a .bp file
152 if proptools.HasTag(field, "blueprint", "mutated") {
153 return true
154 }
155 return false
156}
157
158// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
159// testonly = True, forcing other rules that depend on _test rules to also be
160// marked as testonly = True. This semantic constraint is not present in Soong.
161// To work around, rename "*_test" rules to "*_test_".
162func canonicalizeModuleType(moduleName string) string {
163 if strings.HasSuffix(moduleName, "_test") {
164 return moduleName + "_"
165 }
166
167 return moduleName
168}