Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame^] | 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 | |
| 16 | // This file defines snapshot prebuilt modules, e.g. vendor snapshot and recovery snapshot. Such |
| 17 | // snapshot modules will override original source modules with setting BOARD_VNDK_VERSION, with |
| 18 | // snapshot mutators and snapshot information maps which are also defined in this file. |
| 19 | |
| 20 | import ( |
| 21 | "strings" |
| 22 | "sync" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | // Defines the specifics of different images to which the snapshot process is applicable, e.g., |
| 28 | // vendor, recovery, ramdisk. |
| 29 | type snapshotImage interface { |
| 30 | // Used to register callbacks with the build system. |
| 31 | init() |
| 32 | |
| 33 | // Function that returns true if the module is included in this image. |
| 34 | // Using a function return instead of a value to prevent early |
| 35 | // evalution of a function that may be not be defined. |
| 36 | inImage(m *Module) func() bool |
| 37 | |
| 38 | // Returns the value of the "available" property for a given module for |
| 39 | // and snapshot, e.g., "vendor_available", "recovery_available", etc. |
| 40 | // or nil if the property is not defined. |
| 41 | available(m *Module) *bool |
| 42 | |
| 43 | // Returns true if a dir under source tree is an SoC-owned proprietary |
| 44 | // directory, such as device/, vendor/, etc. |
| 45 | // |
| 46 | // For a given snapshot (e.g., vendor, recovery, etc.) if |
| 47 | // isProprietaryPath(dir) returns true, then the module in dir will be |
| 48 | // built from sources. |
| 49 | isProprietaryPath(dir string) bool |
| 50 | |
| 51 | // Whether to include VNDK in the snapshot for this image. |
| 52 | includeVndk() bool |
| 53 | |
| 54 | // Whether a given module has been explicitly excluded from the |
| 55 | // snapshot, e.g., using the exclude_from_vendor_snapshot or |
| 56 | // exclude_from_recovery_snapshot properties. |
| 57 | excludeFromSnapshot(m *Module) bool |
| 58 | } |
| 59 | |
| 60 | type vendorSnapshotImage struct{} |
| 61 | type recoverySnapshotImage struct{} |
| 62 | |
| 63 | func (vendorSnapshotImage) init() { |
| 64 | android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton) |
| 65 | android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory) |
| 66 | android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory) |
| 67 | android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory) |
| 68 | android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory) |
| 69 | android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory) |
| 70 | } |
| 71 | |
| 72 | func (vendorSnapshotImage) inImage(m *Module) func() bool { |
| 73 | return m.inVendor |
| 74 | } |
| 75 | |
| 76 | func (vendorSnapshotImage) available(m *Module) *bool { |
| 77 | return m.VendorProperties.Vendor_available |
| 78 | } |
| 79 | |
| 80 | func (vendorSnapshotImage) isProprietaryPath(dir string) bool { |
| 81 | return isVendorProprietaryPath(dir) |
| 82 | } |
| 83 | |
| 84 | // vendor snapshot includes static/header libraries with vndk: {enabled: true}. |
| 85 | func (vendorSnapshotImage) includeVndk() bool { |
| 86 | return true |
| 87 | } |
| 88 | |
| 89 | func (vendorSnapshotImage) excludeFromSnapshot(m *Module) bool { |
| 90 | return m.ExcludeFromVendorSnapshot() |
| 91 | } |
| 92 | |
| 93 | func (recoverySnapshotImage) init() { |
| 94 | android.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton) |
| 95 | android.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory) |
| 96 | android.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory) |
| 97 | android.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory) |
| 98 | android.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory) |
| 99 | android.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory) |
| 100 | } |
| 101 | |
| 102 | func (recoverySnapshotImage) inImage(m *Module) func() bool { |
| 103 | return m.InRecovery |
| 104 | } |
| 105 | |
| 106 | func (recoverySnapshotImage) available(m *Module) *bool { |
| 107 | return m.Properties.Recovery_available |
| 108 | } |
| 109 | |
| 110 | func (recoverySnapshotImage) isProprietaryPath(dir string) bool { |
| 111 | return isRecoveryProprietaryPath(dir) |
| 112 | } |
| 113 | |
| 114 | // recovery snapshot does NOT treat vndk specially. |
| 115 | func (recoverySnapshotImage) includeVndk() bool { |
| 116 | return false |
| 117 | } |
| 118 | |
| 119 | func (recoverySnapshotImage) excludeFromSnapshot(m *Module) bool { |
| 120 | return m.ExcludeFromRecoverySnapshot() |
| 121 | } |
| 122 | |
| 123 | var vendorSnapshotImageSingleton vendorSnapshotImage |
| 124 | var recoverySnapshotImageSingleton recoverySnapshotImage |
| 125 | |
| 126 | func init() { |
| 127 | vendorSnapshotImageSingleton.init() |
| 128 | recoverySnapshotImageSingleton.init() |
| 129 | } |
| 130 | |
| 131 | const ( |
| 132 | vendorSnapshotHeaderSuffix = ".vendor_header." |
| 133 | vendorSnapshotSharedSuffix = ".vendor_shared." |
| 134 | vendorSnapshotStaticSuffix = ".vendor_static." |
| 135 | vendorSnapshotBinarySuffix = ".vendor_binary." |
| 136 | vendorSnapshotObjectSuffix = ".vendor_object." |
| 137 | ) |
| 138 | |
| 139 | const ( |
| 140 | recoverySnapshotHeaderSuffix = ".recovery_header." |
| 141 | recoverySnapshotSharedSuffix = ".recovery_shared." |
| 142 | recoverySnapshotStaticSuffix = ".recovery_static." |
| 143 | recoverySnapshotBinarySuffix = ".recovery_binary." |
| 144 | recoverySnapshotObjectSuffix = ".recovery_object." |
| 145 | ) |
| 146 | |
| 147 | var ( |
| 148 | vendorSnapshotsLock sync.Mutex |
| 149 | vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules") |
| 150 | vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs") |
| 151 | vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs") |
| 152 | vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs") |
| 153 | vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries") |
| 154 | vendorSnapshotObjectsKey = android.NewOnceKey("vendorSnapshotObjects") |
| 155 | ) |
| 156 | |
| 157 | // vendorSuffixModules holds names of modules whose vendor variants should have the vendor suffix. |
| 158 | // This is determined by source modules, and then this will be used when exporting snapshot modules |
| 159 | // to Makefile. |
| 160 | // |
| 161 | // For example, if libbase has "vendor_available: true", the name of core variant will be "libbase" |
| 162 | // while the name of vendor variant will be "libbase.vendor". In such cases, the vendor snapshot of |
| 163 | // "libbase" should be exported with the name "libbase.vendor". |
| 164 | // |
| 165 | // Refer to VendorSnapshotSourceMutator and makeLibName which use this. |
| 166 | func vendorSuffixModules(config android.Config) map[string]bool { |
| 167 | return config.Once(vendorSuffixModulesKey, func() interface{} { |
| 168 | return make(map[string]bool) |
| 169 | }).(map[string]bool) |
| 170 | } |
| 171 | |
| 172 | // these are vendor snapshot maps holding names of vendor snapshot modules |
| 173 | func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap { |
| 174 | return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} { |
| 175 | return newSnapshotMap() |
| 176 | }).(*snapshotMap) |
| 177 | } |
| 178 | |
| 179 | func vendorSnapshotSharedLibs(config android.Config) *snapshotMap { |
| 180 | return config.Once(vendorSnapshotSharedLibsKey, func() interface{} { |
| 181 | return newSnapshotMap() |
| 182 | }).(*snapshotMap) |
| 183 | } |
| 184 | |
| 185 | func vendorSnapshotStaticLibs(config android.Config) *snapshotMap { |
| 186 | return config.Once(vendorSnapshotStaticLibsKey, func() interface{} { |
| 187 | return newSnapshotMap() |
| 188 | }).(*snapshotMap) |
| 189 | } |
| 190 | |
| 191 | func vendorSnapshotBinaries(config android.Config) *snapshotMap { |
| 192 | return config.Once(vendorSnapshotBinariesKey, func() interface{} { |
| 193 | return newSnapshotMap() |
| 194 | }).(*snapshotMap) |
| 195 | } |
| 196 | |
| 197 | func vendorSnapshotObjects(config android.Config) *snapshotMap { |
| 198 | return config.Once(vendorSnapshotObjectsKey, func() interface{} { |
| 199 | return newSnapshotMap() |
| 200 | }).(*snapshotMap) |
| 201 | } |
| 202 | |
| 203 | type baseSnapshotDecoratorProperties struct { |
| 204 | // snapshot version. |
| 205 | Version string |
| 206 | |
| 207 | // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64') |
| 208 | Target_arch string |
| 209 | } |
| 210 | |
| 211 | // baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot |
| 212 | // version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't |
| 213 | // collide with source modules. e.g. the following example module, |
| 214 | // |
| 215 | // vendor_snapshot_static { |
| 216 | // name: "libbase", |
| 217 | // arch: "arm64", |
| 218 | // version: 30, |
| 219 | // ... |
| 220 | // } |
| 221 | // |
| 222 | // will be seen as "libbase.vendor_static.30.arm64" by Soong. |
| 223 | type baseSnapshotDecorator struct { |
| 224 | baseProperties baseSnapshotDecoratorProperties |
| 225 | moduleSuffix string |
| 226 | } |
| 227 | |
| 228 | func (p *baseSnapshotDecorator) Name(name string) string { |
| 229 | return name + p.NameSuffix() |
| 230 | } |
| 231 | |
| 232 | func (p *baseSnapshotDecorator) NameSuffix() string { |
| 233 | versionSuffix := p.version() |
| 234 | if p.arch() != "" { |
| 235 | versionSuffix += "." + p.arch() |
| 236 | } |
| 237 | |
| 238 | return p.moduleSuffix + versionSuffix |
| 239 | } |
| 240 | |
| 241 | func (p *baseSnapshotDecorator) version() string { |
| 242 | return p.baseProperties.Version |
| 243 | } |
| 244 | |
| 245 | func (p *baseSnapshotDecorator) arch() string { |
| 246 | return p.baseProperties.Target_arch |
| 247 | } |
| 248 | |
| 249 | func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool { |
| 250 | return true |
| 251 | } |
| 252 | |
| 253 | // Call this with a module suffix after creating a snapshot module, such as |
| 254 | // vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc. |
| 255 | func (p *baseSnapshotDecorator) init(m *Module, suffix string) { |
| 256 | p.moduleSuffix = suffix |
| 257 | m.AddProperties(&p.baseProperties) |
| 258 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 259 | vendorSnapshotLoadHook(ctx, p) |
| 260 | }) |
| 261 | } |
| 262 | |
| 263 | // vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION. |
| 264 | // As vendor snapshot is only for vendor, such modules won't be used at all. |
| 265 | func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) { |
| 266 | if p.version() != ctx.DeviceConfig().VndkVersion() { |
| 267 | ctx.Module().Disable() |
| 268 | return |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // |
| 273 | // Module definitions for snapshots of libraries (shared, static, header). |
| 274 | // |
| 275 | // Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and |
| 276 | // static libraries have their prebuilt library files (.so for shared, .a for static) as their src, |
| 277 | // which can be installed or linked against. Also they export flags needed when linked, such as |
| 278 | // include directories, c flags, sanitize dependency information, etc. |
| 279 | // |
| 280 | // These modules are auto-generated by development/vendor_snapshot/update.py. |
| 281 | type snapshotLibraryProperties struct { |
| 282 | // Prebuilt file for each arch. |
| 283 | Src *string `android:"arch_variant"` |
| 284 | |
| 285 | // list of directories that will be added to the include path (using -I). |
| 286 | Export_include_dirs []string `android:"arch_variant"` |
| 287 | |
| 288 | // list of directories that will be added to the system path (using -isystem). |
| 289 | Export_system_include_dirs []string `android:"arch_variant"` |
| 290 | |
| 291 | // list of flags that will be used for any module that links against this module. |
| 292 | Export_flags []string `android:"arch_variant"` |
| 293 | |
| 294 | // Whether this prebuilt needs to depend on sanitize ubsan runtime or not. |
| 295 | Sanitize_ubsan_dep *bool `android:"arch_variant"` |
| 296 | |
| 297 | // Whether this prebuilt needs to depend on sanitize minimal runtime or not. |
| 298 | Sanitize_minimal_dep *bool `android:"arch_variant"` |
| 299 | } |
| 300 | |
| 301 | type snapshotSanitizer interface { |
| 302 | isSanitizerEnabled(t sanitizerType) bool |
| 303 | setSanitizerVariation(t sanitizerType, enabled bool) |
| 304 | } |
| 305 | |
| 306 | type snapshotLibraryDecorator struct { |
| 307 | baseSnapshotDecorator |
| 308 | *libraryDecorator |
| 309 | properties snapshotLibraryProperties |
| 310 | sanitizerProperties struct { |
| 311 | CfiEnabled bool `blueprint:"mutated"` |
| 312 | |
| 313 | // Library flags for cfi variant. |
| 314 | Cfi snapshotLibraryProperties `android:"arch_variant"` |
| 315 | } |
| 316 | androidMkVendorSuffix bool |
| 317 | } |
| 318 | |
| 319 | func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 320 | p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix()) |
| 321 | return p.libraryDecorator.linkerFlags(ctx, flags) |
| 322 | } |
| 323 | |
| 324 | func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool { |
| 325 | arches := config.Arches() |
| 326 | if len(arches) == 0 || arches[0].ArchType.String() != p.arch() { |
| 327 | return false |
| 328 | } |
| 329 | if !p.header() && p.properties.Src == nil { |
| 330 | return false |
| 331 | } |
| 332 | return true |
| 333 | } |
| 334 | |
| 335 | // cc modules' link functions are to link compiled objects into final binaries. |
| 336 | // As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are |
| 337 | // done by normal library decorator, e.g. exporting flags. |
| 338 | func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path { |
| 339 | m := ctx.Module().(*Module) |
| 340 | p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()] |
| 341 | |
| 342 | if p.header() { |
| 343 | return p.libraryDecorator.link(ctx, flags, deps, objs) |
| 344 | } |
| 345 | |
| 346 | if p.sanitizerProperties.CfiEnabled { |
| 347 | p.properties = p.sanitizerProperties.Cfi |
| 348 | } |
| 349 | |
| 350 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 351 | return nil |
| 352 | } |
| 353 | |
| 354 | p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...) |
| 355 | p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...) |
| 356 | p.libraryDecorator.reexportFlags(p.properties.Export_flags...) |
| 357 | |
| 358 | in := android.PathForModuleSrc(ctx, *p.properties.Src) |
| 359 | p.unstrippedOutputFile = in |
| 360 | |
| 361 | if p.shared() { |
| 362 | libName := in.Base() |
| 363 | builderFlags := flagsToBuilderFlags(flags) |
| 364 | |
| 365 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 366 | // depending on a table of contents file instead of the library itself. |
| 367 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 368 | p.tocFile = android.OptionalPathForPath(tocFile) |
| 369 | transformSharedObjectToToc(ctx, in, tocFile, builderFlags) |
| 370 | |
| 371 | ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ |
| 372 | SharedLibrary: in, |
| 373 | UnstrippedSharedLibrary: p.unstrippedOutputFile, |
| 374 | |
| 375 | TableOfContents: p.tocFile, |
| 376 | }) |
| 377 | } |
| 378 | |
| 379 | if p.static() { |
| 380 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build() |
| 381 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
| 382 | StaticLibrary: in, |
| 383 | |
| 384 | TransitiveStaticLibrariesForOrdering: depSet, |
| 385 | }) |
| 386 | } |
| 387 | |
| 388 | p.libraryDecorator.flagExporter.setProvider(ctx) |
| 389 | |
| 390 | return in |
| 391 | } |
| 392 | |
| 393 | func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) { |
| 394 | if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) { |
| 395 | p.baseInstaller.install(ctx, file) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | func (p *snapshotLibraryDecorator) nativeCoverage() bool { |
| 400 | return false |
| 401 | } |
| 402 | |
| 403 | func (p *snapshotLibraryDecorator) isSanitizerEnabled(t sanitizerType) bool { |
| 404 | switch t { |
| 405 | case cfi: |
| 406 | return p.sanitizerProperties.Cfi.Src != nil |
| 407 | default: |
| 408 | return false |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | func (p *snapshotLibraryDecorator) setSanitizerVariation(t sanitizerType, enabled bool) { |
| 413 | if !enabled { |
| 414 | return |
| 415 | } |
| 416 | switch t { |
| 417 | case cfi: |
| 418 | p.sanitizerProperties.CfiEnabled = true |
| 419 | default: |
| 420 | return |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | func snapshotLibraryFactory(suffix string) (*Module, *snapshotLibraryDecorator) { |
| 425 | module, library := NewLibrary(android.DeviceSupported) |
| 426 | |
| 427 | module.stl = nil |
| 428 | module.sanitize = nil |
| 429 | library.disableStripping() |
| 430 | |
| 431 | prebuilt := &snapshotLibraryDecorator{ |
| 432 | libraryDecorator: library, |
| 433 | } |
| 434 | |
| 435 | prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 436 | prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 437 | |
| 438 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 439 | if prebuilt.baseLinker.Properties.System_shared_libs == nil { |
| 440 | prebuilt.baseLinker.Properties.System_shared_libs = []string{} |
| 441 | } |
| 442 | |
| 443 | module.compiler = nil |
| 444 | module.linker = prebuilt |
| 445 | module.installer = prebuilt |
| 446 | |
| 447 | prebuilt.init(module, suffix) |
| 448 | module.AddProperties( |
| 449 | &prebuilt.properties, |
| 450 | &prebuilt.sanitizerProperties, |
| 451 | ) |
| 452 | |
| 453 | return module, prebuilt |
| 454 | } |
| 455 | |
| 456 | // vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by |
| 457 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared |
| 458 | // overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION |
| 459 | // is set. |
| 460 | func VendorSnapshotSharedFactory() android.Module { |
| 461 | module, prebuilt := snapshotLibraryFactory(vendorSnapshotSharedSuffix) |
| 462 | prebuilt.libraryDecorator.BuildOnlyShared() |
| 463 | return module.Init() |
| 464 | } |
| 465 | |
| 466 | // recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by |
| 467 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared |
| 468 | // overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION |
| 469 | // is set. |
| 470 | func RecoverySnapshotSharedFactory() android.Module { |
| 471 | module, prebuilt := snapshotLibraryFactory(recoverySnapshotSharedSuffix) |
| 472 | prebuilt.libraryDecorator.BuildOnlyShared() |
| 473 | return module.Init() |
| 474 | } |
| 475 | |
| 476 | // vendor_snapshot_static is a special prebuilt static library which is auto-generated by |
| 477 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static |
| 478 | // overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION |
| 479 | // is set. |
| 480 | func VendorSnapshotStaticFactory() android.Module { |
| 481 | module, prebuilt := snapshotLibraryFactory(vendorSnapshotStaticSuffix) |
| 482 | prebuilt.libraryDecorator.BuildOnlyStatic() |
| 483 | return module.Init() |
| 484 | } |
| 485 | |
| 486 | // recovery_snapshot_static is a special prebuilt static library which is auto-generated by |
| 487 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static |
| 488 | // overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION |
| 489 | // is set. |
| 490 | func RecoverySnapshotStaticFactory() android.Module { |
| 491 | module, prebuilt := snapshotLibraryFactory(recoverySnapshotStaticSuffix) |
| 492 | prebuilt.libraryDecorator.BuildOnlyStatic() |
| 493 | return module.Init() |
| 494 | } |
| 495 | |
| 496 | // vendor_snapshot_header is a special header library which is auto-generated by |
| 497 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header |
| 498 | // overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION |
| 499 | // is set. |
| 500 | func VendorSnapshotHeaderFactory() android.Module { |
| 501 | module, prebuilt := snapshotLibraryFactory(vendorSnapshotHeaderSuffix) |
| 502 | prebuilt.libraryDecorator.HeaderOnly() |
| 503 | return module.Init() |
| 504 | } |
| 505 | |
| 506 | // recovery_snapshot_header is a special header library which is auto-generated by |
| 507 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header |
| 508 | // overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION |
| 509 | // is set. |
| 510 | func RecoverySnapshotHeaderFactory() android.Module { |
| 511 | module, prebuilt := snapshotLibraryFactory(recoverySnapshotHeaderSuffix) |
| 512 | prebuilt.libraryDecorator.HeaderOnly() |
| 513 | return module.Init() |
| 514 | } |
| 515 | |
| 516 | var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil) |
| 517 | |
| 518 | // |
| 519 | // Module definitions for snapshots of executable binaries. |
| 520 | // |
| 521 | // Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable |
| 522 | // binaries (e.g. toybox, sh) as their src, which can be installed. |
| 523 | // |
| 524 | // These modules are auto-generated by development/vendor_snapshot/update.py. |
| 525 | type snapshotBinaryProperties struct { |
| 526 | // Prebuilt file for each arch. |
| 527 | Src *string `android:"arch_variant"` |
| 528 | } |
| 529 | |
| 530 | type snapshotBinaryDecorator struct { |
| 531 | baseSnapshotDecorator |
| 532 | *binaryDecorator |
| 533 | properties snapshotBinaryProperties |
| 534 | androidMkVendorSuffix bool |
| 535 | } |
| 536 | |
| 537 | func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool { |
| 538 | if config.DeviceArch() != p.arch() { |
| 539 | return false |
| 540 | } |
| 541 | if p.properties.Src == nil { |
| 542 | return false |
| 543 | } |
| 544 | return true |
| 545 | } |
| 546 | |
| 547 | // cc modules' link functions are to link compiled objects into final binaries. |
| 548 | // As snapshots are prebuilts, this just returns the prebuilt binary |
| 549 | func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path { |
| 550 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 551 | return nil |
| 552 | } |
| 553 | |
| 554 | in := android.PathForModuleSrc(ctx, *p.properties.Src) |
| 555 | p.unstrippedOutputFile = in |
| 556 | binName := in.Base() |
| 557 | |
| 558 | m := ctx.Module().(*Module) |
| 559 | p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()] |
| 560 | |
| 561 | // use cpExecutable to make it executable |
| 562 | outputFile := android.PathForModuleOut(ctx, binName) |
| 563 | ctx.Build(pctx, android.BuildParams{ |
| 564 | Rule: android.CpExecutable, |
| 565 | Description: "prebuilt", |
| 566 | Output: outputFile, |
| 567 | Input: in, |
| 568 | }) |
| 569 | |
| 570 | return outputFile |
| 571 | } |
| 572 | |
| 573 | func (p *snapshotBinaryDecorator) nativeCoverage() bool { |
| 574 | return false |
| 575 | } |
| 576 | |
| 577 | // vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by |
| 578 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary |
| 579 | // overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set. |
| 580 | func VendorSnapshotBinaryFactory() android.Module { |
| 581 | return snapshotBinaryFactory(vendorSnapshotBinarySuffix) |
| 582 | } |
| 583 | |
| 584 | // recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by |
| 585 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary |
| 586 | // overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set. |
| 587 | func RecoverySnapshotBinaryFactory() android.Module { |
| 588 | return snapshotBinaryFactory(recoverySnapshotBinarySuffix) |
| 589 | } |
| 590 | |
| 591 | func snapshotBinaryFactory(suffix string) android.Module { |
| 592 | module, binary := NewBinary(android.DeviceSupported) |
| 593 | binary.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 594 | binary.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 595 | |
| 596 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 597 | if binary.baseLinker.Properties.System_shared_libs == nil { |
| 598 | binary.baseLinker.Properties.System_shared_libs = []string{} |
| 599 | } |
| 600 | |
| 601 | prebuilt := &snapshotBinaryDecorator{ |
| 602 | binaryDecorator: binary, |
| 603 | } |
| 604 | |
| 605 | module.compiler = nil |
| 606 | module.sanitize = nil |
| 607 | module.stl = nil |
| 608 | module.linker = prebuilt |
| 609 | |
| 610 | prebuilt.init(module, suffix) |
| 611 | module.AddProperties(&prebuilt.properties) |
| 612 | return module.Init() |
| 613 | } |
| 614 | |
| 615 | // |
| 616 | // Module definitions for snapshots of object files (*.o). |
| 617 | // |
| 618 | // Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object |
| 619 | // files (*.o) as their src. |
| 620 | // |
| 621 | // These modules are auto-generated by development/vendor_snapshot/update.py. |
| 622 | type vendorSnapshotObjectProperties struct { |
| 623 | // Prebuilt file for each arch. |
| 624 | Src *string `android:"arch_variant"` |
| 625 | } |
| 626 | |
| 627 | type snapshotObjectLinker struct { |
| 628 | baseSnapshotDecorator |
| 629 | objectLinker |
| 630 | properties vendorSnapshotObjectProperties |
| 631 | androidMkVendorSuffix bool |
| 632 | } |
| 633 | |
| 634 | func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool { |
| 635 | if config.DeviceArch() != p.arch() { |
| 636 | return false |
| 637 | } |
| 638 | if p.properties.Src == nil { |
| 639 | return false |
| 640 | } |
| 641 | return true |
| 642 | } |
| 643 | |
| 644 | // cc modules' link functions are to link compiled objects into final binaries. |
| 645 | // As snapshots are prebuilts, this just returns the prebuilt binary |
| 646 | func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path { |
| 647 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 648 | return nil |
| 649 | } |
| 650 | |
| 651 | m := ctx.Module().(*Module) |
| 652 | p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()] |
| 653 | |
| 654 | return android.PathForModuleSrc(ctx, *p.properties.Src) |
| 655 | } |
| 656 | |
| 657 | func (p *snapshotObjectLinker) nativeCoverage() bool { |
| 658 | return false |
| 659 | } |
| 660 | |
| 661 | // vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by |
| 662 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object |
| 663 | // overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set. |
| 664 | func VendorSnapshotObjectFactory() android.Module { |
| 665 | module := newObject() |
| 666 | |
| 667 | prebuilt := &snapshotObjectLinker{ |
| 668 | objectLinker: objectLinker{ |
| 669 | baseLinker: NewBaseLinker(nil), |
| 670 | }, |
| 671 | } |
| 672 | module.linker = prebuilt |
| 673 | |
| 674 | prebuilt.init(module, vendorSnapshotObjectSuffix) |
| 675 | module.AddProperties(&prebuilt.properties) |
| 676 | return module.Init() |
| 677 | } |
| 678 | |
| 679 | // recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by |
| 680 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object |
| 681 | // overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set. |
| 682 | func RecoverySnapshotObjectFactory() android.Module { |
| 683 | module := newObject() |
| 684 | |
| 685 | prebuilt := &snapshotObjectLinker{ |
| 686 | objectLinker: objectLinker{ |
| 687 | baseLinker: NewBaseLinker(nil), |
| 688 | }, |
| 689 | } |
| 690 | module.linker = prebuilt |
| 691 | |
| 692 | prebuilt.init(module, recoverySnapshotObjectSuffix) |
| 693 | module.AddProperties(&prebuilt.properties) |
| 694 | return module.Init() |
| 695 | } |
| 696 | |
| 697 | type snapshotInterface interface { |
| 698 | matchesWithDevice(config android.DeviceConfig) bool |
| 699 | } |
| 700 | |
| 701 | var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil) |
| 702 | var _ snapshotInterface = (*snapshotLibraryDecorator)(nil) |
| 703 | var _ snapshotInterface = (*snapshotBinaryDecorator)(nil) |
| 704 | var _ snapshotInterface = (*snapshotObjectLinker)(nil) |
| 705 | |
| 706 | // |
| 707 | // Mutators that helps vendor snapshot modules override source modules. |
| 708 | // |
| 709 | |
| 710 | // VendorSnapshotMutator gathers all snapshots for vendor, and disable all snapshots which don't |
| 711 | // match with device, e.g. |
| 712 | // - snapshot version is different with BOARD_VNDK_VERSION |
| 713 | // - snapshot arch is different with device's arch (e.g. arm vs x86) |
| 714 | // |
| 715 | // This also handles vndk_prebuilt_shared, except for they won't be disabled in any cases, given |
| 716 | // that any versions of VNDK might be packed into vndk APEX. |
| 717 | // |
| 718 | // TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules |
| 719 | func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) { |
| 720 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 721 | // don't need snapshot if current |
| 722 | if vndkVersion == "current" || vndkVersion == "" { |
| 723 | return |
| 724 | } |
| 725 | |
| 726 | module, ok := ctx.Module().(*Module) |
| 727 | if !ok || !module.Enabled() || module.VndkVersion() != vndkVersion { |
| 728 | return |
| 729 | } |
| 730 | |
| 731 | if !module.isSnapshotPrebuilt() { |
| 732 | return |
| 733 | } |
| 734 | |
| 735 | // isSnapshotPrebuilt ensures snapshotInterface |
| 736 | if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) { |
| 737 | // Disable unnecessary snapshot module, but do not disable |
| 738 | // vndk_prebuilt_shared because they might be packed into vndk APEX |
| 739 | if !module.IsVndk() { |
| 740 | module.Disable() |
| 741 | } |
| 742 | return |
| 743 | } |
| 744 | |
| 745 | var snapshotMap *snapshotMap |
| 746 | |
| 747 | if lib, ok := module.linker.(libraryInterface); ok { |
| 748 | if lib.static() { |
| 749 | snapshotMap = vendorSnapshotStaticLibs(ctx.Config()) |
| 750 | } else if lib.shared() { |
| 751 | snapshotMap = vendorSnapshotSharedLibs(ctx.Config()) |
| 752 | } else { |
| 753 | // header |
| 754 | snapshotMap = vendorSnapshotHeaderLibs(ctx.Config()) |
| 755 | } |
| 756 | } else if _, ok := module.linker.(*snapshotBinaryDecorator); ok { |
| 757 | snapshotMap = vendorSnapshotBinaries(ctx.Config()) |
| 758 | } else if _, ok := module.linker.(*snapshotObjectLinker); ok { |
| 759 | snapshotMap = vendorSnapshotObjects(ctx.Config()) |
| 760 | } else { |
| 761 | return |
| 762 | } |
| 763 | |
| 764 | vendorSnapshotsLock.Lock() |
| 765 | defer vendorSnapshotsLock.Unlock() |
| 766 | snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName()) |
| 767 | } |
| 768 | |
| 769 | // VendorSnapshotSourceMutator disables source modules which have corresponding snapshots. |
| 770 | func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) { |
| 771 | if !ctx.Device() { |
| 772 | return |
| 773 | } |
| 774 | |
| 775 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 776 | // don't need snapshot if current |
| 777 | if vndkVersion == "current" || vndkVersion == "" { |
| 778 | return |
| 779 | } |
| 780 | |
| 781 | module, ok := ctx.Module().(*Module) |
| 782 | if !ok { |
| 783 | return |
| 784 | } |
| 785 | |
| 786 | // vendor suffix should be added to snapshots if the source module isn't vendor: true. |
| 787 | if !module.SocSpecific() { |
| 788 | // But we can't just check SocSpecific() since we already passed the image mutator. |
| 789 | // Check ramdisk and recovery to see if we are real "vendor: true" module. |
| 790 | ramdisk_available := module.InRamdisk() && !module.OnlyInRamdisk() |
| 791 | vendor_ramdisk_available := module.InVendorRamdisk() && !module.OnlyInVendorRamdisk() |
| 792 | recovery_available := module.InRecovery() && !module.OnlyInRecovery() |
| 793 | |
| 794 | if !ramdisk_available && !recovery_available && !vendor_ramdisk_available { |
| 795 | vendorSnapshotsLock.Lock() |
| 796 | defer vendorSnapshotsLock.Unlock() |
| 797 | |
| 798 | vendorSuffixModules(ctx.Config())[ctx.ModuleName()] = true |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if module.isSnapshotPrebuilt() || module.VndkVersion() != ctx.DeviceConfig().VndkVersion() { |
| 803 | // only non-snapshot modules with BOARD_VNDK_VERSION |
| 804 | return |
| 805 | } |
| 806 | |
| 807 | // .. and also filter out llndk library |
| 808 | if module.isLlndk(ctx.Config()) { |
| 809 | return |
| 810 | } |
| 811 | |
| 812 | var snapshotMap *snapshotMap |
| 813 | |
| 814 | if lib, ok := module.linker.(libraryInterface); ok { |
| 815 | if lib.static() { |
| 816 | snapshotMap = vendorSnapshotStaticLibs(ctx.Config()) |
| 817 | } else if lib.shared() { |
| 818 | snapshotMap = vendorSnapshotSharedLibs(ctx.Config()) |
| 819 | } else { |
| 820 | // header |
| 821 | snapshotMap = vendorSnapshotHeaderLibs(ctx.Config()) |
| 822 | } |
| 823 | } else if module.binary() { |
| 824 | snapshotMap = vendorSnapshotBinaries(ctx.Config()) |
| 825 | } else if module.object() { |
| 826 | snapshotMap = vendorSnapshotObjects(ctx.Config()) |
| 827 | } else { |
| 828 | return |
| 829 | } |
| 830 | |
| 831 | if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok { |
| 832 | // Corresponding snapshot doesn't exist |
| 833 | return |
| 834 | } |
| 835 | |
| 836 | // Disables source modules if corresponding snapshot exists. |
| 837 | if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() { |
| 838 | // But do not disable because the shared variant depends on the static variant. |
| 839 | module.SkipInstall() |
| 840 | module.Properties.HideFromMake = true |
| 841 | } else { |
| 842 | module.Disable() |
| 843 | } |
| 844 | } |