blob: 69c3d18d216c2b26daac80b9a508df567325b530 [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 Duffin255f18e2019-12-13 11:22:16 +000046func init() {
47 // Register sdk member types.
Paul Duffina0843f62019-12-13 19:50:38 +000048 android.RegisterSdkMemberType(sharedLibrarySdkMemberType)
49 android.RegisterSdkMemberType(staticLibrarySdkMemberType)
Paul Duffin2f6bc092019-12-13 10:40:56 +000050}
51
52type librarySdkMemberType struct {
Paul Duffin255f18e2019-12-13 11:22:16 +000053 android.SdkMemberTypeBase
54
Paul Duffin2f6bc092019-12-13 10:40:56 +000055 prebuiltModuleType string
56
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000057 noOutputFiles bool // True if there are no srcs files.
58
59 // The set of link types supported. A set of "static", "shared", or nil to
60 // skip link type variations.
Paul Duffin2f6bc092019-12-13 10:40:56 +000061 linkTypes []string
62}
63
64func (mt *librarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
65 targets := mctx.MultiTargets()
66 for _, lib := range names {
67 for _, target := range targets {
68 name, version := StubsLibNameAndVersion(lib)
69 if version == "" {
70 version = LatestStubsVersionFor(mctx.Config(), name)
71 }
Paul Duffin91756d22020-02-21 16:29:57 +000072 if mt.linkTypes == nil {
Paul Duffin2f6bc092019-12-13 10:40:56 +000073 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
74 {Mutator: "image", Variation: android.CoreVariation},
Paul Duffin2f6bc092019-12-13 10:40:56 +000075 {Mutator: "version", Variation: version},
76 }...), dependencyTag, name)
Paul Duffin91756d22020-02-21 16:29:57 +000077 } else {
78 for _, linkType := range mt.linkTypes {
79 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
80 {Mutator: "image", Variation: android.CoreVariation},
81 {Mutator: "link", Variation: linkType},
82 {Mutator: "version", Variation: version},
83 }...), dependencyTag, name)
84 }
Paul Duffin2f6bc092019-12-13 10:40:56 +000085 }
86 }
87 }
88}
89
90func (mt *librarySdkMemberType) IsInstance(module android.Module) bool {
Paul Duffina0843f62019-12-13 19:50:38 +000091 // Check the module to see if it can be used with this module type.
92 if m, ok := module.(*Module); ok {
93 for _, allowableMemberType := range m.sdkMemberTypes {
94 if allowableMemberType == mt {
95 return true
96 }
97 }
98 }
99
100 return false
Paul Duffin2f6bc092019-12-13 10:40:56 +0000101}
102
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000103func (mt *librarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
104 pbm := builder.AddPrebuiltModule(member, mt.prebuiltModuleType)
Paul Duffin0c394f32020-03-05 14:09:58 +0000105
106 ccModule := member.Variants()[0].(*Module)
107
108 sdkVersion := ccModule.SdkVersion()
109 if sdkVersion != "" {
110 pbm.AddProperty("sdk_version", sdkVersion)
111 }
Paul Duffin2f6bc092019-12-13 10:40:56 +0000112
Paul Duffin13f02712020-03-06 12:30:43 +0000113 stl := ccModule.stl.Properties.Stl
114 if stl != nil {
Paul Duffin0174d8d2020-03-11 18:42:08 +0000115 pbm.AddProperty("stl", proptools.String(stl))
Paul Duffin13f02712020-03-06 12:30:43 +0000116 }
Paul Duffin0174d8d2020-03-11 18:42:08 +0000117 return pbm
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000118}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000119
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000120func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
121 return &nativeLibInfoProperties{memberType: mt}
Paul Duffin2f6bc092019-12-13 10:40:56 +0000122}
123
124func isGeneratedHeaderDirectory(p android.Path) bool {
125 _, gen := p.(android.WritablePath)
126 return gen
127}
128
Paul Duffin64f54b02020-02-20 14:33:54 +0000129type includeDirsProperty struct {
130 // Accessor to retrieve the paths
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000131 pathsGetter func(libInfo *nativeLibInfoProperties) android.Paths
Paul Duffin64f54b02020-02-20 14:33:54 +0000132
133 // The name of the property in the prebuilt library, "" means there is no property.
134 propertyName string
135
136 // The directory within the snapshot directory into which items should be copied.
137 snapshotDir string
138
139 // True if the items on the path should be copied.
140 copy bool
141
142 // True if the paths represent directories, files if they represent files.
143 dirs bool
Paul Duffin74fc1902020-01-23 11:45:03 +0000144}
145
Paul Duffin64f54b02020-02-20 14:33:54 +0000146var includeDirProperties = []includeDirsProperty{
147 {
148 // ExportedIncludeDirs lists directories that contains some header files to be
149 // copied into a directory in the snapshot. The snapshot directories must be added to
150 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000151 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.ExportedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000152 propertyName: "export_include_dirs",
153 snapshotDir: nativeIncludeDir,
154 copy: true,
155 dirs: true,
156 },
157 {
158 // ExportedSystemIncludeDirs lists directories that contains some system header files to
159 // be copied into a directory in the snapshot. The snapshot directories must be added to
160 // the export_system_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.ExportedSystemIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000162 propertyName: "export_system_include_dirs",
163 snapshotDir: nativeIncludeDir,
164 copy: true,
165 dirs: true,
166 },
167 {
168 // exportedGeneratedIncludeDirs lists directories that contains some header files
169 // that are explicitly listed in the exportedGeneratedHeaders property. So, the contents
170 // of these directories do not need to be copied, but these directories do need adding to
171 // the export_include_dirs property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000172 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedIncludeDirs },
Paul Duffin64f54b02020-02-20 14:33:54 +0000173 propertyName: "export_include_dirs",
174 snapshotDir: nativeGeneratedIncludeDir,
175 copy: false,
176 dirs: true,
177 },
178 {
179 // exportedGeneratedHeaders lists header files that are in one of the directories
180 // specified in exportedGeneratedIncludeDirs must be copied into the snapshot.
181 // As they are in a directory in exportedGeneratedIncludeDirs they do not need adding to a
182 // property in the prebuilt module in the snapshot.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000183 pathsGetter: func(libInfo *nativeLibInfoProperties) android.Paths { return libInfo.exportedGeneratedHeaders },
Paul Duffin64f54b02020-02-20 14:33:54 +0000184 propertyName: "",
185 snapshotDir: nativeGeneratedIncludeDir,
186 copy: true,
187 dirs: false,
188 },
189}
190
191// Add properties that may, or may not, be arch specific.
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000192func addPossiblyArchSpecificProperties(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, libInfo *nativeLibInfoProperties, outputProperties android.BpPropertySet) {
193
194 // Copy the generated library to the snapshot and add a reference to it in the .bp module.
195 if libInfo.outputFile != nil {
196 nativeLibraryPath := nativeLibraryPathFor(libInfo)
197 builder.CopyToSnapshot(libInfo.outputFile, nativeLibraryPath)
198 outputProperties.AddProperty("srcs", []string{nativeLibraryPath})
199 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000200
Paul Duffin13f02712020-03-06 12:30:43 +0000201 if len(libInfo.SharedLibs) > 0 {
202 outputProperties.AddPropertyWithTag("shared_libs", libInfo.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
203 }
204
205 if len(libInfo.SystemSharedLibs) > 0 {
206 outputProperties.AddPropertyWithTag("system_shared_libs", libInfo.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
207 }
208
Paul Duffin64f54b02020-02-20 14:33:54 +0000209 // Map from property name to the include dirs to add to the prebuilt module in the snapshot.
210 includeDirs := make(map[string][]string)
211
212 // Iterate over each include directory property, copying files and collating property
213 // values where necessary.
214 for _, propertyInfo := range includeDirProperties {
215 // Calculate the base directory in the snapshot into which the files will be copied.
216 // lib.ArchType is "" for common properties.
217 targetDir := filepath.Join(libInfo.archType, propertyInfo.snapshotDir)
218
219 propertyName := propertyInfo.propertyName
220
221 // Iterate over each path in one of the include directory properties.
222 for _, path := range propertyInfo.pathsGetter(libInfo) {
223
224 // Copy the files/directories when necessary.
225 if propertyInfo.copy {
226 if propertyInfo.dirs {
227 // When copying a directory glob and copy all the headers within it.
228 // TODO(jiyong) copy headers having other suffixes
229 headers, _ := sdkModuleContext.GlobWithDeps(path.String()+"/**/*.h", nil)
230 for _, file := range headers {
231 src := android.PathForSource(sdkModuleContext, file)
232 dest := filepath.Join(targetDir, file)
233 builder.CopyToSnapshot(src, dest)
234 }
235 } else {
236 // Otherwise, just copy the files.
237 dest := filepath.Join(targetDir, libInfo.name, path.Rel())
238 builder.CopyToSnapshot(path, dest)
239 }
240 }
241
242 // Only directories are added to a property.
243 if propertyInfo.dirs {
244 var snapshotPath string
245 if isGeneratedHeaderDirectory(path) {
246 snapshotPath = filepath.Join(targetDir, libInfo.name)
247 } else {
248 snapshotPath = filepath.Join(targetDir, path.String())
249 }
250
251 includeDirs[propertyName] = append(includeDirs[propertyName], snapshotPath)
252 }
253 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000254 }
Paul Duffin64f54b02020-02-20 14:33:54 +0000255
256 // Add the collated include dir properties to the output.
257 for property, dirs := range includeDirs {
258 outputProperties.AddProperty(property, dirs)
Paul Duffin74fc1902020-01-23 11:45:03 +0000259 }
Paul Duffin74fc1902020-01-23 11:45:03 +0000260}
261
Paul Duffin2f6bc092019-12-13 10:40:56 +0000262const (
263 nativeIncludeDir = "include"
264 nativeGeneratedIncludeDir = "include_gen"
265 nativeStubDir = "lib"
266)
267
268// path to the native library. Relative to <sdk_root>/<api_dir>
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000269func nativeLibraryPathFor(lib *nativeLibInfoProperties) string {
Paul Duffina04c1072020-03-02 10:16:35 +0000270 return filepath.Join(lib.OsPrefix(), lib.archType,
Paul Duffin2f6bc092019-12-13 10:40:56 +0000271 nativeStubDir, lib.outputFile.Base())
272}
273
Paul Duffin2f6bc092019-12-13 10:40:56 +0000274// nativeLibInfoProperties represents properties of a native lib
275//
276// The exported (capitalized) fields will be examined and may be changed during common value extraction.
277// The unexported fields will be left untouched.
278type nativeLibInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000279 android.SdkMemberPropertiesBase
280
281 memberType *librarySdkMemberType
282
Paul Duffin2f6bc092019-12-13 10:40:56 +0000283 // The name of the library, is not exported as this must not be changed during optimization.
284 name string
285
286 // archType is not exported as if set (to a non default value) it is always arch specific.
287 // This is "" for common properties.
288 archType string
289
Paul Duffin5efd1982020-02-20 14:33:54 +0000290 // The list of possibly common exported include dirs.
291 //
292 // This field is exported as its contents may not be arch specific.
293 ExportedIncludeDirs android.Paths
Paul Duffin2f6bc092019-12-13 10:40:56 +0000294
Paul Duffin5efd1982020-02-20 14:33:54 +0000295 // The list of arch specific exported generated include dirs.
296 //
297 // This field is not exported as its contents are always arch specific.
298 exportedGeneratedIncludeDirs android.Paths
299
300 // The list of arch specific exported generated header files.
301 //
302 // This field is not exported as its contents are is always arch specific.
Paul Duffin2f6bc092019-12-13 10:40:56 +0000303 exportedGeneratedHeaders android.Paths
304
Paul Duffin5efd1982020-02-20 14:33:54 +0000305 // The list of possibly common exported system include dirs.
306 //
307 // This field is exported as its contents may not be arch specific.
308 ExportedSystemIncludeDirs android.Paths
309
310 // The list of possibly common exported flags.
311 //
312 // This field is exported as its contents may not be arch specific.
313 ExportedFlags []string
314
Paul Duffin13f02712020-03-06 12:30:43 +0000315 // The set of shared libraries
316 //
317 // This field is exported as its contents may not be arch specific.
318 SharedLibs []string
319
320 // The set of system shared libraries
321 //
322 // This field is exported as its contents may not be arch specific.
323 SystemSharedLibs []string
324
Paul Duffin2f6bc092019-12-13 10:40:56 +0000325 // outputFile is not exported as it is always arch specific.
326 outputFile android.Path
327}
328
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000329func (p *nativeLibInfoProperties) PopulateFromVariant(variant android.SdkAware) {
330 ccModule := variant.(*Module)
331
332 // If the library has some link types then it produces an output binary file, otherwise it
333 // is header only.
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000334 if !p.memberType.noOutputFiles {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000335 p.outputFile = ccModule.OutputFile().Path()
336 }
337
338 // Separate out the generated include dirs (which are arch specific) from the
339 // include dirs (which may not be).
340 exportedIncludeDirs, exportedGeneratedIncludeDirs := android.FilterPathListPredicate(
341 ccModule.ExportedIncludeDirs(), isGeneratedHeaderDirectory)
342
343 p.name = variant.Name()
344 p.archType = ccModule.Target().Arch.ArchType.String()
345 p.ExportedIncludeDirs = exportedIncludeDirs
346 p.exportedGeneratedIncludeDirs = exportedGeneratedIncludeDirs
347 p.ExportedSystemIncludeDirs = ccModule.ExportedSystemIncludeDirs()
348 p.ExportedFlags = ccModule.ExportedFlags()
Paul Duffin13f02712020-03-06 12:30:43 +0000349 if ccModule.linker != nil {
350 specifiedDeps := specifiedDeps{}
351 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
352
353 p.SharedLibs = specifiedDeps.sharedLibs
354 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
355 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000356 p.exportedGeneratedHeaders = ccModule.ExportedGeneratedHeaders()
357}
358
359func (p *nativeLibInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
360 addPossiblyArchSpecificProperties(sdkModuleContext, builder, p, propertySet)
Paul Duffin2f6bc092019-12-13 10:40:56 +0000361}