DO NOT MERGE "Add ability to declare licenses in soong."

df98d3e4a5

Test: m nothing

Bug: 180688789

Change-Id: Ie7e1695ecb8cd943124426a0cad2c0d1db2b46ed
This commit is contained in:
Bob Badour 2021-01-07 03:34:31 +00:00
parent 76b0ff7cc3
commit 5553f7fb61
9 changed files with 498 additions and 2 deletions

View File

@ -23,6 +23,9 @@ bootstrap_go_package {
"filegroup.go",
"hooks.go",
"image.go",
"license.go",
"license_kind.go",
"licenses.go",
"makevars.go",
"module.go",
"mutator.go",

View File

@ -223,6 +223,9 @@ func InitDefaultsModule(module DefaultsModule) {
// its checking phase and parsing phase so add it to the list as a normal property.
AddVisibilityProperty(module, "visibility", &commonProperties.Visibility)
// The applicable licenses property for defaults is 'licenses'.
setPrimaryLicensesProperty(module, "licenses", &commonProperties.Licenses)
base.module = module
}

82
android/license.go Normal file
View File

@ -0,0 +1,82 @@
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
import (
"github.com/google/blueprint"
)
type licenseKindDependencyTag struct {
blueprint.BaseDependencyTag
}
var (
licenseKindTag = licenseKindDependencyTag{}
)
func init() {
RegisterLicenseBuildComponents(InitRegistrationContext)
}
// Register the license module type.
func RegisterLicenseBuildComponents(ctx RegistrationContext) {
ctx.RegisterModuleType("license", LicenseFactory)
}
type licenseProperties struct {
// Specifies the kinds of license that apply.
License_kinds []string
// Specifies a short copyright notice to use for the license.
Copyright_notice *string
// Specifies the path or label for the text of the license.
License_text []string `android:"path"`
// Specifies the package name to which the license applies.
Package_name *string
// Specifies where this license can be used
Visibility []string
}
type licenseModule struct {
ModuleBase
DefaultableModuleBase
properties licenseProperties
}
func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
}
func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
// Nothing to do.
}
func LicenseFactory() Module {
module := &licenseModule{}
base := module.base()
module.AddProperties(&base.nameProperties, &module.properties)
base.generalProperties = module.GetProperties()
base.customizableProperties = module.GetProperties()
// The visibility property needs to be checked and parsed by the visibility module.
setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
initAndroidModuleBase(module)
InitDefaultableModule(module)
return module
}

66
android/license_kind.go Normal file
View File

@ -0,0 +1,66 @@
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
func init() {
RegisterLicenseKindBuildComponents(InitRegistrationContext)
}
// Register the license_kind module type.
func RegisterLicenseKindBuildComponents(ctx RegistrationContext) {
ctx.RegisterModuleType("license_kind", LicenseKindFactory)
}
type licenseKindProperties struct {
// Specifies the conditions for all licenses of the kind.
Conditions []string
// Specifies the url to the canonical license definition.
Url string
// Specifies where this license can be used
Visibility []string
}
type licenseKindModule struct {
ModuleBase
DefaultableModuleBase
properties licenseKindProperties
}
func (m *licenseKindModule) DepsMutator(ctx BottomUpMutatorContext) {
// Nothing to do.
}
func (m *licenseKindModule) GenerateAndroidBuildActions(ModuleContext) {
// Nothing to do.
}
func LicenseKindFactory() Module {
module := &licenseKindModule{}
base := module.base()
module.AddProperties(&base.nameProperties, &module.properties)
base.generalProperties = module.GetProperties()
base.customizableProperties = module.GetProperties()
// The visibility property needs to be checked and parsed by the visibility module.
setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
initAndroidModuleBase(module)
InitDefaultableModule(module)
return module
}

295
android/licenses.go Normal file
View File

@ -0,0 +1,295 @@
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
import (
"reflect"
"sync"
"github.com/google/blueprint"
)
// Adds cross-cutting licenses dependency to propagate license metadata through the build system.
//
// Stage 1 - bottom-up records package-level default_applicable_licenses property mapped by package name.
// Stage 2 - bottom-up converts licenses property or package default_applicable_licenses to dependencies.
// Stage 3 - bottom-up type-checks every added applicable license dependency and license_kind dependency.
// Stage 4 - GenerateBuildActions calculates properties for the union of license kinds, conditions and texts.
type licensesDependencyTag struct {
blueprint.BaseDependencyTag
}
var (
licensesTag = licensesDependencyTag{}
)
// Describes the property provided by a module to reference applicable licenses.
type applicableLicensesProperty interface {
// The name of the property. e.g. default_applicable_licenses or licenses
getName() string
// The values assigned to the property. (Must reference license modules.)
getStrings() []string
}
type applicableLicensesPropertyImpl struct {
name string
licensesProperty *[]string
}
func newApplicableLicensesProperty(name string, licensesProperty *[]string) applicableLicensesProperty {
return applicableLicensesPropertyImpl{
name: name,
licensesProperty: licensesProperty,
}
}
func (p applicableLicensesPropertyImpl) getName() string {
return p.name
}
func (p applicableLicensesPropertyImpl) getStrings() []string {
return *p.licensesProperty
}
// Set the primary applicable licenses property for a module.
func setPrimaryLicensesProperty(module Module, name string, licensesProperty *[]string) {
module.base().primaryLicensesProperty = newApplicableLicensesProperty(name, licensesProperty)
}
// Storage blob for a package's default_applicable_licenses mapped by package directory.
type licensesContainer struct {
licenses []string
}
func (r licensesContainer) getLicenses() []string {
return r.licenses
}
var packageDefaultLicensesMap = NewOnceKey("packageDefaultLicensesMap")
// The map from package dir name to default applicable licenses as a licensesContainer.
func moduleToPackageDefaultLicensesMap(config Config) *sync.Map {
return config.Once(packageDefaultLicensesMap, func() interface{} {
return &sync.Map{}
}).(*sync.Map)
}
// Registers the function that maps each package to its default_applicable_licenses.
//
// This goes before defaults expansion so the defaults can pick up the package default.
func RegisterLicensesPackageMapper(ctx RegisterMutatorsContext) {
ctx.BottomUp("licensesPackageMapper", licensesPackageMapper).Parallel()
}
// Registers the function that gathers the license dependencies for each module.
//
// This goes after defaults expansion so that it can pick up default licenses and before visibility enforcement.
func RegisterLicensesPropertyGatherer(ctx RegisterMutatorsContext) {
ctx.BottomUp("licensesPropertyGatherer", licensesPropertyGatherer).Parallel()
}
// Registers the function that verifies the licenses and license_kinds dependency types for each module.
func RegisterLicensesDependencyChecker(ctx RegisterMutatorsContext) {
ctx.BottomUp("licensesPropertyChecker", licensesDependencyChecker).Parallel()
}
// Maps each package to its default applicable licenses.
func licensesPackageMapper(ctx BottomUpMutatorContext) {
p, ok := ctx.Module().(*packageModule)
if !ok {
return
}
licenses := getLicenses(ctx, p)
dir := ctx.ModuleDir()
c := makeLicensesContainer(licenses)
moduleToPackageDefaultLicensesMap(ctx.Config()).Store(dir, c)
}
// Copies the default_applicable_licenses property values for mapping by package directory.
func makeLicensesContainer(propVals []string) licensesContainer {
licenses := make([]string, 0, len(propVals))
licenses = append(licenses, propVals...)
return licensesContainer{licenses}
}
// Gathers the applicable licenses into dependency references after defaults expansion.
func licensesPropertyGatherer(ctx BottomUpMutatorContext) {
m, ok := ctx.Module().(Module)
if !ok {
return
}
if exemptFromRequiredApplicableLicensesProperty(m) {
return
}
licenses := getLicenses(ctx, m)
ctx.AddVariationDependencies(nil, licensesTag, licenses...)
}
// Verifies the license and license_kind dependencies are each the correct kind of module.
func licensesDependencyChecker(ctx BottomUpMutatorContext) {
m, ok := ctx.Module().(Module)
if !ok {
return
}
// license modules have no licenses, but license_kinds must refer to license_kind modules
if _, ok := m.(*licenseModule); ok {
for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) {
if _, ok := module.(*licenseKindModule); !ok {
ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
}
}
return
}
if exemptFromRequiredApplicableLicensesProperty(m) {
return
}
for _, module := range ctx.GetDirectDepsWithTag(licensesTag) {
if _, ok := module.(*licenseModule); !ok {
propertyName := "licenses"
primaryProperty := m.base().primaryLicensesProperty
if primaryProperty != nil {
propertyName = primaryProperty.getName()
}
ctx.ModuleErrorf("%s property %q is not a license module", propertyName, ctx.OtherModuleName(module))
}
}
}
// Flattens license and license_kind dependencies into calculated properties.
//
// Re-validates applicable licenses properties refer only to license modules and license_kinds properties refer
// only to license_kind modules.
func licensesPropertyFlattener(ctx ModuleContext) {
m, ok := ctx.Module().(Module)
if !ok {
return
}
// license modules have no licenses, but license_kinds must refer to license_kind modules
if l, ok := m.(*licenseModule); ok {
mergeProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName())
mergeProps(&m.base().commonProperties.Effective_license_text, PathsForModuleSrc(ctx, l.properties.License_text).Strings()...)
for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) {
if lk, ok := module.(*licenseKindModule); ok {
mergeProps(&m.base().commonProperties.Effective_license_conditions, lk.properties.Conditions...)
mergeProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module))
} else {
ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
}
}
return
}
if exemptFromRequiredApplicableLicensesProperty(m) {
return
}
for _, module := range ctx.GetDirectDepsWithTag(licensesTag) {
if l, ok := module.(*licenseModule); ok {
if m.base().commonProperties.Effective_package_name == nil && l.properties.Package_name != nil {
m.base().commonProperties.Effective_package_name = l.properties.Package_name
}
mergeProps(&m.base().commonProperties.Effective_licenses, module.base().commonProperties.Effective_licenses...)
mergeProps(&m.base().commonProperties.Effective_license_text, module.base().commonProperties.Effective_license_text...)
mergeProps(&m.base().commonProperties.Effective_license_kinds, module.base().commonProperties.Effective_license_kinds...)
mergeProps(&m.base().commonProperties.Effective_license_conditions, module.base().commonProperties.Effective_license_conditions...)
} else {
propertyName := "licenses"
primaryProperty := m.base().primaryLicensesProperty
if primaryProperty != nil {
propertyName = primaryProperty.getName()
}
ctx.ModuleErrorf("%s property %q is not a license module", propertyName, ctx.OtherModuleName(module))
}
}
}
// Update a property string array with a distinct union of its values and a list of new values.
func mergeProps(prop *[]string, values ...string) {
s := make(map[string]bool)
for _, v := range *prop {
s[v] = true
}
for _, v := range values {
s[v] = true
}
*prop = []string{}
*prop = append(*prop, SortedStringKeys(s)...)
}
// Get the licenses property falling back to the package default.
func getLicenses(ctx BaseModuleContext, module Module) []string {
if exemptFromRequiredApplicableLicensesProperty(module) {
return nil
}
primaryProperty := module.base().primaryLicensesProperty
if primaryProperty == nil {
if ctx.Config().IsEnvTrue("ANDROID_REQUIRE_LICENSES") {
ctx.ModuleErrorf("module type %q must have an applicable licenses property", ctx.OtherModuleType(module))
}
return nil
}
licenses := primaryProperty.getStrings()
if len(licenses) > 0 {
s := make(map[string]bool)
for _, l := range licenses {
if _, ok := s[l]; ok {
ctx.ModuleErrorf("duplicate %q %s", l, primaryProperty.getName())
}
s[l] = true
}
return licenses
}
dir := ctx.OtherModuleDir(module)
moduleToApplicableLicenses := moduleToPackageDefaultLicensesMap(ctx.Config())
value, ok := moduleToApplicableLicenses.Load(dir)
var c licensesContainer
if ok {
c = value.(licensesContainer)
} else {
c = licensesContainer{}
}
return c.getLicenses()
}
// Returns whether a module is an allowed list of modules that do not have or need applicable licenses.
func exemptFromRequiredApplicableLicensesProperty(module Module) bool {
switch reflect.TypeOf(module).String() {
case "*android.licenseModule": // is a license, doesn't need one
case "*android.licenseKindModule": // is a license, doesn't need one
case "*android.NamespaceModule": // just partitions things, doesn't add anything
case "*android.soongConfigModuleTypeModule": // creates aliases for modules with licenses
case "*android.soongConfigModuleTypeImport": // creates aliases for modules with licenses
case "*android.soongConfigStringVariableDummyModule": // used for creating aliases
case "*android.SoongConfigBoolVariableDummyModule": // used for creating aliases
default:
return false
}
return true
}

View File

@ -385,6 +385,20 @@ type commonProperties struct {
// more details.
Visibility []string
// Describes the licenses applicable to this module. Must reference license modules.
Licenses []string
// Flattened from direct license dependencies. Equal to Licenses unless particular module adds more.
Effective_licenses []string `blueprint:"mutated"`
// Override of module name when reporting licenses
Effective_package_name *string `blueprint:"mutated"`
// Notice files
Effective_license_text []string `blueprint:"mutated"`
// License names
Effective_license_kinds []string `blueprint:"mutated"`
// License conditions
Effective_license_conditions []string `blueprint:"mutated"`
// control whether this module compiles for 32-bit, 64-bit, or both. Possible values
// are "32" (compile for 32-bit only), "64" (compile for 64-bit only), "both" (compile for both
// architectures), or "first" (compile for 64-bit on a 64-bit platform, and 32-bit on a 32-bit
@ -648,6 +662,10 @@ func InitAndroidModule(m Module) {
// The default_visibility property needs to be checked and parsed by the visibility module during
// its checking and parsing phases so make it the primary visibility property.
setPrimaryVisibilityProperty(m, "visibility", &base.commonProperties.Visibility)
// The default_applicable_licenses property needs to be checked and parsed by the licenses module during
// its checking and parsing phases so make it the primary licenses property.
setPrimaryLicensesProperty(m, "licenses", &base.commonProperties.Licenses)
}
func InitAndroidArchModule(m Module, hod HostOrDeviceSupported, defaultMultilib Multilib) {
@ -743,10 +761,13 @@ type ModuleBase struct {
// The primary visibility property, may be nil, that controls access to the module.
primaryVisibilityProperty visibilityProperty
// The primary licenses property, may be nil, records license metadata for the module.
primaryLicensesProperty applicableLicensesProperty
noAddressSanitizer bool
installFiles Paths
checkbuildFiles Paths
noticeFile OptionalPath
noticeFiles Paths
phonies map[string]Paths
// Used by buildTargetSingleton to create checkbuild and per-directory build targets
@ -1349,6 +1370,11 @@ func (m *ModuleBase) GenerateBuildActions(blueprintCtx blueprint.ModuleContext)
m.noticeFile = ExistentPathForSource(ctx, noticePath)
}
licensesPropertyFlattener(ctx)
if ctx.Failed() {
return
}
m.module.GenerateAndroidBuildActions(ctx)
if ctx.Failed() {
return

View File

@ -109,6 +109,11 @@ var preArch = []RegisterMutatorFunc{
//
RegisterVisibilityRuleChecker,
// Record the default_applicable_licenses for each package.
//
// This must run before the defaults so that defaults modules can pick up the package default.
RegisterLicensesPackageMapper,
// Apply properties from defaults modules to the referencing modules.
//
// Any mutators that are added before this will not see any modules created by
@ -123,6 +128,12 @@ var preArch = []RegisterMutatorFunc{
// prebuilt.
RegisterPrebuiltsPreArchMutators,
// Gather the licenses properties for all modules for use during expansion and enforcement.
//
// This must come after the defaults mutators to ensure that any licenses supplied
// in a defaults module has been successfully applied before the rules are gathered.
RegisterLicensesPropertyGatherer,
// Gather the visibility rules for all modules for us during visibility enforcement.
//
// This must come after the defaults mutators to ensure that any visibility supplied
@ -144,6 +155,7 @@ var postDeps = []RegisterMutatorFunc{
registerPathDepsMutator,
RegisterPrebuiltsPostDepsMutators,
RegisterVisibilityRuleEnforcer,
RegisterLicensesDependencyChecker,
RegisterNeverallowMutator,
RegisterOverridePostDepsMutators,
}

View File

@ -31,6 +31,8 @@ func RegisterPackageBuildComponents(ctx RegistrationContext) {
type packageProperties struct {
// Specifies the default visibility for all modules defined in this package.
Default_visibility []string
// Specifies the default license terms for all modules defined in this package.
Default_applicable_licenses []string
}
type packageModule struct {
@ -68,5 +70,9 @@ func PackageFactory() Module {
// its checking and parsing phases so make it the primary visibility property.
setPrimaryVisibilityProperty(module, "default_visibility", &module.properties.Default_visibility)
// The default_applicable_licenses property needs to be checked and parsed by the licenses module during
// its checking and parsing phases so make it the primary licenses property.
setPrimaryLicensesProperty(module, "default_applicable_licenses", &module.properties.Default_applicable_licenses)
return module
}

View File

@ -17,9 +17,11 @@ var packageTests = []struct {
package {
name: "package",
visibility: ["//visibility:private"],
licenses: ["license"],
}`),
},
expectedErrors: []string{
`top/Blueprints:5:14: unrecognized property "licenses"`,
`top/Blueprints:3:10: unrecognized property "name"`,
`top/Blueprints:4:16: unrecognized property "visibility"`,
},
@ -44,9 +46,10 @@ var packageTests = []struct {
"top/Blueprints": []byte(`
package {
default_visibility: ["//visibility:private"],
default_applicable_licenses: ["license"],
}
package {
package {
}`),
},
expectedErrors: []string{