metadata.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. "encoding/json"
  18. )
  19. func init() {
  20. android.RegisterParallelSingletonType("update-meta", UpdateMetaSingleton)
  21. }
  22. func UpdateMetaSingleton() android.Singleton {
  23. return &updateMetaSingleton{}
  24. }
  25. type jsonImported struct {
  26. FileGroups map[string][]string `json:",omitempty"`
  27. }
  28. type metadataJsonFlags struct {
  29. Imported jsonImported `json:",omitempty"`
  30. Exported map[string][]string `json:",omitempty"`
  31. }
  32. type updateMetaSingleton struct {
  33. importedModules []string
  34. generatedMetadataFile android.OutputPath
  35. }
  36. func (s *updateMetaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
  37. metadata := metadataJsonFlags{
  38. Imported: jsonImported{
  39. FileGroups: make(map[string][]string),
  40. },
  41. Exported: make(map[string][]string),
  42. }
  43. ctx.VisitAllModules(func(module android.Module) {
  44. if ifg, ok := module.(*importedFileGroup); ok {
  45. metadata.Imported.FileGroups[ifg.BaseModuleName()] = ifg.properties.Imported
  46. }
  47. if e, ok := module.(ExportableModule); ok {
  48. if e.IsExported() && e.Exportable() {
  49. for tag, files := range e.TaggedOutputs() {
  50. // TODO(b/219846705): refactor this to a dictionary
  51. metadata.Exported[e.Name()+":"+tag] = append(metadata.Exported[e.Name()+":"+tag], files.Strings()...)
  52. }
  53. }
  54. }
  55. })
  56. jsonStr, err := json.Marshal(metadata)
  57. if err != nil {
  58. ctx.Errorf(err.Error())
  59. }
  60. s.generatedMetadataFile = android.PathForOutput(ctx, "multitree", "metadata.json")
  61. android.WriteFileRule(ctx, s.generatedMetadataFile, string(jsonStr))
  62. }
  63. func (s *updateMetaSingleton) MakeVars(ctx android.MakeVarsContext) {
  64. ctx.Strict("MULTITREE_METADATA", s.generatedMetadataFile.String())
  65. }