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