blob: ab8431a89d86481c8175ca4652b5b2b8fcd217aa [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 "fmt"
Sasha Smundak9d2f1742022-08-04 13:28:38 -070019 "os"
Cole Faust2ced8c82022-11-22 16:51:21 -080020
21 "github.com/google/blueprint"
22
23 "android/soong/bazel"
Bob Badour37af0462021-01-07 03:34:31 +000024)
25
26type licenseKindDependencyTag struct {
Paul Duffin3c298a32021-03-04 17:44:03 +000027 blueprint.BaseDependencyTag
Bob Badour37af0462021-01-07 03:34:31 +000028}
29
30var (
31 licenseKindTag = licenseKindDependencyTag{}
32)
33
34func init() {
35 RegisterLicenseBuildComponents(InitRegistrationContext)
36}
37
38// Register the license module type.
39func RegisterLicenseBuildComponents(ctx RegistrationContext) {
40 ctx.RegisterModuleType("license", LicenseFactory)
41}
42
43type 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 Smundak9d2f1742022-08-04 13:28:38 -070056var _ Bazelable = &licenseModule{}
57
Bob Badour37af0462021-01-07 03:34:31 +000058type licenseModule struct {
59 ModuleBase
60 DefaultableModuleBase
Paul Duffinb9e7a3c2021-05-06 15:53:19 +010061 SdkBase
Sasha Smundak9d2f1742022-08-04 13:28:38 -070062 BazelModuleBase
Bob Badour37af0462021-01-07 03:34:31 +000063
64 properties licenseProperties
65}
66
Sasha Smundak9d2f1742022-08-04 13:28:38 -070067type bazelLicenseAttributes struct {
68 License_kinds []string
69 Copyright_notice *string
70 License_text bazel.LabelAttribute
71 Package_name *string
72 Visibility []string
73}
74
75func (m *licenseModule) ConvertWithBp2build(ctx TopDownMutatorContext) {
76 attrs := &bazelLicenseAttributes{
77 License_kinds: m.properties.License_kinds,
78 Copyright_notice: m.properties.Copyright_notice,
79 Package_name: m.properties.Package_name,
80 Visibility: m.properties.Visibility,
81 }
82
Sasha Smundak8bea2672022-08-04 13:31:14 -070083 // TODO(asmundak): Soong supports multiple license texts while Bazel's license
84 // rule does not. Have android_license create a genrule to concatenate multiple
85 // license texts.
86 if len(m.properties.License_text) > 1 && ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") {
87 fmt.Fprintf(os.Stderr, "warning: using only the first license_text item from //%s:%s\n",
Sasha Smundak9d2f1742022-08-04 13:28:38 -070088 ctx.ModuleDir(), m.Name())
89 }
90 if len(m.properties.License_text) >= 1 {
91 attrs.License_text.SetValue(BazelLabelForModuleSrcSingle(ctx, m.properties.License_text[0]))
92 }
93
94 ctx.CreateBazelTargetModule(
95 bazel.BazelTargetModuleProperties{
96 Rule_class: "android_license",
97 Bzl_load_location: "//build/bazel/rules/license:license.bzl",
98 },
99 CommonAttributes{
100 Name: m.Name(),
101 },
102 attrs)
103}
104
Bob Badour37af0462021-01-07 03:34:31 +0000105func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
Cole Faust2ced8c82022-11-22 16:51:21 -0800106 for i, license := range m.properties.License_kinds {
107 for j := i + 1; j < len(m.properties.License_kinds); j++ {
108 if license == m.properties.License_kinds[j] {
109 ctx.ModuleErrorf("Duplicated license kind: %q", license)
110 break
111 }
112 }
113 }
Bob Badour37af0462021-01-07 03:34:31 +0000114 ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
115}
116
117func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Paul Duffindf5a9052021-05-06 21:52:31 +0100118 // license modules have no licenses, but license_kinds must refer to license_kind modules
Paul Duffinec0836a2021-05-10 22:53:30 +0100119 mergeStringProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName())
Bob Badour4101c712022-02-09 11:54:35 -0800120 namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...)
Paul Duffindf5a9052021-05-06 21:52:31 +0100121 for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) {
122 if lk, ok := module.(*licenseKindModule); ok {
Paul Duffinec0836a2021-05-10 22:53:30 +0100123 mergeStringProps(&m.base().commonProperties.Effective_license_conditions, lk.properties.Conditions...)
124 mergeStringProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module))
Paul Duffindf5a9052021-05-06 21:52:31 +0100125 } else {
126 ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
127 }
128 }
Bob Badour37af0462021-01-07 03:34:31 +0000129}
130
131func LicenseFactory() Module {
132 module := &licenseModule{}
133
134 base := module.base()
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700135 module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus)
Bob Badour37af0462021-01-07 03:34:31 +0000136
Bob Badour37af0462021-01-07 03:34:31 +0000137 // The visibility property needs to be checked and parsed by the visibility module.
138 setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
139
Paul Duffinb9e7a3c2021-05-06 15:53:19 +0100140 InitSdkAwareModule(module)
Bob Badour37af0462021-01-07 03:34:31 +0000141 initAndroidModuleBase(module)
142 InitDefaultableModule(module)
Sasha Smundak9d2f1742022-08-04 13:28:38 -0700143 InitBazelModule(module)
Bob Badour37af0462021-01-07 03:34:31 +0000144
145 return module
146}