aidl_library.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2023 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 aidl_library
  15. import (
  16. "android/soong/android"
  17. "android/soong/bazel"
  18. "github.com/google/blueprint"
  19. "github.com/google/blueprint/proptools"
  20. )
  21. var PrepareForTestWithAidlLibrary = android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
  22. registerAidlLibraryBuildComponents(ctx)
  23. })
  24. func init() {
  25. registerAidlLibraryBuildComponents(android.InitRegistrationContext)
  26. }
  27. func registerAidlLibraryBuildComponents(ctx android.RegistrationContext) {
  28. ctx.RegisterModuleType("aidl_library", AidlLibraryFactory)
  29. }
  30. type aidlLibraryProperties struct {
  31. // srcs lists files that are included in this module for aidl compilation
  32. Srcs []string `android:"path"`
  33. // hdrs lists the headers that are imported by srcs but are not compiled by aidl to language binding code
  34. // hdrs is provided to support Bazel migration. It is a no-op until
  35. // we enable input sandbox in aidl compilation action
  36. Hdrs []string `android:"path"`
  37. // The prefix to strip from the paths of the .aidl files
  38. // The remaining path is the package path of the aidl interface
  39. Strip_import_prefix *string
  40. // List of aidl files or aidl_library depended on by the module
  41. Deps []string `android:"arch_variant"`
  42. }
  43. type AidlLibrary struct {
  44. android.ModuleBase
  45. android.BazelModuleBase
  46. properties aidlLibraryProperties
  47. }
  48. type bazelAidlLibraryAttributes struct {
  49. Srcs bazel.LabelListAttribute
  50. Hdrs bazel.LabelListAttribute
  51. Strip_import_prefix *string
  52. Deps bazel.LabelListAttribute
  53. }
  54. func (lib *AidlLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  55. srcs := bazel.MakeLabelListAttribute(
  56. android.BazelLabelForModuleSrc(
  57. ctx,
  58. lib.properties.Srcs,
  59. ),
  60. )
  61. hdrs := bazel.MakeLabelListAttribute(
  62. android.BazelLabelForModuleSrc(
  63. ctx,
  64. lib.properties.Hdrs,
  65. ),
  66. )
  67. tags := []string{"apex_available=//apex_available:anyapex"}
  68. deps := bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, lib.properties.Deps))
  69. attrs := &bazelAidlLibraryAttributes{
  70. Srcs: srcs,
  71. Hdrs: hdrs,
  72. Strip_import_prefix: lib.properties.Strip_import_prefix,
  73. Deps: deps,
  74. }
  75. props := bazel.BazelTargetModuleProperties{
  76. Rule_class: "aidl_library",
  77. Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl",
  78. }
  79. ctx.CreateBazelTargetModule(
  80. props,
  81. android.CommonAttributes{
  82. Name: lib.Name(),
  83. Tags: bazel.MakeStringListAttribute(tags),
  84. },
  85. attrs,
  86. )
  87. }
  88. type AidlLibraryInfo struct {
  89. // The direct aidl files of the module
  90. Srcs android.Paths
  91. // The include dirs to the direct aidl files and those provided from transitive aidl_library deps
  92. IncludeDirs android.DepSet[android.Path]
  93. // The direct hdrs and hdrs from transitive deps
  94. Hdrs android.DepSet[android.Path]
  95. }
  96. // AidlLibraryProvider provides the srcs and the transitive include dirs
  97. var AidlLibraryProvider = blueprint.NewProvider(AidlLibraryInfo{})
  98. func (lib *AidlLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  99. includeDirsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.PREORDER)
  100. hdrsDepSetBuilder := android.NewDepSetBuilder[android.Path](android.PREORDER)
  101. if len(lib.properties.Srcs) == 0 && len(lib.properties.Hdrs) == 0 {
  102. ctx.ModuleErrorf("at least srcs or hdrs prop must be non-empty")
  103. }
  104. srcs := android.PathsForModuleSrc(ctx, lib.properties.Srcs)
  105. hdrs := android.PathsForModuleSrc(ctx, lib.properties.Hdrs)
  106. if lib.properties.Strip_import_prefix != nil {
  107. srcs = android.PathsWithModuleSrcSubDir(
  108. ctx,
  109. srcs,
  110. android.String(lib.properties.Strip_import_prefix),
  111. )
  112. hdrs = android.PathsWithModuleSrcSubDir(
  113. ctx,
  114. hdrs,
  115. android.String(lib.properties.Strip_import_prefix),
  116. )
  117. }
  118. hdrsDepSetBuilder.Direct(hdrs...)
  119. includeDir := android.PathForModuleSrc(
  120. ctx,
  121. proptools.StringDefault(lib.properties.Strip_import_prefix, ""),
  122. )
  123. includeDirsDepSetBuilder.Direct(includeDir)
  124. for _, dep := range ctx.GetDirectDepsWithTag(aidlLibraryTag) {
  125. if ctx.OtherModuleHasProvider(dep, AidlLibraryProvider) {
  126. info := ctx.OtherModuleProvider(dep, AidlLibraryProvider).(AidlLibraryInfo)
  127. includeDirsDepSetBuilder.Transitive(&info.IncludeDirs)
  128. hdrsDepSetBuilder.Transitive(&info.Hdrs)
  129. }
  130. }
  131. ctx.SetProvider(AidlLibraryProvider, AidlLibraryInfo{
  132. Srcs: srcs,
  133. IncludeDirs: *includeDirsDepSetBuilder.Build(),
  134. Hdrs: *hdrsDepSetBuilder.Build(),
  135. })
  136. }
  137. // aidl_library contains a list of .aidl files and the strip_import_prefix to
  138. // to strip from the paths of the .aidl files. The sub-path left-over after stripping
  139. // corresponds to the aidl package path the aidl interfaces are scoped in
  140. func AidlLibraryFactory() android.Module {
  141. module := &AidlLibrary{}
  142. module.AddProperties(&module.properties)
  143. android.InitAndroidModule(module)
  144. android.InitBazelModule(module)
  145. return module
  146. }
  147. type aidlDependencyTag struct {
  148. blueprint.BaseDependencyTag
  149. }
  150. var aidlLibraryTag = aidlDependencyTag{}
  151. func (lib *AidlLibrary) DepsMutator(ctx android.BottomUpMutatorContext) {
  152. for _, dep := range lib.properties.Deps {
  153. ctx.AddDependency(lib, aidlLibraryTag, dep)
  154. }
  155. }