xref: /aosp_15_r20/build/soong/android/license.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2020 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	"github.com/google/blueprint"
19*333d2b36SAndroid Build Coastguard Worker)
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Workertype licenseKindDependencyTag struct {
22*333d2b36SAndroid Build Coastguard Worker	blueprint.BaseDependencyTag
23*333d2b36SAndroid Build Coastguard Worker}
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Workervar (
26*333d2b36SAndroid Build Coastguard Worker	licenseKindTag = licenseKindDependencyTag{}
27*333d2b36SAndroid Build Coastguard Worker)
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Workerfunc init() {
30*333d2b36SAndroid Build Coastguard Worker	RegisterLicenseBuildComponents(InitRegistrationContext)
31*333d2b36SAndroid Build Coastguard Worker}
32*333d2b36SAndroid Build Coastguard Worker
33*333d2b36SAndroid Build Coastguard Worker// Register the license module type.
34*333d2b36SAndroid Build Coastguard Workerfunc RegisterLicenseBuildComponents(ctx RegistrationContext) {
35*333d2b36SAndroid Build Coastguard Worker	ctx.RegisterModuleType("license", LicenseFactory)
36*333d2b36SAndroid Build Coastguard Worker}
37*333d2b36SAndroid Build Coastguard Worker
38*333d2b36SAndroid Build Coastguard Workertype licenseProperties struct {
39*333d2b36SAndroid Build Coastguard Worker	// Specifies the kinds of license that apply.
40*333d2b36SAndroid Build Coastguard Worker	License_kinds []string
41*333d2b36SAndroid Build Coastguard Worker	// Specifies a short copyright notice to use for the license.
42*333d2b36SAndroid Build Coastguard Worker	Copyright_notice *string
43*333d2b36SAndroid Build Coastguard Worker	// Specifies the path or label for the text of the license.
44*333d2b36SAndroid Build Coastguard Worker	License_text []string `android:"path"`
45*333d2b36SAndroid Build Coastguard Worker	// Specifies the package name to which the license applies.
46*333d2b36SAndroid Build Coastguard Worker	Package_name *string
47*333d2b36SAndroid Build Coastguard Worker	// Specifies where this license can be used
48*333d2b36SAndroid Build Coastguard Worker	Visibility []string
49*333d2b36SAndroid Build Coastguard Worker}
50*333d2b36SAndroid Build Coastguard Worker
51*333d2b36SAndroid Build Coastguard Workertype licenseModule struct {
52*333d2b36SAndroid Build Coastguard Worker	ModuleBase
53*333d2b36SAndroid Build Coastguard Worker	DefaultableModuleBase
54*333d2b36SAndroid Build Coastguard Worker
55*333d2b36SAndroid Build Coastguard Worker	properties licenseProperties
56*333d2b36SAndroid Build Coastguard Worker}
57*333d2b36SAndroid Build Coastguard Worker
58*333d2b36SAndroid Build Coastguard Workerfunc (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
59*333d2b36SAndroid Build Coastguard Worker	for i, license := range m.properties.License_kinds {
60*333d2b36SAndroid Build Coastguard Worker		for j := i + 1; j < len(m.properties.License_kinds); j++ {
61*333d2b36SAndroid Build Coastguard Worker			if license == m.properties.License_kinds[j] {
62*333d2b36SAndroid Build Coastguard Worker				ctx.ModuleErrorf("Duplicated license kind: %q", license)
63*333d2b36SAndroid Build Coastguard Worker				break
64*333d2b36SAndroid Build Coastguard Worker			}
65*333d2b36SAndroid Build Coastguard Worker		}
66*333d2b36SAndroid Build Coastguard Worker	}
67*333d2b36SAndroid Build Coastguard Worker	ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
68*333d2b36SAndroid Build Coastguard Worker}
69*333d2b36SAndroid Build Coastguard Worker
70*333d2b36SAndroid Build Coastguard Workerfunc (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
71*333d2b36SAndroid Build Coastguard Worker	// license modules have no licenses, but license_kinds must refer to license_kind modules
72*333d2b36SAndroid Build Coastguard Worker	mergeStringProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName())
73*333d2b36SAndroid Build Coastguard Worker	namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...)
74*333d2b36SAndroid Build Coastguard Worker	for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) {
75*333d2b36SAndroid Build Coastguard Worker		if lk, ok := module.(*licenseKindModule); ok {
76*333d2b36SAndroid Build Coastguard Worker			mergeStringProps(&m.base().commonProperties.Effective_license_conditions, lk.properties.Conditions...)
77*333d2b36SAndroid Build Coastguard Worker			mergeStringProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module))
78*333d2b36SAndroid Build Coastguard Worker		} else {
79*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
80*333d2b36SAndroid Build Coastguard Worker		}
81*333d2b36SAndroid Build Coastguard Worker	}
82*333d2b36SAndroid Build Coastguard Worker}
83*333d2b36SAndroid Build Coastguard Worker
84*333d2b36SAndroid Build Coastguard Workerfunc LicenseFactory() Module {
85*333d2b36SAndroid Build Coastguard Worker	module := &licenseModule{}
86*333d2b36SAndroid Build Coastguard Worker
87*333d2b36SAndroid Build Coastguard Worker	base := module.base()
88*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(&base.nameProperties, &module.properties)
89*333d2b36SAndroid Build Coastguard Worker
90*333d2b36SAndroid Build Coastguard Worker	// The visibility property needs to be checked and parsed by the visibility module.
91*333d2b36SAndroid Build Coastguard Worker	setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
92*333d2b36SAndroid Build Coastguard Worker
93*333d2b36SAndroid Build Coastguard Worker	initAndroidModuleBase(module)
94*333d2b36SAndroid Build Coastguard Worker	InitDefaultableModule(module)
95*333d2b36SAndroid Build Coastguard Worker
96*333d2b36SAndroid Build Coastguard Worker	return module
97*333d2b36SAndroid Build Coastguard Worker}
98