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