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" |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/proptools" |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | // Defines the specifics of different images to which the snapshot process is applicable, e.g., |
| 30 | // vendor, recovery, ramdisk. |
| 31 | type snapshotImage interface { |
| 32 | // Used to register callbacks with the build system. |
| 33 | init() |
| 34 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 35 | // Returns true if a snapshot should be generated for this image. |
| 36 | shouldGenerateSnapshot(ctx android.SingletonContext) bool |
| 37 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 38 | // Function that returns true if the module is included in this image. |
| 39 | // Using a function return instead of a value to prevent early |
| 40 | // evalution of a function that may be not be defined. |
| 41 | inImage(m *Module) func() bool |
| 42 | |
Justin Yun | e09ac17 | 2021-01-20 19:49:01 +0900 | [diff] [blame] | 43 | // Returns true if the module is private and must not be included in the |
| 44 | // snapshot. For example VNDK-private modules must return true for the |
| 45 | // vendor snapshots. But false for the recovery snapshots. |
| 46 | private(m *Module) bool |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 47 | |
| 48 | // Returns true if a dir under source tree is an SoC-owned proprietary |
| 49 | // directory, such as device/, vendor/, etc. |
| 50 | // |
| 51 | // For a given snapshot (e.g., vendor, recovery, etc.) if |
| 52 | // isProprietaryPath(dir) returns true, then the module in dir will be |
| 53 | // built from sources. |
| 54 | isProprietaryPath(dir string) bool |
| 55 | |
| 56 | // Whether to include VNDK in the snapshot for this image. |
| 57 | includeVndk() bool |
| 58 | |
| 59 | // Whether a given module has been explicitly excluded from the |
| 60 | // snapshot, e.g., using the exclude_from_vendor_snapshot or |
| 61 | // exclude_from_recovery_snapshot properties. |
| 62 | excludeFromSnapshot(m *Module) bool |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 63 | |
| 64 | // Returns the snapshotMap to be used for a given module and config, or nil if the |
| 65 | // module is not included in this image. |
| 66 | getSnapshotMap(m *Module, cfg android.Config) *snapshotMap |
| 67 | |
| 68 | // Returns mutex used for mutual exclusion when updating the snapshot maps. |
| 69 | getMutex() *sync.Mutex |
| 70 | |
| 71 | // For a given arch, a maps of which modules are included in this image. |
| 72 | suffixModules(config android.Config) map[string]bool |
| 73 | |
| 74 | // Whether to add a given module to the suffix map. |
| 75 | shouldBeAddedToSuffixModules(m *Module) bool |
| 76 | |
| 77 | // Returns true if the build is using a snapshot for this image. |
| 78 | isUsingSnapshot(cfg android.DeviceConfig) bool |
| 79 | |
| 80 | // Whether to skip the module mutator for a module in a given context. |
| 81 | skipModuleMutator(ctx android.BottomUpMutatorContext) bool |
| 82 | |
| 83 | // Whether to skip the source mutator for a given module. |
| 84 | skipSourceMutator(ctx android.BottomUpMutatorContext) bool |
Inseob Kim | 7cf1465 | 2021-01-06 23:06:52 +0900 | [diff] [blame] | 85 | |
| 86 | // Whether to exclude a given module from the directed snapshot or not. |
| 87 | // If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on, |
| 88 | // and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured. |
| 89 | excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | type vendorSnapshotImage struct{} |
| 93 | type recoverySnapshotImage struct{} |
| 94 | |
| 95 | func (vendorSnapshotImage) init() { |
| 96 | android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton) |
| 97 | android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory) |
| 98 | android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory) |
| 99 | android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory) |
| 100 | android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory) |
| 101 | android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory) |
Inseob Kim | e9aec6a | 2021-01-05 20:03:22 +0900 | [diff] [blame] | 102 | |
| 103 | android.RegisterSingletonType("vendor-fake-snapshot", VendorFakeSnapshotSingleton) |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 104 | } |
| 105 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 106 | func (vendorSnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool { |
| 107 | // BOARD_VNDK_VERSION must be set to 'current' in order to generate a snapshot. |
| 108 | return ctx.DeviceConfig().VndkVersion() == "current" |
| 109 | } |
| 110 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 111 | func (vendorSnapshotImage) inImage(m *Module) func() bool { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 112 | return m.InVendor |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 113 | } |
| 114 | |
Justin Yun | e09ac17 | 2021-01-20 19:49:01 +0900 | [diff] [blame] | 115 | func (vendorSnapshotImage) private(m *Module) bool { |
| 116 | return m.IsVndkPrivate() |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func (vendorSnapshotImage) isProprietaryPath(dir string) bool { |
| 120 | return isVendorProprietaryPath(dir) |
| 121 | } |
| 122 | |
| 123 | // vendor snapshot includes static/header libraries with vndk: {enabled: true}. |
| 124 | func (vendorSnapshotImage) includeVndk() bool { |
| 125 | return true |
| 126 | } |
| 127 | |
| 128 | func (vendorSnapshotImage) excludeFromSnapshot(m *Module) bool { |
| 129 | return m.ExcludeFromVendorSnapshot() |
| 130 | } |
| 131 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 132 | func (vendorSnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap { |
| 133 | if lib, ok := m.linker.(libraryInterface); ok { |
| 134 | if lib.static() { |
| 135 | return vendorSnapshotStaticLibs(cfg) |
| 136 | } else if lib.shared() { |
| 137 | return vendorSnapshotSharedLibs(cfg) |
| 138 | } else { |
| 139 | // header |
| 140 | return vendorSnapshotHeaderLibs(cfg) |
| 141 | } |
| 142 | } else if m.binary() { |
| 143 | return vendorSnapshotBinaries(cfg) |
| 144 | } else if m.object() { |
| 145 | return vendorSnapshotObjects(cfg) |
| 146 | } else { |
| 147 | return nil |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func (vendorSnapshotImage) getMutex() *sync.Mutex { |
| 152 | return &vendorSnapshotsLock |
| 153 | } |
| 154 | |
| 155 | func (vendorSnapshotImage) suffixModules(config android.Config) map[string]bool { |
| 156 | return vendorSuffixModules(config) |
| 157 | } |
| 158 | |
| 159 | func (vendorSnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool { |
| 160 | // vendor suffix should be added to snapshots if the source module isn't vendor: true. |
| 161 | if module.SocSpecific() { |
| 162 | return false |
| 163 | } |
| 164 | |
| 165 | // But we can't just check SocSpecific() since we already passed the image mutator. |
| 166 | // Check ramdisk and recovery to see if we are real "vendor: true" module. |
| 167 | ramdiskAvailable := module.InRamdisk() && !module.OnlyInRamdisk() |
| 168 | vendorRamdiskAvailable := module.InVendorRamdisk() && !module.OnlyInVendorRamdisk() |
| 169 | recoveryAvailable := module.InRecovery() && !module.OnlyInRecovery() |
| 170 | |
| 171 | return !ramdiskAvailable && !recoveryAvailable && !vendorRamdiskAvailable |
| 172 | } |
| 173 | |
| 174 | func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool { |
| 175 | vndkVersion := cfg.VndkVersion() |
| 176 | return vndkVersion != "current" && vndkVersion != "" |
| 177 | } |
| 178 | |
| 179 | func (vendorSnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool { |
| 180 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 181 | module, ok := ctx.Module().(*Module) |
| 182 | return !ok || module.VndkVersion() != vndkVersion |
| 183 | } |
| 184 | |
| 185 | func (vendorSnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool { |
| 186 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 187 | module, ok := ctx.Module().(*Module) |
| 188 | if !ok { |
| 189 | return true |
| 190 | } |
| 191 | if module.VndkVersion() != vndkVersion { |
| 192 | return true |
| 193 | } |
| 194 | // .. and also filter out llndk library |
| 195 | if module.IsLlndk() { |
| 196 | return true |
| 197 | } |
| 198 | return false |
| 199 | } |
| 200 | |
Inseob Kim | 7cf1465 | 2021-01-06 23:06:52 +0900 | [diff] [blame] | 201 | // returns true iff a given module SHOULD BE EXCLUDED, false if included |
| 202 | func (vendorSnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool { |
| 203 | // If we're using full snapshot, not directed snapshot, capture every module |
| 204 | if !cfg.DirectedVendorSnapshot() { |
| 205 | return false |
| 206 | } |
| 207 | // Else, checks if name is in VENDOR_SNAPSHOT_MODULES. |
| 208 | return !cfg.VendorSnapshotModules()[name] |
| 209 | } |
| 210 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 211 | func (recoverySnapshotImage) init() { |
| 212 | android.RegisterSingletonType("recovery-snapshot", RecoverySnapshotSingleton) |
| 213 | android.RegisterModuleType("recovery_snapshot_shared", RecoverySnapshotSharedFactory) |
| 214 | android.RegisterModuleType("recovery_snapshot_static", RecoverySnapshotStaticFactory) |
| 215 | android.RegisterModuleType("recovery_snapshot_header", RecoverySnapshotHeaderFactory) |
| 216 | android.RegisterModuleType("recovery_snapshot_binary", RecoverySnapshotBinaryFactory) |
| 217 | android.RegisterModuleType("recovery_snapshot_object", RecoverySnapshotObjectFactory) |
| 218 | } |
| 219 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 220 | func (recoverySnapshotImage) shouldGenerateSnapshot(ctx android.SingletonContext) bool { |
| 221 | // RECOVERY_SNAPSHOT_VERSION must be set to 'current' in order to generate a |
| 222 | // snapshot. |
| 223 | return ctx.DeviceConfig().RecoverySnapshotVersion() == "current" |
| 224 | } |
| 225 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 226 | func (recoverySnapshotImage) inImage(m *Module) func() bool { |
| 227 | return m.InRecovery |
| 228 | } |
| 229 | |
Justin Yun | e09ac17 | 2021-01-20 19:49:01 +0900 | [diff] [blame] | 230 | // recovery snapshot does not have private libraries. |
| 231 | func (recoverySnapshotImage) private(m *Module) bool { |
| 232 | return false |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | func (recoverySnapshotImage) isProprietaryPath(dir string) bool { |
| 236 | return isRecoveryProprietaryPath(dir) |
| 237 | } |
| 238 | |
| 239 | // recovery snapshot does NOT treat vndk specially. |
| 240 | func (recoverySnapshotImage) includeVndk() bool { |
| 241 | return false |
| 242 | } |
| 243 | |
| 244 | func (recoverySnapshotImage) excludeFromSnapshot(m *Module) bool { |
| 245 | return m.ExcludeFromRecoverySnapshot() |
| 246 | } |
| 247 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 248 | func (recoverySnapshotImage) getSnapshotMap(m *Module, cfg android.Config) *snapshotMap { |
| 249 | if lib, ok := m.linker.(libraryInterface); ok { |
| 250 | if lib.static() { |
| 251 | return recoverySnapshotStaticLibs(cfg) |
| 252 | } else if lib.shared() { |
| 253 | return recoverySnapshotSharedLibs(cfg) |
| 254 | } else { |
| 255 | // header |
| 256 | return recoverySnapshotHeaderLibs(cfg) |
| 257 | } |
| 258 | } else if m.binary() { |
| 259 | return recoverySnapshotBinaries(cfg) |
| 260 | } else if m.object() { |
| 261 | return recoverySnapshotObjects(cfg) |
| 262 | } else { |
| 263 | return nil |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func (recoverySnapshotImage) getMutex() *sync.Mutex { |
| 268 | return &recoverySnapshotsLock |
| 269 | } |
| 270 | |
| 271 | func (recoverySnapshotImage) suffixModules(config android.Config) map[string]bool { |
| 272 | return recoverySuffixModules(config) |
| 273 | } |
| 274 | |
| 275 | func (recoverySnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool { |
| 276 | return proptools.BoolDefault(module.Properties.Recovery_available, false) |
| 277 | } |
| 278 | |
| 279 | func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool { |
| 280 | recoverySnapshotVersion := cfg.RecoverySnapshotVersion() |
| 281 | return recoverySnapshotVersion != "current" && recoverySnapshotVersion != "" |
| 282 | } |
| 283 | |
| 284 | func (recoverySnapshotImage) skipModuleMutator(ctx android.BottomUpMutatorContext) bool { |
| 285 | module, ok := ctx.Module().(*Module) |
| 286 | return !ok || !module.InRecovery() |
| 287 | } |
| 288 | |
| 289 | func (recoverySnapshotImage) skipSourceMutator(ctx android.BottomUpMutatorContext) bool { |
| 290 | module, ok := ctx.Module().(*Module) |
| 291 | return !ok || !module.InRecovery() |
| 292 | } |
| 293 | |
Inseob Kim | 7cf1465 | 2021-01-06 23:06:52 +0900 | [diff] [blame] | 294 | func (recoverySnapshotImage) excludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool { |
| 295 | // directed recovery snapshot is not implemented yet |
| 296 | return false |
| 297 | } |
| 298 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 299 | var vendorSnapshotImageSingleton vendorSnapshotImage |
| 300 | var recoverySnapshotImageSingleton recoverySnapshotImage |
| 301 | |
| 302 | func init() { |
| 303 | vendorSnapshotImageSingleton.init() |
| 304 | recoverySnapshotImageSingleton.init() |
| 305 | } |
| 306 | |
| 307 | const ( |
| 308 | vendorSnapshotHeaderSuffix = ".vendor_header." |
| 309 | vendorSnapshotSharedSuffix = ".vendor_shared." |
| 310 | vendorSnapshotStaticSuffix = ".vendor_static." |
| 311 | vendorSnapshotBinarySuffix = ".vendor_binary." |
| 312 | vendorSnapshotObjectSuffix = ".vendor_object." |
| 313 | ) |
| 314 | |
| 315 | const ( |
| 316 | recoverySnapshotHeaderSuffix = ".recovery_header." |
| 317 | recoverySnapshotSharedSuffix = ".recovery_shared." |
| 318 | recoverySnapshotStaticSuffix = ".recovery_static." |
| 319 | recoverySnapshotBinarySuffix = ".recovery_binary." |
| 320 | recoverySnapshotObjectSuffix = ".recovery_object." |
| 321 | ) |
| 322 | |
| 323 | var ( |
| 324 | vendorSnapshotsLock sync.Mutex |
| 325 | vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules") |
| 326 | vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs") |
| 327 | vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs") |
| 328 | vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs") |
| 329 | vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries") |
| 330 | vendorSnapshotObjectsKey = android.NewOnceKey("vendorSnapshotObjects") |
| 331 | ) |
| 332 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 333 | var ( |
| 334 | recoverySnapshotsLock sync.Mutex |
| 335 | recoverySuffixModulesKey = android.NewOnceKey("recoverySuffixModules") |
| 336 | recoverySnapshotHeaderLibsKey = android.NewOnceKey("recoverySnapshotHeaderLibs") |
| 337 | recoverySnapshotStaticLibsKey = android.NewOnceKey("recoverySnapshotStaticLibs") |
| 338 | recoverySnapshotSharedLibsKey = android.NewOnceKey("recoverySnapshotSharedLibs") |
| 339 | recoverySnapshotBinariesKey = android.NewOnceKey("recoverySnapshotBinaries") |
| 340 | recoverySnapshotObjectsKey = android.NewOnceKey("recoverySnapshotObjects") |
| 341 | ) |
| 342 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 343 | // vendorSuffixModules holds names of modules whose vendor variants should have the vendor suffix. |
| 344 | // This is determined by source modules, and then this will be used when exporting snapshot modules |
| 345 | // to Makefile. |
| 346 | // |
| 347 | // For example, if libbase has "vendor_available: true", the name of core variant will be "libbase" |
| 348 | // while the name of vendor variant will be "libbase.vendor". In such cases, the vendor snapshot of |
| 349 | // "libbase" should be exported with the name "libbase.vendor". |
| 350 | // |
| 351 | // Refer to VendorSnapshotSourceMutator and makeLibName which use this. |
| 352 | func vendorSuffixModules(config android.Config) map[string]bool { |
| 353 | return config.Once(vendorSuffixModulesKey, func() interface{} { |
| 354 | return make(map[string]bool) |
| 355 | }).(map[string]bool) |
| 356 | } |
| 357 | |
| 358 | // these are vendor snapshot maps holding names of vendor snapshot modules |
| 359 | func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap { |
| 360 | return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} { |
| 361 | return newSnapshotMap() |
| 362 | }).(*snapshotMap) |
| 363 | } |
| 364 | |
| 365 | func vendorSnapshotSharedLibs(config android.Config) *snapshotMap { |
| 366 | return config.Once(vendorSnapshotSharedLibsKey, func() interface{} { |
| 367 | return newSnapshotMap() |
| 368 | }).(*snapshotMap) |
| 369 | } |
| 370 | |
| 371 | func vendorSnapshotStaticLibs(config android.Config) *snapshotMap { |
| 372 | return config.Once(vendorSnapshotStaticLibsKey, func() interface{} { |
| 373 | return newSnapshotMap() |
| 374 | }).(*snapshotMap) |
| 375 | } |
| 376 | |
| 377 | func vendorSnapshotBinaries(config android.Config) *snapshotMap { |
| 378 | return config.Once(vendorSnapshotBinariesKey, func() interface{} { |
| 379 | return newSnapshotMap() |
| 380 | }).(*snapshotMap) |
| 381 | } |
| 382 | |
| 383 | func vendorSnapshotObjects(config android.Config) *snapshotMap { |
| 384 | return config.Once(vendorSnapshotObjectsKey, func() interface{} { |
| 385 | return newSnapshotMap() |
| 386 | }).(*snapshotMap) |
| 387 | } |
| 388 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 389 | func recoverySuffixModules(config android.Config) map[string]bool { |
| 390 | return config.Once(recoverySuffixModulesKey, func() interface{} { |
| 391 | return make(map[string]bool) |
| 392 | }).(map[string]bool) |
| 393 | } |
| 394 | |
| 395 | func recoverySnapshotHeaderLibs(config android.Config) *snapshotMap { |
| 396 | return config.Once(recoverySnapshotHeaderLibsKey, func() interface{} { |
| 397 | return newSnapshotMap() |
| 398 | }).(*snapshotMap) |
| 399 | } |
| 400 | |
| 401 | func recoverySnapshotSharedLibs(config android.Config) *snapshotMap { |
| 402 | return config.Once(recoverySnapshotSharedLibsKey, func() interface{} { |
| 403 | return newSnapshotMap() |
| 404 | }).(*snapshotMap) |
| 405 | } |
| 406 | |
| 407 | func recoverySnapshotStaticLibs(config android.Config) *snapshotMap { |
| 408 | return config.Once(recoverySnapshotStaticLibsKey, func() interface{} { |
| 409 | return newSnapshotMap() |
| 410 | }).(*snapshotMap) |
| 411 | } |
| 412 | |
| 413 | func recoverySnapshotBinaries(config android.Config) *snapshotMap { |
| 414 | return config.Once(recoverySnapshotBinariesKey, func() interface{} { |
| 415 | return newSnapshotMap() |
| 416 | }).(*snapshotMap) |
| 417 | } |
| 418 | |
| 419 | func recoverySnapshotObjects(config android.Config) *snapshotMap { |
| 420 | return config.Once(recoverySnapshotObjectsKey, func() interface{} { |
| 421 | return newSnapshotMap() |
| 422 | }).(*snapshotMap) |
| 423 | } |
| 424 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 425 | type baseSnapshotDecoratorProperties struct { |
| 426 | // snapshot version. |
| 427 | Version string |
| 428 | |
| 429 | // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64') |
| 430 | Target_arch string |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 431 | |
| 432 | // Suffix to be added to the module name, e.g., vendor_shared, |
| 433 | // recovery_shared, etc. |
| 434 | Module_suffix string |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // baseSnapshotDecorator provides common basic functions for all snapshot modules, such as snapshot |
| 438 | // version, snapshot arch, etc. It also adds a special suffix to Soong module name, so it doesn't |
| 439 | // collide with source modules. e.g. the following example module, |
| 440 | // |
| 441 | // vendor_snapshot_static { |
| 442 | // name: "libbase", |
| 443 | // arch: "arm64", |
| 444 | // version: 30, |
| 445 | // ... |
| 446 | // } |
| 447 | // |
| 448 | // will be seen as "libbase.vendor_static.30.arm64" by Soong. |
| 449 | type baseSnapshotDecorator struct { |
| 450 | baseProperties baseSnapshotDecoratorProperties |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | func (p *baseSnapshotDecorator) Name(name string) string { |
| 454 | return name + p.NameSuffix() |
| 455 | } |
| 456 | |
| 457 | func (p *baseSnapshotDecorator) NameSuffix() string { |
| 458 | versionSuffix := p.version() |
| 459 | if p.arch() != "" { |
| 460 | versionSuffix += "." + p.arch() |
| 461 | } |
| 462 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 463 | return p.baseProperties.Module_suffix + versionSuffix |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | func (p *baseSnapshotDecorator) version() string { |
| 467 | return p.baseProperties.Version |
| 468 | } |
| 469 | |
| 470 | func (p *baseSnapshotDecorator) arch() string { |
| 471 | return p.baseProperties.Target_arch |
| 472 | } |
| 473 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 474 | func (p *baseSnapshotDecorator) module_suffix() string { |
| 475 | return p.baseProperties.Module_suffix |
| 476 | } |
| 477 | |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 478 | func (p *baseSnapshotDecorator) isSnapshotPrebuilt() bool { |
| 479 | return true |
| 480 | } |
| 481 | |
| 482 | // Call this with a module suffix after creating a snapshot module, such as |
| 483 | // vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc. |
| 484 | func (p *baseSnapshotDecorator) init(m *Module, suffix string) { |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 485 | p.baseProperties.Module_suffix = suffix |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 486 | m.AddProperties(&p.baseProperties) |
| 487 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 488 | vendorSnapshotLoadHook(ctx, p) |
| 489 | }) |
| 490 | } |
| 491 | |
| 492 | // vendorSnapshotLoadHook disables snapshots if it's not BOARD_VNDK_VERSION. |
| 493 | // As vendor snapshot is only for vendor, such modules won't be used at all. |
| 494 | func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *baseSnapshotDecorator) { |
| 495 | if p.version() != ctx.DeviceConfig().VndkVersion() { |
| 496 | ctx.Module().Disable() |
| 497 | return |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // |
| 502 | // Module definitions for snapshots of libraries (shared, static, header). |
| 503 | // |
| 504 | // Modules (vendor|recovery)_snapshot_(shared|static|header) are defined here. Shared libraries and |
| 505 | // static libraries have their prebuilt library files (.so for shared, .a for static) as their src, |
| 506 | // which can be installed or linked against. Also they export flags needed when linked, such as |
| 507 | // include directories, c flags, sanitize dependency information, etc. |
| 508 | // |
| 509 | // These modules are auto-generated by development/vendor_snapshot/update.py. |
| 510 | type snapshotLibraryProperties struct { |
| 511 | // Prebuilt file for each arch. |
| 512 | Src *string `android:"arch_variant"` |
| 513 | |
| 514 | // list of directories that will be added to the include path (using -I). |
| 515 | Export_include_dirs []string `android:"arch_variant"` |
| 516 | |
| 517 | // list of directories that will be added to the system path (using -isystem). |
| 518 | Export_system_include_dirs []string `android:"arch_variant"` |
| 519 | |
| 520 | // list of flags that will be used for any module that links against this module. |
| 521 | Export_flags []string `android:"arch_variant"` |
| 522 | |
| 523 | // Whether this prebuilt needs to depend on sanitize ubsan runtime or not. |
| 524 | Sanitize_ubsan_dep *bool `android:"arch_variant"` |
| 525 | |
| 526 | // Whether this prebuilt needs to depend on sanitize minimal runtime or not. |
| 527 | Sanitize_minimal_dep *bool `android:"arch_variant"` |
| 528 | } |
| 529 | |
| 530 | type snapshotSanitizer interface { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 531 | isSanitizerEnabled(t SanitizerType) bool |
| 532 | setSanitizerVariation(t SanitizerType, enabled bool) |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | type snapshotLibraryDecorator struct { |
| 536 | baseSnapshotDecorator |
| 537 | *libraryDecorator |
| 538 | properties snapshotLibraryProperties |
| 539 | sanitizerProperties struct { |
| 540 | CfiEnabled bool `blueprint:"mutated"` |
| 541 | |
| 542 | // Library flags for cfi variant. |
| 543 | Cfi snapshotLibraryProperties `android:"arch_variant"` |
| 544 | } |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 545 | androidMkSuffix string |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 549 | p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix()) |
| 550 | return p.libraryDecorator.linkerFlags(ctx, flags) |
| 551 | } |
| 552 | |
| 553 | func (p *snapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool { |
| 554 | arches := config.Arches() |
| 555 | if len(arches) == 0 || arches[0].ArchType.String() != p.arch() { |
| 556 | return false |
| 557 | } |
| 558 | if !p.header() && p.properties.Src == nil { |
| 559 | return false |
| 560 | } |
| 561 | return true |
| 562 | } |
| 563 | |
| 564 | // cc modules' link functions are to link compiled objects into final binaries. |
| 565 | // As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are |
| 566 | // done by normal library decorator, e.g. exporting flags. |
| 567 | func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path { |
| 568 | m := ctx.Module().(*Module) |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 569 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 570 | if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] { |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 571 | p.androidMkSuffix = vendorSuffix |
| 572 | } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] { |
| 573 | p.androidMkSuffix = recoverySuffix |
| 574 | } |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 575 | |
| 576 | if p.header() { |
| 577 | return p.libraryDecorator.link(ctx, flags, deps, objs) |
| 578 | } |
| 579 | |
| 580 | if p.sanitizerProperties.CfiEnabled { |
| 581 | p.properties = p.sanitizerProperties.Cfi |
| 582 | } |
| 583 | |
| 584 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 585 | return nil |
| 586 | } |
| 587 | |
| 588 | p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...) |
| 589 | p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...) |
| 590 | p.libraryDecorator.reexportFlags(p.properties.Export_flags...) |
| 591 | |
| 592 | in := android.PathForModuleSrc(ctx, *p.properties.Src) |
| 593 | p.unstrippedOutputFile = in |
| 594 | |
| 595 | if p.shared() { |
| 596 | libName := in.Base() |
| 597 | builderFlags := flagsToBuilderFlags(flags) |
| 598 | |
| 599 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 600 | // depending on a table of contents file instead of the library itself. |
| 601 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 602 | p.tocFile = android.OptionalPathForPath(tocFile) |
| 603 | transformSharedObjectToToc(ctx, in, tocFile, builderFlags) |
| 604 | |
| 605 | ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ |
| 606 | SharedLibrary: in, |
| 607 | UnstrippedSharedLibrary: p.unstrippedOutputFile, |
| 608 | |
| 609 | TableOfContents: p.tocFile, |
| 610 | }) |
| 611 | } |
| 612 | |
| 613 | if p.static() { |
| 614 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build() |
| 615 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
| 616 | StaticLibrary: in, |
| 617 | |
| 618 | TransitiveStaticLibrariesForOrdering: depSet, |
| 619 | }) |
| 620 | } |
| 621 | |
| 622 | p.libraryDecorator.flagExporter.setProvider(ctx) |
| 623 | |
| 624 | return in |
| 625 | } |
| 626 | |
| 627 | func (p *snapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) { |
| 628 | if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) { |
| 629 | p.baseInstaller.install(ctx, file) |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | func (p *snapshotLibraryDecorator) nativeCoverage() bool { |
| 634 | return false |
| 635 | } |
| 636 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 637 | func (p *snapshotLibraryDecorator) isSanitizerEnabled(t SanitizerType) bool { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 638 | switch t { |
| 639 | case cfi: |
| 640 | return p.sanitizerProperties.Cfi.Src != nil |
| 641 | default: |
| 642 | return false |
| 643 | } |
| 644 | } |
| 645 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 646 | func (p *snapshotLibraryDecorator) setSanitizerVariation(t SanitizerType, enabled bool) { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 647 | if !enabled { |
| 648 | return |
| 649 | } |
| 650 | switch t { |
| 651 | case cfi: |
| 652 | p.sanitizerProperties.CfiEnabled = true |
| 653 | default: |
| 654 | return |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | func snapshotLibraryFactory(suffix string) (*Module, *snapshotLibraryDecorator) { |
| 659 | module, library := NewLibrary(android.DeviceSupported) |
| 660 | |
| 661 | module.stl = nil |
| 662 | module.sanitize = nil |
| 663 | library.disableStripping() |
| 664 | |
| 665 | prebuilt := &snapshotLibraryDecorator{ |
| 666 | libraryDecorator: library, |
| 667 | } |
| 668 | |
| 669 | prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 670 | prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 671 | |
| 672 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 673 | if prebuilt.baseLinker.Properties.System_shared_libs == nil { |
| 674 | prebuilt.baseLinker.Properties.System_shared_libs = []string{} |
| 675 | } |
| 676 | |
| 677 | module.compiler = nil |
| 678 | module.linker = prebuilt |
| 679 | module.installer = prebuilt |
| 680 | |
| 681 | prebuilt.init(module, suffix) |
| 682 | module.AddProperties( |
| 683 | &prebuilt.properties, |
| 684 | &prebuilt.sanitizerProperties, |
| 685 | ) |
| 686 | |
| 687 | return module, prebuilt |
| 688 | } |
| 689 | |
| 690 | // vendor_snapshot_shared is a special prebuilt shared library which is auto-generated by |
| 691 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_shared |
| 692 | // overrides the vendor variant of the cc shared library with the same name, if BOARD_VNDK_VERSION |
| 693 | // is set. |
| 694 | func VendorSnapshotSharedFactory() android.Module { |
| 695 | module, prebuilt := snapshotLibraryFactory(vendorSnapshotSharedSuffix) |
| 696 | prebuilt.libraryDecorator.BuildOnlyShared() |
| 697 | return module.Init() |
| 698 | } |
| 699 | |
| 700 | // recovery_snapshot_shared is a special prebuilt shared library which is auto-generated by |
| 701 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_shared |
| 702 | // overrides the recovery variant of the cc shared library with the same name, if BOARD_VNDK_VERSION |
| 703 | // is set. |
| 704 | func RecoverySnapshotSharedFactory() android.Module { |
| 705 | module, prebuilt := snapshotLibraryFactory(recoverySnapshotSharedSuffix) |
| 706 | prebuilt.libraryDecorator.BuildOnlyShared() |
| 707 | return module.Init() |
| 708 | } |
| 709 | |
| 710 | // vendor_snapshot_static is a special prebuilt static library which is auto-generated by |
| 711 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_static |
| 712 | // overrides the vendor variant of the cc static library with the same name, if BOARD_VNDK_VERSION |
| 713 | // is set. |
| 714 | func VendorSnapshotStaticFactory() android.Module { |
| 715 | module, prebuilt := snapshotLibraryFactory(vendorSnapshotStaticSuffix) |
| 716 | prebuilt.libraryDecorator.BuildOnlyStatic() |
| 717 | return module.Init() |
| 718 | } |
| 719 | |
| 720 | // recovery_snapshot_static is a special prebuilt static library which is auto-generated by |
| 721 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_static |
| 722 | // overrides the recovery variant of the cc static library with the same name, if BOARD_VNDK_VERSION |
| 723 | // is set. |
| 724 | func RecoverySnapshotStaticFactory() android.Module { |
| 725 | module, prebuilt := snapshotLibraryFactory(recoverySnapshotStaticSuffix) |
| 726 | prebuilt.libraryDecorator.BuildOnlyStatic() |
| 727 | return module.Init() |
| 728 | } |
| 729 | |
| 730 | // vendor_snapshot_header is a special header library which is auto-generated by |
| 731 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_header |
| 732 | // overrides the vendor variant of the cc header library with the same name, if BOARD_VNDK_VERSION |
| 733 | // is set. |
| 734 | func VendorSnapshotHeaderFactory() android.Module { |
| 735 | module, prebuilt := snapshotLibraryFactory(vendorSnapshotHeaderSuffix) |
| 736 | prebuilt.libraryDecorator.HeaderOnly() |
| 737 | return module.Init() |
| 738 | } |
| 739 | |
| 740 | // recovery_snapshot_header is a special header library which is auto-generated by |
| 741 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_header |
| 742 | // overrides the recovery variant of the cc header library with the same name, if BOARD_VNDK_VERSION |
| 743 | // is set. |
| 744 | func RecoverySnapshotHeaderFactory() android.Module { |
| 745 | module, prebuilt := snapshotLibraryFactory(recoverySnapshotHeaderSuffix) |
| 746 | prebuilt.libraryDecorator.HeaderOnly() |
| 747 | return module.Init() |
| 748 | } |
| 749 | |
| 750 | var _ snapshotSanitizer = (*snapshotLibraryDecorator)(nil) |
| 751 | |
| 752 | // |
| 753 | // Module definitions for snapshots of executable binaries. |
| 754 | // |
| 755 | // Modules (vendor|recovery)_snapshot_binary are defined here. They have their prebuilt executable |
| 756 | // binaries (e.g. toybox, sh) as their src, which can be installed. |
| 757 | // |
| 758 | // These modules are auto-generated by development/vendor_snapshot/update.py. |
| 759 | type snapshotBinaryProperties struct { |
| 760 | // Prebuilt file for each arch. |
| 761 | Src *string `android:"arch_variant"` |
| 762 | } |
| 763 | |
| 764 | type snapshotBinaryDecorator struct { |
| 765 | baseSnapshotDecorator |
| 766 | *binaryDecorator |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 767 | properties snapshotBinaryProperties |
| 768 | androidMkSuffix string |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool { |
| 772 | if config.DeviceArch() != p.arch() { |
| 773 | return false |
| 774 | } |
| 775 | if p.properties.Src == nil { |
| 776 | return false |
| 777 | } |
| 778 | return true |
| 779 | } |
| 780 | |
| 781 | // cc modules' link functions are to link compiled objects into final binaries. |
| 782 | // As snapshots are prebuilts, this just returns the prebuilt binary |
| 783 | func (p *snapshotBinaryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path { |
| 784 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 785 | return nil |
| 786 | } |
| 787 | |
| 788 | in := android.PathForModuleSrc(ctx, *p.properties.Src) |
| 789 | p.unstrippedOutputFile = in |
| 790 | binName := in.Base() |
| 791 | |
| 792 | m := ctx.Module().(*Module) |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 793 | if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] { |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 794 | p.androidMkSuffix = vendorSuffix |
| 795 | } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] { |
| 796 | p.androidMkSuffix = recoverySuffix |
| 797 | |
| 798 | } |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 799 | |
| 800 | // use cpExecutable to make it executable |
| 801 | outputFile := android.PathForModuleOut(ctx, binName) |
| 802 | ctx.Build(pctx, android.BuildParams{ |
| 803 | Rule: android.CpExecutable, |
| 804 | Description: "prebuilt", |
| 805 | Output: outputFile, |
| 806 | Input: in, |
| 807 | }) |
| 808 | |
| 809 | return outputFile |
| 810 | } |
| 811 | |
| 812 | func (p *snapshotBinaryDecorator) nativeCoverage() bool { |
| 813 | return false |
| 814 | } |
| 815 | |
| 816 | // vendor_snapshot_binary is a special prebuilt executable binary which is auto-generated by |
| 817 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_binary |
| 818 | // overrides the vendor variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set. |
| 819 | func VendorSnapshotBinaryFactory() android.Module { |
| 820 | return snapshotBinaryFactory(vendorSnapshotBinarySuffix) |
| 821 | } |
| 822 | |
| 823 | // recovery_snapshot_binary is a special prebuilt executable binary which is auto-generated by |
| 824 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_binary |
| 825 | // overrides the recovery variant of the cc binary with the same name, if BOARD_VNDK_VERSION is set. |
| 826 | func RecoverySnapshotBinaryFactory() android.Module { |
| 827 | return snapshotBinaryFactory(recoverySnapshotBinarySuffix) |
| 828 | } |
| 829 | |
| 830 | func snapshotBinaryFactory(suffix string) android.Module { |
| 831 | module, binary := NewBinary(android.DeviceSupported) |
| 832 | binary.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 833 | binary.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 834 | |
| 835 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 836 | if binary.baseLinker.Properties.System_shared_libs == nil { |
| 837 | binary.baseLinker.Properties.System_shared_libs = []string{} |
| 838 | } |
| 839 | |
| 840 | prebuilt := &snapshotBinaryDecorator{ |
| 841 | binaryDecorator: binary, |
| 842 | } |
| 843 | |
| 844 | module.compiler = nil |
| 845 | module.sanitize = nil |
| 846 | module.stl = nil |
| 847 | module.linker = prebuilt |
| 848 | |
| 849 | prebuilt.init(module, suffix) |
| 850 | module.AddProperties(&prebuilt.properties) |
| 851 | return module.Init() |
| 852 | } |
| 853 | |
| 854 | // |
| 855 | // Module definitions for snapshots of object files (*.o). |
| 856 | // |
| 857 | // Modules (vendor|recovery)_snapshot_object are defined here. They have their prebuilt object |
| 858 | // files (*.o) as their src. |
| 859 | // |
| 860 | // These modules are auto-generated by development/vendor_snapshot/update.py. |
| 861 | type vendorSnapshotObjectProperties struct { |
| 862 | // Prebuilt file for each arch. |
| 863 | Src *string `android:"arch_variant"` |
| 864 | } |
| 865 | |
| 866 | type snapshotObjectLinker struct { |
| 867 | baseSnapshotDecorator |
| 868 | objectLinker |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 869 | properties vendorSnapshotObjectProperties |
| 870 | androidMkSuffix string |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool { |
| 874 | if config.DeviceArch() != p.arch() { |
| 875 | return false |
| 876 | } |
| 877 | if p.properties.Src == nil { |
| 878 | return false |
| 879 | } |
| 880 | return true |
| 881 | } |
| 882 | |
| 883 | // cc modules' link functions are to link compiled objects into final binaries. |
| 884 | // As snapshots are prebuilts, this just returns the prebuilt binary |
| 885 | func (p *snapshotObjectLinker) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path { |
| 886 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 887 | return nil |
| 888 | } |
| 889 | |
| 890 | m := ctx.Module().(*Module) |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 891 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 892 | if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] { |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 893 | p.androidMkSuffix = vendorSuffix |
| 894 | } else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] { |
| 895 | p.androidMkSuffix = recoverySuffix |
| 896 | } |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 897 | |
| 898 | return android.PathForModuleSrc(ctx, *p.properties.Src) |
| 899 | } |
| 900 | |
| 901 | func (p *snapshotObjectLinker) nativeCoverage() bool { |
| 902 | return false |
| 903 | } |
| 904 | |
| 905 | // vendor_snapshot_object is a special prebuilt compiled object file which is auto-generated by |
| 906 | // development/vendor_snapshot/update.py. As a part of vendor snapshot, vendor_snapshot_object |
| 907 | // overrides the vendor variant of the cc object with the same name, if BOARD_VNDK_VERSION is set. |
| 908 | func VendorSnapshotObjectFactory() android.Module { |
| 909 | module := newObject() |
| 910 | |
| 911 | prebuilt := &snapshotObjectLinker{ |
| 912 | objectLinker: objectLinker{ |
| 913 | baseLinker: NewBaseLinker(nil), |
| 914 | }, |
| 915 | } |
| 916 | module.linker = prebuilt |
| 917 | |
| 918 | prebuilt.init(module, vendorSnapshotObjectSuffix) |
| 919 | module.AddProperties(&prebuilt.properties) |
| 920 | return module.Init() |
| 921 | } |
| 922 | |
| 923 | // recovery_snapshot_object is a special prebuilt compiled object file which is auto-generated by |
| 924 | // development/vendor_snapshot/update.py. As a part of recovery snapshot, recovery_snapshot_object |
| 925 | // overrides the recovery variant of the cc object with the same name, if BOARD_VNDK_VERSION is set. |
| 926 | func RecoverySnapshotObjectFactory() android.Module { |
| 927 | module := newObject() |
| 928 | |
| 929 | prebuilt := &snapshotObjectLinker{ |
| 930 | objectLinker: objectLinker{ |
| 931 | baseLinker: NewBaseLinker(nil), |
| 932 | }, |
| 933 | } |
| 934 | module.linker = prebuilt |
| 935 | |
| 936 | prebuilt.init(module, recoverySnapshotObjectSuffix) |
| 937 | module.AddProperties(&prebuilt.properties) |
| 938 | return module.Init() |
| 939 | } |
| 940 | |
| 941 | type snapshotInterface interface { |
| 942 | matchesWithDevice(config android.DeviceConfig) bool |
| 943 | } |
| 944 | |
| 945 | var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil) |
| 946 | var _ snapshotInterface = (*snapshotLibraryDecorator)(nil) |
| 947 | var _ snapshotInterface = (*snapshotBinaryDecorator)(nil) |
| 948 | var _ snapshotInterface = (*snapshotObjectLinker)(nil) |
| 949 | |
| 950 | // |
| 951 | // Mutators that helps vendor snapshot modules override source modules. |
| 952 | // |
| 953 | |
| 954 | // VendorSnapshotMutator gathers all snapshots for vendor, and disable all snapshots which don't |
| 955 | // match with device, e.g. |
| 956 | // - snapshot version is different with BOARD_VNDK_VERSION |
| 957 | // - snapshot arch is different with device's arch (e.g. arm vs x86) |
| 958 | // |
| 959 | // This also handles vndk_prebuilt_shared, except for they won't be disabled in any cases, given |
| 960 | // that any versions of VNDK might be packed into vndk APEX. |
| 961 | // |
| 962 | // TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules |
| 963 | func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) { |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 964 | snapshotMutator(ctx, vendorSnapshotImageSingleton) |
| 965 | } |
| 966 | |
| 967 | func RecoverySnapshotMutator(ctx android.BottomUpMutatorContext) { |
| 968 | snapshotMutator(ctx, recoverySnapshotImageSingleton) |
| 969 | } |
| 970 | |
| 971 | func snapshotMutator(ctx android.BottomUpMutatorContext, image snapshotImage) { |
| 972 | if !image.isUsingSnapshot(ctx.DeviceConfig()) { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 973 | return |
| 974 | } |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 975 | module, ok := ctx.Module().(*Module) |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 976 | if !ok || !module.Enabled() { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 977 | return |
| 978 | } |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 979 | if image.skipModuleMutator(ctx) { |
| 980 | return |
| 981 | } |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 982 | if !module.isSnapshotPrebuilt() { |
| 983 | return |
| 984 | } |
| 985 | |
| 986 | // isSnapshotPrebuilt ensures snapshotInterface |
| 987 | if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) { |
| 988 | // Disable unnecessary snapshot module, but do not disable |
| 989 | // vndk_prebuilt_shared because they might be packed into vndk APEX |
| 990 | if !module.IsVndk() { |
| 991 | module.Disable() |
| 992 | } |
| 993 | return |
| 994 | } |
| 995 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 996 | var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config()) |
| 997 | if snapshotMap == nil { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 998 | return |
| 999 | } |
| 1000 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 1001 | mutex := image.getMutex() |
| 1002 | mutex.Lock() |
| 1003 | defer mutex.Unlock() |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1004 | snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName()) |
| 1005 | } |
| 1006 | |
| 1007 | // VendorSnapshotSourceMutator disables source modules which have corresponding snapshots. |
| 1008 | func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) { |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 1009 | snapshotSourceMutator(ctx, vendorSnapshotImageSingleton) |
| 1010 | } |
| 1011 | |
| 1012 | func RecoverySnapshotSourceMutator(ctx android.BottomUpMutatorContext) { |
| 1013 | snapshotSourceMutator(ctx, recoverySnapshotImageSingleton) |
| 1014 | } |
| 1015 | |
| 1016 | func snapshotSourceMutator(ctx android.BottomUpMutatorContext, image snapshotImage) { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1017 | if !ctx.Device() { |
| 1018 | return |
| 1019 | } |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 1020 | if !image.isUsingSnapshot(ctx.DeviceConfig()) { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1021 | return |
| 1022 | } |
| 1023 | |
| 1024 | module, ok := ctx.Module().(*Module) |
| 1025 | if !ok { |
| 1026 | return |
| 1027 | } |
| 1028 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 1029 | if image.shouldBeAddedToSuffixModules(module) { |
| 1030 | mutex := image.getMutex() |
| 1031 | mutex.Lock() |
| 1032 | defer mutex.Unlock() |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1033 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 1034 | image.suffixModules(ctx.Config())[ctx.ModuleName()] = true |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1035 | } |
| 1036 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 1037 | if module.isSnapshotPrebuilt() { |
| 1038 | return |
| 1039 | } |
| 1040 | if image.skipSourceMutator(ctx) { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1041 | return |
| 1042 | } |
| 1043 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 1044 | var snapshotMap *snapshotMap = image.getSnapshotMap(module, ctx.Config()) |
| 1045 | if snapshotMap == nil { |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1046 | return |
| 1047 | } |
| 1048 | |
| 1049 | if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok { |
| 1050 | // Corresponding snapshot doesn't exist |
| 1051 | return |
| 1052 | } |
| 1053 | |
| 1054 | // Disables source modules if corresponding snapshot exists. |
| 1055 | if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() { |
| 1056 | // But do not disable because the shared variant depends on the static variant. |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 1057 | module.HideFromMake() |
Inseob Kim | de5744a | 2020-12-02 13:14:28 +0900 | [diff] [blame] | 1058 | module.Properties.HideFromMake = true |
| 1059 | } else { |
| 1060 | module.Disable() |
| 1061 | } |
| 1062 | } |