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 ( |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 18 | "fmt" |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 19 | "os" |
Cole Faust | 2ced8c8 | 2022-11-22 16:51:21 -0800 | [diff] [blame] | 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | |
| 23 | "android/soong/bazel" |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type licenseKindDependencyTag struct { |
Paul Duffin | 3c298a3 | 2021-03-04 17:44:03 +0000 | [diff] [blame] | 27 | blueprint.BaseDependencyTag |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | var ( |
| 31 | licenseKindTag = licenseKindDependencyTag{} |
| 32 | ) |
| 33 | |
| 34 | func init() { |
| 35 | RegisterLicenseBuildComponents(InitRegistrationContext) |
| 36 | } |
| 37 | |
| 38 | // Register the license module type. |
| 39 | func RegisterLicenseBuildComponents(ctx RegistrationContext) { |
| 40 | ctx.RegisterModuleType("license", LicenseFactory) |
| 41 | } |
| 42 | |
| 43 | type licenseProperties struct { |
| 44 | // Specifies the kinds of license that apply. |
| 45 | License_kinds []string |
| 46 | // Specifies a short copyright notice to use for the license. |
| 47 | Copyright_notice *string |
| 48 | // Specifies the path or label for the text of the license. |
| 49 | License_text []string `android:"path"` |
| 50 | // Specifies the package name to which the license applies. |
| 51 | Package_name *string |
| 52 | // Specifies where this license can be used |
| 53 | Visibility []string |
| 54 | } |
| 55 | |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 56 | var _ Bazelable = &licenseModule{} |
| 57 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 58 | type licenseModule struct { |
| 59 | ModuleBase |
| 60 | DefaultableModuleBase |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 61 | BazelModuleBase |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 62 | |
| 63 | properties licenseProperties |
| 64 | } |
| 65 | |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 66 | type bazelLicenseAttributes struct { |
| 67 | License_kinds []string |
| 68 | Copyright_notice *string |
| 69 | License_text bazel.LabelAttribute |
| 70 | Package_name *string |
| 71 | Visibility []string |
| 72 | } |
| 73 | |
| 74 | func (m *licenseModule) ConvertWithBp2build(ctx TopDownMutatorContext) { |
| 75 | attrs := &bazelLicenseAttributes{ |
| 76 | License_kinds: m.properties.License_kinds, |
| 77 | Copyright_notice: m.properties.Copyright_notice, |
| 78 | Package_name: m.properties.Package_name, |
| 79 | Visibility: m.properties.Visibility, |
| 80 | } |
| 81 | |
Sasha Smundak | 8bea267 | 2022-08-04 13:31:14 -0700 | [diff] [blame] | 82 | // TODO(asmundak): Soong supports multiple license texts while Bazel's license |
| 83 | // rule does not. Have android_license create a genrule to concatenate multiple |
| 84 | // license texts. |
| 85 | if len(m.properties.License_text) > 1 && ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") { |
| 86 | fmt.Fprintf(os.Stderr, "warning: using only the first license_text item from //%s:%s\n", |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 87 | ctx.ModuleDir(), m.Name()) |
| 88 | } |
| 89 | if len(m.properties.License_text) >= 1 { |
| 90 | attrs.License_text.SetValue(BazelLabelForModuleSrcSingle(ctx, m.properties.License_text[0])) |
| 91 | } |
| 92 | |
| 93 | ctx.CreateBazelTargetModule( |
| 94 | bazel.BazelTargetModuleProperties{ |
| 95 | Rule_class: "android_license", |
| 96 | Bzl_load_location: "//build/bazel/rules/license:license.bzl", |
| 97 | }, |
| 98 | CommonAttributes{ |
| 99 | Name: m.Name(), |
| 100 | }, |
| 101 | attrs) |
| 102 | } |
| 103 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 104 | func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) { |
Cole Faust | 2ced8c8 | 2022-11-22 16:51:21 -0800 | [diff] [blame] | 105 | for i, license := range m.properties.License_kinds { |
| 106 | for j := i + 1; j < len(m.properties.License_kinds); j++ { |
| 107 | if license == m.properties.License_kinds[j] { |
| 108 | ctx.ModuleErrorf("Duplicated license kind: %q", license) |
| 109 | break |
| 110 | } |
| 111 | } |
| 112 | } |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 113 | ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...) |
| 114 | } |
| 115 | |
| 116 | func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Paul Duffin | df5a905 | 2021-05-06 21:52:31 +0100 | [diff] [blame] | 117 | // 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] | 118 | mergeStringProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName()) |
Bob Badour | 4101c71 | 2022-02-09 11:54:35 -0800 | [diff] [blame] | 119 | namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...) |
Paul Duffin | df5a905 | 2021-05-06 21:52:31 +0100 | [diff] [blame] | 120 | for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) { |
| 121 | if lk, ok := module.(*licenseKindModule); ok { |
Paul Duffin | ec0836a | 2021-05-10 22:53:30 +0100 | [diff] [blame] | 122 | mergeStringProps(&m.base().commonProperties.Effective_license_conditions, lk.properties.Conditions...) |
| 123 | mergeStringProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module)) |
Paul Duffin | df5a905 | 2021-05-06 21:52:31 +0100 | [diff] [blame] | 124 | } else { |
| 125 | ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module)) |
| 126 | } |
| 127 | } |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | func LicenseFactory() Module { |
| 131 | module := &licenseModule{} |
| 132 | |
| 133 | base := module.base() |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 134 | module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 135 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 136 | // The visibility property needs to be checked and parsed by the visibility module. |
| 137 | setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility) |
| 138 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 139 | initAndroidModuleBase(module) |
| 140 | InitDefaultableModule(module) |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 141 | InitBazelModule(module) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 142 | |
| 143 | return module |
| 144 | } |