xref: /aosp_15_r20/build/soong/android/license_sdk_member.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2021 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage android
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"path/filepath"
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
21*333d2b36SAndroid Build Coastguard Worker)
22*333d2b36SAndroid Build Coastguard Worker
23*333d2b36SAndroid Build Coastguard Worker// Contains support for adding license modules to an sdk.
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Workerfunc init() {
26*333d2b36SAndroid Build Coastguard Worker	RegisterSdkMemberType(LicenseModuleSdkMemberType)
27*333d2b36SAndroid Build Coastguard Worker}
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Worker// licenseSdkMemberType determines how a license module is added to the sdk.
30*333d2b36SAndroid Build Coastguard Workertype licenseSdkMemberType struct {
31*333d2b36SAndroid Build Coastguard Worker	SdkMemberTypeBase
32*333d2b36SAndroid Build Coastguard Worker}
33*333d2b36SAndroid Build Coastguard Worker
34*333d2b36SAndroid Build Coastguard Workerfunc (l *licenseSdkMemberType) AddDependencies(ctx SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
35*333d2b36SAndroid Build Coastguard Worker	// Add dependencies onto the license module from the sdk module.
36*333d2b36SAndroid Build Coastguard Worker	ctx.AddDependency(ctx.Module(), dependencyTag, names...)
37*333d2b36SAndroid Build Coastguard Worker}
38*333d2b36SAndroid Build Coastguard Worker
39*333d2b36SAndroid Build Coastguard Workerfunc (l *licenseSdkMemberType) IsInstance(module Module) bool {
40*333d2b36SAndroid Build Coastguard Worker	// Verify that the module being added is compatible with this module type.
41*333d2b36SAndroid Build Coastguard Worker	_, ok := module.(*licenseModule)
42*333d2b36SAndroid Build Coastguard Worker	return ok
43*333d2b36SAndroid Build Coastguard Worker}
44*333d2b36SAndroid Build Coastguard Worker
45*333d2b36SAndroid Build Coastguard Workerfunc (l *licenseSdkMemberType) AddPrebuiltModule(ctx SdkMemberContext, member SdkMember) BpModule {
46*333d2b36SAndroid Build Coastguard Worker	// Add the basics of a prebuilt module.
47*333d2b36SAndroid Build Coastguard Worker	return ctx.SnapshotBuilder().AddPrebuiltModule(member, "license")
48*333d2b36SAndroid Build Coastguard Worker}
49*333d2b36SAndroid Build Coastguard Worker
50*333d2b36SAndroid Build Coastguard Workerfunc (l *licenseSdkMemberType) CreateVariantPropertiesStruct() SdkMemberProperties {
51*333d2b36SAndroid Build Coastguard Worker	// Create the structure into which the properties of the license module that need to be output to
52*333d2b36SAndroid Build Coastguard Worker	// the snapshot will be placed. The structure may be populated with information from a variant or
53*333d2b36SAndroid Build Coastguard Worker	// may be used as the destination for properties that are common to a set of variants.
54*333d2b36SAndroid Build Coastguard Worker	return &licenseSdkMemberProperties{}
55*333d2b36SAndroid Build Coastguard Worker}
56*333d2b36SAndroid Build Coastguard Worker
57*333d2b36SAndroid Build Coastguard Worker// LicenseModuleSdkMemberType is the instance of licenseSdkMemberType
58*333d2b36SAndroid Build Coastguard Workervar LicenseModuleSdkMemberType = &licenseSdkMemberType{
59*333d2b36SAndroid Build Coastguard Worker	SdkMemberTypeBase{
60*333d2b36SAndroid Build Coastguard Worker		PropertyName: "licenses",
61*333d2b36SAndroid Build Coastguard Worker
62*333d2b36SAndroid Build Coastguard Worker		// This should never be added directly to an sdk/module_exports, all license modules should be
63*333d2b36SAndroid Build Coastguard Worker		// added indirectly as transitive dependencies of other sdk members.
64*333d2b36SAndroid Build Coastguard Worker		BpPropertyNotRequired: true,
65*333d2b36SAndroid Build Coastguard Worker
66*333d2b36SAndroid Build Coastguard Worker		SupportsSdk: true,
67*333d2b36SAndroid Build Coastguard Worker
68*333d2b36SAndroid Build Coastguard Worker		// The snapshot of the license module is just another license module (not a prebuilt). They are
69*333d2b36SAndroid Build Coastguard Worker		// internal modules only so will have an sdk specific name that will not clash with the
70*333d2b36SAndroid Build Coastguard Worker		// originating source module.
71*333d2b36SAndroid Build Coastguard Worker		UseSourceModuleTypeInSnapshot: true,
72*333d2b36SAndroid Build Coastguard Worker	},
73*333d2b36SAndroid Build Coastguard Worker}
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Workervar _ SdkMemberType = (*licenseSdkMemberType)(nil)
76*333d2b36SAndroid Build Coastguard Worker
77*333d2b36SAndroid Build Coastguard Worker// licenseSdkMemberProperties is the set of properties that need to be added to the license module
78*333d2b36SAndroid Build Coastguard Worker// in the snapshot.
79*333d2b36SAndroid Build Coastguard Workertype licenseSdkMemberProperties struct {
80*333d2b36SAndroid Build Coastguard Worker	SdkMemberPropertiesBase
81*333d2b36SAndroid Build Coastguard Worker
82*333d2b36SAndroid Build Coastguard Worker	// The kinds of licenses provided by the module.
83*333d2b36SAndroid Build Coastguard Worker	License_kinds []string
84*333d2b36SAndroid Build Coastguard Worker
85*333d2b36SAndroid Build Coastguard Worker	// The source paths to the files containing license text.
86*333d2b36SAndroid Build Coastguard Worker	License_text Paths
87*333d2b36SAndroid Build Coastguard Worker}
88*333d2b36SAndroid Build Coastguard Worker
89*333d2b36SAndroid Build Coastguard Workerfunc (p *licenseSdkMemberProperties) PopulateFromVariant(_ SdkMemberContext, variant Module) {
90*333d2b36SAndroid Build Coastguard Worker	// Populate the properties from the variant.
91*333d2b36SAndroid Build Coastguard Worker	l := variant.(*licenseModule)
92*333d2b36SAndroid Build Coastguard Worker	p.License_kinds = l.properties.License_kinds
93*333d2b36SAndroid Build Coastguard Worker	p.License_text = make(Paths, 0, len(l.base().commonProperties.Effective_license_text))
94*333d2b36SAndroid Build Coastguard Worker	for _, np := range l.base().commonProperties.Effective_license_text {
95*333d2b36SAndroid Build Coastguard Worker		p.License_text = append(p.License_text, np.Path)
96*333d2b36SAndroid Build Coastguard Worker	}
97*333d2b36SAndroid Build Coastguard Worker}
98*333d2b36SAndroid Build Coastguard Worker
99*333d2b36SAndroid Build Coastguard Workerfunc (p *licenseSdkMemberProperties) AddToPropertySet(ctx SdkMemberContext, propertySet BpPropertySet) {
100*333d2b36SAndroid Build Coastguard Worker	// Just pass any specified license_kinds straight through.
101*333d2b36SAndroid Build Coastguard Worker	if len(p.License_kinds) > 0 {
102*333d2b36SAndroid Build Coastguard Worker		propertySet.AddProperty("license_kinds", p.License_kinds)
103*333d2b36SAndroid Build Coastguard Worker	}
104*333d2b36SAndroid Build Coastguard Worker
105*333d2b36SAndroid Build Coastguard Worker	// Copy any license test files to the snapshot into a module specific location.
106*333d2b36SAndroid Build Coastguard Worker	if len(p.License_text) > 0 {
107*333d2b36SAndroid Build Coastguard Worker		dests := []string{}
108*333d2b36SAndroid Build Coastguard Worker		for _, path := range p.License_text {
109*333d2b36SAndroid Build Coastguard Worker			// The destination path only uses the path of the license file in the source not the license
110*333d2b36SAndroid Build Coastguard Worker			// module name. That ensures that if the same license file is used by multiple license modules
111*333d2b36SAndroid Build Coastguard Worker			// that it only gets copied once as the snapshot builder will dedup copies where the source
112*333d2b36SAndroid Build Coastguard Worker			// and destination match.
113*333d2b36SAndroid Build Coastguard Worker			dest := filepath.Join("licenses", path.String())
114*333d2b36SAndroid Build Coastguard Worker			dests = append(dests, dest)
115*333d2b36SAndroid Build Coastguard Worker			ctx.SnapshotBuilder().CopyToSnapshot(path, dest)
116*333d2b36SAndroid Build Coastguard Worker		}
117*333d2b36SAndroid Build Coastguard Worker		propertySet.AddProperty("license_text", dests)
118*333d2b36SAndroid Build Coastguard Worker	}
119*333d2b36SAndroid Build Coastguard Worker}
120*333d2b36SAndroid Build Coastguard Worker
121*333d2b36SAndroid Build Coastguard Workervar _ SdkMemberProperties = (*licenseSdkMemberProperties)(nil)
122