blob: 16b9e6f836955ee4732b9d780e8a253181cbc519 [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)
100 return pbm
Paul Duffin2f6bc092019-12-13 10:40:56 +0000101}
102
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000103func (mt *librarySdkMemberType) FinalizeModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember, bpModule android.BpModule) {
104 bpModule.AddProperty("stl", "none")
105 bpModule.AddProperty("system_shared_libs", []string{})
106}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000107
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000108func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
109 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000110}
111
112func isGeneratedHeaderDirectory(p android.Path) bool {
113 _, gen := p.(android.WritablePath)
114 return gen
115}
116
Paul Duffin64f54b02020-02-20 14:33:54 +0000117type includeDirsProperty struct {
118 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000119 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000120
121 // The name of the property in the prebuilt library, "" means there is no property.
122 propertyName string
123
124 // The directory within the snapshot directory into which items should be copied.
125 snapshotDir string
126
127 // True if the items on the path should be copied.
128 copy bool
129
130 // True if the paths represent directories, files if they represent files.
131 dirs bool
Paul Duffin74fc1902020-01-23 11:45:03 +0000132}
133
Paul Duffin64f54b02020-02-20 14:33:54 +0000134var includeDirProperties = []includeDirsProperty{
135 {
136 // ExportedIncludeDirs lists directories that contains some header files to be
137 // copied into a directory in the snapshot. The snapshot directories must be added to
138 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000139 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000140 propertyName: "export_include_dirs",
141 snapshotDir: nativeIncludeDir,
142 copy: true,
143 dirs: true,
144 },
145 {
146 // ExportedSystemIncludeDirs lists directories that contains some system header files to
147 // be copied into a directory in the snapshot. The snapshot directories must be added to
148 // the export_system_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000149 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000150 propertyName: "export_system_include_dirs",
151 snapshotDir: nativeIncludeDir,
152 copy: true,
153 dirs: true,
154 },
155 {
156 // exportedGeneratedIncludeDirs lists directories that contains some header files
157 // that are explicitly listed in the exportedGeneratedHeaders property. So, the contents
158 // of these directories do not need to be copied, but these directories do need adding to
159 // the export_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.exportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000161 propertyName: "export_include_dirs",
162 snapshotDir: nativeGeneratedIncludeDir,
163 copy: false,
164 dirs: true,
165 },
166 {
167 // exportedGeneratedHeaders lists header files that are in one of the directories
168 // specified in exportedGeneratedIncludeDirs must be copied into the snapshot.
169 // As they are in a directory in exportedGeneratedIncludeDirs they do not need adding to a
170 // property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000171 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000172 propertyName: "",
173 snapshotDir: nativeGeneratedIncludeDir,
174 copy: true,
175 dirs: false,
176 },
177}
178
179// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000180func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
181
182 // Copy the generated library to the snapshot and add a reference to it in the .bp module.
183 if libInfo.outputFile != nil {
184 nativeLibraryPath := nativeLibraryPathFor(libInfo)
185 builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
186 outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
187 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000188
189 // Map from property name to the include dirs to add to the prebuilt module in the snapshot.
190 includeDirs := make(map[string][]string)
191
192 // Iterate over each include directory property, copying files and collating property
193 // values where necessary.
194 for _, propertyInfo := range includeDirProperties {
195 // Calculate the base directory in the snapshot into which the files will be copied.
196 // lib.ArchType is "" for common properties.
197 targetDir := filepath.Join(libInfo.archType, propertyInfo.snapshotDir)
198
199 propertyName := propertyInfo.propertyName
200
201 // Iterate over each path in one of the include directory properties.
202 for _, path := range propertyInfo.pathsGetter(libInfo) {
203
204 // Copy the files/directories when necessary.
205 if propertyInfo.copy {
206 if propertyInfo.dirs {
207 // When copying a directory glob and copy all the headers within it.
208 // TODO(jiyong) copy headers having other suffixes
209 headers, _ := sdkModuleContext.GlobWithDeps(path.String()+"/**/*.h", nil)
210 for _, file := range headers {
211 src := android.PathForSource(sdkModuleContext, file)
212 dest := filepath.Join(targetDir, file)
213 builder.CopyToSnapshot(src, dest)
214 }
215 } else {
216 // Otherwise, just copy the files.
217 dest := filepath.Join(targetDir, libInfo.name, path.Rel())
218 builder.CopyToSnapshot(path, dest)
219 }
220 }
221
222 // Only directories are added to a property.
223 if propertyInfo.dirs {
224 var snapshotPath string
225 if isGeneratedHeaderDirectory(path) {
226 snapshotPath = filepath.Join(targetDir, libInfo.name)
227 } else {
228 snapshotPath = filepath.Join(targetDir, path.String())
229 }
230
231 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotPath)
232 }
233 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000234 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000235
236 // Add the collated include dir properties to the output.
237 for property, dirs := range includeDirs {
238 outputProperties.AddProperty(property, dirs)
Paul Duffin74fc1902020-01-23 11:45:03 +0000239 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000240}
241
Paul Duffin2f6bc092019-12-13 10:40:56 +0000242const (
243 nativeIncludeDir = "include"
244 nativeGeneratedIncludeDir = "include_gen"
245 nativeStubDir = "lib"
246)
247
248// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000249func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffin2f6bc092019-12-13 10:40:56 +0000250 return filepath.Join(lib.archType,
251 nativeStubDir, lib.outputFile.Base())
252}
253
Paul Duffin2f6bc092019-12-13 10:40:56 +0000254// nativeLibInfoProperties represents properties of a native lib
255//
256// The exported (capitalized) fields will be examined and may be changed during common value extraction.
257// The unexported fields will be left untouched.
258type nativeLibInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000259 android.SdkMemberPropertiesBase
260
261 memberType *librarySdkMemberType
262
Paul Duffin2f6bc092019-12-13 10:40:56 +0000263 // The name of the library, is not exported as this must not be changed during optimization.
264 name string
265
266 // archType is not exported as if set (to a non default value) it is always arch specific.
267 // This is "" for common properties.
268 archType string
269
Paul Duffin5efd1982020-02-20 14:33:54 +0000270 // The list of possibly common exported include dirs.
271 //
272 // This field is exported as its contents may not be arch specific.
273 ExportedIncludeDirs android.Paths
Paul Duffin2f6bc092019-12-13 10:40:56 +0000274
Paul Duffin5efd1982020-02-20 14:33:54 +0000275 // The list of arch specific exported generated include dirs.
276 //
277 // This field is not exported as its contents are always arch specific.
278 exportedGeneratedIncludeDirs android.Paths
279
280 // The list of arch specific exported generated header files.
281 //
282 // This field is not exported as its contents are is always arch specific.
Paul Duffin2f6bc092019-12-13 10:40:56 +0000283 exportedGeneratedHeaders android.Paths
284
Paul Duffin5efd1982020-02-20 14:33:54 +0000285 // The list of possibly common exported system include dirs.
286 //
287 // This field is exported as its contents may not be arch specific.
288 ExportedSystemIncludeDirs android.Paths
289
290 // The list of possibly common exported flags.
291 //
292 // This field is exported as its contents may not be arch specific.
293 ExportedFlags []string
294
Paul Duffin2f6bc092019-12-13 10:40:56 +0000295 // outputFile is not exported as it is always arch specific.
296 outputFile android.Path
297}
298
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000299func (p *nativeLibInfoProperties) PopulateFromVariant(variant android.SdkAware) {
300 ccModule := variant.(*Module)
301
302 // If the library has some link types then it produces an output binary file, otherwise it
303 // is header only.
304 if p.memberType.linkTypes != nil {
305 p.outputFile = ccModule.OutputFile().Path()
306 }
307
308 // Separate out the generated include dirs (which are arch specific) from the
309 // include dirs (which may not be).
310 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
311 ccModule.ExportedIncludeDirs(), isGeneratedHeaderDirectory)
312
313 p.name = variant.Name()
314 p.archType = ccModule.Target().Arch.ArchType.String()
315 p.ExportedIncludeDirs = exportedIncludeDirs
316 p.exportedGeneratedIncludeDirs = exportedGeneratedIncludeDirs
317 p.ExportedSystemIncludeDirs = ccModule.ExportedSystemIncludeDirs()
318 p.ExportedFlags = ccModule.ExportedFlags()
319 p.exportedGeneratedHeaders = ccModule.ExportedGeneratedHeaders()
320}
321
322func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
323 addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000324}