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" |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 19 | |
| 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 Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame] | 26 | var sharedLibrarySdkMemberType = &librarySdkMemberType{ |
| 27 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 28 | PropertyName: "native_shared_libs", |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 29 | SupportsSdk: true, |
Paul Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame] | 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", |
Paul Duffin | e602918 | 2019-12-16 17:43:48 +0000 | [diff] [blame] | 38 | SupportsSdk: true, |
Paul Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame] | 39 | }, |
| 40 | prebuiltModuleType: "cc_prebuilt_library_static", |
| 41 | linkTypes: []string{"static"}, |
| 42 | } |
| 43 | |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 44 | func init() { |
| 45 | // Register sdk member types. |
Paul Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame] | 46 | android.RegisterSdkMemberType(sharedLibrarySdkMemberType) |
| 47 | android.RegisterSdkMemberType(staticLibrarySdkMemberType) |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | type librarySdkMemberType struct { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 51 | android.SdkMemberTypeBase |
| 52 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 53 | prebuiltModuleType string |
| 54 | |
| 55 | // The set of link types supported, set of "static", "shared". |
| 56 | linkTypes []string |
| 57 | } |
| 58 | |
| 59 | func (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 Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 67 | if mt.linkTypes == nil { |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 68 | mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{ |
| 69 | {Mutator: "image", Variation: android.CoreVariation}, |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 70 | {Mutator: "version", Variation: version}, |
| 71 | }...), dependencyTag, name) |
Paul Duffin | 91756d2 | 2020-02-21 16:29:57 +0000 | [diff] [blame] | 72 | } 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func (mt *librarySdkMemberType) IsInstance(module android.Module) bool { |
Paul Duffin | a0843f6 | 2019-12-13 19:50:38 +0000 | [diff] [blame] | 86 | // 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 98 | func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule { |
| 99 | pbm := builder.AddPrebuiltModule(member, mt.prebuiltModuleType) |
Paul Duffin | 0c394f3 | 2020-03-05 14:09:58 +0000 | [diff] [blame^] | 100 | |
| 101 | ccModule := member.Variants()[0].(*Module) |
| 102 | |
| 103 | sdkVersion := ccModule.SdkVersion() |
| 104 | if sdkVersion != "" { |
| 105 | pbm.AddProperty("sdk_version", sdkVersion) |
| 106 | } |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 107 | return pbm |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 110 | func (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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 114 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 115 | func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
| 116 | return &nativeLibInfoProperties{memberType: mt} |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func isGeneratedHeaderDirectory(p android.Path) bool { |
| 120 | _, gen := p.(android.WritablePath) |
| 121 | return gen |
| 122 | } |
| 123 | |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 124 | type includeDirsProperty struct { |
| 125 | // Accessor to retrieve the paths |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 126 | pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 127 | |
| 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 Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 141 | var 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 146 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 147 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 156 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 157 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 167 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedIncludeDirs }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 168 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 178 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 179 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 187 | func 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 Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 195 | |
| 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 Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 241 | } |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 242 | |
| 243 | // Add the collated include dir properties to the output. |
| 244 | for property, dirs := range includeDirs { |
| 245 | outputProperties.AddProperty(property, dirs) |
Paul Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 246 | } |
Paul Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 249 | const ( |
| 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 256 | func nativeLibraryPathFor(lib *nativeLibInfoProperties) string { |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 257 | return filepath.Join(lib.archType, |
| 258 | nativeStubDir, lib.outputFile.Base()) |
| 259 | } |
| 260 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 261 | // 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. |
| 265 | type nativeLibInfoProperties struct { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 266 | android.SdkMemberPropertiesBase |
| 267 | |
| 268 | memberType *librarySdkMemberType |
| 269 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 270 | // 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 Duffin | 5efd198 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 277 | // 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 281 | |
Paul Duffin | 5efd198 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 282 | // 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 290 | exportedGeneratedHeaders android.Paths |
| 291 | |
Paul Duffin | 5efd198 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 292 | // 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 302 | // outputFile is not exported as it is always arch specific. |
| 303 | outputFile android.Path |
| 304 | } |
| 305 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 306 | func (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 | |
| 329 | func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) { |
| 330 | addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet) |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 331 | } |