blob: 7b4aeeb5d625c716a40cd56c4f47ee401a073895 [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 {
Yu Liuddccb2a2025-01-17 20:04:46 +000022 PackageName *string
23 EffectiveLicenseText NamedPaths
24 EffectiveLicenseKinds []string
25 EffectiveLicenseConditions []string
26}
27
28var LicenseInfoProvider = blueprint.NewProvider[LicenseInfo]()
29
Bob Badour37af0462021-01-07 03:34:31 +000030type licenseKindDependencyTag struct {
Paul Duffin3c298a32021-03-04 17:44:03 +000031 blueprint.BaseDependencyTag
Bob Badour37af0462021-01-07 03:34:31 +000032}
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) {
Cole Faust2ced8c82022-11-22 16:51:21 -080068 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 }
Bob Badour37af0462021-01-07 03:34:31 +000076 ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
77}
78
79func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffindf5a9052021-05-06 21:52:31 +010080 // license modules have no licenses, but license_kinds must refer to license_kind modules
Bob Badour4101c712022-02-09 11:54:35 -080081 namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...)
Yu Liu47361852025-01-23 19:28:17 +000082 var conditions []string
83 var kinds []string
Yu Liuddccb2a2025-01-17 20:04:46 +000084 for _, module := range ctx.GetDirectDepsProxyWithTag(licenseKindTag) {
85 if lk, ok := OtherModuleProvider(ctx, module, LicenseKindInfoProvider); ok {
Yu Liu47361852025-01-23 19:28:17 +000086 conditions = append(conditions, lk.Conditions...)
87 kinds = append(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{
Yu Liuddccb2a2025-01-17 20:04:46 +000094 PackageName: m.properties.Package_name,
95 EffectiveLicenseText: m.base().commonProperties.Effective_license_text,
Yu Liu47361852025-01-23 19:28:17 +000096 EffectiveLicenseKinds: SortedUniqueStrings(kinds),
97 EffectiveLicenseConditions: SortedUniqueStrings(conditions),
Yu Liuddccb2a2025-01-17 20:04:46 +000098 })
Bob Badour37af0462021-01-07 03:34:31 +000099}
100
101func LicenseFactory() Module {
102 module := &licenseModule{}
103
104 base := module.base()
Colin Cross8ff10582023-12-07 13:10:56 -0800105 module.AddProperties(&base.nameProperties, &module.properties)
Bob Badour37af0462021-01-07 03:34:31 +0000106
Bob Badour37af0462021-01-07 03:34:31 +0000107 // The visibility property needs to be checked and parsed by the visibility module.
108 setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
109
Bob Badour37af0462021-01-07 03:34:31 +0000110 initAndroidModuleBase(module)
111 InitDefaultableModule(module)
Bob Badour37af0462021-01-07 03:34:31 +0000112
113 return module
114}