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
16import (
17	"android/soong/android"
18)
19
20var (
21	headerExts = []string{".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"}
22)
23
24type snapshotLibraryInterface interface {
25	exportedFlagsProducer
26	libraryInterface
27	collectHeadersForSnapshot(ctx android.ModuleContext)
28	snapshotHeaders() android.Paths
29}
30
31var _ snapshotLibraryInterface = (*prebuiltLibraryLinker)(nil)
32var _ snapshotLibraryInterface = (*libraryDecorator)(nil)
33
34type snapshotMap struct {
35	snapshots map[string]string
36}
37
38func newSnapshotMap() *snapshotMap {
39	return &snapshotMap{
40		snapshots: make(map[string]string),
41	}
42}
43
44func snapshotMapKey(name string, arch android.ArchType) string {
45	return name + ":" + arch.String()
46}
47
48// Adds a snapshot name for given module name and architecture.
49// e.g. add("libbase", X86, "libbase.vndk.29.x86")
50func (s *snapshotMap) add(name string, arch android.ArchType, snapshot string) {
51	s.snapshots[snapshotMapKey(name, arch)] = snapshot
52}
53
54// Returns snapshot name for given module name and architecture, if found.
55// e.g. get("libcutils", X86) => "libcutils.vndk.29.x86", true
56func (s *snapshotMap) get(name string, arch android.ArchType) (snapshot string, found bool) {
57	snapshot, found = s.snapshots[snapshotMapKey(name, arch)]
58	return snapshot, found
59}
60
61func isSnapshotAware(ctx android.ModuleContext, m *Module) bool {
62	if _, _, ok := isVndkSnapshotLibrary(ctx.DeviceConfig(), m); ok {
63		return ctx.Config().VndkSnapshotBuildArtifacts()
64	} else if isVendorSnapshotModule(m, ctx.ModuleDir()) {
65		return true
66	}
67	return false
68}
69
70func copyFile(ctx android.SingletonContext, path android.Path, out string) android.OutputPath {
71	outPath := android.PathForOutput(ctx, out)
72	ctx.Build(pctx, android.BuildParams{
73		Rule:        android.Cp,
74		Input:       path,
75		Output:      outPath,
76		Description: "Cp " + out,
77		Args: map[string]string{
78			"cpFlags": "-f -L",
79		},
80	})
81	return outPath
82}
83
84func combineNotices(ctx android.SingletonContext, paths android.Paths, out string) android.OutputPath {
85	outPath := android.PathForOutput(ctx, out)
86	ctx.Build(pctx, android.BuildParams{
87		Rule:        android.Cat,
88		Inputs:      paths,
89		Output:      outPath,
90		Description: "combine notices for " + out,
91	})
92	return outPath
93}
94
95func writeStringToFile(ctx android.SingletonContext, content, out string) android.OutputPath {
96	outPath := android.PathForOutput(ctx, out)
97	ctx.Build(pctx, android.BuildParams{
98		Rule:        android.WriteFile,
99		Output:      outPath,
100		Description: "WriteFile " + out,
101		Args: map[string]string{
102			"content": content,
103		},
104	})
105	return outPath
106}
107