api_domain.go 4.3 KB

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