blob: 9f5781fa90d437df7714b904be3ad703e873c8f7 [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 }
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800108 if !m.InProduct() && !m.InVendor() {
109 // Return NDK if the library is both NDK and LLNDK.
110 if m.IsNdk(ctx.Config()) {
111 return "NDK"
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800112 }
Hsin-Yi Chen27bafd02024-01-08 18:38:42 +0800113 if m.isImplementationForLLNDKPublic() {
114 return "LLNDK"
115 }
116 if m.library.hasStubsVariants() {
117 return "PLATFORM"
118 }
Hsin-Yi Chen8feb4132022-12-26 15:54:36 +0800119 }
120 if headerAbiChecker.enabled() {
121 if m.InProduct() {
122 return "PRODUCT"
123 }
124 if m.InVendor() {
125 return "VENDOR"
126 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800127 return "PLATFORM"
128 }
129 return ""
130}
131
132// Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
133// ctx should be wrapping a native library type module.
134func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800135 // Only generate ABI dump for device modules.
136 if !ctx.Device() {
137 return false
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800138 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800139
140 m := ctx.Module().(*Module)
141
142 // Only create ABI dump for native library module types.
143 if m.library == nil {
144 return false
145 }
146
147 // Create ABI dump for static libraries only if they are dependencies of ABI checked libraries.
148 if m.library.static() {
149 return m.sabi.shouldCreateSourceAbiDump()
150 }
151
152 // Module is shared library type.
153
Yo Chiangd737d3f2020-11-30 20:00:42 +0800154 // Don't check uninstallable modules.
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800155 if m.IsHideFromMake() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800156 return false
157 }
158
159 // Don't check ramdisk or recovery variants. Only check core, vendor or product variants.
160 if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() {
161 return false
162 }
163
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800164 // Don't create ABI dump for prebuilts.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400165 if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800166 return false
167 }
168
169 // Coverage builds have extra symbols.
170 if m.isCoverageVariant() {
171 return false
172 }
173
174 // Some sanitizer variants may have different ABI.
175 if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() {
176 return false
177 }
178
179 // Don't create ABI dump for stubs.
Colin Cross127bb8b2020-12-16 16:46:01 -0800180 if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800181 return false
182 }
183
Colin Crossff694a82023-12-13 15:54:49 -0800184 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
185 if apexInfo.IsForPlatform() {
Yo Chiangd737d3f2020-11-30 20:00:42 +0800186 // Bionic libraries that are installed to the bootstrap directory are not ABI checked.
187 // Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs,
188 // are checked.
189 if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800190 return false
191 }
Yo Chiangd737d3f2020-11-30 20:00:42 +0800192 } else {
193 // Don't create ABI dump if this library is for APEX but isn't exported.
194 if !m.HasStubsVariants() {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800195 return false
196 }
197 }
198 return classifySourceAbiDump(ctx) != ""
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800199}
200
201// Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps
202// of their dependencies would be generated.
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800203func sabiDepsMutator(mctx android.TopDownMutatorContext) {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800204 // Escape hatch to not check any ABI dump.
205 if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
206 return
207 }
208 // Only create ABI dump for native shared libraries and their static library dependencies.
209 if m, ok := mctx.Module().(*Module); ok && m.sabi != nil {
210 if shouldCreateSourceAbiDumpForLibrary(mctx) {
211 // Mark this module so that .sdump / .lsdump for this library can be generated.
212 m.sabi.Properties.ShouldCreateSourceAbiDump = true
213 // Mark all of its static library dependencies.
214 mctx.VisitDirectDeps(func(child android.Module) {
215 depTag := mctx.OtherModuleDependencyTag(child)
Yi-Yo Chiang21d1c6d2021-06-07 20:22:35 +0800216 if IsStaticDepTag(depTag) || depTag == reuseObjTag {
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800217 if c, ok := child.(*Module); ok && c.sabi != nil {
218 // Mark this module so that .sdump for this static library can be generated.
219 c.sabi.Properties.ShouldCreateSourceAbiDump = true
220 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800221 }
Yo Chiang2bbadfa2020-12-14 11:42:16 +0800222 })
223 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800224 }
225}
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800226
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800227// Add an entry to the global list of lsdump. The list is exported to a Make variable by
228// `cc.makeVarsProvider`.
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800229func addLsdumpPath(lsdumpPath string) {
Yo Chiang8aa4e3f2020-11-19 16:30:49 +0800230 lsdumpPathsLock.Lock()
231 defer lsdumpPathsLock.Unlock()
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800232 lsdumpPaths = append(lsdumpPaths, lsdumpPath)
Hsin-Yi Chen53489642019-07-31 17:10:45 +0800233}