blob: ffda58b37b149f449f6df48b7307fed4204b0c4a [file] [log] [blame]
Bob Badour37af0462021-01-07 03:34:31 +00001// 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 (
Cole Faust2ced8c82022-11-22 16:51:21 -080018 "github.com/google/blueprint"
Bob Badour37af0462021-01-07 03:34:31 +000019)
20
Yu Liuddccb2a2025-01-17 20:04:46 +000021type LicenseInfo struct {
22 EffectiveLicenses []string
23 PackageName *string
24 EffectiveLicenseText NamedPaths
25 EffectiveLicenseKinds []string
26 EffectiveLicenseConditions []string
27}
28
29var LicenseInfoProvider = blueprint.NewProvider[LicenseInfo]()
30
Bob Badour37af0462021-01-07 03:34:31 +000031type licenseKindDependencyTag struct {
Paul Duffin3c298a32021-03-04 17:44:03 +000032 blueprint.BaseDependencyTag
Bob Badour37af0462021-01-07 03:34:31 +000033}
34
35var (
36 licenseKindTag = licenseKindDependencyTag{}
37)
38
39func init() {
40 RegisterLicenseBuildComponents(InitRegistrationContext)
41}
42
43// Register the license module type.
44func RegisterLicenseBuildComponents(ctx RegistrationContext) {
45 ctx.RegisterModuleType("license", LicenseFactory)
46}
47
48type licenseProperties struct {
49 // Specifies the kinds of license that apply.
50 License_kinds []string
51 // Specifies a short copyright notice to use for the license.
52 Copyright_notice *string
53 // Specifies the path or label for the text of the license.
54 License_text []string `android:"path"`
55 // Specifies the package name to which the license applies.
56 Package_name *string
57 // Specifies where this license can be used
58 Visibility []string
59}
60
61type licenseModule struct {
62 ModuleBase
63 DefaultableModuleBase
64
65 properties licenseProperties
66}
67
68func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
Cole Faust2ced8c82022-11-22 16:51:21 -080069 for i, license := range m.properties.License_kinds {
70 for j := i + 1; j < len(m.properties.License_kinds); j++ {
71 if license == m.properties.License_kinds[j] {
72 ctx.ModuleErrorf("Duplicated license kind: %q", license)
73 break
74 }
75 }
76 }
Bob Badour37af0462021-01-07 03:34:31 +000077 ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
78}
79
80func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffindf5a9052021-05-06 21:52:31 +010081 // license modules have no licenses, but license_kinds must refer to license_kind modules
Paul Duffinec0836a2021-05-10 22:53:30 +010082 mergeStringProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName())
Bob Badour4101c712022-02-09 11:54:35 -080083 namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...)
Yu Liuddccb2a2025-01-17 20:04:46 +000084 for _, module := range ctx.GetDirectDepsProxyWithTag(licenseKindTag) {
85 if lk, ok := OtherModuleProvider(ctx, module, LicenseKindInfoProvider); ok {
86 mergeStringProps(&m.base().commonProperties.Effective_license_conditions, lk.Conditions...)
Paul Duffinec0836a2021-05-10 22:53:30 +010087 mergeStringProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module))
Paul Duffindf5a9052021-05-06 21:52:31 +010088 } else {
89 ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
90 }
91 }
Yu Liuddccb2a2025-01-17 20:04:46 +000092
93 SetProvider(ctx, LicenseInfoProvider, LicenseInfo{
94 EffectiveLicenses: m.base().commonProperties.Effective_licenses,
95 PackageName: m.properties.Package_name,
96 EffectiveLicenseText: m.base().commonProperties.Effective_license_text,
97 EffectiveLicenseKinds: m.base().commonProperties.Effective_license_kinds,
98 EffectiveLicenseConditions: m.base().commonProperties.Effective_license_conditions,
99 })
Bob Badour37af0462021-01-07 03:34:31 +0000100}
101
102func LicenseFactory() Module {
103 module := &licenseModule{}
104
105 base := module.base()
Colin Cross8ff10582023-12-07 13:10:56 -0800106 module.AddProperties(&base.nameProperties, &module.properties)
Bob Badour37af0462021-01-07 03:34:31 +0000107
Bob Badour37af0462021-01-07 03:34:31 +0000108 // The visibility property needs to be checked and parsed by the visibility module.
109 setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
110
Bob Badour37af0462021-01-07 03:34:31 +0000111 initAndroidModuleBase(module)
112 InitDefaultableModule(module)
Bob Badour37af0462021-01-07 03:34:31 +0000113
114 return module
115}