blob: 053c460696e8576a16a459042fcdcd0b6f944a46 [file] [log] [blame]
Paul Duffin2f6bc092019-12-13 10:40:56 +00001// 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"
Paul Duffin2f6bc092019-12-13 10:40:56 +000019
20 "android/soong/android"
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000021
Paul Duffin2f6bc092019-12-13 10:40:56 +000022 "github.com/google/blueprint"
Paul Duffin13f02712020-03-06 12:30:43 +000023 "github.com/google/blueprint/proptools"
Paul Duffin2f6bc092019-12-13 10:40:56 +000024)
25
26// This file contains support for using cc library modules within an sdk.
27
Paul Duffina0843f62019-12-13 19:50:38 +000028var sharedLibrarySdkMemberType = &librarySdkMemberType{
29 SdkMemberTypeBase: android.SdkMemberTypeBase{
Liz Kammer96320df2022-05-12 20:40:00 -040030 PropertyName: "native_shared_libs",
31 SupportsSdk: true,
32 HostOsDependent: true,
33 SupportedLinkageNames: []string{"shared"},
Alyssa Ketpreechasawat59ec0fa2024-07-04 19:51:17 +000034 StripDisabled: true,
Paul Duffina0843f62019-12-13 19:50:38 +000035 },
36 prebuiltModuleType: "cc_prebuilt_library_shared",
Paul Duffina0843f62019-12-13 19:50:38 +000037}
38
39var staticLibrarySdkMemberType = &librarySdkMemberType{
40 SdkMemberTypeBase: android.SdkMemberTypeBase{
Liz Kammer96320df2022-05-12 20:40:00 -040041 PropertyName: "native_static_libs",
42 SupportsSdk: true,
43 HostOsDependent: true,
44 SupportedLinkageNames: []string{"static"},
Paul Duffina0843f62019-12-13 19:50:38 +000045 },
46 prebuiltModuleType: "cc_prebuilt_library_static",
Paul Duffina0843f62019-12-13 19:50:38 +000047}
48
Paul Duffin9b76c0b2020-03-12 10:24:35 +000049var staticAndSharedLibrarySdkMemberType = &librarySdkMemberType{
50 SdkMemberTypeBase: android.SdkMemberTypeBase{
Liz Kammer96320df2022-05-12 20:40:00 -040051 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"},
Paul Duffin9b76c0b2020-03-12 10:24:35 +000056 },
57 prebuiltModuleType: "cc_prebuilt_library",
Paul Duffin9b76c0b2020-03-12 10:24:35 +000058}
59
Paul Duffin255f18e2019-12-13 11:22:16 +000060func init() {
61 // Register sdk member types.
Paul Duffina0843f62019-12-13 19:50:38 +000062 android.RegisterSdkMemberType(sharedLibrarySdkMemberType)
63 android.RegisterSdkMemberType(staticLibrarySdkMemberType)
Paul Duffin9b76c0b2020-03-12 10:24:35 +000064 android.RegisterSdkMemberType(staticAndSharedLibrarySdkMemberType)
Paul Duffin2f6bc092019-12-13 10:40:56 +000065}
66
67type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +000068 android.SdkMemberTypeBase
69
Paul Duffin2f6bc092019-12-13 10:40:56 +000070 prebuiltModuleType string
71
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000072 noOutputFiles bool // True if there are no srcs files.
73
Paul Duffin2f6bc092019-12-13 10:40:56 +000074}
75
Paul Duffin296701e2021-07-14 10:29:36 +010076func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
Paul Duffin93b750e2019-11-19 19:44:10 +000077 // 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
Paul Duffin2f6bc092019-12-13 10:40:56 +000083 for _, lib := range names {
Paul Duffin93b750e2019-11-19 19:44:10 +000084 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 }
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100104
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 })
Paul Duffin63696222021-09-06 10:28:34 +0100135
Paul Duffin12a0a312021-09-15 17:25:10 +0100136 // 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
Paul Duffin63696222021-09-06 10:28:34 +0100146 // 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 }
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100155 }
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
Liz Kammer96320df2022-05-12 20:40:00 -0400167 if mt.SupportedLinkageNames == nil {
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100168 // 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.
Liz Kammer96320df2022-05-12 20:40:00 -0400172 for _, linkType := range mt.SupportedLinkageNames {
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100173 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)
Colin Crossa717db72020-10-23 14:53:06 -0700182 }
Paul Duffin91756d22020-02-21 16:29:57 +0000183 }
Paul Duffin2f6bc092019-12-13 10:40:56 +0000184 }
185 }
186 }
187}
188
189func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
Paul Duffina0843f62019-12-13 19:50:38 +0000190 // 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
Paul Duffin2f6bc092019-12-13 10:40:56 +0000200}
201
Paul Duffin3a4eb502020-03-19 16:11:18 +0000202func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
203 pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, mt.prebuiltModuleType)
Paul Duffin0c394f32020-03-05 14:09:58 +0000204
205 ccModule := member.Variants()[0].(*Module)
206
Paul Duffin93b750e2019-11-19 19:44:10 +0000207 if ctx.RequiresTrait(nativeBridgeSdkTrait) {
208 pbm.AddProperty("native_bridge_supported", true)
209 }
210
Paul Duffin12a0a312021-09-15 17:25:10 +0100211 if ctx.RequiresTrait(ramdiskImageRequiredSdkTrait) {
212 pbm.AddProperty("ramdisk_available", true)
213 }
214
Paul Duffin63696222021-09-06 10:28:34 +0100215 if ctx.RequiresTrait(recoveryImageRequiredSdkTrait) {
Paul Duffind6abaa72020-09-07 16:39:22 +0100216 pbm.AddProperty("recovery_available", true)
217 }
218
Paul Duffind1edbd42020-08-13 19:45:31 +0100219 if proptools.Bool(ccModule.VendorProperties.Vendor_available) {
220 pbm.AddProperty("vendor_available", true)
221 }
222
Justin Yunebcf0c52021-01-08 18:00:19 +0900223 if proptools.Bool(ccModule.VendorProperties.Odm_available) {
224 pbm.AddProperty("odm_available", true)
225 }
226
Justin Yun63e9ec72020-10-29 16:49:43 +0900227 if proptools.Bool(ccModule.VendorProperties.Product_available) {
228 pbm.AddProperty("product_available", true)
229 }
230
Paul Duffin0c394f32020-03-05 14:09:58 +0000231 sdkVersion := ccModule.SdkVersion()
232 if sdkVersion != "" {
233 pbm.AddProperty("sdk_version", sdkVersion)
234 }
Paul Duffin2f6bc092019-12-13 10:40:56 +0000235
Paul Duffin13f02712020-03-06 12:30:43 +0000236 stl := ccModule.stl.Properties.Stl
237 if stl != nil {
Paul Duffin0174d8d2020-03-11 18:42:08 +0000238 pbm.AddProperty("stl", proptools.String(stl))
Paul Duffin13f02712020-03-06 12:30:43 +0000239 }
Martin Stjernholm47ed3522020-06-17 22:52:25 +0100240
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
Paul Duffin0174d8d2020-03-11 18:42:08 +0000248 return pbm
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000249}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000250
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000251func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
252 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000253}
254
255func isGeneratedHeaderDirectory(p android.Path) bool {
256 _, gen := p.(android.WritablePath)
Colin Cross8ff10582023-12-07 13:10:56 -0800257 return gen
Paul Duffin2f6bc092019-12-13 10:40:56 +0000258}
259
Paul Duffin64f54b02020-02-20 14:33:54 +0000260type includeDirsProperty struct {
261 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000262 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000263
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
Paul Duffin74fc1902020-01-23 11:45:03 +0000275}
276
Paul Duffin64f54b02020-02-20 14:33:54 +0000277var 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.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000282 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000283 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.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000292 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000293 propertyName: "export_system_include_dirs",
294 snapshotDir: nativeIncludeDir,
295 copy: true,
296 dirs: true,
297 },
298 {
Paul Duffin7a7d0672021-02-17 12:17:40 +0000299 // ExportedGeneratedIncludeDirs lists directories that contains some header files
300 // that are explicitly listed in the ExportedGeneratedHeaders property. So, the contents
Paul Duffin64f54b02020-02-20 14:33:54 +0000301 // 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.
Paul Duffin7a7d0672021-02-17 12:17:40 +0000303 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000304 propertyName: "export_include_dirs",
305 snapshotDir: nativeGeneratedIncludeDir,
306 copy: false,
307 dirs: true,
308 },
309 {
Paul Duffin7a7d0672021-02-17 12:17:40 +0000310 // 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
Paul Duffin64f54b02020-02-20 14:33:54 +0000313 // property in the prebuilt module in the snapshot.
Paul Duffin7a7d0672021-02-17 12:17:40 +0000314 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000315 propertyName: "",
316 snapshotDir: nativeGeneratedIncludeDir,
317 copy: true,
318 dirs: false,
319 },
320}
321
322// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000323func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
324
Martin Stjernholmb0249572020-09-15 02:32:35 +0100325 outputProperties.AddProperty("sanitize", &libInfo.Sanitize)
Martin Stjernholmfbb486f2020-08-21 18:43:51 +0100326
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000327 // 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 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000333
Paul Duffin13f02712020-03-06 12:30:43 +0000334 if len(libInfo.SharedLibs) > 0 {
335 outputProperties.AddPropertyWithTag("shared_libs", libInfo.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
336 }
337
Martin Stjernholm10566a02020-03-24 01:19:52 +0000338 // 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 {
Paul Duffin13f02712020-03-06 12:30:43 +0000341 outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
342 }
343
Paul Duffin64f54b02020-02-20 14:33:54 +0000344 // 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.
Paul Duffin96f18322021-09-03 17:53:38 +0100351 // lib.archSubDir is "" for common properties.
352 targetDir := filepath.Join(libInfo.OsPrefix(), libInfo.archSubDir, propertyInfo.snapshotDir)
Paul Duffin64f54b02020-02-20 14:33:54 +0000353
354 propertyName := propertyInfo.propertyName
355
356 // Iterate over each path in one of the include directory properties.
357 for _, path := range propertyInfo.pathsGetter(libInfo) {
Paul Duffin42dd4e62021-02-22 11:35:24 +0000358 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)
Paul Duffin64f54b02020-02-20 14:33:54 +0000372
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
Paul Duffin42dd4e62021-02-22 11:35:24 +0000378 headers, _ := sdkModuleContext.GlobWithDeps(inputPath+"/**/*.h", nil)
Paul Duffin64f54b02020-02-20 14:33:54 +0000379 for _, file := range headers {
380 src := android.PathForSource(sdkModuleContext, file)
Paul Duffin42dd4e62021-02-22 11:35:24 +0000381
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)
Paul Duffin64f54b02020-02-20 14:33:54 +0000386 builder.CopyToSnapshot(src, dest)
387 }
388 } else {
Paul Duffin42dd4e62021-02-22 11:35:24 +0000389 // Otherwise, just copy the file to its snapshot relative path.
390 builder.CopyToSnapshot(path, snapshotRelativePath)
Paul Duffin64f54b02020-02-20 14:33:54 +0000391 }
392 }
393
394 // Only directories are added to a property.
395 if propertyInfo.dirs {
Paul Duffin42dd4e62021-02-22 11:35:24 +0000396 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotRelativePath)
Paul Duffin64f54b02020-02-20 14:33:54 +0000397 }
398 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000399 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000400
401 // Add the collated include dir properties to the output.
Cole Faust18994c72023-02-28 16:02:16 -0800402 for _, property := range android.SortedKeys(includeDirs) {
Colin Cross2c033612020-09-11 15:44:31 -0700403 outputProperties.AddProperty(property, includeDirs[property])
Paul Duffin74fc1902020-01-23 11:45:03 +0000404 }
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100405
Martin Stjernholm618b6712020-09-24 16:53:04 +0100406 if len(libInfo.StubsVersions) > 0 {
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100407 stubsSet := outputProperties.AddPropertySet("stubs")
Martin Stjernholm618b6712020-09-24 16:53:04 +0100408 stubsSet.AddProperty("versions", libInfo.StubsVersions)
Spandan Dase20c56c2024-07-23 21:34:24 +0000409 // 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())
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100412 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000413}
414
Paul Duffin2f6bc092019-12-13 10:40:56 +0000415const (
416 nativeIncludeDir = "include"
417 nativeGeneratedIncludeDir = "include_gen"
418 nativeStubDir = "lib"
419)
420
421// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000422func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffin96f18322021-09-03 17:53:38 +0100423 return filepath.Join(lib.OsPrefix(), lib.archSubDir,
Paul Duffin2f6bc092019-12-13 10:40:56 +0000424 nativeStubDir, lib.outputFile.Base())
425}
426
Paul Duffin2f6bc092019-12-13 10:40:56 +0000427// 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 {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000432 android.SdkMemberPropertiesBase
433
434 memberType *librarySdkMemberType
435
Paul Duffin96f18322021-09-03 17:53:38 +0100436 // 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
Paul Duffin2f6bc092019-12-13 10:40:56 +0000442
Paul Duffin5efd1982020-02-20 14:33:54 +0000443 // The list of possibly common exported include dirs.
444 //
445 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100446 ExportedIncludeDirs android.Paths `android:"arch_variant"`
Paul Duffin2f6bc092019-12-13 10:40:56 +0000447
Paul Duffin5efd1982020-02-20 14:33:54 +0000448 // The list of arch specific exported generated include dirs.
449 //
Paul Duffin7a7d0672021-02-17 12:17:40 +0000450 // This field is exported as its contents may not be arch specific, e.g. protos.
451 ExportedGeneratedIncludeDirs android.Paths `android:"arch_variant"`
Paul Duffin5efd1982020-02-20 14:33:54 +0000452
453 // The list of arch specific exported generated header files.
454 //
Paul Duffin7a7d0672021-02-17 12:17:40 +0000455 // This field is exported as its contents may not be arch specific, e.g. protos.
456 ExportedGeneratedHeaders android.Paths `android:"arch_variant"`
Paul Duffin2f6bc092019-12-13 10:40:56 +0000457
Paul Duffin5efd1982020-02-20 14:33:54 +0000458 // The list of possibly common exported system include dirs.
459 //
460 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100461 ExportedSystemIncludeDirs android.Paths `android:"arch_variant"`
Paul Duffin5efd1982020-02-20 14:33:54 +0000462
463 // The list of possibly common exported flags.
464 //
465 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100466 ExportedFlags []string `android:"arch_variant"`
Paul Duffin5efd1982020-02-20 14:33:54 +0000467
Paul Duffin13f02712020-03-06 12:30:43 +0000468 // The set of shared libraries
469 //
470 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100471 SharedLibs []string `android:"arch_variant"`
Paul Duffin13f02712020-03-06 12:30:43 +0000472
Martin Stjernholm10566a02020-03-24 01:19:52 +0000473 // The set of system shared libraries. Note nil and [] are semantically
474 // distinct - see BaseLinkerProperties.System_shared_libs.
Paul Duffin13f02712020-03-06 12:30:43 +0000475 //
476 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100477 SystemSharedLibs []string `android:"arch_variant"`
Paul Duffin13f02712020-03-06 12:30:43 +0000478
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100479 // The specific stubs version for the lib variant, or empty string if stubs
480 // are not in use.
Paul Duffin7a1f7f32020-05-04 15:32:08 +0100481 //
Martin Stjernholm618b6712020-09-24 16:53:04 +0100482 // 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"`
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100486
Spandan Dase20c56c2024-07-23 21:34:24 +0000487 // The symbol file containing the APIs exported by this library.
488 StubsSymbolFilePath android.Path `sdk:"ignored-on-host"`
489
Martin Stjernholmb0249572020-09-15 02:32:35 +0100490 // 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"`
Martin Stjernholmfbb486f2020-08-21 18:43:51 +0100494
Paul Duffin2f6bc092019-12-13 10:40:56 +0000495 // outputFile is not exported as it is always arch specific.
496 outputFile android.Path
497}
498
Paul Duffin3a4eb502020-03-19 16:11:18 +0000499func (p *nativeLibInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Martin Stjernholm59e0c7a2020-10-28 23:38:33 +0000500 addOutputFile := true
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000501 ccModule := variant.(*Module)
502
Martin Stjernholm59e0c7a2020-10-28 23:38:33 +0000503 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 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000519 }
520
Colin Cross313aa542023-12-13 13:47:44 -0800521 exportedInfo, _ := android.OtherModuleProvider(ctx.SdkModuleContext(), variant, FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700522
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000523 // Separate out the generated include dirs (which are arch specific) from the
524 // include dirs (which may not be).
525 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
Colin Cross0de8a1e2020-09-18 14:15:30 -0700526 exportedInfo.IncludeDirs, isGeneratedHeaderDirectory)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000527
Paul Duffin93b750e2019-11-19 19:44:10 +0000528 target := ccModule.Target()
529 p.archSubDir = target.Arch.ArchType.String()
530 if target.NativeBridge == android.NativeBridgeEnabled {
531 p.archSubDir += "_native_bridge"
532 }
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000533
534 // Make sure that the include directories are unique.
535 p.ExportedIncludeDirs = android.FirstUniquePaths(exportedIncludeDirs)
Paul Duffin7a7d0672021-02-17 12:17:40 +0000536 p.ExportedGeneratedIncludeDirs = android.FirstUniquePaths(exportedGeneratedIncludeDirs)
Paul Duffinab5467d2020-06-18 16:31:04 +0100537
538 // Take a copy before filtering out duplicates to avoid changing the slice owned by the
539 // ccModule.
Colin Cross0de8a1e2020-09-18 14:15:30 -0700540 dirs := append(android.Paths(nil), exportedInfo.SystemIncludeDirs...)
Paul Duffinab5467d2020-06-18 16:31:04 +0100541 p.ExportedSystemIncludeDirs = android.FirstUniquePaths(dirs)
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000542
Colin Cross0de8a1e2020-09-18 14:15:30 -0700543 p.ExportedFlags = exportedInfo.Flags
Paul Duffin13f02712020-03-06 12:30:43 +0000544 if ccModule.linker != nil {
545 specifiedDeps := specifiedDeps{}
Cole Faustf0006e72024-08-19 14:39:19 -0700546 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(ctx, ccModule, specifiedDeps)
Paul Duffin13f02712020-03-06 12:30:43 +0000547
Colin Cross31076b32020-10-23 17:22:06 -0700548 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()
Spandan Dase20c56c2024-07-23 21:34:24 +0000558 if lib.buildStubs() && ccModule.stubsSymbolFilePath() == nil {
559 ctx.ModuleErrorf("Could not determine symbol_file")
560 } else {
561 p.StubsSymbolFilePath = ccModule.stubsSymbolFilePath()
562 }
Colin Cross31076b32020-10-23 17:22:06 -0700563 }
Martin Stjernholmcc330d62020-04-21 20:45:35 +0100564 }
Paul Duffin13f02712020-03-06 12:30:43 +0000565 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
566 }
Paul Duffin7a7d0672021-02-17 12:17:40 +0000567 p.ExportedGeneratedHeaders = exportedInfo.GeneratedHeaders
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100568
Martin Stjernholm59e0c7a2020-10-28 23:38:33 +0000569 if !p.memberType.noOutputFiles && addOutputFile {
570 p.outputFile = getRequiredMemberOutputFile(ctx, ccModule)
Martin Stjernholmfbb486f2020-08-21 18:43:51 +0100571 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000572}
573
Paul Duffin712993c2020-05-05 14:11:57 +0100574func 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
Paul Duffin3a4eb502020-03-19 16:11:18 +0000585func (p *nativeLibInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
586 addPossiblyArchSpecificProperties(ctx.SdkModuleContext(), ctx.SnapshotBuilder(), p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000587}