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