blob: 52e78d73c0b6280e3f7d6ee4d99b5ac30e10235b [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"},
Paul Duffina0843f62019-12-13 19:50:38 +000034 },
35 prebuiltModuleType: "cc_prebuilt_library_shared",
Paul Duffina0843f62019-12-13 19:50:38 +000036}
37
38var staticLibrarySdkMemberType = &librarySdkMemberType{
39 SdkMemberTypeBase: android.SdkMemberTypeBase{
Liz Kammer96320df2022-05-12 20:40:00 -040040 PropertyName: "native_static_libs",
41 SupportsSdk: true,
42 HostOsDependent: true,
43 SupportedLinkageNames: []string{"static"},
Paul Duffina0843f62019-12-13 19:50:38 +000044 },
45 prebuiltModuleType: "cc_prebuilt_library_static",
Paul Duffina0843f62019-12-13 19:50:38 +000046}
47
Paul Duffin9b76c0b2020-03-12 10:24:35 +000048var staticAndSharedLibrarySdkMemberType = &librarySdkMemberType{
49 SdkMemberTypeBase: android.SdkMemberTypeBase{
Liz Kammer96320df2022-05-12 20:40:00 -040050 PropertyName: "native_libs",
51 OverridesPropertyNames: map[string]bool{"native_shared_libs": true, "native_static_libs": true},
52 SupportsSdk: true,
53 HostOsDependent: true,
54 SupportedLinkageNames: []string{"static", "shared"},
Paul Duffin9b76c0b2020-03-12 10:24:35 +000055 },
56 prebuiltModuleType: "cc_prebuilt_library",
Paul Duffin9b76c0b2020-03-12 10:24:35 +000057}
58
Paul Duffin255f18e2019-12-13 11:22:16 +000059func init() {
60 // Register sdk member types.
Paul Duffina0843f62019-12-13 19:50:38 +000061 android.RegisterSdkMemberType(sharedLibrarySdkMemberType)
62 android.RegisterSdkMemberType(staticLibrarySdkMemberType)
Paul Duffin9b76c0b2020-03-12 10:24:35 +000063 android.RegisterSdkMemberType(staticAndSharedLibrarySdkMemberType)
Paul Duffin2f6bc092019-12-13 10:40:56 +000064}
65
66type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +000067 android.SdkMemberTypeBase
68
Paul Duffin2f6bc092019-12-13 10:40:56 +000069 prebuiltModuleType string
70
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000071 noOutputFiles bool // True if there are no srcs files.
72
Paul Duffin2f6bc092019-12-13 10:40:56 +000073}
74
Paul Duffin296701e2021-07-14 10:29:36 +010075func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
Paul Duffin93b750e2019-11-19 19:44:10 +000076 // The base set of targets which does not include native bridge targets.
77 defaultTargets := ctx.MultiTargets()
78
79 // The lazily created list of native bridge targets.
80 var includeNativeBridgeTargets []android.Target
81
Paul Duffin2f6bc092019-12-13 10:40:56 +000082 for _, lib := range names {
Paul Duffin93b750e2019-11-19 19:44:10 +000083 targets := defaultTargets
84
85 // If native bridge support is required in the sdk snapshot then add native bridge targets to
86 // the basic list of targets that are required.
87 nativeBridgeSupport := ctx.RequiresTrait(lib, nativeBridgeSdkTrait)
88 if nativeBridgeSupport && ctx.Device() {
89 // If not already computed then compute the list of native bridge targets.
90 if includeNativeBridgeTargets == nil {
91 includeNativeBridgeTargets = append([]android.Target{}, defaultTargets...)
92 allAndroidTargets := ctx.Config().Targets[android.Android]
93 for _, possibleNativeBridgeTarget := range allAndroidTargets {
94 if possibleNativeBridgeTarget.NativeBridge == android.NativeBridgeEnabled {
95 includeNativeBridgeTargets = append(includeNativeBridgeTargets, possibleNativeBridgeTarget)
96 }
97 }
98 }
99
100 // Include the native bridge targets as well.
101 targets = includeNativeBridgeTargets
102 }
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100103
104 // memberDependency encapsulates information about the dependencies to add for this member.
105 type memberDependency struct {
106 // The targets to depend upon.
107 targets []android.Target
108
109 // Additional image variations to depend upon, is either nil for no image variation or
110 // contains a single image variation.
111 imageVariations []blueprint.Variation
112 }
113
114 // Extract the name and version from the module name.
115 name, version := StubsLibNameAndVersion(lib)
116 if version == "" {
117 version = "latest"
118 }
119
120 // Compute the set of dependencies to add.
121 var memberDependencies []memberDependency
122 if ctx.Host() {
123 // Host does not support image variations so add a dependency without any.
124 memberDependencies = append(memberDependencies, memberDependency{
125 targets: targets,
126 })
127 } else {
128 // Otherwise, this is targeting the device so add a dependency on the core image variation
129 // (image:"").
130 memberDependencies = append(memberDependencies, memberDependency{
131 imageVariations: []blueprint.Variation{{Mutator: "image", Variation: android.CoreVariation}},
132 targets: targets,
133 })
Paul Duffin63696222021-09-06 10:28:34 +0100134
Paul Duffin12a0a312021-09-15 17:25:10 +0100135 // If required add additional dependencies on the image:ramdisk variants.
136 if ctx.RequiresTrait(lib, ramdiskImageRequiredSdkTrait) {
137 memberDependencies = append(memberDependencies, memberDependency{
138 imageVariations: []blueprint.Variation{{Mutator: "image", Variation: android.RamdiskVariation}},
139 // Only add a dependency on the first target as that is the only one which will have an
140 // image:ramdisk variant.
141 targets: targets[:1],
142 })
143 }
144
Paul Duffin63696222021-09-06 10:28:34 +0100145 // If required add additional dependencies on the image:recovery variants.
146 if ctx.RequiresTrait(lib, recoveryImageRequiredSdkTrait) {
147 memberDependencies = append(memberDependencies, memberDependency{
148 imageVariations: []blueprint.Variation{{Mutator: "image", Variation: android.RecoveryVariation}},
149 // Only add a dependency on the first target as that is the only one which will have an
150 // image:recovery variant.
151 targets: targets[:1],
152 })
153 }
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100154 }
155
156 // For each dependency in the list add dependencies on the targets with the correct variations.
157 for _, dependency := range memberDependencies {
158 // For each target add a dependency on the target with any additional dependencies.
159 for _, target := range dependency.targets {
160 // Get the variations for the target.
161 variations := target.Variations()
162
163 // Add any additional dependencies needed.
164 variations = append(variations, dependency.imageVariations...)
165
Liz Kammer96320df2022-05-12 20:40:00 -0400166 if mt.SupportedLinkageNames == nil {
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100167 // No link types are supported so add a dependency directly.
168 ctx.AddFarVariationDependencies(variations, dependencyTag, name)
169 } else {
170 // Otherwise, add a dependency on each supported link type in turn.
Liz Kammer96320df2022-05-12 20:40:00 -0400171 for _, linkType := range mt.SupportedLinkageNames {
Paul Duffinb1f0f2a2021-09-09 17:06:07 +0100172 libVariations := append(variations,
173 blueprint.Variation{Mutator: "link", Variation: linkType})
174 // If this is for the device and a shared link type then add a dependency onto the
175 // appropriate version specific variant of the module.
176 if ctx.Device() && linkType == "shared" {
177 libVariations = append(libVariations,
178 blueprint.Variation{Mutator: "version", Variation: version})
179 }
180 ctx.AddFarVariationDependencies(libVariations, dependencyTag, name)
Colin Crossa717db72020-10-23 14:53:06 -0700181 }
Paul Duffin91756d22020-02-21 16:29:57 +0000182 }
Paul Duffin2f6bc092019-12-13 10:40:56 +0000183 }
184 }
185 }
186}
187
188func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
Paul Duffina0843f62019-12-13 19:50:38 +0000189 // Check the module to see if it can be used with this module type.
190 if m, ok := module.(*Module); ok {
191 for _, allowableMemberType := range m.sdkMemberTypes {
192 if allowableMemberType == mt {
193 return true
194 }
195 }
196 }
197
198 return false
Paul Duffin2f6bc092019-12-13 10:40:56 +0000199}
200
Paul Duffin3a4eb502020-03-19 16:11:18 +0000201func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
202 pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, mt.prebuiltModuleType)
Paul Duffin0c394f32020-03-05 14:09:58 +0000203
204 ccModule := member.Variants()[0].(*Module)
205
Paul Duffin93b750e2019-11-19 19:44:10 +0000206 if ctx.RequiresTrait(nativeBridgeSdkTrait) {
207 pbm.AddProperty("native_bridge_supported", true)
208 }
209
Paul Duffin12a0a312021-09-15 17:25:10 +0100210 if ctx.RequiresTrait(ramdiskImageRequiredSdkTrait) {
211 pbm.AddProperty("ramdisk_available", true)
212 }
213
Paul Duffin63696222021-09-06 10:28:34 +0100214 if ctx.RequiresTrait(recoveryImageRequiredSdkTrait) {
Paul Duffind6abaa72020-09-07 16:39:22 +0100215 pbm.AddProperty("recovery_available", true)
216 }
217
Paul Duffind1edbd42020-08-13 19:45:31 +0100218 if proptools.Bool(ccModule.VendorProperties.Vendor_available) {
219 pbm.AddProperty("vendor_available", true)
220 }
221
Justin Yunebcf0c52021-01-08 18:00:19 +0900222 if proptools.Bool(ccModule.VendorProperties.Odm_available) {
223 pbm.AddProperty("odm_available", true)
224 }
225
Justin Yun63e9ec72020-10-29 16:49:43 +0900226 if proptools.Bool(ccModule.VendorProperties.Product_available) {
227 pbm.AddProperty("product_available", true)
228 }
229
Paul Duffin0c394f32020-03-05 14:09:58 +0000230 sdkVersion := ccModule.SdkVersion()
231 if sdkVersion != "" {
232 pbm.AddProperty("sdk_version", sdkVersion)
233 }
Paul Duffin2f6bc092019-12-13 10:40:56 +0000234
Paul Duffin13f02712020-03-06 12:30:43 +0000235 stl := ccModule.stl.Properties.Stl
236 if stl != nil {
Paul Duffin0174d8d2020-03-11 18:42:08 +0000237 pbm.AddProperty("stl", proptools.String(stl))
Paul Duffin13f02712020-03-06 12:30:43 +0000238 }
Martin Stjernholm47ed3522020-06-17 22:52:25 +0100239
240 if lib, ok := ccModule.linker.(*libraryDecorator); ok {
241 uhs := lib.Properties.Unique_host_soname
242 if uhs != nil {
243 pbm.AddProperty("unique_host_soname", proptools.Bool(uhs))
244 }
245 }
246
Paul Duffin0174d8d2020-03-11 18:42:08 +0000247 return pbm
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000248}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000249
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000250func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
251 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000252}
253
254func isGeneratedHeaderDirectory(p android.Path) bool {
255 _, gen := p.(android.WritablePath)
Colin Cross8ff10582023-12-07 13:10:56 -0800256 return gen
Paul Duffin2f6bc092019-12-13 10:40:56 +0000257}
258
Paul Duffin64f54b02020-02-20 14:33:54 +0000259type includeDirsProperty struct {
260 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000261 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000262
263 // The name of the property in the prebuilt library, "" means there is no property.
264 propertyName string
265
266 // The directory within the snapshot directory into which items should be copied.
267 snapshotDir string
268
269 // True if the items on the path should be copied.
270 copy bool
271
272 // True if the paths represent directories, files if they represent files.
273 dirs bool
Paul Duffin74fc1902020-01-23 11:45:03 +0000274}
275
Paul Duffin64f54b02020-02-20 14:33:54 +0000276var includeDirProperties = []includeDirsProperty{
277 {
278 // ExportedIncludeDirs lists directories that contains some header files to be
279 // copied into a directory in the snapshot. The snapshot directories must be added to
280 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000281 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000282 propertyName: "export_include_dirs",
283 snapshotDir: nativeIncludeDir,
284 copy: true,
285 dirs: true,
286 },
287 {
288 // ExportedSystemIncludeDirs lists directories that contains some system header files to
289 // be copied into a directory in the snapshot. The snapshot directories must be added to
290 // the export_system_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000291 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000292 propertyName: "export_system_include_dirs",
293 snapshotDir: nativeIncludeDir,
294 copy: true,
295 dirs: true,
296 },
297 {
Paul Duffin7a7d0672021-02-17 12:17:40 +0000298 // ExportedGeneratedIncludeDirs lists directories that contains some header files
299 // that are explicitly listed in the ExportedGeneratedHeaders property. So, the contents
Paul Duffin64f54b02020-02-20 14:33:54 +0000300 // of these directories do not need to be copied, but these directories do need adding to
301 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin7a7d0672021-02-17 12:17:40 +0000302 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000303 propertyName: "export_include_dirs",
304 snapshotDir: nativeGeneratedIncludeDir,
305 copy: false,
306 dirs: true,
307 },
308 {
Paul Duffin7a7d0672021-02-17 12:17:40 +0000309 // ExportedGeneratedHeaders lists header files that are in one of the directories
310 // specified in ExportedGeneratedIncludeDirs must be copied into the snapshot.
311 // As they are in a directory in ExportedGeneratedIncludeDirs they do not need adding to a
Paul Duffin64f54b02020-02-20 14:33:54 +0000312 // property in the prebuilt module in the snapshot.
Paul Duffin7a7d0672021-02-17 12:17:40 +0000313 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000314 propertyName: "",
315 snapshotDir: nativeGeneratedIncludeDir,
316 copy: true,
317 dirs: false,
318 },
319}
320
321// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000322func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
323
Martin Stjernholmb0249572020-09-15 02:32:35 +0100324 outputProperties.AddProperty("sanitize", &libInfo.Sanitize)
Martin Stjernholmfbb486f2020-08-21 18:43:51 +0100325
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000326 // Copy the generated library to the snapshot and add a reference to it in the .bp module.
327 if libInfo.outputFile != nil {
328 nativeLibraryPath := nativeLibraryPathFor(libInfo)
329 builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
330 outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
331 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000332
Paul Duffin13f02712020-03-06 12:30:43 +0000333 if len(libInfo.SharedLibs) > 0 {
334 outputProperties.AddPropertyWithTag("shared_libs", libInfo.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
335 }
336
Martin Stjernholm10566a02020-03-24 01:19:52 +0000337 // SystemSharedLibs needs to be propagated if it's a list, even if it's empty,
338 // so check for non-nil instead of nonzero length.
339 if libInfo.SystemSharedLibs != nil {
Paul Duffin13f02712020-03-06 12:30:43 +0000340 outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
341 }
342
Paul Duffin64f54b02020-02-20 14:33:54 +0000343 // Map from property name to the include dirs to add to the prebuilt module in the snapshot.
344 includeDirs := make(map[string][]string)
345
346 // Iterate over each include directory property, copying files and collating property
347 // values where necessary.
348 for _, propertyInfo := range includeDirProperties {
349 // Calculate the base directory in the snapshot into which the files will be copied.
Paul Duffin96f18322021-09-03 17:53:38 +0100350 // lib.archSubDir is "" for common properties.
351 targetDir := filepath.Join(libInfo.OsPrefix(), libInfo.archSubDir, propertyInfo.snapshotDir)
Paul Duffin64f54b02020-02-20 14:33:54 +0000352
353 propertyName := propertyInfo.propertyName
354
355 // Iterate over each path in one of the include directory properties.
356 for _, path := range propertyInfo.pathsGetter(libInfo) {
Paul Duffin42dd4e62021-02-22 11:35:24 +0000357 inputPath := path.String()
358
359 // Map the input path to a snapshot relative path. The mapping is independent of the module
360 // that references them so that if multiple modules within the same snapshot export the same
361 // header files they end up in the same place in the snapshot and so do not get duplicated.
362 targetRelativePath := inputPath
363 if isGeneratedHeaderDirectory(path) {
364 // Remove everything up to the .intermediates/ from the generated output directory to
365 // leave a module relative path.
366 base := android.PathForIntermediates(sdkModuleContext, "")
367 targetRelativePath = android.Rel(sdkModuleContext, base.String(), inputPath)
368 }
369
370 snapshotRelativePath := filepath.Join(targetDir, targetRelativePath)
Paul Duffin64f54b02020-02-20 14:33:54 +0000371
372 // Copy the files/directories when necessary.
373 if propertyInfo.copy {
374 if propertyInfo.dirs {
375 // When copying a directory glob and copy all the headers within it.
376 // TODO(jiyong) copy headers having other suffixes
Paul Duffin42dd4e62021-02-22 11:35:24 +0000377 headers, _ := sdkModuleContext.GlobWithDeps(inputPath+"/**/*.h", nil)
Paul Duffin64f54b02020-02-20 14:33:54 +0000378 for _, file := range headers {
379 src := android.PathForSource(sdkModuleContext, file)
Paul Duffin42dd4e62021-02-22 11:35:24 +0000380
381 // The destination path in the snapshot is constructed from the snapshot relative path
382 // of the input directory and the input directory relative path of the header file.
383 inputRelativePath := android.Rel(sdkModuleContext, inputPath, file)
384 dest := filepath.Join(snapshotRelativePath, inputRelativePath)
Paul Duffin64f54b02020-02-20 14:33:54 +0000385 builder.CopyToSnapshot(src, dest)
386 }
387 } else {
Paul Duffin42dd4e62021-02-22 11:35:24 +0000388 // Otherwise, just copy the file to its snapshot relative path.
389 builder.CopyToSnapshot(path, snapshotRelativePath)
Paul Duffin64f54b02020-02-20 14:33:54 +0000390 }
391 }
392
393 // Only directories are added to a property.
394 if propertyInfo.dirs {
Paul Duffin42dd4e62021-02-22 11:35:24 +0000395 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotRelativePath)
Paul Duffin64f54b02020-02-20 14:33:54 +0000396 }
397 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000398 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000399
400 // Add the collated include dir properties to the output.
Cole Faust18994c72023-02-28 16:02:16 -0800401 for _, property := range android.SortedKeys(includeDirs) {
Colin Cross2c033612020-09-11 15:44:31 -0700402 outputProperties.AddProperty(property, includeDirs[property])
Paul Duffin74fc1902020-01-23 11:45:03 +0000403 }
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100404
Martin Stjernholm618b6712020-09-24 16:53:04 +0100405 if len(libInfo.StubsVersions) > 0 {
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100406 stubsSet := outputProperties.AddPropertySet("stubs")
Martin Stjernholm618b6712020-09-24 16:53:04 +0100407 stubsSet.AddProperty("versions", libInfo.StubsVersions)
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100408 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000409}
410
Paul Duffin2f6bc092019-12-13 10:40:56 +0000411const (
412 nativeIncludeDir = "include"
413 nativeGeneratedIncludeDir = "include_gen"
414 nativeStubDir = "lib"
415)
416
417// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000418func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffin96f18322021-09-03 17:53:38 +0100419 return filepath.Join(lib.OsPrefix(), lib.archSubDir,
Paul Duffin2f6bc092019-12-13 10:40:56 +0000420 nativeStubDir, lib.outputFile.Base())
421}
422
Paul Duffin2f6bc092019-12-13 10:40:56 +0000423// nativeLibInfoProperties represents properties of a native lib
424//
425// The exported (capitalized) fields will be examined and may be changed during common value extraction.
426// The unexported fields will be left untouched.
427type nativeLibInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000428 android.SdkMemberPropertiesBase
429
430 memberType *librarySdkMemberType
431
Paul Duffin96f18322021-09-03 17:53:38 +0100432 // archSubDir is the subdirectory within the OS directory in the sdk snapshot into which arch
433 // specific files will be copied.
434 //
435 // It is not exported since any value other than "" is always going to be arch specific.
436 // This is "" for non-arch specific common properties.
437 archSubDir string
Paul Duffin2f6bc092019-12-13 10:40:56 +0000438
Paul Duffin5efd1982020-02-20 14:33:54 +0000439 // The list of possibly common exported include dirs.
440 //
441 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100442 ExportedIncludeDirs android.Paths `android:"arch_variant"`
Paul Duffin2f6bc092019-12-13 10:40:56 +0000443
Paul Duffin5efd1982020-02-20 14:33:54 +0000444 // The list of arch specific exported generated include dirs.
445 //
Paul Duffin7a7d0672021-02-17 12:17:40 +0000446 // This field is exported as its contents may not be arch specific, e.g. protos.
447 ExportedGeneratedIncludeDirs android.Paths `android:"arch_variant"`
Paul Duffin5efd1982020-02-20 14:33:54 +0000448
449 // The list of arch specific exported generated header files.
450 //
Paul Duffin7a7d0672021-02-17 12:17:40 +0000451 // This field is exported as its contents may not be arch specific, e.g. protos.
452 ExportedGeneratedHeaders android.Paths `android:"arch_variant"`
Paul Duffin2f6bc092019-12-13 10:40:56 +0000453
Paul Duffin5efd1982020-02-20 14:33:54 +0000454 // The list of possibly common exported system include dirs.
455 //
456 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100457 ExportedSystemIncludeDirs android.Paths `android:"arch_variant"`
Paul Duffin5efd1982020-02-20 14:33:54 +0000458
459 // The list of possibly common exported flags.
460 //
461 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100462 ExportedFlags []string `android:"arch_variant"`
Paul Duffin5efd1982020-02-20 14:33:54 +0000463
Paul Duffin13f02712020-03-06 12:30:43 +0000464 // The set of shared libraries
465 //
466 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100467 SharedLibs []string `android:"arch_variant"`
Paul Duffin13f02712020-03-06 12:30:43 +0000468
Martin Stjernholm10566a02020-03-24 01:19:52 +0000469 // The set of system shared libraries. Note nil and [] are semantically
470 // distinct - see BaseLinkerProperties.System_shared_libs.
Paul Duffin13f02712020-03-06 12:30:43 +0000471 //
472 // This field is exported as its contents may not be arch specific.
Paul Duffin864e1b42020-05-06 10:23:19 +0100473 SystemSharedLibs []string `android:"arch_variant"`
Paul Duffin13f02712020-03-06 12:30:43 +0000474
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100475 // The specific stubs version for the lib variant, or empty string if stubs
476 // are not in use.
Paul Duffin7a1f7f32020-05-04 15:32:08 +0100477 //
Martin Stjernholm618b6712020-09-24 16:53:04 +0100478 // Marked 'ignored-on-host' as the AllStubsVersions() from which this is
479 // initialized is not set on host and the stubs.versions property which this
480 // is written to does not vary by arch so cannot be android specific.
481 StubsVersions []string `sdk:"ignored-on-host"`
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100482
Martin Stjernholmb0249572020-09-15 02:32:35 +0100483 // Value of SanitizeProperties.Sanitize. Several - but not all - of these
484 // affect the expanded variants. All are propagated to avoid entangling the
485 // sanitizer logic with the snapshot generation.
486 Sanitize SanitizeUserProps `android:"arch_variant"`
Martin Stjernholmfbb486f2020-08-21 18:43:51 +0100487
Paul Duffin2f6bc092019-12-13 10:40:56 +0000488 // outputFile is not exported as it is always arch specific.
489 outputFile android.Path
490}
491
Paul Duffin3a4eb502020-03-19 16:11:18 +0000492func (p *nativeLibInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Martin Stjernholm59e0c7a2020-10-28 23:38:33 +0000493 addOutputFile := true
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000494 ccModule := variant.(*Module)
495
Martin Stjernholm59e0c7a2020-10-28 23:38:33 +0000496 if s := ccModule.sanitize; s != nil {
497 // We currently do not capture sanitizer flags for libs with sanitizers
498 // enabled, because they may vary among variants that cannot be represented
499 // in the input blueprint files. In particular, sanitizerDepsMutator enables
500 // various sanitizers on dependencies, but in many cases only on static
501 // ones, and we cannot specify sanitizer flags at the link type level (i.e.
502 // in StaticOrSharedProperties).
503 if s.isUnsanitizedVariant() {
504 // This still captures explicitly disabled sanitizers, which may be
505 // necessary to avoid cyclic dependencies.
506 p.Sanitize = s.Properties.Sanitize
507 } else {
508 // Do not add the output file to the snapshot if we don't represent it
509 // properly.
510 addOutputFile = false
511 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000512 }
513
Colin Cross0de8a1e2020-09-18 14:15:30 -0700514 exportedInfo := ctx.SdkModuleContext().OtherModuleProvider(variant, FlagExporterInfoProvider).(FlagExporterInfo)
515
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000516 // Separate out the generated include dirs (which are arch specific) from the
517 // include dirs (which may not be).
518 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
Colin Cross0de8a1e2020-09-18 14:15:30 -0700519 exportedInfo.IncludeDirs, isGeneratedHeaderDirectory)
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000520
Paul Duffin93b750e2019-11-19 19:44:10 +0000521 target := ccModule.Target()
522 p.archSubDir = target.Arch.ArchType.String()
523 if target.NativeBridge == android.NativeBridgeEnabled {
524 p.archSubDir += "_native_bridge"
525 }
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000526
527 // Make sure that the include directories are unique.
528 p.ExportedIncludeDirs = android.FirstUniquePaths(exportedIncludeDirs)
Paul Duffin7a7d0672021-02-17 12:17:40 +0000529 p.ExportedGeneratedIncludeDirs = android.FirstUniquePaths(exportedGeneratedIncludeDirs)
Paul Duffinab5467d2020-06-18 16:31:04 +0100530
531 // Take a copy before filtering out duplicates to avoid changing the slice owned by the
532 // ccModule.
Colin Cross0de8a1e2020-09-18 14:15:30 -0700533 dirs := append(android.Paths(nil), exportedInfo.SystemIncludeDirs...)
Paul Duffinab5467d2020-06-18 16:31:04 +0100534 p.ExportedSystemIncludeDirs = android.FirstUniquePaths(dirs)
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000535
Colin Cross0de8a1e2020-09-18 14:15:30 -0700536 p.ExportedFlags = exportedInfo.Flags
Paul Duffin13f02712020-03-06 12:30:43 +0000537 if ccModule.linker != nil {
538 specifiedDeps := specifiedDeps{}
539 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
540
Colin Cross31076b32020-10-23 17:22:06 -0700541 if lib := ccModule.library; lib != nil {
542 if !lib.hasStubsVariants() {
543 // Propagate dynamic dependencies for implementation libs, but not stubs.
544 p.SharedLibs = specifiedDeps.sharedLibs
545 } else {
546 // TODO(b/169373910): 1. Only output the specific version (from
547 // ccModule.StubsVersion()) if the module is versioned. 2. Ensure that all
548 // the versioned stub libs are retained in the prebuilt tree; currently only
549 // the stub corresponding to ccModule.StubsVersion() is.
550 p.StubsVersions = lib.allStubsVersions()
551 }
Martin Stjernholmcc330d62020-04-21 20:45:35 +0100552 }
Paul Duffin13f02712020-03-06 12:30:43 +0000553 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
554 }
Paul Duffin7a7d0672021-02-17 12:17:40 +0000555 p.ExportedGeneratedHeaders = exportedInfo.GeneratedHeaders
Martin Stjernholmc5dd4f72020-04-01 20:38:01 +0100556
Martin Stjernholm59e0c7a2020-10-28 23:38:33 +0000557 if !p.memberType.noOutputFiles && addOutputFile {
558 p.outputFile = getRequiredMemberOutputFile(ctx, ccModule)
Martin Stjernholmfbb486f2020-08-21 18:43:51 +0100559 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000560}
561
Paul Duffin712993c2020-05-05 14:11:57 +0100562func getRequiredMemberOutputFile(ctx android.SdkMemberContext, ccModule *Module) android.Path {
563 var path android.Path
564 outputFile := ccModule.OutputFile()
565 if outputFile.Valid() {
566 path = outputFile.Path()
567 } else {
568 ctx.SdkModuleContext().ModuleErrorf("member variant %s does not have a valid output file", ccModule)
569 }
570 return path
571}
572
Paul Duffin3a4eb502020-03-19 16:11:18 +0000573func (p *nativeLibInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
574 addPossiblyArchSpecificProperties(ctx.SdkModuleContext(), ctx.SnapshotBuilder(), p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000575}