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
15package android
16
17import (
18	"path/filepath"
19
20	"github.com/google/blueprint"
21)
22
23// Contains support for adding license modules to an sdk.
24
25func init() {
26	RegisterSdkMemberType(LicenseModuleSdkMemberType)
27}
28
29// licenseSdkMemberType determines how a license module is added to the sdk.
30type licenseSdkMemberType struct {
31	SdkMemberTypeBase
32}
33
34func (l *licenseSdkMemberType) AddDependencies(mctx BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
35	// Add dependencies onto the license module from the sdk module.
36	mctx.AddDependency(mctx.Module(), dependencyTag, names...)
37}
38
39func (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
45func (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
50func (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
58var 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
75var _ SdkMemberType = (*licenseSdkMemberType)(nil)
76
77// licenseSdkMemberProperties is the set of properties that need to be added to the license module
78// in the snapshot.
79type 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
89func (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
93	p.License_text = l.base().commonProperties.Effective_license_text
94}
95
96func (p *licenseSdkMemberProperties) AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet) {
97	// Just pass any specified license_kinds straight through.
98	if len(p.License_kinds) > 0 {
99		propertySet.AddProperty("license_kinds", p.License_kinds)
100	}
101
102	// Copy any license test files to the snapshot into a module specific location.
103	if len(p.License_text) > 0 {
104		dests := []string{}
105		for _, path := range p.License_text {
106			// The destination path only uses the path of the license file in the source not the license
107			// module name. That ensures that if the same license file is used by multiple license modules
108			// that it only gets copied once as the snapshot builder will dedup copies where the source
109			// and destination match.
110			dest := filepath.Join("licenses", path.String())
111			dests = append(dests, dest)
112			ctx.SnapshotBuilder().CopyToSnapshot(path, dest)
113		}
114		propertySet.AddProperty("license_text", dests)
115	}
116}
117
118var _ SdkMemberProperties = (*licenseSdkMemberProperties)(nil)
119