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