license_sdk_member.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2021 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. package android
  15. import (
  16. "path/filepath"
  17. "github.com/google/blueprint"
  18. )
  19. // Contains support for adding license modules to an sdk.
  20. func init() {
  21. RegisterSdkMemberType(LicenseModuleSdkMemberType)
  22. }
  23. // licenseSdkMemberType determines how a license module is added to the sdk.
  24. type licenseSdkMemberType struct {
  25. SdkMemberTypeBase
  26. }
  27. func (l *licenseSdkMemberType) AddDependencies(ctx SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
  28. // Add dependencies onto the license module from the sdk module.
  29. ctx.AddDependency(ctx.Module(), dependencyTag, names...)
  30. }
  31. func (l *licenseSdkMemberType) IsInstance(module Module) bool {
  32. // Verify that the module being added is compatible with this module type.
  33. _, ok := module.(*licenseModule)
  34. return ok
  35. }
  36. func (l *licenseSdkMemberType) AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule {
  37. // Add the basics of a prebuilt module.
  38. return ctx.SnapshotBuilder().AddPrebuiltModule(member, "license")
  39. }
  40. func (l *licenseSdkMemberType) CreateVariantPropertiesStruct() SdkMemberProperties {
  41. // Create the structure into which the properties of the license module that need to be output to
  42. // the snapshot will be placed. The structure may be populated with information from a variant or
  43. // may be used as the destination for properties that are common to a set of variants.
  44. return &licenseSdkMemberProperties{}
  45. }
  46. // LicenseModuleSdkMemberType is the instance of licenseSdkMemberType
  47. var LicenseModuleSdkMemberType = &licenseSdkMemberType{
  48. SdkMemberTypeBase{
  49. PropertyName: "licenses",
  50. // This should never be added directly to an sdk/module_exports, all license modules should be
  51. // added indirectly as transitive dependencies of other sdk members.
  52. BpPropertyNotRequired: true,
  53. SupportsSdk: true,
  54. // The snapshot of the license module is just another license module (not a prebuilt). They are
  55. // internal modules only so will have an sdk specific name that will not clash with the
  56. // originating source module.
  57. UseSourceModuleTypeInSnapshot: true,
  58. },
  59. }
  60. var _ SdkMemberType = (*licenseSdkMemberType)(nil)
  61. // licenseSdkMemberProperties is the set of properties that need to be added to the license module
  62. // in the snapshot.
  63. type licenseSdkMemberProperties struct {
  64. SdkMemberPropertiesBase
  65. // The kinds of licenses provided by the module.
  66. License_kinds []string
  67. // The source paths to the files containing license text.
  68. License_text Paths
  69. }
  70. func (p *licenseSdkMemberProperties) PopulateFromVariant(_ SdkMemberContext, variant Module) {
  71. // Populate the properties from the variant.
  72. l := variant.(*licenseModule)
  73. p.License_kinds = l.properties.License_kinds
  74. p.License_text = make(Paths, 0, len(l.base().commonProperties.Effective_license_text))
  75. for _, np := range l.base().commonProperties.Effective_license_text {
  76. p.License_text = append(p.License_text, np.Path)
  77. }
  78. }
  79. func (p *licenseSdkMemberProperties) AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet) {
  80. // Just pass any specified license_kinds straight through.
  81. if len(p.License_kinds) > 0 {
  82. propertySet.AddProperty("license_kinds", p.License_kinds)
  83. }
  84. // Copy any license test files to the snapshot into a module specific location.
  85. if len(p.License_text) > 0 {
  86. dests := []string{}
  87. for _, path := range p.License_text {
  88. // The destination path only uses the path of the license file in the source not the license
  89. // module name. That ensures that if the same license file is used by multiple license modules
  90. // that it only gets copied once as the snapshot builder will dedup copies where the source
  91. // and destination match.
  92. dest := filepath.Join("licenses", path.String())
  93. dests = append(dests, dest)
  94. ctx.SnapshotBuilder().CopyToSnapshot(path, dest)
  95. }
  96. propertySet.AddProperty("license_text", dests)
  97. }
  98. }
  99. var _ SdkMemberProperties = (*licenseSdkMemberProperties)(nil)