Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Cole Faust | 2ced8c8 | 2022-11-22 16:51:21 -0800 | [diff] [blame] | 18 | "github.com/google/blueprint" |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 19 | ) |
| 20 | |
Yu Liu | ddccb2a | 2025-01-17 20:04:46 +0000 | [diff] [blame] | 21 | type LicenseInfo struct { |
| 22 | EffectiveLicenses []string |
| 23 | PackageName *string |
| 24 | EffectiveLicenseText NamedPaths |
| 25 | EffectiveLicenseKinds []string |
| 26 | EffectiveLicenseConditions []string |
| 27 | } |
| 28 | |
| 29 | var LicenseInfoProvider = blueprint.NewProvider[LicenseInfo]() |
| 30 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 31 | type licenseKindDependencyTag struct { |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 32 | blueprint.BaseDependencyTag |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | var ( |
| 36 | licenseKindTag = licenseKindDependencyTag{} |
| 37 | ) |
| 38 | |
| 39 | func init() { |
| 40 | RegisterLicenseBuildComponents(InitRegistrationContext) |
| 41 | } |
| 42 | |
| 43 | // Register the license module type. |
| 44 | func RegisterLicenseBuildComponents(ctx RegistrationContext) { |
| 45 | ctx.RegisterModuleType("license", LicenseFactory) |
| 46 | } |
| 47 | |
| 48 | type 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 | |
| 61 | type licenseModule struct { |
| 62 | ModuleBase |
| 63 | DefaultableModuleBase |
| 64 | |
| 65 | properties licenseProperties |
| 66 | } |
| 67 | |
| 68 | func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) { |
Cole Faust | 2ced8c8 | 2022-11-22 16:51:21 -0800 | [diff] [blame] | 69 | 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 Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 77 | ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...) |
| 78 | } |
| 79 | |
| 80 | func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Paul Duffin | df5a905 | 2021-05-06 21:52:31 +0100 | [diff] [blame] | 81 | // license modules have no licenses, but license_kinds must refer to license_kind modules |
Paul Duffin | ec0836a | 2021-05-10 22:53:30 +0100 | [diff] [blame] | 82 | mergeStringProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName()) |
Bob Badour | 4101c71 | 2022-02-09 11:54:35 -0800 | [diff] [blame] | 83 | namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...) |
Yu Liu | ddccb2a | 2025-01-17 20:04:46 +0000 | [diff] [blame] | 84 | 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 Duffin | ec0836a | 2021-05-10 22:53:30 +0100 | [diff] [blame] | 87 | mergeStringProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module)) |
Paul Duffin | df5a905 | 2021-05-06 21:52:31 +0100 | [diff] [blame] | 88 | } else { |
| 89 | ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module)) |
| 90 | } |
| 91 | } |
Yu Liu | ddccb2a | 2025-01-17 20:04:46 +0000 | [diff] [blame] | 92 | |
| 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 Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func LicenseFactory() Module { |
| 103 | module := &licenseModule{} |
| 104 | |
| 105 | base := module.base() |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 106 | module.AddProperties(&base.nameProperties, &module.properties) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 107 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 108 | // The visibility property needs to be checked and parsed by the visibility module. |
| 109 | setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility) |
| 110 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 111 | initAndroidModuleBase(module) |
| 112 | InitDefaultableModule(module) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 113 | |
| 114 | return module |
| 115 | } |