license.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. BazelModuleBase
  51. properties licenseProperties
  52. }
  53. type bazelLicenseAttributes struct {
  54. License_kinds []string
  55. Copyright_notice *string
  56. License_text bazel.LabelAttribute
  57. Package_name *string
  58. Visibility []string
  59. }
  60. func (m *licenseModule) ConvertWithBp2build(ctx TopDownMutatorContext) {
  61. attrs := &bazelLicenseAttributes{
  62. License_kinds: m.properties.License_kinds,
  63. Copyright_notice: m.properties.Copyright_notice,
  64. Package_name: m.properties.Package_name,
  65. Visibility: m.properties.Visibility,
  66. }
  67. // TODO(asmundak): Soong supports multiple license texts while Bazel's license
  68. // rule does not. Have android_license create a genrule to concatenate multiple
  69. // license texts.
  70. if len(m.properties.License_text) > 1 && ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") {
  71. fmt.Fprintf(os.Stderr, "warning: using only the first license_text item from //%s:%s\n",
  72. ctx.ModuleDir(), m.Name())
  73. }
  74. if len(m.properties.License_text) >= 1 {
  75. attrs.License_text.SetValue(BazelLabelForModuleSrcSingle(ctx, m.properties.License_text[0]))
  76. }
  77. ctx.CreateBazelTargetModule(
  78. bazel.BazelTargetModuleProperties{
  79. Rule_class: "android_license",
  80. Bzl_load_location: "//build/bazel/rules/license:license.bzl",
  81. },
  82. CommonAttributes{
  83. Name: m.Name(),
  84. },
  85. attrs)
  86. }
  87. func (m *licenseModule) DepsMutator(ctx BottomUpMutatorContext) {
  88. for i, license := range m.properties.License_kinds {
  89. for j := i + 1; j < len(m.properties.License_kinds); j++ {
  90. if license == m.properties.License_kinds[j] {
  91. ctx.ModuleErrorf("Duplicated license kind: %q", license)
  92. break
  93. }
  94. }
  95. }
  96. ctx.AddVariationDependencies(nil, licenseKindTag, m.properties.License_kinds...)
  97. }
  98. func (m *licenseModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  99. // license modules have no licenses, but license_kinds must refer to license_kind modules
  100. mergeStringProps(&m.base().commonProperties.Effective_licenses, ctx.ModuleName())
  101. namePathProps(&m.base().commonProperties.Effective_license_text, m.properties.Package_name, PathsForModuleSrc(ctx, m.properties.License_text)...)
  102. for _, module := range ctx.GetDirectDepsWithTag(licenseKindTag) {
  103. if lk, ok := module.(*licenseKindModule); ok {
  104. mergeStringProps(&m.base().commonProperties.Effective_license_conditions, lk.properties.Conditions...)
  105. mergeStringProps(&m.base().commonProperties.Effective_license_kinds, ctx.OtherModuleName(module))
  106. } else {
  107. ctx.ModuleErrorf("license_kinds property %q is not a license_kind module", ctx.OtherModuleName(module))
  108. }
  109. }
  110. }
  111. func LicenseFactory() Module {
  112. module := &licenseModule{}
  113. base := module.base()
  114. module.AddProperties(&base.nameProperties, &module.properties, &base.commonProperties.BazelConversionStatus)
  115. // The visibility property needs to be checked and parsed by the visibility module.
  116. setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
  117. initAndroidModuleBase(module)
  118. InitDefaultableModule(module)
  119. InitBazelModule(module)
  120. return module
  121. }