blob: 3ab846b41df9cd58a83a1c6937198b070bb0eb52 [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)))
39
Jingwen Chenbf61afb2021-05-06 13:31:18 +000040 return files
41}
42
Jingwen Chen61174502021-09-17 08:40:45 +000043func convertedModules(convertedModules []string) string {
44 return strings.Join(convertedModules, "\n")
Jingwen Chenc63677b2021-06-17 05:43:19 +000045}
46
Liz Kammer2dd9ca42020-11-25 16:06:39 -080047func CreateBazelFiles(
48 ruleShims map[string]RuleShim,
Jingwen Chen40067de2021-01-26 21:58:43 -050049 buildToTargets map[string]BazelTargets,
Jingwen Chen33832f92021-01-24 22:55:54 -050050 mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080051
Jingwen Chen6c309cd2021-04-01 07:11:11 +000052 var files []BazelFile
Liz Kammer2dd9ca42020-11-25 16:06:39 -080053
Jingwen Chen33832f92021-01-24 22:55:54 -050054 if mode == QueryView {
Jingwen Chen6c309cd2021-04-01 07:11:11 +000055 // Write top level WORKSPACE.
56 files = append(files, newFile("", "WORKSPACE", ""))
57
Jingwen Chen12b4c272021-03-10 02:05:59 -050058 // Used to denote that the top level directory is a package.
59 files = append(files, newFile("", GeneratedBuildFileName, ""))
60
61 files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
62
Jingwen Chen73850672020-12-14 08:25:34 -050063 // These files are only used for queryview.
64 files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
65
66 for bzlFileName, ruleShim := range ruleShims {
67 files = append(files, newFile(bazelRulesSubDir, bzlFileName+".bzl", ruleShim.content))
68 }
69 files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080070 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080071
Jingwen Chen33832f92021-01-24 22:55:54 -050072 files = append(files, createBuildFiles(buildToTargets, mode)...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080073
74 return files
75}
76
Jingwen Chen40067de2021-01-26 21:58:43 -050077func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080078 files := make([]BazelFile, 0, len(buildToTargets))
79 for _, dir := range android.SortedStringKeys(buildToTargets) {
Rupert Shuttleworth00960792021-05-12 21:20:13 -040080 if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040081 fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
82 continue
83 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080084 targets := buildToTargets[dir]
Jingwen Chen49109762021-05-25 05:16:48 +000085 targets.sort()
86
87 var content string
Jingwen Chen40067de2021-01-26 21:58:43 -050088 if mode == Bp2Build {
Jingwen Chen49109762021-05-25 05:16:48 +000089 content = `# READ THIS FIRST:
90# This file was automatically generated by bp2build for the Bazel migration project.
91# Feel free to edit or test it, but do *not* check it into your version control system.
92`
93 if targets.hasHandcraftedTargets() {
94 // For BUILD files with both handcrafted and generated targets,
95 // don't hardcode actual content, like package() declarations.
96 // Leave that responsibility to the checked-in BUILD file
97 // instead.
98 content += `# This file contains generated targets and handcrafted targets that are manually managed in the source tree.`
99 } else {
100 // For fully-generated BUILD files, hardcode the default visibility.
101 content += "package(default_visibility = [\"//visibility:public\"])"
102 }
103 content += "\n"
Jingwen Chen1c231732021-02-05 09:38:15 -0500104 content += targets.LoadStatements()
Jingwen Chen49109762021-05-25 05:16:48 +0000105 } else if mode == QueryView {
106 content = soongModuleLoad
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800107 }
Jingwen Chen40067de2021-01-26 21:58:43 -0500108 if content != "" {
109 // If there are load statements, add a couple of newlines.
110 content += "\n\n"
111 }
112 content += targets.String()
Liz Kammerba3ea162021-02-17 13:22:03 -0500113 files = append(files, newFile(dir, GeneratedBuildFileName, content))
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800114 }
115 return files
116}
117
118func newFile(dir, basename, content string) BazelFile {
119 return BazelFile{
120 Dir: dir,
121 Basename: basename,
122 Contents: content,
123 }
124}
125
126const (
127 bazelRulesSubDir = "build/bazel/queryview_rules"
128
129 // additional files:
130 // * workspace file
131 // * base BUILD file
132 // * rules BUILD file
133 // * rules providers.bzl file
134 // * rules soong_module.bzl file
135 numAdditionalFiles = 5
136)
137
138var (
139 // Certain module property names are blocklisted/ignored here, for the reasons commented.
140 ignoredPropNames = map[string]bool{
141 "name": true, // redundant, since this is explicitly generated for every target
142 "from": true, // reserved keyword
143 "in": true, // reserved keyword
Jingwen Chen88ae4082021-02-24 19:55:50 -0500144 "size": true, // reserved for tests
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800145 "arch": true, // interface prop type is not supported yet.
146 "multilib": true, // interface prop type is not supported yet.
147 "target": true, // interface prop type is not supported yet.
148 "visibility": true, // Bazel has native visibility semantics. Handle later.
149 "features": true, // There is already a built-in attribute 'features' which cannot be overridden.
150 }
151)
152
153func shouldGenerateAttribute(prop string) bool {
154 return !ignoredPropNames[prop]
155}
156
157func shouldSkipStructField(field reflect.StructField) bool {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400158 if field.PkgPath != "" && !field.Anonymous {
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800159 // Skip unexported fields. Some properties are
160 // internal to Soong only, and these fields do not have PkgPath.
161 return true
162 }
163 // fields with tag `blueprint:"mutated"` are exported to enable modification in mutators, etc
164 // but cannot be set in a .bp file
165 if proptools.HasTag(field, "blueprint", "mutated") {
166 return true
167 }
168 return false
169}
170
171// FIXME(b/168089390): In Bazel, rules ending with "_test" needs to be marked as
172// testonly = True, forcing other rules that depend on _test rules to also be
173// marked as testonly = True. This semantic constraint is not present in Soong.
174// To work around, rename "*_test" rules to "*_test_".
175func canonicalizeModuleType(moduleName string) string {
176 if strings.HasSuffix(moduleName, "_test") {
177 return moduleName + "_"
178 }
179
180 return moduleName
181}