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) |
| 100 | return pbm |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 103 | func (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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 107 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 108 | func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
| 109 | return &nativeLibInfoProperties{memberType: mt} |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func isGeneratedHeaderDirectory(p android.Path) bool { |
| 113 | _, gen := p.(android.WritablePath) |
| 114 | return gen |
| 115 | } |
| 116 | |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 117 | type includeDirsProperty struct { |
| 118 | // Accessor to retrieve the paths |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 119 | pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 120 | |
| 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 Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 134 | var 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 139 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 140 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 149 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 150 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 160 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedIncludeDirs }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 161 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 171 | pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders }, |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 172 | 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 180 | func 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 Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 188 | |
| 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 Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 234 | } |
Paul Duffin | 64f54b0 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 235 | |
| 236 | // Add the collated include dir properties to the output. |
| 237 | for property, dirs := range includeDirs { |
| 238 | outputProperties.AddProperty(property, dirs) |
Paul Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 239 | } |
Paul Duffin | 74fc190 | 2020-01-23 11:45:03 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 242 | const ( |
| 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 Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 249 | func nativeLibraryPathFor(lib *nativeLibInfoProperties) string { |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 250 | return filepath.Join(lib.archType, |
| 251 | nativeStubDir, lib.outputFile.Base()) |
| 252 | } |
| 253 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 254 | // 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. |
| 258 | type nativeLibInfoProperties struct { |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 259 | android.SdkMemberPropertiesBase |
| 260 | |
| 261 | memberType *librarySdkMemberType |
| 262 | |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 263 | // 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 Duffin | 5efd198 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 270 | // 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 274 | |
Paul Duffin | 5efd198 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 275 | // 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 283 | exportedGeneratedHeaders android.Paths |
| 284 | |
Paul Duffin | 5efd198 | 2020-02-20 14:33:54 +0000 | [diff] [blame] | 285 | // 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 Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 295 | // outputFile is not exported as it is always arch specific. |
| 296 | outputFile android.Path |
| 297 | } |
| 298 | |
Paul Duffin | 88f2fbe | 2020-02-27 16:00:53 +0000 | [diff] [blame] | 299 | func (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 | |
| 322 | func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) { |
| 323 | addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet) |
Paul Duffin | 2f6bc09 | 2019-12-13 10:40:56 +0000 | [diff] [blame] | 324 | } |