blob: cde5e6e6e5fc14d1c95bdb4135536dcd4f5ac791 [file] [log] [blame]
Bob Badour37af0462021-01-07 03:34:31 +00001// 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
15package android
16
17import (
Sasha Smundak9d2f1742022-08-04 13:28:38 -070018 "android/soong/bazel"
19 "fmt"
Bob Badour37af0462021-01-07 03:34:31 +000020 "github.com/google/blueprint"
Sasha Smundak9d2f1742022-08-04 13:28:38 -070021 "os"
Bob Badour37af0462021-01-07 03:34:31 +000022)
23
24type licenseKindDependencyTag struct {
Paul Duffin3c298a32021-03-04 17:44:03 +000025 blueprint.BaseDependencyTag
Bob Badour37af0462021-01-07 03:34:31 +000026}
27
28var (
29 licenseKindTag = licenseKindDependencyTag{}
30)
31
32func init() {
33 RegisterLicenseBuildComponents(InitRegistrationContext)
34}
35
36// Register the license module type.
37func RegisterLicenseBuildComponents(ctx RegistrationContext) {
38 ctx.RegisterModuleType("license", LicenseFactory)
39}
40
41type licenseProperties struct {
42 // Specifies the kinds of license that apply.
43 License_kinds []string
44 // Specifies a short copyright notice to use for the license.
45 Copyright_notice *string
46 // Specifies the path or label for the text of the license.
47 License_text []string `android:"path"`
48 // Specifies the package name to which the license applies.
49 Package_name *string
50 // Specifies where this license can be used
51 Visibility []string
52}
53
Sasha Smundak9d2f1742022-08-04 13:28:38 -070054var _ Bazelable = &licenseModule{}
55
Bob Badour37af0462021-01-07 03:34:31 +000056type licenseModule struct {
57 ModuleBase
58 DefaultableModuleBase
Paul Duffinb9e7a3c2021-05-06 15:53:19 +010059 SdkBase
Sasha Smundak9d2f1742022-08-04 13:28:38 -070060 BazelModuleBase
Bob Badour37af0462021-01-07 03:34:31 +000061
62 properties licenseProperties
63}
64
Sasha Smundak9d2f1742022-08-04 13:28:38 -070065type bazelLicenseAttributes struct {
66 License_kinds []string
67 Copyright_notice *string
68 License_text bazel.LabelAttribute
69 Package_name *string
70 Visibility []string
71}
72
73func (m *licenseModule) ConvertWithBp2build(ctx TopDownMutatorContext) {
74 attrs := &bazelLicenseAttributes{
75 License_kinds: m.properties.License_kinds,
76 Copyright_notice: m.properties.Copyright_notice,
77 Package_name: m.properties.Package_name,
78 Visibility: m.properties.Visibility,
79 }
80
Sasha Smundak8bea2672022-08-04 13:31:14 -070081 // TODO(asmundak): Soong supports multiple license texts while Bazel's license
82 // rule does not. Have android_license create a genrule to concatenate multiple
83 // license texts.
84 if len(m.properties.License_text) > 1 && ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") {
85 fmt.Fprintf(os.Stderr, "warning: using only the first license_text item from //%s:%s\n",
Sasha Smundak9d2f1742022-08-04 13:28:38 -070086 ctx.ModuleDir(), m.Name())
87 }
88 if len(m.properties.License_text) >= 1 {
89 attrs.License_text.SetValue(BazelLabelForModuleSrcSingle(ctx, m.properties.License_text[0]))
90 }
91
92 ctx.CreateBazelTargetModule(
93 bazel.BazelTargetModuleProperties{
94 Rule_class: "android_license",
95 Bzl_load_location: "//build/bazel/rules/license:license.bzl",
96 },
97 CommonAttributes{
98 Name: m.Name(),
99 },
100 attrs)
101}
102
Bob Badour37af0462021-01-07 03:34:31 +0000103func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
104 ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
105}
106
107func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffindf5a9052021-05-06 21:52:31 +0100108 // license modules have no licenses, but license_kinds must refer to license_kind modules
Paul Duffinec0836a2021-05-10 22:53:30 +0100109 mergeStringProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName())
Bob Badour4101c712022-02-09 11:54:35 -0800110 namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...)
Paul Duffindf5a9052021-05-06 21:52:31 +0100111 for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) {
112 if lk, ok := module.(*licenseKindModule); ok {
Paul Duffinec0836a2021-05-10 22:53:30 +0100113 mergeStringProps(&m.base().commonProperties.Effective_license_conditions, lk.properties.Conditions...)
114 mergeStringProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module))
Paul Duffindf5a9052021-05-06 21:52:31 +0100115 } else {
116 ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
117 }
118 }
Bob Badour37af0462021-01-07 03:34:31 +0000119}
120
121func LicenseFactory() Module {
122 module := &licenseModule{}
123
124 base := module.base()
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700125 module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus)
Bob Badour37af0462021-01-07 03:34:31 +0000126
Bob Badour37af0462021-01-07 03:34:31 +0000127 // The visibility property needs to be checked and parsed by the visibility module.
128 setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
129
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100130 InitSdkAwareModule(module)
Bob Badour37af0462021-01-07 03:34:31 +0000131 initAndroidModuleBase(module)
132 InitDefaultableModule(module)
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700133 InitBazelModule(module)
Bob Badour37af0462021-01-07 03:34:31 +0000134
135 return module
136}