api_imports.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 multitree
  15. import (
  16. "android/soong/android"
  17. "strings"
  18. "github.com/google/blueprint"
  19. )
  20. var (
  21. apiImportNameSuffix = ".apiimport"
  22. )
  23. func init() {
  24. RegisterApiImportsModule(android.InitRegistrationContext)
  25. android.RegisterMakeVarsProvider(pctx, makeVarsProvider)
  26. }
  27. func RegisterApiImportsModule(ctx android.RegistrationContext) {
  28. ctx.RegisterModuleType("api_imports", apiImportsFactory)
  29. }
  30. type ApiImports struct {
  31. android.ModuleBase
  32. properties apiImportsProperties
  33. }
  34. type apiImportsProperties struct {
  35. Shared_libs []string // List of C shared libraries from API surfaces
  36. Header_libs []string // List of C header libraries from API surfaces
  37. Apex_shared_libs []string // List of C shared libraries with APEX stubs
  38. }
  39. // 'api_imports' is a module which describes modules available from API surfaces.
  40. // This module is required to get the list of all imported API modules, because
  41. // it is discouraged to loop and fetch all modules from its type information. The
  42. // only module with name 'api_imports' will be used from the build.
  43. func apiImportsFactory() android.Module {
  44. module := &ApiImports{}
  45. module.AddProperties(&module.properties)
  46. android.InitAndroidModule(module)
  47. return module
  48. }
  49. func (imports *ApiImports) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  50. // ApiImport module does not generate any build actions
  51. }
  52. type ApiImportInfo struct {
  53. SharedLibs, HeaderLibs, ApexSharedLibs map[string]string
  54. }
  55. var ApiImportsProvider = blueprint.NewMutatorProvider(ApiImportInfo{}, "deps")
  56. // Store module lists into ApiImportInfo and share it over mutator provider.
  57. func (imports *ApiImports) DepsMutator(ctx android.BottomUpMutatorContext) {
  58. generateNameMapWithSuffix := func(names []string) map[string]string {
  59. moduleNameMap := make(map[string]string)
  60. for _, name := range names {
  61. moduleNameMap[name] = name + apiImportNameSuffix
  62. }
  63. return moduleNameMap
  64. }
  65. sharedLibs := generateNameMapWithSuffix(imports.properties.Shared_libs)
  66. headerLibs := generateNameMapWithSuffix(imports.properties.Header_libs)
  67. apexSharedLibs := generateNameMapWithSuffix(imports.properties.Apex_shared_libs)
  68. ctx.SetProvider(ApiImportsProvider, ApiImportInfo{
  69. SharedLibs: sharedLibs,
  70. HeaderLibs: headerLibs,
  71. ApexSharedLibs: apexSharedLibs,
  72. })
  73. }
  74. func GetApiImportSuffix() string {
  75. return apiImportNameSuffix
  76. }
  77. func makeVarsProvider(ctx android.MakeVarsContext) {
  78. ctx.VisitAllModules(func(m android.Module) {
  79. if i, ok := m.(*ApiImports); ok {
  80. ctx.Strict("API_IMPORTED_SHARED_LIBRARIES", strings.Join(i.properties.Shared_libs, " "))
  81. ctx.Strict("API_IMPORTED_HEADER_LIBRARIES", strings.Join(i.properties.Header_libs, " "))
  82. }
  83. })
  84. }