Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Jayant Chowdhary | dcd33b6 | 2018-02-23 16:43:23 -0800 | [diff] [blame] | 18 | "sync" |
Jayant Chowdhary | 9677e8c | 2017-06-15 14:45:18 -0700 | [diff] [blame] | 19 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 20 | "android/soong/android" |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 21 | "android/soong/cc/config" |
| 22 | ) |
| 23 | |
Jayant Chowdhary | dcd33b6 | 2018-02-23 16:43:23 -0800 | [diff] [blame] | 24 | var ( |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 25 | lsdumpPaths []string |
| 26 | lsdumpPathsLock sync.Mutex |
Jayant Chowdhary | dcd33b6 | 2018-02-23 16:43:23 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 29 | type SAbiProperties struct { |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 30 | // Whether ABI dump should be created for this module. |
| 31 | // Set by `sabiDepsMutator` if this module is a shared library that needs ABI check, or a static |
| 32 | // library that is depended on by an ABI checked library. |
| 33 | ShouldCreateSourceAbiDump bool `blueprint:"mutated"` |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 34 | |
| 35 | // Include directories that may contain ABI information exported by a library. |
| 36 | // These directories are passed to the header-abi-dumper. |
Inseob Kim | 6937844 | 2019-06-03 19:10:47 +0900 | [diff] [blame] | 37 | ReexportedIncludes []string `blueprint:"mutated"` |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type sabi struct { |
| 41 | Properties SAbiProperties |
| 42 | } |
| 43 | |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 44 | func (sabi *sabi) props() []interface{} { |
| 45 | return []interface{}{&sabi.Properties} |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 48 | func (sabi *sabi) begin(ctx BaseModuleContext) {} |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 49 | |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 50 | func (sabi *sabi) deps(ctx BaseModuleContext, deps Deps) Deps { |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 51 | return deps |
| 52 | } |
| 53 | |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 54 | func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags { |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 55 | // Filter out flags which libTooling don't understand. |
| 56 | // This is here for legacy reasons and future-proof, in case the version of libTooling and clang |
| 57 | // diverge. |
| 58 | flags.Local.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CFlags) |
| 59 | flags.Global.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CFlags) |
| 60 | flags.Local.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CppFlags) |
| 61 | flags.Global.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CppFlags) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 62 | return flags |
| 63 | } |
| 64 | |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 65 | // Returns true if ABI dump should be created for this library, either because library is ABI |
| 66 | // checked or is depended on by an ABI checked library. |
| 67 | // Could be called as a nil receiver. |
| 68 | func (sabi *sabi) shouldCreateSourceAbiDump() bool { |
| 69 | return sabi != nil && sabi.Properties.ShouldCreateSourceAbiDump |
| 70 | } |
| 71 | |
| 72 | // Returns a string that represents the class of the ABI dump. |
| 73 | // Returns an empty string if ABI check is disabled for this library. |
| 74 | func classifySourceAbiDump(ctx android.BaseModuleContext) string { |
| 75 | m := ctx.Module().(*Module) |
| 76 | if m.library.headerAbiCheckerExplicitlyDisabled() { |
| 77 | return "" |
| 78 | } |
| 79 | // Return NDK if the library is both NDK and LLNDK. |
| 80 | if m.IsNdk(ctx.Config()) { |
| 81 | return "NDK" |
| 82 | } |
| 83 | if m.isLlndkPublic(ctx.Config()) { |
| 84 | return "LLNDK" |
| 85 | } |
| 86 | if m.UseVndk() && m.IsVndk() && !m.IsVndkPrivate(ctx.Config()) { |
| 87 | if m.isVndkSp() { |
| 88 | if m.IsVndkExt() { |
| 89 | return "VNDK-SP-ext" |
| 90 | } else { |
| 91 | return "VNDK-SP" |
| 92 | } |
| 93 | } else { |
| 94 | if m.IsVndkExt() { |
| 95 | return "VNDK-ext" |
| 96 | } else { |
| 97 | return "VNDK-core" |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | if m.library.headerAbiCheckerEnabled() || m.library.hasStubsVariants() { |
| 102 | return "PLATFORM" |
| 103 | } |
| 104 | return "" |
| 105 | } |
| 106 | |
| 107 | // Called from sabiDepsMutator to check whether ABI dumps should be created for this module. |
| 108 | // ctx should be wrapping a native library type module. |
| 109 | func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool { |
| 110 | if ctx.Fuchsia() { |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 111 | return false |
| 112 | } |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 113 | |
| 114 | // Only generate ABI dump for device modules. |
| 115 | if !ctx.Device() { |
| 116 | return false |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 117 | } |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 118 | |
| 119 | m := ctx.Module().(*Module) |
| 120 | |
| 121 | // Only create ABI dump for native library module types. |
| 122 | if m.library == nil { |
| 123 | return false |
| 124 | } |
| 125 | |
| 126 | // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries. |
| 127 | if m.library.static() { |
| 128 | return m.sabi.shouldCreateSourceAbiDump() |
| 129 | } |
| 130 | |
| 131 | // Module is shared library type. |
| 132 | |
| 133 | // Don't create ABI dump for prebuilts. |
| 134 | if m.Prebuilt() != nil || m.isSnapshotPrebuilt() { |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | // Coverage builds have extra symbols. |
| 139 | if m.isCoverageVariant() { |
| 140 | return false |
| 141 | } |
| 142 | |
| 143 | // Some sanitizer variants may have different ABI. |
| 144 | if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() { |
| 145 | return false |
| 146 | } |
| 147 | |
| 148 | // Don't create ABI dump for stubs. |
| 149 | if m.isNDKStubLibrary() || m.IsStubs() { |
| 150 | return false |
| 151 | } |
| 152 | |
| 153 | // Special case for APEX variants. |
| 154 | if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() { |
| 155 | // Don't create ABI dump if this library is for APEX but isn't exported. |
| 156 | if !m.HasStubsVariants() { |
| 157 | return false |
| 158 | } |
| 159 | if !m.library.headerAbiCheckerEnabled() { |
| 160 | // Skip ABI checks if this library is for APEX and did not explicitly enable |
| 161 | // ABI checks. |
| 162 | // TODO(b/145608479): ABI checks should be enabled by default. Remove this |
| 163 | // after evaluating the extra build time. |
| 164 | return false |
| 165 | } |
| 166 | } |
| 167 | return classifySourceAbiDump(ctx) != "" |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps |
| 171 | // of their dependencies would be generated. |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 172 | func sabiDepsMutator(mctx android.TopDownMutatorContext) { |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 173 | // Escape hatch to not check any ABI dump. |
| 174 | if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") { |
| 175 | return |
| 176 | } |
| 177 | // Only create ABI dump for native shared libraries and their static library dependencies. |
| 178 | if m, ok := mctx.Module().(*Module); ok && m.sabi != nil { |
| 179 | if shouldCreateSourceAbiDumpForLibrary(mctx) { |
| 180 | // Mark this module so that .sdump / .lsdump for this library can be generated. |
| 181 | m.sabi.Properties.ShouldCreateSourceAbiDump = true |
| 182 | // Mark all of its static library dependencies. |
| 183 | mctx.VisitDirectDeps(func(child android.Module) { |
| 184 | depTag := mctx.OtherModuleDependencyTag(child) |
| 185 | if libDepTag, ok := depTag.(libraryDependencyTag); ok && libDepTag.static() { |
| 186 | if c, ok := child.(*Module); ok && c.sabi != nil { |
| 187 | // Mark this module so that .sdump for this static library can be generated. |
| 188 | c.sabi.Properties.ShouldCreateSourceAbiDump = true |
| 189 | } |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 190 | } |
Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame^] | 191 | }) |
| 192 | } |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 195 | |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 196 | // Add an entry to the global list of lsdump. The list is exported to a Make variable by |
| 197 | // `cc.makeVarsProvider`. |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 198 | func addLsdumpPath(lsdumpPath string) { |
Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 199 | lsdumpPathsLock.Lock() |
| 200 | defer lsdumpPathsLock.Unlock() |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 201 | lsdumpPaths = append(lsdumpPaths, lsdumpPath) |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 202 | } |