blob: 81a4b26242a3644474b763cd84ceee2b1b98301d [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001package bp2build
2
3import (
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -04004 "fmt"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08005 "reflect"
Liz Kammer2dd9ca42020-11-25 16:06:39 -08006 "strings"
7
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +00008 "android/soong/android"
9 "android/soong/cc/config"
10
Liz Kammer2dd9ca42020-11-25 16:06:39 -080011 "github.com/google/blueprint/proptools"
12)
13
14type BazelFile struct {
15 Dir string
16 Basename string
17 Contents string
18}
19
Chris Parsons3b1f83d2021-10-14 14:08:38 -040020func CreateSoongInjectionFiles(cfg android.Config, metrics CodegenMetrics) []BazelFile {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000021 var files []BazelFile
22
Jingwen Chenc63677b2021-06-17 05:43:19 +000023 files = append(files, newFile("cc_toolchain", GeneratedBuildFileName, "")) // Creates a //cc_toolchain package.
Chris Parsons3b1f83d2021-10-14 14:08:38 -040024 files = append(files, newFile("cc_toolchain", "constants.bzl", config.BazelCcToolchainVars(cfg)))
Jingwen Chenbf61afb2021-05-06 13:31:18 +000025
Jingwen Chen61174502021-09-17 08:40:45 +000026 files = append(files, newFile("metrics", "converted_modules.txt", strings.Join(metrics.convertedModules, "\n")))
Jingwen Chenc63677b2021-06-17 05:43:19 +000027
Jingwen Chen01812022021-11-19 14:29:43 +000028 files = append(files, newFile("product_config", "soong_config_variables.bzl", cfg.Bp2buildSoongConfigDefinitions.String()))
29
Jingwen Chenbf61afb2021-05-06 13:31:18 +000030 return files
31}
32
Jingwen Chen61174502021-09-17 08:40:45 +000033func convertedModules(convertedModules []string) string {
34 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000035}
36
Liz Kammer2dd9ca42020-11-25 16:06:39 -080037func CreateBazelFiles(
38 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050039 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050040 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080041
Jingwen Chen6c309cd2021-04-01 07:11:11 +000042 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080043
Jingwen Chen33832f92021-01-24 22:55:54 -050044 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000045 // Write top level WORKSPACE.
46 files = append(files, newFile("", "WORKSPACE", ""))
47
Jingwen Chen12b4c272021-03-10 02:05:59 -050048 // Used to denote that the top level directory is a package.
49 files = append(files, newFile("", GeneratedBuildFileName, ""))
50
51 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
52
Jingwen Chen73850672020-12-14 08:25:34 -050053 // These files are only used for queryview.
54 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
55
56 for bzlFileName, ruleShim := range ruleShims {
57 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
58 }
59 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080060 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080061
Jingwen Chen33832f92021-01-24 22:55:54 -050062 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080063
64 return files
65}
66
Jingwen Chen40067de2021-01-26 21:58:43 -050067func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068 files := make([]BazelFile, 0, len(buildToTargets))
69 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040070 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040071 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
72 continue
73 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080074 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000075 targets.sort()
76
77 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -050078 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +000079 content = `# READ THIS FIRST:
80# This file was automatically generated by bp2build for the Bazel migration project.
81# Feel free to edit or test it, but do *not* check it into your version control system.
82`
83 if targets.hasHandcraftedTargets() {
84 // For BUILD files with both handcrafted and generated targets,
85 // don't hardcode actual content, like package() declarations.
86 // Leave that responsibility to the checked-in BUILD file
87 // instead.
88 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
89 } else {
90 // For fully-generated BUILD files, hardcode the default visibility.
91 content += "package(default_visibility = [\"//visibility:public\"])"
92 }
93 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -050094 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +000095 } else if mode == QueryView {
96 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -080097 }
Jingwen Chen40067de2021-01-26 21:58:43 -050098 if content != "" {
99 // If there are load statements, add a couple of newlines.
100 content += "\n\n"
101 }
102 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500103 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800104 }
105 return files
106}
107
108func newFile(dir, basename, content string) BazelFile {
109 return BazelFile{
110 Dir: dir,
111 Basename: basename,
112 Contents: content,
113 }
114}
115
116const (
117 bazelRulesSubDir = "build/bazel/queryview_rules"
118
119 // additional files:
120 // * workspace file
121 // * base BUILD file
122 // * rules BUILD file
123 // * rules providers.bzl file
124 // * rules soong_module.bzl file
125 numAdditionalFiles = 5
126)
127
128var (
129 // Certain module property names are blocklisted/ignored here, for the reasons commented.
130 ignoredPropNames = map[string]bool{
131 "name": true, // redundant, since this is explicitly generated for every target
132 "from": true, // reserved keyword
133 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500134 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800135 "arch": true, // interface prop type is not supported yet.
136 "multilib": true, // interface prop type is not supported yet.
137 "target": true, // interface prop type is not supported yet.
138 "visibility": true, // Bazel has native visibility semantics. Handle later.
139 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
140 }
141)
142
143func shouldGenerateAttribute(prop string) bool {
144 return !ignoredPropNames[prop]
145}
146
147func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400148 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800149 // Skip unexported fields. Some properties are
150 // internal to Soong only, and these fields do not have PkgPath.
151 return true
152 }
153 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
154 // but cannot be set in a .bp file
155 if proptools.HasTag(field, "blueprint", "mutated") {
156 return true
157 }
158 return false
159}
160
161// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
162// testonly = True, forcing other rules that depend on _test rules to also be
163// marked as testonly = True. This semantic constraint is not present in Soong.
164// To work around, rename "*_test" rules to "*_test_".
165func canonicalizeModuleType(moduleName string) string {
166 if strings.HasSuffix(moduleName, "_test") {
167 return moduleName + "_"
168 }
169
170 return moduleName
171}