Inseob Kim | 8471cda | 2019-11-15 09:59:12 +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 | import ( |
| 17 | "encoding/json" |
| 18 | "path/filepath" |
| 19 | "sort" |
| 20 | "strings" |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 21 | "sync" |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | |
| 25 | "android/soong/android" |
| 26 | ) |
| 27 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 28 | const ( |
| 29 | vendorSnapshotHeaderSuffix = ".vendor_header." |
| 30 | vendorSnapshotSharedSuffix = ".vendor_shared." |
| 31 | vendorSnapshotStaticSuffix = ".vendor_static." |
| 32 | vendorSnapshotBinarySuffix = ".vendor_binary." |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 33 | vendorSnapshotObjectSuffix = ".vendor_object." |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | var ( |
| 37 | vendorSnapshotsLock sync.Mutex |
| 38 | vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules") |
| 39 | vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs") |
| 40 | vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs") |
| 41 | vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs") |
| 42 | vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries") |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 43 | vendorSnapshotObjectsKey = android.NewOnceKey("vendorSnapshotObjects") |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 44 | ) |
| 45 | |
Inseob Kim | 5f64aec | 2020-02-18 17:27:19 +0900 | [diff] [blame] | 46 | // vendor snapshot maps hold names of vendor snapshot modules per arch |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 47 | func vendorSuffixModules(config android.Config) map[string]bool { |
| 48 | return config.Once(vendorSuffixModulesKey, func() interface{} { |
| 49 | return make(map[string]bool) |
| 50 | }).(map[string]bool) |
| 51 | } |
| 52 | |
| 53 | func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap { |
| 54 | return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} { |
| 55 | return newSnapshotMap() |
| 56 | }).(*snapshotMap) |
| 57 | } |
| 58 | |
| 59 | func vendorSnapshotSharedLibs(config android.Config) *snapshotMap { |
| 60 | return config.Once(vendorSnapshotSharedLibsKey, func() interface{} { |
| 61 | return newSnapshotMap() |
| 62 | }).(*snapshotMap) |
| 63 | } |
| 64 | |
| 65 | func vendorSnapshotStaticLibs(config android.Config) *snapshotMap { |
| 66 | return config.Once(vendorSnapshotStaticLibsKey, func() interface{} { |
| 67 | return newSnapshotMap() |
| 68 | }).(*snapshotMap) |
| 69 | } |
| 70 | |
| 71 | func vendorSnapshotBinaries(config android.Config) *snapshotMap { |
| 72 | return config.Once(vendorSnapshotBinariesKey, func() interface{} { |
| 73 | return newSnapshotMap() |
| 74 | }).(*snapshotMap) |
| 75 | } |
| 76 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 77 | func vendorSnapshotObjects(config android.Config) *snapshotMap { |
| 78 | return config.Once(vendorSnapshotObjectsKey, func() interface{} { |
| 79 | return newSnapshotMap() |
| 80 | }).(*snapshotMap) |
| 81 | } |
| 82 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 83 | type vendorSnapshotBaseProperties struct { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 84 | // snapshot version. |
| 85 | Version string |
| 86 | |
| 87 | // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64') |
| 88 | Target_arch string |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 89 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 90 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 91 | // vendorSnapshotModuleBase provides common basic functions for all snapshot modules. |
| 92 | type vendorSnapshotModuleBase struct { |
| 93 | baseProperties vendorSnapshotBaseProperties |
| 94 | moduleSuffix string |
| 95 | } |
| 96 | |
| 97 | func (p *vendorSnapshotModuleBase) Name(name string) string { |
| 98 | return name + p.NameSuffix() |
| 99 | } |
| 100 | |
| 101 | func (p *vendorSnapshotModuleBase) NameSuffix() string { |
| 102 | versionSuffix := p.version() |
| 103 | if p.arch() != "" { |
| 104 | versionSuffix += "." + p.arch() |
| 105 | } |
| 106 | |
| 107 | return p.moduleSuffix + versionSuffix |
| 108 | } |
| 109 | |
| 110 | func (p *vendorSnapshotModuleBase) version() string { |
| 111 | return p.baseProperties.Version |
| 112 | } |
| 113 | |
| 114 | func (p *vendorSnapshotModuleBase) arch() string { |
| 115 | return p.baseProperties.Target_arch |
| 116 | } |
| 117 | |
| 118 | func (p *vendorSnapshotModuleBase) isSnapshotPrebuilt() bool { |
| 119 | return true |
| 120 | } |
| 121 | |
| 122 | // Call this after creating a snapshot module with module suffix |
| 123 | // such as vendorSnapshotSharedSuffix |
| 124 | func (p *vendorSnapshotModuleBase) init(m *Module, suffix string) { |
| 125 | p.moduleSuffix = suffix |
| 126 | m.AddProperties(&p.baseProperties) |
| 127 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 128 | vendorSnapshotLoadHook(ctx, p) |
| 129 | }) |
| 130 | } |
| 131 | |
| 132 | func vendorSnapshotLoadHook(ctx android.LoadHookContext, p *vendorSnapshotModuleBase) { |
| 133 | if p.version() != ctx.DeviceConfig().VndkVersion() { |
| 134 | ctx.Module().Disable() |
| 135 | return |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | type vendorSnapshotLibraryProperties struct { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 140 | // Prebuilt file for each arch. |
| 141 | Src *string `android:"arch_variant"` |
| 142 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 143 | // list of directories that will be added to the include path (using -I). |
| 144 | Export_include_dirs []string `android:"arch_variant"` |
| 145 | |
| 146 | // list of directories that will be added to the system path (using -isystem). |
| 147 | Export_system_include_dirs []string `android:"arch_variant"` |
| 148 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 149 | // list of flags that will be used for any module that links against this module. |
| 150 | Export_flags []string `android:"arch_variant"` |
| 151 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 152 | // Whether this prebuilt needs to depend on sanitize ubsan runtime or not. |
| 153 | Sanitize_ubsan_dep *bool `android:"arch_variant"` |
| 154 | |
| 155 | // Whether this prebuilt needs to depend on sanitize minimal runtime or not. |
| 156 | Sanitize_minimal_dep *bool `android:"arch_variant"` |
| 157 | } |
| 158 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 159 | type snapshotSanitizer interface { |
| 160 | isSanitizerEnabled(t sanitizerType) bool |
| 161 | setSanitizerVariation(t sanitizerType, enabled bool) |
| 162 | } |
| 163 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 164 | type vendorSnapshotLibraryDecorator struct { |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 165 | vendorSnapshotModuleBase |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 166 | *libraryDecorator |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 167 | properties vendorSnapshotLibraryProperties |
| 168 | sanitizerProperties struct { |
| 169 | CfiEnabled bool `blueprint:"mutated"` |
| 170 | |
| 171 | // Library flags for cfi variant. |
| 172 | Cfi vendorSnapshotLibraryProperties `android:"arch_variant"` |
| 173 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 174 | androidMkVendorSuffix bool |
| 175 | } |
| 176 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 177 | func (p *vendorSnapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 178 | p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix()) |
| 179 | return p.libraryDecorator.linkerFlags(ctx, flags) |
| 180 | } |
| 181 | |
| 182 | func (p *vendorSnapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool { |
| 183 | arches := config.Arches() |
| 184 | if len(arches) == 0 || arches[0].ArchType.String() != p.arch() { |
| 185 | return false |
| 186 | } |
| 187 | if !p.header() && p.properties.Src == nil { |
| 188 | return false |
| 189 | } |
| 190 | return true |
| 191 | } |
| 192 | |
| 193 | func (p *vendorSnapshotLibraryDecorator) link(ctx ModuleContext, |
| 194 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 195 | m := ctx.Module().(*Module) |
| 196 | p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()] |
| 197 | |
| 198 | if p.header() { |
| 199 | return p.libraryDecorator.link(ctx, flags, deps, objs) |
| 200 | } |
| 201 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 202 | if p.sanitizerProperties.CfiEnabled { |
| 203 | p.properties = p.sanitizerProperties.Cfi |
| 204 | } |
| 205 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 206 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 207 | return nil |
| 208 | } |
| 209 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 210 | p.libraryDecorator.reexportDirs(android.PathsForModuleSrc(ctx, p.properties.Export_include_dirs)...) |
| 211 | p.libraryDecorator.reexportSystemDirs(android.PathsForModuleSrc(ctx, p.properties.Export_system_include_dirs)...) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 212 | p.libraryDecorator.reexportFlags(p.properties.Export_flags...) |
| 213 | |
| 214 | in := android.PathForModuleSrc(ctx, *p.properties.Src) |
| 215 | p.unstrippedOutputFile = in |
| 216 | |
| 217 | if p.shared() { |
| 218 | libName := in.Base() |
| 219 | builderFlags := flagsToBuilderFlags(flags) |
| 220 | |
| 221 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 222 | // depending on a table of contents file instead of the library itself. |
| 223 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 224 | p.tocFile = android.OptionalPathForPath(tocFile) |
| 225 | TransformSharedObjectToToc(ctx, in, tocFile, builderFlags) |
| 226 | } |
| 227 | |
| 228 | return in |
| 229 | } |
| 230 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 231 | func (p *vendorSnapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) { |
| 232 | if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) { |
| 233 | p.baseInstaller.install(ctx, file) |
| 234 | } |
| 235 | } |
| 236 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 237 | func (p *vendorSnapshotLibraryDecorator) nativeCoverage() bool { |
| 238 | return false |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 241 | func (p *vendorSnapshotLibraryDecorator) isSanitizerEnabled(t sanitizerType) bool { |
| 242 | switch t { |
| 243 | case cfi: |
| 244 | return p.sanitizerProperties.Cfi.Src != nil |
| 245 | default: |
| 246 | return false |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | func (p *vendorSnapshotLibraryDecorator) setSanitizerVariation(t sanitizerType, enabled bool) { |
| 251 | if !enabled { |
| 252 | return |
| 253 | } |
| 254 | switch t { |
| 255 | case cfi: |
| 256 | p.sanitizerProperties.CfiEnabled = true |
| 257 | default: |
| 258 | return |
| 259 | } |
| 260 | } |
| 261 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 262 | func vendorSnapshotLibrary(suffix string) (*Module, *vendorSnapshotLibraryDecorator) { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 263 | module, library := NewLibrary(android.DeviceSupported) |
| 264 | |
| 265 | module.stl = nil |
| 266 | module.sanitize = nil |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame^] | 267 | library.disableStripping() |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 268 | |
| 269 | prebuilt := &vendorSnapshotLibraryDecorator{ |
| 270 | libraryDecorator: library, |
| 271 | } |
| 272 | |
| 273 | prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 274 | prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 275 | |
| 276 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 277 | if prebuilt.baseLinker.Properties.System_shared_libs == nil { |
| 278 | prebuilt.baseLinker.Properties.System_shared_libs = []string{} |
| 279 | } |
| 280 | |
| 281 | module.compiler = nil |
| 282 | module.linker = prebuilt |
| 283 | module.installer = prebuilt |
| 284 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 285 | prebuilt.init(module, suffix) |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 286 | module.AddProperties( |
| 287 | &prebuilt.properties, |
| 288 | &prebuilt.sanitizerProperties, |
| 289 | ) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 290 | |
| 291 | return module, prebuilt |
| 292 | } |
| 293 | |
| 294 | func VendorSnapshotSharedFactory() android.Module { |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 295 | module, prebuilt := vendorSnapshotLibrary(vendorSnapshotSharedSuffix) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 296 | prebuilt.libraryDecorator.BuildOnlyShared() |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 297 | return module.Init() |
| 298 | } |
| 299 | |
| 300 | func VendorSnapshotStaticFactory() android.Module { |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 301 | module, prebuilt := vendorSnapshotLibrary(vendorSnapshotStaticSuffix) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 302 | prebuilt.libraryDecorator.BuildOnlyStatic() |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 303 | return module.Init() |
| 304 | } |
| 305 | |
| 306 | func VendorSnapshotHeaderFactory() android.Module { |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 307 | module, prebuilt := vendorSnapshotLibrary(vendorSnapshotHeaderSuffix) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 308 | prebuilt.libraryDecorator.HeaderOnly() |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 309 | return module.Init() |
| 310 | } |
| 311 | |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 312 | var _ snapshotSanitizer = (*vendorSnapshotLibraryDecorator)(nil) |
| 313 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 314 | type vendorSnapshotBinaryProperties struct { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 315 | // Prebuilt file for each arch. |
| 316 | Src *string `android:"arch_variant"` |
| 317 | } |
| 318 | |
| 319 | type vendorSnapshotBinaryDecorator struct { |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 320 | vendorSnapshotModuleBase |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 321 | *binaryDecorator |
| 322 | properties vendorSnapshotBinaryProperties |
| 323 | androidMkVendorSuffix bool |
| 324 | } |
| 325 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 326 | func (p *vendorSnapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool { |
| 327 | if config.DeviceArch() != p.arch() { |
| 328 | return false |
| 329 | } |
| 330 | if p.properties.Src == nil { |
| 331 | return false |
| 332 | } |
| 333 | return true |
| 334 | } |
| 335 | |
| 336 | func (p *vendorSnapshotBinaryDecorator) link(ctx ModuleContext, |
| 337 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 338 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 339 | return nil |
| 340 | } |
| 341 | |
| 342 | in := android.PathForModuleSrc(ctx, *p.properties.Src) |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame^] | 343 | stripFlags := flagsToStripFlags(flags) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 344 | p.unstrippedOutputFile = in |
| 345 | binName := in.Base() |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame^] | 346 | if p.stripper.NeedsStrip(ctx) { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 347 | stripped := android.PathForModuleOut(ctx, "stripped", binName) |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame^] | 348 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 349 | in = stripped |
| 350 | } |
| 351 | |
| 352 | m := ctx.Module().(*Module) |
| 353 | p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()] |
| 354 | |
| 355 | // use cpExecutable to make it executable |
| 356 | outputFile := android.PathForModuleOut(ctx, binName) |
| 357 | ctx.Build(pctx, android.BuildParams{ |
| 358 | Rule: android.CpExecutable, |
| 359 | Description: "prebuilt", |
| 360 | Output: outputFile, |
| 361 | Input: in, |
| 362 | }) |
| 363 | |
| 364 | return outputFile |
| 365 | } |
| 366 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 367 | func (p *vendorSnapshotBinaryDecorator) nativeCoverage() bool { |
| 368 | return false |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 369 | } |
| 370 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 371 | func VendorSnapshotBinaryFactory() android.Module { |
| 372 | module, binary := NewBinary(android.DeviceSupported) |
| 373 | binary.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 374 | binary.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 375 | |
| 376 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 377 | if binary.baseLinker.Properties.System_shared_libs == nil { |
| 378 | binary.baseLinker.Properties.System_shared_libs = []string{} |
| 379 | } |
| 380 | |
| 381 | prebuilt := &vendorSnapshotBinaryDecorator{ |
| 382 | binaryDecorator: binary, |
| 383 | } |
| 384 | |
| 385 | module.compiler = nil |
| 386 | module.sanitize = nil |
| 387 | module.stl = nil |
| 388 | module.linker = prebuilt |
| 389 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 390 | prebuilt.init(module, vendorSnapshotBinarySuffix) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 391 | module.AddProperties(&prebuilt.properties) |
| 392 | return module.Init() |
| 393 | } |
| 394 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 395 | type vendorSnapshotObjectProperties struct { |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 396 | // Prebuilt file for each arch. |
| 397 | Src *string `android:"arch_variant"` |
| 398 | } |
| 399 | |
| 400 | type vendorSnapshotObjectLinker struct { |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 401 | vendorSnapshotModuleBase |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 402 | objectLinker |
| 403 | properties vendorSnapshotObjectProperties |
| 404 | androidMkVendorSuffix bool |
| 405 | } |
| 406 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 407 | func (p *vendorSnapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool { |
| 408 | if config.DeviceArch() != p.arch() { |
| 409 | return false |
| 410 | } |
| 411 | if p.properties.Src == nil { |
| 412 | return false |
| 413 | } |
| 414 | return true |
| 415 | } |
| 416 | |
| 417 | func (p *vendorSnapshotObjectLinker) link(ctx ModuleContext, |
| 418 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 419 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
| 420 | return nil |
| 421 | } |
| 422 | |
| 423 | m := ctx.Module().(*Module) |
| 424 | p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()] |
| 425 | |
| 426 | return android.PathForModuleSrc(ctx, *p.properties.Src) |
| 427 | } |
| 428 | |
| 429 | func (p *vendorSnapshotObjectLinker) nativeCoverage() bool { |
| 430 | return false |
| 431 | } |
| 432 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 433 | func VendorSnapshotObjectFactory() android.Module { |
| 434 | module := newObject() |
| 435 | |
| 436 | prebuilt := &vendorSnapshotObjectLinker{ |
| 437 | objectLinker: objectLinker{ |
| 438 | baseLinker: NewBaseLinker(nil), |
| 439 | }, |
| 440 | } |
| 441 | module.linker = prebuilt |
| 442 | |
Inseob Kim | 2d34ad9 | 2020-07-30 21:04:09 +0900 | [diff] [blame] | 443 | prebuilt.init(module, vendorSnapshotObjectSuffix) |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 444 | module.AddProperties(&prebuilt.properties) |
| 445 | return module.Init() |
| 446 | } |
| 447 | |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 448 | func init() { |
| 449 | android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 450 | android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory) |
| 451 | android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory) |
| 452 | android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory) |
| 453 | android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory) |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 454 | android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory) |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | func VendorSnapshotSingleton() android.Singleton { |
| 458 | return &vendorSnapshotSingleton{} |
| 459 | } |
| 460 | |
| 461 | type vendorSnapshotSingleton struct { |
| 462 | vendorSnapshotZipFile android.OptionalPath |
| 463 | } |
| 464 | |
| 465 | var ( |
| 466 | // Modules under following directories are ignored. They are OEM's and vendor's |
Daniel Norman | 713387d | 2020-07-28 16:04:38 -0700 | [diff] [blame] | 467 | // proprietary modules(device/, kernel/, vendor/, and hardware/). |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 468 | // TODO(b/65377115): Clean up these with more maintainable way |
| 469 | vendorProprietaryDirs = []string{ |
| 470 | "device", |
Daniel Norman | 713387d | 2020-07-28 16:04:38 -0700 | [diff] [blame] | 471 | "kernel", |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 472 | "vendor", |
| 473 | "hardware", |
| 474 | } |
| 475 | |
| 476 | // Modules under following directories are included as they are in AOSP, |
Daniel Norman | 713387d | 2020-07-28 16:04:38 -0700 | [diff] [blame] | 477 | // although hardware/ and kernel/ are normally for vendor's own. |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 478 | // TODO(b/65377115): Clean up these with more maintainable way |
| 479 | aospDirsUnderProprietary = []string{ |
Daniel Norman | 713387d | 2020-07-28 16:04:38 -0700 | [diff] [blame] | 480 | "kernel/configs", |
| 481 | "kernel/prebuilts", |
| 482 | "kernel/tests", |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 483 | "hardware/interfaces", |
| 484 | "hardware/libhardware", |
| 485 | "hardware/libhardware_legacy", |
| 486 | "hardware/ril", |
| 487 | } |
| 488 | ) |
| 489 | |
| 490 | // Determine if a dir under source tree is an SoC-owned proprietary directory, such as |
| 491 | // device/, vendor/, etc. |
| 492 | func isVendorProprietaryPath(dir string) bool { |
| 493 | for _, p := range vendorProprietaryDirs { |
| 494 | if strings.HasPrefix(dir, p) { |
| 495 | // filter out AOSP defined directories, e.g. hardware/interfaces/ |
| 496 | aosp := false |
| 497 | for _, p := range aospDirsUnderProprietary { |
| 498 | if strings.HasPrefix(dir, p) { |
| 499 | aosp = true |
| 500 | break |
| 501 | } |
| 502 | } |
| 503 | if !aosp { |
| 504 | return true |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | return false |
| 509 | } |
| 510 | |
| 511 | // Determine if a module is going to be included in vendor snapshot or not. |
| 512 | // |
| 513 | // Targets of vendor snapshot are "vendor: true" or "vendor_available: true" modules in |
| 514 | // AOSP. They are not guaranteed to be compatible with older vendor images. (e.g. might |
| 515 | // depend on newer VNDK) So they are captured as vendor snapshot To build older vendor |
| 516 | // image and newer system image altogether. |
Inseob Kim | eda2e9c | 2020-03-03 22:06:32 +0900 | [diff] [blame] | 517 | func isVendorSnapshotModule(m *Module, moduleDir string) bool { |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 518 | if !m.Enabled() || m.Properties.HideFromMake { |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 519 | return false |
| 520 | } |
| 521 | // skip proprietary modules, but include all VNDK (static) |
Inseob Kim | eda2e9c | 2020-03-03 22:06:32 +0900 | [diff] [blame] | 522 | if isVendorProprietaryPath(moduleDir) && !m.IsVndk() { |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 523 | return false |
| 524 | } |
| 525 | if m.Target().Os.Class != android.Device { |
| 526 | return false |
| 527 | } |
| 528 | if m.Target().NativeBridge == android.NativeBridgeEnabled { |
| 529 | return false |
| 530 | } |
| 531 | // the module must be installed in /vendor |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 532 | if !m.IsForPlatform() || m.isSnapshotPrebuilt() || !m.inVendor() { |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 533 | return false |
| 534 | } |
Inseob Kim | 65ca36a | 2020-06-11 13:55:45 +0900 | [diff] [blame] | 535 | // skip kernel_headers which always depend on vendor |
| 536 | if _, ok := m.linker.(*kernelHeadersDecorator); ok { |
| 537 | return false |
| 538 | } |
Justin Yun | f2664c6 | 2020-07-30 18:57:54 +0900 | [diff] [blame] | 539 | // skip llndk_library and llndk_headers which are backward compatible |
| 540 | if _, ok := m.linker.(*llndkStubDecorator); ok { |
| 541 | return false |
| 542 | } |
| 543 | if _, ok := m.linker.(*llndkHeadersDecorator); ok { |
| 544 | return false |
| 545 | } |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 546 | |
| 547 | // Libraries |
| 548 | if l, ok := m.linker.(snapshotLibraryInterface); ok { |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 549 | // TODO(b/65377115): add full support for sanitizer |
| 550 | if m.sanitize != nil { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 551 | // scs and hwasan export both sanitized and unsanitized variants for static and header |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 552 | // Always use unsanitized variants of them. |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 553 | for _, t := range []sanitizerType{scs, hwasan} { |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 554 | if !l.shared() && m.sanitize.isSanitizerEnabled(t) { |
| 555 | return false |
| 556 | } |
| 557 | } |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 558 | // cfi also exports both variants. But for static, we capture both. |
| 559 | if !l.static() && !l.shared() && m.sanitize.isSanitizerEnabled(cfi) { |
| 560 | return false |
| 561 | } |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 562 | } |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 563 | if l.static() { |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 564 | return m.outputFile.Valid() && proptools.BoolDefault(m.VendorProperties.Vendor_available, true) |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 565 | } |
| 566 | if l.shared() { |
Bill Peckham | 7d3f096 | 2020-06-29 16:49:15 -0700 | [diff] [blame] | 567 | if !m.outputFile.Valid() { |
| 568 | return false |
| 569 | } |
| 570 | if !m.IsVndk() { |
| 571 | return true |
| 572 | } |
| 573 | return m.isVndkExt() |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 574 | } |
| 575 | return true |
| 576 | } |
| 577 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 578 | // Binaries and Objects |
| 579 | if m.binary() || m.object() { |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 580 | return m.outputFile.Valid() && proptools.BoolDefault(m.VendorProperties.Vendor_available, true) |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 581 | } |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 582 | |
| 583 | return false |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | func (c *vendorSnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 587 | // BOARD_VNDK_VERSION must be set to 'current' in order to generate a vendor snapshot. |
| 588 | if ctx.DeviceConfig().VndkVersion() != "current" { |
| 589 | return |
| 590 | } |
| 591 | |
| 592 | var snapshotOutputs android.Paths |
| 593 | |
| 594 | /* |
| 595 | Vendor snapshot zipped artifacts directory structure: |
| 596 | {SNAPSHOT_ARCH}/ |
| 597 | arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/ |
| 598 | shared/ |
| 599 | (.so shared libraries) |
| 600 | static/ |
| 601 | (.a static libraries) |
| 602 | header/ |
| 603 | (header only libraries) |
| 604 | binary/ |
| 605 | (executable binaries) |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 606 | object/ |
| 607 | (.o object files) |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 608 | arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/ |
| 609 | shared/ |
| 610 | (.so shared libraries) |
| 611 | static/ |
| 612 | (.a static libraries) |
| 613 | header/ |
| 614 | (header only libraries) |
| 615 | binary/ |
| 616 | (executable binaries) |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 617 | object/ |
| 618 | (.o object files) |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 619 | NOTICE_FILES/ |
| 620 | (notice files, e.g. libbase.txt) |
| 621 | configs/ |
| 622 | (config files, e.g. init.rc files, vintf_fragments.xml files, etc.) |
| 623 | include/ |
| 624 | (header files of same directory structure with source tree) |
| 625 | */ |
| 626 | |
| 627 | snapshotDir := "vendor-snapshot" |
| 628 | snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch()) |
| 629 | |
| 630 | includeDir := filepath.Join(snapshotArchDir, "include") |
| 631 | configsDir := filepath.Join(snapshotArchDir, "configs") |
| 632 | noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES") |
| 633 | |
| 634 | installedNotices := make(map[string]bool) |
| 635 | installedConfigs := make(map[string]bool) |
| 636 | |
| 637 | var headers android.Paths |
| 638 | |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 639 | installSnapshot := func(m *Module) android.Paths { |
| 640 | targetArch := "arch-" + m.Target().Arch.ArchType.String() |
| 641 | if m.Target().Arch.ArchVariant != "" { |
| 642 | targetArch += "-" + m.Target().Arch.ArchVariant |
| 643 | } |
| 644 | |
| 645 | var ret android.Paths |
| 646 | |
| 647 | prop := struct { |
| 648 | ModuleName string `json:",omitempty"` |
| 649 | RelativeInstallPath string `json:",omitempty"` |
| 650 | |
| 651 | // library flags |
| 652 | ExportedDirs []string `json:",omitempty"` |
| 653 | ExportedSystemDirs []string `json:",omitempty"` |
| 654 | ExportedFlags []string `json:",omitempty"` |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 655 | Sanitize string `json:",omitempty"` |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 656 | SanitizeMinimalDep bool `json:",omitempty"` |
| 657 | SanitizeUbsanDep bool `json:",omitempty"` |
| 658 | |
| 659 | // binary flags |
| 660 | Symlinks []string `json:",omitempty"` |
| 661 | |
| 662 | // dependencies |
| 663 | SharedLibs []string `json:",omitempty"` |
| 664 | RuntimeLibs []string `json:",omitempty"` |
| 665 | Required []string `json:",omitempty"` |
| 666 | |
| 667 | // extra config files |
| 668 | InitRc []string `json:",omitempty"` |
| 669 | VintfFragments []string `json:",omitempty"` |
| 670 | }{} |
| 671 | |
| 672 | // Common properties among snapshots. |
| 673 | prop.ModuleName = ctx.ModuleName(m) |
Bill Peckham | 7d3f096 | 2020-06-29 16:49:15 -0700 | [diff] [blame] | 674 | if m.isVndkExt() { |
| 675 | // vndk exts are installed to /vendor/lib(64)?/vndk(-sp)? |
| 676 | if m.isVndkSp() { |
| 677 | prop.RelativeInstallPath = "vndk-sp" |
| 678 | } else { |
| 679 | prop.RelativeInstallPath = "vndk" |
| 680 | } |
| 681 | } else { |
| 682 | prop.RelativeInstallPath = m.RelativeInstallPath() |
| 683 | } |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 684 | prop.RuntimeLibs = m.Properties.SnapshotRuntimeLibs |
| 685 | prop.Required = m.RequiredModuleNames() |
| 686 | for _, path := range m.InitRc() { |
| 687 | prop.InitRc = append(prop.InitRc, filepath.Join("configs", path.Base())) |
| 688 | } |
| 689 | for _, path := range m.VintfFragments() { |
| 690 | prop.VintfFragments = append(prop.VintfFragments, filepath.Join("configs", path.Base())) |
| 691 | } |
| 692 | |
| 693 | // install config files. ignores any duplicates. |
| 694 | for _, path := range append(m.InitRc(), m.VintfFragments()...) { |
| 695 | out := filepath.Join(configsDir, path.Base()) |
| 696 | if !installedConfigs[out] { |
| 697 | installedConfigs[out] = true |
| 698 | ret = append(ret, copyFile(ctx, path, out)) |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | var propOut string |
| 703 | |
Inseob Kim | eda2e9c | 2020-03-03 22:06:32 +0900 | [diff] [blame] | 704 | if l, ok := m.linker.(snapshotLibraryInterface); ok { |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 705 | |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 706 | // library flags |
| 707 | prop.ExportedFlags = l.exportedFlags() |
| 708 | for _, dir := range l.exportedDirs() { |
| 709 | prop.ExportedDirs = append(prop.ExportedDirs, filepath.Join("include", dir.String())) |
| 710 | } |
| 711 | for _, dir := range l.exportedSystemDirs() { |
| 712 | prop.ExportedSystemDirs = append(prop.ExportedSystemDirs, filepath.Join("include", dir.String())) |
| 713 | } |
| 714 | // shared libs dependencies aren't meaningful on static or header libs |
| 715 | if l.shared() { |
| 716 | prop.SharedLibs = m.Properties.SnapshotSharedLibs |
| 717 | } |
| 718 | if l.static() && m.sanitize != nil { |
| 719 | prop.SanitizeMinimalDep = m.sanitize.Properties.MinimalRuntimeDep || enableMinimalRuntime(m.sanitize) |
| 720 | prop.SanitizeUbsanDep = m.sanitize.Properties.UbsanRuntimeDep || enableUbsanRuntime(m.sanitize) |
| 721 | } |
| 722 | |
| 723 | var libType string |
| 724 | if l.static() { |
| 725 | libType = "static" |
| 726 | } else if l.shared() { |
| 727 | libType = "shared" |
| 728 | } else { |
| 729 | libType = "header" |
| 730 | } |
| 731 | |
| 732 | var stem string |
| 733 | |
| 734 | // install .a or .so |
| 735 | if libType != "header" { |
| 736 | libPath := m.outputFile.Path() |
| 737 | stem = libPath.Base() |
Inseob Kim | c42f2f2 | 2020-07-29 20:32:10 +0900 | [diff] [blame] | 738 | if l.static() && m.sanitize != nil && m.sanitize.isSanitizerEnabled(cfi) { |
| 739 | // both cfi and non-cfi variant for static libraries can exist. |
| 740 | // attach .cfi to distinguish between cfi and non-cfi. |
| 741 | // e.g. libbase.a -> libbase.cfi.a |
| 742 | ext := filepath.Ext(stem) |
| 743 | stem = strings.TrimSuffix(stem, ext) + ".cfi" + ext |
| 744 | prop.Sanitize = "cfi" |
| 745 | prop.ModuleName += ".cfi" |
| 746 | } |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 747 | snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, libType, stem) |
| 748 | ret = append(ret, copyFile(ctx, libPath, snapshotLibOut)) |
| 749 | } else { |
| 750 | stem = ctx.ModuleName(m) |
| 751 | } |
| 752 | |
| 753 | propOut = filepath.Join(snapshotArchDir, targetArch, libType, stem+".json") |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 754 | } else if m.binary() { |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 755 | // binary flags |
| 756 | prop.Symlinks = m.Symlinks() |
| 757 | prop.SharedLibs = m.Properties.SnapshotSharedLibs |
| 758 | |
| 759 | // install bin |
| 760 | binPath := m.outputFile.Path() |
| 761 | snapshotBinOut := filepath.Join(snapshotArchDir, targetArch, "binary", binPath.Base()) |
| 762 | ret = append(ret, copyFile(ctx, binPath, snapshotBinOut)) |
| 763 | propOut = snapshotBinOut + ".json" |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 764 | } else if m.object() { |
| 765 | // object files aren't installed to the device, so their names can conflict. |
| 766 | // Use module name as stem. |
| 767 | objPath := m.outputFile.Path() |
| 768 | snapshotObjOut := filepath.Join(snapshotArchDir, targetArch, "object", |
| 769 | ctx.ModuleName(m)+filepath.Ext(objPath.Base())) |
| 770 | ret = append(ret, copyFile(ctx, objPath, snapshotObjOut)) |
| 771 | propOut = snapshotObjOut + ".json" |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 772 | } else { |
| 773 | ctx.Errorf("unknown module %q in vendor snapshot", m.String()) |
| 774 | return nil |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | j, err := json.Marshal(prop) |
| 778 | if err != nil { |
| 779 | ctx.Errorf("json marshal to %q failed: %#v", propOut, err) |
| 780 | return nil |
| 781 | } |
| 782 | ret = append(ret, writeStringToFile(ctx, string(j), propOut)) |
| 783 | |
| 784 | return ret |
| 785 | } |
| 786 | |
| 787 | ctx.VisitAllModules(func(module android.Module) { |
| 788 | m, ok := module.(*Module) |
Inseob Kim | eda2e9c | 2020-03-03 22:06:32 +0900 | [diff] [blame] | 789 | if !ok { |
| 790 | return |
| 791 | } |
| 792 | |
| 793 | moduleDir := ctx.ModuleDir(module) |
| 794 | if !isVendorSnapshotModule(m, moduleDir) { |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 795 | return |
| 796 | } |
| 797 | |
| 798 | snapshotOutputs = append(snapshotOutputs, installSnapshot(m)...) |
Inseob Kim | eda2e9c | 2020-03-03 22:06:32 +0900 | [diff] [blame] | 799 | if l, ok := m.linker.(snapshotLibraryInterface); ok { |
| 800 | headers = append(headers, l.snapshotHeaders()...) |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 801 | } |
| 802 | |
Bob Badour | a75b057 | 2020-02-18 20:21:55 -0800 | [diff] [blame] | 803 | if len(m.NoticeFiles()) > 0 { |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 804 | noticeName := ctx.ModuleName(m) + ".txt" |
| 805 | noticeOut := filepath.Join(noticeDir, noticeName) |
| 806 | // skip already copied notice file |
| 807 | if !installedNotices[noticeOut] { |
| 808 | installedNotices[noticeOut] = true |
Bob Badour | a75b057 | 2020-02-18 20:21:55 -0800 | [diff] [blame] | 809 | snapshotOutputs = append(snapshotOutputs, combineNotices( |
| 810 | ctx, m.NoticeFiles(), noticeOut)) |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | }) |
| 814 | |
| 815 | // install all headers after removing duplicates |
| 816 | for _, header := range android.FirstUniquePaths(headers) { |
| 817 | snapshotOutputs = append(snapshotOutputs, copyFile( |
| 818 | ctx, header, filepath.Join(includeDir, header.String()))) |
| 819 | } |
| 820 | |
| 821 | // All artifacts are ready. Sort them to normalize ninja and then zip. |
| 822 | sort.Slice(snapshotOutputs, func(i, j int) bool { |
| 823 | return snapshotOutputs[i].String() < snapshotOutputs[j].String() |
| 824 | }) |
| 825 | |
| 826 | zipPath := android.PathForOutput(ctx, snapshotDir, "vendor-"+ctx.Config().DeviceName()+".zip") |
| 827 | zipRule := android.NewRuleBuilder() |
| 828 | |
| 829 | // filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr |
| 830 | snapshotOutputList := android.PathForOutput(ctx, snapshotDir, "vendor-"+ctx.Config().DeviceName()+"_list") |
| 831 | zipRule.Command(). |
| 832 | Text("tr"). |
| 833 | FlagWithArg("-d ", "\\'"). |
| 834 | FlagWithRspFileInputList("< ", snapshotOutputs). |
| 835 | FlagWithOutput("> ", snapshotOutputList) |
| 836 | |
| 837 | zipRule.Temporary(snapshotOutputList) |
| 838 | |
| 839 | zipRule.Command(). |
| 840 | BuiltTool(ctx, "soong_zip"). |
| 841 | FlagWithOutput("-o ", zipPath). |
| 842 | FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()). |
| 843 | FlagWithInput("-l ", snapshotOutputList) |
| 844 | |
| 845 | zipRule.Build(pctx, ctx, zipPath.String(), "vendor snapshot "+zipPath.String()) |
| 846 | zipRule.DeleteTemporaryFiles() |
| 847 | c.vendorSnapshotZipFile = android.OptionalPathForPath(zipPath) |
| 848 | } |
| 849 | |
| 850 | func (c *vendorSnapshotSingleton) MakeVars(ctx android.MakeVarsContext) { |
| 851 | ctx.Strict("SOONG_VENDOR_SNAPSHOT_ZIP", c.vendorSnapshotZipFile.String()) |
| 852 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 853 | |
| 854 | type snapshotInterface interface { |
| 855 | matchesWithDevice(config android.DeviceConfig) bool |
| 856 | } |
| 857 | |
| 858 | var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil) |
| 859 | var _ snapshotInterface = (*vendorSnapshotLibraryDecorator)(nil) |
| 860 | var _ snapshotInterface = (*vendorSnapshotBinaryDecorator)(nil) |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 861 | var _ snapshotInterface = (*vendorSnapshotObjectLinker)(nil) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 862 | |
| 863 | // gathers all snapshot modules for vendor, and disable unnecessary snapshots |
| 864 | // TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules |
| 865 | func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) { |
| 866 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 867 | // don't need snapshot if current |
| 868 | if vndkVersion == "current" || vndkVersion == "" { |
| 869 | return |
| 870 | } |
| 871 | |
| 872 | module, ok := ctx.Module().(*Module) |
| 873 | if !ok || !module.Enabled() || module.VndkVersion() != vndkVersion { |
| 874 | return |
| 875 | } |
| 876 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 877 | if !module.isSnapshotPrebuilt() { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 878 | return |
| 879 | } |
| 880 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 881 | // isSnapshotPrebuilt ensures snapshotInterface |
| 882 | if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 883 | // Disable unnecessary snapshot module, but do not disable |
| 884 | // vndk_prebuilt_shared because they might be packed into vndk APEX |
| 885 | if !module.IsVndk() { |
| 886 | module.Disable() |
| 887 | } |
| 888 | return |
| 889 | } |
| 890 | |
| 891 | var snapshotMap *snapshotMap |
| 892 | |
| 893 | if lib, ok := module.linker.(libraryInterface); ok { |
| 894 | if lib.static() { |
| 895 | snapshotMap = vendorSnapshotStaticLibs(ctx.Config()) |
| 896 | } else if lib.shared() { |
| 897 | snapshotMap = vendorSnapshotSharedLibs(ctx.Config()) |
| 898 | } else { |
| 899 | // header |
| 900 | snapshotMap = vendorSnapshotHeaderLibs(ctx.Config()) |
| 901 | } |
| 902 | } else if _, ok := module.linker.(*vendorSnapshotBinaryDecorator); ok { |
| 903 | snapshotMap = vendorSnapshotBinaries(ctx.Config()) |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 904 | } else if _, ok := module.linker.(*vendorSnapshotObjectLinker); ok { |
| 905 | snapshotMap = vendorSnapshotObjects(ctx.Config()) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 906 | } else { |
| 907 | return |
| 908 | } |
| 909 | |
| 910 | vendorSnapshotsLock.Lock() |
| 911 | defer vendorSnapshotsLock.Unlock() |
| 912 | snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName()) |
| 913 | } |
| 914 | |
| 915 | // Disables source modules which have snapshots |
| 916 | func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) { |
Inseob Kim | 5f64aec | 2020-02-18 17:27:19 +0900 | [diff] [blame] | 917 | if !ctx.Device() { |
| 918 | return |
| 919 | } |
| 920 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 921 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 922 | // don't need snapshot if current |
| 923 | if vndkVersion == "current" || vndkVersion == "" { |
| 924 | return |
| 925 | } |
| 926 | |
| 927 | module, ok := ctx.Module().(*Module) |
| 928 | if !ok { |
| 929 | return |
| 930 | } |
| 931 | |
Inseob Kim | 5f64aec | 2020-02-18 17:27:19 +0900 | [diff] [blame] | 932 | // vendor suffix should be added to snapshots if the source module isn't vendor: true. |
| 933 | if !module.SocSpecific() { |
| 934 | // But we can't just check SocSpecific() since we already passed the image mutator. |
| 935 | // Check ramdisk and recovery to see if we are real "vendor: true" module. |
| 936 | ramdisk_available := module.InRamdisk() && !module.OnlyInRamdisk() |
| 937 | recovery_available := module.InRecovery() && !module.OnlyInRecovery() |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 938 | |
Inseob Kim | 5f64aec | 2020-02-18 17:27:19 +0900 | [diff] [blame] | 939 | if !ramdisk_available && !recovery_available { |
| 940 | vendorSnapshotsLock.Lock() |
| 941 | defer vendorSnapshotsLock.Unlock() |
| 942 | |
| 943 | vendorSuffixModules(ctx.Config())[ctx.ModuleName()] = true |
| 944 | } |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | if module.isSnapshotPrebuilt() || module.VndkVersion() != ctx.DeviceConfig().VndkVersion() { |
| 948 | // only non-snapshot modules with BOARD_VNDK_VERSION |
| 949 | return |
| 950 | } |
| 951 | |
Inseob Kim | 206665c | 2020-06-02 23:48:32 +0900 | [diff] [blame] | 952 | // .. and also filter out llndk library |
| 953 | if module.isLlndk(ctx.Config()) { |
| 954 | return |
| 955 | } |
| 956 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 957 | var snapshotMap *snapshotMap |
| 958 | |
| 959 | if lib, ok := module.linker.(libraryInterface); ok { |
| 960 | if lib.static() { |
| 961 | snapshotMap = vendorSnapshotStaticLibs(ctx.Config()) |
| 962 | } else if lib.shared() { |
| 963 | snapshotMap = vendorSnapshotSharedLibs(ctx.Config()) |
| 964 | } else { |
| 965 | // header |
| 966 | snapshotMap = vendorSnapshotHeaderLibs(ctx.Config()) |
| 967 | } |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 968 | } else if module.binary() { |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 969 | snapshotMap = vendorSnapshotBinaries(ctx.Config()) |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 970 | } else if module.object() { |
| 971 | snapshotMap = vendorSnapshotObjects(ctx.Config()) |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 972 | } else { |
| 973 | return |
| 974 | } |
| 975 | |
| 976 | if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok { |
| 977 | // Corresponding snapshot doesn't exist |
| 978 | return |
| 979 | } |
| 980 | |
| 981 | // Disables source modules if corresponding snapshot exists. |
| 982 | if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() { |
| 983 | // But do not disable because the shared variant depends on the static variant. |
| 984 | module.SkipInstall() |
| 985 | module.Properties.HideFromMake = true |
| 986 | } else { |
| 987 | module.Disable() |
| 988 | } |
| 989 | } |