From 9d2f1743d27d5873642b75fd9795fb9b5fc0dcb3 Mon Sep 17 00:00:00 2001 From: Sasha Smundak Date: Thu, 4 Aug 2022 13:28:38 -0700 Subject: [PATCH] Implement bp2build for the `license` module Also, add ExpectedRuleTarget type to simplify bp2build testing Bug: 190817312 Test: treehugger Change-Id: Id3a642680d3518d36360ba957919a6fc96222672 --- android/allowlists/allowlists.go | 1 + android/license.go | 45 +++++++++++++++- bp2build/Android.bp | 1 + bp2build/license_conversion_test.go | 81 +++++++++++++++++++++++++++++ bp2build/testing.go | 13 ++++- 5 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 bp2build/license_conversion_test.go diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go index d424df05b..ab028e5b0 100644 --- a/android/allowlists/allowlists.go +++ b/android/allowlists/allowlists.go @@ -304,6 +304,7 @@ var ( // external/bazelbuild-rules_android/... is needed by mixed builds, otherwise mixed builds analysis fails // e.g. ERROR: Analysis of target '@soong_injection//mixed_builds:buildroot' failed "external/bazelbuild-rules_android":/* recursive = */ true, + "external/bazelbuild-rules_license":/* recursive = */ true, "external/bazelbuild-kotlin-rules":/* recursive = */ true, "external/bazel-skylib":/* recursive = */ true, "external/guava":/* recursive = */ true, diff --git a/android/license.go b/android/license.go index ebee05576..c36f48870 100644 --- a/android/license.go +++ b/android/license.go @@ -15,7 +15,10 @@ package android import ( + "android/soong/bazel" + "fmt" "github.com/google/blueprint" + "os" ) type licenseKindDependencyTag struct { @@ -48,14 +51,53 @@ type licenseProperties struct { Visibility []string } +var _ Bazelable = &licenseModule{} + type licenseModule struct { ModuleBase DefaultableModuleBase SdkBase + BazelModuleBase properties licenseProperties } +type bazelLicenseAttributes struct { + License_kinds []string + Copyright_notice *string + License_text bazel.LabelAttribute + Package_name *string + Visibility []string +} + +func (m *licenseModule) ConvertWithBp2build(ctx TopDownMutatorContext) { + attrs := &bazelLicenseAttributes{ + License_kinds: m.properties.License_kinds, + Copyright_notice: m.properties.Copyright_notice, + Package_name: m.properties.Package_name, + Visibility: m.properties.Visibility, + } + + // TODO(asmundak): Soong supports multiple license texts while Bazel does not. + if len(m.properties.License_text) > 1 { + fmt.Fprintf(os.Stderr, "%s:%s: warning: using only the first license_text item\n", + ctx.ModuleDir(), m.Name()) + } + if len(m.properties.License_text) >= 1 { + attrs.License_text.SetValue(BazelLabelForModuleSrcSingle(ctx, m.properties.License_text[0])) + } + + ctx.CreateBazelTargetModule( + bazel.BazelTargetModuleProperties{ + Rule_class: "android_license", + Bzl_load_location: "//build/bazel/rules/license:license.bzl", + }, + CommonAttributes{ + Name: m.Name(), + }, + attrs) +} + func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) { ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...) } @@ -78,7 +120,7 @@ func LicenseFactory() Module { module := &licenseModule{} base := module.base() - module.AddProperties(&base.nameProperties, &module.properties) + module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus) // The visibility property needs to be checked and parsed by the visibility module. setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility) @@ -86,6 +128,7 @@ func LicenseFactory() Module { InitSdkAwareModule(module) initAndroidModuleBase(module) InitDefaultableModule(module) + InitBazelModule(module) return module } diff --git a/bp2build/Android.bp b/bp2build/Android.bp index 3d9fc5a2b..8aef66f59 100644 --- a/bp2build/Android.bp +++ b/bp2build/Android.bp @@ -63,6 +63,7 @@ bootstrap_go_package { "java_library_host_conversion_test.go", "java_plugin_conversion_test.go", "java_proto_conversion_test.go", + "license_conversion_test.go", "linker_config_conversion_test.go", "ndk_headers_conversion_test.go", "performance_test.go", diff --git a/bp2build/license_conversion_test.go b/bp2build/license_conversion_test.go new file mode 100644 index 000000000..ea6b27a17 --- /dev/null +++ b/bp2build/license_conversion_test.go @@ -0,0 +1,81 @@ +// Copyright 2022 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 bp2build + +import ( + "android/soong/android" + "testing" +) + +func registerLicenseModuleTypes(_ android.RegistrationContext) {} + +func TestLicenseBp2Build(t *testing.T) { + tests := []struct { + description string + module string + expected ExpectedRuleTarget + }{ + { + description: "license kind and text notice", + module: ` +license { + name: "my_license", + license_kinds: [ "SPDX-license-identifier-Apache-2.0"], + license_text: [ "NOTICE"], +}`, + expected: ExpectedRuleTarget{ + "android_license", + "my_license", + AttrNameToString{ + "license_kinds": `["SPDX-license-identifier-Apache-2.0"]`, + "license_text": `"NOTICE"`, + }, + android.HostAndDeviceDefault, + }, + }, + { + description: "visibility, package_name, copyright_notice", + module: ` +license { + name: "my_license", + package_name: "my_package", + visibility: [":__subpackages__"], + copyright_notice: "Copyright © 2022", +}`, + expected: ExpectedRuleTarget{ + "android_license", + "my_license", + AttrNameToString{ + "copyright_notice": `"Copyright © 2022"`, + "package_name": `"my_package"`, + "visibility": `[":__subpackages__"]`, + }, + android.HostAndDeviceDefault, + }, + }, + } + + for _, test := range tests { + RunBp2BuildTestCase(t, + registerLicenseModuleTypes, + Bp2buildTestCase{ + Description: test.description, + ModuleTypeUnderTest: "license", + ModuleTypeUnderTestFactory: android.LicenseFactory, + Blueprint: test.module, + ExpectedBazelTargets: []string{test.expected.String()}, + }) + } +} diff --git a/bp2build/testing.go b/bp2build/testing.go index ee9200af8..edc5c4a99 100644 --- a/bp2build/testing.go +++ b/bp2build/testing.go @@ -141,7 +141,7 @@ func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.Regi android.FailIfErrored(t, errs) } if actualCount, expectedCount := len(bazelTargets), len(tc.ExpectedBazelTargets); actualCount != expectedCount { - t.Errorf("%s: Expected %d bazel target (%s), got `%d`` (%s)", + t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)", tc.Description, expectedCount, tc.ExpectedBazelTargets, actualCount, bazelTargets) } else { for i, target := range bazelTargets { @@ -452,3 +452,14 @@ func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) str func MakeBazelTarget(typ, name string, attrs AttrNameToString) string { return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported) } + +type ExpectedRuleTarget struct { + Rule string + Name string + Attrs AttrNameToString + Hod android.HostOrDeviceSupported +} + +func (ebr ExpectedRuleTarget) String() string { + return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod) +}