blob: 8f48f869bb6817dc655de71b657ae8ab82a64294 [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 "strings"
18
19 "android/soong/android"
20)
21
22var (
23 headerExts = []string{".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"}
24)
25
26type snapshotLibraryInterface interface {
27 exportedFlagsProducer
28 libraryInterface
29}
30
31var _ snapshotLibraryInterface = (*prebuiltLibraryLinker)(nil)
32var _ snapshotLibraryInterface = (*libraryDecorator)(nil)
33
Inseob Kimeec88e12020-01-22 11:11:29 +090034type 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
Inseob Kim8471cda2019-11-15 09:59:12 +090061func exportedHeaders(ctx android.SingletonContext, l exportedFlagsProducer) android.Paths {
62 var ret android.Paths
63
64 // Headers in the source tree should be globbed. On the contrast, generated headers
65 // can't be globbed, and they should be manually collected.
66 // So, we first filter out intermediate directories (which contains generated headers)
67 // from exported directories, and then glob headers under remaining directories.
68 for _, path := range append(l.exportedDirs(), l.exportedSystemDirs()...) {
69 dir := path.String()
70 // Skip if dir is for generated headers
71 if strings.HasPrefix(dir, android.PathForOutput(ctx).String()) {
72 continue
73 }
74 exts := headerExts
75 // Glob all files under this special directory, because of C++ headers.
76 if strings.HasPrefix(dir, "external/libcxx/include") {
77 exts = []string{""}
78 }
79 for _, ext := range exts {
80 glob, err := ctx.GlobWithDeps(dir+"/**/*"+ext, nil)
81 if err != nil {
82 ctx.Errorf("%#v\n", err)
83 return nil
84 }
85 for _, header := range glob {
86 if strings.HasSuffix(header, "/") {
87 continue
88 }
89 ret = append(ret, android.PathForSource(ctx, header))
90 }
91 }
92 }
93
94 // Collect generated headers
95 for _, header := range append(l.exportedGeneratedHeaders(), l.exportedDeps()...) {
96 // TODO(b/148123511): remove exportedDeps after cleaning up genrule
97 if strings.HasSuffix(header.Base(), "-phony") {
98 continue
99 }
100 ret = append(ret, header)
101 }
102
103 return ret
104}
105
106func copyFile(ctx android.SingletonContext, path android.Path, out string) android.OutputPath {
107 outPath := android.PathForOutput(ctx, out)
108 ctx.Build(pctx, android.BuildParams{
109 Rule: android.Cp,
110 Input: path,
111 Output: outPath,
112 Description: "Cp " + out,
113 Args: map[string]string{
114 "cpFlags": "-f -L",
115 },
116 })
117 return outPath
118}
119
120func writeStringToFile(ctx android.SingletonContext, content, out string) android.OutputPath {
121 outPath := android.PathForOutput(ctx, out)
122 ctx.Build(pctx, android.BuildParams{
123 Rule: android.WriteFile,
124 Output: outPath,
125 Description: "WriteFile " + out,
126 Args: map[string]string{
127 "content": content,
128 },
129 })
130 return outPath
131}