blob: 13106859f7860516dd42568ab53302e1b9a25ce4 [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 Chen8feb4132022-12-26 15:54:36 +080029// Properties for ABI compatibility checker in Android.bp.
30type 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
55func (props *headerAbiCheckerProperties) enabled() bool {
56 return Bool(props.Enabled)
57}
58
59func (props *headerAbiCheckerProperties) explicitlyDisabled() bool {
60 return !BoolDefault(props.Enabled, true)
61}
62
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080063type SAbiProperties struct {
Yo Chiang2bbadfa2020-12-14 11:42:16 +080064 // 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 Chiang8aa4e3f2020-11-19 16:30:49 +080068
69 // Include directories that may contain ABI information exported by a library.
70 // These directories are passed to the header-abi-dumper.
Inseob Kim69378442019-06-03 19:10:47 +090071 ReexportedIncludes []string `blueprint:"mutated"`
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080072}
73
74type sabi struct {
75 Properties SAbiProperties
76}
77
Yo Chiang2bbadfa2020-12-14 11:42:16 +080078func (sabi *sabi) props() []interface{} {
79 return []interface{}{&sabi.Properties}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080080}
81
Yo Chiang2bbadfa2020-12-14 11:42:16 +080082func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +080083 // 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 Chowdhary3e231fd2017-02-08 13:45:53 -080090 return flags
91}
92
Yo Chiang2bbadfa2020-12-14 11:42:16 +080093// 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.
96func (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.
102func classifySourceAbiDump(ctx android.BaseModuleContext) string {
103 m := ctx.Module().(*Module)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800104 headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx)
105 if headerAbiChecker.explicitlyDisabled() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800106 return ""
107 }
108 // Return NDK if the library is both NDK and LLNDK.
109 if m.IsNdk(ctx.Config()) {
110 return "NDK"
111 }
Colin Cross127bb8b2020-12-16 16:46:01 -0800112 if m.isImplementationForLLNDKPublic() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800113 return "LLNDK"
114 }
Colin Cross127bb8b2020-12-16 16:46:01 -0800115 if m.UseVndk() && m.IsVndk() && !m.IsVndkPrivate() {
Ivan Lozanod7586b62021-04-01 09:49:36 -0400116 if m.IsVndkSp() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800117 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 Chen8feb4132022-12-26 15:54:36 +0800130 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 Chiang2bbadfa2020-12-14 11:42:16 +0800140 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.
147func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800148 // Only generate ABI dump for device modules.
149 if !ctx.Device() {
150 return false
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800151 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800152
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 Chiangd737d3f2020-11-30 20:00:42 +0800167 // Don't check uninstallable modules.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800168 if m.IsHideFromMake() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800169 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 Chiang2bbadfa2020-12-14 11:42:16 +0800177 // Don't create ABI dump for prebuilts.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400178 if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800179 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 Cross127bb8b2020-12-16 16:46:01 -0800193 if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800194 return false
195 }
196
Colin Crossff694a82023-12-13 15:54:49 -0800197 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
198 if apexInfo.IsForPlatform() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800199 // 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 Chiang2bbadfa2020-12-14 11:42:16 +0800203 return false
204 }
Yo Chiangd737d3f2020-11-30 20:00:42 +0800205 } else {
206 // Don't create ABI dump if this library is for APEX but isn't exported.
207 if !m.HasStubsVariants() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800208 return false
209 }
210 }
211 return classifySourceAbiDump(ctx) != ""
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800212}
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 Chowdhary3e231fd2017-02-08 13:45:53 -0800216func sabiDepsMutator(mctx android.TopDownMutatorContext) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800217 // 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 Chiang21d1c6d2021-06-07 20:22:35 +0800229 if IsStaticDepTag(depTag) || depTag == reuseObjTag {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800230 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 Chowdhary3e231fd2017-02-08 13:45:53 -0800234 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800235 })
236 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800237 }
238}
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800239
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800240// Add an entry to the global list of lsdump. The list is exported to a Make variable by
241// `cc.makeVarsProvider`.
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800242func addLsdumpPath(lsdumpPath string) {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800243 lsdumpPathsLock.Lock()
244 defer lsdumpPathsLock.Unlock()
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800245 lsdumpPaths = append(lsdumpPaths, lsdumpPath)
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800246}