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 ( |
| 18 | "reflect" |
| 19 | "sync" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | // Adds cross-cutting licenses dependency to propagate license metadata through the build system. |
| 25 | // |
| 26 | // Stage 1 - bottom-up records package-level default_applicable_licenses property mapped by package name. |
| 27 | // Stage 2 - bottom-up converts licenses property or package default_applicable_licenses to dependencies. |
| 28 | // Stage 3 - bottom-up type-checks every added applicable license dependency and license_kind dependency. |
| 29 | // Stage 4 - GenerateBuildActions calculates properties for the union of license kinds, conditions and texts. |
| 30 | |
| 31 | type licensesDependencyTag struct { |
| 32 | blueprint.BaseDependencyTag |
| 33 | } |
| 34 | |
| 35 | var ( |
| 36 | licensesTag = licensesDependencyTag{} |
| 37 | ) |
| 38 | |
| 39 | // Describes the property provided by a module to reference applicable licenses. |
| 40 | type applicableLicensesProperty interface { |
| 41 | // The name of the property. e.g. default_applicable_licenses or licenses |
| 42 | getName() string |
| 43 | // The values assigned to the property. (Must reference license modules.) |
| 44 | getStrings() []string |
| 45 | } |
| 46 | |
| 47 | type applicableLicensesPropertyImpl struct { |
| 48 | name string |
| 49 | licensesProperty *[]string |
| 50 | } |
| 51 | |
| 52 | func newApplicableLicensesProperty(name string, licensesProperty *[]string) applicableLicensesProperty { |
| 53 | return applicableLicensesPropertyImpl{ |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 54 | name: name, |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 55 | licensesProperty: licensesProperty, |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func (p applicableLicensesPropertyImpl) getName() string { |
| 60 | return p.name |
| 61 | } |
| 62 | |
| 63 | func (p applicableLicensesPropertyImpl) getStrings() []string { |
| 64 | return *p.licensesProperty |
| 65 | } |
| 66 | |
| 67 | // Set the primary applicable licenses property for a module. |
| 68 | func setPrimaryLicensesProperty(module Module, name string, licensesProperty *[]string) { |
| 69 | module.base().primaryLicensesProperty = newApplicableLicensesProperty(name, licensesProperty) |
| 70 | } |
| 71 | |
| 72 | // Storage blob for a package's default_applicable_licenses mapped by package directory. |
| 73 | type licensesContainer struct { |
| 74 | licenses []string |
| 75 | } |
| 76 | |
| 77 | func (r licensesContainer) getLicenses() []string { |
| 78 | return r.licenses |
| 79 | } |
| 80 | |
| 81 | var packageDefaultLicensesMap = NewOnceKey("packageDefaultLicensesMap") |
| 82 | |
| 83 | // The map from package dir name to default applicable licenses as a licensesContainer. |
| 84 | func moduleToPackageDefaultLicensesMap(config Config) *sync.Map { |
| 85 | return config.Once(packageDefaultLicensesMap, func() interface{} { |
| 86 | return &sync.Map{} |
| 87 | }).(*sync.Map) |
| 88 | } |
| 89 | |
| 90 | // Registers the function that maps each package to its default_applicable_licenses. |
| 91 | // |
| 92 | // This goes before defaults expansion so the defaults can pick up the package default. |
| 93 | func RegisterLicensesPackageMapper(ctx RegisterMutatorsContext) { |
| 94 | ctx.BottomUp("licensesPackageMapper", licensesPackageMapper).Parallel() |
| 95 | } |
| 96 | |
| 97 | // Registers the function that gathers the license dependencies for each module. |
| 98 | // |
| 99 | // This goes after defaults expansion so that it can pick up default licenses and before visibility enforcement. |
| 100 | func RegisterLicensesPropertyGatherer(ctx RegisterMutatorsContext) { |
| 101 | ctx.BottomUp("licensesPropertyGatherer", licensesPropertyGatherer).Parallel() |
| 102 | } |
| 103 | |
| 104 | // Registers the function that verifies the licenses and license_kinds dependency types for each module. |
| 105 | func RegisterLicensesDependencyChecker(ctx RegisterMutatorsContext) { |
| 106 | ctx.BottomUp("licensesPropertyChecker", licensesDependencyChecker).Parallel() |
| 107 | } |
| 108 | |
| 109 | // Maps each package to its default applicable licenses. |
| 110 | func licensesPackageMapper(ctx BottomUpMutatorContext) { |
| 111 | p, ok := ctx.Module().(*packageModule) |
| 112 | if !ok { |
| 113 | return |
| 114 | } |
| 115 | |
| 116 | licenses := getLicenses(ctx, p) |
| 117 | |
| 118 | dir := ctx.ModuleDir() |
| 119 | c := makeLicensesContainer(licenses) |
| 120 | moduleToPackageDefaultLicensesMap(ctx.Config()).Store(dir, c) |
| 121 | } |
| 122 | |
| 123 | // Copies the default_applicable_licenses property values for mapping by package directory. |
| 124 | func makeLicensesContainer(propVals []string) licensesContainer { |
| 125 | licenses := make([]string, 0, len(propVals)) |
| 126 | licenses = append(licenses, propVals...) |
| 127 | |
| 128 | return licensesContainer{licenses} |
| 129 | } |
| 130 | |
| 131 | // Gathers the applicable licenses into dependency references after defaults expansion. |
| 132 | func licensesPropertyGatherer(ctx BottomUpMutatorContext) { |
| 133 | m, ok := ctx.Module().(Module) |
| 134 | if !ok { |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | if exemptFromRequiredApplicableLicensesProperty(m) { |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | licenses := getLicenses(ctx, m) |
| 143 | |
| 144 | ctx.AddVariationDependencies(nil, licensesTag, licenses...) |
| 145 | } |
| 146 | |
| 147 | // Verifies the license and license_kind dependencies are each the correct kind of module. |
| 148 | func licensesDependencyChecker(ctx BottomUpMutatorContext) { |
| 149 | m, ok := ctx.Module().(Module) |
| 150 | if !ok { |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | // license modules have no licenses, but license_kinds must refer to license_kind modules |
| 155 | if _, ok := m.(*licenseModule); ok { |
| 156 | for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) { |
| 157 | if _, ok := module.(*licenseKindModule); !ok { |
| 158 | ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module)) |
| 159 | } |
| 160 | } |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | if exemptFromRequiredApplicableLicensesProperty(m) { |
| 165 | return |
| 166 | } |
| 167 | |
| 168 | for _, module := range ctx.GetDirectDepsWithTag(licensesTag) { |
| 169 | if _, ok := module.(*licenseModule); !ok { |
| 170 | propertyName := "licenses" |
| 171 | primaryProperty := m.base().primaryLicensesProperty |
| 172 | if primaryProperty != nil { |
| 173 | propertyName = primaryProperty.getName() |
| 174 | } |
| 175 | ctx.ModuleErrorf("%s property %q is not a license module", propertyName, ctx.OtherModuleName(module)) |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Flattens license and license_kind dependencies into calculated properties. |
| 181 | // |
| 182 | // Re-validates applicable licenses properties refer only to license modules and license_kinds properties refer |
| 183 | // only to license_kind modules. |
| 184 | func licensesPropertyFlattener(ctx ModuleContext) { |
| 185 | m, ok := ctx.Module().(Module) |
| 186 | if !ok { |
| 187 | return |
| 188 | } |
| 189 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 190 | if exemptFromRequiredApplicableLicensesProperty(m) { |
| 191 | return |
| 192 | } |
| 193 | |
| 194 | for _, module := range ctx.GetDirectDepsWithTag(licensesTag) { |
| 195 | if l, ok := module.(*licenseModule); ok { |
| 196 | if m.base().commonProperties.Effective_package_name == nil && l.properties.Package_name != nil { |
| 197 | m.base().commonProperties.Effective_package_name = l.properties.Package_name |
| 198 | } |
Paul Duffin | ec0836a | 2021-05-10 22:53:30 +0100 | [diff] [blame^] | 199 | mergeStringProps(&m.base().commonProperties.Effective_licenses, module.base().commonProperties.Effective_licenses...) |
| 200 | mergePathProps(&m.base().commonProperties.Effective_license_text, module.base().commonProperties.Effective_license_text...) |
| 201 | mergeStringProps(&m.base().commonProperties.Effective_license_kinds, module.base().commonProperties.Effective_license_kinds...) |
| 202 | mergeStringProps(&m.base().commonProperties.Effective_license_conditions, module.base().commonProperties.Effective_license_conditions...) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 203 | } else { |
| 204 | propertyName := "licenses" |
| 205 | primaryProperty := m.base().primaryLicensesProperty |
| 206 | if primaryProperty != nil { |
| 207 | propertyName = primaryProperty.getName() |
| 208 | } |
| 209 | ctx.ModuleErrorf("%s property %q is not a license module", propertyName, ctx.OtherModuleName(module)) |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Update a property string array with a distinct union of its values and a list of new values. |
Paul Duffin | ec0836a | 2021-05-10 22:53:30 +0100 | [diff] [blame^] | 215 | func mergeStringProps(prop *[]string, values ...string) { |
| 216 | *prop = append(*prop, values...) |
| 217 | *prop = SortedUniqueStrings(*prop) |
| 218 | } |
| 219 | |
| 220 | // Update a property Path array with a distinct union of its values and a list of new values. |
| 221 | func mergePathProps(prop *Paths, values ...Path) { |
| 222 | *prop = append(*prop, values...) |
| 223 | *prop = SortedUniquePaths(*prop) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Get the licenses property falling back to the package default. |
| 227 | func getLicenses(ctx BaseModuleContext, module Module) []string { |
| 228 | if exemptFromRequiredApplicableLicensesProperty(module) { |
| 229 | return nil |
| 230 | } |
| 231 | |
| 232 | primaryProperty := module.base().primaryLicensesProperty |
| 233 | if primaryProperty == nil { |
| 234 | if ctx.Config().IsEnvTrue("ANDROID_REQUIRE_LICENSES") { |
| 235 | ctx.ModuleErrorf("module type %q must have an applicable licenses property", ctx.OtherModuleType(module)) |
| 236 | } |
| 237 | return nil |
| 238 | } |
| 239 | |
| 240 | licenses := primaryProperty.getStrings() |
| 241 | if len(licenses) > 0 { |
| 242 | s := make(map[string]bool) |
| 243 | for _, l := range licenses { |
| 244 | if _, ok := s[l]; ok { |
| 245 | ctx.ModuleErrorf("duplicate %q %s", l, primaryProperty.getName()) |
| 246 | } |
| 247 | s[l] = true |
| 248 | } |
| 249 | return licenses |
| 250 | } |
| 251 | |
| 252 | dir := ctx.OtherModuleDir(module) |
| 253 | |
| 254 | moduleToApplicableLicenses := moduleToPackageDefaultLicensesMap(ctx.Config()) |
| 255 | value, ok := moduleToApplicableLicenses.Load(dir) |
| 256 | var c licensesContainer |
| 257 | if ok { |
| 258 | c = value.(licensesContainer) |
| 259 | } else { |
| 260 | c = licensesContainer{} |
| 261 | } |
| 262 | return c.getLicenses() |
| 263 | } |
| 264 | |
| 265 | // Returns whether a module is an allowed list of modules that do not have or need applicable licenses. |
| 266 | func exemptFromRequiredApplicableLicensesProperty(module Module) bool { |
| 267 | switch reflect.TypeOf(module).String() { |
| 268 | case "*android.licenseModule": // is a license, doesn't need one |
| 269 | case "*android.licenseKindModule": // is a license, doesn't need one |
| 270 | case "*android.NamespaceModule": // just partitions things, doesn't add anything |
| 271 | case "*android.soongConfigModuleTypeModule": // creates aliases for modules with licenses |
| 272 | case "*android.soongConfigModuleTypeImport": // creates aliases for modules with licenses |
| 273 | case "*android.soongConfigStringVariableDummyModule": // used for creating aliases |
| 274 | case "*android.SoongConfigBoolVariableDummyModule": // used for creating aliases |
| 275 | default: |
| 276 | return false |
| 277 | } |
| 278 | return true |
| 279 | } |