Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "reflect" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | "github.com/google/blueprint" |
| 23 | ) |
| 24 | |
| 25 | // This file contains support for using cc library modules within an sdk. |
| 26 | |
Paul Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame^] | 27 | var sharedLibrarySdkMemberType = &librarySdkMemberType{ |
| 28 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 29 | PropertyName: "native_shared_libs", |
| 30 | }, |
| 31 | prebuiltModuleType: "cc_prebuilt_library_shared", |
| 32 | linkTypes: []string{"shared"}, |
| 33 | } |
| 34 | |
| 35 | var staticLibrarySdkMemberType = &librarySdkMemberType{ |
| 36 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 37 | PropertyName: "native_static_libs", |
| 38 | }, |
| 39 | prebuiltModuleType: "cc_prebuilt_library_static", |
| 40 | linkTypes: []string{"static"}, |
| 41 | } |
| 42 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 43 | func init() { |
| 44 | // Register sdk member types. |
Paul Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame^] | 45 | android.RegisterSdkMemberType(sharedLibrarySdkMemberType) |
| 46 | android.RegisterSdkMemberType(staticLibrarySdkMemberType) |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | type librarySdkMemberType struct { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 50 | android.SdkMemberTypeBase |
| 51 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 52 | prebuiltModuleType string |
| 53 | |
| 54 | // The set of link types supported, set of "static", "shared". |
| 55 | linkTypes []string |
| 56 | } |
| 57 | |
| 58 | func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) { |
| 59 | targets := mctx.MultiTargets() |
| 60 | for _, lib := range names { |
| 61 | for _, target := range targets { |
| 62 | name, version := StubsLibNameAndVersion(lib) |
| 63 | if version == "" { |
| 64 | version = LatestStubsVersionFor(mctx.Config(), name) |
| 65 | } |
| 66 | for _, linkType := range mt.linkTypes { |
| 67 | mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{ |
| 68 | {Mutator: "image", Variation: android.CoreVariation}, |
| 69 | {Mutator: "link", Variation: linkType}, |
| 70 | {Mutator: "version", Variation: version}, |
| 71 | }...), dependencyTag, name) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func (mt *librarySdkMemberType) IsInstance(module android.Module) bool { |
Paul Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame^] | 78 | // Check the module to see if it can be used with this module type. |
| 79 | if m, ok := module.(*Module); ok { |
| 80 | for _, allowableMemberType := range m.sdkMemberTypes { |
| 81 | if allowableMemberType == mt { |
| 82 | return true |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return false |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // copy exported header files and stub *.so files |
| 91 | func (mt *librarySdkMemberType) BuildSnapshot(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) { |
| 92 | info := mt.organizeVariants(member) |
| 93 | buildSharedNativeLibSnapshot(sdkModuleContext, info, builder, member) |
| 94 | } |
| 95 | |
| 96 | // Organize the variants by architecture. |
| 97 | func (mt *librarySdkMemberType) organizeVariants(member android.SdkMember) *nativeLibInfo { |
| 98 | memberName := member.Name() |
| 99 | info := &nativeLibInfo{ |
| 100 | name: memberName, |
| 101 | memberType: mt, |
| 102 | } |
| 103 | |
| 104 | for _, variant := range member.Variants() { |
| 105 | ccModule := variant.(*Module) |
| 106 | |
| 107 | // Separate out the generated include dirs (which are arch specific) from the |
| 108 | // include dirs (which may not be). |
| 109 | exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate( |
| 110 | ccModule.ExportedIncludeDirs(), isGeneratedHeaderDirectory) |
| 111 | |
| 112 | info.archVariantProperties = append(info.archVariantProperties, nativeLibInfoProperties{ |
| 113 | name: memberName, |
| 114 | archType: ccModule.Target().Arch.ArchType.String(), |
| 115 | ExportedIncludeDirs: exportedIncludeDirs, |
| 116 | ExportedGeneratedIncludeDirs: exportedGeneratedIncludeDirs, |
| 117 | ExportedSystemIncludeDirs: ccModule.ExportedSystemIncludeDirs(), |
| 118 | ExportedFlags: ccModule.ExportedFlags(), |
| 119 | exportedGeneratedHeaders: ccModule.ExportedGeneratedHeaders(), |
| 120 | outputFile: ccModule.OutputFile().Path(), |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | // Initialize the unexported properties that will not be set during the |
| 125 | // extraction process. |
| 126 | info.commonProperties.name = memberName |
| 127 | |
| 128 | // Extract common properties from the arch specific properties. |
| 129 | extractCommonProperties(&info.commonProperties, info.archVariantProperties) |
| 130 | |
| 131 | return info |
| 132 | } |
| 133 | |
| 134 | func isGeneratedHeaderDirectory(p android.Path) bool { |
| 135 | _, gen := p.(android.WritablePath) |
| 136 | return gen |
| 137 | } |
| 138 | |
| 139 | // Extract common properties from a slice of property structures of the same type. |
| 140 | // |
| 141 | // All the property structures must be of the same type. |
| 142 | // commonProperties - must be a pointer to the structure into which common properties will be added. |
| 143 | // inputPropertiesSlice - must be a slice of input properties structures. |
| 144 | // |
| 145 | // Iterates over each exported field (capitalized name) and checks to see whether they |
| 146 | // have the same value (using DeepEquals) across all the input properties. If it does not then no |
| 147 | // change is made. Otherwise, the common value is stored in the field in the commonProperties |
| 148 | // and the field in each of the input properties structure is set to its default value. |
| 149 | func extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) { |
| 150 | commonStructValue := reflect.ValueOf(commonProperties).Elem() |
| 151 | propertiesStructType := commonStructValue.Type() |
| 152 | |
| 153 | // Create an empty structure from which default values for the field can be copied. |
| 154 | emptyStructValue := reflect.New(propertiesStructType).Elem() |
| 155 | |
| 156 | for f := 0; f < propertiesStructType.NumField(); f++ { |
| 157 | // Check to see if all the structures have the same value for the field. The commonValue |
| 158 | // is nil on entry to the loop and if it is nil on exit then there is no common value, |
| 159 | // otherwise it points to the common value. |
| 160 | var commonValue *reflect.Value |
| 161 | sliceValue := reflect.ValueOf(inputPropertiesSlice) |
| 162 | |
| 163 | for i := 0; i < sliceValue.Len(); i++ { |
| 164 | structValue := sliceValue.Index(i) |
| 165 | fieldValue := structValue.Field(f) |
| 166 | if !fieldValue.CanInterface() { |
| 167 | // The field is not exported so ignore it. |
| 168 | continue |
| 169 | } |
| 170 | |
| 171 | if commonValue == nil { |
| 172 | // Use the first value as the commonProperties value. |
| 173 | commonValue = &fieldValue |
| 174 | } else { |
| 175 | // If the value does not match the current common value then there is |
| 176 | // no value in common so break out. |
| 177 | if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) { |
| 178 | commonValue = nil |
| 179 | break |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // If the fields all have a common value then store it in the common struct field |
| 185 | // and set the input struct's field to the empty value. |
| 186 | if commonValue != nil { |
| 187 | emptyValue := emptyStructValue.Field(f) |
| 188 | commonStructValue.Field(f).Set(*commonValue) |
| 189 | for i := 0; i < sliceValue.Len(); i++ { |
| 190 | structValue := sliceValue.Index(i) |
| 191 | fieldValue := structValue.Field(f) |
| 192 | fieldValue.Set(emptyValue) |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func buildSharedNativeLibSnapshot(sdkModuleContext android.ModuleContext, info *nativeLibInfo, builder android.SnapshotBuilder, member android.SdkMember) { |
| 199 | // a function for emitting include dirs |
| 200 | addExportedDirCopyCommandsForNativeLibs := func(lib nativeLibInfoProperties) { |
| 201 | // Do not include ExportedGeneratedIncludeDirs in the list of directories whose |
| 202 | // contents are copied as they are copied from exportedGeneratedHeaders below. |
| 203 | includeDirs := lib.ExportedIncludeDirs |
| 204 | includeDirs = append(includeDirs, lib.ExportedSystemIncludeDirs...) |
| 205 | for _, dir := range includeDirs { |
| 206 | // lib.ArchType is "" for common properties. |
| 207 | targetDir := filepath.Join(lib.archType, nativeIncludeDir) |
| 208 | |
| 209 | // TODO(jiyong) copy headers having other suffixes |
| 210 | headers, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.h", nil) |
| 211 | for _, file := range headers { |
| 212 | src := android.PathForSource(sdkModuleContext, file) |
| 213 | dest := filepath.Join(targetDir, file) |
| 214 | builder.CopyToSnapshot(src, dest) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | genHeaders := lib.exportedGeneratedHeaders |
| 219 | for _, file := range genHeaders { |
| 220 | // lib.ArchType is "" for common properties. |
| 221 | targetDir := filepath.Join(lib.archType, nativeGeneratedIncludeDir) |
| 222 | |
| 223 | dest := filepath.Join(targetDir, lib.name, file.Rel()) |
| 224 | builder.CopyToSnapshot(file, dest) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | addExportedDirCopyCommandsForNativeLibs(info.commonProperties) |
| 229 | |
| 230 | // for each architecture |
| 231 | for _, av := range info.archVariantProperties { |
| 232 | builder.CopyToSnapshot(av.outputFile, nativeLibraryPathFor(av)) |
| 233 | |
| 234 | addExportedDirCopyCommandsForNativeLibs(av) |
| 235 | } |
| 236 | |
| 237 | info.generatePrebuiltLibrary(sdkModuleContext, builder, member) |
| 238 | } |
| 239 | |
| 240 | func (info *nativeLibInfo) generatePrebuiltLibrary(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) { |
| 241 | |
| 242 | // a function for emitting include dirs |
| 243 | addExportedDirsForNativeLibs := func(lib nativeLibInfoProperties, properties android.BpPropertySet, systemInclude bool) { |
| 244 | includeDirs := nativeIncludeDirPathsFor(lib, systemInclude) |
| 245 | if len(includeDirs) == 0 { |
| 246 | return |
| 247 | } |
| 248 | var propertyName string |
| 249 | if !systemInclude { |
| 250 | propertyName = "export_include_dirs" |
| 251 | } else { |
| 252 | propertyName = "export_system_include_dirs" |
| 253 | } |
| 254 | properties.AddProperty(propertyName, includeDirs) |
| 255 | } |
| 256 | |
| 257 | pbm := builder.AddPrebuiltModule(member, info.memberType.prebuiltModuleType) |
| 258 | |
| 259 | addExportedDirsForNativeLibs(info.commonProperties, pbm, false /*systemInclude*/) |
| 260 | addExportedDirsForNativeLibs(info.commonProperties, pbm, true /*systemInclude*/) |
| 261 | |
| 262 | archProperties := pbm.AddPropertySet("arch") |
| 263 | for _, av := range info.archVariantProperties { |
| 264 | archTypeProperties := archProperties.AddPropertySet(av.archType) |
| 265 | archTypeProperties.AddProperty("srcs", []string{nativeLibraryPathFor(av)}) |
| 266 | |
| 267 | // export_* properties are added inside the arch: {<arch>: {...}} block |
| 268 | addExportedDirsForNativeLibs(av, archTypeProperties, false /*systemInclude*/) |
| 269 | addExportedDirsForNativeLibs(av, archTypeProperties, true /*systemInclude*/) |
| 270 | } |
| 271 | pbm.AddProperty("stl", "none") |
| 272 | pbm.AddProperty("system_shared_libs", []string{}) |
| 273 | } |
| 274 | |
| 275 | const ( |
| 276 | nativeIncludeDir = "include" |
| 277 | nativeGeneratedIncludeDir = "include_gen" |
| 278 | nativeStubDir = "lib" |
| 279 | ) |
| 280 | |
| 281 | // path to the native library. Relative to <sdk_root>/<api_dir> |
| 282 | func nativeLibraryPathFor(lib nativeLibInfoProperties) string { |
| 283 | return filepath.Join(lib.archType, |
| 284 | nativeStubDir, lib.outputFile.Base()) |
| 285 | } |
| 286 | |
| 287 | // paths to the include dirs of a native shared library. Relative to <sdk_root>/<api_dir> |
| 288 | func nativeIncludeDirPathsFor(lib nativeLibInfoProperties, systemInclude bool) []string { |
| 289 | var result []string |
| 290 | var includeDirs []android.Path |
| 291 | if !systemInclude { |
| 292 | // Include the generated include dirs in the exported include dirs. |
| 293 | includeDirs = append(lib.ExportedIncludeDirs, lib.ExportedGeneratedIncludeDirs...) |
| 294 | } else { |
| 295 | includeDirs = lib.ExportedSystemIncludeDirs |
| 296 | } |
| 297 | for _, dir := range includeDirs { |
| 298 | var path string |
| 299 | if isGeneratedHeaderDirectory(dir) { |
| 300 | path = filepath.Join(nativeGeneratedIncludeDir, lib.name) |
| 301 | } else { |
| 302 | path = filepath.Join(nativeIncludeDir, dir.String()) |
| 303 | } |
| 304 | |
| 305 | // lib.ArchType is "" for common properties. |
| 306 | path = filepath.Join(lib.archType, path) |
| 307 | result = append(result, path) |
| 308 | } |
| 309 | return result |
| 310 | } |
| 311 | |
| 312 | // nativeLibInfoProperties represents properties of a native lib |
| 313 | // |
| 314 | // The exported (capitalized) fields will be examined and may be changed during common value extraction. |
| 315 | // The unexported fields will be left untouched. |
| 316 | type nativeLibInfoProperties struct { |
| 317 | // The name of the library, is not exported as this must not be changed during optimization. |
| 318 | name string |
| 319 | |
| 320 | // archType is not exported as if set (to a non default value) it is always arch specific. |
| 321 | // This is "" for common properties. |
| 322 | archType string |
| 323 | |
| 324 | ExportedIncludeDirs android.Paths |
| 325 | ExportedGeneratedIncludeDirs android.Paths |
| 326 | ExportedSystemIncludeDirs android.Paths |
| 327 | ExportedFlags []string |
| 328 | |
| 329 | // exportedGeneratedHeaders is not exported as if set it is always arch specific. |
| 330 | exportedGeneratedHeaders android.Paths |
| 331 | |
| 332 | // outputFile is not exported as it is always arch specific. |
| 333 | outputFile android.Path |
| 334 | } |
| 335 | |
| 336 | // nativeLibInfo represents a collection of arch-specific modules having the same name |
| 337 | type nativeLibInfo struct { |
| 338 | name string |
| 339 | memberType *librarySdkMemberType |
| 340 | archVariantProperties []nativeLibInfoProperties |
| 341 | commonProperties nativeLibInfoProperties |
| 342 | } |