xref: /aosp_15_r20/build/soong/cc/library_sdk_member.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2019 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	"path/filepath"
19
20	"android/soong/android"
21
22	"github.com/google/blueprint"
23	"github.com/google/blueprint/proptools"
24)
25
26// This file contains support for using cc library modules within an sdk.
27
28var sharedLibrarySdkMemberType = &librarySdkMemberType{
29	SdkMemberTypeBase: android.SdkMemberTypeBase{
30		PropertyName:          "native_shared_libs",
31		SupportsSdk:           true,
32		HostOsDependent:       true,
33		SupportedLinkageNames: []string{"shared"},
34		StripDisabled:         true,
35	},
36	prebuiltModuleType: "cc_prebuilt_library_shared",
37}
38
39var staticLibrarySdkMemberType = &librarySdkMemberType{
40	SdkMemberTypeBase: android.SdkMemberTypeBase{
41		PropertyName:          "native_static_libs",
42		SupportsSdk:           true,
43		HostOsDependent:       true,
44		SupportedLinkageNames: []string{"static"},
45	},
46	prebuiltModuleType: "cc_prebuilt_library_static",
47}
48
49var staticAndSharedLibrarySdkMemberType = &librarySdkMemberType{
50	SdkMemberTypeBase: android.SdkMemberTypeBase{
51		PropertyName:           "native_libs",
52		OverridesPropertyNames: map[string]bool{"native_shared_libs": true, "native_static_libs": true},
53		SupportsSdk:            true,
54		HostOsDependent:        true,
55		SupportedLinkageNames:  []string{"static", "shared"},
56	},
57	prebuiltModuleType: "cc_prebuilt_library",
58}
59
60func init() {
61	// Register sdk member types.
62	android.RegisterSdkMemberType(sharedLibrarySdkMemberType)
63	android.RegisterSdkMemberType(staticLibrarySdkMemberType)
64	android.RegisterSdkMemberType(staticAndSharedLibrarySdkMemberType)
65}
66
67type librarySdkMemberType struct {
68	android.SdkMemberTypeBase
69
70	prebuiltModuleType string
71
72	noOutputFiles bool // True if there are no srcs files.
73
74}
75
76func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
77	// The base set of targets which does not include native bridge targets.
78	defaultTargets := ctx.MultiTargets()
79
80	// The lazily created list of native bridge targets.
81	var includeNativeBridgeTargets []android.Target
82
83	for _, lib := range names {
84		targets := defaultTargets
85
86		// If native bridge support is required in the sdk snapshot then add native bridge targets to
87		// the basic list of targets that are required.
88		nativeBridgeSupport := ctx.RequiresTrait(lib, nativeBridgeSdkTrait)
89		if nativeBridgeSupport && ctx.Device() {
90			// If not already computed then compute the list of native bridge targets.
91			if includeNativeBridgeTargets == nil {
92				includeNativeBridgeTargets = append([]android.Target{}, defaultTargets...)
93				allAndroidTargets := ctx.Config().Targets[android.Android]
94				for _, possibleNativeBridgeTarget := range allAndroidTargets {
95					if possibleNativeBridgeTarget.NativeBridge == android.NativeBridgeEnabled {
96						includeNativeBridgeTargets = append(includeNativeBridgeTargets, possibleNativeBridgeTarget)
97					}
98				}
99			}
100
101			// Include the native bridge targets as well.
102			targets = includeNativeBridgeTargets
103		}
104
105		// memberDependency encapsulates information about the dependencies to add for this member.
106		type memberDependency struct {
107			// The targets to depend upon.
108			targets []android.Target
109
110			// Additional image variations to depend upon, is either nil for no image variation or
111			// contains a single image variation.
112			imageVariations []blueprint.Variation
113		}
114
115		// Extract the name and version from the module name.
116		name, version := StubsLibNameAndVersion(lib)
117		if version == "" {
118			version = "latest"
119		}
120
121		// Compute the set of dependencies to add.
122		var memberDependencies []memberDependency
123		if ctx.Host() {
124			// Host does not support image variations so add a dependency without any.
125			memberDependencies = append(memberDependencies, memberDependency{
126				targets: targets,
127			})
128		} else {
129			// Otherwise, this is targeting the device so add a dependency on the core image variation
130			// (image:"").
131			memberDependencies = append(memberDependencies, memberDependency{
132				imageVariations: []blueprint.Variation{{Mutator: "image", Variation: android.CoreVariation}},
133				targets:         targets,
134			})
135
136			// If required add additional dependencies on the image:ramdisk variants.
137			if ctx.RequiresTrait(lib, ramdiskImageRequiredSdkTrait) {
138				memberDependencies = append(memberDependencies, memberDependency{
139					imageVariations: []blueprint.Variation{{Mutator: "image", Variation: android.RamdiskVariation}},
140					// Only add a dependency on the first target as that is the only one which will have an
141					// image:ramdisk variant.
142					targets: targets[:1],
143				})
144			}
145
146			// If required add additional dependencies on the image:recovery variants.
147			if ctx.RequiresTrait(lib, recoveryImageRequiredSdkTrait) {
148				memberDependencies = append(memberDependencies, memberDependency{
149					imageVariations: []blueprint.Variation{{Mutator: "image", Variation: android.RecoveryVariation}},
150					// Only add a dependency on the first target as that is the only one which will have an
151					// image:recovery variant.
152					targets: targets[:1],
153				})
154			}
155		}
156
157		// For each dependency in the list add dependencies on the targets with the correct variations.
158		for _, dependency := range memberDependencies {
159			// For each target add a dependency on the target with any additional dependencies.
160			for _, target := range dependency.targets {
161				// Get the variations for the target.
162				variations := target.Variations()
163
164				// Add any additional dependencies needed.
165				variations = append(variations, dependency.imageVariations...)
166
167				if mt.SupportedLinkageNames == nil {
168					// No link types are supported so add a dependency directly.
169					ctx.AddFarVariationDependencies(variations, dependencyTag, name)
170				} else {
171					// Otherwise, add a dependency on each supported link type in turn.
172					for _, linkType := range mt.SupportedLinkageNames {
173						libVariations := append(variations,
174							blueprint.Variation{Mutator: "link", Variation: linkType})
175						// If this is for the device and a shared link type then add a dependency onto the
176						// appropriate version specific variant of the module.
177						if ctx.Device() && linkType == "shared" {
178							libVariations = append(libVariations,
179								blueprint.Variation{Mutator: "version", Variation: version})
180						}
181						ctx.AddFarVariationDependencies(libVariations, dependencyTag, name)
182					}
183				}
184			}
185		}
186	}
187}
188
189func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
190	// Check the module to see if it can be used with this module type.
191	if m, ok := module.(*Module); ok {
192		for _, allowableMemberType := range m.sdkMemberTypes {
193			if allowableMemberType == mt {
194				return true
195			}
196		}
197	}
198
199	return false
200}
201
202func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
203	pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, mt.prebuiltModuleType)
204
205	ccModule := member.Variants()[0].(*Module)
206
207	if ctx.RequiresTrait(nativeBridgeSdkTrait) {
208		pbm.AddProperty("native_bridge_supported", true)
209	}
210
211	if ctx.RequiresTrait(ramdiskImageRequiredSdkTrait) {
212		pbm.AddProperty("ramdisk_available", true)
213	}
214
215	if ctx.RequiresTrait(recoveryImageRequiredSdkTrait) {
216		pbm.AddProperty("recovery_available", true)
217	}
218
219	if proptools.Bool(ccModule.VendorProperties.Vendor_available) {
220		pbm.AddProperty("vendor_available", true)
221	}
222
223	if proptools.Bool(ccModule.VendorProperties.Odm_available) {
224		pbm.AddProperty("odm_available", true)
225	}
226
227	if proptools.Bool(ccModule.VendorProperties.Product_available) {
228		pbm.AddProperty("product_available", true)
229	}
230
231	sdkVersion := ccModule.SdkVersion()
232	if sdkVersion != "" {
233		pbm.AddProperty("sdk_version", sdkVersion)
234	}
235
236	stl := ccModule.stl.Properties.Stl
237	if stl != nil {
238		pbm.AddProperty("stl", proptools.String(stl))
239	}
240
241	if lib, ok := ccModule.linker.(*libraryDecorator); ok {
242		uhs := lib.Properties.Unique_host_soname
243		if uhs != nil {
244			pbm.AddProperty("unique_host_soname", proptools.Bool(uhs))
245		}
246	}
247
248	return pbm
249}
250
251func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
252	return &nativeLibInfoProperties{memberType: mt}
253}
254
255func isGeneratedHeaderDirectory(p android.Path) bool {
256	_, gen := p.(android.WritablePath)
257	return gen
258}
259
260type includeDirsProperty struct {
261	// Accessor to retrieve the paths
262	pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
263
264	// The name of the property in the prebuilt library, "" means there is no property.
265	propertyName string
266
267	// The directory within the snapshot directory into which items should be copied.
268	snapshotDir string
269
270	// True if the items on the path should be copied.
271	copy bool
272
273	// True if the paths represent directories, files if they represent files.
274	dirs bool
275}
276
277var includeDirProperties = []includeDirsProperty{
278	{
279		// ExportedIncludeDirs lists directories that contains some header files to be
280		// copied into a directory in the snapshot. The snapshot directories must be added to
281		// the export_include_dirs property in the prebuilt module in the snapshot.
282		pathsGetter:  func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
283		propertyName: "export_include_dirs",
284		snapshotDir:  nativeIncludeDir,
285		copy:         true,
286		dirs:         true,
287	},
288	{
289		// ExportedSystemIncludeDirs lists directories that contains some system header files to
290		// be copied into a directory in the snapshot. The snapshot directories must be added to
291		// the export_system_include_dirs property in the prebuilt module in the snapshot.
292		pathsGetter:  func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
293		propertyName: "export_system_include_dirs",
294		snapshotDir:  nativeIncludeDir,
295		copy:         true,
296		dirs:         true,
297	},
298	{
299		// ExportedGeneratedIncludeDirs lists directories that contains some header files
300		// that are explicitly listed in the ExportedGeneratedHeaders property. So, the contents
301		// of these directories do not need to be copied, but these directories do need adding to
302		// the export_include_dirs property in the prebuilt module in the snapshot.
303		pathsGetter:  func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedGeneratedIncludeDirs },
304		propertyName: "export_include_dirs",
305		snapshotDir:  nativeGeneratedIncludeDir,
306		copy:         false,
307		dirs:         true,
308	},
309	{
310		// ExportedGeneratedHeaders lists header files that are in one of the directories
311		// specified in ExportedGeneratedIncludeDirs must be copied into the snapshot.
312		// As they are in a directory in ExportedGeneratedIncludeDirs they do not need adding to a
313		// property in the prebuilt module in the snapshot.
314		pathsGetter:  func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedGeneratedHeaders },
315		propertyName: "",
316		snapshotDir:  nativeGeneratedIncludeDir,
317		copy:         true,
318		dirs:         false,
319	},
320}
321
322// Add properties that may, or may not, be arch specific.
323func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
324
325	outputProperties.AddProperty("sanitize", &libInfo.Sanitize)
326
327	// Copy the generated library to the snapshot and add a reference to it in the .bp module.
328	if libInfo.outputFile != nil {
329		nativeLibraryPath := nativeLibraryPathFor(libInfo)
330		builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
331		outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
332	}
333
334	if len(libInfo.SharedLibs) > 0 {
335		outputProperties.AddPropertyWithTag("shared_libs", libInfo.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
336	}
337
338	// SystemSharedLibs needs to be propagated if it's a list, even if it's empty,
339	// so check for non-nil instead of nonzero length.
340	if libInfo.SystemSharedLibs != nil {
341		outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
342	}
343
344	// Map from property name to the include dirs to add to the prebuilt module in the snapshot.
345	includeDirs := make(map[string][]string)
346
347	// Iterate over each include directory property, copying files and collating property
348	// values where necessary.
349	for _, propertyInfo := range includeDirProperties {
350		// Calculate the base directory in the snapshot into which the files will be copied.
351		// lib.archSubDir is "" for common properties.
352		targetDir := filepath.Join(libInfo.OsPrefix(), libInfo.archSubDir, propertyInfo.snapshotDir)
353
354		propertyName := propertyInfo.propertyName
355
356		// Iterate over each path in one of the include directory properties.
357		for _, path := range propertyInfo.pathsGetter(libInfo) {
358			inputPath := path.String()
359
360			// Map the input path to a snapshot relative path. The mapping is independent of the module
361			// that references them so that if multiple modules within the same snapshot export the same
362			// header files they end up in the same place in the snapshot and so do not get duplicated.
363			targetRelativePath := inputPath
364			if isGeneratedHeaderDirectory(path) {
365				// Remove everything up to the .intermediates/ from the generated output directory to
366				// leave a module relative path.
367				base := android.PathForIntermediates(sdkModuleContext, "")
368				targetRelativePath = android.Rel(sdkModuleContext, base.String(), inputPath)
369			}
370
371			snapshotRelativePath := filepath.Join(targetDir, targetRelativePath)
372
373			// Copy the files/directories when necessary.
374			if propertyInfo.copy {
375				if propertyInfo.dirs {
376					// When copying a directory glob and copy all the headers within it.
377					// TODO(jiyong) copy headers having other suffixes
378					headers, _ := sdkModuleContext.GlobWithDeps(inputPath+"/**/*.h", nil)
379					for _, file := range headers {
380						src := android.PathForSource(sdkModuleContext, file)
381
382						// The destination path in the snapshot is constructed from the snapshot relative path
383						// of the input directory and the input directory relative path of the header file.
384						inputRelativePath := android.Rel(sdkModuleContext, inputPath, file)
385						dest := filepath.Join(snapshotRelativePath, inputRelativePath)
386						builder.CopyToSnapshot(src, dest)
387					}
388				} else {
389					// Otherwise, just copy the file to its snapshot relative path.
390					builder.CopyToSnapshot(path, snapshotRelativePath)
391				}
392			}
393
394			// Only directories are added to a property.
395			if propertyInfo.dirs {
396				includeDirs[propertyName] = append(includeDirs[propertyName], snapshotRelativePath)
397			}
398		}
399	}
400
401	// Add the collated include dir properties to the output.
402	for _, property := range android.SortedKeys(includeDirs) {
403		outputProperties.AddProperty(property, includeDirs[property])
404	}
405
406	if len(libInfo.StubsVersions) > 0 {
407		stubsSet := outputProperties.AddPropertySet("stubs")
408		stubsSet.AddProperty("versions", libInfo.StubsVersions)
409		// The symbol file will be copied next to the Android.bp file
410		stubsSet.AddProperty("symbol_file", libInfo.StubsSymbolFilePath.Base())
411		builder.CopyToSnapshot(libInfo.StubsSymbolFilePath, libInfo.StubsSymbolFilePath.Base())
412	}
413}
414
415const (
416	nativeIncludeDir          = "include"
417	nativeGeneratedIncludeDir = "include_gen"
418	nativeStubDir             = "lib"
419)
420
421// path to the native library. Relative to <sdk_root>/<api_dir>
422func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
423	return filepath.Join(lib.OsPrefix(), lib.archSubDir,
424		nativeStubDir, lib.outputFile.Base())
425}
426
427// nativeLibInfoProperties represents properties of a native lib
428//
429// The exported (capitalized) fields will be examined and may be changed during common value extraction.
430// The unexported fields will be left untouched.
431type nativeLibInfoProperties struct {
432	android.SdkMemberPropertiesBase
433
434	memberType *librarySdkMemberType
435
436	// archSubDir is the subdirectory within the OS directory in the sdk snapshot into which arch
437	// specific files will be copied.
438	//
439	// It is not exported since any value other than "" is always going to be arch specific.
440	// This is "" for non-arch specific common properties.
441	archSubDir string
442
443	// The list of possibly common exported include dirs.
444	//
445	// This field is exported as its contents may not be arch specific.
446	ExportedIncludeDirs android.Paths `android:"arch_variant"`
447
448	// The list of arch specific exported generated include dirs.
449	//
450	// This field is exported as its contents may not be arch specific, e.g. protos.
451	ExportedGeneratedIncludeDirs android.Paths `android:"arch_variant"`
452
453	// The list of arch specific exported generated header files.
454	//
455	// This field is exported as its contents may not be arch specific, e.g. protos.
456	ExportedGeneratedHeaders android.Paths `android:"arch_variant"`
457
458	// The list of possibly common exported system include dirs.
459	//
460	// This field is exported as its contents may not be arch specific.
461	ExportedSystemIncludeDirs android.Paths `android:"arch_variant"`
462
463	// The list of possibly common exported flags.
464	//
465	// This field is exported as its contents may not be arch specific.
466	ExportedFlags []string `android:"arch_variant"`
467
468	// The set of shared libraries
469	//
470	// This field is exported as its contents may not be arch specific.
471	SharedLibs []string `android:"arch_variant"`
472
473	// The set of system shared libraries. Note nil and [] are semantically
474	// distinct - see BaseLinkerProperties.System_shared_libs.
475	//
476	// This field is exported as its contents may not be arch specific.
477	SystemSharedLibs []string `android:"arch_variant"`
478
479	// The specific stubs version for the lib variant, or empty string if stubs
480	// are not in use.
481	//
482	// Marked 'ignored-on-host' as the AllStubsVersions() from which this is
483	// initialized is not set on host and the stubs.versions property which this
484	// is written to does not vary by arch so cannot be android specific.
485	StubsVersions []string `sdk:"ignored-on-host"`
486
487	// The symbol file containing the APIs exported by this library.
488	StubsSymbolFilePath android.Path `sdk:"ignored-on-host"`
489
490	// Value of SanitizeProperties.Sanitize. Several - but not all - of these
491	// affect the expanded variants. All are propagated to avoid entangling the
492	// sanitizer logic with the snapshot generation.
493	Sanitize SanitizeUserProps `android:"arch_variant"`
494
495	// outputFile is not exported as it is always arch specific.
496	outputFile android.Path
497}
498
499func (p *nativeLibInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
500	addOutputFile := true
501	ccModule := variant.(*Module)
502
503	if s := ccModule.sanitize; s != nil {
504		// We currently do not capture sanitizer flags for libs with sanitizers
505		// enabled, because they may vary among variants that cannot be represented
506		// in the input blueprint files. In particular, sanitizerDepsMutator enables
507		// various sanitizers on dependencies, but in many cases only on static
508		// ones, and we cannot specify sanitizer flags at the link type level (i.e.
509		// in StaticOrSharedProperties).
510		if s.isUnsanitizedVariant() {
511			// This still captures explicitly disabled sanitizers, which may be
512			// necessary to avoid cyclic dependencies.
513			p.Sanitize = s.Properties.Sanitize
514		} else {
515			// Do not add the output file to the snapshot if we don't represent it
516			// properly.
517			addOutputFile = false
518		}
519	}
520
521	exportedInfo, _ := android.OtherModuleProvider(ctx.SdkModuleContext(), variant, FlagExporterInfoProvider)
522
523	// Separate out the generated include dirs (which are arch specific) from the
524	// include dirs (which may not be).
525	exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
526		exportedInfo.IncludeDirs, isGeneratedHeaderDirectory)
527
528	target := ccModule.Target()
529	p.archSubDir = target.Arch.ArchType.String()
530	if target.NativeBridge == android.NativeBridgeEnabled {
531		p.archSubDir += "_native_bridge"
532	}
533
534	// Make sure that the include directories are unique.
535	p.ExportedIncludeDirs = android.FirstUniquePaths(exportedIncludeDirs)
536	p.ExportedGeneratedIncludeDirs = android.FirstUniquePaths(exportedGeneratedIncludeDirs)
537
538	// Take a copy before filtering out duplicates to avoid changing the slice owned by the
539	// ccModule.
540	dirs := append(android.Paths(nil), exportedInfo.SystemIncludeDirs...)
541	p.ExportedSystemIncludeDirs = android.FirstUniquePaths(dirs)
542
543	p.ExportedFlags = exportedInfo.Flags
544	if ccModule.linker != nil {
545		specifiedDeps := specifiedDeps{}
546		specifiedDeps = ccModule.linker.linkerSpecifiedDeps(ctx.SdkModuleContext(), ccModule, specifiedDeps)
547
548		if lib := ccModule.library; lib != nil {
549			if !lib.hasStubsVariants() {
550				// Propagate dynamic dependencies for implementation libs, but not stubs.
551				p.SharedLibs = specifiedDeps.sharedLibs
552			} else {
553				// TODO(b/169373910): 1. Only output the specific version (from
554				// ccModule.StubsVersion()) if the module is versioned. 2. Ensure that all
555				// the versioned stub libs are retained in the prebuilt tree; currently only
556				// the stub corresponding to ccModule.StubsVersion() is.
557				p.StubsVersions = lib.allStubsVersions()
558				if lib.buildStubs() && ccModule.stubsSymbolFilePath() == nil {
559					ctx.ModuleErrorf("Could not determine symbol_file")
560				} else {
561					p.StubsSymbolFilePath = ccModule.stubsSymbolFilePath()
562				}
563			}
564		}
565		p.SystemSharedLibs = specifiedDeps.systemSharedLibs
566	}
567	p.ExportedGeneratedHeaders = exportedInfo.GeneratedHeaders
568
569	if !p.memberType.noOutputFiles && addOutputFile {
570		p.outputFile = getRequiredMemberOutputFile(ctx, ccModule)
571	}
572}
573
574func getRequiredMemberOutputFile(ctx android.SdkMemberContext, ccModule *Module) android.Path {
575	var path android.Path
576	outputFile := ccModule.OutputFile()
577	if outputFile.Valid() {
578		path = outputFile.Path()
579	} else {
580		ctx.SdkModuleContext().ModuleErrorf("member variant %s does not have a valid output file", ccModule)
581	}
582	return path
583}
584
585func (p *nativeLibInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
586	addPossiblyArchSpecificProperties(ctx.SdkModuleContext(), ctx.SnapshotBuilder(), p, propertySet)
587}
588