| 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 |  | 
| Hsin-Yi Chen | 8feb413 | 2022-12-26 15:54:36 +0800 | [diff] [blame] | 29 | // Properties for ABI compatibility checker in Android.bp. | 
|  | 30 | type headerAbiCheckerProperties struct { | 
|  | 31 | // Enable ABI checks (even if this is not an LLNDK/VNDK lib) | 
|  | 32 | Enabled *bool | 
|  | 33 |  | 
|  | 34 | // Path to a symbol file that specifies the symbols to be included in the generated | 
|  | 35 | // ABI dump file | 
|  | 36 | Symbol_file *string `android:"path"` | 
|  | 37 |  | 
|  | 38 | // Symbol versions that should be ignored from the symbol file | 
|  | 39 | Exclude_symbol_versions []string | 
|  | 40 |  | 
|  | 41 | // Symbol tags that should be ignored from the symbol file | 
|  | 42 | Exclude_symbol_tags []string | 
|  | 43 |  | 
|  | 44 | // Run checks on all APIs (in addition to the ones referred by | 
|  | 45 | // one of exported ELF symbols.) | 
|  | 46 | Check_all_apis *bool | 
|  | 47 |  | 
|  | 48 | // Extra flags passed to header-abi-diff | 
|  | 49 | Diff_flags []string | 
|  | 50 |  | 
|  | 51 | // Opt-in reference dump directories | 
|  | 52 | Ref_dump_dirs []string | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | func (props *headerAbiCheckerProperties) enabled() bool { | 
|  | 56 | return Bool(props.Enabled) | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | func (props *headerAbiCheckerProperties) explicitlyDisabled() bool { | 
|  | 60 | return !BoolDefault(props.Enabled, true) | 
|  | 61 | } | 
|  | 62 |  | 
| Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 63 | type SAbiProperties struct { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 64 | // Whether ABI dump should be created for this module. | 
|  | 65 | // Set by `sabiDepsMutator` if this module is a shared library that needs ABI check, or a static | 
|  | 66 | // library that is depended on by an ABI checked library. | 
|  | 67 | ShouldCreateSourceAbiDump bool `blueprint:"mutated"` | 
| Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 68 |  | 
|  | 69 | // Include directories that may contain ABI information exported by a library. | 
|  | 70 | // These directories are passed to the header-abi-dumper. | 
| Inseob Kim | 6937844 | 2019-06-03 19:10:47 +0900 | [diff] [blame] | 71 | ReexportedIncludes []string `blueprint:"mutated"` | 
| Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | type sabi struct { | 
|  | 75 | Properties SAbiProperties | 
|  | 76 | } | 
|  | 77 |  | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 78 | func (sabi *sabi) props() []interface{} { | 
|  | 79 | return []interface{}{&sabi.Properties} | 
| Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 82 | func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags { | 
| Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 83 | // Filter out flags which libTooling don't understand. | 
|  | 84 | // This is here for legacy reasons and future-proof, in case the version of libTooling and clang | 
|  | 85 | // diverge. | 
|  | 86 | flags.Local.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CFlags) | 
|  | 87 | flags.Global.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CFlags) | 
|  | 88 | flags.Local.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CppFlags) | 
|  | 89 | flags.Global.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CppFlags) | 
| Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 90 | return flags | 
|  | 91 | } | 
|  | 92 |  | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 93 | // Returns true if ABI dump should be created for this library, either because library is ABI | 
|  | 94 | // checked or is depended on by an ABI checked library. | 
|  | 95 | // Could be called as a nil receiver. | 
|  | 96 | func (sabi *sabi) shouldCreateSourceAbiDump() bool { | 
|  | 97 | return sabi != nil && sabi.Properties.ShouldCreateSourceAbiDump | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | // Returns a string that represents the class of the ABI dump. | 
|  | 101 | // Returns an empty string if ABI check is disabled for this library. | 
|  | 102 | func classifySourceAbiDump(ctx android.BaseModuleContext) string { | 
|  | 103 | m := ctx.Module().(*Module) | 
| Hsin-Yi Chen | 8feb413 | 2022-12-26 15:54:36 +0800 | [diff] [blame] | 104 | headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx) | 
|  | 105 | if headerAbiChecker.explicitlyDisabled() { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 106 | return "" | 
|  | 107 | } | 
|  | 108 | // Return NDK if the library is both NDK and LLNDK. | 
|  | 109 | if m.IsNdk(ctx.Config()) { | 
|  | 110 | return "NDK" | 
|  | 111 | } | 
| Colin Cross | 127bb8b | 2020-12-16 16:46:01 -0800 | [diff] [blame] | 112 | if m.isImplementationForLLNDKPublic() { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 113 | return "LLNDK" | 
|  | 114 | } | 
| Colin Cross | 127bb8b | 2020-12-16 16:46:01 -0800 | [diff] [blame] | 115 | if m.UseVndk() && m.IsVndk() && !m.IsVndkPrivate() { | 
| Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 116 | if m.IsVndkSp() { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 117 | if m.IsVndkExt() { | 
|  | 118 | return "VNDK-SP-ext" | 
|  | 119 | } else { | 
|  | 120 | return "VNDK-SP" | 
|  | 121 | } | 
|  | 122 | } else { | 
|  | 123 | if m.IsVndkExt() { | 
|  | 124 | return "VNDK-ext" | 
|  | 125 | } else { | 
|  | 126 | return "VNDK-core" | 
|  | 127 | } | 
|  | 128 | } | 
|  | 129 | } | 
| Hsin-Yi Chen | 8feb413 | 2022-12-26 15:54:36 +0800 | [diff] [blame] | 130 | if m.library.hasStubsVariants() && !m.InProduct() && !m.InVendor() { | 
|  | 131 | return "PLATFORM" | 
|  | 132 | } | 
|  | 133 | if headerAbiChecker.enabled() { | 
|  | 134 | if m.InProduct() { | 
|  | 135 | return "PRODUCT" | 
|  | 136 | } | 
|  | 137 | if m.InVendor() { | 
|  | 138 | return "VENDOR" | 
|  | 139 | } | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 140 | return "PLATFORM" | 
|  | 141 | } | 
|  | 142 | return "" | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | // Called from sabiDepsMutator to check whether ABI dumps should be created for this module. | 
|  | 146 | // ctx should be wrapping a native library type module. | 
|  | 147 | func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 148 | // Only generate ABI dump for device modules. | 
|  | 149 | if !ctx.Device() { | 
|  | 150 | return false | 
| Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 151 | } | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 152 |  | 
|  | 153 | m := ctx.Module().(*Module) | 
|  | 154 |  | 
|  | 155 | // Only create ABI dump for native library module types. | 
|  | 156 | if m.library == nil { | 
|  | 157 | return false | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries. | 
|  | 161 | if m.library.static() { | 
|  | 162 | return m.sabi.shouldCreateSourceAbiDump() | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | // Module is shared library type. | 
|  | 166 |  | 
| Yo Chiang | d737d3f | 2020-11-30 20:00:42 +0800 | [diff] [blame] | 167 | // Don't check uninstallable modules. | 
| Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 168 | if m.IsHideFromMake() { | 
| Yo Chiang | d737d3f | 2020-11-30 20:00:42 +0800 | [diff] [blame] | 169 | return false | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | // Don't check ramdisk or recovery variants. Only check core, vendor or product variants. | 
|  | 173 | if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() { | 
|  | 174 | return false | 
|  | 175 | } | 
|  | 176 |  | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 177 | // Don't create ABI dump for prebuilts. | 
| Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 178 | if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 179 | return false | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | // Coverage builds have extra symbols. | 
|  | 183 | if m.isCoverageVariant() { | 
|  | 184 | return false | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | // Some sanitizer variants may have different ABI. | 
|  | 188 | if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() { | 
|  | 189 | return false | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | // Don't create ABI dump for stubs. | 
| Colin Cross | 127bb8b | 2020-12-16 16:46:01 -0800 | [diff] [blame] | 193 | if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 194 | return false | 
|  | 195 | } | 
|  | 196 |  | 
| Yo Chiang | d737d3f | 2020-11-30 20:00:42 +0800 | [diff] [blame] | 197 | isPlatformVariant := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() | 
|  | 198 | if isPlatformVariant { | 
|  | 199 | // Bionic libraries that are installed to the bootstrap directory are not ABI checked. | 
|  | 200 | // Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs, | 
|  | 201 | // are checked. | 
|  | 202 | if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 203 | return false | 
|  | 204 | } | 
| Yo Chiang | d737d3f | 2020-11-30 20:00:42 +0800 | [diff] [blame] | 205 | } else { | 
|  | 206 | // Don't create ABI dump if this library is for APEX but isn't exported. | 
|  | 207 | if !m.HasStubsVariants() { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 208 | return false | 
|  | 209 | } | 
|  | 210 | } | 
|  | 211 | return classifySourceAbiDump(ctx) != "" | 
| Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
|  | 214 | // Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps | 
|  | 215 | // of their dependencies would be generated. | 
| Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 216 | func sabiDepsMutator(mctx android.TopDownMutatorContext) { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 217 | // Escape hatch to not check any ABI dump. | 
|  | 218 | if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") { | 
|  | 219 | return | 
|  | 220 | } | 
|  | 221 | // Only create ABI dump for native shared libraries and their static library dependencies. | 
|  | 222 | if m, ok := mctx.Module().(*Module); ok && m.sabi != nil { | 
|  | 223 | if shouldCreateSourceAbiDumpForLibrary(mctx) { | 
|  | 224 | // Mark this module so that .sdump / .lsdump for this library can be generated. | 
|  | 225 | m.sabi.Properties.ShouldCreateSourceAbiDump = true | 
|  | 226 | // Mark all of its static library dependencies. | 
|  | 227 | mctx.VisitDirectDeps(func(child android.Module) { | 
|  | 228 | depTag := mctx.OtherModuleDependencyTag(child) | 
| Yi-Yo Chiang | 21d1c6d | 2021-06-07 20:22:35 +0800 | [diff] [blame] | 229 | if IsStaticDepTag(depTag) || depTag == reuseObjTag { | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 230 | if c, ok := child.(*Module); ok && c.sabi != nil { | 
|  | 231 | // Mark this module so that .sdump for this static library can be generated. | 
|  | 232 | c.sabi.Properties.ShouldCreateSourceAbiDump = true | 
|  | 233 | } | 
| Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 234 | } | 
| Yo Chiang | 2bbadfa | 2020-12-14 11:42:16 +0800 | [diff] [blame] | 235 | }) | 
|  | 236 | } | 
| Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 237 | } | 
|  | 238 | } | 
| Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 239 |  | 
| Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 240 | // Add an entry to the global list of lsdump. The list is exported to a Make variable by | 
|  | 241 | // `cc.makeVarsProvider`. | 
| Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 242 | func addLsdumpPath(lsdumpPath string) { | 
| Yo Chiang | 8aa4e3f | 2020-11-19 16:30:49 +0800 | [diff] [blame] | 243 | lsdumpPathsLock.Lock() | 
|  | 244 | defer lsdumpPathsLock.Unlock() | 
| Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 245 | lsdumpPaths = append(lsdumpPaths, lsdumpPath) | 
| Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 246 | } |