diff options
author | Yimin Li <ymli@google.com> | 2021-03-23 14:46:16 -0700 |
---|---|---|
committer | Yimin Li <ymli@google.com> | 2021-03-23 14:46:16 -0700 |
commit | e8fc502ca0efb3549f549aa154740fbcf548426d (patch) | |
tree | 39397751df01453715e196b9ee7bb3e359694274 | |
parent | fc7080feb55703ed75dc10261244d734b55844c0 (diff) | |
parent | ac442d1758d6230285fef3e269802e4097068e11 (diff) | |
download | soong-e8fc502ca0efb3549f549aa154740fbcf548426d.tar.gz |
Merge commit 'ac442d1758d6230285fef3e269802e4097068e11' into HEAD
Change-Id: Ic95739af73b5257be0f77234a36fe90b89e73ec9
-rw-r--r-- | android/Android.bp | 2 | ||||
-rw-r--r-- | android/license.go | 70 | ||||
-rw-r--r-- | android/license_test.go | 142 | ||||
-rw-r--r-- | android/module.go | 3 | ||||
-rw-r--r-- | android/package.go | 2 | ||||
-rw-r--r-- | android/package_test.go | 1 |
6 files changed, 220 insertions, 0 deletions
diff --git a/android/Android.bp b/android/Android.bp index 9712c46e3..bd72b7b14 100644 --- a/android/Android.bp +++ b/android/Android.bp @@ -23,6 +23,7 @@ bootstrap_go_package { "filegroup.go", "hooks.go", "image.go", + "license.go", "makevars.go", "module.go", "mutator.go", @@ -62,6 +63,7 @@ bootstrap_go_package { "csuite_config_test.go", "depset_test.go", "expand_test.go", + "license_test.go", "module_test.go", "mutator_test.go", "namespace_test.go", diff --git a/android/license.go b/android/license.go new file mode 100644 index 000000000..00ddacd41 --- /dev/null +++ b/android/license.go @@ -0,0 +1,70 @@ +// 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() { + 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) { + // Do nothing. +} + +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 +} diff --git a/android/license_test.go b/android/license_test.go new file mode 100644 index 000000000..e80ba5e94 --- /dev/null +++ b/android/license_test.go @@ -0,0 +1,142 @@ +package android + +import ( + "testing" +) + +var licenseTests = []struct { + name string + fs map[string][]byte + expectedErrors []string +}{ + { + name: "license must not accept licenses property", + fs: map[string][]byte{ + "top/Blueprints": []byte(` + license { + name: "top_license", + visibility: ["//visibility:private"], + licenses: ["other_license"], + + }`), + }, + expectedErrors: []string{ + `top/Blueprints:5:14: unrecognized property "licenses"`, + }, + }, + { + name: "public license", + fs: map[string][]byte{ + "top/Blueprints": []byte(` + license { + name: "top_proprietary", + license_kinds: ["top_by_exception_only"], + visibility: ["//visibility:public"], + }`), + "other/Blueprints": []byte(` + rule { + name: "arule", + licenses: ["top_proprietary"], + + }`), + "yetmore/Blueprints": []byte(` + package { + default_applicable_licenses: ["top_proprietary"], + }`), + }, + }, + { + name: "multiple licenses", + fs: map[string][]byte{ + "top/Blueprints": []byte(` + package { + default_applicable_licenses: ["top_proprietary"], + } + license { + name: "top_allowed_as_notice", + license_kinds: ["top_notice"], + } + license { + name: "top_proprietary", + license_kinds: ["top_by_exception_only"], + visibility: ["//visibility:public"], + } + rule { + name: "myrule", + licenses: ["top_allowed_as_notice", "top_proprietary"] + }`), + "other/Blueprints": []byte(` + rule { + name: "arule", + licenses: ["top_proprietary"], + + }`), + "yetmore/Blueprints": []byte(` + package { + default_applicable_licenses: ["top_proprietary"], + }`), + }, + }, +} + +func TestLicense(t *testing.T) { + for _, test := range licenseTests { + t.Run(test.name, func(t *testing.T) { + _, errs := testLicense(test.fs) + expectedErrors := test.expectedErrors + if expectedErrors == nil { + FailIfErrored(t, errs) + } else { + for _, expectedError := range expectedErrors { + FailIfNoMatchingErrors(t, expectedError, errs) + } + if len(errs) > len(expectedErrors) { + t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs)) + for i, expectedError := range expectedErrors { + t.Errorf("expectedErrors[%d] = %s", i, expectedError) + } + for i, err := range errs { + t.Errorf("errs[%d] = %s", i, err) + } + } + } + }) + } +} +func testLicense(fs map[string][]byte) (*TestContext, []error) { + // Create a new config per test as visibility information is stored in the config. + env := make(map[string]string) + env["ANDROID_REQUIRE_LICENSES"] = "1" + config := TestArchConfig(buildDir, env, "", fs) + ctx := NewTestArchContext() + RegisterPackageBuildComponents(ctx) + registerTestPrebuiltBuildComponents(ctx) + RegisterLicenseBuildComponents(ctx) + ctx.RegisterModuleType("rule", newMockRuleModule) + ctx.PreArchMutators(RegisterVisibilityRuleChecker) + ctx.PreArchMutators(RegisterDefaultsPreArchMutators) + ctx.PreArchMutators(RegisterVisibilityRuleGatherer) + ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer) + ctx.Register(config) + _, errs := ctx.ParseBlueprintsFiles(".") + if len(errs) > 0 { + return ctx, errs + } + _, errs = ctx.PrepareBuildActions(config) + return ctx, errs +} + +type mockRuleModule struct { + ModuleBase + DefaultableModuleBase +} + +func newMockRuleModule() Module { + m := &mockRuleModule{} + InitAndroidModule(m) + InitDefaultableModule(m) + return m +} + +func (p *mockRuleModule) GenerateAndroidBuildActions(ModuleContext) { +} diff --git a/android/module.go b/android/module.go index cd4baabb5..a498839ce 100644 --- a/android/module.go +++ b/android/module.go @@ -360,6 +360,9 @@ type commonProperties struct { // more details. Visibility []string + // Names of the licenses that apply to this module. + Licenses []string + // 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 diff --git a/android/package.go b/android/package.go index 182b3ed6e..053fec286 100644 --- a/android/package.go +++ b/android/package.go @@ -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 names of the default licenses for all modules defined in this package. + Default_applicable_licenses []string } type packageModule struct { diff --git a/android/package_test.go b/android/package_test.go index 04dfc08a9..f25599cf8 100644 --- a/android/package_test.go +++ b/android/package_test.go @@ -44,6 +44,7 @@ var packageTests = []struct { "top/Blueprints": []byte(` package { default_visibility: ["//visibility:private"], + default_applicable_licenses: ["license"], } package { |