bazel.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // 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. package android
  15. import (
  16. "bufio"
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "path/filepath"
  21. "strings"
  22. "github.com/google/blueprint"
  23. "github.com/google/blueprint/proptools"
  24. )
  25. type bazelModuleProperties struct {
  26. // The label of the Bazel target replacing this Soong module. When run in conversion mode, this
  27. // will import the handcrafted build target into the autogenerated file. Note: this may result in
  28. // a conflict due to duplicate targets if bp2build_available is also set.
  29. Label *string
  30. // If true, bp2build will generate the converted Bazel target for this module. Note: this may
  31. // cause a conflict due to the duplicate targets if label is also set.
  32. //
  33. // This is a bool pointer to support tristates: true, false, not set.
  34. //
  35. // To opt-in a module, set bazel_module: { bp2build_available: true }
  36. // To opt-out a module, set bazel_module: { bp2build_available: false }
  37. // To defer the default setting for the directory, do not set the value.
  38. Bp2build_available *bool
  39. // CanConvertToBazel is set via InitBazelModule to indicate that a module type can be converted to
  40. // Bazel with Bp2build.
  41. CanConvertToBazel bool `blueprint:"mutated"`
  42. }
  43. // Properties contains common module properties for Bazel migration purposes.
  44. type properties struct {
  45. // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
  46. // this Soong module.
  47. Bazel_module bazelModuleProperties
  48. }
  49. // namespacedVariableProperties is a map from a string representing a Soong
  50. // config variable namespace, like "android" or "vendor_name" to a slice of
  51. // pointer to a struct containing a single field called Soong_config_variables
  52. // whose value mirrors the structure in the Blueprint file.
  53. type namespacedVariableProperties map[string][]interface{}
  54. // BazelModuleBase contains the property structs with metadata for modules which can be converted to
  55. // Bazel.
  56. type BazelModuleBase struct {
  57. bazelProperties properties
  58. // namespacedVariableProperties is used for soong_config_module_type support
  59. // in bp2build. Soong config modules allow users to set module properties
  60. // based on custom product variables defined in Android.bp files. These
  61. // variables are namespaced to prevent clobbering, especially when set from
  62. // Makefiles.
  63. namespacedVariableProperties namespacedVariableProperties
  64. // baseModuleType is set when this module was created from a module type
  65. // defined by a soong_config_module_type. Every soong_config_module_type
  66. // "wraps" another module type, e.g. a soong_config_module_type can wrap a
  67. // cc_defaults to a custom_cc_defaults, or cc_binary to a custom_cc_binary.
  68. // This baseModuleType is set to the wrapped module type.
  69. baseModuleType string
  70. }
  71. // Bazelable is specifies the interface for modules that can be converted to Bazel.
  72. type Bazelable interface {
  73. bazelProps() *properties
  74. HasHandcraftedLabel() bool
  75. HandcraftedLabel() string
  76. GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
  77. ShouldConvertWithBp2build(ctx BazelConversionContext) bool
  78. shouldConvertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool
  79. GetBazelBuildFileContents(c Config, path, name string) (string, error)
  80. ConvertWithBp2build(ctx TopDownMutatorContext)
  81. // namespacedVariableProps is a map from a soong config variable namespace
  82. // (e.g. acme, android) to a map of interfaces{}, which are really
  83. // reflect.Struct pointers, representing the value of the
  84. // soong_config_variables property of a module. The struct pointer is the
  85. // one with the single member called Soong_config_variables, which itself is
  86. // a struct containing fields for each supported feature in that namespace.
  87. //
  88. // The reason for using an slice of interface{} is to support defaults
  89. // propagation of the struct pointers.
  90. namespacedVariableProps() namespacedVariableProperties
  91. setNamespacedVariableProps(props namespacedVariableProperties)
  92. BaseModuleType() string
  93. SetBaseModuleType(baseModuleType string)
  94. }
  95. // BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
  96. type BazelModule interface {
  97. Module
  98. Bazelable
  99. }
  100. // InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
  101. // properties.
  102. func InitBazelModule(module BazelModule) {
  103. module.AddProperties(module.bazelProps())
  104. module.bazelProps().Bazel_module.CanConvertToBazel = true
  105. }
  106. // bazelProps returns the Bazel properties for the given BazelModuleBase.
  107. func (b *BazelModuleBase) bazelProps() *properties {
  108. return &b.bazelProperties
  109. }
  110. func (b *BazelModuleBase) namespacedVariableProps() namespacedVariableProperties {
  111. return b.namespacedVariableProperties
  112. }
  113. func (b *BazelModuleBase) setNamespacedVariableProps(props namespacedVariableProperties) {
  114. b.namespacedVariableProperties = props
  115. }
  116. func (b *BazelModuleBase) BaseModuleType() string {
  117. return b.baseModuleType
  118. }
  119. func (b *BazelModuleBase) SetBaseModuleType(baseModuleType string) {
  120. b.baseModuleType = baseModuleType
  121. }
  122. // HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
  123. func (b *BazelModuleBase) HasHandcraftedLabel() bool {
  124. return b.bazelProperties.Bazel_module.Label != nil
  125. }
  126. // HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
  127. func (b *BazelModuleBase) HandcraftedLabel() string {
  128. return proptools.String(b.bazelProperties.Bazel_module.Label)
  129. }
  130. // GetBazelLabel returns the Bazel label for the given BazelModuleBase.
  131. func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
  132. if b.HasHandcraftedLabel() {
  133. return b.HandcraftedLabel()
  134. }
  135. if b.ShouldConvertWithBp2build(ctx) {
  136. return bp2buildModuleLabel(ctx, module)
  137. }
  138. return "" // no label for unconverted module
  139. }
  140. // Configuration to decide if modules in a directory should default to true/false for bp2build_available
  141. type Bp2BuildConfig map[string]BazelConversionConfigEntry
  142. type BazelConversionConfigEntry int
  143. const (
  144. // A sentinel value to be used as a key in Bp2BuildConfig for modules with
  145. // no package path. This is also the module dir for top level Android.bp
  146. // modules.
  147. BP2BUILD_TOPLEVEL = "."
  148. // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
  149. // which can also mean that the key doesn't exist in a lookup.
  150. // all modules in this package and subpackages default to bp2build_available: true.
  151. // allows modules to opt-out.
  152. Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
  153. // all modules in this package (not recursively) default to bp2build_available: true.
  154. // allows modules to opt-out.
  155. Bp2BuildDefaultTrue
  156. // all modules in this package (not recursively) default to bp2build_available: false.
  157. // allows modules to opt-in.
  158. Bp2BuildDefaultFalse
  159. )
  160. var (
  161. // Keep any existing BUILD files (and do not generate new BUILD files) for these directories
  162. // in the synthetic Bazel workspace.
  163. bp2buildKeepExistingBuildFile = map[string]bool{
  164. // This is actually build/bazel/build.BAZEL symlinked to ./BUILD
  165. ".":/*recursive = */ false,
  166. // build/bazel/examples/apex/... BUILD files should be generated, so
  167. // build/bazel is not recursive. Instead list each subdirectory under
  168. // build/bazel explicitly.
  169. "build/bazel":/* recursive = */ false,
  170. "build/bazel/ci/dist":/* recursive = */ false,
  171. "build/bazel/examples/android_app":/* recursive = */ true,
  172. "build/bazel/examples/java":/* recursive = */ true,
  173. "build/bazel/bazel_skylib":/* recursive = */ true,
  174. "build/bazel/rules":/* recursive = */ true,
  175. "build/bazel/rules_cc":/* recursive = */ true,
  176. "build/bazel/scripts":/* recursive = */ true,
  177. "build/bazel/tests":/* recursive = */ true,
  178. "build/bazel/platforms":/* recursive = */ true,
  179. "build/bazel/product_variables":/* recursive = */ true,
  180. "build/bazel_common_rules":/* recursive = */ true,
  181. // build/make/tools/signapk BUILD file is generated, so build/make/tools is not recursive.
  182. "build/make/tools":/* recursive = */ false,
  183. "build/pesto":/* recursive = */ true,
  184. // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails
  185. // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed
  186. "external/bazelbuild-rules_android":/* recursive = */ true,
  187. "external/bazel-skylib":/* recursive = */ true,
  188. "external/guava":/* recursive = */ true,
  189. "external/jsr305":/* recursive = */ true,
  190. "frameworks/ex/common":/* recursive = */ true,
  191. "packages/apps/Music":/* recursive = */ true,
  192. "packages/apps/QuickSearchBox":/* recursive = */ true,
  193. "packages/apps/WallpaperPicker":/* recursive = */ false,
  194. "prebuilts/gcc":/* recursive = */ true,
  195. "prebuilts/build-tools":/* recursive = */ false,
  196. "prebuilts/sdk":/* recursive = */ false,
  197. "prebuilts/sdk/current/extras/app-toolkit":/* recursive = */ false,
  198. "prebuilts/sdk/current/support":/* recursive = */ false,
  199. "prebuilts/sdk/tools":/* recursive = */ false,
  200. "prebuilts/r8":/* recursive = */ false,
  201. }
  202. // Configure modules in these directories to enable bp2build_available: true or false by default.
  203. bp2buildDefaultConfig = Bp2BuildConfig{
  204. "art/libartpalette": Bp2BuildDefaultTrueRecursively,
  205. "art/libdexfile": Bp2BuildDefaultTrueRecursively,
  206. "art/runtime": Bp2BuildDefaultTrueRecursively,
  207. "art/tools": Bp2BuildDefaultTrue,
  208. "bionic": Bp2BuildDefaultTrueRecursively,
  209. "bootable/recovery/tools/recovery_l10n": Bp2BuildDefaultTrue,
  210. "build/bazel/examples/soong_config_variables": Bp2BuildDefaultTrueRecursively,
  211. "build/bazel/examples/apex/minimal": Bp2BuildDefaultTrueRecursively,
  212. "build/make/tools/signapk": Bp2BuildDefaultTrue,
  213. "build/soong": Bp2BuildDefaultTrue,
  214. "build/soong/cc/libbuildversion": Bp2BuildDefaultTrue, // Skip tests subdir
  215. "build/soong/cc/ndkstubgen": Bp2BuildDefaultTrue,
  216. "build/soong/cc/symbolfile": Bp2BuildDefaultTrue,
  217. "build/soong/linkerconfig": Bp2BuildDefaultTrueRecursively,
  218. "build/soong/scripts": Bp2BuildDefaultTrueRecursively,
  219. "cts/common/device-side/nativetesthelper/jni": Bp2BuildDefaultTrueRecursively,
  220. "development/apps/DevelopmentSettings": Bp2BuildDefaultTrue,
  221. "development/apps/Fallback": Bp2BuildDefaultTrue,
  222. "development/apps/WidgetPreview": Bp2BuildDefaultTrue,
  223. "development/samples/BasicGLSurfaceView": Bp2BuildDefaultTrue,
  224. "development/samples/BluetoothChat": Bp2BuildDefaultTrue,
  225. "development/samples/BrokenKeyDerivation": Bp2BuildDefaultTrue,
  226. "development/samples/Compass": Bp2BuildDefaultTrue,
  227. "development/samples/ContactManager": Bp2BuildDefaultTrue,
  228. "development/samples/FixedGridLayout": Bp2BuildDefaultTrue,
  229. "development/samples/HelloEffects": Bp2BuildDefaultTrue,
  230. "development/samples/Home": Bp2BuildDefaultTrue,
  231. "development/samples/HoneycombGallery": Bp2BuildDefaultTrue,
  232. "development/samples/JetBoy": Bp2BuildDefaultTrue,
  233. "development/samples/KeyChainDemo": Bp2BuildDefaultTrue,
  234. "development/samples/LceDemo": Bp2BuildDefaultTrue,
  235. "development/samples/LunarLander": Bp2BuildDefaultTrue,
  236. "development/samples/MultiResolution": Bp2BuildDefaultTrue,
  237. "development/samples/MultiWindow": Bp2BuildDefaultTrue,
  238. "development/samples/NotePad": Bp2BuildDefaultTrue,
  239. "development/samples/Obb": Bp2BuildDefaultTrue,
  240. "development/samples/RSSReader": Bp2BuildDefaultTrue,
  241. "development/samples/ReceiveShareDemo": Bp2BuildDefaultTrue,
  242. "development/samples/SearchableDictionary": Bp2BuildDefaultTrue,
  243. "development/samples/SipDemo": Bp2BuildDefaultTrue,
  244. "development/samples/SkeletonApp": Bp2BuildDefaultTrue,
  245. "development/samples/Snake": Bp2BuildDefaultTrue,
  246. "development/samples/SpellChecker/": Bp2BuildDefaultTrueRecursively,
  247. "development/samples/ThemedNavBarKeyboard": Bp2BuildDefaultTrue,
  248. "development/samples/ToyVpn": Bp2BuildDefaultTrue,
  249. "development/samples/TtsEngine": Bp2BuildDefaultTrue,
  250. "development/samples/USB/AdbTest": Bp2BuildDefaultTrue,
  251. "development/samples/USB/MissileLauncher": Bp2BuildDefaultTrue,
  252. "development/samples/VoiceRecognitionService": Bp2BuildDefaultTrue,
  253. "development/samples/VoicemailProviderDemo": Bp2BuildDefaultTrue,
  254. "development/samples/WiFiDirectDemo": Bp2BuildDefaultTrue,
  255. "development/sdk": Bp2BuildDefaultTrueRecursively,
  256. "external/arm-optimized-routines": Bp2BuildDefaultTrueRecursively,
  257. "external/auto/common": Bp2BuildDefaultTrueRecursively,
  258. "external/auto/service": Bp2BuildDefaultTrueRecursively,
  259. "external/boringssl": Bp2BuildDefaultTrueRecursively,
  260. "external/bouncycastle": Bp2BuildDefaultTrue,
  261. "external/brotli": Bp2BuildDefaultTrue,
  262. "external/conscrypt": Bp2BuildDefaultTrue,
  263. "external/error_prone": Bp2BuildDefaultTrueRecursively,
  264. "external/fmtlib": Bp2BuildDefaultTrueRecursively,
  265. "external/google-benchmark": Bp2BuildDefaultTrueRecursively,
  266. "external/googletest": Bp2BuildDefaultTrueRecursively,
  267. "external/gwp_asan": Bp2BuildDefaultTrueRecursively,
  268. "external/icu": Bp2BuildDefaultTrueRecursively,
  269. "external/icu/android_icu4j": Bp2BuildDefaultFalse, // java rules incomplete
  270. "external/icu/icu4j": Bp2BuildDefaultFalse, // java rules incomplete
  271. "external/javapoet": Bp2BuildDefaultTrueRecursively,
  272. "external/jemalloc_new": Bp2BuildDefaultTrueRecursively,
  273. "external/jsoncpp": Bp2BuildDefaultTrueRecursively,
  274. "external/libcap": Bp2BuildDefaultTrueRecursively,
  275. "external/libcxx": Bp2BuildDefaultTrueRecursively,
  276. "external/libcxxabi": Bp2BuildDefaultTrueRecursively,
  277. "external/libevent": Bp2BuildDefaultTrueRecursively,
  278. "external/libpng": Bp2BuildDefaultTrueRecursively,
  279. "external/lz4/lib": Bp2BuildDefaultTrue,
  280. "external/lzma/C": Bp2BuildDefaultTrueRecursively,
  281. "external/mdnsresponder": Bp2BuildDefaultTrueRecursively,
  282. "external/minijail": Bp2BuildDefaultTrueRecursively,
  283. "external/pcre": Bp2BuildDefaultTrueRecursively,
  284. "external/protobuf": Bp2BuildDefaultTrueRecursively,
  285. "external/python/six": Bp2BuildDefaultTrueRecursively,
  286. "external/scudo": Bp2BuildDefaultTrueRecursively,
  287. "external/selinux/libselinux": Bp2BuildDefaultTrueRecursively,
  288. "external/selinux/libsepol": Bp2BuildDefaultTrueRecursively,
  289. "external/zlib": Bp2BuildDefaultTrueRecursively,
  290. "external/zstd": Bp2BuildDefaultTrueRecursively,
  291. "frameworks/base/media/tests/MediaDump": Bp2BuildDefaultTrue,
  292. "frameworks/base/startop/apps/test": Bp2BuildDefaultTrue,
  293. "frameworks/native/libs/adbd_auth": Bp2BuildDefaultTrueRecursively,
  294. "frameworks/native/opengl/tests/gl2_cameraeye": Bp2BuildDefaultTrue,
  295. "frameworks/native/opengl/tests/gl2_java": Bp2BuildDefaultTrue,
  296. "frameworks/native/opengl/tests/testLatency": Bp2BuildDefaultTrue,
  297. "frameworks/native/opengl/tests/testPauseResume": Bp2BuildDefaultTrue,
  298. "frameworks/native/opengl/tests/testViewport": Bp2BuildDefaultTrue,
  299. "frameworks/proto_logging/stats/stats_log_api_gen": Bp2BuildDefaultTrueRecursively,
  300. "libnativehelper": Bp2BuildDefaultTrueRecursively,
  301. "packages/apps/DevCamera": Bp2BuildDefaultTrue,
  302. "packages/apps/HTMLViewer": Bp2BuildDefaultTrue,
  303. "packages/apps/Protips": Bp2BuildDefaultTrue,
  304. "packages/modules/StatsD/lib/libstatssocket": Bp2BuildDefaultTrueRecursively,
  305. "packages/modules/adb": Bp2BuildDefaultTrue,
  306. "packages/modules/adb/apex": Bp2BuildDefaultTrue,
  307. "packages/modules/adb/crypto": Bp2BuildDefaultTrueRecursively,
  308. "packages/modules/adb/libs": Bp2BuildDefaultTrueRecursively,
  309. "packages/modules/adb/pairing_auth": Bp2BuildDefaultTrueRecursively,
  310. "packages/modules/adb/pairing_connection": Bp2BuildDefaultTrueRecursively,
  311. "packages/modules/adb/proto": Bp2BuildDefaultTrueRecursively,
  312. "packages/modules/adb/tls": Bp2BuildDefaultTrueRecursively,
  313. "packages/providers/MediaProvider/tools/dialogs": Bp2BuildDefaultTrue,
  314. "packages/screensavers/Basic": Bp2BuildDefaultTrue,
  315. "packages/services/Car/tests/SampleRearViewCamera": Bp2BuildDefaultTrue,
  316. "prebuilts/clang/host/linux-x86": Bp2BuildDefaultTrueRecursively,
  317. "prebuilts/tools/common/m2": Bp2BuildDefaultTrue,
  318. "system/apex": Bp2BuildDefaultFalse, // TODO(b/207466993): flaky failures
  319. "system/apex/proto": Bp2BuildDefaultTrueRecursively,
  320. "system/apex/libs": Bp2BuildDefaultTrueRecursively,
  321. "system/core/debuggerd": Bp2BuildDefaultTrueRecursively,
  322. "system/core/diagnose_usb": Bp2BuildDefaultTrueRecursively,
  323. "system/core/libasyncio": Bp2BuildDefaultTrue,
  324. "system/core/libcrypto_utils": Bp2BuildDefaultTrueRecursively,
  325. "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
  326. "system/core/libpackagelistparser": Bp2BuildDefaultTrueRecursively,
  327. "system/core/libprocessgroup": Bp2BuildDefaultTrue,
  328. "system/core/libprocessgroup/cgrouprc": Bp2BuildDefaultTrue,
  329. "system/core/libprocessgroup/cgrouprc_format": Bp2BuildDefaultTrue,
  330. "system/core/libsystem": Bp2BuildDefaultTrueRecursively,
  331. "system/core/libutils": Bp2BuildDefaultTrueRecursively,
  332. "system/core/libvndksupport": Bp2BuildDefaultTrueRecursively,
  333. "system/core/property_service/libpropertyinfoparser": Bp2BuildDefaultTrueRecursively,
  334. "system/libbase": Bp2BuildDefaultTrueRecursively,
  335. "system/libprocinfo": Bp2BuildDefaultTrue,
  336. "system/libziparchive": Bp2BuildDefaultTrueRecursively,
  337. "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
  338. "system/sepolicy/apex": Bp2BuildDefaultTrueRecursively,
  339. "system/timezone/apex": Bp2BuildDefaultTrueRecursively,
  340. "system/timezone/output_data": Bp2BuildDefaultTrueRecursively,
  341. "system/unwinding/libbacktrace": Bp2BuildDefaultTrueRecursively,
  342. "system/unwinding/libunwindstack": Bp2BuildDefaultTrueRecursively,
  343. "tools/apksig": Bp2BuildDefaultTrue,
  344. "tools/platform-compat/java/android/compat": Bp2BuildDefaultTrueRecursively,
  345. }
  346. // Per-module allowlist to always opt modules in of both bp2build and mixed builds.
  347. bp2buildModuleAlwaysConvertList = []string{
  348. "junit-params-assertj-core",
  349. }
  350. // Per-module denylist to always opt modules out of both bp2build and mixed builds.
  351. bp2buildModuleDoNotConvertList = []string{
  352. "libnativehelper_compat_libc", // Broken compile: implicit declaration of function 'strerror_r' is invalid in C99
  353. "libart", // depends on unconverted modules: art_operator_srcs, libodrstatslog, libelffile, art_cmdlineparser_headers, cpp-define-generator-definitions, libcpu_features, libdexfile, libartpalette, libbacktrace, libnativebridge, libnativeloader, libsigchain, libunwindstack, libartbase, libprofile, cpp-define-generator-asm-support, apex-info-list-tinyxml, libtinyxml2, libnativeloader-headers, libstatssocket, heapprofd_client_api
  354. "libart-runtime-gtest", // depends on unconverted modules: libgtest_isolated, libart-compiler, libdexfile, libprofile, libartbase, libbacktrace, libartbase-art-gtest
  355. "libart_headers", // depends on unconverted modules: art_libartbase_headers
  356. "libartd", // depends on unconverted modules: apex-info-list-tinyxml, libtinyxml2, libnativeloader-headers, libstatssocket, heapprofd_client_api, art_operator_srcs, libodrstatslog, libelffiled, art_cmdlineparser_headers, cpp-define-generator-definitions, libcpu_features, libdexfiled, libartpalette, libbacktrace, libnativebridge, libnativeloader, libsigchain, libunwindstack, libartbased, libprofiled, cpp-define-generator-asm-support
  357. "libartd-runtime-gtest", // depends on unconverted modules: libgtest_isolated, libartd-compiler, libdexfiled, libprofiled, libartbased, libbacktrace, libartbased-art-gtest
  358. "libstatslog_art", // depends on unconverted modules: statslog_art.cpp, statslog_art.h
  359. "statslog_art.h", "statslog_art.cpp", // depends on unconverted modules: stats-log-api-gen
  360. "libandroid_runtime_lazy", // depends on unconverted modules: libbinder_headers
  361. "libcmd", // depends on unconverted modules: libbinder
  362. "libdexfile_support_static", // Depends on unconverted module: libdexfile_external_headers
  363. "libunwindstack_local", "libunwindstack_utils", "libc_malloc_debug", "libfdtrack", // Depends on unconverted module: libunwindstack
  364. "libdexfile_support", // TODO(b/210546943): Enabled based on product variables.
  365. "libdexfile_external_headers", // TODO(b/210546943): Enabled based on product variables.
  366. "libunwindstack", // Depends on unconverted module libdexfile_support.
  367. "libnativehelper_compat_libc++", // Broken compile: implicit declaration of function 'strerror_r' is invalid in C99
  368. "chkcon", "sefcontext_compile", // depends on unconverted modules: libsepol
  369. "libsepol", // TODO(b/207408632): Unsupported case of .l sources in cc library rules
  370. "gen-kotlin-build-file.py", // module has same name as source
  371. "libactivitymanager_aidl", // TODO(b/207426160): Depends on activity_manager_procstate_aidl, which is an aidl filegroup.
  372. "libnativehelper_lazy_mts_jni", "libnativehelper_mts_jni", // depends on unconverted modules: libgmock_ndk
  373. "libnativetesthelper_jni", "libgmock_main_ndk", "libgmock_ndk", // depends on unconverted module: libgtest_ndk_c++
  374. "statslog-framework-java-gen", "statslog.cpp", "statslog.h", "statslog.rs", "statslog_header.rs", // depends on unconverted modules: stats-log-api-gen
  375. "stats-log-api-gen", // depends on unconverted modules: libstats_proto_host, libprotobuf-cpp-full
  376. "libstatslog", // depends on unconverted modules: statslog.cpp, statslog.h, ...
  377. "cmd", // depends on unconverted module packagemanager_aidl-cpp, of unsupported type aidl_interface
  378. "servicedispatcher", // depends on unconverted module android.debug_aidl, of unsupported type aidl_interface
  379. "libutilscallstack", // depends on unconverted module libbacktrace
  380. "libbacktrace", // depends on unconverted module libunwindstack
  381. "libdebuggerd_handler", // depends on unconverted module libdebuggerd_handler_core
  382. "libdebuggerd_handler_core", "libdebuggerd_handler_fallback", // depends on unconverted module libdebuggerd
  383. "unwind_for_offline", // depends on unconverted module libunwindstack_utils
  384. "libdebuggerd", // depends on unconverted modules libdexfile_support, libunwindstack, gwp_asan_crash_handler, libtombstone_proto, libprotobuf-cpp-lite
  385. "libdexfile_static", // depends on libartpalette, libartbase, libdexfile, which are of unsupported type: art_cc_library.
  386. "static_crasher", // depends on unconverted modules: libdebuggerd_handler
  387. "pbtombstone", "crash_dump", // depends on libdebuggerd, libunwindstack
  388. "libbase_ndk", // http://b/186826477, fails to link libctscamera2_jni for device (required for CtsCameraTestCases)
  389. "libprotobuf-internal-protos", // b/210751803, we don't handle path property for filegroups
  390. "libprotobuf-internal-python-srcs", // b/210751803, we don't handle path property for filegroups
  391. "libprotobuf-java-full", // b/210751803, we don't handle path property for filegroups
  392. "host-libprotobuf-java-full", // b/210751803, we don't handle path property for filegroups
  393. "libprotobuf-java-util-full", // b/210751803, we don't handle path property for filegroups
  394. "conscrypt", // b/210751803, we don't handle path property for filegroups
  395. "conscrypt-for-host", // b/210751803, we don't handle path property for filegroups
  396. "host-libprotobuf-java-lite", // b/217236083, java_library cannot have deps without srcs
  397. "host-libprotobuf-java-micro", // b/217236083, java_library cannot have deps without srcs
  398. "host-libprotobuf-java-nano", // b/217236083, java_library cannot have deps without srcs
  399. "error_prone_core", // b/217236083, java_library cannot have deps without srcs
  400. "bouncycastle-host", // b/217236083, java_library cannot have deps without srcs
  401. "apex_manifest_proto_java", // b/215230097, we don't handle .proto files in java_library srcs attribute
  402. "libc_musl_sysroot_bionic_arch_headers", // b/218405924, depends on soong_zip
  403. "libc_musl_sysroot_bionic_headers", // b/218405924, depends on soong_zip and generates duplicate srcs
  404. // python protos
  405. "libprotobuf-python", // contains .proto sources
  406. "conv_linker_config", // depends on linker_config_proto, a python lib with proto sources
  407. "apex_build_info_proto", "apex_manifest_proto", // a python lib with proto sources
  408. "linker_config_proto", // contains .proto sources
  409. "brotli-fuzzer-corpus", // b/202015218: outputs are in location incompatible with bazel genrule handling.
  410. // b/203369847: multiple genrules in the same package creating the same file
  411. // //development/sdk/...
  412. "platform_tools_properties",
  413. "build_tools_source_properties",
  414. // APEX support
  415. "com.android.runtime", // depends on unconverted modules: bionic-linker-config, linkerconfig
  416. "libgtest_ndk_c++", // b/201816222: Requires sdk_version support.
  417. "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support.
  418. "abb", // depends on unconverted modules: libcmd, libbinder
  419. "adb", // depends on unconverted modules: AdbWinApi, libadb_host, libandroidfw, libapp_processes_protos_full, libfastdeploy_host, libopenscreen-discovery, libopenscreen-platform-impl, libusb, bin2c_fastdeployagent, AdbWinUsbApi
  420. "libadb_host", // depends on unconverted modules: libopenscreen-discovery, libopenscreen-platform-impl, libusb, AdbWinApi
  421. "libfastdeploy_host", // depends on unconverted modules: libandroidfw, libusb, AdbWinApi
  422. "linker", // depends on unconverted modules: libdebuggerd_handler_fallback
  423. "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_*
  424. "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host, of unsupported type llvm_host_prebuilt_library_shared
  425. "linkerconfig", // http://b/202876379 has arch-variant static_executable
  426. "mdnsd", // http://b/202876379 has arch-variant static_executable
  427. "CarHTMLViewer", // depends on unconverted modules android.car-stubs, car-ui-lib
  428. "libdexfile", // depends on unconverted modules: dexfile_operator_srcs, libartbase, libartpalette,
  429. "libdexfiled", // depends on unconverted modules: dexfile_operator_srcs, libartbased, libartpalette
  430. // go deps:
  431. "apex-protos", // depends on soong_zip, a go binary
  432. "generated_android_icu4j_src_files", "generated_android_icu4j_test_files", "icu4c_test_data", // depends on unconverted modules: soong_zip
  433. "host_bionic_linker_asm", // depends on extract_linker, a go binary.
  434. "host_bionic_linker_script", // depends on extract_linker, a go binary.
  435. "robolectric-sqlite4java-native", // depends on soong_zip, a go binary
  436. "robolectric_tzdata", // depends on soong_zip, a go binary
  437. "android_icu4j_srcgen_binary", // Bazel build error: deps not allowed without srcs; move to runtime_deps
  438. "core-icu4j-for-host", // Bazel build error: deps not allowed without srcs; move to runtime_deps
  439. // java deps
  440. "android_icu4j_srcgen", // depends on unconverted modules: currysrc
  441. "bin2c_fastdeployagent", // depends on deployagent, a java binary
  442. "currysrc", // depends on unconverted modules: currysrc_org.eclipse, guavalib, jopt-simple-4.9
  443. "robolectric-sqlite4java-0.282", // depends on unconverted modules: robolectric-sqlite4java-import, robolectric-sqlite4java-native
  444. "timezone-host", // depends on unconverted modules: art.module.api.annotations
  445. "truth-host-prebuilt", // depends on unconverted modules: truth-prebuilt
  446. "truth-prebuilt", // depends on unconverted modules: asm-7.0, guava
  447. "generated_android_icu4j_resources", // depends on unconverted modules: android_icu4j_srcgen_binary, soong_zip
  448. "generated_android_icu4j_test_resources", // depends on unconverted modules: android_icu4j_srcgen_binary, soong_zip
  449. "art-script", // depends on unconverted modules: dalvikvm, dex2oat
  450. "dex2oat-script", // depends on unconverted modules: dex2oat
  451. "error_prone_checkerframework_dataflow_nullaway", // TODO(b/219908977): "Error in fail: deps not allowed without srcs; move to runtime_deps?"
  452. }
  453. // Per-module denylist of cc_library modules to only generate the static
  454. // variant if their shared variant isn't ready or buildable by Bazel.
  455. bp2buildCcLibraryStaticOnlyList = []string{}
  456. // Per-module denylist to opt modules out of mixed builds. Such modules will
  457. // still be generated via bp2build.
  458. mixedBuildsDisabledList = []string{
  459. "art_libdexfile_dex_instruction_list_header", // breaks libart_mterp.armng, header not found
  460. "libbrotli", // http://b/198585397, ld.lld: error: bionic/libc/arch-arm64/generic/bionic/memmove.S:95:(.text+0x10): relocation R_AARCH64_CONDBR19 out of range: -1404176 is not in [-1048576, 1048575]; references __memcpy
  461. "minijail_constants_json", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
  462. "cap_names.h", // TODO(b/204913827) runfiles need to be handled in mixed builds
  463. "libcap", // TODO(b/204913827) runfiles need to be handled in mixed builds
  464. "libprotobuf-cpp-full", "libprotobuf-cpp-lite", // Unsupported product&vendor suffix. b/204811222 and b/204810610.
  465. // Depends on libprotobuf-cpp-*
  466. "libadb_pairing_connection",
  467. "libadb_pairing_connection_static",
  468. "libadb_pairing_server", "libadb_pairing_server_static",
  469. // TODO(b/204811222) support suffix in cc_binary
  470. "acvp_modulewrapper",
  471. "android.hardware.media.c2@1.0-service-v4l2",
  472. "app_process",
  473. "bar_test",
  474. "bench_cxa_atexit",
  475. "bench_noop",
  476. "bench_noop_nostl",
  477. "bench_noop_static",
  478. "boringssl_self_test",
  479. "boringssl_self_test_vendor",
  480. "bssl",
  481. "cavp",
  482. "crash_dump",
  483. "crasher",
  484. "libcxx_test_template",
  485. "linker",
  486. "memory_replay",
  487. "native_bridge_guest_linker",
  488. "native_bridge_stub_library_defaults",
  489. "noop",
  490. "simpleperf_ndk",
  491. "toybox-static",
  492. "zlib_bench",
  493. }
  494. // Used for quicker lookups
  495. bp2buildModuleDoNotConvert = map[string]bool{}
  496. bp2buildModuleAlwaysConvert = map[string]bool{}
  497. bp2buildCcLibraryStaticOnly = map[string]bool{}
  498. mixedBuildsDisabled = map[string]bool{}
  499. )
  500. func init() {
  501. for _, moduleName := range bp2buildModuleAlwaysConvertList {
  502. bp2buildModuleAlwaysConvert[moduleName] = true
  503. }
  504. for _, moduleName := range bp2buildModuleDoNotConvertList {
  505. bp2buildModuleDoNotConvert[moduleName] = true
  506. }
  507. for _, moduleName := range bp2buildCcLibraryStaticOnlyList {
  508. bp2buildCcLibraryStaticOnly[moduleName] = true
  509. }
  510. for _, moduleName := range mixedBuildsDisabledList {
  511. mixedBuildsDisabled[moduleName] = true
  512. }
  513. }
  514. func GenerateCcLibraryStaticOnly(moduleName string) bool {
  515. return bp2buildCcLibraryStaticOnly[moduleName]
  516. }
  517. func ShouldKeepExistingBuildFileForDir(dir string) bool {
  518. if _, ok := bp2buildKeepExistingBuildFile[dir]; ok {
  519. // Exact dir match
  520. return true
  521. }
  522. // Check if subtree match
  523. for prefix, recursive := range bp2buildKeepExistingBuildFile {
  524. if recursive {
  525. if strings.HasPrefix(dir, prefix+"/") {
  526. return true
  527. }
  528. }
  529. }
  530. // Default
  531. return false
  532. }
  533. // MixedBuildsEnabled checks that a module is ready to be replaced by a
  534. // converted or handcrafted Bazel target.
  535. func (b *BazelModuleBase) MixedBuildsEnabled(ctx ModuleContext) bool {
  536. if ctx.Os() == Windows {
  537. // Windows toolchains are not currently supported.
  538. return false
  539. }
  540. if !ctx.Module().Enabled() {
  541. return false
  542. }
  543. if !ctx.Config().BazelContext.BazelEnabled() {
  544. return false
  545. }
  546. if !convertedToBazel(ctx, ctx.Module()) {
  547. return false
  548. }
  549. if GenerateCcLibraryStaticOnly(ctx.Module().Name()) {
  550. // Don't use partially-converted cc_library targets in mixed builds,
  551. // since mixed builds would generally rely on both static and shared
  552. // variants of a cc_library.
  553. return false
  554. }
  555. return !mixedBuildsDisabled[ctx.Module().Name()]
  556. }
  557. // ConvertedToBazel returns whether this module has been converted (with bp2build or manually) to Bazel.
  558. func convertedToBazel(ctx BazelConversionContext, module blueprint.Module) bool {
  559. b, ok := module.(Bazelable)
  560. if !ok {
  561. return false
  562. }
  563. return b.shouldConvertWithBp2build(ctx, module) || b.HasHandcraftedLabel()
  564. }
  565. // ShouldConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
  566. func (b *BazelModuleBase) ShouldConvertWithBp2build(ctx BazelConversionContext) bool {
  567. return b.shouldConvertWithBp2build(ctx, ctx.Module())
  568. }
  569. func (b *BazelModuleBase) shouldConvertWithBp2build(ctx BazelConversionContext, module blueprint.Module) bool {
  570. moduleNameNoPrefix := RemoveOptionalPrebuiltPrefix(module.Name())
  571. alwaysConvert := bp2buildModuleAlwaysConvert[moduleNameNoPrefix]
  572. if bp2buildModuleDoNotConvert[moduleNameNoPrefix] {
  573. if alwaysConvert {
  574. ctx.(BaseModuleContext).ModuleErrorf("a module cannot be in bp2buildModuleDoNotConvert" +
  575. " and also be in bp2buildModuleAlwaysConvert")
  576. }
  577. return false
  578. }
  579. if !b.bazelProps().Bazel_module.CanConvertToBazel {
  580. return false
  581. }
  582. packagePath := ctx.OtherModuleDir(module)
  583. config := ctx.Config().bp2buildPackageConfig
  584. // This is a tristate value: true, false, or unset.
  585. propValue := b.bazelProperties.Bazel_module.Bp2build_available
  586. if bp2buildDefaultTrueRecursively(packagePath, config) {
  587. if alwaysConvert {
  588. ctx.(BaseModuleContext).ModuleErrorf("a module cannot be in a directory marked Bp2BuildDefaultTrue" +
  589. " or Bp2BuildDefaultTrueRecursively and also be in bp2buildModuleAlwaysConvert")
  590. }
  591. // Allow modules to explicitly opt-out.
  592. return proptools.BoolDefault(propValue, true)
  593. }
  594. // Allow modules to explicitly opt-in.
  595. return proptools.BoolDefault(propValue, alwaysConvert)
  596. }
  597. // bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
  598. // set of package prefixes where all modules must be converted. That is, if the
  599. // package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
  600. // return true.
  601. //
  602. // However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
  603. // exactly, this module will return false early.
  604. //
  605. // This function will also return false if the package doesn't match anything in
  606. // the config.
  607. func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
  608. ret := false
  609. // Check if the package path has an exact match in the config.
  610. if config[packagePath] == Bp2BuildDefaultTrue || config[packagePath] == Bp2BuildDefaultTrueRecursively {
  611. return true
  612. } else if config[packagePath] == Bp2BuildDefaultFalse {
  613. return false
  614. }
  615. // If not, check for the config recursively.
  616. packagePrefix := ""
  617. // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
  618. for _, part := range strings.Split(packagePath, "/") {
  619. packagePrefix += part
  620. if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
  621. // package contains this prefix and this prefix should convert all modules
  622. return true
  623. }
  624. // Continue to the next part of the package dir.
  625. packagePrefix += "/"
  626. }
  627. return ret
  628. }
  629. // GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
  630. // an error if there are errors reading the file.
  631. // TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
  632. // something more targeted based on the rule type and target.
  633. func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
  634. if !strings.Contains(b.HandcraftedLabel(), path) {
  635. return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
  636. }
  637. name = filepath.Join(path, name)
  638. f, err := c.fs.Open(name)
  639. if err != nil {
  640. return "", err
  641. }
  642. defer f.Close()
  643. data, err := ioutil.ReadAll(f)
  644. if err != nil {
  645. return "", err
  646. }
  647. return string(data[:]), nil
  648. }
  649. func registerBp2buildConversionMutator(ctx RegisterMutatorsContext) {
  650. ctx.TopDown("bp2build_conversion", convertWithBp2build).Parallel()
  651. }
  652. func convertWithBp2build(ctx TopDownMutatorContext) {
  653. bModule, ok := ctx.Module().(Bazelable)
  654. if !ok || !bModule.shouldConvertWithBp2build(ctx, ctx.Module()) {
  655. return
  656. }
  657. bModule.ConvertWithBp2build(ctx)
  658. }
  659. // GetMainClassInManifest scans the manifest file specified in filepath and returns
  660. // the value of attribute Main-Class in the manifest file if it exists, or returns error.
  661. // WARNING: this is for bp2build converters of java_* modules only.
  662. func GetMainClassInManifest(c Config, filepath string) (string, error) {
  663. file, err := c.fs.Open(filepath)
  664. if err != nil {
  665. return "", err
  666. }
  667. defer file.Close()
  668. scanner := bufio.NewScanner(file)
  669. for scanner.Scan() {
  670. line := scanner.Text()
  671. if strings.HasPrefix(line, "Main-Class:") {
  672. return strings.TrimSpace(line[len("Main-Class:"):]), nil
  673. }
  674. }
  675. return "", errors.New("Main-Class is not found.")
  676. }