Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 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. |
| 14 | package cc |
| 15 | |
| 16 | import ( |
| 17 | "strings" |
| 18 | |
| 19 | "android/soong/android" |
| 20 | ) |
| 21 | |
| 22 | var ( |
| 23 | headerExts = []string{".h", ".hh", ".hpp", ".hxx", ".h++", ".inl", ".inc", ".ipp", ".h.generic"} |
| 24 | ) |
| 25 | |
| 26 | type snapshotLibraryInterface interface { |
| 27 | exportedFlagsProducer |
| 28 | libraryInterface |
| 29 | } |
| 30 | |
| 31 | var _ snapshotLibraryInterface = (*prebuiltLibraryLinker)(nil) |
| 32 | var _ snapshotLibraryInterface = (*libraryDecorator)(nil) |
| 33 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 34 | type snapshotMap struct { |
| 35 | snapshots map[string]string |
| 36 | } |
| 37 | |
| 38 | func newSnapshotMap() *snapshotMap { |
| 39 | return &snapshotMap{ |
| 40 | snapshots: make(map[string]string), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func 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") |
| 50 | func (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 |
| 56 | func (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 Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 61 | func 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 | |
| 106 | func 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 | |
Bob Badour | a75b057 | 2020-02-18 20:21:55 -0800 | [diff] [blame^] | 120 | func combineNotices(ctx android.SingletonContext, paths android.Paths, out string) android.OutputPath { |
| 121 | outPath := android.PathForOutput(ctx, out) |
| 122 | ctx.Build(pctx, android.BuildParams{ |
| 123 | Rule: android.Cat, |
| 124 | Inputs: paths, |
| 125 | Output: outPath, |
| 126 | Description: "combine notices for " + out, |
| 127 | }) |
| 128 | return outPath |
| 129 | } |
| 130 | |
Inseob Kim | 8471cda | 2019-11-15 09:59:12 +0900 | [diff] [blame] | 131 | func writeStringToFile(ctx android.SingletonContext, content, out string) android.OutputPath { |
| 132 | outPath := android.PathForOutput(ctx, out) |
| 133 | ctx.Build(pctx, android.BuildParams{ |
| 134 | Rule: android.WriteFile, |
| 135 | Output: outPath, |
| 136 | Description: "WriteFile " + out, |
| 137 | Args: map[string]string{ |
| 138 | "content": content, |
| 139 | }, |
| 140 | }) |
| 141 | return outPath |
| 142 | } |