1// 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 (
18	"sync"
19
20	"android/soong/android"
21	"android/soong/cc/config"
22)
23
24var (
25	lsdumpPaths     []string
26	lsdumpPathsLock sync.Mutex
27)
28
29type lsdumpTag string
30
31const (
32	apexLsdumpTag     lsdumpTag = "APEX"
33	llndkLsdumpTag    lsdumpTag = "LLNDK"
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 apexLsdumpTag:
43		return "platform"
44	case llndkLsdumpTag:
45		return "vndk"
46	default:
47		return ""
48	}
49}
50
51// Properties for ABI compatibility checker in Android.bp.
52type headerAbiCheckerProperties struct {
53	// Enable ABI checks (even if this is not an LLNDK/VNDK lib)
54	Enabled *bool
55
56	// Path to a symbol file that specifies the symbols to be included in the generated
57	// ABI dump file
58	Symbol_file *string `android:"path"`
59
60	// Symbol versions that should be ignored from the symbol file
61	Exclude_symbol_versions []string
62
63	// Symbol tags that should be ignored from the symbol file
64	Exclude_symbol_tags []string
65
66	// Run checks on all APIs (in addition to the ones referred by
67	// one of exported ELF symbols.)
68	Check_all_apis *bool
69
70	// Extra flags passed to header-abi-diff
71	Diff_flags []string
72
73	// Opt-in reference dump directories
74	Ref_dump_dirs []string
75}
76
77func (props *headerAbiCheckerProperties) enabled() bool {
78	return Bool(props.Enabled)
79}
80
81func (props *headerAbiCheckerProperties) explicitlyDisabled() bool {
82	return !BoolDefault(props.Enabled, true)
83}
84
85type SAbiProperties struct {
86	// Whether ABI dump should be created for this module.
87	// Set by `sabiDepsMutator` if this module is a shared library that needs ABI check, or a static
88	// library that is depended on by an ABI checked library.
89	ShouldCreateSourceAbiDump bool `blueprint:"mutated"`
90
91	// Include directories that may contain ABI information exported by a library.
92	// These directories are passed to the header-abi-dumper.
93	ReexportedIncludes       []string `blueprint:"mutated"`
94	ReexportedSystemIncludes []string `blueprint:"mutated"`
95}
96
97type sabi struct {
98	Properties SAbiProperties
99}
100
101func (sabi *sabi) props() []interface{} {
102	return []interface{}{&sabi.Properties}
103}
104
105func (sabi *sabi) flags(ctx ModuleContext, flags Flags) Flags {
106	// Filter out flags which libTooling don't understand.
107	// This is here for legacy reasons and future-proof, in case the version of libTooling and clang
108	// diverge.
109	flags.Local.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CFlags)
110	flags.Global.ToolingCFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CFlags)
111	flags.Local.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Local.CppFlags)
112	flags.Global.ToolingCppFlags = config.ClangLibToolingFilterUnknownCflags(flags.Global.CppFlags)
113	return flags
114}
115
116// Returns true if ABI dump should be created for this library, either because library is ABI
117// checked or is depended on by an ABI checked library.
118// Could be called as a nil receiver.
119func (sabi *sabi) shouldCreateSourceAbiDump() bool {
120	return sabi != nil && sabi.Properties.ShouldCreateSourceAbiDump
121}
122
123// Returns a slice of strings that represent the ABI dumps generated for this module.
124func classifySourceAbiDump(ctx android.BaseModuleContext) []lsdumpTag {
125	result := []lsdumpTag{}
126	m := ctx.Module().(*Module)
127	headerAbiChecker := m.library.getHeaderAbiCheckerProperties(ctx)
128	if headerAbiChecker.explicitlyDisabled() {
129		return result
130	}
131	if !m.InProduct() && !m.InVendor() {
132		if m.isImplementationForLLNDKPublic() {
133			result = append(result, llndkLsdumpTag)
134		}
135		if m.library.hasStubsVariants() {
136			result = append(result, apexLsdumpTag)
137		}
138		if headerAbiChecker.enabled() {
139			result = append(result, platformLsdumpTag)
140		}
141	} else if headerAbiChecker.enabled() {
142		if m.InProduct() {
143			result = append(result, productLsdumpTag)
144		}
145		if m.InVendor() {
146			result = append(result, vendorLsdumpTag)
147		}
148	}
149	return result
150}
151
152// Called from sabiDepsMutator to check whether ABI dumps should be created for this module.
153// ctx should be wrapping a native library type module.
154func shouldCreateSourceAbiDumpForLibrary(ctx android.BaseModuleContext) bool {
155	// Only generate ABI dump for device modules.
156	if !ctx.Device() {
157		return false
158	}
159
160	m := ctx.Module().(*Module)
161
162	// Only create ABI dump for native library module types.
163	if m.library == nil {
164		return false
165	}
166
167	// Create ABI dump for static libraries only if they are dependencies of ABI checked libraries.
168	if m.library.static() {
169		return m.sabi.shouldCreateSourceAbiDump()
170	}
171
172	// Module is shared library type.
173
174	// Don't check uninstallable modules.
175	if m.IsHideFromMake() {
176		return false
177	}
178
179	// Don't check ramdisk or recovery variants. Only check core, vendor or product variants.
180	if m.InRamdisk() || m.InVendorRamdisk() || m.InRecovery() {
181		return false
182	}
183
184	// Don't create ABI dump for prebuilts.
185	if m.Prebuilt() != nil || m.IsSnapshotPrebuilt() {
186		return false
187	}
188
189	// Coverage builds have extra symbols.
190	if m.isCoverageVariant() {
191		return false
192	}
193
194	// Some sanitizer variants may have different ABI.
195	if m.sanitize != nil && !m.sanitize.isVariantOnProductionDevice() {
196		return false
197	}
198
199	// Don't create ABI dump for stubs.
200	if m.isNDKStubLibrary() || m.IsLlndk() || m.IsStubs() {
201		return false
202	}
203
204	apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
205	if apexInfo.IsForPlatform() {
206		// Bionic libraries that are installed to the bootstrap directory are not ABI checked.
207		// Only the runtime APEX variants, which are the implementation libraries of bionic NDK stubs,
208		// are checked.
209		if InstallToBootstrap(m.BaseModuleName(), ctx.Config()) {
210			return false
211		}
212	} else {
213		// Don't create ABI dump if this library is for APEX but isn't exported.
214		if !m.HasStubsVariants() {
215			return false
216		}
217	}
218	return len(classifySourceAbiDump(ctx)) > 0
219}
220
221// Mark the direct and transitive dependencies of libraries that need ABI check, so that ABI dumps
222// of their dependencies would be generated.
223func sabiDepsMutator(mctx android.TopDownMutatorContext) {
224	// Escape hatch to not check any ABI dump.
225	if mctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
226		return
227	}
228	// Only create ABI dump for native shared libraries and their static library dependencies.
229	if m, ok := mctx.Module().(*Module); ok && m.sabi != nil {
230		if shouldCreateSourceAbiDumpForLibrary(mctx) {
231			// Mark this module so that .sdump / .lsdump for this library can be generated.
232			m.sabi.Properties.ShouldCreateSourceAbiDump = true
233			// Mark all of its static library dependencies.
234			mctx.VisitDirectDeps(func(child android.Module) {
235				depTag := mctx.OtherModuleDependencyTag(child)
236				if IsStaticDepTag(depTag) || depTag == reuseObjTag {
237					if c, ok := child.(*Module); ok && c.sabi != nil {
238						// Mark this module so that .sdump for this static library can be generated.
239						c.sabi.Properties.ShouldCreateSourceAbiDump = true
240					}
241				}
242			})
243		}
244	}
245}
246
247// Add an entry to the global list of lsdump. The list is exported to a Make variable by
248// `cc.makeVarsProvider`.
249func addLsdumpPath(lsdumpPath string) {
250	lsdumpPathsLock.Lock()
251	defer lsdumpPathsLock.Unlock()
252	lsdumpPaths = append(lsdumpPaths, lsdumpPath)
253}
254