xref: /aosp_15_r20/build/soong/cc/library.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2016 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	"fmt"
19	"io"
20	"path/filepath"
21	"regexp"
22	"slices"
23	"strconv"
24	"strings"
25	"sync"
26
27	"android/soong/android"
28
29	"github.com/google/blueprint"
30	"github.com/google/blueprint/depset"
31	"github.com/google/blueprint/pathtools"
32	"github.com/google/blueprint/proptools"
33)
34
35// LibraryProperties is a collection of properties shared by cc library rules/cc.
36type LibraryProperties struct {
37	// local file name to pass to the linker as -unexported_symbols_list
38	Unexported_symbols_list *string `android:"path,arch_variant"`
39	// local file name to pass to the linker as -force_symbols_not_weak_list
40	Force_symbols_not_weak_list *string `android:"path,arch_variant"`
41	// local file name to pass to the linker as -force_symbols_weak_list
42	Force_symbols_weak_list *string `android:"path,arch_variant"`
43
44	// rename host libraries to prevent overlap with system installed libraries
45	Unique_host_soname *bool
46
47	Aidl struct {
48		// export headers generated from .aidl sources
49		Export_aidl_headers *bool
50	}
51
52	Proto struct {
53		// export headers generated from .proto sources
54		Export_proto_headers *bool
55	}
56
57	Sysprop struct {
58		// Whether platform owns this sysprop library.
59		Platform *bool
60	} `blueprint:"mutated"`
61
62	Static_ndk_lib *bool
63
64	// Generate stubs to make this library accessible to APEXes.
65	Stubs struct {
66		// Relative path to the symbol map. The symbol map provides the list of
67		// symbols that are exported for stubs variant of this library.
68		Symbol_file *string `android:"path,arch_variant"`
69
70		// List versions to generate stubs libs for. The version name "current" is always
71		// implicitly added.
72		Versions []string
73
74		// Whether to not require the implementation of the library to be installed if a
75		// client of the stubs is installed. Defaults to true; set to false if the
76		// implementation is made available by some other means, e.g. in a Microdroid
77		// virtual machine.
78		Implementation_installable *bool
79	} `android:"arch_variant"`
80
81	// set the name of the output
82	Stem *string `android:"arch_variant"`
83
84	// set suffix of the name of the output
85	Suffix *string `android:"arch_variant"`
86
87	// Properties for ABI compatibility checker.
88	Header_abi_checker headerAbiCheckerProperties
89
90	Target struct {
91		Vendor, Product struct {
92			// set suffix of the name of the output
93			Suffix *string `android:"arch_variant"`
94
95			Header_abi_checker headerAbiCheckerProperties
96
97			// Disable stubs for vendor/product variants
98			// This is a workaround to keep `stubs` only for "core" variant (not product/vendor).
99			// It would be nice if we could put `stubs` into a `target: { core: {} }`
100			// block but it's not supported in soong yet. This could be removed/simplified once we have
101			// a better syntax.
102			No_stubs bool
103		}
104
105		Platform struct {
106			Header_abi_checker headerAbiCheckerProperties
107		}
108	}
109
110	// Names of modules to be overridden. Listed modules can only be other shared libraries
111	// (in Make or Soong).
112	// This does not completely prevent installation of the overridden libraries, but if both
113	// binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
114	// from PRODUCT_PACKAGES.
115	Overrides []string
116
117	// Inject boringssl hash into the shared library.  This is only intended for use by external/boringssl.
118	Inject_bssl_hash *bool `android:"arch_variant"`
119
120	// If this is an LLNDK library, properties to describe the LLNDK stubs.  Will be copied from
121	// the module pointed to by llndk_stubs if it is set.
122	Llndk llndkLibraryProperties `android:"arch_variant"`
123
124	// If this is a vendor public library, properties to describe the vendor public library stubs.
125	Vendor_public_library vendorPublicLibraryProperties
126}
127
128// StaticProperties is a properties stanza to affect only attributes of the "static" variants of a
129// library module.
130type StaticProperties struct {
131	Static StaticOrSharedProperties `android:"arch_variant"`
132}
133
134// SharedProperties is a properties stanza to affect only attributes of the "shared" variants of a
135// library module.
136type SharedProperties struct {
137	Shared StaticOrSharedProperties `android:"arch_variant"`
138}
139
140// StaticOrSharedProperties is an embedded struct representing properties to affect attributes of
141// either only the "static" variants or only the "shared" variants of a library module. These override
142// the base properties of the same name.
143// Use `StaticProperties` or `SharedProperties`, depending on which variant is needed.
144// `StaticOrSharedProperties` exists only to avoid duplication.
145type StaticOrSharedProperties struct {
146	Srcs proptools.Configurable[[]string] `android:"path,arch_variant"`
147
148	Tidy_disabled_srcs []string `android:"path,arch_variant"`
149
150	Tidy_timeout_srcs []string `android:"path,arch_variant"`
151
152	Sanitized Sanitized `android:"arch_variant"`
153
154	Cflags proptools.Configurable[[]string] `android:"arch_variant"`
155
156	Enabled            *bool                            `android:"arch_variant"`
157	Whole_static_libs  proptools.Configurable[[]string] `android:"arch_variant"`
158	Static_libs        proptools.Configurable[[]string] `android:"arch_variant"`
159	Shared_libs        proptools.Configurable[[]string] `android:"arch_variant"`
160	System_shared_libs []string                         `android:"arch_variant"`
161
162	Export_shared_lib_headers []string `android:"arch_variant"`
163	Export_static_lib_headers []string `android:"arch_variant"`
164
165	Apex_available []string `android:"arch_variant"`
166
167	Installable *bool `android:"arch_variant"`
168}
169
170type LibraryMutatedProperties struct {
171	// Build a static variant
172	BuildStatic bool `blueprint:"mutated"`
173	// Build a shared variant
174	BuildShared bool `blueprint:"mutated"`
175	// This variant is shared
176	VariantIsShared bool `blueprint:"mutated"`
177	// This variant is static
178	VariantIsStatic bool `blueprint:"mutated"`
179
180	// This variant is a stubs lib
181	BuildStubs bool `blueprint:"mutated"`
182	// This variant is the latest version
183	IsLatestVersion bool `blueprint:"mutated"`
184	// Version of the stubs lib
185	StubsVersion string `blueprint:"mutated"`
186	// List of all stubs versions associated with an implementation lib
187	AllStubsVersions []string `blueprint:"mutated"`
188}
189
190type FlagExporterProperties struct {
191	// list of directories relative to the Blueprints file that will
192	// be added to the include path (using -I) for this module and any module that links
193	// against this module.  Directories listed in export_include_dirs do not need to be
194	// listed in local_include_dirs.
195	Export_include_dirs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
196
197	// list of directories that will be added to the system include path
198	// using -isystem for this module and any module that links against this module.
199	Export_system_include_dirs []string `android:"arch_variant,variant_prepend"`
200
201	Target struct {
202		Vendor, Product struct {
203			// list of exported include directories, like
204			// export_include_dirs, that will be applied to
205			// vendor or product variant of this library.
206			// This will overwrite any other declarations.
207			Override_export_include_dirs []string
208		}
209	}
210}
211
212func init() {
213	RegisterLibraryBuildComponents(android.InitRegistrationContext)
214}
215
216func RegisterLibraryBuildComponents(ctx android.RegistrationContext) {
217	ctx.RegisterModuleType("cc_library_static", LibraryStaticFactory)
218	ctx.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
219	ctx.RegisterModuleType("cc_library", LibraryFactory)
220	ctx.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
221	ctx.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
222}
223
224// cc_library creates both static and/or shared libraries for a device and/or
225// host. By default, a cc_library has a single variant that targets the device.
226// Specifying `host_supported: true` also creates a library that targets the
227// host.
228func LibraryFactory() android.Module {
229	module, _ := NewLibrary(android.HostAndDeviceSupported)
230	// Can be used as both a static and a shared library.
231	module.sdkMemberTypes = []android.SdkMemberType{
232		sharedLibrarySdkMemberType,
233		staticLibrarySdkMemberType,
234		staticAndSharedLibrarySdkMemberType,
235	}
236	return module.Init()
237}
238
239// cc_library_static creates a static library for a device and/or host binary.
240func LibraryStaticFactory() android.Module {
241	module, library := NewLibrary(android.HostAndDeviceSupported)
242	library.BuildOnlyStatic()
243	module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType}
244	return module.Init()
245}
246
247// cc_library_shared creates a shared library for a device and/or host.
248func LibrarySharedFactory() android.Module {
249	module, library := NewLibrary(android.HostAndDeviceSupported)
250	library.BuildOnlyShared()
251	module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType}
252	return module.Init()
253}
254
255// cc_library_host_static creates a static library that is linkable to a host
256// binary.
257func LibraryHostStaticFactory() android.Module {
258	module, library := NewLibrary(android.HostSupported)
259	library.BuildOnlyStatic()
260	module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType}
261	return module.Init()
262}
263
264// cc_library_host_shared creates a shared library that is usable on a host.
265func LibraryHostSharedFactory() android.Module {
266	module, library := NewLibrary(android.HostSupported)
267	library.BuildOnlyShared()
268	module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType}
269	return module.Init()
270}
271
272// flagExporter is a separated portion of libraryDecorator pertaining to exported
273// include paths and flags. Keeping this dependency-related information separate
274// from the rest of library information is helpful in keeping data more structured
275// and explicit.
276type flagExporter struct {
277	Properties FlagExporterProperties
278
279	dirs         android.Paths // Include directories to be included with -I
280	systemDirs   android.Paths // System include directories to be included with -isystem
281	flags        []string      // Exported raw flags.
282	deps         android.Paths
283	headers      android.Paths
284	rustRlibDeps []RustRlibDep
285}
286
287// exportedIncludes returns the effective include paths for this module and
288// any module that links against this module. This is obtained from
289// the export_include_dirs property in the appropriate target stanza.
290func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
291	if ctx.inVendor() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
292		return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
293	}
294	if ctx.inProduct() && f.Properties.Target.Product.Override_export_include_dirs != nil {
295		return android.PathsForModuleSrc(ctx, f.Properties.Target.Product.Override_export_include_dirs)
296	}
297	return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs.GetOrDefault(ctx, nil))
298}
299
300func (f *flagExporter) exportedSystemIncludes(ctx ModuleContext) android.Paths {
301	return android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)
302}
303
304// exportIncludes registers the include directories and system include directories to be exported
305// transitively to modules depending on this module.
306func (f *flagExporter) exportIncludes(ctx ModuleContext) {
307	f.dirs = append(f.dirs, f.exportedIncludes(ctx)...)
308	f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...)
309}
310
311// exportIncludesAsSystem registers the include directories and system include directories to be
312// exported transitively both as system include directories to modules depending on this module.
313func (f *flagExporter) exportIncludesAsSystem(ctx ModuleContext) {
314	// all dirs are force exported as system
315	f.systemDirs = append(f.systemDirs, f.exportedIncludes(ctx)...)
316	f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...)
317}
318
319// reexportDirs registers the given directories as include directories to be exported transitively
320// to modules depending on this module.
321func (f *flagExporter) reexportDirs(dirs ...android.Path) {
322	f.dirs = append(f.dirs, dirs...)
323}
324
325// reexportSystemDirs registers the given directories as system include directories
326// to be exported transitively to modules depending on this module.
327func (f *flagExporter) reexportSystemDirs(dirs ...android.Path) {
328	f.systemDirs = append(f.systemDirs, dirs...)
329}
330
331// reexportFlags registers the flags to be exported transitively to modules depending on this
332// module.
333func (f *flagExporter) reexportFlags(flags ...string) {
334	if android.PrefixInList(flags, "-I") || android.PrefixInList(flags, "-isystem") {
335		panic(fmt.Errorf("Exporting invalid flag %q: "+
336			"use reexportDirs or reexportSystemDirs to export directories", flag))
337	}
338	f.flags = append(f.flags, flags...)
339}
340
341func (f *flagExporter) reexportDeps(deps ...android.Path) {
342	f.deps = append(f.deps, deps...)
343}
344
345func (f *flagExporter) reexportRustStaticDeps(deps ...RustRlibDep) {
346	f.rustRlibDeps = append(f.rustRlibDeps, deps...)
347}
348
349// addExportedGeneratedHeaders does nothing but collects generated header files.
350// This can be differ to exportedDeps which may contain phony files to minimize ninja.
351func (f *flagExporter) addExportedGeneratedHeaders(headers ...android.Path) {
352	f.headers = append(f.headers, headers...)
353}
354
355func (f *flagExporter) setProvider(ctx android.ModuleContext) {
356	android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
357		// Comes from Export_include_dirs property, and those of exported transitive deps
358		IncludeDirs: android.FirstUniquePaths(f.dirs),
359		// Comes from Export_system_include_dirs property, and those of exported transitive deps
360		SystemIncludeDirs: android.FirstUniquePaths(f.systemDirs),
361		// Used in very few places as a one-off way of adding extra defines.
362		Flags: f.flags,
363		// Used sparingly, for extra files that need to be explicitly exported to dependers,
364		// or for phony files to minimize ninja.
365		Deps: f.deps,
366		// Used for exporting rlib deps of static libraries to dependents.
367		RustRlibDeps: f.rustRlibDeps,
368		// For exported generated headers, such as exported aidl headers, proto headers, or
369		// sysprop headers.
370		GeneratedHeaders: f.headers,
371	})
372}
373
374// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
375// functionality: static vs. shared linkage, reusing object files for shared libraries
376type libraryDecorator struct {
377	Properties        LibraryProperties
378	StaticProperties  StaticProperties
379	SharedProperties  SharedProperties
380	MutatedProperties LibraryMutatedProperties
381
382	// For reusing static library objects for shared library
383	reuseObjects Objects
384
385	// table-of-contents file to optimize out relinking when possible
386	tocFile android.OptionalPath
387
388	flagExporter
389	flagExporterInfo *FlagExporterInfo
390	stripper         Stripper
391
392	// For whole_static_libs
393	objects                      Objects
394	wholeStaticLibsFromPrebuilts android.Paths
395
396	// Uses the module's name if empty, but can be overridden. Does not include
397	// shlib suffix.
398	libName string
399
400	sabi *sabi
401
402	// Output archive of gcno coverage information files
403	coverageOutputFile android.OptionalPath
404
405	// Source Abi Diff
406	sAbiDiff android.Paths
407
408	// Location of the static library in the sysroot. Empty if the library is
409	// not included in the NDK.
410	ndkSysrootPath android.Path
411
412	// Location of the linked, unstripped library for shared libraries
413	unstrippedOutputFile android.Path
414	// Location of the linked, stripped library for shared libraries, strip: "all"
415	strippedAllOutputFile android.Path
416
417	// Location of the file that should be copied to dist dir when requested
418	distFile android.Path
419
420	versionScriptPath android.OptionalPath
421
422	postInstallCmds []string
423
424	skipAPIDefine bool
425
426	// Decorated interfaces
427	*baseCompiler
428	*baseLinker
429	*baseInstaller
430
431	apiListCoverageXmlPath android.ModuleOutPath
432
433	// Path to the file containing the APIs exported by this library
434	stubsSymbolFilePath android.Path
435}
436
437// linkerProps returns the list of properties structs relevant for this library. (For example, if
438// the library is cc_shared_library, then static-library properties are omitted.)
439func (library *libraryDecorator) linkerProps() []interface{} {
440	var props []interface{}
441	props = append(props, library.baseLinker.linkerProps()...)
442	props = append(props,
443		&library.Properties,
444		&library.MutatedProperties,
445		&library.flagExporter.Properties,
446		&library.stripper.StripProperties)
447
448	if library.MutatedProperties.BuildShared {
449		props = append(props, &library.SharedProperties)
450	}
451	if library.MutatedProperties.BuildStatic {
452		props = append(props, &library.StaticProperties)
453	}
454
455	return props
456}
457
458// linkerFlags takes a Flags struct and augments it to contain linker flags that are defined by this
459// library, or that are implied by attributes of this library (such as whether this library is a
460// shared library).
461func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
462	flags = library.baseLinker.linkerFlags(ctx, flags)
463
464	// MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
465	// all code is position independent, and then those warnings get promoted to
466	// errors.
467	if !ctx.Windows() {
468		flags.Global.CFlags = append(flags.Global.CFlags, "-fPIC")
469	}
470
471	if library.static() {
472		flags.Local.CFlags = append(flags.Local.CFlags, library.StaticProperties.Static.Cflags.GetOrDefault(ctx, nil)...)
473	} else if library.shared() {
474		flags.Local.CFlags = append(flags.Local.CFlags, library.SharedProperties.Shared.Cflags.GetOrDefault(ctx, nil)...)
475	}
476
477	if library.shared() {
478		libName := library.getLibName(ctx)
479		var f []string
480		if ctx.toolchain().Bionic() {
481			f = append(f,
482				"-nostdlib",
483				"-Wl,--gc-sections",
484			)
485		}
486
487		if ctx.Darwin() {
488			f = append(f,
489				"-dynamiclib",
490				"-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
491			)
492			if ctx.Arch().ArchType == android.X86 {
493				f = append(f,
494					"-read_only_relocs suppress",
495				)
496			}
497		} else {
498			f = append(f, "-shared")
499			if !ctx.Windows() {
500				f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
501			}
502		}
503
504		flags.Global.LdFlags = append(flags.Global.LdFlags, f...)
505	}
506
507	return flags
508}
509
510// compilerFlags takes a Flags and augments it to contain compile flags from global values,
511// per-target values, module type values, per-module Blueprints properties, extra flags from
512// `flags`, and generated sources from `deps`.
513func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
514	exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
515	if len(exportIncludeDirs) > 0 {
516		f := includeDirsToFlags(exportIncludeDirs)
517		flags.Local.CommonFlags = append(flags.Local.CommonFlags, f)
518		flags.Local.YasmFlags = append(flags.Local.YasmFlags, f)
519	}
520
521	flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
522	if ctx.IsLlndk() {
523		// LLNDK libraries ignore most of the properties on the cc_library and use the
524		// LLNDK-specific properties instead.
525		// Wipe all the module-local properties, leaving only the global properties.
526		flags.Local = LocalOrGlobalFlags{}
527	}
528	if library.buildStubs() {
529		// Remove -include <file> when compiling stubs. Otherwise, the force included
530		// headers might cause conflicting types error with the symbols in the
531		// generated stubs source code. e.g.
532		// double acos(double); // in header
533		// void acos() {} // in the generated source code
534		removeInclude := func(flags []string) []string {
535			ret := flags[:0]
536			for _, f := range flags {
537				if strings.HasPrefix(f, "-include ") {
538					continue
539				}
540				ret = append(ret, f)
541			}
542			return ret
543		}
544		flags.Local.CommonFlags = removeInclude(flags.Local.CommonFlags)
545		flags.Local.CFlags = removeInclude(flags.Local.CFlags)
546
547		flags = addStubLibraryCompilerFlags(flags)
548	}
549	return flags
550}
551
552func (library *libraryDecorator) getHeaderAbiCheckerProperties(m *Module) headerAbiCheckerProperties {
553	variantProps := &library.Properties.Target.Platform.Header_abi_checker
554	if m.InVendor() {
555		variantProps = &library.Properties.Target.Vendor.Header_abi_checker
556	} else if m.InProduct() {
557		variantProps = &library.Properties.Target.Product.Header_abi_checker
558	}
559	props := library.Properties.Header_abi_checker
560	err := proptools.AppendProperties(&props, variantProps, nil)
561	if err != nil {
562		panic(fmt.Errorf("Cannot merge headerAbiCheckerProperties: %s", err.Error()))
563	}
564	return props
565}
566
567func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
568	if ctx.IsLlndk() {
569		// Get the matching SDK version for the vendor API level.
570		version, err := android.GetSdkVersionForVendorApiLevel(ctx.Config().VendorApiLevel())
571		if err != nil {
572			panic(err)
573		}
574
575		// This is the vendor variant of an LLNDK library, build the LLNDK stubs.
576		nativeAbiResult := parseNativeAbiDefinition(ctx,
577			String(library.Properties.Llndk.Symbol_file),
578			nativeClampedApiLevel(ctx, version), "--llndk")
579		objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
580		if !Bool(library.Properties.Llndk.Unversioned) {
581			library.versionScriptPath = android.OptionalPathForPath(
582				nativeAbiResult.versionScript)
583		}
584		return objs
585	}
586	if ctx.IsVendorPublicLibrary() {
587		nativeAbiResult := parseNativeAbiDefinition(ctx,
588			String(library.Properties.Vendor_public_library.Symbol_file),
589			android.FutureApiLevel, "")
590		objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
591		if !Bool(library.Properties.Vendor_public_library.Unversioned) {
592			library.versionScriptPath = android.OptionalPathForPath(nativeAbiResult.versionScript)
593		}
594		return objs
595	}
596	if library.buildStubs() {
597		return library.compileModuleLibApiStubs(ctx, flags, deps)
598	}
599
600	srcs := library.baseCompiler.Properties.Srcs.GetOrDefault(ctx, nil)
601	staticSrcs := library.StaticProperties.Static.Srcs.GetOrDefault(ctx, nil)
602	sharedSrcs := library.SharedProperties.Shared.Srcs.GetOrDefault(ctx, nil)
603	if !library.buildShared() && !library.buildStatic() {
604		if len(srcs) > 0 {
605			ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
606		}
607		if len(staticSrcs) > 0 {
608			ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
609		}
610		if len(sharedSrcs) > 0 {
611			ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
612		}
613		return Objects{}
614	}
615	if library.sabi.shouldCreateSourceAbiDump() {
616		dirs := library.exportedIncludeDirsForAbiCheck(ctx)
617		flags.SAbiFlags = make([]string, 0, len(dirs))
618		for _, dir := range dirs {
619			flags.SAbiFlags = append(flags.SAbiFlags, "-I"+dir)
620		}
621		totalLength := len(srcs) + len(deps.GeneratedSources) +
622			len(sharedSrcs) + len(staticSrcs)
623		if totalLength > 0 {
624			flags.SAbiDump = true
625		}
626	}
627	objs := library.baseCompiler.compile(ctx, flags, deps)
628	library.reuseObjects = objs
629	buildFlags := flagsToBuilderFlags(flags)
630
631	if library.static() {
632		srcs := android.PathsForModuleSrc(ctx, staticSrcs)
633		objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary, srcs,
634			android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Tidy_disabled_srcs),
635			android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Tidy_timeout_srcs),
636			library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
637	} else if library.shared() {
638		srcs := android.PathsForModuleSrc(ctx, sharedSrcs)
639		objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary, srcs,
640			android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Tidy_disabled_srcs),
641			android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Tidy_timeout_srcs),
642			library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
643	}
644
645	return objs
646}
647
648// Compile stubs for the API surface between platform and apex
649// This method will be used by source and prebuilt cc module types.
650func (library *libraryDecorator) compileModuleLibApiStubs(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
651	// TODO (b/275273834): Make this a hard error when the symbol files have been added to module sdk.
652	if library.Properties.Stubs.Symbol_file == nil {
653		return Objects{}
654	}
655	symbolFile := String(library.Properties.Stubs.Symbol_file)
656	library.stubsSymbolFilePath = android.PathForModuleSrc(ctx, symbolFile)
657	// b/239274367 --apex and --systemapi filters symbols tagged with # apex and #
658	// systemapi, respectively. The former is for symbols defined in platform libraries
659	// and the latter is for symbols defined in APEXes.
660	// A single library can contain either # apex or # systemapi, but not both.
661	// The stub generator (ndkstubgen) is additive, so passing _both_ of these to it should be a no-op.
662	// However, having this distinction helps guard accidental
663	// promotion or demotion of API and also helps the API review process b/191371676
664	var flag string
665	if ctx.notInPlatform() {
666		flag = "--apex"
667	} else {
668		flag = "--systemapi"
669	}
670	// b/184712170, unless the lib is an NDK library, exclude all public symbols from
671	// the stub so that it is mandated that all symbols are explicitly marked with
672	// either apex or systemapi.
673	if !ctx.Module().(*Module).IsNdk(ctx.Config()) &&
674		// the symbol files of libclang libs are autogenerated and do not contain systemapi tags
675		// TODO (spandandas): Update mapfile.py to include #systemapi tag on all symbols
676		!strings.Contains(ctx.ModuleName(), "libclang_rt") {
677		flag = flag + " --no-ndk"
678	}
679	// TODO(b/361303067): Remove this special case if bionic/ projects are added to ART development branches.
680	if isBionic(ctx.baseModuleName()) {
681		// set the flags explicitly for bionic libs.
682		// this is necessary for development in minimal branches which does not contain bionic/*.
683		// In such minimal branches, e.g. on the prebuilt libc stubs
684		// 1. IsNdk will return false (since the ndk_library definition for libc does not exist)
685		// 2. NotInPlatform will return true (since the source com.android.runtime does not exist)
686		flag = "--apex"
687	}
688	nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile,
689		android.ApiLevelOrPanic(ctx, library.MutatedProperties.StubsVersion), flag)
690	objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
691
692	library.versionScriptPath = android.OptionalPathForPath(
693		nativeAbiResult.versionScript)
694
695	// Parse symbol file to get API list for coverage
696	if library.stubsVersion() == "current" && ctx.PrimaryArch() && !ctx.inRecovery() && !ctx.inProduct() && !ctx.inVendor() {
697		library.apiListCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile)
698	}
699
700	return objs
701}
702
703type libraryInterface interface {
704	versionedInterface
705
706	static() bool
707	shared() bool
708	objs() Objects
709	reuseObjs() Objects
710	toc() android.OptionalPath
711
712	// Returns true if the build options for the module have selected a static or shared build
713	buildStatic() bool
714	buildShared() bool
715
716	// Sets whether a specific variant is static or shared
717	setStatic()
718	setShared()
719
720	// Gets the ABI properties for vendor, product, or platform variant
721	getHeaderAbiCheckerProperties(m *Module) headerAbiCheckerProperties
722
723	// Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff
724	androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer)
725
726	availableFor(string) bool
727
728	getAPIListCoverageXMLPath() android.ModuleOutPath
729
730	installable() *bool
731}
732
733type versionedInterface interface {
734	buildStubs() bool
735	setBuildStubs(isLatest bool)
736	hasStubsVariants() bool
737	isStubsImplementationRequired() bool
738	setStubsVersion(string)
739	stubsVersion() string
740
741	stubsVersions(ctx android.BaseModuleContext) []string
742	setAllStubsVersions([]string)
743	allStubsVersions() []string
744
745	implementationModuleName(name string) string
746	hasLLNDKStubs() bool
747	hasLLNDKHeaders() bool
748	hasVendorPublicLibrary() bool
749	isLLNDKMovedToApex() bool
750}
751
752var _ libraryInterface = (*libraryDecorator)(nil)
753var _ versionedInterface = (*libraryDecorator)(nil)
754
755func (library *libraryDecorator) getLibNameHelper(baseModuleName string, inVendor bool, inProduct bool) string {
756	name := library.libName
757	if name == "" {
758		name = String(library.Properties.Stem)
759		if name == "" {
760			name = baseModuleName
761		}
762	}
763
764	suffix := ""
765	if inVendor {
766		suffix = String(library.Properties.Target.Vendor.Suffix)
767	} else if inProduct {
768		suffix = String(library.Properties.Target.Product.Suffix)
769	}
770	if suffix == "" {
771		suffix = String(library.Properties.Suffix)
772	}
773
774	return name + suffix
775}
776
777// getLibName returns the actual canonical name of the library (the name which
778// should be passed to the linker via linker flags).
779func (library *libraryDecorator) getLibName(ctx BaseModuleContext) string {
780	name := library.getLibNameHelper(ctx.baseModuleName(), ctx.inVendor(), ctx.inProduct())
781
782	if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
783		if !strings.HasSuffix(name, "-host") {
784			name = name + "-host"
785		}
786	}
787
788	return name
789}
790
791var versioningMacroNamesListMutex sync.Mutex
792
793func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
794	location := InstallInSystem
795	if library.baseLinker.sanitize.inSanitizerDir() {
796		location = InstallInSanitizerDir
797	}
798	library.baseInstaller.location = location
799	library.baseLinker.linkerInit(ctx)
800	// Let baseLinker know whether this variant is for stubs or not, so that
801	// it can omit things that are not required for linking stubs.
802	library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
803
804	if library.buildStubs() {
805		macroNames := versioningMacroNamesList(ctx.Config())
806		myName := versioningMacroName(ctx.ModuleName())
807		versioningMacroNamesListMutex.Lock()
808		defer versioningMacroNamesListMutex.Unlock()
809		if (*macroNames)[myName] == "" {
810			(*macroNames)[myName] = ctx.ModuleName()
811		} else if (*macroNames)[myName] != ctx.ModuleName() {
812			ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
813		}
814	}
815}
816
817func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
818	if ctx.IsLlndk() {
819		// LLNDK libraries ignore most of the properties on the cc_library and use the
820		// LLNDK-specific properties instead.
821		return deps
822	}
823
824	deps = library.baseCompiler.compilerDeps(ctx, deps)
825
826	return deps
827}
828
829func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
830	if ctx.IsLlndk() {
831		// LLNDK libraries ignore most of the properties on the cc_library and use the
832		// LLNDK-specific properties instead.
833		deps.HeaderLibs = append([]string(nil), library.Properties.Llndk.Export_llndk_headers...)
834		deps.ReexportHeaderLibHeaders = append([]string(nil), library.Properties.Llndk.Export_llndk_headers...)
835		return deps
836	}
837	if ctx.IsVendorPublicLibrary() {
838		headers := library.Properties.Vendor_public_library.Export_public_headers
839		deps.HeaderLibs = append([]string(nil), headers...)
840		deps.ReexportHeaderLibHeaders = append([]string(nil), headers...)
841		return deps
842	}
843
844	if library.static() {
845		// Compare with nil because an empty list needs to be propagated.
846		if library.StaticProperties.Static.System_shared_libs != nil {
847			library.baseLinker.Properties.System_shared_libs = library.StaticProperties.Static.System_shared_libs
848		}
849	} else if library.shared() {
850		// Compare with nil because an empty list needs to be propagated.
851		if library.SharedProperties.Shared.System_shared_libs != nil {
852			library.baseLinker.Properties.System_shared_libs = library.SharedProperties.Shared.System_shared_libs
853		}
854	}
855
856	deps = library.baseLinker.linkerDeps(ctx, deps)
857
858	if library.static() {
859		deps.WholeStaticLibs = append(deps.WholeStaticLibs,
860			library.StaticProperties.Static.Whole_static_libs.GetOrDefault(ctx, nil)...)
861		deps.StaticLibs = append(deps.StaticLibs, library.StaticProperties.Static.Static_libs.GetOrDefault(ctx, nil)...)
862		deps.SharedLibs = append(deps.SharedLibs, library.StaticProperties.Static.Shared_libs.GetOrDefault(ctx, nil)...)
863
864		deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.StaticProperties.Static.Export_shared_lib_headers...)
865		deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.StaticProperties.Static.Export_static_lib_headers...)
866	} else if library.shared() {
867		if library.baseLinker.Properties.crt() {
868			deps.CrtBegin = append(deps.CrtBegin, ctx.toolchain().CrtBeginSharedLibrary()...)
869			deps.CrtEnd = append(deps.CrtEnd, ctx.toolchain().CrtEndSharedLibrary()...)
870
871		}
872		if library.baseLinker.Properties.crtPadSegment() {
873			deps.CrtEnd = append(deps.CrtEnd, ctx.toolchain().CrtPadSegmentSharedLibrary()...)
874		}
875		deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.SharedProperties.Shared.Whole_static_libs.GetOrDefault(ctx, nil)...)
876		deps.StaticLibs = append(deps.StaticLibs, library.SharedProperties.Shared.Static_libs.GetOrDefault(ctx, nil)...)
877		deps.SharedLibs = append(deps.SharedLibs, library.SharedProperties.Shared.Shared_libs.GetOrDefault(ctx, nil)...)
878
879		deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.SharedProperties.Shared.Export_shared_lib_headers...)
880		deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.SharedProperties.Shared.Export_static_lib_headers...)
881
882		deps.LlndkHeaderLibs = append(deps.LlndkHeaderLibs, library.Properties.Llndk.Export_llndk_headers...)
883	}
884	if ctx.inVendor() {
885		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
886		deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
887		deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
888		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
889		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
890	}
891	if ctx.inProduct() {
892		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
893		deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Product.Exclude_shared_libs)
894		deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
895		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_shared_libs)
896		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_static_libs)
897	}
898	if ctx.inRecovery() {
899		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
900		deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
901		deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
902		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
903		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
904	}
905	if ctx.inRamdisk() {
906		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
907		deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs)
908		deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
909		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs)
910		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs)
911	}
912	if ctx.inVendorRamdisk() {
913		deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
914		deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
915		deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
916		deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs)
917		deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs)
918	}
919
920	return deps
921}
922
923func (library *libraryDecorator) linkerSpecifiedDeps(ctx android.ConfigurableEvaluatorContext, module *Module, specifiedDeps specifiedDeps) specifiedDeps {
924	specifiedDeps = library.baseLinker.linkerSpecifiedDeps(ctx, module, specifiedDeps)
925	var properties StaticOrSharedProperties
926	if library.static() {
927		properties = library.StaticProperties.Static
928	} else if library.shared() {
929		properties = library.SharedProperties.Shared
930	}
931
932	eval := module.ConfigurableEvaluator(ctx)
933	specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, properties.Shared_libs.GetOrDefault(eval, nil)...)
934
935	// Must distinguish nil and [] in system_shared_libs - ensure that [] in
936	// either input list doesn't come out as nil.
937	if specifiedDeps.systemSharedLibs == nil {
938		specifiedDeps.systemSharedLibs = properties.System_shared_libs
939	} else {
940		specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, properties.System_shared_libs...)
941	}
942
943	specifiedDeps.sharedLibs = android.FirstUniqueStrings(specifiedDeps.sharedLibs)
944	if len(specifiedDeps.systemSharedLibs) > 0 {
945		// Skip this if systemSharedLibs is either nil or [], to ensure they are
946		// retained.
947		specifiedDeps.systemSharedLibs = android.FirstUniqueStrings(specifiedDeps.systemSharedLibs)
948	}
949	return specifiedDeps
950}
951
952func (library *libraryDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
953	if library.static() {
954		moduleInfoJSON.Class = []string{"STATIC_LIBRARIES"}
955		moduleInfoJSON.Uninstallable = true
956	} else if library.shared() {
957		moduleInfoJSON.Class = []string{"SHARED_LIBRARIES"}
958	} else if library.header() {
959		moduleInfoJSON.Class = []string{"HEADER_LIBRARIES"}
960		moduleInfoJSON.Uninstallable = true
961	}
962
963	if library.buildStubs() && library.stubsVersion() != "" {
964		moduleInfoJSON.SubName += "." + library.stubsVersion()
965	}
966
967	// If a library providing a stub is included in an APEX, the private APIs of the library
968	// is accessible only inside the APEX. From outside of the APEX, clients can only use the
969	// public APIs via the stub. To enforce this, the (latest version of the) stub gets the
970	// name of the library. The impl library instead gets the `.bootstrap` suffix to so that
971	// they can be exceptionally used directly when APEXes are not available (e.g. during the
972	// very early stage in the boot process).
973	if len(library.Properties.Stubs.Versions) > 0 && !ctx.Host() && ctx.notInPlatform() &&
974		!ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() && !ctx.useVndk() && !ctx.static() {
975		if library.buildStubs() && library.isLatestStubVersion() {
976			moduleInfoJSON.SubName = ""
977		}
978		if !library.buildStubs() {
979			moduleInfoJSON.SubName = ".bootstrap"
980		}
981	}
982
983	library.baseLinker.moduleInfoJSON(ctx, moduleInfoJSON)
984}
985
986func (library *libraryDecorator) linkStatic(ctx ModuleContext,
987	flags Flags, deps PathDeps, objs Objects) android.Path {
988
989	library.objects = deps.WholeStaticLibObjs.Copy()
990	library.objects = library.objects.Append(objs)
991	library.wholeStaticLibsFromPrebuilts = android.CopyOfPaths(deps.WholeStaticLibsFromPrebuilts)
992
993	fileName := ctx.ModuleName() + staticLibraryExtension
994	outputFile := android.PathForModuleOut(ctx, fileName)
995	builderFlags := flagsToBuilderFlags(flags)
996
997	if Bool(library.baseLinker.Properties.Use_version_lib) {
998		if ctx.Host() {
999			versionedOutputFile := outputFile
1000			outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
1001			library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1002		} else {
1003			versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
1004			library.distFile = versionedOutputFile
1005			library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1006		}
1007	}
1008
1009	transformObjToStaticLib(ctx, library.objects.objFiles, deps.WholeStaticLibsFromPrebuilts, builderFlags, outputFile, nil, objs.tidyDepFiles)
1010
1011	library.coverageOutputFile = transformCoverageFilesToZip(ctx, library.objects, ctx.ModuleName())
1012
1013	ctx.CheckbuildFile(outputFile)
1014
1015	if library.static() {
1016		android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
1017			StaticLibrary:                outputFile,
1018			ReuseObjects:                 library.reuseObjects,
1019			Objects:                      library.objects,
1020			WholeStaticLibsFromPrebuilts: library.wholeStaticLibsFromPrebuilts,
1021
1022			TransitiveStaticLibrariesForOrdering: depset.NewBuilder[android.Path](depset.TOPOLOGICAL).
1023				Direct(outputFile).
1024				Transitive(deps.TranstiveStaticLibrariesForOrdering).
1025				Build(),
1026		})
1027	}
1028
1029	if library.header() {
1030		android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
1031	}
1032
1033	return outputFile
1034}
1035
1036func ndkSharedLibDeps(ctx ModuleContext) android.Paths {
1037	if ctx.Module().(*Module).IsSdkVariant() {
1038		// The NDK sysroot timestamp file depends on all the NDK
1039		// sysroot header and shared library files.
1040		return android.Paths{getNdkBaseTimestampFile(ctx)}
1041	}
1042	return nil
1043}
1044
1045func (library *libraryDecorator) linkShared(ctx ModuleContext,
1046	flags Flags, deps PathDeps, objs Objects) android.Path {
1047
1048	var linkerDeps android.Paths
1049	linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
1050	linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...)
1051
1052	unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
1053	forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
1054	forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
1055	if !ctx.Darwin() {
1056		if unexportedSymbols.Valid() {
1057			ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1058		}
1059		if forceNotWeakSymbols.Valid() {
1060			ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1061		}
1062		if forceWeakSymbols.Valid() {
1063			ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1064		}
1065	} else {
1066		if unexportedSymbols.Valid() {
1067			flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1068			linkerDeps = append(linkerDeps, unexportedSymbols.Path())
1069		}
1070		if forceNotWeakSymbols.Valid() {
1071			flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1072			linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
1073		}
1074		if forceWeakSymbols.Valid() {
1075			flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1076			linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
1077		}
1078	}
1079	if library.versionScriptPath.Valid() {
1080		linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
1081		flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlags)
1082		linkerDeps = append(linkerDeps, library.versionScriptPath.Path())
1083	}
1084
1085	fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
1086	outputFile := android.PathForModuleOut(ctx, fileName)
1087	unstrippedOutputFile := outputFile
1088
1089	var implicitOutputs android.WritablePaths
1090	if ctx.Windows() {
1091		importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib"))
1092
1093		flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--out-implib="+importLibraryPath.String())
1094		implicitOutputs = append(implicitOutputs, importLibraryPath)
1095	}
1096
1097	builderFlags := flagsToBuilderFlags(flags)
1098
1099	if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() {
1100		fatOutputFile := outputFile
1101		outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName)
1102		transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path())
1103	}
1104
1105	// Optimize out relinking against shared libraries whose interface hasn't changed by
1106	// depending on a table of contents file instead of the library itself.
1107	tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.ShlibSuffix()[1:]+".toc")
1108	library.tocFile = android.OptionalPathForPath(tocFile)
1109	TransformSharedObjectToToc(ctx, outputFile, tocFile)
1110
1111	stripFlags := flagsToStripFlags(flags)
1112	needsStrip := library.stripper.NeedsStrip(ctx)
1113	if library.buildStubs() {
1114		// No need to strip stubs libraries
1115		needsStrip = false
1116	}
1117	if needsStrip {
1118		if ctx.Darwin() {
1119			stripFlags.StripUseGnuStrip = true
1120		}
1121		strippedOutputFile := outputFile
1122		outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
1123		library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags)
1124	}
1125	library.unstrippedOutputFile = outputFile
1126
1127	outputFile = maybeInjectBoringSSLHash(ctx, outputFile, library.Properties.Inject_bssl_hash, fileName)
1128
1129	if Bool(library.baseLinker.Properties.Use_version_lib) {
1130		if ctx.Host() {
1131			versionedOutputFile := outputFile
1132			outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
1133			library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1134		} else {
1135			versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
1136			library.distFile = versionedOutputFile
1137
1138			if library.stripper.NeedsStrip(ctx) {
1139				out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
1140				library.distFile = out
1141				library.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags)
1142			}
1143
1144			library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
1145		}
1146	}
1147
1148	// Generate an output file for dist as if strip: "all" is set on the module.
1149	// Currently this is for layoutlib release process only.
1150	for _, dist := range ctx.Module().(*Module).Dists() {
1151		if dist.Tag != nil && *dist.Tag == "stripped_all" {
1152			strippedAllOutputFile := android.PathForModuleOut(ctx, "stripped_all", fileName)
1153			transformStrip(ctx, outputFile, strippedAllOutputFile, StripFlags{Toolchain: flags.Toolchain})
1154			library.strippedAllOutputFile = strippedAllOutputFile
1155			break
1156		}
1157	}
1158
1159	sharedLibs := deps.EarlySharedLibs
1160	sharedLibs = append(sharedLibs, deps.SharedLibs...)
1161	sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1162
1163	linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
1164	linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
1165	linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
1166
1167	if generatedLib := generateRustStaticlib(ctx, deps.RustRlibDeps); generatedLib != nil && !library.buildStubs() {
1168		if ctx.Module().(*Module).WholeRustStaticlib {
1169			deps.WholeStaticLibs = append(deps.WholeStaticLibs, generatedLib)
1170		} else {
1171			deps.StaticLibs = append(deps.StaticLibs, generatedLib)
1172		}
1173	}
1174
1175	transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
1176		deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin,
1177		deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs, objs.tidyDepFiles)
1178
1179	objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
1180	objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
1181	objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
1182	objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
1183
1184	library.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, library.getLibName(ctx))
1185	library.linkSAbiDumpFiles(ctx, deps, objs, fileName, unstrippedOutputFile)
1186
1187	var transitiveStaticLibrariesForOrdering depset.DepSet[android.Path]
1188	if static := ctx.GetDirectDepsWithTag(staticVariantTag); len(static) > 0 {
1189		s, _ := android.OtherModuleProvider(ctx, static[0], StaticLibraryInfoProvider)
1190		transitiveStaticLibrariesForOrdering = s.TransitiveStaticLibrariesForOrdering
1191	}
1192
1193	android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
1194		TableOfContents:                      android.OptionalPathForPath(tocFile),
1195		SharedLibrary:                        unstrippedOutputFile,
1196		TransitiveStaticLibrariesForOrdering: transitiveStaticLibrariesForOrdering,
1197		Target:                               ctx.Target(),
1198		IsStubs:                              library.buildStubs(),
1199	})
1200
1201	addStubDependencyProviders(ctx)
1202
1203	return unstrippedOutputFile
1204}
1205
1206// Visits the stub variants of the library and returns a struct containing the stub .so paths
1207func addStubDependencyProviders(ctx ModuleContext) []SharedStubLibrary {
1208	stubsInfo := []SharedStubLibrary{}
1209	stubs := ctx.GetDirectDepsWithTag(stubImplDepTag)
1210	if len(stubs) > 0 {
1211		for _, stub := range stubs {
1212			stubInfo, ok := android.OtherModuleProvider(ctx, stub, SharedLibraryInfoProvider)
1213			// TODO (b/275273834): Make this a hard error when the symbol files have been added to module sdk.
1214			if !ok {
1215				continue
1216			}
1217			flagInfo, _ := android.OtherModuleProvider(ctx, stub, FlagExporterInfoProvider)
1218			stubsInfo = append(stubsInfo, SharedStubLibrary{
1219				Version:           moduleLibraryInterface(stub).stubsVersion(),
1220				SharedLibraryInfo: stubInfo,
1221				FlagExporterInfo:  flagInfo,
1222			})
1223		}
1224		if len(stubsInfo) > 0 {
1225			android.SetProvider(ctx, SharedLibraryStubsProvider, SharedLibraryStubsInfo{
1226				SharedStubLibraries: stubsInfo,
1227				IsLLNDK:             ctx.IsLlndk(),
1228			})
1229		}
1230	}
1231	return stubsInfo
1232}
1233
1234func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
1235	return library.unstrippedOutputFile
1236}
1237
1238func (library *libraryDecorator) strippedAllOutputFilePath() android.Path {
1239	return library.strippedAllOutputFile
1240}
1241
1242func (library *libraryDecorator) disableStripping() {
1243	library.stripper.StripProperties.Strip.None = BoolPtr(true)
1244}
1245
1246func (library *libraryDecorator) nativeCoverage() bool {
1247	if library.header() || library.buildStubs() {
1248		return false
1249	}
1250	return true
1251}
1252
1253func (library *libraryDecorator) coverageOutputFilePath() android.OptionalPath {
1254	return library.coverageOutputFile
1255}
1256
1257func (library *libraryDecorator) exportedIncludeDirsForAbiCheck(ctx ModuleContext) []string {
1258	exportIncludeDirs := library.flagExporter.exportedIncludes(ctx).Strings()
1259	exportIncludeDirs = append(exportIncludeDirs, library.sabi.Properties.ReexportedIncludes...)
1260	exportSystemIncludeDirs := library.flagExporter.exportedSystemIncludes(ctx).Strings()
1261	exportSystemIncludeDirs = append(exportSystemIncludeDirs, library.sabi.Properties.ReexportedSystemIncludes...)
1262	// The ABI checker does not distinguish normal and system headers.
1263	return append(exportIncludeDirs, exportSystemIncludeDirs...)
1264}
1265
1266func (library *libraryDecorator) llndkIncludeDirsForAbiCheck(ctx ModuleContext, deps PathDeps) []string {
1267	var includeDirs, systemIncludeDirs []string
1268
1269	if library.Properties.Llndk.Override_export_include_dirs != nil {
1270		includeDirs = append(includeDirs, android.PathsForModuleSrc(
1271			ctx, library.Properties.Llndk.Override_export_include_dirs).Strings()...)
1272	} else {
1273		includeDirs = append(includeDirs, library.flagExporter.exportedIncludes(ctx).Strings()...)
1274		// Ignore library.sabi.Properties.ReexportedIncludes because
1275		// LLNDK does not reexport the implementation's dependencies, such as export_header_libs.
1276	}
1277
1278	systemIncludeDirs = append(systemIncludeDirs,
1279		library.flagExporter.exportedSystemIncludes(ctx).Strings()...)
1280	if Bool(library.Properties.Llndk.Export_headers_as_system) {
1281		systemIncludeDirs = append(systemIncludeDirs, includeDirs...)
1282		includeDirs = nil
1283	}
1284	// Header libs.
1285	includeDirs = append(includeDirs, deps.LlndkIncludeDirs.Strings()...)
1286	systemIncludeDirs = append(systemIncludeDirs, deps.LlndkSystemIncludeDirs.Strings()...)
1287	// The ABI checker does not distinguish normal and system headers.
1288	return append(includeDirs, systemIncludeDirs...)
1289}
1290
1291func (library *libraryDecorator) linkLlndkSAbiDumpFiles(ctx ModuleContext,
1292	deps PathDeps, sAbiDumpFiles android.Paths, soFile android.Path, libFileName string,
1293	excludeSymbolVersions, excludeSymbolTags []string,
1294	sdkVersionForVendorApiLevel string) android.Path {
1295	return transformDumpToLinkedDump(ctx,
1296		sAbiDumpFiles, soFile, libFileName+".llndk",
1297		library.llndkIncludeDirsForAbiCheck(ctx, deps),
1298		android.OptionalPathForModuleSrc(ctx, library.Properties.Llndk.Symbol_file),
1299		append([]string{"*_PLATFORM", "*_PRIVATE"}, excludeSymbolVersions...),
1300		append([]string{"platform-only"}, excludeSymbolTags...),
1301		[]string{"llndk"}, sdkVersionForVendorApiLevel)
1302}
1303
1304func (library *libraryDecorator) linkApexSAbiDumpFiles(ctx ModuleContext,
1305	deps PathDeps, sAbiDumpFiles android.Paths, soFile android.Path, libFileName string,
1306	excludeSymbolVersions, excludeSymbolTags []string,
1307	sdkVersion string) android.Path {
1308	return transformDumpToLinkedDump(ctx,
1309		sAbiDumpFiles, soFile, libFileName+".apex",
1310		library.exportedIncludeDirsForAbiCheck(ctx),
1311		android.OptionalPathForModuleSrc(ctx, library.Properties.Stubs.Symbol_file),
1312		append([]string{"*_PLATFORM", "*_PRIVATE"}, excludeSymbolVersions...),
1313		append([]string{"platform-only"}, excludeSymbolTags...),
1314		[]string{"apex", "systemapi"}, sdkVersion)
1315}
1316
1317func getRefAbiDumpFile(ctx android.ModuleInstallPathContext,
1318	versionedDumpDir, fileName string) android.OptionalPath {
1319
1320	currentArchType := ctx.Arch().ArchType
1321	primaryArchType := ctx.Config().DevicePrimaryArchType()
1322	archName := currentArchType.String()
1323	if currentArchType != primaryArchType {
1324		archName += "_" + primaryArchType.String()
1325	}
1326
1327	return android.ExistentPathForSource(ctx, versionedDumpDir, archName, "source-based",
1328		fileName+".lsdump")
1329}
1330
1331// Return the previous and current SDK versions for cross-version ABI diff.
1332func crossVersionAbiDiffSdkVersions(ctx ModuleContext, dumpDir string) (int, int) {
1333	sdkVersionInt := ctx.Config().PlatformSdkVersion().FinalInt()
1334
1335	if ctx.Config().PlatformSdkFinal() {
1336		return sdkVersionInt - 1, sdkVersionInt
1337	} else {
1338		// The platform SDK version can be upgraded before finalization while the corresponding abi dumps hasn't
1339		// been generated. Thus the Cross-Version Check chooses PLATFORM_SDK_VERION - 1 as previous version.
1340		// This situation could be identified by checking the existence of the PLATFORM_SDK_VERION dump directory.
1341		versionedDumpDir := android.ExistentPathForSource(ctx,
1342			dumpDir, ctx.Config().PlatformSdkVersion().String())
1343		if versionedDumpDir.Valid() {
1344			return sdkVersionInt, sdkVersionInt + 1
1345		} else {
1346			return sdkVersionInt - 1, sdkVersionInt
1347		}
1348	}
1349}
1350
1351// Return the SDK version for same-version ABI diff.
1352func currRefAbiDumpSdkVersion(ctx ModuleContext) string {
1353	if ctx.Config().PlatformSdkFinal() {
1354		// After sdk finalization, the ABI of the latest API level must be consistent with the source code,
1355		// so choose PLATFORM_SDK_VERSION as the current version.
1356		return ctx.Config().PlatformSdkVersion().String()
1357	} else {
1358		return "current"
1359	}
1360}
1361
1362// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.lsdump).
1363func (library *libraryDecorator) sourceAbiDiff(ctx android.ModuleContext,
1364	sourceDump, referenceDump android.Path,
1365	baseName, nameExt string, isLlndk, allowExtensions bool,
1366	sourceVersion, errorMessage string) {
1367
1368	extraFlags := []string{"-target-version", sourceVersion}
1369	headerAbiChecker := library.getHeaderAbiCheckerProperties(ctx.Module().(*Module))
1370	if Bool(headerAbiChecker.Check_all_apis) {
1371		extraFlags = append(extraFlags, "-check-all-apis")
1372	} else {
1373		extraFlags = append(extraFlags,
1374			"-allow-unreferenced-changes",
1375			"-allow-unreferenced-elf-symbol-changes")
1376	}
1377	if isLlndk {
1378		extraFlags = append(extraFlags, "-consider-opaque-types-different")
1379	}
1380	if allowExtensions {
1381		extraFlags = append(extraFlags, "-allow-extensions")
1382	}
1383	extraFlags = append(extraFlags, headerAbiChecker.Diff_flags...)
1384
1385	library.sAbiDiff = append(
1386		library.sAbiDiff,
1387		transformAbiDumpToAbiDiff(ctx, sourceDump, referenceDump,
1388			baseName, nameExt, extraFlags, errorMessage))
1389}
1390
1391func (library *libraryDecorator) crossVersionAbiDiff(ctx android.ModuleContext,
1392	sourceDump, referenceDump android.Path,
1393	baseName, nameExt string, isLlndk bool, sourceVersion, prevDumpDir string) {
1394
1395	errorMessage := "error: Please follow https://android.googlesource.com/platform/development/+/main/vndk/tools/header-checker/README.md#configure-cross_version-abi-check to resolve the difference between your source code and the ABI dumps in " + prevDumpDir
1396
1397	library.sourceAbiDiff(ctx, sourceDump, referenceDump, baseName, nameExt,
1398		isLlndk, true /* allowExtensions */, sourceVersion, errorMessage)
1399}
1400
1401func (library *libraryDecorator) sameVersionAbiDiff(ctx android.ModuleContext,
1402	sourceDump, referenceDump android.Path,
1403	baseName, nameExt string, isLlndk bool, lsdumpTagName string) {
1404
1405	libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
1406	errorMessage := "error: Please update ABI references with: $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py --lib " + libName + " --lib-variant " + lsdumpTagName
1407
1408	targetRelease := ctx.Config().Getenv("TARGET_RELEASE")
1409	if targetRelease != "" {
1410		errorMessage += " --release " + targetRelease
1411	}
1412
1413	library.sourceAbiDiff(ctx, sourceDump, referenceDump, baseName, nameExt,
1414		isLlndk, false /* allowExtensions */, "current", errorMessage)
1415}
1416
1417func (library *libraryDecorator) optInAbiDiff(ctx android.ModuleContext,
1418	sourceDump, referenceDump android.Path,
1419	baseName, nameExt string, refDumpDir string, lsdumpTagName string) {
1420
1421	libName := strings.TrimSuffix(baseName, filepath.Ext(baseName))
1422	errorMessage := "error: Please update ABI references with: $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py --lib " + libName + " --lib-variant " + lsdumpTagName + " --ref-dump-dir $$ANDROID_BUILD_TOP/" + refDumpDir
1423
1424	targetRelease := ctx.Config().Getenv("TARGET_RELEASE")
1425	if targetRelease != "" {
1426		errorMessage += " --release " + targetRelease
1427	}
1428
1429	// Most opt-in libraries do not have dumps for all default architectures.
1430	if ctx.Config().HasDeviceProduct() {
1431		errorMessage += " --product " + ctx.Config().DeviceProduct()
1432	}
1433
1434	library.sourceAbiDiff(ctx, sourceDump, referenceDump, baseName, nameExt,
1435		false /* isLlndk */, false /* allowExtensions */, "current", errorMessage)
1436}
1437
1438func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, deps PathDeps, objs Objects, fileName string, soFile android.Path) {
1439	if library.sabi.shouldCreateSourceAbiDump() {
1440		exportedIncludeDirs := library.exportedIncludeDirsForAbiCheck(ctx)
1441		headerAbiChecker := library.getHeaderAbiCheckerProperties(ctx.Module().(*Module))
1442		currSdkVersion := currRefAbiDumpSdkVersion(ctx)
1443		currVendorVersion := ctx.Config().VendorApiLevel()
1444
1445		// Generate source dumps.
1446		implDump := transformDumpToLinkedDump(ctx,
1447			objs.sAbiDumpFiles, soFile, fileName,
1448			exportedIncludeDirs,
1449			android.OptionalPathForModuleSrc(ctx, library.symbolFileForAbiCheck(ctx)),
1450			headerAbiChecker.Exclude_symbol_versions,
1451			headerAbiChecker.Exclude_symbol_tags,
1452			[]string{} /* includeSymbolTags */, currSdkVersion)
1453
1454		var llndkDump, apexVariantDump android.Path
1455		tags := classifySourceAbiDump(ctx.Module().(*Module))
1456		optInTags := []lsdumpTag{}
1457		for _, tag := range tags {
1458			if tag == llndkLsdumpTag && currVendorVersion != "" {
1459				if llndkDump == nil {
1460					sdkVersion, err := android.GetSdkVersionForVendorApiLevel(currVendorVersion)
1461					if err != nil {
1462						ctx.ModuleErrorf("Cannot create %s llndk dump: %s", fileName, err)
1463						return
1464					}
1465					// TODO(b/323447559): Evaluate if replacing sAbiDumpFiles with implDump is faster
1466					llndkDump = library.linkLlndkSAbiDumpFiles(ctx,
1467						deps, objs.sAbiDumpFiles, soFile, fileName,
1468						headerAbiChecker.Exclude_symbol_versions,
1469						headerAbiChecker.Exclude_symbol_tags,
1470						nativeClampedApiLevel(ctx, sdkVersion).String())
1471				}
1472				addLsdumpPath(ctx.Config(), string(tag)+":"+llndkDump.String())
1473			} else if tag == apexLsdumpTag {
1474				if apexVariantDump == nil {
1475					apexVariantDump = library.linkApexSAbiDumpFiles(ctx,
1476						deps, objs.sAbiDumpFiles, soFile, fileName,
1477						headerAbiChecker.Exclude_symbol_versions,
1478						headerAbiChecker.Exclude_symbol_tags,
1479						currSdkVersion)
1480				}
1481				addLsdumpPath(ctx.Config(), string(tag)+":"+apexVariantDump.String())
1482			} else {
1483				if tag.dirName() == "" {
1484					optInTags = append(optInTags, tag)
1485				}
1486				addLsdumpPath(ctx.Config(), string(tag)+":"+implDump.String())
1487			}
1488		}
1489
1490		// Diff source dumps and reference dumps.
1491		for _, tag := range tags {
1492			dumpDirName := tag.dirName()
1493			if dumpDirName == "" {
1494				continue
1495			}
1496			dumpDir := filepath.Join("prebuilts", "abi-dumps", dumpDirName)
1497			isLlndk := (tag == llndkLsdumpTag)
1498			isApex := (tag == apexLsdumpTag)
1499			binderBitness := ctx.DeviceConfig().BinderBitness()
1500			nameExt := ""
1501			if isLlndk {
1502				nameExt = "llndk"
1503			} else if isApex {
1504				nameExt = "apex"
1505			}
1506			// Check against the previous version.
1507			var prevVersion, currVersion string
1508			sourceDump := implDump
1509			// If this release config does not define VendorApiLevel, fall back to the old policy.
1510			if isLlndk && currVendorVersion != "" {
1511				prevVersion = ctx.Config().PrevVendorApiLevel()
1512				currVersion = currVendorVersion
1513				// LLNDK dumps are generated by different rules after trunk stable.
1514				if android.IsTrunkStableVendorApiLevel(prevVersion) {
1515					sourceDump = llndkDump
1516				}
1517			} else {
1518				prevVersionInt, currVersionInt := crossVersionAbiDiffSdkVersions(ctx, dumpDir)
1519				prevVersion = strconv.Itoa(prevVersionInt)
1520				currVersion = strconv.Itoa(currVersionInt)
1521				// APEX dumps are generated by different rules after trunk stable.
1522				if isApex && prevVersionInt > 34 {
1523					sourceDump = apexVariantDump
1524				}
1525			}
1526			prevDumpDir := filepath.Join(dumpDir, prevVersion, binderBitness)
1527			prevDumpFile := getRefAbiDumpFile(ctx, prevDumpDir, fileName)
1528			if prevDumpFile.Valid() {
1529				library.crossVersionAbiDiff(ctx, sourceDump, prevDumpFile.Path(),
1530					fileName, nameExt+prevVersion, isLlndk, currVersion, prevDumpDir)
1531			}
1532			// Check against the current version.
1533			sourceDump = implDump
1534			if isLlndk && currVendorVersion != "" {
1535				currVersion = currVendorVersion
1536				if android.IsTrunkStableVendorApiLevel(currVersion) {
1537					sourceDump = llndkDump
1538				}
1539			} else {
1540				currVersion = currSdkVersion
1541				if isApex && (!ctx.Config().PlatformSdkFinal() ||
1542					ctx.Config().PlatformSdkVersion().FinalInt() > 34) {
1543					sourceDump = apexVariantDump
1544				}
1545			}
1546			currDumpDir := filepath.Join(dumpDir, currVersion, binderBitness)
1547			currDumpFile := getRefAbiDumpFile(ctx, currDumpDir, fileName)
1548			if currDumpFile.Valid() {
1549				library.sameVersionAbiDiff(ctx, sourceDump, currDumpFile.Path(),
1550					fileName, nameExt, isLlndk, string(tag))
1551			}
1552		}
1553
1554		// Assert that a module is tagged with at most one of platformLsdumpTag, productLsdumpTag, or vendorLsdumpTag.
1555		if len(headerAbiChecker.Ref_dump_dirs) > 0 && len(optInTags) != 1 {
1556			ctx.ModuleErrorf("Expect exactly one opt-in lsdump tag when ref_dump_dirs are specified: %s", optInTags)
1557			return
1558		}
1559		// Ensure that a module tagged with only platformLsdumpTag has ref_dump_dirs.
1560		// Android.bp in vendor projects should be cleaned up before this is enforced for vendorLsdumpTag and productLsdumpTag.
1561		if len(headerAbiChecker.Ref_dump_dirs) == 0 && len(tags) == 1 && tags[0] == platformLsdumpTag {
1562			ctx.ModuleErrorf("header_abi_checker is explicitly enabled, but no ref_dump_dirs are specified.")
1563		}
1564		// Check against the opt-in reference dumps.
1565		for i, optInDumpDir := range headerAbiChecker.Ref_dump_dirs {
1566			optInDumpDirPath := android.PathForModuleSrc(ctx, optInDumpDir)
1567			// Ref_dump_dirs are not versioned.
1568			// They do not contain subdir for binder bitness because 64-bit binder has been mandatory.
1569			optInDumpFile := getRefAbiDumpFile(ctx, optInDumpDirPath.String(), fileName)
1570			if !optInDumpFile.Valid() {
1571				continue
1572			}
1573			library.optInAbiDiff(ctx,
1574				implDump, optInDumpFile.Path(),
1575				fileName, "opt"+strconv.Itoa(i), optInDumpDirPath.String(), string(optInTags[0]))
1576		}
1577	}
1578}
1579
1580// link registers actions to link this library, and sets various fields
1581// on this library to reflect information that should be exported up the build
1582// tree (for example, exported flags and include paths).
1583func (library *libraryDecorator) link(ctx ModuleContext,
1584	flags Flags, deps PathDeps, objs Objects) android.Path {
1585
1586	if ctx.IsLlndk() {
1587		// override the module's export_include_dirs with llndk.override_export_include_dirs
1588		// if it is set.
1589		if override := library.Properties.Llndk.Override_export_include_dirs; override != nil {
1590			library.flagExporter.Properties.Export_include_dirs = proptools.NewConfigurable[[]string](
1591				nil,
1592				[]proptools.ConfigurableCase[[]string]{
1593					proptools.NewConfigurableCase[[]string](nil, &override),
1594				},
1595			)
1596		}
1597
1598		if Bool(library.Properties.Llndk.Export_headers_as_system) {
1599			library.flagExporter.Properties.Export_system_include_dirs = append(
1600				library.flagExporter.Properties.Export_system_include_dirs,
1601				library.flagExporter.Properties.Export_include_dirs.GetOrDefault(ctx, nil)...)
1602			library.flagExporter.Properties.Export_include_dirs = proptools.NewConfigurable[[]string](nil, nil)
1603		}
1604	}
1605
1606	if ctx.IsVendorPublicLibrary() {
1607		// override the module's export_include_dirs with vendor_public_library.override_export_include_dirs
1608		// if it is set.
1609		if override := library.Properties.Vendor_public_library.Override_export_include_dirs; override != nil {
1610			library.flagExporter.Properties.Export_include_dirs = proptools.NewConfigurable[[]string](
1611				nil,
1612				[]proptools.ConfigurableCase[[]string]{
1613					proptools.NewConfigurableCase[[]string](nil, &override),
1614				},
1615			)
1616		}
1617	}
1618
1619	// Linking this library consists of linking `deps.Objs` (.o files in dependencies
1620	// of this library), together with `objs` (.o files created by compiling this
1621	// library).
1622	objs = deps.Objs.Copy().Append(objs)
1623	var out android.Path
1624	if library.static() || library.header() {
1625		out = library.linkStatic(ctx, flags, deps, objs)
1626	} else {
1627		out = library.linkShared(ctx, flags, deps, objs)
1628	}
1629
1630	// Export include paths and flags to be propagated up the tree.
1631	library.exportIncludes(ctx)
1632	library.reexportDirs(deps.ReexportedDirs...)
1633	library.reexportSystemDirs(deps.ReexportedSystemDirs...)
1634	library.reexportFlags(deps.ReexportedFlags...)
1635	library.reexportDeps(deps.ReexportedDeps...)
1636	library.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
1637
1638	if library.static() && len(deps.ReexportedRustRlibDeps) > 0 {
1639		library.reexportRustStaticDeps(deps.ReexportedRustRlibDeps...)
1640	}
1641
1642	// Optionally export aidl headers.
1643	if Bool(library.Properties.Aidl.Export_aidl_headers) {
1644		if library.baseCompiler.hasAidl(ctx, deps) {
1645			if library.baseCompiler.hasSrcExt(ctx, ".aidl") {
1646				dir := android.PathForModuleGen(ctx, "aidl")
1647				library.reexportDirs(dir)
1648			}
1649			if len(deps.AidlLibraryInfos) > 0 {
1650				dir := android.PathForModuleGen(ctx, "aidl_library")
1651				library.reexportDirs(dir)
1652			}
1653
1654			library.reexportDeps(library.baseCompiler.aidlOrderOnlyDeps...)
1655			library.addExportedGeneratedHeaders(library.baseCompiler.aidlHeaders...)
1656		}
1657	}
1658
1659	// Optionally export proto headers.
1660	if Bool(library.Properties.Proto.Export_proto_headers) {
1661		if library.baseCompiler.hasSrcExt(ctx, ".proto") {
1662			var includes android.Paths
1663			if flags.proto.CanonicalPathFromRoot {
1664				includes = append(includes, flags.proto.SubDir)
1665			}
1666			includes = append(includes, flags.proto.Dir)
1667			library.reexportDirs(includes...)
1668
1669			library.reexportDeps(library.baseCompiler.protoOrderOnlyDeps...)
1670			library.addExportedGeneratedHeaders(library.baseCompiler.protoHeaders...)
1671		}
1672	}
1673
1674	// If the library is sysprop_library, expose either public or internal header selectively.
1675	if library.baseCompiler.hasSrcExt(ctx, ".sysprop") {
1676		dir := android.PathForModuleGen(ctx, "sysprop", "include")
1677		if library.Properties.Sysprop.Platform != nil {
1678			isOwnerPlatform := Bool(library.Properties.Sysprop.Platform)
1679
1680			// If the owner is different from the user, expose public header. That is,
1681			// 1) if the user is product (as owner can only be platform / vendor)
1682			// 2) if the owner is platform and the client is vendor
1683			// We don't care Platform -> Vendor dependency as it's already forbidden.
1684			if ctx.Device() && (ctx.ProductSpecific() || (isOwnerPlatform && ctx.inVendor())) {
1685				dir = android.PathForModuleGen(ctx, "sysprop/public", "include")
1686			}
1687		}
1688
1689		// Make sure to only export headers which are within the include directory.
1690		_, headers := android.FilterPathListPredicate(library.baseCompiler.syspropHeaders, func(path android.Path) bool {
1691			_, isRel := android.MaybeRel(ctx, dir.String(), path.String())
1692			return isRel
1693		})
1694
1695		// Add sysprop-related directories to the exported directories of this library.
1696		library.reexportDirs(dir)
1697		library.reexportDeps(library.baseCompiler.syspropOrderOnlyDeps...)
1698		library.addExportedGeneratedHeaders(headers...)
1699	}
1700
1701	// Add stub-related flags if this library is a stub library.
1702	library.exportVersioningMacroIfNeeded(ctx)
1703
1704	// Propagate a Provider containing information about exported flags, deps, and include paths.
1705	library.flagExporter.setProvider(ctx)
1706
1707	return out
1708}
1709
1710func (library *libraryDecorator) exportVersioningMacroIfNeeded(ctx android.BaseModuleContext) {
1711	if library.buildStubs() && library.stubsVersion() != "" && !library.skipAPIDefine {
1712		name := versioningMacroName(ctx.Module().(*Module).ImplementationModuleName(ctx))
1713		apiLevel, err := android.ApiLevelFromUser(ctx, library.stubsVersion())
1714		if err != nil {
1715			ctx.ModuleErrorf("Can't export version macro: %s", err.Error())
1716		}
1717		library.reexportFlags("-D" + name + "=" + strconv.Itoa(apiLevel.FinalOrPreviewInt()))
1718	}
1719}
1720
1721// buildStatic returns true if this library should be built as a static library.
1722func (library *libraryDecorator) buildStatic() bool {
1723	return library.MutatedProperties.BuildStatic &&
1724		BoolDefault(library.StaticProperties.Static.Enabled, true)
1725}
1726
1727// buildShared returns true if this library should be built as a shared library.
1728func (library *libraryDecorator) buildShared() bool {
1729	return library.MutatedProperties.BuildShared &&
1730		BoolDefault(library.SharedProperties.Shared.Enabled, true)
1731}
1732
1733func (library *libraryDecorator) objs() Objects {
1734	return library.objects
1735}
1736
1737func (library *libraryDecorator) reuseObjs() Objects {
1738	return library.reuseObjects
1739}
1740
1741func (library *libraryDecorator) toc() android.OptionalPath {
1742	return library.tocFile
1743}
1744
1745func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) {
1746	dir := library.baseInstaller.installDir(ctx)
1747	dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir)
1748	// libc_hwasan has relative_install_dir set, which would mess up the dir.Base() logic.
1749	// hardcode here because it's the only target, if we have other targets that use this
1750	// we can generalise this.
1751	var target string
1752	if ctx.baseModuleName() == "libc_hwasan" {
1753		target = "/" + filepath.Join("apex", "com.android.runtime", "lib64", "bionic", "hwasan", file.Base())
1754	} else {
1755		base := dir.Base()
1756		target = "/" + filepath.Join("apex", "com.android.runtime", base, "bionic", file.Base())
1757	}
1758	ctx.InstallAbsoluteSymlink(dir, file.Base(), target)
1759	library.postInstallCmds = append(library.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target))
1760}
1761
1762func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
1763	if library.shared() {
1764		translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled
1765		if library.hasStubsVariants() && !ctx.Host() && !ctx.isSdkVariant() &&
1766			InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !library.buildStubs() &&
1767			!translatedArch && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() {
1768			// Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory.
1769			// The original path becomes a symlink to the corresponding file in the
1770			// runtime APEX.
1771			if ctx.Device() {
1772				library.installSymlinkToRuntimeApex(ctx, file)
1773			}
1774			library.baseInstaller.subDir = "bootstrap"
1775		}
1776
1777		library.baseInstaller.install(ctx, file)
1778	}
1779
1780	if Bool(library.Properties.Static_ndk_lib) && library.static() &&
1781		!ctx.InVendorOrProduct() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() && ctx.Device() &&
1782		library.baseLinker.sanitize.isUnsanitizedVariant() &&
1783		ctx.isForPlatform() && !ctx.isPreventInstall() {
1784		installPath := getUnversionedLibraryInstallPath(ctx).Join(ctx, file.Base())
1785
1786		ctx.ModuleBuild(pctx, android.ModuleBuildParams{
1787			Rule:        android.Cp,
1788			Description: "install " + installPath.Base(),
1789			Output:      installPath,
1790			Input:       file,
1791		})
1792
1793		library.ndkSysrootPath = installPath
1794	}
1795}
1796
1797func (library *libraryDecorator) everInstallable() bool {
1798	// Only shared and static libraries are installed. Header libraries (which are
1799	// neither static or shared) are not installed.
1800	return library.shared() || library.static()
1801}
1802
1803// static returns true if this library is for a "static' variant.
1804func (library *libraryDecorator) static() bool {
1805	return library.MutatedProperties.VariantIsStatic
1806}
1807
1808// shared returns true if this library is for a "shared' variant.
1809func (library *libraryDecorator) shared() bool {
1810	return library.MutatedProperties.VariantIsShared
1811}
1812
1813// header returns true if this library is for a header-only variant.
1814func (library *libraryDecorator) header() bool {
1815	// Neither "static" nor "shared" implies this library is header-only.
1816	return !library.static() && !library.shared()
1817}
1818
1819// setStatic marks the library variant as "static".
1820func (library *libraryDecorator) setStatic() {
1821	library.MutatedProperties.VariantIsStatic = true
1822	library.MutatedProperties.VariantIsShared = false
1823}
1824
1825// setShared marks the library variant as "shared".
1826func (library *libraryDecorator) setShared() {
1827	library.MutatedProperties.VariantIsStatic = false
1828	library.MutatedProperties.VariantIsShared = true
1829}
1830
1831// BuildOnlyStatic disables building this library as a shared library.
1832func (library *libraryDecorator) BuildOnlyStatic() {
1833	library.MutatedProperties.BuildShared = false
1834}
1835
1836// BuildOnlyShared disables building this library as a static library.
1837func (library *libraryDecorator) BuildOnlyShared() {
1838	library.MutatedProperties.BuildStatic = false
1839}
1840
1841// HeaderOnly disables building this library as a shared or static library;
1842// the library only exists to propagate header file dependencies up the build graph.
1843func (library *libraryDecorator) HeaderOnly() {
1844	library.MutatedProperties.BuildShared = false
1845	library.MutatedProperties.BuildStatic = false
1846}
1847
1848// hasLLNDKStubs returns true if this cc_library module has a variant that will build LLNDK stubs.
1849func (library *libraryDecorator) hasLLNDKStubs() bool {
1850	return String(library.Properties.Llndk.Symbol_file) != ""
1851}
1852
1853// hasLLNDKStubs returns true if this cc_library module has a variant that will build LLNDK stubs.
1854func (library *libraryDecorator) hasLLNDKHeaders() bool {
1855	return Bool(library.Properties.Llndk.Llndk_headers)
1856}
1857
1858// isLLNDKMovedToApex returns true if this cc_library module sets the llndk.moved_to_apex property.
1859func (library *libraryDecorator) isLLNDKMovedToApex() bool {
1860	return Bool(library.Properties.Llndk.Moved_to_apex)
1861}
1862
1863// hasVendorPublicLibrary returns true if this cc_library module has a variant that will build
1864// vendor public library stubs.
1865func (library *libraryDecorator) hasVendorPublicLibrary() bool {
1866	return String(library.Properties.Vendor_public_library.Symbol_file) != ""
1867}
1868
1869func (library *libraryDecorator) implementationModuleName(name string) string {
1870	return name
1871}
1872
1873func (library *libraryDecorator) buildStubs() bool {
1874	return library.MutatedProperties.BuildStubs
1875}
1876
1877func (library *libraryDecorator) symbolFileForAbiCheck(ctx ModuleContext) *string {
1878	if props := library.getHeaderAbiCheckerProperties(ctx.Module().(*Module)); props.Symbol_file != nil {
1879		return props.Symbol_file
1880	}
1881	if library.hasStubsVariants() && library.Properties.Stubs.Symbol_file != nil {
1882		return library.Properties.Stubs.Symbol_file
1883	}
1884	// TODO(b/309880485): Distinguish platform, NDK, LLNDK, and APEX version scripts.
1885	if library.baseLinker.Properties.Version_script != nil {
1886		return library.baseLinker.Properties.Version_script
1887	}
1888	return nil
1889}
1890
1891func (library *libraryDecorator) hasStubsVariants() bool {
1892	// Just having stubs.symbol_file is enough to create a stub variant. In that case
1893	// the stub for the future API level is created.
1894	return library.Properties.Stubs.Symbol_file != nil ||
1895		len(library.Properties.Stubs.Versions) > 0
1896}
1897
1898func (library *libraryDecorator) isStubsImplementationRequired() bool {
1899	return BoolDefault(library.Properties.Stubs.Implementation_installable, true)
1900}
1901
1902func (library *libraryDecorator) stubsVersions(ctx android.BaseModuleContext) []string {
1903	if !library.hasStubsVariants() {
1904		return nil
1905	}
1906
1907	if library.hasLLNDKStubs() && ctx.Module().(*Module).InVendorOrProduct() {
1908		// LLNDK libraries only need a single stubs variant (""), which is
1909		// added automatically in createVersionVariations().
1910		return nil
1911	}
1912
1913	// Future API level is implicitly added if there isn't
1914	versions := addCurrentVersionIfNotPresent(library.Properties.Stubs.Versions)
1915	normalizeVersions(ctx, versions)
1916	return versions
1917}
1918
1919func addCurrentVersionIfNotPresent(vers []string) []string {
1920	if inList(android.FutureApiLevel.String(), vers) {
1921		return vers
1922	}
1923	// In some cases, people use the raw value "10000" in the versions property.
1924	// We shouldn't add the future API level in that case, otherwise there will
1925	// be two identical versions.
1926	if inList(strconv.Itoa(android.FutureApiLevel.FinalOrFutureInt()), vers) {
1927		return vers
1928	}
1929	return append(vers, android.FutureApiLevel.String())
1930}
1931
1932func (library *libraryDecorator) setStubsVersion(version string) {
1933	library.MutatedProperties.StubsVersion = version
1934}
1935
1936func (library *libraryDecorator) stubsVersion() string {
1937	return library.MutatedProperties.StubsVersion
1938}
1939
1940func (library *libraryDecorator) setBuildStubs(isLatest bool) {
1941	library.MutatedProperties.BuildStubs = true
1942	library.MutatedProperties.IsLatestVersion = isLatest
1943}
1944
1945func (library *libraryDecorator) setAllStubsVersions(versions []string) {
1946	library.MutatedProperties.AllStubsVersions = versions
1947}
1948
1949func (library *libraryDecorator) allStubsVersions() []string {
1950	return library.MutatedProperties.AllStubsVersions
1951}
1952
1953func (library *libraryDecorator) isLatestStubVersion() bool {
1954	return library.MutatedProperties.IsLatestVersion
1955}
1956
1957func (library *libraryDecorator) availableFor(what string) bool {
1958	var list []string
1959	if library.static() {
1960		list = library.StaticProperties.Static.Apex_available
1961	} else if library.shared() {
1962		list = library.SharedProperties.Shared.Apex_available
1963	}
1964	if len(list) == 0 {
1965		return false
1966	}
1967	return android.CheckAvailableForApex(what, list)
1968}
1969
1970func (library *libraryDecorator) installable() *bool {
1971	if library.static() {
1972		return library.StaticProperties.Static.Installable
1973	} else if library.shared() {
1974		return library.SharedProperties.Shared.Installable
1975	}
1976	return nil
1977}
1978
1979func (library *libraryDecorator) makeUninstallable(mod *Module) {
1980	if library.static() && library.buildStatic() && !library.buildStubs() {
1981		// If we're asked to make a static library uninstallable we don't do
1982		// anything since AndroidMkEntries always sets LOCAL_UNINSTALLABLE_MODULE
1983		// for these entries. This is done to still get the make targets for NOTICE
1984		// files from notice_files.mk, which other libraries might depend on.
1985		return
1986	}
1987	mod.ModuleBase.MakeUninstallable()
1988}
1989
1990func (library *libraryDecorator) getPartition() string {
1991	return library.path.Partition()
1992}
1993
1994func (library *libraryDecorator) getAPIListCoverageXMLPath() android.ModuleOutPath {
1995	return library.apiListCoverageXmlPath
1996}
1997
1998func (library *libraryDecorator) overriddenModules() []string {
1999	return library.Properties.Overrides
2000}
2001
2002var _ overridable = (*libraryDecorator)(nil)
2003
2004var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
2005
2006// versioningMacroNamesList returns a singleton map, where keys are "version macro names",
2007// and values are the module name responsible for registering the version macro name.
2008//
2009// Version macros are used when building against stubs, to provide version information about
2010// the stub. Only stub libraries should have an entry in this list.
2011//
2012// For example, when building against libFoo#ver, __LIBFOO_API__ macro is set to ver so
2013// that headers from libFoo can be conditionally compiled (this may hide APIs
2014// that are not available for the version).
2015//
2016// This map is used to ensure that there aren't conflicts between these version macro names.
2017func versioningMacroNamesList(config android.Config) *map[string]string {
2018	return config.Once(versioningMacroNamesListKey, func() interface{} {
2019		m := make(map[string]string)
2020		return &m
2021	}).(*map[string]string)
2022}
2023
2024// alphanumeric and _ characters are preserved.
2025// other characters are all converted to _
2026var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
2027
2028// versioningMacroName returns the canonical version macro name for the given module.
2029func versioningMacroName(moduleName string) string {
2030	macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
2031	macroName = strings.ToUpper(macroName)
2032	return "__" + macroName + "_API__"
2033}
2034
2035// NewLibrary builds and returns a new Module corresponding to a C++ library.
2036// Individual module implementations which comprise a C++ library (or something like
2037// a C++ library) should call this function, set some fields on the result, and
2038// then call the Init function.
2039func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
2040	module := newModule(hod, android.MultilibBoth)
2041
2042	library := &libraryDecorator{
2043		MutatedProperties: LibraryMutatedProperties{
2044			BuildShared: true,
2045			BuildStatic: true,
2046		},
2047		baseCompiler:  NewBaseCompiler(),
2048		baseLinker:    NewBaseLinker(module.sanitize),
2049		baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
2050		sabi:          module.sabi,
2051	}
2052
2053	module.compiler = library
2054	module.linker = library
2055	module.installer = library
2056	module.library = library
2057
2058	return module, library
2059}
2060
2061// connects a shared library to a static library in order to reuse its .o files to avoid
2062// compiling source files twice.
2063func reuseStaticLibrary(ctx android.BottomUpMutatorContext, shared *Module) {
2064	if sharedCompiler, ok := shared.compiler.(*libraryDecorator); ok {
2065
2066		// Check libraries in addition to cflags, since libraries may be exporting different
2067		// include directories.
2068		if len(sharedCompiler.StaticProperties.Static.Cflags.GetOrDefault(ctx, nil)) == 0 &&
2069			len(sharedCompiler.SharedProperties.Shared.Cflags.GetOrDefault(ctx, nil)) == 0 &&
2070			len(sharedCompiler.StaticProperties.Static.Whole_static_libs.GetOrDefault(ctx, nil)) == 0 &&
2071			len(sharedCompiler.SharedProperties.Shared.Whole_static_libs.GetOrDefault(ctx, nil)) == 0 &&
2072			len(sharedCompiler.StaticProperties.Static.Static_libs.GetOrDefault(ctx, nil)) == 0 &&
2073			len(sharedCompiler.SharedProperties.Shared.Static_libs.GetOrDefault(ctx, nil)) == 0 &&
2074			len(sharedCompiler.StaticProperties.Static.Shared_libs.GetOrDefault(ctx, nil)) == 0 &&
2075			len(sharedCompiler.SharedProperties.Shared.Shared_libs.GetOrDefault(ctx, nil)) == 0 &&
2076			// Compare System_shared_libs properties with nil because empty lists are
2077			// semantically significant for them.
2078			sharedCompiler.StaticProperties.Static.System_shared_libs == nil &&
2079			sharedCompiler.SharedProperties.Shared.System_shared_libs == nil {
2080
2081			ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, reuseObjTag, ctx.ModuleName())
2082		}
2083
2084		// This dep is just to reference static variant from shared variant
2085		ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticVariantTag, ctx.ModuleName())
2086	}
2087}
2088
2089// linkageTransitionMutator adds "static" or "shared" variants for modules depending
2090// on whether the module can be built as a static library or a shared library.
2091type linkageTransitionMutator struct{}
2092
2093func (linkageTransitionMutator) Split(ctx android.BaseModuleContext) []string {
2094	ccPrebuilt := false
2095	if m, ok := ctx.Module().(*Module); ok && m.linker != nil {
2096		_, ccPrebuilt = m.linker.(prebuiltLibraryInterface)
2097	}
2098	if ccPrebuilt {
2099		library := ctx.Module().(*Module).linker.(prebuiltLibraryInterface)
2100
2101		// Differentiate between header only and building an actual static/shared library
2102		buildStatic := library.buildStatic()
2103		buildShared := library.buildShared()
2104		if buildStatic || buildShared {
2105			// Always create both the static and shared variants for prebuilt libraries, and then disable the one
2106			// that is not being used.  This allows them to share the name of a cc_library module, which requires that
2107			// all the variants of the cc_library also exist on the prebuilt.
2108			return []string{"static", "shared"}
2109		} else {
2110			// Header only
2111		}
2112	} else if library, ok := ctx.Module().(LinkableInterface); ok && (library.CcLibraryInterface() || library.RustLibraryInterface()) {
2113		// Non-cc.Modules may need an empty variant for their mutators.
2114		variations := []string{}
2115		if library.NonCcVariants() {
2116			variations = append(variations, "")
2117		}
2118		isLLNDK := false
2119		if m, ok := ctx.Module().(*Module); ok {
2120			isLLNDK = m.IsLlndk()
2121		}
2122		buildStatic := library.BuildStaticVariant() && !isLLNDK
2123		buildShared := library.BuildSharedVariant()
2124		if buildStatic && buildShared {
2125			variations = append([]string{"static", "shared"}, variations...)
2126			return variations
2127		} else if buildStatic {
2128			variations = append([]string{"static"}, variations...)
2129		} else if buildShared {
2130			variations = append([]string{"shared"}, variations...)
2131		}
2132
2133		if len(variations) > 0 {
2134			return variations
2135		}
2136	}
2137	return []string{""}
2138}
2139
2140func (linkageTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
2141	return ""
2142}
2143
2144func (linkageTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
2145	ccPrebuilt := false
2146	if m, ok := ctx.Module().(*Module); ok && m.linker != nil {
2147		_, ccPrebuilt = m.linker.(prebuiltLibraryInterface)
2148	}
2149	if ccPrebuilt {
2150		if incomingVariation != "" {
2151			return incomingVariation
2152		}
2153		library := ctx.Module().(*Module).linker.(prebuiltLibraryInterface)
2154		if library.buildShared() {
2155			return "shared"
2156		} else if library.buildStatic() {
2157			return "static"
2158		}
2159		return ""
2160	} else if library, ok := ctx.Module().(LinkableInterface); ok && library.CcLibraryInterface() {
2161		isLLNDK := false
2162		if m, ok := ctx.Module().(*Module); ok {
2163			isLLNDK = m.IsLlndk()
2164		}
2165		buildStatic := library.BuildStaticVariant() && !isLLNDK
2166		buildShared := library.BuildSharedVariant()
2167		if library.BuildRlibVariant() && library.IsRustFFI() && !buildStatic && (incomingVariation == "static" || incomingVariation == "") {
2168			// Rust modules do not build static libs, but rlibs are used as if they
2169			// were via `static_libs`. Thus we need to alias the BuildRlibVariant
2170			// to "static" for Rust FFI libraries.
2171			return ""
2172		}
2173		if incomingVariation != "" {
2174			return incomingVariation
2175		}
2176		if buildShared {
2177			return "shared"
2178		} else if buildStatic {
2179			return "static"
2180		}
2181		return ""
2182	}
2183	return ""
2184}
2185
2186func (linkageTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
2187	ccPrebuilt := false
2188	if m, ok := ctx.Module().(*Module); ok && m.linker != nil {
2189		_, ccPrebuilt = m.linker.(prebuiltLibraryInterface)
2190	}
2191	if ccPrebuilt {
2192		library := ctx.Module().(*Module).linker.(prebuiltLibraryInterface)
2193		if variation == "static" {
2194			library.setStatic()
2195			if !library.buildStatic() {
2196				library.disablePrebuilt()
2197			}
2198		} else if variation == "shared" {
2199			library.setShared()
2200			if !library.buildShared() {
2201				library.disablePrebuilt()
2202			}
2203		}
2204	} else if library, ok := ctx.Module().(LinkableInterface); ok && library.CcLibraryInterface() {
2205		if variation == "static" {
2206			library.SetStatic()
2207		} else if variation == "shared" {
2208			library.SetShared()
2209			var isLLNDK bool
2210			if m, ok := ctx.Module().(*Module); ok {
2211				isLLNDK = m.IsLlndk()
2212			}
2213			buildStatic := library.BuildStaticVariant() && !isLLNDK
2214			buildShared := library.BuildSharedVariant()
2215			if buildStatic && buildShared {
2216				if _, ok := library.(*Module); ok {
2217					reuseStaticLibrary(ctx, library.(*Module))
2218				}
2219			}
2220		}
2221	}
2222}
2223
2224// normalizeVersions modifies `versions` in place, so that each raw version
2225// string becomes its normalized canonical form.
2226// Validates that the versions in `versions` are specified in least to greatest order.
2227func normalizeVersions(ctx android.BaseModuleContext, versions []string) {
2228	var previous android.ApiLevel
2229	for i, v := range versions {
2230		ver, err := android.ApiLevelFromUser(ctx, v)
2231		if err != nil {
2232			ctx.PropertyErrorf("versions", "%s", err.Error())
2233			return
2234		}
2235		if i > 0 && ver.LessThanOrEqualTo(previous) {
2236			ctx.PropertyErrorf("versions", "not sorted: %v", versions)
2237		}
2238		versions[i] = ver.String()
2239		previous = ver
2240	}
2241}
2242
2243func perApiVersionVariations(mctx android.BaseModuleContext, minSdkVersion string) []string {
2244	from, err := nativeApiLevelFromUser(mctx, minSdkVersion)
2245	if err != nil {
2246		mctx.PropertyErrorf("min_sdk_version", err.Error())
2247		return []string{""}
2248	}
2249
2250	return ndkLibraryVersions(mctx, from)
2251}
2252
2253func canBeOrLinkAgainstVersionVariants(module interface {
2254	Host() bool
2255	InRamdisk() bool
2256	InVendorRamdisk() bool
2257}) bool {
2258	return !module.Host() && !module.InRamdisk() && !module.InVendorRamdisk()
2259}
2260
2261func canBeVersionVariant(module interface {
2262	Host() bool
2263	InRamdisk() bool
2264	InVendorRamdisk() bool
2265	CcLibraryInterface() bool
2266	Shared() bool
2267}) bool {
2268	return canBeOrLinkAgainstVersionVariants(module) &&
2269		module.CcLibraryInterface() && module.Shared()
2270}
2271
2272func moduleLibraryInterface(module blueprint.Module) libraryInterface {
2273	if m, ok := module.(*Module); ok {
2274		return m.library
2275	}
2276	return nil
2277}
2278
2279// setStubsVersions normalizes the versions in the Stubs.Versions property into MutatedProperties.AllStubsVersions.
2280func setStubsVersions(mctx android.BaseModuleContext, library libraryInterface, module *Module) {
2281	if !library.buildShared() || !canBeVersionVariant(module) {
2282		return
2283	}
2284	versions := library.stubsVersions(mctx)
2285	if mctx.Failed() {
2286		return
2287	}
2288	// Set the versions on the pre-mutated module so they can be read by any llndk modules that
2289	// depend on the implementation library and haven't been mutated yet.
2290	library.setAllStubsVersions(versions)
2291}
2292
2293// versionTransitionMutator splits a module into the mandatory non-stubs variant
2294// (which is unnamed) and zero or more stubs variants.
2295type versionTransitionMutator struct{}
2296
2297func (versionTransitionMutator) Split(ctx android.BaseModuleContext) []string {
2298	if ctx.Os() != android.Android {
2299		return []string{""}
2300	}
2301
2302	m, ok := ctx.Module().(*Module)
2303	if library := moduleLibraryInterface(ctx.Module()); library != nil && canBeVersionVariant(m) {
2304		setStubsVersions(ctx, library, m)
2305
2306		return append(slices.Clone(library.allStubsVersions()), "")
2307	} else if ok && m.SplitPerApiLevel() && m.IsSdkVariant() {
2308		return perApiVersionVariations(ctx, m.MinSdkVersion())
2309	}
2310
2311	return []string{""}
2312}
2313
2314func (versionTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
2315	return ""
2316}
2317
2318func (versionTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
2319	if ctx.Os() != android.Android {
2320		return ""
2321	}
2322	m, ok := ctx.Module().(*Module)
2323	if library := moduleLibraryInterface(ctx.Module()); library != nil && canBeVersionVariant(m) {
2324		if incomingVariation == "latest" {
2325			latestVersion := ""
2326			versions := library.allStubsVersions()
2327			if len(versions) > 0 {
2328				latestVersion = versions[len(versions)-1]
2329			}
2330			return latestVersion
2331		}
2332		return incomingVariation
2333	} else if ok && m.SplitPerApiLevel() && m.IsSdkVariant() {
2334		// If this module only has variants with versions and the incoming dependency doesn't specify which one
2335		// is needed then assume the latest version.
2336		if incomingVariation == "" {
2337			return android.FutureApiLevel.String()
2338		}
2339		return incomingVariation
2340	}
2341
2342	return ""
2343}
2344
2345func (versionTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
2346	// Optimization: return early if this module can't be affected.
2347	if ctx.Os() != android.Android {
2348		return
2349	}
2350
2351	m, ok := ctx.Module().(*Module)
2352	if library := moduleLibraryInterface(ctx.Module()); library != nil && canBeVersionVariant(m) {
2353		isLLNDK := m.IsLlndk()
2354		isVendorPublicLibrary := m.IsVendorPublicLibrary()
2355
2356		if variation != "" || isLLNDK || isVendorPublicLibrary {
2357			// A stubs or LLNDK stubs variant.
2358			if m.sanitize != nil {
2359				m.sanitize.Properties.ForceDisable = true
2360			}
2361			if m.stl != nil {
2362				m.stl.Properties.Stl = StringPtr("none")
2363			}
2364			m.Properties.PreventInstall = true
2365			lib := moduleLibraryInterface(m)
2366			allStubsVersions := library.allStubsVersions()
2367			isLatest := len(allStubsVersions) > 0 && variation == allStubsVersions[len(allStubsVersions)-1]
2368			lib.setBuildStubs(isLatest)
2369		}
2370		if variation != "" {
2371			// A non-LLNDK stubs module is hidden from make
2372			library.setStubsVersion(variation)
2373			m.Properties.HideFromMake = true
2374		} else {
2375			// A non-LLNDK implementation module has a dependency to all stubs versions
2376			for _, version := range library.allStubsVersions() {
2377				ctx.AddVariationDependencies([]blueprint.Variation{{"version", version}},
2378					stubImplDepTag, ctx.ModuleName())
2379			}
2380		}
2381	} else if ok && m.SplitPerApiLevel() && m.IsSdkVariant() {
2382		m.Properties.Sdk_version = StringPtr(variation)
2383		m.Properties.Min_sdk_version = StringPtr(variation)
2384	}
2385}
2386
2387// maybeInjectBoringSSLHash adds a rule to run bssl_inject_hash on the output file if the module has the
2388// inject_bssl_hash or if any static library dependencies have inject_bssl_hash set.  It returns the output path
2389// that the linked output file should be written to.
2390// TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
2391func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.ModuleOutPath,
2392	inject *bool, fileName string) android.ModuleOutPath {
2393	// TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries.
2394	injectBoringSSLHash := Bool(inject)
2395	ctx.VisitDirectDeps(func(dep android.Module) {
2396		if tag, ok := ctx.OtherModuleDependencyTag(dep).(libraryDependencyTag); ok && tag.static() {
2397			if cc, ok := dep.(*Module); ok {
2398				if library, ok := cc.linker.(*libraryDecorator); ok {
2399					if Bool(library.Properties.Inject_bssl_hash) {
2400						injectBoringSSLHash = true
2401					}
2402				}
2403			}
2404		}
2405	})
2406	if injectBoringSSLHash {
2407		hashedOutputfile := outputFile
2408		outputFile = android.PathForModuleOut(ctx, "unhashed", fileName)
2409
2410		rule := android.NewRuleBuilder(pctx, ctx)
2411		rule.Command().
2412			BuiltTool("bssl_inject_hash").
2413			FlagWithInput("-in-object ", outputFile).
2414			FlagWithOutput("-o ", hashedOutputfile)
2415		rule.Build("injectCryptoHash", "inject crypto hash")
2416	}
2417
2418	return outputFile
2419}
2420