blob: ef43c8daf0bfc336097c894a453001a97128878e [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.
Hsin-Yi Chen5f228b02024-04-02 12:38:47 +080095 ReexportedIncludes []string `blueprint:"mutated"`
96 ReexportedSystemIncludes []string `blueprint:"mutated"`
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080097}
98
99type sabi struct {
100 Properties SAbiProperties
101}
102
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800103func (sabi *sabi) props() []interface{} {
104 return []interface{}{&sabi.Properties}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800105}
106
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800107func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800108 // Filter out flags which libTooling don't understand.
109 // This is here for legacy reasons and future-proof, in case the version of libTooling and clang
110 // diverge.
111 flags.Local.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CFlags)
112 flags.Global.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CFlags)
113 flags.Local.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CppFlags)
114 flags.Global.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CppFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800115 return flags
116}
117
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800118// Returns true if ABI dump should be created for this library, either because library is ABI
119// checked or is depended on by an ABI checked library.
120// Could be called as a nil receiver.
121func (sabi *sabi) shouldCreateSourceAbiDump() bool {
122 return sabi != nil && sabi.Properties.ShouldCreateSourceAbiDump
123}
124
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800125// Returns a slice of strings that represent the ABI dumps generated for this module.
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800126func classifySourceAbiDump(ctx android.BaseModuleContext) []lsdumpTag {
127 result := []lsdumpTag{}
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800128 m := ctx.Module().(*Module)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800129 headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx)
130 if headerAbiChecker.explicitlyDisabled() {
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800131 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800132 }
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800133 if !m.InProduct() && !m.InVendor() {
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800134 if m.isImplementationForLLNDKPublic() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800135 result = append(result, llndkLsdumpTag)
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800136 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800137 // Return NDK if the library is both NDK and APEX.
138 // TODO(b/309880485): Split NDK and APEX ABI.
139 if m.IsNdk(ctx.Config()) {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800140 result = append(result, ndkLsdumpTag)
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800141 } else if m.library.hasStubsVariants() || headerAbiChecker.enabled() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800142 result = append(result, platformLsdumpTag)
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800143 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800144 } else if headerAbiChecker.enabled() {
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800145 if m.InProduct() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800146 result = append(result, productLsdumpTag)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800147 }
148 if m.InVendor() {
Hsin-Yi Chen362c1882024-02-06 15:43:17 +0800149 result = append(result, vendorLsdumpTag)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800150 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800151 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800152 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800153}
154
155// Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
156// ctx should be wrapping a native library type module.
157func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800158 // Only generate ABI dump for device modules.
159 if !ctx.Device() {
160 return false
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800161 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800162
163 m := ctx.Module().(*Module)
164
165 // Only create ABI dump for native library module types.
166 if m.library == nil {
167 return false
168 }
169
170 // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries.
171 if m.library.static() {
172 return m.sabi.shouldCreateSourceAbiDump()
173 }
174
175 // Module is shared library type.
176
Yo Chiangd737d3f2020-11-30 20:00:42 +0800177 // Don't check uninstallable modules.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800178 if m.IsHideFromMake() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800179 return false
180 }
181
182 // Don't check ramdisk or recovery variants. Only check core, vendor or product variants.
183 if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() {
184 return false
185 }
186
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800187 // Don't create ABI dump for prebuilts.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400188 if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800189 return false
190 }
191
192 // Coverage builds have extra symbols.
193 if m.isCoverageVariant() {
194 return false
195 }
196
197 // Some sanitizer variants may have different ABI.
198 if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() {
199 return false
200 }
201
202 // Don't create ABI dump for stubs.
Colin Cross127bb8b2020-12-16 16:46:01 -0800203 if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800204 return false
205 }
206
Colin Crossff694a82023-12-13 15:54:49 -0800207 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
208 if apexInfo.IsForPlatform() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800209 // Bionic libraries that are installed to the bootstrap directory are not ABI checked.
210 // Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs,
211 // are checked.
212 if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800213 return false
214 }
Yo Chiangd737d3f2020-11-30 20:00:42 +0800215 } else {
216 // Don't create ABI dump if this library is for APEX but isn't exported.
217 if !m.HasStubsVariants() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800218 return false
219 }
220 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800221 return len(classifySourceAbiDump(ctx)) > 0
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800222}
223
224// Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps
225// of their dependencies would be generated.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800226func sabiDepsMutator(mctx android.TopDownMutatorContext) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800227 // Escape hatch to not check any ABI dump.
228 if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
229 return
230 }
231 // Only create ABI dump for native shared libraries and their static library dependencies.
232 if m, ok := mctx.Module().(*Module); ok && m.sabi != nil {
233 if shouldCreateSourceAbiDumpForLibrary(mctx) {
234 // Mark this module so that .sdump / .lsdump for this library can be generated.
235 m.sabi.Properties.ShouldCreateSourceAbiDump = true
236 // Mark all of its static library dependencies.
237 mctx.VisitDirectDeps(func(child android.Module) {
238 depTag := mctx.OtherModuleDependencyTag(child)
Yi-Yo Chiang21d1c6d2021-06-07 20:22:35 +0800239 if IsStaticDepTag(depTag) || depTag == reuseObjTag {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800240 if c, ok := child.(*Module); ok && c.sabi != nil {
241 // Mark this module so that .sdump for this static library can be generated.
242 c.sabi.Properties.ShouldCreateSourceAbiDump = true
243 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800244 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800245 })
246 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800247 }
248}
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800249
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800250// Add an entry to the global list of lsdump. The list is exported to a Make variable by
251// `cc.makeVarsProvider`.
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800252func addLsdumpPath(lsdumpPath string) {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800253 lsdumpPathsLock.Lock()
254 defer lsdumpPathsLock.Unlock()
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800255 lsdumpPaths = append(lsdumpPaths, lsdumpPath)
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800256}