api_domain.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2022 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. "github.com/google/blueprint"
  17. "android/soong/bazel"
  18. )
  19. func init() {
  20. RegisterApiDomainBuildComponents(InitRegistrationContext)
  21. }
  22. func RegisterApiDomainBuildComponents(ctx RegistrationContext) {
  23. ctx.RegisterModuleType("api_domain", ApiDomainFactory)
  24. }
  25. type ApiSurface int
  26. // TODO(b/246656800): Reconcile with android.SdkKind
  27. const (
  28. PublicApi ApiSurface = iota
  29. SystemApi
  30. VendorApi
  31. )
  32. func (a ApiSurface) String() string {
  33. switch a {
  34. case PublicApi:
  35. return "publicapi"
  36. case SystemApi:
  37. return "systemapi"
  38. case VendorApi:
  39. return "vendorapi"
  40. default:
  41. return "invalid"
  42. }
  43. }
  44. type apiDomain struct {
  45. ModuleBase
  46. BazelModuleBase
  47. properties apiDomainProperties
  48. }
  49. type apiDomainProperties struct {
  50. // cc library contributions (.h files/.map.txt) of this API domain
  51. // This dependency is a no-op in Soong, but the corresponding Bazel target in the bp2build workspace will provide a `CcApiContributionInfo` provider
  52. Cc_api_contributions []string
  53. }
  54. func ApiDomainFactory() Module {
  55. m := &apiDomain{}
  56. m.AddProperties(&m.properties)
  57. InitAndroidArchModule(m, DeviceSupported, MultilibBoth)
  58. return m
  59. }
  60. func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) {
  61. for _, cc := range a.properties.Cc_api_contributions {
  62. // Use FarVariationDependencies since the variants of api_domain is a subset of the variants of the dependency cc module
  63. // Creating a dependency on the first variant is ok since this is a no-op in Soong
  64. // The primary function of this dependency is to create a connected graph in the corresponding bp2build workspace
  65. ctx.AddFarVariationDependencies([]blueprint.Variation{}, nil, cc)
  66. }
  67. }
  68. // API domain does not have any builld actions yet
  69. func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) {
  70. }
  71. const (
  72. apiContributionSuffix = ".contribution"
  73. )
  74. // ApiContributionTargetName returns the name of the bp2build target (e.g. cc_api_contribution) of contribution modules (e.g. ndk_library)
  75. // A suffix is necessary to prevent a name collision with the base target in the same bp2build bazel package
  76. func ApiContributionTargetName(moduleName string) string {
  77. return moduleName + apiContributionSuffix
  78. }
  79. // For each contributing cc_library, format the name to its corresponding contribution bazel target in the bp2build workspace
  80. func contributionBazelAttributes(ctx TopDownMutatorContext, contributions []string) bazel.LabelListAttribute {
  81. addSuffix := func(ctx BazelConversionPathContext, module blueprint.Module) string {
  82. baseLabel := BazelModuleLabel(ctx, module)
  83. return ApiContributionTargetName(baseLabel)
  84. }
  85. bazelLabels := BazelLabelForModuleDepsWithFn(ctx, contributions, addSuffix)
  86. return bazel.MakeLabelListAttribute(bazelLabels)
  87. }
  88. type bazelApiDomainAttributes struct {
  89. Cc_api_contributions bazel.LabelListAttribute
  90. }
  91. var _ ApiProvider = (*apiDomain)(nil)
  92. func (a *apiDomain) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
  93. props := bazel.BazelTargetModuleProperties{
  94. Rule_class: "api_domain",
  95. Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl",
  96. }
  97. attrs := &bazelApiDomainAttributes{
  98. Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions),
  99. }
  100. ctx.CreateBazelTargetModule(props, CommonAttributes{
  101. Name: ctx.ModuleName(),
  102. }, attrs)
  103. }