import.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. )
  18. var (
  19. nameSuffix = ".imported"
  20. )
  21. type MultitreeImportedModuleInterface interface {
  22. GetMultitreeImportedModuleName() string
  23. }
  24. func init() {
  25. android.RegisterModuleType("imported_filegroup", importedFileGroupFactory)
  26. android.PreArchMutators(RegisterMultitreePreArchMutators)
  27. }
  28. type importedFileGroupProperties struct {
  29. // Imported modules from the other components in a multi-tree
  30. Imported []string
  31. }
  32. type importedFileGroup struct {
  33. android.ModuleBase
  34. properties importedFileGroupProperties
  35. srcs android.Paths
  36. }
  37. func (ifg *importedFileGroup) Name() string {
  38. return ifg.BaseModuleName() + nameSuffix
  39. }
  40. func importedFileGroupFactory() android.Module {
  41. module := &importedFileGroup{}
  42. module.AddProperties(&module.properties)
  43. android.InitAndroidModule(module)
  44. return module
  45. }
  46. var _ MultitreeImportedModuleInterface = (*importedFileGroup)(nil)
  47. func (ifg *importedFileGroup) GetMultitreeImportedModuleName() string {
  48. // The base module name of the imported filegroup is used as the imported module name
  49. return ifg.BaseModuleName()
  50. }
  51. var _ android.SourceFileProducer = (*importedFileGroup)(nil)
  52. func (ifg *importedFileGroup) Srcs() android.Paths {
  53. return ifg.srcs
  54. }
  55. func (ifg *importedFileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  56. // srcs from this module must not be used. Adding a dot path to avoid the empty
  57. // source failure. Still soong returns error when a module wants to build against
  58. // this source, which is intended.
  59. ifg.srcs = android.PathsForModuleSrc(ctx, []string{"."})
  60. }
  61. func RegisterMultitreePreArchMutators(ctx android.RegisterMutatorsContext) {
  62. ctx.BottomUp("multitree_imported_rename", MultitreeImportedRenameMutator).Parallel()
  63. }
  64. func MultitreeImportedRenameMutator(ctx android.BottomUpMutatorContext) {
  65. if m, ok := ctx.Module().(MultitreeImportedModuleInterface); ok {
  66. name := m.GetMultitreeImportedModuleName()
  67. if !ctx.OtherModuleExists(name) {
  68. // Provide an empty filegroup not to break the build while updating the metadata.
  69. // In other cases, soong will report an error to guide users to run 'm update-meta'
  70. // first.
  71. if !ctx.Config().TargetMultitreeUpdateMeta() {
  72. ctx.ModuleErrorf("\"%s\" filegroup must be imported.\nRun 'm update-meta' first to import the filegroup.", name)
  73. }
  74. ctx.Rename(name)
  75. }
  76. }
  77. }