blob: 20762a8f2ff3edcc83c1f27d3df4621da79f0ccb [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."
33)
34
35var (
36 vendorSnapshotsLock sync.Mutex
37 vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules")
38 vendorSnapshotHeaderLibsKey = android.NewOnceKey("vendorSnapshotHeaderLibs")
39 vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs")
40 vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs")
41 vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries")
42)
43
Inseob Kim5f64aec2020-02-18 17:27:19 +090044// vendor snapshot maps hold names of vendor snapshot modules per arch
Inseob Kimeec88e12020-01-22 11:11:29 +090045func vendorSuffixModules(config android.Config) map[string]bool {
46 return config.Once(vendorSuffixModulesKey, func() interface{} {
47 return make(map[string]bool)
48 }).(map[string]bool)
49}
50
51func vendorSnapshotHeaderLibs(config android.Config) *snapshotMap {
52 return config.Once(vendorSnapshotHeaderLibsKey, func() interface{} {
53 return newSnapshotMap()
54 }).(*snapshotMap)
55}
56
57func vendorSnapshotSharedLibs(config android.Config) *snapshotMap {
58 return config.Once(vendorSnapshotSharedLibsKey, func() interface{} {
59 return newSnapshotMap()
60 }).(*snapshotMap)
61}
62
63func vendorSnapshotStaticLibs(config android.Config) *snapshotMap {
64 return config.Once(vendorSnapshotStaticLibsKey, func() interface{} {
65 return newSnapshotMap()
66 }).(*snapshotMap)
67}
68
69func vendorSnapshotBinaries(config android.Config) *snapshotMap {
70 return config.Once(vendorSnapshotBinariesKey, func() interface{} {
71 return newSnapshotMap()
72 }).(*snapshotMap)
73}
74
75type vendorSnapshotLibraryProperties struct {
76 // snapshot version.
77 Version string
78
79 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
80 Target_arch string
81
82 // Prebuilt file for each arch.
83 Src *string `android:"arch_variant"`
84
85 // list of flags that will be used for any module that links against this module.
86 Export_flags []string `android:"arch_variant"`
87
88 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined symbols,
89 // etc).
90 Check_elf_files *bool
91
92 // Whether this prebuilt needs to depend on sanitize ubsan runtime or not.
93 Sanitize_ubsan_dep *bool `android:"arch_variant"`
94
95 // Whether this prebuilt needs to depend on sanitize minimal runtime or not.
96 Sanitize_minimal_dep *bool `android:"arch_variant"`
97}
98
99type vendorSnapshotLibraryDecorator struct {
100 *libraryDecorator
101 properties vendorSnapshotLibraryProperties
102 androidMkVendorSuffix bool
103}
104
105func (p *vendorSnapshotLibraryDecorator) Name(name string) string {
106 return name + p.NameSuffix()
107}
108
109func (p *vendorSnapshotLibraryDecorator) NameSuffix() string {
110 versionSuffix := p.version()
111 if p.arch() != "" {
112 versionSuffix += "." + p.arch()
113 }
114
115 var linkageSuffix string
116 if p.buildShared() {
117 linkageSuffix = vendorSnapshotSharedSuffix
118 } else if p.buildStatic() {
119 linkageSuffix = vendorSnapshotStaticSuffix
120 } else {
121 linkageSuffix = vendorSnapshotHeaderSuffix
122 }
123
124 return linkageSuffix + versionSuffix
125}
126
127func (p *vendorSnapshotLibraryDecorator) version() string {
128 return p.properties.Version
129}
130
131func (p *vendorSnapshotLibraryDecorator) arch() string {
132 return p.properties.Target_arch
133}
134
135func (p *vendorSnapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
136 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
137 return p.libraryDecorator.linkerFlags(ctx, flags)
138}
139
140func (p *vendorSnapshotLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
141 arches := config.Arches()
142 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
143 return false
144 }
145 if !p.header() && p.properties.Src == nil {
146 return false
147 }
148 return true
149}
150
151func (p *vendorSnapshotLibraryDecorator) link(ctx ModuleContext,
152 flags Flags, deps PathDeps, objs Objects) android.Path {
153 m := ctx.Module().(*Module)
154 p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()]
155
156 if p.header() {
157 return p.libraryDecorator.link(ctx, flags, deps, objs)
158 }
159
160 if !p.matchesWithDevice(ctx.DeviceConfig()) {
161 return nil
162 }
163
164 p.libraryDecorator.exportIncludes(ctx)
165 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
166
167 in := android.PathForModuleSrc(ctx, *p.properties.Src)
168 p.unstrippedOutputFile = in
169
170 if p.shared() {
171 libName := in.Base()
172 builderFlags := flagsToBuilderFlags(flags)
173
174 // Optimize out relinking against shared libraries whose interface hasn't changed by
175 // depending on a table of contents file instead of the library itself.
176 tocFile := android.PathForModuleOut(ctx, libName+".toc")
177 p.tocFile = android.OptionalPathForPath(tocFile)
178 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
179 }
180
181 return in
182}
183
184func (p *vendorSnapshotLibraryDecorator) nativeCoverage() bool {
185 return false
186}
187
188func (p *vendorSnapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) {
189 if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) {
190 p.baseInstaller.install(ctx, file)
191 }
192}
193
194type vendorSnapshotInterface interface {
195 version() string
196}
197
198func vendorSnapshotLoadHook(ctx android.LoadHookContext, p vendorSnapshotInterface) {
199 if p.version() != ctx.DeviceConfig().VndkVersion() {
200 ctx.Module().Disable()
201 return
202 }
203}
204
205func vendorSnapshotLibrary() (*Module, *vendorSnapshotLibraryDecorator) {
206 module, library := NewLibrary(android.DeviceSupported)
207
208 module.stl = nil
209 module.sanitize = nil
210 library.StripProperties.Strip.None = BoolPtr(true)
211
212 prebuilt := &vendorSnapshotLibraryDecorator{
213 libraryDecorator: library,
214 }
215
216 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
217 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
218
219 // Prevent default system libs (libc, libm, and libdl) from being linked
220 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
221 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
222 }
223
224 module.compiler = nil
225 module.linker = prebuilt
226 module.installer = prebuilt
227
228 module.AddProperties(
229 &prebuilt.properties,
230 )
231
232 return module, prebuilt
233}
234
235func VendorSnapshotSharedFactory() android.Module {
236 module, prebuilt := vendorSnapshotLibrary()
237 prebuilt.libraryDecorator.BuildOnlyShared()
238 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
239 vendorSnapshotLoadHook(ctx, prebuilt)
240 })
241 return module.Init()
242}
243
244func VendorSnapshotStaticFactory() android.Module {
245 module, prebuilt := vendorSnapshotLibrary()
246 prebuilt.libraryDecorator.BuildOnlyStatic()
247 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
248 vendorSnapshotLoadHook(ctx, prebuilt)
249 })
250 return module.Init()
251}
252
253func VendorSnapshotHeaderFactory() android.Module {
254 module, prebuilt := vendorSnapshotLibrary()
255 prebuilt.libraryDecorator.HeaderOnly()
256 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
257 vendorSnapshotLoadHook(ctx, prebuilt)
258 })
259 return module.Init()
260}
261
262type vendorSnapshotBinaryProperties struct {
263 // snapshot version.
264 Version string
265
266 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64_ab')
267 Target_arch string
268
269 // Prebuilt file for each arch.
270 Src *string `android:"arch_variant"`
271}
272
273type vendorSnapshotBinaryDecorator struct {
274 *binaryDecorator
275 properties vendorSnapshotBinaryProperties
276 androidMkVendorSuffix bool
277}
278
279func (p *vendorSnapshotBinaryDecorator) Name(name string) string {
280 return name + p.NameSuffix()
281}
282
283func (p *vendorSnapshotBinaryDecorator) NameSuffix() string {
284 versionSuffix := p.version()
285 if p.arch() != "" {
286 versionSuffix += "." + p.arch()
287 }
288 return vendorSnapshotBinarySuffix + versionSuffix
289}
290
291func (p *vendorSnapshotBinaryDecorator) version() string {
292 return p.properties.Version
293}
294
295func (p *vendorSnapshotBinaryDecorator) arch() string {
296 return p.properties.Target_arch
297}
298
299func (p *vendorSnapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
300 if config.DeviceArch() != p.arch() {
301 return false
302 }
303 if p.properties.Src == nil {
304 return false
305 }
306 return true
307}
308
309func (p *vendorSnapshotBinaryDecorator) link(ctx ModuleContext,
310 flags Flags, deps PathDeps, objs Objects) android.Path {
311 if !p.matchesWithDevice(ctx.DeviceConfig()) {
312 return nil
313 }
314
315 in := android.PathForModuleSrc(ctx, *p.properties.Src)
316 builderFlags := flagsToBuilderFlags(flags)
317 p.unstrippedOutputFile = in
318 binName := in.Base()
319 if p.needsStrip(ctx) {
320 stripped := android.PathForModuleOut(ctx, "stripped", binName)
321 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
322 in = stripped
323 }
324
325 m := ctx.Module().(*Module)
326 p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()]
327
328 // use cpExecutable to make it executable
329 outputFile := android.PathForModuleOut(ctx, binName)
330 ctx.Build(pctx, android.BuildParams{
331 Rule: android.CpExecutable,
332 Description: "prebuilt",
333 Output: outputFile,
334 Input: in,
335 })
336
337 return outputFile
338}
339
340func VendorSnapshotBinaryFactory() android.Module {
341 module, binary := NewBinary(android.DeviceSupported)
342 binary.baseLinker.Properties.No_libcrt = BoolPtr(true)
343 binary.baseLinker.Properties.Nocrt = BoolPtr(true)
344
345 // Prevent default system libs (libc, libm, and libdl) from being linked
346 if binary.baseLinker.Properties.System_shared_libs == nil {
347 binary.baseLinker.Properties.System_shared_libs = []string{}
348 }
349
350 prebuilt := &vendorSnapshotBinaryDecorator{
351 binaryDecorator: binary,
352 }
353
354 module.compiler = nil
355 module.sanitize = nil
356 module.stl = nil
357 module.linker = prebuilt
358
359 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
360 vendorSnapshotLoadHook(ctx, prebuilt)
361 })
362
363 module.AddProperties(&prebuilt.properties)
364 return module.Init()
365}
366
Inseob Kim8471cda2019-11-15 09:59:12 +0900367func init() {
368 android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton)
Inseob Kimeec88e12020-01-22 11:11:29 +0900369 android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory)
370 android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory)
371 android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory)
372 android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory)
Inseob Kim8471cda2019-11-15 09:59:12 +0900373}
374
375func VendorSnapshotSingleton() android.Singleton {
376 return &vendorSnapshotSingleton{}
377}
378
379type vendorSnapshotSingleton struct {
380 vendorSnapshotZipFile android.OptionalPath
381}
382
383var (
384 // Modules under following directories are ignored. They are OEM's and vendor's
385 // proprietary modules(device/, vendor/, and hardware/).
386 // TODO(b/65377115): Clean up these with more maintainable way
387 vendorProprietaryDirs = []string{
388 "device",
389 "vendor",
390 "hardware",
391 }
392
393 // Modules under following directories are included as they are in AOSP,
394 // although hardware/ is normally for vendor's own.
395 // TODO(b/65377115): Clean up these with more maintainable way
396 aospDirsUnderProprietary = []string{
397 "hardware/interfaces",
398 "hardware/libhardware",
399 "hardware/libhardware_legacy",
400 "hardware/ril",
401 }
402)
403
404// Determine if a dir under source tree is an SoC-owned proprietary directory, such as
405// device/, vendor/, etc.
406func isVendorProprietaryPath(dir string) bool {
407 for _, p := range vendorProprietaryDirs {
408 if strings.HasPrefix(dir, p) {
409 // filter out AOSP defined directories, e.g. hardware/interfaces/
410 aosp := false
411 for _, p := range aospDirsUnderProprietary {
412 if strings.HasPrefix(dir, p) {
413 aosp = true
414 break
415 }
416 }
417 if !aosp {
418 return true
419 }
420 }
421 }
422 return false
423}
424
425// Determine if a module is going to be included in vendor snapshot or not.
426//
427// Targets of vendor snapshot are "vendor: true" or "vendor_available: true" modules in
428// AOSP. They are not guaranteed to be compatible with older vendor images. (e.g. might
429// depend on newer VNDK) So they are captured as vendor snapshot To build older vendor
430// image and newer system image altogether.
431func isVendorSnapshotModule(ctx android.SingletonContext, m *Module) bool {
432 if !m.Enabled() {
433 return false
434 }
435 // skip proprietary modules, but include all VNDK (static)
436 if isVendorProprietaryPath(ctx.ModuleDir(m)) && !m.IsVndk() {
437 return false
438 }
439 if m.Target().Os.Class != android.Device {
440 return false
441 }
442 if m.Target().NativeBridge == android.NativeBridgeEnabled {
443 return false
444 }
445 // the module must be installed in /vendor
446 if !m.installable() || m.isSnapshotPrebuilt() || !m.inVendor() {
447 return false
448 }
449 // exclude test modules
450 if _, ok := m.linker.(interface{ gtest() bool }); ok {
451 return false
452 }
453 // TODO(b/65377115): add full support for sanitizer
454 if m.sanitize != nil && !m.sanitize.isUnsanitizedVariant() {
455 return false
456 }
457
458 // Libraries
459 if l, ok := m.linker.(snapshotLibraryInterface); ok {
460 if l.static() {
461 return proptools.BoolDefault(m.VendorProperties.Vendor_available, true)
462 }
463 if l.shared() {
464 return !m.IsVndk()
465 }
466 return true
467 }
468
469 // Binaries
470 _, ok := m.linker.(*binaryDecorator)
471 if !ok {
472 if _, ok := m.linker.(*prebuiltBinaryLinker); !ok {
473 return false
474 }
475 }
476 return proptools.BoolDefault(m.VendorProperties.Vendor_available, true)
477}
478
479func (c *vendorSnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
480 // BOARD_VNDK_VERSION must be set to 'current' in order to generate a vendor snapshot.
481 if ctx.DeviceConfig().VndkVersion() != "current" {
482 return
483 }
484
485 var snapshotOutputs android.Paths
486
487 /*
488 Vendor snapshot zipped artifacts directory structure:
489 {SNAPSHOT_ARCH}/
490 arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
491 shared/
492 (.so shared libraries)
493 static/
494 (.a static libraries)
495 header/
496 (header only libraries)
497 binary/
498 (executable binaries)
499 arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
500 shared/
501 (.so shared libraries)
502 static/
503 (.a static libraries)
504 header/
505 (header only libraries)
506 binary/
507 (executable binaries)
508 NOTICE_FILES/
509 (notice files, e.g. libbase.txt)
510 configs/
511 (config files, e.g. init.rc files, vintf_fragments.xml files, etc.)
512 include/
513 (header files of same directory structure with source tree)
514 */
515
516 snapshotDir := "vendor-snapshot"
517 snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch())
518
519 includeDir := filepath.Join(snapshotArchDir, "include")
520 configsDir := filepath.Join(snapshotArchDir, "configs")
521 noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
522
523 installedNotices := make(map[string]bool)
524 installedConfigs := make(map[string]bool)
525
526 var headers android.Paths
527
528 type vendorSnapshotLibraryInterface interface {
529 exportedFlagsProducer
530 libraryInterface
531 }
532
533 var _ vendorSnapshotLibraryInterface = (*prebuiltLibraryLinker)(nil)
534 var _ vendorSnapshotLibraryInterface = (*libraryDecorator)(nil)
535
536 installSnapshot := func(m *Module) android.Paths {
537 targetArch := "arch-" + m.Target().Arch.ArchType.String()
538 if m.Target().Arch.ArchVariant != "" {
539 targetArch += "-" + m.Target().Arch.ArchVariant
540 }
541
542 var ret android.Paths
543
544 prop := struct {
545 ModuleName string `json:",omitempty"`
546 RelativeInstallPath string `json:",omitempty"`
547
548 // library flags
549 ExportedDirs []string `json:",omitempty"`
550 ExportedSystemDirs []string `json:",omitempty"`
551 ExportedFlags []string `json:",omitempty"`
552 SanitizeMinimalDep bool `json:",omitempty"`
553 SanitizeUbsanDep bool `json:",omitempty"`
554
555 // binary flags
556 Symlinks []string `json:",omitempty"`
557
558 // dependencies
559 SharedLibs []string `json:",omitempty"`
560 RuntimeLibs []string `json:",omitempty"`
561 Required []string `json:",omitempty"`
562
563 // extra config files
564 InitRc []string `json:",omitempty"`
565 VintfFragments []string `json:",omitempty"`
566 }{}
567
568 // Common properties among snapshots.
569 prop.ModuleName = ctx.ModuleName(m)
570 prop.RelativeInstallPath = m.RelativeInstallPath()
571 prop.RuntimeLibs = m.Properties.SnapshotRuntimeLibs
572 prop.Required = m.RequiredModuleNames()
573 for _, path := range m.InitRc() {
574 prop.InitRc = append(prop.InitRc, filepath.Join("configs", path.Base()))
575 }
576 for _, path := range m.VintfFragments() {
577 prop.VintfFragments = append(prop.VintfFragments, filepath.Join("configs", path.Base()))
578 }
579
580 // install config files. ignores any duplicates.
581 for _, path := range append(m.InitRc(), m.VintfFragments()...) {
582 out := filepath.Join(configsDir, path.Base())
583 if !installedConfigs[out] {
584 installedConfigs[out] = true
585 ret = append(ret, copyFile(ctx, path, out))
586 }
587 }
588
589 var propOut string
590
591 if l, ok := m.linker.(vendorSnapshotLibraryInterface); ok {
592 // library flags
593 prop.ExportedFlags = l.exportedFlags()
594 for _, dir := range l.exportedDirs() {
595 prop.ExportedDirs = append(prop.ExportedDirs, filepath.Join("include", dir.String()))
596 }
597 for _, dir := range l.exportedSystemDirs() {
598 prop.ExportedSystemDirs = append(prop.ExportedSystemDirs, filepath.Join("include", dir.String()))
599 }
600 // shared libs dependencies aren't meaningful on static or header libs
601 if l.shared() {
602 prop.SharedLibs = m.Properties.SnapshotSharedLibs
603 }
604 if l.static() && m.sanitize != nil {
605 prop.SanitizeMinimalDep = m.sanitize.Properties.MinimalRuntimeDep || enableMinimalRuntime(m.sanitize)
606 prop.SanitizeUbsanDep = m.sanitize.Properties.UbsanRuntimeDep || enableUbsanRuntime(m.sanitize)
607 }
608
609 var libType string
610 if l.static() {
611 libType = "static"
612 } else if l.shared() {
613 libType = "shared"
614 } else {
615 libType = "header"
616 }
617
618 var stem string
619
620 // install .a or .so
621 if libType != "header" {
622 libPath := m.outputFile.Path()
623 stem = libPath.Base()
624 snapshotLibOut := filepath.Join(snapshotArchDir, targetArch, libType, stem)
625 ret = append(ret, copyFile(ctx, libPath, snapshotLibOut))
626 } else {
627 stem = ctx.ModuleName(m)
628 }
629
630 propOut = filepath.Join(snapshotArchDir, targetArch, libType, stem+".json")
631 } else {
632 // binary flags
633 prop.Symlinks = m.Symlinks()
634 prop.SharedLibs = m.Properties.SnapshotSharedLibs
635
636 // install bin
637 binPath := m.outputFile.Path()
638 snapshotBinOut := filepath.Join(snapshotArchDir, targetArch, "binary", binPath.Base())
639 ret = append(ret, copyFile(ctx, binPath, snapshotBinOut))
640 propOut = snapshotBinOut + ".json"
641 }
642
643 j, err := json.Marshal(prop)
644 if err != nil {
645 ctx.Errorf("json marshal to %q failed: %#v", propOut, err)
646 return nil
647 }
648 ret = append(ret, writeStringToFile(ctx, string(j), propOut))
649
650 return ret
651 }
652
653 ctx.VisitAllModules(func(module android.Module) {
654 m, ok := module.(*Module)
655 if !ok || !isVendorSnapshotModule(ctx, m) {
656 return
657 }
658
659 snapshotOutputs = append(snapshotOutputs, installSnapshot(m)...)
660 if l, ok := m.linker.(vendorSnapshotLibraryInterface); ok {
661 headers = append(headers, exportedHeaders(ctx, l)...)
662 }
663
Bob Badoura75b0572020-02-18 20:21:55 -0800664 if len(m.NoticeFiles()) > 0 {
Inseob Kim8471cda2019-11-15 09:59:12 +0900665 noticeName := ctx.ModuleName(m) + ".txt"
666 noticeOut := filepath.Join(noticeDir, noticeName)
667 // skip already copied notice file
668 if !installedNotices[noticeOut] {
669 installedNotices[noticeOut] = true
Bob Badoura75b0572020-02-18 20:21:55 -0800670 snapshotOutputs = append(snapshotOutputs, combineNotices(
671 ctx, m.NoticeFiles(), noticeOut))
Inseob Kim8471cda2019-11-15 09:59:12 +0900672 }
673 }
674 })
675
676 // install all headers after removing duplicates
677 for _, header := range android.FirstUniquePaths(headers) {
678 snapshotOutputs = append(snapshotOutputs, copyFile(
679 ctx, header, filepath.Join(includeDir, header.String())))
680 }
681
682 // All artifacts are ready. Sort them to normalize ninja and then zip.
683 sort.Slice(snapshotOutputs, func(i, j int) bool {
684 return snapshotOutputs[i].String() < snapshotOutputs[j].String()
685 })
686
687 zipPath := android.PathForOutput(ctx, snapshotDir, "vendor-"+ctx.Config().DeviceName()+".zip")
688 zipRule := android.NewRuleBuilder()
689
690 // filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr
691 snapshotOutputList := android.PathForOutput(ctx, snapshotDir, "vendor-"+ctx.Config().DeviceName()+"_list")
692 zipRule.Command().
693 Text("tr").
694 FlagWithArg("-d ", "\\'").
695 FlagWithRspFileInputList("< ", snapshotOutputs).
696 FlagWithOutput("> ", snapshotOutputList)
697
698 zipRule.Temporary(snapshotOutputList)
699
700 zipRule.Command().
701 BuiltTool(ctx, "soong_zip").
702 FlagWithOutput("-o ", zipPath).
703 FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()).
704 FlagWithInput("-l ", snapshotOutputList)
705
706 zipRule.Build(pctx, ctx, zipPath.String(), "vendor snapshot "+zipPath.String())
707 zipRule.DeleteTemporaryFiles()
708 c.vendorSnapshotZipFile = android.OptionalPathForPath(zipPath)
709}
710
711func (c *vendorSnapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
712 ctx.Strict("SOONG_VENDOR_SNAPSHOT_ZIP", c.vendorSnapshotZipFile.String())
713}
Inseob Kimeec88e12020-01-22 11:11:29 +0900714
715type snapshotInterface interface {
716 matchesWithDevice(config android.DeviceConfig) bool
717}
718
719var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
720var _ snapshotInterface = (*vendorSnapshotLibraryDecorator)(nil)
721var _ snapshotInterface = (*vendorSnapshotBinaryDecorator)(nil)
722
723// gathers all snapshot modules for vendor, and disable unnecessary snapshots
724// TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules
725func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) {
726 vndkVersion := ctx.DeviceConfig().VndkVersion()
727 // don't need snapshot if current
728 if vndkVersion == "current" || vndkVersion == "" {
729 return
730 }
731
732 module, ok := ctx.Module().(*Module)
733 if !ok || !module.Enabled() || module.VndkVersion() != vndkVersion {
734 return
735 }
736
737 snapshot, ok := module.linker.(snapshotInterface)
738 if !ok {
739 return
740 }
741
742 if !snapshot.matchesWithDevice(ctx.DeviceConfig()) {
743 // Disable unnecessary snapshot module, but do not disable
744 // vndk_prebuilt_shared because they might be packed into vndk APEX
745 if !module.IsVndk() {
746 module.Disable()
747 }
748 return
749 }
750
751 var snapshotMap *snapshotMap
752
753 if lib, ok := module.linker.(libraryInterface); ok {
754 if lib.static() {
755 snapshotMap = vendorSnapshotStaticLibs(ctx.Config())
756 } else if lib.shared() {
757 snapshotMap = vendorSnapshotSharedLibs(ctx.Config())
758 } else {
759 // header
760 snapshotMap = vendorSnapshotHeaderLibs(ctx.Config())
761 }
762 } else if _, ok := module.linker.(*vendorSnapshotBinaryDecorator); ok {
763 snapshotMap = vendorSnapshotBinaries(ctx.Config())
764 } else {
765 return
766 }
767
768 vendorSnapshotsLock.Lock()
769 defer vendorSnapshotsLock.Unlock()
770 snapshotMap.add(module.BaseModuleName(), ctx.Arch().ArchType, ctx.ModuleName())
771}
772
773// Disables source modules which have snapshots
774func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
Inseob Kim5f64aec2020-02-18 17:27:19 +0900775 if !ctx.Device() {
776 return
777 }
778
Inseob Kimeec88e12020-01-22 11:11:29 +0900779 vndkVersion := ctx.DeviceConfig().VndkVersion()
780 // don't need snapshot if current
781 if vndkVersion == "current" || vndkVersion == "" {
782 return
783 }
784
785 module, ok := ctx.Module().(*Module)
786 if !ok {
787 return
788 }
789
Inseob Kim5f64aec2020-02-18 17:27:19 +0900790 // vendor suffix should be added to snapshots if the source module isn't vendor: true.
791 if !module.SocSpecific() {
792 // But we can't just check SocSpecific() since we already passed the image mutator.
793 // Check ramdisk and recovery to see if we are real "vendor: true" module.
794 ramdisk_available := module.InRamdisk() && !module.OnlyInRamdisk()
795 recovery_available := module.InRecovery() && !module.OnlyInRecovery()
Inseob Kimeec88e12020-01-22 11:11:29 +0900796
Inseob Kim5f64aec2020-02-18 17:27:19 +0900797 if !ramdisk_available && !recovery_available {
798 vendorSnapshotsLock.Lock()
799 defer vendorSnapshotsLock.Unlock()
800
801 vendorSuffixModules(ctx.Config())[ctx.ModuleName()] = true
802 }
Inseob Kimeec88e12020-01-22 11:11:29 +0900803 }
804
805 if module.isSnapshotPrebuilt() || module.VndkVersion() != ctx.DeviceConfig().VndkVersion() {
806 // only non-snapshot modules with BOARD_VNDK_VERSION
807 return
808 }
809
810 var snapshotMap *snapshotMap
811
812 if lib, ok := module.linker.(libraryInterface); ok {
813 if lib.static() {
814 snapshotMap = vendorSnapshotStaticLibs(ctx.Config())
815 } else if lib.shared() {
816 snapshotMap = vendorSnapshotSharedLibs(ctx.Config())
817 } else {
818 // header
819 snapshotMap = vendorSnapshotHeaderLibs(ctx.Config())
820 }
821 } else if _, ok := module.linker.(*binaryDecorator); ok {
822 snapshotMap = vendorSnapshotBinaries(ctx.Config())
823 } else if _, ok := module.linker.(*prebuiltBinaryLinker); ok {
824 snapshotMap = vendorSnapshotBinaries(ctx.Config())
825 } else {
826 return
827 }
828
829 if _, ok := snapshotMap.get(ctx.ModuleName(), ctx.Arch().ArchType); !ok {
830 // Corresponding snapshot doesn't exist
831 return
832 }
833
834 // Disables source modules if corresponding snapshot exists.
835 if lib, ok := module.linker.(libraryInterface); ok && lib.buildStatic() && lib.buildShared() {
836 // But do not disable because the shared variant depends on the static variant.
837 module.SkipInstall()
838 module.Properties.HideFromMake = true
839 } else {
840 module.Disable()
841 }
842}