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.
14package cc
15
16// This file contains utility types and functions for VNDK / vendor snapshot.
17
18import (
19	"android/soong/android"
20)
21
22var (
23	headerExts = []string{".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"}
24)
25
26func (m *Module) IsSnapshotLibrary() bool {
27	if _, ok := m.linker.(snapshotLibraryInterface); ok {
28		return true
29	}
30	return false
31}
32
33func (m *Module) SnapshotHeaders() android.Paths {
34	if m.IsSnapshotLibrary() {
35		return m.linker.(snapshotLibraryInterface).snapshotHeaders()
36	}
37	return android.Paths{}
38}
39
40func (m *Module) Dylib() bool {
41	return false
42}
43
44func (m *Module) Rlib() bool {
45	return false
46}
47
48func (m *Module) SnapshotRuntimeLibs() []string {
49	return m.Properties.SnapshotRuntimeLibs
50}
51
52func (m *Module) SnapshotSharedLibs() []string {
53	return m.Properties.SnapshotSharedLibs
54}
55
56// snapshotLibraryInterface is an interface for libraries captured to VNDK / vendor snapshots.
57type snapshotLibraryInterface interface {
58	libraryInterface
59
60	// collectHeadersForSnapshot is called in GenerateAndroidBuildActions for snapshot aware
61	// modules (See isSnapshotAware below).
62	// This function should gather all headers needed for snapshot.
63	collectHeadersForSnapshot(ctx android.ModuleContext)
64
65	// snapshotHeaders should return collected headers by collectHeadersForSnapshot.
66	// Calling snapshotHeaders before collectHeadersForSnapshot is an error.
67	snapshotHeaders() android.Paths
68}
69
70var _ snapshotLibraryInterface = (*prebuiltLibraryLinker)(nil)
71var _ snapshotLibraryInterface = (*libraryDecorator)(nil)
72
73// snapshotMap is a helper wrapper to a map from base module name to snapshot module name.
74type snapshotMap struct {
75	snapshots map[string]string
76}
77
78func newSnapshotMap() *snapshotMap {
79	return &snapshotMap{
80		snapshots: make(map[string]string),
81	}
82}
83
84func snapshotMapKey(name string, arch android.ArchType) string {
85	return name + ":" + arch.String()
86}
87
88// Adds a snapshot name for given module name and architecture.
89// e.g. add("libbase", X86, "libbase.vndk.29.x86")
90func (s *snapshotMap) add(name string, arch android.ArchType, snapshot string) {
91	s.snapshots[snapshotMapKey(name, arch)] = snapshot
92}
93
94// Returns snapshot name for given module name and architecture, if found.
95// e.g. get("libcutils", X86) => "libcutils.vndk.29.x86", true
96func (s *snapshotMap) get(name string, arch android.ArchType) (snapshot string, found bool) {
97	snapshot, found = s.snapshots[snapshotMapKey(name, arch)]
98	return snapshot, found
99}
100
101// ShouldCollectHeadersForSnapshot determines if the module is a possible candidate for snapshot.
102// If it's true, collectHeadersForSnapshot will be called in GenerateAndroidBuildActions.
103func ShouldCollectHeadersForSnapshot(ctx android.ModuleContext, m LinkableInterface, apexInfo android.ApexInfo) bool {
104	if ctx.DeviceConfig().VndkVersion() != "current" &&
105		ctx.DeviceConfig().RecoverySnapshotVersion() != "current" {
106		return false
107	}
108	if _, ok := isVndkSnapshotAware(ctx.DeviceConfig(), m, apexInfo); ok {
109		return ctx.Config().VndkSnapshotBuildArtifacts()
110	}
111
112	for _, image := range []snapshotImage{vendorSnapshotImageSingleton, recoverySnapshotImageSingleton} {
113		if isSnapshotAware(ctx.DeviceConfig(), m, image.isProprietaryPath(ctx.ModuleDir(), ctx.DeviceConfig()), apexInfo, image) {
114			return true
115		}
116	}
117	return false
118}
119