blob: a262697723307a31b4e02d135cdbe41f09c931ba [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"
22)
23
24// This file contains support for using cc library modules within an sdk.
25
Paul Duffina0843f62019-12-13 19:50:38 +000026var sharedLibrarySdkMemberType = &librarySdkMemberType{
27 SdkMemberTypeBase: android.SdkMemberTypeBase{
28 PropertyName: "native_shared_libs",
Paul Duffine6029182019-12-16 17:43:48 +000029 SupportsSdk: true,
Paul Duffina0843f62019-12-13 19:50:38 +000030 },
31 prebuiltModuleType: "cc_prebuilt_library_shared",
32 linkTypes: []string{"shared"},
33}
34
35var staticLibrarySdkMemberType = &librarySdkMemberType{
36 SdkMemberTypeBase: android.SdkMemberTypeBase{
37 PropertyName: "native_static_libs",
Paul Duffine6029182019-12-16 17:43:48 +000038 SupportsSdk: true,
Paul Duffina0843f62019-12-13 19:50:38 +000039 },
40 prebuiltModuleType: "cc_prebuilt_library_static",
41 linkTypes: []string{"static"},
42}
43
Paul Duffin255f18e2019-12-13 11:22:16 +000044func init() {
45 // Register sdk member types.
Paul Duffina0843f62019-12-13 19:50:38 +000046 android.RegisterSdkMemberType(sharedLibrarySdkMemberType)
47 android.RegisterSdkMemberType(staticLibrarySdkMemberType)
Paul Duffin2f6bc092019-12-13 10:40:56 +000048}
49
50type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +000051 android.SdkMemberTypeBase
52
Paul Duffin2f6bc092019-12-13 10:40:56 +000053 prebuiltModuleType string
54
55 // The set of link types supported, set of "static", "shared".
56 linkTypes []string
57}
58
59func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
60 targets := mctx.MultiTargets()
61 for _, lib := range names {
62 for _, target := range targets {
63 name, version := StubsLibNameAndVersion(lib)
64 if version == "" {
65 version = LatestStubsVersionFor(mctx.Config(), name)
66 }
Paul Duffin91756d22020-02-21 16:29:57 +000067 if mt.linkTypes == nil {
Paul Duffin2f6bc092019-12-13 10:40:56 +000068 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
69 {Mutator: "image", Variation: android.CoreVariation},
Paul Duffin2f6bc092019-12-13 10:40:56 +000070 {Mutator: "version", Variation: version},
71 }...), dependencyTag, name)
Paul Duffin91756d22020-02-21 16:29:57 +000072 } else {
73 for _, linkType := range mt.linkTypes {
74 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
75 {Mutator: "image", Variation: android.CoreVariation},
76 {Mutator: "link", Variation: linkType},
77 {Mutator: "version", Variation: version},
78 }...), dependencyTag, name)
79 }
Paul Duffin2f6bc092019-12-13 10:40:56 +000080 }
81 }
82 }
83}
84
85func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
Paul Duffina0843f62019-12-13 19:50:38 +000086 // Check the module to see if it can be used with this module type.
87 if m, ok := module.(*Module); ok {
88 for _, allowableMemberType := range m.sdkMemberTypes {
89 if allowableMemberType == mt {
90 return true
91 }
92 }
93 }
94
95 return false
Paul Duffin2f6bc092019-12-13 10:40:56 +000096}
97
Paul Duffin88f2fbe2020-02-27 16:00:53 +000098func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
99 pbm := builder.AddPrebuiltModule(member, mt.prebuiltModuleType)
Paul Duffin0c394f32020-03-05 14:09:58 +0000100
101 ccModule := member.Variants()[0].(*Module)
102
103 sdkVersion := ccModule.SdkVersion()
104 if sdkVersion != "" {
105 pbm.AddProperty("sdk_version", sdkVersion)
106 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000107 return pbm
Paul Duffin2f6bc092019-12-13 10:40:56 +0000108}
109
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000110func (mt *librarySdkMemberType) FinalizeModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember, bpModule android.BpModule) {
111 bpModule.AddProperty("stl", "none")
112 bpModule.AddProperty("system_shared_libs", []string{})
113}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000114
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000115func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
116 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000117}
118
119func isGeneratedHeaderDirectory(p android.Path) bool {
120 _, gen := p.(android.WritablePath)
121 return gen
122}
123
Paul Duffin64f54b02020-02-20 14:33:54 +0000124type includeDirsProperty struct {
125 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000126 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000127
128 // The name of the property in the prebuilt library, "" means there is no property.
129 propertyName string
130
131 // The directory within the snapshot directory into which items should be copied.
132 snapshotDir string
133
134 // True if the items on the path should be copied.
135 copy bool
136
137 // True if the paths represent directories, files if they represent files.
138 dirs bool
Paul Duffin74fc1902020-01-23 11:45:03 +0000139}
140
Paul Duffin64f54b02020-02-20 14:33:54 +0000141var includeDirProperties = []includeDirsProperty{
142 {
143 // ExportedIncludeDirs lists directories that contains some header files to be
144 // copied into a directory in the snapshot. The snapshot directories must be added to
145 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000146 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000147 propertyName: "export_include_dirs",
148 snapshotDir: nativeIncludeDir,
149 copy: true,
150 dirs: true,
151 },
152 {
153 // ExportedSystemIncludeDirs lists directories that contains some system header files to
154 // be copied into a directory in the snapshot. The snapshot directories must be added to
155 // the export_system_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000156 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000157 propertyName: "export_system_include_dirs",
158 snapshotDir: nativeIncludeDir,
159 copy: true,
160 dirs: true,
161 },
162 {
163 // exportedGeneratedIncludeDirs lists directories that contains some header files
164 // that are explicitly listed in the exportedGeneratedHeaders property. So, the contents
165 // of these directories do not need to be copied, but these directories do need adding to
166 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000167 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000168 propertyName: "export_include_dirs",
169 snapshotDir: nativeGeneratedIncludeDir,
170 copy: false,
171 dirs: true,
172 },
173 {
174 // exportedGeneratedHeaders lists header files that are in one of the directories
175 // specified in exportedGeneratedIncludeDirs must be copied into the snapshot.
176 // As they are in a directory in exportedGeneratedIncludeDirs they do not need adding to a
177 // property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000178 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000179 propertyName: "",
180 snapshotDir: nativeGeneratedIncludeDir,
181 copy: true,
182 dirs: false,
183 },
184}
185
186// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000187func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
188
189 // Copy the generated library to the snapshot and add a reference to it in the .bp module.
190 if libInfo.outputFile != nil {
191 nativeLibraryPath := nativeLibraryPathFor(libInfo)
192 builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
193 outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
194 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000195
196 // Map from property name to the include dirs to add to the prebuilt module in the snapshot.
197 includeDirs := make(map[string][]string)
198
199 // Iterate over each include directory property, copying files and collating property
200 // values where necessary.
201 for _, propertyInfo := range includeDirProperties {
202 // Calculate the base directory in the snapshot into which the files will be copied.
203 // lib.ArchType is "" for common properties.
204 targetDir := filepath.Join(libInfo.archType, propertyInfo.snapshotDir)
205
206 propertyName := propertyInfo.propertyName
207
208 // Iterate over each path in one of the include directory properties.
209 for _, path := range propertyInfo.pathsGetter(libInfo) {
210
211 // Copy the files/directories when necessary.
212 if propertyInfo.copy {
213 if propertyInfo.dirs {
214 // When copying a directory glob and copy all the headers within it.
215 // TODO(jiyong) copy headers having other suffixes
216 headers, _ := sdkModuleContext.GlobWithDeps(path.String()+"/**/*.h", nil)
217 for _, file := range headers {
218 src := android.PathForSource(sdkModuleContext, file)
219 dest := filepath.Join(targetDir, file)
220 builder.CopyToSnapshot(src, dest)
221 }
222 } else {
223 // Otherwise, just copy the files.
224 dest := filepath.Join(targetDir, libInfo.name, path.Rel())
225 builder.CopyToSnapshot(path, dest)
226 }
227 }
228
229 // Only directories are added to a property.
230 if propertyInfo.dirs {
231 var snapshotPath string
232 if isGeneratedHeaderDirectory(path) {
233 snapshotPath = filepath.Join(targetDir, libInfo.name)
234 } else {
235 snapshotPath = filepath.Join(targetDir, path.String())
236 }
237
238 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotPath)
239 }
240 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000241 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000242
243 // Add the collated include dir properties to the output.
244 for property, dirs := range includeDirs {
245 outputProperties.AddProperty(property, dirs)
Paul Duffin74fc1902020-01-23 11:45:03 +0000246 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000247}
248
Paul Duffin2f6bc092019-12-13 10:40:56 +0000249const (
250 nativeIncludeDir = "include"
251 nativeGeneratedIncludeDir = "include_gen"
252 nativeStubDir = "lib"
253)
254
255// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000256func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffin2f6bc092019-12-13 10:40:56 +0000257 return filepath.Join(lib.archType,
258 nativeStubDir, lib.outputFile.Base())
259}
260
Paul Duffin2f6bc092019-12-13 10:40:56 +0000261// nativeLibInfoProperties represents properties of a native lib
262//
263// The exported (capitalized) fields will be examined and may be changed during common value extraction.
264// The unexported fields will be left untouched.
265type nativeLibInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000266 android.SdkMemberPropertiesBase
267
268 memberType *librarySdkMemberType
269
Paul Duffin2f6bc092019-12-13 10:40:56 +0000270 // The name of the library, is not exported as this must not be changed during optimization.
271 name string
272
273 // archType is not exported as if set (to a non default value) it is always arch specific.
274 // This is "" for common properties.
275 archType string
276
Paul Duffin5efd1982020-02-20 14:33:54 +0000277 // The list of possibly common exported include dirs.
278 //
279 // This field is exported as its contents may not be arch specific.
280 ExportedIncludeDirs android.Paths
Paul Duffin2f6bc092019-12-13 10:40:56 +0000281
Paul Duffin5efd1982020-02-20 14:33:54 +0000282 // The list of arch specific exported generated include dirs.
283 //
284 // This field is not exported as its contents are always arch specific.
285 exportedGeneratedIncludeDirs android.Paths
286
287 // The list of arch specific exported generated header files.
288 //
289 // This field is not exported as its contents are is always arch specific.
Paul Duffin2f6bc092019-12-13 10:40:56 +0000290 exportedGeneratedHeaders android.Paths
291
Paul Duffin5efd1982020-02-20 14:33:54 +0000292 // The list of possibly common exported system include dirs.
293 //
294 // This field is exported as its contents may not be arch specific.
295 ExportedSystemIncludeDirs android.Paths
296
297 // The list of possibly common exported flags.
298 //
299 // This field is exported as its contents may not be arch specific.
300 ExportedFlags []string
301
Paul Duffin2f6bc092019-12-13 10:40:56 +0000302 // outputFile is not exported as it is always arch specific.
303 outputFile android.Path
304}
305
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000306func (p *nativeLibInfoProperties) PopulateFromVariant(variant android.SdkAware) {
307 ccModule := variant.(*Module)
308
309 // If the library has some link types then it produces an output binary file, otherwise it
310 // is header only.
311 if p.memberType.linkTypes != nil {
312 p.outputFile = ccModule.OutputFile().Path()
313 }
314
315 // Separate out the generated include dirs (which are arch specific) from the
316 // include dirs (which may not be).
317 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
318 ccModule.ExportedIncludeDirs(), isGeneratedHeaderDirectory)
319
320 p.name = variant.Name()
321 p.archType = ccModule.Target().Arch.ArchType.String()
322 p.ExportedIncludeDirs = exportedIncludeDirs
323 p.exportedGeneratedIncludeDirs = exportedGeneratedIncludeDirs
324 p.ExportedSystemIncludeDirs = ccModule.ExportedSystemIncludeDirs()
325 p.ExportedFlags = ccModule.ExportedFlags()
326 p.exportedGeneratedHeaders = ccModule.ExportedGeneratedHeaders()
327}
328
329func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
330 addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000331}