blob: 4b18ad6e2cf12fb3f1fcbb11a87bc58e6028968a [file] [log] [blame]
Liz Kammerea6666f2021-02-17 10:17:28 -05001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
Liz Kammerba3ea162021-02-17 13:22:03 -050017import (
18 "fmt"
19 "io/ioutil"
20 "path/filepath"
21 "strings"
22
Liz Kammerbdc60992021-02-24 16:55:11 -050023 "github.com/google/blueprint"
Liz Kammerba3ea162021-02-17 13:22:03 -050024 "github.com/google/blueprint/proptools"
25)
26
27type bazelModuleProperties struct {
28 // The label of the Bazel target replacing this Soong module. When run in conversion mode, this
29 // will import the handcrafted build target into the autogenerated file. Note: this may result in
30 // a conflict due to duplicate targets if bp2build_available is also set.
31 Label *string
32
33 // If true, bp2build will generate the converted Bazel target for this module. Note: this may
34 // cause a conflict due to the duplicate targets if label is also set.
Jingwen Chen12b4c272021-03-10 02:05:59 -050035 //
36 // This is a bool pointer to support tristates: true, false, not set.
37 //
38 // To opt-in a module, set bazel_module: { bp2build_available: true }
39 // To opt-out a module, set bazel_module: { bp2build_available: false }
40 // To defer the default setting for the directory, do not set the value.
41 Bp2build_available *bool
Liz Kammerba3ea162021-02-17 13:22:03 -050042}
43
44// Properties contains common module properties for Bazel migration purposes.
45type properties struct {
46 // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
47 // this Soong module.
48 Bazel_module bazelModuleProperties
49}
Liz Kammerea6666f2021-02-17 10:17:28 -050050
51// BazelModuleBase contains the property structs with metadata for modules which can be converted to
52// Bazel.
53type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050054 bazelProperties properties
Liz Kammerea6666f2021-02-17 10:17:28 -050055}
56
57// Bazelable is specifies the interface for modules that can be converted to Bazel.
58type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -050059 bazelProps() *properties
60 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -050061 HandcraftedLabel() string
62 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Jingwen Chen12b4c272021-03-10 02:05:59 -050063 ConvertWithBp2build(ctx BazelConversionPathContext) bool
Liz Kammerba3ea162021-02-17 13:22:03 -050064 GetBazelBuildFileContents(c Config, path, name string) (string, error)
Jingwen Chen12b4c272021-03-10 02:05:59 -050065 ConvertedToBazel(ctx BazelConversionPathContext) bool
Liz Kammerea6666f2021-02-17 10:17:28 -050066}
67
68// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
69type BazelModule interface {
70 Module
71 Bazelable
72}
73
74// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
75// properties.
76func InitBazelModule(module BazelModule) {
77 module.AddProperties(module.bazelProps())
78}
79
80// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -050081func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -050082 return &b.bazelProperties
83}
84
Liz Kammerba3ea162021-02-17 13:22:03 -050085// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
86func (b *BazelModuleBase) HasHandcraftedLabel() bool {
87 return b.bazelProperties.Bazel_module.Label != nil
88}
89
90// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
91func (b *BazelModuleBase) HandcraftedLabel() string {
92 return proptools.String(b.bazelProperties.Bazel_module.Label)
93}
94
Liz Kammerea6666f2021-02-17 10:17:28 -050095// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -050096func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
97 if b.HasHandcraftedLabel() {
98 return b.HandcraftedLabel()
99 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500100 if b.ConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500101 return bp2buildModuleLabel(ctx, module)
102 }
103 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -0500104}
105
Jingwen Chen12b4c272021-03-10 02:05:59 -0500106// Configuration to decide if modules in a directory should default to true/false for bp2build_available
107type Bp2BuildConfig map[string]BazelConversionConfigEntry
108type BazelConversionConfigEntry int
109
110const (
Jingwen Chen91220d72021-03-24 02:18:33 -0400111 // A sentinel value to be used as a key in Bp2BuildConfig for modules with
112 // no package path. This is also the module dir for top level Android.bp
113 // modules.
114 BP2BUILD_TOPLEVEL = "."
115
Jingwen Chen12b4c272021-03-10 02:05:59 -0500116 // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
117 // which can also mean that the key doesn't exist in a lookup.
118
119 // all modules in this package and subpackages default to bp2build_available: true.
120 // allows modules to opt-out.
121 Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
122
123 // all modules in this package (not recursively) default to bp2build_available: false.
124 // allows modules to opt-in.
125 Bp2BuildDefaultFalse
126)
127
128var (
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400129 // Do not write BUILD files for these directories
130 // NOTE: this is not recursive
131 bp2buildDoNotWriteBuildFileList = []string{
132 // Don't generate these BUILD files - because external BUILD files already exist
133 "external/boringssl",
134 "external/brotli",
135 "external/dagger2",
136 "external/flatbuffers",
137 "external/gflags",
138 "external/google-fruit",
139 "external/grpc-grpc",
140 "external/grpc-grpc/test/core/util",
141 "external/grpc-grpc/test/cpp/common",
142 "external/grpc-grpc/third_party/address_sorting",
143 "external/nanopb-c",
144 "external/nos/host/generic",
145 "external/nos/host/generic/libnos",
146 "external/nos/host/generic/libnos/generator",
147 "external/nos/host/generic/libnos_datagram",
148 "external/nos/host/generic/libnos_transport",
149 "external/nos/host/generic/nugget/proto",
150 "external/perfetto",
151 "external/protobuf",
152 "external/rust/cxx",
153 "external/rust/cxx/demo",
154 "external/ruy",
155 "external/tensorflow",
156 "external/tensorflow/tensorflow/lite",
157 "external/tensorflow/tensorflow/lite/java",
158 "external/tensorflow/tensorflow/lite/kernels",
159 "external/tflite-support",
160 "external/tinyalsa_new",
161 "external/wycheproof",
162 "external/libyuv",
163 }
164
Jingwen Chen12b4c272021-03-10 02:05:59 -0500165 // Configure modules in these directories to enable bp2build_available: true or false by default.
166 bp2buildDefaultConfig = Bp2BuildConfig{
167 "bionic": Bp2BuildDefaultTrueRecursively,
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400168 "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
Jingwen Chen12b4c272021-03-10 02:05:59 -0500169 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
170 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
171 }
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000172
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400173 // Per-module denylist to always opt modules out of both bp2build and mixed builds.
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000174 bp2buildModuleDoNotConvertList = []string{
Liz Kammercc86ac82021-04-26 22:17:48 +0000175 "libBionicBenchmarksUtils", // ruperts@, cc_library_static, 'map' file not found
176 "libbionic_spawn_benchmark", // ruperts@, cc_library_static, depends on //system/libbase
177 "libc_jemalloc_wrapper", // ruperts@, cc_library_static, depends on //external/jemalloc_new
178 "libc_bootstrap", // ruperts@, cc_library_static, 'private/bionic_auxv.h' file not found
179 "libc_init_static", // ruperts@, cc_library_static, 'private/bionic_elf_tls.h' file not found
180 "libc_init_dynamic", // ruperts@, cc_library_static, 'private/bionic_defs.h' file not found
181 "libc_tzcode", // ruperts@, cc_library_static, error: expected expression
182 "libc_netbsd", // ruperts@, cc_library_static, 'engine.c' file not found
183 "libc_fortify", // ruperts@, cc_library_static, 'private/bionic_fortify.h' file not found
184 "libc_bionic", // ruperts@, cc_library_static, 'private/bionic_asm.h' file not found
185 "libc_bionic_ndk", // ruperts@, cc_library_static, depends on //bionic/libc/system_properties
186 "libc_bionic_systrace", // ruperts@, cc_library_static, 'private/bionic_systrace.h' file not found
187 "libc_pthread", // ruperts@, cc_library_static, 'private/bionic_defs.h' file not found
188 "libc_syscalls", // eakammer@, cc_library_static, 'private/bionic_asm.h' file not found
189 "libc_ndk", // ruperts@, cc_library_static, depends on //bionic/libm:libm
190 "libc_nopthread", // ruperts@, cc_library_static, depends on //external/arm-optimized-routines
191 "libc_common", // ruperts@, cc_library_static, depends on //bionic/libc:libc_nopthread
192 "libc_common_static", // ruperts@, cc_library_static, depends on //bionic/libc:libc_common
193 "libc_common_shared", // ruperts@, cc_library_static, depends on //bionic/libc:libc_common
194 "libc_unwind_static", // ruperts@, cc_library_static, 'private/bionic_elf_tls.h' file not found
195 "libc_nomalloc", // ruperts@, cc_library_static, depends on //bionic/libc:libc_common
196 "libasync_safe", // ruperts@, cc_library_static, 'private/CachedProperty.h' file not found
197 "libc_malloc_debug_backtrace", // ruperts@, cc_library_static, depends on //system/libbase
198 "libsystemproperties", // ruperts@, cc_library_static, depends on //system/core/property_service/libpropertyinfoparser
199 "libdl_static", // ruperts@, cc_library_static, 'private/CFIShadow.h' file not found
200 "liblinker_main", // ruperts@, cc_library_static, depends on //system/libbase
201 "liblinker_malloc", // ruperts@, cc_library_static, depends on //system/logging/liblog:liblog
202 "liblinker_debuggerd_stub", // ruperts@, cc_library_static, depends on //system/libbase
203 "libbionic_tests_headers_posix", // ruperts@, cc_library_static, 'complex.h' file not found
204 "libc_dns", // ruperts@, cc_library_static, 'private/android_filesystem_config.h' file not found
205 "libc_static_dispatch", // eakammer@, cc_library_static, 'private/bionic_asm.h' file not found
206 "libc_dynamic_dispatch", // eakammer@, cc_library_static, 'private/bionic_ifuncs.h' file not found
207 "note_memtag_heap_async", // jingwen@, cc_library_static, 'private/bionic_asm.h' file not found (arm64)
208 "note_memtag_heap_sync", // jingwen@, cc_library_static, 'private/bionic_asm.h' file not found (arm64)
Jingwen Chen0a92ed72021-04-12 05:37:42 +0000209
Liz Kammercc86ac82021-04-26 22:17:48 +0000210 // List of all full_cc_libraries in //bionic, with their immediate failures
211 "libc", // jingwen@, cc_library, depends on //external/gwp_asan
212 "libc_malloc_debug", // jingwen@, cc_library, fatal error: 'assert.h' file not found
213 "libc_malloc_hooks", // jingwen@, cc_library, fatal error: 'errno.h' file not found
214 "libdl", // jingwen@, cc_library, ld.lld: error: no input files
215 "libm", // lberki@, cc_library, compiler error: "Unexpected token in argument list"
216 "libseccomp_policy", // lberki@, cc_library, 'linux/filter.h' not found, caused by missing -isystem bionic/libc/kernel/uapi, dunno where it comes from in Soong
217 "libstdc++", // jingwen@, cc_library, depends on //external/gwp_asan
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400218 }
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400219
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400220 // Per-module denylist to opt modules out of mixed builds. Such modules will
221 // still be generated via bp2build.
222 mixedBuildsDisabledList = []string{
Jingwen Chene32e9e02021-04-23 09:17:24 +0000223 "libc_gdtoa", // ruperts@, cc_library_static, OK for bp2build but undefined symbol: __strtorQ for mixed builds
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000224 }
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000225
226 // Used for quicker lookups
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400227 bp2buildDoNotWriteBuildFile = map[string]bool{}
228 bp2buildModuleDoNotConvert = map[string]bool{}
229 mixedBuildsDisabled = map[string]bool{}
Jingwen Chen12b4c272021-03-10 02:05:59 -0500230)
231
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000232func init() {
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400233 for _, moduleName := range bp2buildDoNotWriteBuildFileList {
234 bp2buildDoNotWriteBuildFile[moduleName] = true
235 }
236
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000237 for _, moduleName := range bp2buildModuleDoNotConvertList {
238 bp2buildModuleDoNotConvert[moduleName] = true
239 }
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400240
241 for _, moduleName := range mixedBuildsDisabledList {
242 mixedBuildsDisabled[moduleName] = true
243 }
244}
245
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400246func ShouldWriteBuildFileForDir(dir string) bool {
247 if _, ok := bp2buildDoNotWriteBuildFile[dir]; ok {
248 return false
249 } else {
250 return true
251 }
252}
253
Chris Parsonsbab4d7e2021-04-15 17:27:08 -0400254// MixedBuildsEnabled checks that a module is ready to be replaced by a
255// converted or handcrafted Bazel target.
256func (b *BazelModuleBase) MixedBuildsEnabled(ctx BazelConversionPathContext) bool {
257 if !ctx.Config().BazelContext.BazelEnabled() {
258 return false
259 }
260 if len(b.GetBazelLabel(ctx, ctx.Module())) == 0 {
261 return false
262 }
263 return !mixedBuildsDisabled[ctx.Module().Name()]
Rupert Shuttleworth4f43fe92021-03-30 14:13:16 +0000264}
265
Liz Kammerea6666f2021-02-17 10:17:28 -0500266// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500267func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool {
Jingwen Chen5d72cba2021-03-25 09:28:38 +0000268 if bp2buildModuleDoNotConvert[ctx.Module().Name()] {
269 return false
270 }
271
Jingwen Chen12b4c272021-03-10 02:05:59 -0500272 // Ensure that the module type of this module has a bp2build converter. This
273 // prevents mixed builds from using auto-converted modules just by matching
274 // the package dir; it also has to have a bp2build mutator as well.
275 if ctx.Config().bp2buildModuleTypeConfig[ctx.ModuleType()] == false {
276 return false
277 }
278
279 packagePath := ctx.ModuleDir()
280 config := ctx.Config().bp2buildPackageConfig
281
282 // This is a tristate value: true, false, or unset.
283 propValue := b.bazelProperties.Bazel_module.Bp2build_available
284 if bp2buildDefaultTrueRecursively(packagePath, config) {
285 // Allow modules to explicitly opt-out.
286 return proptools.BoolDefault(propValue, true)
287 }
288
289 // Allow modules to explicitly opt-in.
290 return proptools.BoolDefault(propValue, false)
291}
292
293// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
294// set of package prefixes where all modules must be converted. That is, if the
295// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
296// return true.
297//
298// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
299// exactly, this module will return false early.
300//
301// This function will also return false if the package doesn't match anything in
302// the config.
303func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
304 ret := false
305
Jingwen Chen91220d72021-03-24 02:18:33 -0400306 // Return exact matches in the config.
307 if config[packagePath] == Bp2BuildDefaultTrueRecursively {
308 return true
309 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500310 if config[packagePath] == Bp2BuildDefaultFalse {
311 return false
312 }
313
Jingwen Chen91220d72021-03-24 02:18:33 -0400314 // If not, check for the config recursively.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500315 packagePrefix := ""
316 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
317 for _, part := range strings.Split(packagePath, "/") {
318 packagePrefix += part
319 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
320 // package contains this prefix and this prefix should convert all modules
321 return true
322 }
323 // Continue to the next part of the package dir.
324 packagePrefix += "/"
325 }
326
327 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500328}
Liz Kammerba3ea162021-02-17 13:22:03 -0500329
330// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
331// an error if there are errors reading the file.
332// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
333// something more targeted based on the rule type and target.
334func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500335 if !strings.Contains(b.HandcraftedLabel(), path) {
336 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500337 }
338 name = filepath.Join(path, name)
339 f, err := c.fs.Open(name)
340 if err != nil {
341 return "", err
342 }
343 defer f.Close()
344
345 data, err := ioutil.ReadAll(f)
346 if err != nil {
347 return "", err
348 }
349 return string(data[:]), nil
350}
Liz Kammerbdc60992021-02-24 16:55:11 -0500351
352// ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically
353// or manually
Jingwen Chen12b4c272021-03-10 02:05:59 -0500354func (b *BazelModuleBase) ConvertedToBazel(ctx BazelConversionPathContext) bool {
355 return b.ConvertWithBp2build(ctx) || b.HasHandcraftedLabel()
Liz Kammerbdc60992021-02-24 16:55:11 -0500356}