api_domain.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 api_bp2build workspace
  52. // will provide a `CcApiContributionInfo` provider
  53. Cc_api_contributions []string
  54. // java library contributions (as .txt) of this API domain
  55. // This dependency is a no-op in Soong, but the corresponding Bazel target in the api_bp2build workspace
  56. // will provide a `JavaApiContributionInfo` provider
  57. Java_api_contributions []string
  58. }
  59. func ApiDomainFactory() Module {
  60. m := &apiDomain{}
  61. m.AddProperties(&m.properties)
  62. InitAndroidArchModule(m, DeviceSupported, MultilibBoth)
  63. return m
  64. }
  65. // Do not create any dependency edges in Soong for now to skip visibility checks for some systemapi libraries.
  66. // Currently, all api_domain modules reside in build/orchestrator/apis/Android.bp
  67. // However, cc libraries like libsigchain (com.android.art) restrict their visibility to art/*
  68. // When the api_domain module types are collocated with their contributions, this dependency edge can be restored
  69. func (a *apiDomain) DepsMutator(ctx BottomUpMutatorContext) {
  70. }
  71. // API domain does not have any builld actions yet
  72. func (a *apiDomain) GenerateAndroidBuildActions(ctx ModuleContext) {
  73. }
  74. const (
  75. apiContributionSuffix = ".contribution"
  76. )
  77. // ApiContributionTargetName returns the name of the bp2build target (e.g. cc_api_contribution) of contribution modules (e.g. ndk_library)
  78. // A suffix is necessary to prevent a name collision with the base target in the same bp2build bazel package
  79. func ApiContributionTargetName(moduleName string) string {
  80. return moduleName + apiContributionSuffix
  81. }
  82. // For each contributing cc_library, format the name to its corresponding contribution bazel target in the bp2build workspace
  83. func contributionBazelAttributes(ctx TopDownMutatorContext, contributions []string) bazel.LabelListAttribute {
  84. addSuffix := func(ctx BazelConversionPathContext, module blueprint.Module) string {
  85. baseLabel := BazelModuleLabel(ctx, module)
  86. return ApiContributionTargetName(baseLabel)
  87. }
  88. bazelLabels := BazelLabelForModuleDepsWithFn(ctx, contributions, addSuffix)
  89. return bazel.MakeLabelListAttribute(bazelLabels)
  90. }
  91. type bazelApiDomainAttributes struct {
  92. Cc_api_contributions bazel.LabelListAttribute
  93. Java_api_contributions bazel.LabelListAttribute
  94. }
  95. var _ ApiProvider = (*apiDomain)(nil)
  96. func (a *apiDomain) ConvertWithApiBp2build(ctx TopDownMutatorContext) {
  97. props := bazel.BazelTargetModuleProperties{
  98. Rule_class: "api_domain",
  99. Bzl_load_location: "//build/bazel/rules/apis:api_domain.bzl",
  100. }
  101. attrs := &bazelApiDomainAttributes{
  102. Cc_api_contributions: contributionBazelAttributes(ctx, a.properties.Cc_api_contributions),
  103. Java_api_contributions: contributionBazelAttributes(ctx, a.properties.Java_api_contributions),
  104. }
  105. ctx.CreateBazelTargetModule(props, CommonAttributes{
  106. Name: ctx.ModuleName(),
  107. }, attrs)
  108. }