blob: bced4c19fbb4ddb96a18a90878efcf0d8d6e5d36 [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 Chenbf61afb2021-05-06 13:31:18 +000019func CreateSoongInjectionFiles() []BazelFile {
20 var files []BazelFile
21
22 files = append(files, newFile("cc_toolchain", "BUILD", "")) // Creates a //cc_toolchain package.
23 files = append(files, newFile("cc_toolchain", "constants.bzl", config.BazelCcToolchainVars()))
24
25 return files
26}
27
Liz Kammer2dd9ca42020-11-25 16:06:39 -080028func CreateBazelFiles(
29 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050030 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050031 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032
Jingwen Chen6c309cd2021-04-01 07:11:11 +000033 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080034
Jingwen Chen33832f92021-01-24 22:55:54 -050035 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000036 // Write top level WORKSPACE.
37 files = append(files, newFile("", "WORKSPACE", ""))
38
Jingwen Chen12b4c272021-03-10 02:05:59 -050039 // Used to denote that the top level directory is a package.
40 files = append(files, newFile("", GeneratedBuildFileName, ""))
41
42 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
43
Jingwen Chen73850672020-12-14 08:25:34 -050044 // These files are only used for queryview.
45 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
46
47 for bzlFileName, ruleShim := range ruleShims {
48 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
49 }
50 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080051 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080052
Jingwen Chen33832f92021-01-24 22:55:54 -050053 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080054
55 return files
56}
57
Jingwen Chen40067de2021-01-26 21:58:43 -050058func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059 files := make([]BazelFile, 0, len(buildToTargets))
60 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040061 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040062 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
63 continue
64 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000066 targets.sort()
67
68 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -050069 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +000070 content = `# READ THIS FIRST:
71# This file was automatically generated by bp2build for the Bazel migration project.
72# Feel free to edit or test it, but do *not* check it into your version control system.
73`
74 if targets.hasHandcraftedTargets() {
75 // For BUILD files with both handcrafted and generated targets,
76 // don't hardcode actual content, like package() declarations.
77 // Leave that responsibility to the checked-in BUILD file
78 // instead.
79 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
80 } else {
81 // For fully-generated BUILD files, hardcode the default visibility.
82 content += "package(default_visibility = [\"//visibility:public\"])"
83 }
84 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -050085 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +000086 } else if mode == QueryView {
87 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -080088 }
Jingwen Chen40067de2021-01-26 21:58:43 -050089 if content != "" {
90 // If there are load statements, add a couple of newlines.
91 content += "\n\n"
92 }
93 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -050094 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080095 }
96 return files
97}
98
99func newFile(dir, basename, content string) BazelFile {
100 return BazelFile{
101 Dir: dir,
102 Basename: basename,
103 Contents: content,
104 }
105}
106
107const (
108 bazelRulesSubDir = "build/bazel/queryview_rules"
109
110 // additional files:
111 // * workspace file
112 // * base BUILD file
113 // * rules BUILD file
114 // * rules providers.bzl file
115 // * rules soong_module.bzl file
116 numAdditionalFiles = 5
117)
118
119var (
120 // Certain module property names are blocklisted/ignored here, for the reasons commented.
121 ignoredPropNames = map[string]bool{
122 "name": true, // redundant, since this is explicitly generated for every target
123 "from": true, // reserved keyword
124 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500125 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800126 "arch": true, // interface prop type is not supported yet.
127 "multilib": true, // interface prop type is not supported yet.
128 "target": true, // interface prop type is not supported yet.
129 "visibility": true, // Bazel has native visibility semantics. Handle later.
130 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
131 }
132)
133
134func shouldGenerateAttribute(prop string) bool {
135 return !ignoredPropNames[prop]
136}
137
138func shouldSkipStructField(field reflect.StructField) bool {
139 if field.PkgPath != "" {
140 // Skip unexported fields. Some properties are
141 // internal to Soong only, and these fields do not have PkgPath.
142 return true
143 }
144 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
145 // but cannot be set in a .bp file
146 if proptools.HasTag(field, "blueprint", "mutated") {
147 return true
148 }
149 return false
150}
151
152// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
153// testonly = True, forcing other rules that depend on _test rules to also be
154// marked as testonly = True. This semantic constraint is not present in Soong.
155// To work around, rename "*_test" rules to "*_test_".
156func canonicalizeModuleType(moduleName string) string {
157 if strings.HasSuffix(moduleName, "_test") {
158 return moduleName + "_"
159 }
160
161 return moduleName
162}