package.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2019 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. "path/filepath"
  17. "android/soong/bazel"
  18. "github.com/google/blueprint"
  19. "github.com/google/blueprint/proptools"
  20. )
  21. func init() {
  22. RegisterPackageBuildComponents(InitRegistrationContext)
  23. }
  24. var PrepareForTestWithPackageModule = FixtureRegisterWithContext(RegisterPackageBuildComponents)
  25. // Register the package module type.
  26. func RegisterPackageBuildComponents(ctx RegistrationContext) {
  27. ctx.RegisterModuleType("package", PackageFactory)
  28. }
  29. type packageProperties struct {
  30. // Specifies the default visibility for all modules defined in this package.
  31. Default_visibility []string
  32. // Specifies the default license terms for all modules defined in this package.
  33. Default_applicable_licenses []string
  34. }
  35. type bazelPackageAttributes struct {
  36. Default_visibility []string
  37. Default_package_metadata bazel.LabelListAttribute
  38. }
  39. type packageModule struct {
  40. ModuleBase
  41. BazelModuleBase
  42. properties packageProperties
  43. }
  44. var _ Bazelable = &packageModule{}
  45. func (p *packageModule) ConvertWithBp2build(ctx TopDownMutatorContext) {
  46. defaultPackageMetadata := bazel.MakeLabelListAttribute(BazelLabelForModuleDeps(ctx, p.properties.Default_applicable_licenses))
  47. // If METADATA file exists in the package, add it to package(default_package_metadata=) using a
  48. // filegroup(name="default_metadata_file") which can be accessed later on each module in Bazel
  49. // using attribute "applicable_licenses".
  50. // Attribute applicable_licenses of filegroup "default_metadata_file" has to be set to [],
  51. // otherwise Bazel reports cyclic reference error.
  52. if existed, _, _ := ctx.Config().fs.Exists(filepath.Join(ctx.ModuleDir(), "METADATA")); existed {
  53. ctx.CreateBazelTargetModule(
  54. bazel.BazelTargetModuleProperties{
  55. Rule_class: "filegroup",
  56. },
  57. CommonAttributes{Name: "default_metadata_file"},
  58. &bazelFilegroupAttributes{
  59. Srcs: bazel.MakeLabelListAttribute(BazelLabelForModuleSrc(ctx, []string{"METADATA"})),
  60. Applicable_licenses: bazel.LabelListAttribute{Value: bazel.LabelList{Includes: []bazel.Label{}}, EmitEmptyList: true},
  61. })
  62. defaultPackageMetadata.Value.Add(&bazel.Label{Label: ":default_metadata_file"})
  63. }
  64. ctx.CreateBazelTargetModule(
  65. bazel.BazelTargetModuleProperties{
  66. Rule_class: "package",
  67. },
  68. CommonAttributes{},
  69. &bazelPackageAttributes{
  70. Default_package_metadata: defaultPackageMetadata,
  71. // FIXME(asmundak): once b/221436821 is resolved
  72. Default_visibility: []string{"//visibility:public"},
  73. })
  74. }
  75. func (p *packageModule) GenerateAndroidBuildActions(ModuleContext) {
  76. // Nothing to do.
  77. }
  78. func (p *packageModule) GenerateBuildActions(ctx blueprint.ModuleContext) {
  79. // Nothing to do.
  80. }
  81. func (p *packageModule) qualifiedModuleId(ctx BaseModuleContext) qualifiedModuleName {
  82. // Override to create a package id.
  83. return newPackageId(ctx.ModuleDir())
  84. }
  85. func PackageFactory() Module {
  86. module := &packageModule{}
  87. module.AddProperties(&module.properties, &module.commonProperties.BazelConversionStatus)
  88. // The name is the relative path from build root to the directory containing this
  89. // module. Set that name at the earliest possible moment that information is available
  90. // which is in a LoadHook.
  91. AddLoadHook(module, func(ctx LoadHookContext) {
  92. module.nameProperties.Name = proptools.StringPtr("//" + ctx.ModuleDir())
  93. })
  94. // The default_visibility property needs to be checked and parsed by the visibility module during
  95. // its checking and parsing phases so make it the primary visibility property.
  96. setPrimaryVisibilityProperty(module, "default_visibility", &module.properties.Default_visibility)
  97. // The default_applicable_licenses property needs to be checked and parsed by the licenses module during
  98. // its checking and parsing phases so make it the primary licenses property.
  99. setPrimaryLicensesProperty(module, "default_applicable_licenses", &module.properties.Default_applicable_licenses)
  100. InitBazelModule(module)
  101. return module
  102. }