license.go 4.5 KB

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