blob: 843ebb04af3780f5ac7fa6e6109ae42eccaf8481 [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"
21 "github.com/google/blueprint"
Paul Duffin13f02712020-03-06 12:30:43 +000022 "github.com/google/blueprint/proptools"
Paul Duffin2f6bc092019-12-13 10:40:56 +000023)
24
25// This file contains support for using cc library modules within an sdk.
26
Paul Duffina0843f62019-12-13 19:50:38 +000027var sharedLibrarySdkMemberType = &librarySdkMemberType{
28 SdkMemberTypeBase: android.SdkMemberTypeBase{
29 PropertyName: "native_shared_libs",
Paul Duffine6029182019-12-16 17:43:48 +000030 SupportsSdk: true,
Paul Duffina0843f62019-12-13 19:50:38 +000031 },
32 prebuiltModuleType: "cc_prebuilt_library_shared",
33 linkTypes: []string{"shared"},
34}
35
36var staticLibrarySdkMemberType = &librarySdkMemberType{
37 SdkMemberTypeBase: android.SdkMemberTypeBase{
38 PropertyName: "native_static_libs",
Paul Duffine6029182019-12-16 17:43:48 +000039 SupportsSdk: true,
Paul Duffina0843f62019-12-13 19:50:38 +000040 },
41 prebuiltModuleType: "cc_prebuilt_library_static",
42 linkTypes: []string{"static"},
43}
44
Paul Duffin255f18e2019-12-13 11:22:16 +000045func init() {
46 // Register sdk member types.
Paul Duffina0843f62019-12-13 19:50:38 +000047 android.RegisterSdkMemberType(sharedLibrarySdkMemberType)
48 android.RegisterSdkMemberType(staticLibrarySdkMemberType)
Paul Duffin2f6bc092019-12-13 10:40:56 +000049}
50
51type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +000052 android.SdkMemberTypeBase
53
Paul Duffin2f6bc092019-12-13 10:40:56 +000054 prebuiltModuleType string
55
56 // The set of link types supported, set of "static", "shared".
57 linkTypes []string
58}
59
60func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
61 targets := mctx.MultiTargets()
62 for _, lib := range names {
63 for _, target := range targets {
64 name, version := StubsLibNameAndVersion(lib)
65 if version == "" {
66 version = LatestStubsVersionFor(mctx.Config(), name)
67 }
Paul Duffin91756d22020-02-21 16:29:57 +000068 if mt.linkTypes == nil {
Paul Duffin2f6bc092019-12-13 10:40:56 +000069 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
70 {Mutator: "image", Variation: android.CoreVariation},
Paul Duffin2f6bc092019-12-13 10:40:56 +000071 {Mutator: "version", Variation: version},
72 }...), dependencyTag, name)
Paul Duffin91756d22020-02-21 16:29:57 +000073 } else {
74 for _, linkType := range mt.linkTypes {
75 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
76 {Mutator: "image", Variation: android.CoreVariation},
77 {Mutator: "link", Variation: linkType},
78 {Mutator: "version", Variation: version},
79 }...), dependencyTag, name)
80 }
Paul Duffin2f6bc092019-12-13 10:40:56 +000081 }
82 }
83 }
84}
85
86func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
Paul Duffina0843f62019-12-13 19:50:38 +000087 // Check the module to see if it can be used with this module type.
88 if m, ok := module.(*Module); ok {
89 for _, allowableMemberType := range m.sdkMemberTypes {
90 if allowableMemberType == mt {
91 return true
92 }
93 }
94 }
95
96 return false
Paul Duffin2f6bc092019-12-13 10:40:56 +000097}
98
Paul Duffin88f2fbe2020-02-27 16:00:53 +000099func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
100 pbm := builder.AddPrebuiltModule(member, mt.prebuiltModuleType)
Paul Duffin0c394f32020-03-05 14:09:58 +0000101
102 ccModule := member.Variants()[0].(*Module)
103
104 sdkVersion := ccModule.SdkVersion()
105 if sdkVersion != "" {
106 pbm.AddProperty("sdk_version", sdkVersion)
107 }
Paul Duffin2f6bc092019-12-13 10:40:56 +0000108
Paul Duffin13f02712020-03-06 12:30:43 +0000109 stl := ccModule.stl.Properties.Stl
110 if stl != nil {
Paul Duffin0174d8d2020-03-11 18:42:08 +0000111 pbm.AddProperty("stl", proptools.String(stl))
Paul Duffin13f02712020-03-06 12:30:43 +0000112 }
Paul Duffin0174d8d2020-03-11 18:42:08 +0000113 return pbm
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000114}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000115
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000116func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
117 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000118}
119
120func isGeneratedHeaderDirectory(p android.Path) bool {
121 _, gen := p.(android.WritablePath)
122 return gen
123}
124
Paul Duffin64f54b02020-02-20 14:33:54 +0000125type includeDirsProperty struct {
126 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000127 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000128
129 // The name of the property in the prebuilt library, "" means there is no property.
130 propertyName string
131
132 // The directory within the snapshot directory into which items should be copied.
133 snapshotDir string
134
135 // True if the items on the path should be copied.
136 copy bool
137
138 // True if the paths represent directories, files if they represent files.
139 dirs bool
Paul Duffin74fc1902020-01-23 11:45:03 +0000140}
141
Paul Duffin64f54b02020-02-20 14:33:54 +0000142var includeDirProperties = []includeDirsProperty{
143 {
144 // ExportedIncludeDirs lists directories that contains some header files to be
145 // copied into a directory in the snapshot. The snapshot directories must be added to
146 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000147 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000148 propertyName: "export_include_dirs",
149 snapshotDir: nativeIncludeDir,
150 copy: true,
151 dirs: true,
152 },
153 {
154 // ExportedSystemIncludeDirs lists directories that contains some system header files to
155 // be copied into a directory in the snapshot. The snapshot directories must be added to
156 // the export_system_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000157 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000158 propertyName: "export_system_include_dirs",
159 snapshotDir: nativeIncludeDir,
160 copy: true,
161 dirs: true,
162 },
163 {
164 // exportedGeneratedIncludeDirs lists directories that contains some header files
165 // that are explicitly listed in the exportedGeneratedHeaders property. So, the contents
166 // of these directories do not need to be copied, but these directories do need adding to
167 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000168 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000169 propertyName: "export_include_dirs",
170 snapshotDir: nativeGeneratedIncludeDir,
171 copy: false,
172 dirs: true,
173 },
174 {
175 // exportedGeneratedHeaders lists header files that are in one of the directories
176 // specified in exportedGeneratedIncludeDirs must be copied into the snapshot.
177 // As they are in a directory in exportedGeneratedIncludeDirs they do not need adding to a
178 // property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000179 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000180 propertyName: "",
181 snapshotDir: nativeGeneratedIncludeDir,
182 copy: true,
183 dirs: false,
184 },
185}
186
187// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000188func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
189
190 // Copy the generated library to the snapshot and add a reference to it in the .bp module.
191 if libInfo.outputFile != nil {
192 nativeLibraryPath := nativeLibraryPathFor(libInfo)
193 builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
194 outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
195 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000196
Paul Duffin13f02712020-03-06 12:30:43 +0000197 if len(libInfo.SharedLibs) > 0 {
198 outputProperties.AddPropertyWithTag("shared_libs", libInfo.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
199 }
200
201 if len(libInfo.SystemSharedLibs) > 0 {
202 outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
203 }
204
Paul Duffin64f54b02020-02-20 14:33:54 +0000205 // Map from property name to the include dirs to add to the prebuilt module in the snapshot.
206 includeDirs := make(map[string][]string)
207
208 // Iterate over each include directory property, copying files and collating property
209 // values where necessary.
210 for _, propertyInfo := range includeDirProperties {
211 // Calculate the base directory in the snapshot into which the files will be copied.
212 // lib.ArchType is "" for common properties.
213 targetDir := filepath.Join(libInfo.archType, propertyInfo.snapshotDir)
214
215 propertyName := propertyInfo.propertyName
216
217 // Iterate over each path in one of the include directory properties.
218 for _, path := range propertyInfo.pathsGetter(libInfo) {
219
220 // Copy the files/directories when necessary.
221 if propertyInfo.copy {
222 if propertyInfo.dirs {
223 // When copying a directory glob and copy all the headers within it.
224 // TODO(jiyong) copy headers having other suffixes
225 headers, _ := sdkModuleContext.GlobWithDeps(path.String()+"/**/*.h", nil)
226 for _, file := range headers {
227 src := android.PathForSource(sdkModuleContext, file)
228 dest := filepath.Join(targetDir, file)
229 builder.CopyToSnapshot(src, dest)
230 }
231 } else {
232 // Otherwise, just copy the files.
233 dest := filepath.Join(targetDir, libInfo.name, path.Rel())
234 builder.CopyToSnapshot(path, dest)
235 }
236 }
237
238 // Only directories are added to a property.
239 if propertyInfo.dirs {
240 var snapshotPath string
241 if isGeneratedHeaderDirectory(path) {
242 snapshotPath = filepath.Join(targetDir, libInfo.name)
243 } else {
244 snapshotPath = filepath.Join(targetDir, path.String())
245 }
246
247 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotPath)
248 }
249 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000250 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000251
252 // Add the collated include dir properties to the output.
253 for property, dirs := range includeDirs {
254 outputProperties.AddProperty(property, dirs)
Paul Duffin74fc1902020-01-23 11:45:03 +0000255 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000256}
257
Paul Duffin2f6bc092019-12-13 10:40:56 +0000258const (
259 nativeIncludeDir = "include"
260 nativeGeneratedIncludeDir = "include_gen"
261 nativeStubDir = "lib"
262)
263
264// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000265func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffina04c1072020-03-02 10:16:35 +0000266 return filepath.Join(lib.OsPrefix(), lib.archType,
Paul Duffin2f6bc092019-12-13 10:40:56 +0000267 nativeStubDir, lib.outputFile.Base())
268}
269
Paul Duffin2f6bc092019-12-13 10:40:56 +0000270// nativeLibInfoProperties represents properties of a native lib
271//
272// The exported (capitalized) fields will be examined and may be changed during common value extraction.
273// The unexported fields will be left untouched.
274type nativeLibInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000275 android.SdkMemberPropertiesBase
276
277 memberType *librarySdkMemberType
278
Paul Duffin2f6bc092019-12-13 10:40:56 +0000279 // The name of the library, is not exported as this must not be changed during optimization.
280 name string
281
282 // archType is not exported as if set (to a non default value) it is always arch specific.
283 // This is "" for common properties.
284 archType string
285
Paul Duffin5efd1982020-02-20 14:33:54 +0000286 // The list of possibly common exported include dirs.
287 //
288 // This field is exported as its contents may not be arch specific.
289 ExportedIncludeDirs android.Paths
Paul Duffin2f6bc092019-12-13 10:40:56 +0000290
Paul Duffin5efd1982020-02-20 14:33:54 +0000291 // The list of arch specific exported generated include dirs.
292 //
293 // This field is not exported as its contents are always arch specific.
294 exportedGeneratedIncludeDirs android.Paths
295
296 // The list of arch specific exported generated header files.
297 //
298 // This field is not exported as its contents are is always arch specific.
Paul Duffin2f6bc092019-12-13 10:40:56 +0000299 exportedGeneratedHeaders android.Paths
300
Paul Duffin5efd1982020-02-20 14:33:54 +0000301 // The list of possibly common exported system include dirs.
302 //
303 // This field is exported as its contents may not be arch specific.
304 ExportedSystemIncludeDirs android.Paths
305
306 // The list of possibly common exported flags.
307 //
308 // This field is exported as its contents may not be arch specific.
309 ExportedFlags []string
310
Paul Duffin13f02712020-03-06 12:30:43 +0000311 // The set of shared libraries
312 //
313 // This field is exported as its contents may not be arch specific.
314 SharedLibs []string
315
316 // The set of system shared libraries
317 //
318 // This field is exported as its contents may not be arch specific.
319 SystemSharedLibs []string
320
Paul Duffin2f6bc092019-12-13 10:40:56 +0000321 // outputFile is not exported as it is always arch specific.
322 outputFile android.Path
323}
324
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000325func (p *nativeLibInfoProperties) PopulateFromVariant(variant android.SdkAware) {
326 ccModule := variant.(*Module)
327
328 // If the library has some link types then it produces an output binary file, otherwise it
329 // is header only.
330 if p.memberType.linkTypes != nil {
331 p.outputFile = ccModule.OutputFile().Path()
332 }
333
334 // Separate out the generated include dirs (which are arch specific) from the
335 // include dirs (which may not be).
336 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
337 ccModule.ExportedIncludeDirs(), isGeneratedHeaderDirectory)
338
339 p.name = variant.Name()
340 p.archType = ccModule.Target().Arch.ArchType.String()
341 p.ExportedIncludeDirs = exportedIncludeDirs
342 p.exportedGeneratedIncludeDirs = exportedGeneratedIncludeDirs
343 p.ExportedSystemIncludeDirs = ccModule.ExportedSystemIncludeDirs()
344 p.ExportedFlags = ccModule.ExportedFlags()
Paul Duffin13f02712020-03-06 12:30:43 +0000345 if ccModule.linker != nil {
346 specifiedDeps := specifiedDeps{}
347 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
348
349 p.SharedLibs = specifiedDeps.sharedLibs
350 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
351 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000352 p.exportedGeneratedHeaders = ccModule.ExportedGeneratedHeaders()
353}
354
355func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
356 addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000357}