blob: af26726345df37c4ab3e68b7f430e4e3b8571f76 [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 (
32 llndkLsdumpTag lsdumpTag = "LLNDK"
33 ndkLsdumpTag lsdumpTag = "NDK"
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.
40func (tag *lsdumpTag) dirName() string {
41 switch *tag {
42 case ndkLsdumpTag:
43 return "ndk"
44 case llndkLsdumpTag:
45 return "vndk"
46 case platformLsdumpTag:
47 return "platform"
48 default:
49 return ""
50 }
51}
52
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +080053// Properties for ABI compatibility checker in Android.bp.
54type headerAbiCheckerProperties struct {
55 // Enable ABI checks (even if this is not an LLNDK/VNDK lib)
56 Enabled *bool
57
58 // Path to a symbol file that specifies the symbols to be included in the generated
59 // ABI dump file
60 Symbol_file *string `android:"path"`
61
62 // Symbol versions that should be ignored from the symbol file
63 Exclude_symbol_versions []string
64
65 // Symbol tags that should be ignored from the symbol file
66 Exclude_symbol_tags []string
67
68 // Run checks on all APIs (in addition to the ones referred by
69 // one of exported ELF symbols.)
70 Check_all_apis *bool
71
72 // Extra flags passed to header-abi-diff
73 Diff_flags []string
74
75 // Opt-in reference dump directories
76 Ref_dump_dirs []string
77}
78
79func (props *headerAbiCheckerProperties) enabled() bool {
80 return Bool(props.Enabled)
81}
82
83func (props *headerAbiCheckerProperties) explicitlyDisabled() bool {
84 return !BoolDefault(props.Enabled, true)
85}
86
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080087type SAbiProperties struct {
Yo Chiang2bbadfa2020-12-14 11:42:16 +080088 // Whether ABI dump should be created for this module.
89 // Set by `sabiDepsMutator` if this module is a shared library that needs ABI check, or a static
90 // library that is depended on by an ABI checked library.
91 ShouldCreateSourceAbiDump bool `blueprint:"mutated"`
Yo Chiang8aa4e3f2020-11-19 16:30:49 +080092
93 // Include directories that may contain ABI information exported by a library.
94 // These directories are passed to the header-abi-dumper.
Inseob Kim69378442019-06-03 19:10:47 +090095 ReexportedIncludes []string `blueprint:"mutated"`
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080096}
97
98type sabi struct {
99 Properties SAbiProperties
100}
101
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800102func (sabi *sabi) props() []interface{} {
103 return []interface{}{&sabi.Properties}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800104}
105
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800106func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800107 // Filter out flags which libTooling don't understand.
108 // This is here for legacy reasons and future-proof, in case the version of libTooling and clang
109 // diverge.
110 flags.Local.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CFlags)
111 flags.Global.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CFlags)
112 flags.Local.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CppFlags)
113 flags.Global.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CppFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800114 return flags
115}
116
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800117// Returns true if ABI dump should be created for this library, either because library is ABI
118// checked or is depended on by an ABI checked library.
119// Could be called as a nil receiver.
120func (sabi *sabi) shouldCreateSourceAbiDump() bool {
121 return sabi != nil && sabi.Properties.ShouldCreateSourceAbiDump
122}
123
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800124// Returns a slice of strings that represent the ABI dumps generated for this module.
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800125func classifySourceAbiDump(ctx android.BaseModuleContext) []lsdumpTag {
126 result := []lsdumpTag{}
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800127 m := ctx.Module().(*Module)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800128 headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx)
129 if headerAbiChecker.explicitlyDisabled() {
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800130 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800131 }
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800132 if !m.InProduct() && !m.InVendor() {
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800133 if m.isImplementationForLLNDKPublic() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800134 result = append(result, llndkLsdumpTag)
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800135 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800136 // Return NDK if the library is both NDK and APEX.
137 // TODO(b/309880485): Split NDK and APEX ABI.
138 if m.IsNdk(ctx.Config()) {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800139 result = append(result, ndkLsdumpTag)
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800140 } else if m.library.hasStubsVariants() || headerAbiChecker.enabled() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800141 result = append(result, platformLsdumpTag)
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800142 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800143 } else if headerAbiChecker.enabled() {
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800144 if m.InProduct() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800145 result = append(result, productLsdumpTag)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800146 }
147 if m.InVendor() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800148 result = append(result, vendorLsdumpTag)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800149 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800150 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800151 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800152}
153
154// Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
155// ctx should be wrapping a native library type module.
156func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800157 // Only generate ABI dump for device modules.
158 if !ctx.Device() {
159 return false
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800160 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800161
162 m := ctx.Module().(*Module)
163
164 // Only create ABI dump for native library module types.
165 if m.library == nil {
166 return false
167 }
168
169 // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries.
170 if m.library.static() {
171 return m.sabi.shouldCreateSourceAbiDump()
172 }
173
174 // Module is shared library type.
175
Yo Chiangd737d3f2020-11-30 20:00:42 +0800176 // Don't check uninstallable modules.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800177 if m.IsHideFromMake() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800178 return false
179 }
180
181 // Don't check ramdisk or recovery variants. Only check core, vendor or product variants.
182 if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() {
183 return false
184 }
185
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800186 // Don't create ABI dump for prebuilts.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400187 if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800188 return false
189 }
190
191 // Coverage builds have extra symbols.
192 if m.isCoverageVariant() {
193 return false
194 }
195
196 // Some sanitizer variants may have different ABI.
197 if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() {
198 return false
199 }
200
201 // Don't create ABI dump for stubs.
Colin Cross127bb8b2020-12-16 16:46:01 -0800202 if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800203 return false
204 }
205
Colin Crossff694a82023-12-13 15:54:49 -0800206 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
207 if apexInfo.IsForPlatform() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800208 // Bionic libraries that are installed to the bootstrap directory are not ABI checked.
209 // Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs,
210 // are checked.
211 if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800212 return false
213 }
Yo Chiangd737d3f2020-11-30 20:00:42 +0800214 } else {
215 // Don't create ABI dump if this library is for APEX but isn't exported.
216 if !m.HasStubsVariants() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800217 return false
218 }
219 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800220 return len(classifySourceAbiDump(ctx)) > 0
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800221}
222
223// Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps
224// of their dependencies would be generated.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800225func sabiDepsMutator(mctx android.TopDownMutatorContext) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800226 // Escape hatch to not check any ABI dump.
227 if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
228 return
229 }
230 // Only create ABI dump for native shared libraries and their static library dependencies.
231 if m, ok := mctx.Module().(*Module); ok && m.sabi != nil {
232 if shouldCreateSourceAbiDumpForLibrary(mctx) {
233 // Mark this module so that .sdump / .lsdump for this library can be generated.
234 m.sabi.Properties.ShouldCreateSourceAbiDump = true
235 // Mark all of its static library dependencies.
236 mctx.VisitDirectDeps(func(child android.Module) {
237 depTag := mctx.OtherModuleDependencyTag(child)
Yi-Yo Chiang21d1c6d2021-06-07 20:22:35 +0800238 if IsStaticDepTag(depTag) || depTag == reuseObjTag {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800239 if c, ok := child.(*Module); ok && c.sabi != nil {
240 // Mark this module so that .sdump for this static library can be generated.
241 c.sabi.Properties.ShouldCreateSourceAbiDump = true
242 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800243 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800244 })
245 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800246 }
247}
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800248
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800249// Add an entry to the global list of lsdump. The list is exported to a Make variable by
250// `cc.makeVarsProvider`.
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800251func addLsdumpPath(lsdumpPath string) {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800252 lsdumpPathsLock.Lock()
253 defer lsdumpPathsLock.Unlock()
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800254 lsdumpPaths = append(lsdumpPaths, lsdumpPath)
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800255}