blob: cd9bf6393205b693e40fea3d6fffd287eb5c76b0 [file] [log] [blame]
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001// 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
15package cc
16
17import (
Jayant Chowdharydcd33b62018-02-23 16:43:23 -080018 "sync"
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -070019
Colin Cross36242852017-06-23 15:06:31 -070020 "android/soong/android"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080021 "android/soong/cc/config"
22)
23
Jayant Chowdharydcd33b62018-02-23 16:43:23 -080024var (
Yo Chiang8aa4e3f2020-11-19 16:30:49 +080025 lsdumpPaths []string
26 lsdumpPathsLock sync.Mutex
Jayant Chowdharydcd33b62018-02-23 16:43:23 -080027)
28
Hsin-Yi Chen362c1882024-02-06 15:43:17 +080029type lsdumpTag string
30
31const (
Hsin-Yi Chen98da0212024-04-14 19:08:17 +080032 apexLsdumpTag lsdumpTag = "APEX"
Hsin-Yi Chen362c1882024-02-06 15:43:17 +080033 llndkLsdumpTag lsdumpTag = "LLNDK"
34 ndkLsdumpTag lsdumpTag = "NDK"
35 platformLsdumpTag lsdumpTag = "PLATFORM"
36 productLsdumpTag lsdumpTag = "PRODUCT"
37 vendorLsdumpTag lsdumpTag = "VENDOR"
38)
39
40// Return the prebuilt ABI dump directory for a tag; an empty string for an opt-in dump.
41func (tag *lsdumpTag) dirName() string {
42 switch *tag {
Hsin-Yi Chen98da0212024-04-14 19:08:17 +080043 case apexLsdumpTag:
44 return "platform"
Hsin-Yi Chen362c1882024-02-06 15:43:17 +080045 case ndkLsdumpTag:
46 return "ndk"
47 case llndkLsdumpTag:
48 return "vndk"
49 case platformLsdumpTag:
50 return "platform"
51 default:
52 return ""
53 }
54}
55
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +080056// Properties for ABI compatibility checker in Android.bp.
57type headerAbiCheckerProperties struct {
58 // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
59 Enabled *bool
60
61 // Path to a symbol file that specifies the symbols to be included in the generated
62 // ABI dump file
63 Symbol_file *string `android:"path"`
64
65 // Symbol versions that should be ignored from the symbol file
66 Exclude_symbol_versions []string
67
68 // Symbol tags that should be ignored from the symbol file
69 Exclude_symbol_tags []string
70
71 // Run checks on all APIs (in addition to the ones referred by
72 // one of exported ELF symbols.)
73 Check_all_apis *bool
74
75 // Extra flags passed to header-abi-diff
76 Diff_flags []string
77
78 // Opt-in reference dump directories
79 Ref_dump_dirs []string
80}
81
82func (props *headerAbiCheckerProperties) enabled() bool {
83 return Bool(props.Enabled)
84}
85
86func (props *headerAbiCheckerProperties) explicitlyDisabled() bool {
87 return !BoolDefault(props.Enabled, true)
88}
89
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080090type SAbiProperties struct {
Yo Chiang2bbadfa2020-12-14 11:42:16 +080091 // Whether ABI dump should be created for this module.
92 // Set by `sabiDepsMutator` if this module is a shared library that needs ABI check, or a static
93 // library that is depended on by an ABI checked library.
94 ShouldCreateSourceAbiDump bool `blueprint:"mutated"`
Yo Chiang8aa4e3f2020-11-19 16:30:49 +080095
96 // Include directories that may contain ABI information exported by a library.
97 // These directories are passed to the header-abi-dumper.
Hsin-Yi Chen5f228b02024-04-02 12:38:47 +080098 ReexportedIncludes []string `blueprint:"mutated"`
99 ReexportedSystemIncludes []string `blueprint:"mutated"`
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800100}
101
102type sabi struct {
103 Properties SAbiProperties
104}
105
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800106func (sabi *sabi) props() []interface{} {
107 return []interface{}{&sabi.Properties}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800108}
109
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800110func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800111 // Filter out flags which libTooling don't understand.
112 // This is here for legacy reasons and future-proof, in case the version of libTooling and clang
113 // diverge.
114 flags.Local.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CFlags)
115 flags.Global.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CFlags)
116 flags.Local.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CppFlags)
117 flags.Global.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CppFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800118 return flags
119}
120
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800121// Returns true if ABI dump should be created for this library, either because library is ABI
122// checked or is depended on by an ABI checked library.
123// Could be called as a nil receiver.
124func (sabi *sabi) shouldCreateSourceAbiDump() bool {
125 return sabi != nil && sabi.Properties.ShouldCreateSourceAbiDump
126}
127
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800128// Returns a slice of strings that represent the ABI dumps generated for this module.
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800129func classifySourceAbiDump(ctx android.BaseModuleContext) []lsdumpTag {
130 result := []lsdumpTag{}
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800131 m := ctx.Module().(*Module)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800132 headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx)
133 if headerAbiChecker.explicitlyDisabled() {
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800134 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800135 }
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800136 if !m.InProduct() && !m.InVendor() {
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800137 if m.isImplementationForLLNDKPublic() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800138 result = append(result, llndkLsdumpTag)
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800139 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800140 if m.IsNdk(ctx.Config()) {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800141 result = append(result, ndkLsdumpTag)
Hsin-Yi Chen98da0212024-04-14 19:08:17 +0800142 }
143 // APEX and opt-in platform dumps are placed in the same directory.
144 if m.library.hasStubsVariants() {
145 result = append(result, apexLsdumpTag)
146 } else if headerAbiChecker.enabled() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800147 result = append(result, platformLsdumpTag)
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800148 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800149 } else if headerAbiChecker.enabled() {
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800150 if m.InProduct() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800151 result = append(result, productLsdumpTag)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800152 }
153 if m.InVendor() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800154 result = append(result, vendorLsdumpTag)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800155 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800156 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800157 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800158}
159
160// Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
161// ctx should be wrapping a native library type module.
162func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800163 // Only generate ABI dump for device modules.
164 if !ctx.Device() {
165 return false
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800166 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800167
168 m := ctx.Module().(*Module)
169
170 // Only create ABI dump for native library module types.
171 if m.library == nil {
172 return false
173 }
174
175 // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries.
176 if m.library.static() {
177 return m.sabi.shouldCreateSourceAbiDump()
178 }
179
180 // Module is shared library type.
181
Yo Chiangd737d3f2020-11-30 20:00:42 +0800182 // Don't check uninstallable modules.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800183 if m.IsHideFromMake() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800184 return false
185 }
186
187 // Don't check ramdisk or recovery variants. Only check core, vendor or product variants.
188 if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() {
189 return false
190 }
191
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800192 // Don't create ABI dump for prebuilts.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400193 if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800194 return false
195 }
196
197 // Coverage builds have extra symbols.
198 if m.isCoverageVariant() {
199 return false
200 }
201
202 // Some sanitizer variants may have different ABI.
203 if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() {
204 return false
205 }
206
207 // Don't create ABI dump for stubs.
Colin Cross127bb8b2020-12-16 16:46:01 -0800208 if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800209 return false
210 }
211
Colin Crossff694a82023-12-13 15:54:49 -0800212 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
213 if apexInfo.IsForPlatform() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800214 // Bionic libraries that are installed to the bootstrap directory are not ABI checked.
215 // Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs,
216 // are checked.
217 if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800218 return false
219 }
Yo Chiangd737d3f2020-11-30 20:00:42 +0800220 } else {
221 // Don't create ABI dump if this library is for APEX but isn't exported.
222 if !m.HasStubsVariants() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800223 return false
224 }
225 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800226 return len(classifySourceAbiDump(ctx)) > 0
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800227}
228
229// Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps
230// of their dependencies would be generated.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800231func sabiDepsMutator(mctx android.TopDownMutatorContext) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800232 // Escape hatch to not check any ABI dump.
233 if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
234 return
235 }
236 // Only create ABI dump for native shared libraries and their static library dependencies.
237 if m, ok := mctx.Module().(*Module); ok && m.sabi != nil {
238 if shouldCreateSourceAbiDumpForLibrary(mctx) {
239 // Mark this module so that .sdump / .lsdump for this library can be generated.
240 m.sabi.Properties.ShouldCreateSourceAbiDump = true
241 // Mark all of its static library dependencies.
242 mctx.VisitDirectDeps(func(child android.Module) {
243 depTag := mctx.OtherModuleDependencyTag(child)
Yi-Yo Chiang21d1c6d2021-06-07 20:22:35 +0800244 if IsStaticDepTag(depTag) || depTag == reuseObjTag {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800245 if c, ok := child.(*Module); ok && c.sabi != nil {
246 // Mark this module so that .sdump for this static library can be generated.
247 c.sabi.Properties.ShouldCreateSourceAbiDump = true
248 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800249 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800250 })
251 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800252 }
253}
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800254
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800255// Add an entry to the global list of lsdump. The list is exported to a Make variable by
256// `cc.makeVarsProvider`.
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800257func addLsdumpPath(lsdumpPath string) {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800258 lsdumpPathsLock.Lock()
259 defer lsdumpPathsLock.Unlock()
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800260 lsdumpPaths = append(lsdumpPaths, lsdumpPath)
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800261}