• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 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	"github.com/google/blueprint"
19)
20
21type LicenseInfo struct {
22	PackageName                *string
23	EffectiveLicenseText       NamedPaths
24	EffectiveLicenseKinds      []string
25	EffectiveLicenseConditions []string
26}
27
28var LicenseInfoProvider = blueprint.NewProvider[LicenseInfo]()
29
30type licenseKindDependencyTag struct {
31	blueprint.BaseDependencyTag
32}
33
34var (
35	licenseKindTag = licenseKindDependencyTag{}
36)
37
38func init() {
39	RegisterLicenseBuildComponents(InitRegistrationContext)
40}
41
42// Register the license module type.
43func RegisterLicenseBuildComponents(ctx RegistrationContext) {
44	ctx.RegisterModuleType("license", LicenseFactory)
45}
46
47type licenseProperties struct {
48	// Specifies the kinds of license that apply.
49	License_kinds []string
50	// Specifies a short copyright notice to use for the license.
51	Copyright_notice *string
52	// Specifies the path or label for the text of the license.
53	License_text []string `android:"path"`
54	// Specifies the package name to which the license applies.
55	Package_name *string
56	// Specifies where this license can be used
57	Visibility []string
58}
59
60type licenseModule struct {
61	ModuleBase
62	DefaultableModuleBase
63
64	properties licenseProperties
65}
66
67func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
68	for i, license := range m.properties.License_kinds {
69		for j := i + 1; j < len(m.properties.License_kinds); j++ {
70			if license == m.properties.License_kinds[j] {
71				ctx.ModuleErrorf("Duplicated license kind: %q", license)
72				break
73			}
74		}
75	}
76	ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
77}
78
79func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
80	// license modules have no licenses, but license_kinds must refer to license_kind modules
81	namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...)
82	var conditions []string
83	var kinds []string
84	for _, module := range ctx.GetDirectDepsProxyWithTag(licenseKindTag) {
85		if lk, ok := OtherModuleProvider(ctx, module, LicenseKindInfoProvider); ok {
86			conditions = append(conditions, lk.Conditions...)
87			kinds = append(kinds, ctx.OtherModuleName(module))
88		} else {
89			ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
90		}
91	}
92
93	SetProvider(ctx, LicenseInfoProvider, LicenseInfo{
94		PackageName:                m.properties.Package_name,
95		EffectiveLicenseText:       m.base().commonProperties.Effective_license_text,
96		EffectiveLicenseKinds:      SortedUniqueStrings(kinds),
97		EffectiveLicenseConditions: SortedUniqueStrings(conditions),
98	})
99}
100
101func LicenseFactory() Module {
102	module := &licenseModule{}
103
104	base := module.base()
105	module.AddProperties(&base.nameProperties, &module.properties)
106
107	// The visibility property needs to be checked and parsed by the visibility module.
108	setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
109
110	initAndroidModuleBase(module)
111	InitDefaultableModule(module)
112
113	return module
114}
115