blob: 0a11af126497e0bee11e13f8e957cb1d873f0055 [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"
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000021
Paul Duffin2f6bc092019-12-13 10:40:56 +000022 "github.com/google/blueprint"
Paul Duffin13f02712020-03-06 12:30:43 +000023 "github.com/google/blueprint/proptools"
Paul Duffin2f6bc092019-12-13 10:40:56 +000024)
25
26// This file contains support for using cc library modules within an sdk.
27
Paul Duffina0843f62019-12-13 19:50:38 +000028var sharedLibrarySdkMemberType = &librarySdkMemberType{
29 SdkMemberTypeBase: android.SdkMemberTypeBase{
30 PropertyName: "native_shared_libs",
Paul Duffine6029182019-12-16 17:43:48 +000031 SupportsSdk: true,
Paul Duffina0843f62019-12-13 19:50:38 +000032 },
33 prebuiltModuleType: "cc_prebuilt_library_shared",
34 linkTypes: []string{"shared"},
35}
36
37var staticLibrarySdkMemberType = &librarySdkMemberType{
38 SdkMemberTypeBase: android.SdkMemberTypeBase{
39 PropertyName: "native_static_libs",
Paul Duffine6029182019-12-16 17:43:48 +000040 SupportsSdk: true,
Paul Duffina0843f62019-12-13 19:50:38 +000041 },
42 prebuiltModuleType: "cc_prebuilt_library_static",
43 linkTypes: []string{"static"},
44}
45
Paul Duffin9b76c0b2020-03-12 10:24:35 +000046var staticAndSharedLibrarySdkMemberType = &librarySdkMemberType{
47 SdkMemberTypeBase: android.SdkMemberTypeBase{
48 PropertyName: "native_libs",
49 SupportsSdk: true,
50 },
51 prebuiltModuleType: "cc_prebuilt_library",
52 linkTypes: []string{"static", "shared"},
53}
54
Paul Duffin255f18e2019-12-13 11:22:16 +000055func init() {
56 // Register sdk member types.
Paul Duffina0843f62019-12-13 19:50:38 +000057 android.RegisterSdkMemberType(sharedLibrarySdkMemberType)
58 android.RegisterSdkMemberType(staticLibrarySdkMemberType)
Paul Duffin9b76c0b2020-03-12 10:24:35 +000059 android.RegisterSdkMemberType(staticAndSharedLibrarySdkMemberType)
Paul Duffin2f6bc092019-12-13 10:40:56 +000060}
61
62type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +000063 android.SdkMemberTypeBase
64
Paul Duffin2f6bc092019-12-13 10:40:56 +000065 prebuiltModuleType string
66
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000067 noOutputFiles bool // True if there are no srcs files.
68
69 // The set of link types supported. A set of "static", "shared", or nil to
70 // skip link type variations.
Paul Duffin2f6bc092019-12-13 10:40:56 +000071 linkTypes []string
72}
73
74func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
75 targets := mctx.MultiTargets()
76 for _, lib := range names {
77 for _, target := range targets {
78 name, version := StubsLibNameAndVersion(lib)
79 if version == "" {
80 version = LatestStubsVersionFor(mctx.Config(), name)
81 }
Paul Duffin91756d22020-02-21 16:29:57 +000082 if mt.linkTypes == nil {
Paul Duffin2f6bc092019-12-13 10:40:56 +000083 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
84 {Mutator: "image", Variation: android.CoreVariation},
Paul Duffin2f6bc092019-12-13 10:40:56 +000085 {Mutator: "version", Variation: version},
86 }...), dependencyTag, name)
Paul Duffin91756d22020-02-21 16:29:57 +000087 } else {
88 for _, linkType := range mt.linkTypes {
89 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
90 {Mutator: "image", Variation: android.CoreVariation},
91 {Mutator: "link", Variation: linkType},
92 {Mutator: "version", Variation: version},
93 }...), dependencyTag, name)
94 }
Paul Duffin2f6bc092019-12-13 10:40:56 +000095 }
96 }
97 }
98}
99
100func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
Paul Duffina0843f62019-12-13 19:50:38 +0000101 // Check the module to see if it can be used with this module type.
102 if m, ok := module.(*Module); ok {
103 for _, allowableMemberType := range m.sdkMemberTypes {
104 if allowableMemberType == mt {
105 return true
106 }
107 }
108 }
109
110 return false
Paul Duffin2f6bc092019-12-13 10:40:56 +0000111}
112
Paul Duffin3a4eb502020-03-19 16:11:18 +0000113func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
114 pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, mt.prebuiltModuleType)
Paul Duffin0c394f32020-03-05 14:09:58 +0000115
116 ccModule := member.Variants()[0].(*Module)
117
118 sdkVersion := ccModule.SdkVersion()
119 if sdkVersion != "" {
120 pbm.AddProperty("sdk_version", sdkVersion)
121 }
Paul Duffin2f6bc092019-12-13 10:40:56 +0000122
Paul Duffin13f02712020-03-06 12:30:43 +0000123 stl := ccModule.stl.Properties.Stl
124 if stl != nil {
Paul Duffin0174d8d2020-03-11 18:42:08 +0000125 pbm.AddProperty("stl", proptools.String(stl))
Paul Duffin13f02712020-03-06 12:30:43 +0000126 }
Paul Duffin0174d8d2020-03-11 18:42:08 +0000127 return pbm
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000128}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000129
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000130func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
131 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000132}
133
134func isGeneratedHeaderDirectory(p android.Path) bool {
135 _, gen := p.(android.WritablePath)
136 return gen
137}
138
Paul Duffin64f54b02020-02-20 14:33:54 +0000139type includeDirsProperty struct {
140 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000141 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000142
143 // The name of the property in the prebuilt library, "" means there is no property.
144 propertyName string
145
146 // The directory within the snapshot directory into which items should be copied.
147 snapshotDir string
148
149 // True if the items on the path should be copied.
150 copy bool
151
152 // True if the paths represent directories, files if they represent files.
153 dirs bool
Paul Duffin74fc1902020-01-23 11:45:03 +0000154}
155
Paul Duffin64f54b02020-02-20 14:33:54 +0000156var includeDirProperties = []includeDirsProperty{
157 {
158 // ExportedIncludeDirs lists directories that contains some header files to be
159 // copied into a directory in the snapshot. The snapshot directories must be added to
160 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000161 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000162 propertyName: "export_include_dirs",
163 snapshotDir: nativeIncludeDir,
164 copy: true,
165 dirs: true,
166 },
167 {
168 // ExportedSystemIncludeDirs lists directories that contains some system header files to
169 // be copied into a directory in the snapshot. The snapshot directories must be added to
170 // the export_system_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000171 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000172 propertyName: "export_system_include_dirs",
173 snapshotDir: nativeIncludeDir,
174 copy: true,
175 dirs: true,
176 },
177 {
178 // exportedGeneratedIncludeDirs lists directories that contains some header files
179 // that are explicitly listed in the exportedGeneratedHeaders property. So, the contents
180 // of these directories do not need to be copied, but these directories do need adding to
181 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000182 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000183 propertyName: "export_include_dirs",
184 snapshotDir: nativeGeneratedIncludeDir,
185 copy: false,
186 dirs: true,
187 },
188 {
189 // exportedGeneratedHeaders lists header files that are in one of the directories
190 // specified in exportedGeneratedIncludeDirs must be copied into the snapshot.
191 // As they are in a directory in exportedGeneratedIncludeDirs they do not need adding to a
192 // property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000193 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000194 propertyName: "",
195 snapshotDir: nativeGeneratedIncludeDir,
196 copy: true,
197 dirs: false,
198 },
199}
200
201// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000202func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
203
204 // Copy the generated library to the snapshot and add a reference to it in the .bp module.
205 if libInfo.outputFile != nil {
206 nativeLibraryPath := nativeLibraryPathFor(libInfo)
207 builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
208 outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
209 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000210
Paul Duffin13f02712020-03-06 12:30:43 +0000211 if len(libInfo.SharedLibs) > 0 {
212 outputProperties.AddPropertyWithTag("shared_libs", libInfo.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
213 }
214
Martin Stjernholm10566a02020-03-24 01:19:52 +0000215 // SystemSharedLibs needs to be propagated if it's a list, even if it's empty,
216 // so check for non-nil instead of nonzero length.
217 if libInfo.SystemSharedLibs != nil {
Paul Duffin13f02712020-03-06 12:30:43 +0000218 outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
219 }
220
Paul Duffin64f54b02020-02-20 14:33:54 +0000221 // Map from property name to the include dirs to add to the prebuilt module in the snapshot.
222 includeDirs := make(map[string][]string)
223
224 // Iterate over each include directory property, copying files and collating property
225 // values where necessary.
226 for _, propertyInfo := range includeDirProperties {
227 // Calculate the base directory in the snapshot into which the files will be copied.
228 // lib.ArchType is "" for common properties.
229 targetDir := filepath.Join(libInfo.archType, propertyInfo.snapshotDir)
230
231 propertyName := propertyInfo.propertyName
232
233 // Iterate over each path in one of the include directory properties.
234 for _, path := range propertyInfo.pathsGetter(libInfo) {
235
236 // Copy the files/directories when necessary.
237 if propertyInfo.copy {
238 if propertyInfo.dirs {
239 // When copying a directory glob and copy all the headers within it.
240 // TODO(jiyong) copy headers having other suffixes
241 headers, _ := sdkModuleContext.GlobWithDeps(path.String()+"/**/*.h", nil)
242 for _, file := range headers {
243 src := android.PathForSource(sdkModuleContext, file)
244 dest := filepath.Join(targetDir, file)
245 builder.CopyToSnapshot(src, dest)
246 }
247 } else {
248 // Otherwise, just copy the files.
249 dest := filepath.Join(targetDir, libInfo.name, path.Rel())
250 builder.CopyToSnapshot(path, dest)
251 }
252 }
253
254 // Only directories are added to a property.
255 if propertyInfo.dirs {
256 var snapshotPath string
257 if isGeneratedHeaderDirectory(path) {
258 snapshotPath = filepath.Join(targetDir, libInfo.name)
259 } else {
260 snapshotPath = filepath.Join(targetDir, path.String())
261 }
262
263 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotPath)
264 }
265 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000266 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000267
268 // Add the collated include dir properties to the output.
269 for property, dirs := range includeDirs {
270 outputProperties.AddProperty(property, dirs)
Paul Duffin74fc1902020-01-23 11:45:03 +0000271 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000272}
273
Paul Duffin2f6bc092019-12-13 10:40:56 +0000274const (
275 nativeIncludeDir = "include"
276 nativeGeneratedIncludeDir = "include_gen"
277 nativeStubDir = "lib"
278)
279
280// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000281func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffina04c1072020-03-02 10:16:35 +0000282 return filepath.Join(lib.OsPrefix(), lib.archType,
Paul Duffin2f6bc092019-12-13 10:40:56 +0000283 nativeStubDir, lib.outputFile.Base())
284}
285
Paul Duffin2f6bc092019-12-13 10:40:56 +0000286// nativeLibInfoProperties represents properties of a native lib
287//
288// The exported (capitalized) fields will be examined and may be changed during common value extraction.
289// The unexported fields will be left untouched.
290type nativeLibInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000291 android.SdkMemberPropertiesBase
292
293 memberType *librarySdkMemberType
294
Paul Duffin2f6bc092019-12-13 10:40:56 +0000295 // The name of the library, is not exported as this must not be changed during optimization.
296 name string
297
298 // archType is not exported as if set (to a non default value) it is always arch specific.
299 // This is "" for common properties.
300 archType string
301
Paul Duffin5efd1982020-02-20 14:33:54 +0000302 // The list of possibly common exported include dirs.
303 //
304 // This field is exported as its contents may not be arch specific.
305 ExportedIncludeDirs android.Paths
Paul Duffin2f6bc092019-12-13 10:40:56 +0000306
Paul Duffin5efd1982020-02-20 14:33:54 +0000307 // The list of arch specific exported generated include dirs.
308 //
309 // This field is not exported as its contents are always arch specific.
310 exportedGeneratedIncludeDirs android.Paths
311
312 // The list of arch specific exported generated header files.
313 //
314 // This field is not exported as its contents are is always arch specific.
Paul Duffin2f6bc092019-12-13 10:40:56 +0000315 exportedGeneratedHeaders android.Paths
316
Paul Duffin5efd1982020-02-20 14:33:54 +0000317 // The list of possibly common exported system include dirs.
318 //
319 // This field is exported as its contents may not be arch specific.
320 ExportedSystemIncludeDirs android.Paths
321
322 // The list of possibly common exported flags.
323 //
324 // This field is exported as its contents may not be arch specific.
325 ExportedFlags []string
326
Paul Duffin13f02712020-03-06 12:30:43 +0000327 // The set of shared libraries
328 //
329 // This field is exported as its contents may not be arch specific.
330 SharedLibs []string
331
Martin Stjernholm10566a02020-03-24 01:19:52 +0000332 // The set of system shared libraries. Note nil and [] are semantically
333 // distinct - see BaseLinkerProperties.System_shared_libs.
Paul Duffin13f02712020-03-06 12:30:43 +0000334 //
335 // This field is exported as its contents may not be arch specific.
336 SystemSharedLibs []string
337
Paul Duffin2f6bc092019-12-13 10:40:56 +0000338 // outputFile is not exported as it is always arch specific.
339 outputFile android.Path
340}
341
Paul Duffin3a4eb502020-03-19 16:11:18 +0000342func (p *nativeLibInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000343 ccModule := variant.(*Module)
344
345 // If the library has some link types then it produces an output binary file, otherwise it
346 // is header only.
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000347 if !p.memberType.noOutputFiles {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000348 p.outputFile = ccModule.OutputFile().Path()
349 }
350
351 // Separate out the generated include dirs (which are arch specific) from the
352 // include dirs (which may not be).
353 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
354 ccModule.ExportedIncludeDirs(), isGeneratedHeaderDirectory)
355
356 p.name = variant.Name()
357 p.archType = ccModule.Target().Arch.ArchType.String()
Paul Duffin9b76c0b2020-03-12 10:24:35 +0000358
359 // Make sure that the include directories are unique.
360 p.ExportedIncludeDirs = android.FirstUniquePaths(exportedIncludeDirs)
361 p.exportedGeneratedIncludeDirs = android.FirstUniquePaths(exportedGeneratedIncludeDirs)
362 p.ExportedSystemIncludeDirs = android.FirstUniquePaths(ccModule.ExportedSystemIncludeDirs())
363
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000364 p.ExportedFlags = ccModule.ExportedFlags()
Paul Duffin13f02712020-03-06 12:30:43 +0000365 if ccModule.linker != nil {
366 specifiedDeps := specifiedDeps{}
367 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
368
369 p.SharedLibs = specifiedDeps.sharedLibs
370 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
371 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000372 p.exportedGeneratedHeaders = ccModule.ExportedGeneratedHeaders()
373}
374
Paul Duffin3a4eb502020-03-19 16:11:18 +0000375func (p *nativeLibInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
376 addPossiblyArchSpecificProperties(ctx.SdkModuleContext(), ctx.SnapshotBuilder(), p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000377}