blob: 4ca9f5c6e2d3490921751facff064a5a056bdfbc [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
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800100// Returns a slice of strings that represent the ABI dumps generated for this module.
101func classifySourceAbiDump(ctx android.BaseModuleContext) []string {
102 result := []string{}
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800103 m := ctx.Module().(*Module)
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800104 headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx)
105 if headerAbiChecker.explicitlyDisabled() {
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800106 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800107 }
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800108 if !m.InProduct() && !m.InVendor() {
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800109 if m.isImplementationForLLNDKPublic() {
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800110 result = append(result, "LLNDK")
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800111 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800112 // Return NDK if the library is both NDK and APEX.
113 // TODO(b/309880485): Split NDK and APEX ABI.
114 if m.IsNdk(ctx.Config()) {
115 result = append(result, "NDK")
116 } else if m.library.hasStubsVariants() || headerAbiChecker.enabled() {
117 result = append(result, "PLATFORM")
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800118 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800119 } else if headerAbiChecker.enabled() {
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800120 if m.InProduct() {
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800121 result = append(result, "PRODUCT")
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800122 }
123 if m.InVendor() {
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800124 result = append(result, "VENDOR")
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800125 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800126 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800127 return result
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800128}
129
130// Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
131// ctx should be wrapping a native library type module.
132func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800133 // Only generate ABI dump for device modules.
134 if !ctx.Device() {
135 return false
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800136 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800137
138 m := ctx.Module().(*Module)
139
140 // Only create ABI dump for native library module types.
141 if m.library == nil {
142 return false
143 }
144
145 // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries.
146 if m.library.static() {
147 return m.sabi.shouldCreateSourceAbiDump()
148 }
149
150 // Module is shared library type.
151
Yo Chiangd737d3f2020-11-30 20:00:42 +0800152 // Don't check uninstallable modules.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800153 if m.IsHideFromMake() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800154 return false
155 }
156
157 // Don't check ramdisk or recovery variants. Only check core, vendor or product variants.
158 if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() {
159 return false
160 }
161
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800162 // Don't create ABI dump for prebuilts.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400163 if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800164 return false
165 }
166
167 // Coverage builds have extra symbols.
168 if m.isCoverageVariant() {
169 return false
170 }
171
172 // Some sanitizer variants may have different ABI.
173 if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() {
174 return false
175 }
176
177 // Don't create ABI dump for stubs.
Colin Cross127bb8b2020-12-16 16:46:01 -0800178 if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800179 return false
180 }
181
Colin Crossff694a82023-12-13 15:54:49 -0800182 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
183 if apexInfo.IsForPlatform() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800184 // Bionic libraries that are installed to the bootstrap directory are not ABI checked.
185 // Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs,
186 // are checked.
187 if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800188 return false
189 }
Yo Chiangd737d3f2020-11-30 20:00:42 +0800190 } else {
191 // Don't create ABI dump if this library is for APEX but isn't exported.
192 if !m.HasStubsVariants() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800193 return false
194 }
195 }
Hsin-Yi Chen50884dc2024-01-05 14:41:06 +0800196 return len(classifySourceAbiDump(ctx)) > 0
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800197}
198
199// Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps
200// of their dependencies would be generated.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800201func sabiDepsMutator(mctx android.TopDownMutatorContext) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800202 // Escape hatch to not check any ABI dump.
203 if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
204 return
205 }
206 // Only create ABI dump for native shared libraries and their static library dependencies.
207 if m, ok := mctx.Module().(*Module); ok && m.sabi != nil {
208 if shouldCreateSourceAbiDumpForLibrary(mctx) {
209 // Mark this module so that .sdump / .lsdump for this library can be generated.
210 m.sabi.Properties.ShouldCreateSourceAbiDump = true
211 // Mark all of its static library dependencies.
212 mctx.VisitDirectDeps(func(child android.Module) {
213 depTag := mctx.OtherModuleDependencyTag(child)
Yi-Yo Chiang21d1c6d2021-06-07 20:22:35 +0800214 if IsStaticDepTag(depTag) || depTag == reuseObjTag {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800215 if c, ok := child.(*Module); ok && c.sabi != nil {
216 // Mark this module so that .sdump for this static library can be generated.
217 c.sabi.Properties.ShouldCreateSourceAbiDump = true
218 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800219 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800220 })
221 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800222 }
223}
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800224
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800225// Add an entry to the global list of lsdump. The list is exported to a Make variable by
226// `cc.makeVarsProvider`.
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800227func addLsdumpPath(lsdumpPath string) {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800228 lsdumpPathsLock.Lock()
229 defer lsdumpPathsLock.Unlock()
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800230 lsdumpPaths = append(lsdumpPaths, lsdumpPath)
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800231}