api_surface.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2021 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 multitree
  15. import (
  16. "android/soong/android"
  17. "fmt"
  18. "github.com/google/blueprint"
  19. )
  20. var (
  21. pctx = android.NewPackageContext("android/soong/multitree")
  22. )
  23. func init() {
  24. RegisterApiSurfaceBuildComponents(android.InitRegistrationContext)
  25. }
  26. var PrepareForTestWithApiSurface = android.FixtureRegisterWithContext(RegisterApiSurfaceBuildComponents)
  27. func RegisterApiSurfaceBuildComponents(ctx android.RegistrationContext) {
  28. ctx.RegisterModuleType("api_surface", ApiSurfaceFactory)
  29. }
  30. type ApiSurface struct {
  31. android.ModuleBase
  32. ExportableModuleBase
  33. properties apiSurfaceProperties
  34. allOutputs android.Paths
  35. taggedOutputs map[string]android.Paths
  36. }
  37. type apiSurfaceProperties struct {
  38. Contributions []string
  39. }
  40. func ApiSurfaceFactory() android.Module {
  41. module := &ApiSurface{}
  42. module.AddProperties(&module.properties)
  43. android.InitAndroidModule(module)
  44. InitExportableModule(module)
  45. return module
  46. }
  47. func (surface *ApiSurface) DepsMutator(ctx android.BottomUpMutatorContext) {
  48. if surface.properties.Contributions != nil {
  49. ctx.AddVariationDependencies(nil, nil, surface.properties.Contributions...)
  50. }
  51. }
  52. func (surface *ApiSurface) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  53. contributionFiles := make(map[string]android.Paths)
  54. var allOutputs android.Paths
  55. ctx.WalkDeps(func(child, parent android.Module) bool {
  56. if contribution, ok := child.(ApiContribution); ok {
  57. copied := contribution.CopyFilesWithTag(ctx)
  58. for tag, files := range copied {
  59. contributionFiles[child.Name()+"#"+tag] = files
  60. }
  61. for _, paths := range copied {
  62. allOutputs = append(allOutputs, paths...)
  63. }
  64. return false // no transitive dependencies
  65. }
  66. return false
  67. })
  68. // phony target
  69. ctx.Build(pctx, android.BuildParams{
  70. Rule: blueprint.Phony,
  71. Output: android.PathForPhony(ctx, ctx.ModuleName()),
  72. Inputs: allOutputs,
  73. })
  74. surface.allOutputs = allOutputs
  75. surface.taggedOutputs = contributionFiles
  76. }
  77. func (surface *ApiSurface) OutputFiles(tag string) (android.Paths, error) {
  78. if tag != "" {
  79. return nil, fmt.Errorf("unknown tag: %q", tag)
  80. }
  81. return surface.allOutputs, nil
  82. }
  83. func (surface *ApiSurface) TaggedOutputs() map[string]android.Paths {
  84. return surface.taggedOutputs
  85. }
  86. func (surface *ApiSurface) Exportable() bool {
  87. return true
  88. }
  89. var _ android.OutputFileProducer = (*ApiSurface)(nil)
  90. var _ Exportable = (*ApiSurface)(nil)
  91. type ApiContribution interface {
  92. // copy files necessaryt to construct an API surface
  93. // For C, it will be map.txt and .h files
  94. // For Java, it will be api.txt
  95. CopyFilesWithTag(ctx android.ModuleContext) map[string]android.Paths // output paths
  96. // Generate Android.bp in out/ to use the exported .txt files
  97. // GenerateBuildFiles(ctx ModuleContext) Paths //output paths
  98. }