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