blob: f1b097561ad9e0cc19365beaa22ac6ca24b6dd69 [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 Duffin88f2fbe2020-02-27 16:00:53 +0000108 return pbm
Paul Duffin2f6bc092019-12-13 10:40:56 +0000109}
110
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000111func (mt *librarySdkMemberType) FinalizeModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember, bpModule android.BpModule) {
Paul Duffin13f02712020-03-06 12:30:43 +0000112 ccModule := (member.Variants()[0]).(*Module)
113 stl := ccModule.stl.Properties.Stl
114 if stl != nil {
115 bpModule.AddProperty("stl", proptools.String(stl))
116 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000117}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000118
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000119func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
120 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000121}
122
123func isGeneratedHeaderDirectory(p android.Path) bool {
124 _, gen := p.(android.WritablePath)
125 return gen
126}
127
Paul Duffin64f54b02020-02-20 14:33:54 +0000128type includeDirsProperty struct {
129 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000130 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000131
132 // The name of the property in the prebuilt library, "" means there is no property.
133 propertyName string
134
135 // The directory within the snapshot directory into which items should be copied.
136 snapshotDir string
137
138 // True if the items on the path should be copied.
139 copy bool
140
141 // True if the paths represent directories, files if they represent files.
142 dirs bool
Paul Duffin74fc1902020-01-23 11:45:03 +0000143}
144
Paul Duffin64f54b02020-02-20 14:33:54 +0000145var includeDirProperties = []includeDirsProperty{
146 {
147 // ExportedIncludeDirs lists directories that contains some header files to be
148 // copied into a directory in the snapshot. The snapshot directories must be added to
149 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000150 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000151 propertyName: "export_include_dirs",
152 snapshotDir: nativeIncludeDir,
153 copy: true,
154 dirs: true,
155 },
156 {
157 // ExportedSystemIncludeDirs lists directories that contains some system header files to
158 // be copied into a directory in the snapshot. The snapshot directories must be added to
159 // the export_system_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000160 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000161 propertyName: "export_system_include_dirs",
162 snapshotDir: nativeIncludeDir,
163 copy: true,
164 dirs: true,
165 },
166 {
167 // exportedGeneratedIncludeDirs lists directories that contains some header files
168 // that are explicitly listed in the exportedGeneratedHeaders property. So, the contents
169 // of these directories do not need to be copied, but these directories do need adding to
170 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000171 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000172 propertyName: "export_include_dirs",
173 snapshotDir: nativeGeneratedIncludeDir,
174 copy: false,
175 dirs: true,
176 },
177 {
178 // exportedGeneratedHeaders lists header files that are in one of the directories
179 // specified in exportedGeneratedIncludeDirs must be copied into the snapshot.
180 // As they are in a directory in exportedGeneratedIncludeDirs they do not need adding to a
181 // property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000182 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000183 propertyName: "",
184 snapshotDir: nativeGeneratedIncludeDir,
185 copy: true,
186 dirs: false,
187 },
188}
189
190// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000191func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
192
193 // Copy the generated library to the snapshot and add a reference to it in the .bp module.
194 if libInfo.outputFile != nil {
195 nativeLibraryPath := nativeLibraryPathFor(libInfo)
196 builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
197 outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
198 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000199
Paul Duffin13f02712020-03-06 12:30:43 +0000200 if len(libInfo.SharedLibs) > 0 {
201 outputProperties.AddPropertyWithTag("shared_libs", libInfo.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
202 }
203
204 if len(libInfo.SystemSharedLibs) > 0 {
205 outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
206 }
207
Paul Duffin64f54b02020-02-20 14:33:54 +0000208 // Map from property name to the include dirs to add to the prebuilt module in the snapshot.
209 includeDirs := make(map[string][]string)
210
211 // Iterate over each include directory property, copying files and collating property
212 // values where necessary.
213 for _, propertyInfo := range includeDirProperties {
214 // Calculate the base directory in the snapshot into which the files will be copied.
215 // lib.ArchType is "" for common properties.
216 targetDir := filepath.Join(libInfo.archType, propertyInfo.snapshotDir)
217
218 propertyName := propertyInfo.propertyName
219
220 // Iterate over each path in one of the include directory properties.
221 for _, path := range propertyInfo.pathsGetter(libInfo) {
222
223 // Copy the files/directories when necessary.
224 if propertyInfo.copy {
225 if propertyInfo.dirs {
226 // When copying a directory glob and copy all the headers within it.
227 // TODO(jiyong) copy headers having other suffixes
228 headers, _ := sdkModuleContext.GlobWithDeps(path.String()+"/**/*.h", nil)
229 for _, file := range headers {
230 src := android.PathForSource(sdkModuleContext, file)
231 dest := filepath.Join(targetDir, file)
232 builder.CopyToSnapshot(src, dest)
233 }
234 } else {
235 // Otherwise, just copy the files.
236 dest := filepath.Join(targetDir, libInfo.name, path.Rel())
237 builder.CopyToSnapshot(path, dest)
238 }
239 }
240
241 // Only directories are added to a property.
242 if propertyInfo.dirs {
243 var snapshotPath string
244 if isGeneratedHeaderDirectory(path) {
245 snapshotPath = filepath.Join(targetDir, libInfo.name)
246 } else {
247 snapshotPath = filepath.Join(targetDir, path.String())
248 }
249
250 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotPath)
251 }
252 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000253 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000254
255 // Add the collated include dir properties to the output.
256 for property, dirs := range includeDirs {
257 outputProperties.AddProperty(property, dirs)
Paul Duffin74fc1902020-01-23 11:45:03 +0000258 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000259}
260
Paul Duffin2f6bc092019-12-13 10:40:56 +0000261const (
262 nativeIncludeDir = "include"
263 nativeGeneratedIncludeDir = "include_gen"
264 nativeStubDir = "lib"
265)
266
267// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000268func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffina04c1072020-03-02 10:16:35 +0000269 return filepath.Join(lib.OsPrefix(), lib.archType,
Paul Duffin2f6bc092019-12-13 10:40:56 +0000270 nativeStubDir, lib.outputFile.Base())
271}
272
Paul Duffin2f6bc092019-12-13 10:40:56 +0000273// nativeLibInfoProperties represents properties of a native lib
274//
275// The exported (capitalized) fields will be examined and may be changed during common value extraction.
276// The unexported fields will be left untouched.
277type nativeLibInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000278 android.SdkMemberPropertiesBase
279
280 memberType *librarySdkMemberType
281
Paul Duffin2f6bc092019-12-13 10:40:56 +0000282 // The name of the library, is not exported as this must not be changed during optimization.
283 name string
284
285 // archType is not exported as if set (to a non default value) it is always arch specific.
286 // This is "" for common properties.
287 archType string
288
Paul Duffin5efd1982020-02-20 14:33:54 +0000289 // The list of possibly common exported include dirs.
290 //
291 // This field is exported as its contents may not be arch specific.
292 ExportedIncludeDirs android.Paths
Paul Duffin2f6bc092019-12-13 10:40:56 +0000293
Paul Duffin5efd1982020-02-20 14:33:54 +0000294 // The list of arch specific exported generated include dirs.
295 //
296 // This field is not exported as its contents are always arch specific.
297 exportedGeneratedIncludeDirs android.Paths
298
299 // The list of arch specific exported generated header files.
300 //
301 // This field is not exported as its contents are is always arch specific.
Paul Duffin2f6bc092019-12-13 10:40:56 +0000302 exportedGeneratedHeaders android.Paths
303
Paul Duffin5efd1982020-02-20 14:33:54 +0000304 // The list of possibly common exported system include dirs.
305 //
306 // This field is exported as its contents may not be arch specific.
307 ExportedSystemIncludeDirs android.Paths
308
309 // The list of possibly common exported flags.
310 //
311 // This field is exported as its contents may not be arch specific.
312 ExportedFlags []string
313
Paul Duffin13f02712020-03-06 12:30:43 +0000314 // The set of shared libraries
315 //
316 // This field is exported as its contents may not be arch specific.
317 SharedLibs []string
318
319 // The set of system shared libraries
320 //
321 // This field is exported as its contents may not be arch specific.
322 SystemSharedLibs []string
323
Paul Duffin2f6bc092019-12-13 10:40:56 +0000324 // outputFile is not exported as it is always arch specific.
325 outputFile android.Path
326}
327
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000328func (p *nativeLibInfoProperties) PopulateFromVariant(variant android.SdkAware) {
329 ccModule := variant.(*Module)
330
331 // If the library has some link types then it produces an output binary file, otherwise it
332 // is header only.
333 if p.memberType.linkTypes != nil {
334 p.outputFile = ccModule.OutputFile().Path()
335 }
336
337 // Separate out the generated include dirs (which are arch specific) from the
338 // include dirs (which may not be).
339 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
340 ccModule.ExportedIncludeDirs(), isGeneratedHeaderDirectory)
341
342 p.name = variant.Name()
343 p.archType = ccModule.Target().Arch.ArchType.String()
344 p.ExportedIncludeDirs = exportedIncludeDirs
345 p.exportedGeneratedIncludeDirs = exportedGeneratedIncludeDirs
346 p.ExportedSystemIncludeDirs = ccModule.ExportedSystemIncludeDirs()
347 p.ExportedFlags = ccModule.ExportedFlags()
Paul Duffin13f02712020-03-06 12:30:43 +0000348 if ccModule.linker != nil {
349 specifiedDeps := specifiedDeps{}
350 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
351
352 p.SharedLibs = specifiedDeps.sharedLibs
353 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
354 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000355 p.exportedGeneratedHeaders = ccModule.ExportedGeneratedHeaders()
356}
357
358func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
359 addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000360}