snapshot_prebuilt.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. // Copyright 2020 The Android Open Source Project
  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 cc
  15. // This file defines snapshot prebuilt modules, e.g. vendor snapshot and recovery snapshot. Such
  16. // snapshot modules will override original source modules with setting BOARD_VNDK_VERSION, with
  17. // snapshot mutators and snapshot information maps which are also defined in this file.
  18. import (
  19. "fmt"
  20. "strings"
  21. "android/soong/android"
  22. "android/soong/snapshot"
  23. "github.com/google/blueprint"
  24. )
  25. // This interface overrides snapshot.SnapshotImage to implement cc module specific functions
  26. type SnapshotImage interface {
  27. snapshot.SnapshotImage
  28. // The image variant name for this snapshot image.
  29. // For example, recovery snapshot image will return "recovery", and vendor snapshot image will
  30. // return "vendor." + version.
  31. imageVariantName(cfg android.DeviceConfig) string
  32. // The variant suffix for snapshot modules. For example, vendor snapshot modules will have
  33. // ".vendor" as their suffix.
  34. moduleNameSuffix() string
  35. }
  36. type vendorSnapshotImage struct {
  37. *snapshot.VendorSnapshotImage
  38. }
  39. type recoverySnapshotImage struct {
  40. *snapshot.RecoverySnapshotImage
  41. }
  42. func (vendorSnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
  43. return VendorVariationPrefix + cfg.VndkVersion()
  44. }
  45. func (vendorSnapshotImage) moduleNameSuffix() string {
  46. return VendorSuffix
  47. }
  48. func (recoverySnapshotImage) imageVariantName(cfg android.DeviceConfig) string {
  49. return android.RecoveryVariation
  50. }
  51. func (recoverySnapshotImage) moduleNameSuffix() string {
  52. return RecoverySuffix
  53. }
  54. // Override existing vendor and recovery snapshot for cc module specific extra functions
  55. var VendorSnapshotImageSingleton vendorSnapshotImage = vendorSnapshotImage{&snapshot.VendorSnapshotImageSingleton}
  56. var RecoverySnapshotImageSingleton recoverySnapshotImage = recoverySnapshotImage{&snapshot.RecoverySnapshotImageSingleton}
  57. func RegisterVendorSnapshotModules(ctx android.RegistrationContext) {
  58. ctx.RegisterModuleType("vendor_snapshot", vendorSnapshotFactory)
  59. ctx.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
  60. ctx.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
  61. ctx.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
  62. ctx.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
  63. ctx.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory)
  64. }
  65. func RegisterRecoverySnapshotModules(ctx android.RegistrationContext) {
  66. ctx.RegisterModuleType("recovery_snapshot", recoverySnapshotFactory)
  67. ctx.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory)
  68. ctx.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory)
  69. ctx.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory)
  70. ctx.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory)
  71. ctx.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory)
  72. }
  73. func init() {
  74. RegisterVendorSnapshotModules(android.InitRegistrationContext)
  75. RegisterRecoverySnapshotModules(android.InitRegistrationContext)
  76. android.RegisterMakeVarsProvider(pctx, snapshotMakeVarsProvider)
  77. }
  78. const (
  79. snapshotHeaderSuffix = "_header."
  80. SnapshotSharedSuffix = "_shared."
  81. SnapshotStaticSuffix = "_static."
  82. snapshotBinarySuffix = "_binary."
  83. snapshotObjectSuffix = "_object."
  84. SnapshotRlibSuffix = "_rlib."
  85. )
  86. type SnapshotProperties struct {
  87. Header_libs []string `android:"arch_variant"`
  88. Static_libs []string `android:"arch_variant"`
  89. Shared_libs []string `android:"arch_variant"`
  90. Rlibs []string `android:"arch_variant"`
  91. Vndk_libs []string `android:"arch_variant"`
  92. Binaries []string `android:"arch_variant"`
  93. Objects []string `android:"arch_variant"`
  94. }
  95. type snapshotModule struct {
  96. android.ModuleBase
  97. properties SnapshotProperties
  98. baseSnapshot BaseSnapshotDecorator
  99. image SnapshotImage
  100. }
  101. func (s *snapshotModule) ImageMutatorBegin(ctx android.BaseModuleContext) {
  102. cfg := ctx.DeviceConfig()
  103. if !s.image.IsUsingSnapshot(cfg) || s.image.TargetSnapshotVersion(cfg) != s.baseSnapshot.Version() {
  104. s.Disable()
  105. }
  106. }
  107. func (s *snapshotModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
  108. return false
  109. }
  110. func (s *snapshotModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  111. return false
  112. }
  113. func (s *snapshotModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  114. return false
  115. }
  116. func (s *snapshotModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  117. return false
  118. }
  119. func (s *snapshotModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
  120. return false
  121. }
  122. func (s *snapshotModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
  123. return []string{s.image.imageVariantName(ctx.DeviceConfig())}
  124. }
  125. func (s *snapshotModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
  126. }
  127. func (s *snapshotModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  128. // Nothing, the snapshot module is only used to forward dependency information in DepsMutator.
  129. }
  130. func getSnapshotNameSuffix(moduleSuffix, version, arch string) string {
  131. versionSuffix := version
  132. if arch != "" {
  133. versionSuffix += "." + arch
  134. }
  135. return moduleSuffix + versionSuffix
  136. }
  137. func (s *snapshotModule) DepsMutator(ctx android.BottomUpMutatorContext) {
  138. collectSnapshotMap := func(names []string, snapshotSuffix, moduleSuffix string) map[string]string {
  139. snapshotMap := make(map[string]string)
  140. for _, name := range names {
  141. snapshotMap[name] = name +
  142. getSnapshotNameSuffix(snapshotSuffix+moduleSuffix,
  143. s.baseSnapshot.Version(),
  144. ctx.DeviceConfig().Arches()[0].ArchType.String())
  145. }
  146. return snapshotMap
  147. }
  148. snapshotSuffix := s.image.moduleNameSuffix()
  149. headers := collectSnapshotMap(s.properties.Header_libs, snapshotSuffix, snapshotHeaderSuffix)
  150. binaries := collectSnapshotMap(s.properties.Binaries, snapshotSuffix, snapshotBinarySuffix)
  151. objects := collectSnapshotMap(s.properties.Objects, snapshotSuffix, snapshotObjectSuffix)
  152. staticLibs := collectSnapshotMap(s.properties.Static_libs, snapshotSuffix, SnapshotStaticSuffix)
  153. sharedLibs := collectSnapshotMap(s.properties.Shared_libs, snapshotSuffix, SnapshotSharedSuffix)
  154. rlibs := collectSnapshotMap(s.properties.Rlibs, snapshotSuffix, SnapshotRlibSuffix)
  155. vndkLibs := collectSnapshotMap(s.properties.Vndk_libs, "", vndkSuffix)
  156. for k, v := range vndkLibs {
  157. sharedLibs[k] = v
  158. }
  159. ctx.SetProvider(SnapshotInfoProvider, SnapshotInfo{
  160. HeaderLibs: headers,
  161. Binaries: binaries,
  162. Objects: objects,
  163. StaticLibs: staticLibs,
  164. SharedLibs: sharedLibs,
  165. Rlibs: rlibs,
  166. })
  167. }
  168. type SnapshotInfo struct {
  169. HeaderLibs, Binaries, Objects, StaticLibs, SharedLibs, Rlibs map[string]string
  170. }
  171. var SnapshotInfoProvider = blueprint.NewMutatorProvider(SnapshotInfo{}, "deps")
  172. var _ android.ImageInterface = (*snapshotModule)(nil)
  173. func snapshotMakeVarsProvider(ctx android.MakeVarsContext) {
  174. snapshotSet := map[string]struct{}{}
  175. ctx.VisitAllModules(func(m android.Module) {
  176. if s, ok := m.(*snapshotModule); ok {
  177. if _, ok := snapshotSet[s.Name()]; ok {
  178. // arch variant generates duplicated modules
  179. // skip this as we only need to know the path of the module.
  180. return
  181. }
  182. snapshotSet[s.Name()] = struct{}{}
  183. imageNameVersion := strings.Split(s.image.imageVariantName(ctx.DeviceConfig()), ".")
  184. ctx.Strict(
  185. strings.Join([]string{strings.ToUpper(imageNameVersion[0]), s.baseSnapshot.Version(), "SNAPSHOT_DIR"}, "_"),
  186. ctx.ModuleDir(s))
  187. }
  188. })
  189. }
  190. func vendorSnapshotFactory() android.Module {
  191. return snapshotFactory(VendorSnapshotImageSingleton)
  192. }
  193. func recoverySnapshotFactory() android.Module {
  194. return snapshotFactory(RecoverySnapshotImageSingleton)
  195. }
  196. func snapshotFactory(image SnapshotImage) android.Module {
  197. snapshotModule := &snapshotModule{}
  198. snapshotModule.image = image
  199. snapshotModule.AddProperties(
  200. &snapshotModule.properties,
  201. &snapshotModule.baseSnapshot.baseProperties)
  202. android.InitAndroidArchModule(snapshotModule, android.DeviceSupported, android.MultilibBoth)
  203. return snapshotModule
  204. }
  205. type BaseSnapshotDecoratorProperties struct {
  206. // snapshot version.
  207. Version string
  208. // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
  209. Target_arch string
  210. // Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
  211. Androidmk_suffix string `blueprint:"mutated"`
  212. // Suffix to be added to the module name, e.g., vendor_shared,
  213. // recovery_shared, etc.
  214. ModuleSuffix string `blueprint:"mutated"`
  215. }
  216. // BaseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot
  217. // version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't
  218. // collide with source modules. e.g. the following example module,
  219. //
  220. // vendor_snapshot_static {
  221. // name: "libbase",
  222. // arch: "arm64",
  223. // version: 30,
  224. // ...
  225. // }
  226. //
  227. // will be seen as "libbase.vendor_static.30.arm64" by Soong.
  228. type BaseSnapshotDecorator struct {
  229. baseProperties BaseSnapshotDecoratorProperties
  230. Image SnapshotImage
  231. }
  232. func (p *BaseSnapshotDecorator) Name(name string) string {
  233. return name + p.NameSuffix()
  234. }
  235. func (p *BaseSnapshotDecorator) NameSuffix() string {
  236. return getSnapshotNameSuffix(p.moduleSuffix(), p.Version(), p.Arch())
  237. }
  238. func (p *BaseSnapshotDecorator) Version() string {
  239. return p.baseProperties.Version
  240. }
  241. func (p *BaseSnapshotDecorator) Arch() string {
  242. return p.baseProperties.Target_arch
  243. }
  244. func (p *BaseSnapshotDecorator) moduleSuffix() string {
  245. return p.baseProperties.ModuleSuffix
  246. }
  247. func (p *BaseSnapshotDecorator) IsSnapshotPrebuilt() bool {
  248. return true
  249. }
  250. func (p *BaseSnapshotDecorator) SnapshotAndroidMkSuffix() string {
  251. return p.baseProperties.Androidmk_suffix
  252. }
  253. func (p *BaseSnapshotDecorator) SetSnapshotAndroidMkSuffix(ctx android.ModuleContext, variant string) {
  254. // If there are any 2 or more variations among {core, product, vendor, recovery}
  255. // we have to add the androidmk suffix to avoid duplicate modules with the same
  256. // name.
  257. variations := append(ctx.Target().Variations(), blueprint.Variation{
  258. Mutator: "image",
  259. Variation: android.CoreVariation})
  260. if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
  261. p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
  262. return
  263. }
  264. variations = append(ctx.Target().Variations(), blueprint.Variation{
  265. Mutator: "image",
  266. Variation: ProductVariationPrefix + ctx.DeviceConfig().PlatformVndkVersion()})
  267. if ctx.OtherModuleFarDependencyVariantExists(variations, ctx.Module().(LinkableInterface).BaseModuleName()) {
  268. p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
  269. return
  270. }
  271. images := []SnapshotImage{VendorSnapshotImageSingleton, RecoverySnapshotImageSingleton}
  272. for _, image := range images {
  273. if p.Image == image {
  274. continue
  275. }
  276. variations = append(ctx.Target().Variations(), blueprint.Variation{
  277. Mutator: "image",
  278. Variation: image.imageVariantName(ctx.DeviceConfig())})
  279. if ctx.OtherModuleFarDependencyVariantExists(variations,
  280. ctx.Module().(LinkableInterface).BaseModuleName()+
  281. getSnapshotNameSuffix(
  282. image.moduleNameSuffix()+variant,
  283. p.Version(),
  284. ctx.DeviceConfig().Arches()[0].ArchType.String())) {
  285. p.baseProperties.Androidmk_suffix = p.Image.moduleNameSuffix()
  286. return
  287. }
  288. }
  289. p.baseProperties.Androidmk_suffix = ""
  290. }
  291. // Call this with a module suffix after creating a snapshot module, such as
  292. // vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
  293. func (p *BaseSnapshotDecorator) Init(m LinkableInterface, image SnapshotImage, moduleSuffix string) {
  294. p.Image = image
  295. p.baseProperties.ModuleSuffix = image.moduleNameSuffix() + moduleSuffix
  296. m.AddProperties(&p.baseProperties)
  297. android.AddLoadHook(m, func(ctx android.LoadHookContext) {
  298. vendorSnapshotLoadHook(ctx, p)
  299. })
  300. }
  301. // vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION.
  302. // As vendor snapshot is only for vendor, such modules won't be used at all.
  303. func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *BaseSnapshotDecorator) {
  304. if p.Version() != ctx.DeviceConfig().VndkVersion() {
  305. ctx.Module().Disable()
  306. return
  307. }
  308. }
  309. // Module definitions for snapshots of libraries (shared, static, header).
  310. //
  311. // Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and
  312. // static libraries have their prebuilt library files (.so for shared, .a for static) as their src,
  313. // which can be installed or linked against. Also they export flags needed when linked, such as
  314. // include directories, c flags, sanitize dependency information, etc.
  315. //
  316. // These modules are auto-generated by development/vendor_snapshot/update.py.
  317. type SnapshotLibraryProperties struct {
  318. // Prebuilt file for each arch.
  319. Src *string `android:"arch_variant"`
  320. // list of directories that will be added to the include path (using -I).
  321. Export_include_dirs []string `android:"arch_variant"`
  322. // list of directories that will be added to the system path (using -isystem).
  323. Export_system_include_dirs []string `android:"arch_variant"`
  324. // list of flags that will be used for any module that links against this module.
  325. Export_flags []string `android:"arch_variant"`
  326. // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
  327. Sanitize_ubsan_dep *bool `android:"arch_variant"`
  328. // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
  329. Sanitize_minimal_dep *bool `android:"arch_variant"`
  330. }
  331. type snapshotSanitizer interface {
  332. isSanitizerAvailable(t SanitizerType) bool
  333. setSanitizerVariation(t SanitizerType, enabled bool)
  334. isSanitizerEnabled(t SanitizerType) bool
  335. isUnsanitizedVariant() bool
  336. }
  337. type snapshotLibraryDecorator struct {
  338. BaseSnapshotDecorator
  339. *libraryDecorator
  340. properties SnapshotLibraryProperties
  341. sanitizerProperties struct {
  342. SanitizerVariation SanitizerType `blueprint:"mutated"`
  343. // Library flags for cfi variant.
  344. Cfi SnapshotLibraryProperties `android:"arch_variant"`
  345. // Library flags for hwasan variant.
  346. Hwasan SnapshotLibraryProperties `android:"arch_variant"`
  347. }
  348. }
  349. func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
  350. p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
  351. return p.libraryDecorator.linkerFlags(ctx, flags)
  352. }
  353. func (p *snapshotLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
  354. arches := config.Arches()
  355. if len(arches) == 0 || arches[0].ArchType.String() != p.Arch() {
  356. return false
  357. }
  358. if !p.header() && p.properties.Src == nil {
  359. return false
  360. }
  361. return true
  362. }
  363. // cc modules' link functions are to link compiled objects into final binaries.
  364. // As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
  365. // done by normal library decorator, e.g. exporting flags.
  366. func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
  367. var variant string
  368. if p.shared() {
  369. variant = SnapshotSharedSuffix
  370. } else if p.static() {
  371. variant = SnapshotStaticSuffix
  372. } else {
  373. variant = snapshotHeaderSuffix
  374. }
  375. p.SetSnapshotAndroidMkSuffix(ctx, variant)
  376. if p.header() {
  377. return p.libraryDecorator.link(ctx, flags, deps, objs)
  378. }
  379. if p.isSanitizerEnabled(cfi) {
  380. p.properties = p.sanitizerProperties.Cfi
  381. } else if p.isSanitizerEnabled(Hwasan) {
  382. p.properties = p.sanitizerProperties.Hwasan
  383. }
  384. if !p.MatchesWithDevice(ctx.DeviceConfig()) {
  385. return nil
  386. }
  387. // Flags specified directly to this module.
  388. p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...)
  389. p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...)
  390. p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
  391. // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
  392. p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
  393. p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
  394. p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
  395. p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
  396. p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
  397. in := android.PathForModuleSrc(ctx, *p.properties.Src)
  398. p.unstrippedOutputFile = in
  399. if p.shared() {
  400. libName := in.Base()
  401. // Optimize out relinking against shared libraries whose interface hasn't changed by
  402. // depending on a table of contents file instead of the library itself.
  403. tocFile := android.PathForModuleOut(ctx, libName+".toc")
  404. p.tocFile = android.OptionalPathForPath(tocFile)
  405. TransformSharedObjectToToc(ctx, in, tocFile)
  406. ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
  407. SharedLibrary: in,
  408. Target: ctx.Target(),
  409. TableOfContents: p.tocFile,
  410. })
  411. }
  412. if p.static() {
  413. depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build()
  414. ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
  415. StaticLibrary: in,
  416. TransitiveStaticLibrariesForOrdering: depSet,
  417. })
  418. }
  419. p.libraryDecorator.flagExporter.setProvider(ctx)
  420. return in
  421. }
  422. func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
  423. if p.MatchesWithDevice(ctx.DeviceConfig()) && p.shared() {
  424. p.baseInstaller.install(ctx, file)
  425. }
  426. }
  427. func (p *snapshotLibraryDecorator) nativeCoverage() bool {
  428. return false
  429. }
  430. var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil)
  431. func (p *snapshotLibraryDecorator) isSanitizerAvailable(t SanitizerType) bool {
  432. switch t {
  433. case cfi:
  434. return p.sanitizerProperties.Cfi.Src != nil
  435. case Hwasan:
  436. return p.sanitizerProperties.Hwasan.Src != nil
  437. default:
  438. return false
  439. }
  440. }
  441. func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) {
  442. if !enabled || p.isSanitizerEnabled(t) {
  443. return
  444. }
  445. if !p.isUnsanitizedVariant() {
  446. panic(fmt.Errorf("snapshot Sanitizer must be one of Cfi or Hwasan but not both"))
  447. }
  448. p.sanitizerProperties.SanitizerVariation = t
  449. }
  450. func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool {
  451. return p.sanitizerProperties.SanitizerVariation == t
  452. }
  453. func (p *snapshotLibraryDecorator) isUnsanitizedVariant() bool {
  454. return !p.isSanitizerEnabled(Asan) &&
  455. !p.isSanitizerEnabled(Hwasan)
  456. }
  457. func snapshotLibraryFactory(image SnapshotImage, moduleSuffix string) (*Module, *snapshotLibraryDecorator) {
  458. module, library := NewLibrary(android.DeviceSupported)
  459. module.stl = nil
  460. module.sanitize = nil
  461. library.disableStripping()
  462. prebuilt := &snapshotLibraryDecorator{
  463. libraryDecorator: library,
  464. }
  465. prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
  466. prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
  467. // Prevent default system libs (libc, libm, and libdl) from being linked
  468. if prebuilt.baseLinker.Properties.System_shared_libs == nil {
  469. prebuilt.baseLinker.Properties.System_shared_libs = []string{}
  470. }
  471. module.compiler = nil
  472. module.linker = prebuilt
  473. module.installer = prebuilt
  474. prebuilt.Init(module, image, moduleSuffix)
  475. module.AddProperties(
  476. &prebuilt.properties,
  477. &prebuilt.sanitizerProperties,
  478. )
  479. return module, prebuilt
  480. }
  481. // vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by
  482. // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared
  483. // overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
  484. // is set.
  485. func VendorSnapshotSharedFactory() android.Module {
  486. module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotSharedSuffix)
  487. prebuilt.libraryDecorator.BuildOnlyShared()
  488. return module.Init()
  489. }
  490. // recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by
  491. // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared
  492. // overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION
  493. // is set.
  494. func RecoverySnapshotSharedFactory() android.Module {
  495. module, prebuilt := snapshotLibraryFactory(RecoverySnapshotImageSingleton, SnapshotSharedSuffix)
  496. prebuilt.libraryDecorator.BuildOnlyShared()
  497. return module.Init()
  498. }
  499. // vendor_snapshot_static is a special prebuilt static library which is auto-generated by
  500. // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static
  501. // overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION
  502. // is set.
  503. func VendorSnapshotStaticFactory() android.Module {
  504. module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, SnapshotStaticSuffix)
  505. prebuilt.libraryDecorator.BuildOnlyStatic()
  506. return module.Init()
  507. }
  508. // recovery_snapshot_static is a special prebuilt static library which is auto-generated by
  509. // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static
  510. // overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION
  511. // is set.
  512. func RecoverySnapshotStaticFactory() android.Module {
  513. module, prebuilt := snapshotLibraryFactory(RecoverySnapshotImageSingleton, SnapshotStaticSuffix)
  514. prebuilt.libraryDecorator.BuildOnlyStatic()
  515. return module.Init()
  516. }
  517. // vendor_snapshot_header is a special header library which is auto-generated by
  518. // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header
  519. // overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION
  520. // is set.
  521. func VendorSnapshotHeaderFactory() android.Module {
  522. module, prebuilt := snapshotLibraryFactory(VendorSnapshotImageSingleton, snapshotHeaderSuffix)
  523. prebuilt.libraryDecorator.HeaderOnly()
  524. return module.Init()
  525. }
  526. // recovery_snapshot_header is a special header library which is auto-generated by
  527. // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header
  528. // overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION
  529. // is set.
  530. func RecoverySnapshotHeaderFactory() android.Module {
  531. module, prebuilt := snapshotLibraryFactory(RecoverySnapshotImageSingleton, snapshotHeaderSuffix)
  532. prebuilt.libraryDecorator.HeaderOnly()
  533. return module.Init()
  534. }
  535. // Module definitions for snapshots of executable binaries.
  536. //
  537. // Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable
  538. // binaries (e.g. toybox, sh) as their src, which can be installed.
  539. //
  540. // These modules are auto-generated by development/vendor_snapshot/update.py.
  541. type snapshotBinaryProperties struct {
  542. // Prebuilt file for each arch.
  543. Src *string `android:"arch_variant"`
  544. }
  545. type snapshotBinaryDecorator struct {
  546. BaseSnapshotDecorator
  547. *binaryDecorator
  548. properties snapshotBinaryProperties
  549. }
  550. func (p *snapshotBinaryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
  551. if config.DeviceArch() != p.Arch() {
  552. return false
  553. }
  554. if p.properties.Src == nil {
  555. return false
  556. }
  557. return true
  558. }
  559. // cc modules' link functions are to link compiled objects into final binaries.
  560. // As snapshots are prebuilts, this just returns the prebuilt binary
  561. func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
  562. p.SetSnapshotAndroidMkSuffix(ctx, snapshotBinarySuffix)
  563. if !p.MatchesWithDevice(ctx.DeviceConfig()) {
  564. return nil
  565. }
  566. in := android.PathForModuleSrc(ctx, *p.properties.Src)
  567. p.unstrippedOutputFile = in
  568. binName := in.Base()
  569. // use cpExecutable to make it executable
  570. outputFile := android.PathForModuleOut(ctx, binName)
  571. ctx.Build(pctx, android.BuildParams{
  572. Rule: android.CpExecutable,
  573. Description: "prebuilt",
  574. Output: outputFile,
  575. Input: in,
  576. })
  577. // binary snapshots need symlinking
  578. p.setSymlinkList(ctx)
  579. return outputFile
  580. }
  581. func (p *snapshotBinaryDecorator) nativeCoverage() bool {
  582. return false
  583. }
  584. // vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by
  585. // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary
  586. // overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
  587. func VendorSnapshotBinaryFactory() android.Module {
  588. return snapshotBinaryFactory(VendorSnapshotImageSingleton, snapshotBinarySuffix)
  589. }
  590. // recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by
  591. // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary
  592. // overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set.
  593. func RecoverySnapshotBinaryFactory() android.Module {
  594. return snapshotBinaryFactory(RecoverySnapshotImageSingleton, snapshotBinarySuffix)
  595. }
  596. func snapshotBinaryFactory(image SnapshotImage, moduleSuffix string) android.Module {
  597. module, binary := NewBinary(android.DeviceSupported)
  598. binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
  599. binary.baseLinker.Properties.Nocrt = BoolPtr(true)
  600. // Prevent default system libs (libc, libm, and libdl) from being linked
  601. if binary.baseLinker.Properties.System_shared_libs == nil {
  602. binary.baseLinker.Properties.System_shared_libs = []string{}
  603. }
  604. prebuilt := &snapshotBinaryDecorator{
  605. binaryDecorator: binary,
  606. }
  607. module.compiler = nil
  608. module.sanitize = nil
  609. module.stl = nil
  610. module.linker = prebuilt
  611. prebuilt.Init(module, image, moduleSuffix)
  612. module.AddProperties(&prebuilt.properties)
  613. return module.Init()
  614. }
  615. // Module definitions for snapshots of object files (*.o).
  616. //
  617. // Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object
  618. // files (*.o) as their src.
  619. //
  620. // These modules are auto-generated by development/vendor_snapshot/update.py.
  621. type vendorSnapshotObjectProperties struct {
  622. // Prebuilt file for each arch.
  623. Src *string `android:"arch_variant"`
  624. }
  625. type snapshotObjectLinker struct {
  626. BaseSnapshotDecorator
  627. objectLinker
  628. properties vendorSnapshotObjectProperties
  629. }
  630. func (p *snapshotObjectLinker) MatchesWithDevice(config android.DeviceConfig) bool {
  631. if config.DeviceArch() != p.Arch() {
  632. return false
  633. }
  634. if p.properties.Src == nil {
  635. return false
  636. }
  637. return true
  638. }
  639. // cc modules' link functions are to link compiled objects into final binaries.
  640. // As snapshots are prebuilts, this just returns the prebuilt binary
  641. func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
  642. p.SetSnapshotAndroidMkSuffix(ctx, snapshotObjectSuffix)
  643. if !p.MatchesWithDevice(ctx.DeviceConfig()) {
  644. return nil
  645. }
  646. return android.PathForModuleSrc(ctx, *p.properties.Src)
  647. }
  648. func (p *snapshotObjectLinker) nativeCoverage() bool {
  649. return false
  650. }
  651. // vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by
  652. // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object
  653. // overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
  654. func VendorSnapshotObjectFactory() android.Module {
  655. module := newObject(android.DeviceSupported)
  656. prebuilt := &snapshotObjectLinker{
  657. objectLinker: objectLinker{
  658. baseLinker: NewBaseLinker(nil),
  659. },
  660. }
  661. module.linker = prebuilt
  662. prebuilt.Init(module, VendorSnapshotImageSingleton, snapshotObjectSuffix)
  663. module.AddProperties(&prebuilt.properties)
  664. // vendor_snapshot_object module does not provide sanitizer variants
  665. module.sanitize.Properties.Sanitize.Never = BoolPtr(true)
  666. return module.Init()
  667. }
  668. // recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by
  669. // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object
  670. // overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set.
  671. func RecoverySnapshotObjectFactory() android.Module {
  672. module := newObject(android.DeviceSupported)
  673. prebuilt := &snapshotObjectLinker{
  674. objectLinker: objectLinker{
  675. baseLinker: NewBaseLinker(nil),
  676. },
  677. }
  678. module.linker = prebuilt
  679. prebuilt.Init(module, RecoverySnapshotImageSingleton, snapshotObjectSuffix)
  680. module.AddProperties(&prebuilt.properties)
  681. return module.Init()
  682. }
  683. type SnapshotInterface interface {
  684. MatchesWithDevice(config android.DeviceConfig) bool
  685. IsSnapshotPrebuilt() bool
  686. Version() string
  687. SnapshotAndroidMkSuffix() string
  688. }
  689. var _ SnapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
  690. var _ SnapshotInterface = (*snapshotLibraryDecorator)(nil)
  691. var _ SnapshotInterface = (*snapshotBinaryDecorator)(nil)
  692. var _ SnapshotInterface = (*snapshotObjectLinker)(nil)