aboutsummaryrefslogtreecommitdiff
path: root/multitree/import.go
blob: 1e5c421bcf27e07c5197631a020fcc23cce851da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2022 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package multitree

import (
	"android/soong/android"
)

var (
	nameSuffix = ".imported"
)

type MultitreeImportedModuleInterface interface {
	GetMultitreeImportedModuleName() string
}

func init() {
	android.RegisterModuleType("imported_filegroup", importedFileGroupFactory)

	android.PreArchMutators(RegisterMultitreePreArchMutators)
}

type importedFileGroupProperties struct {
	// Imported modules from the other components in a multi-tree
	Imported []string
}

type importedFileGroup struct {
	android.ModuleBase

	properties importedFileGroupProperties
	srcs       android.Paths
}

func (ifg *importedFileGroup) Name() string {
	return ifg.BaseModuleName() + nameSuffix
}

func importedFileGroupFactory() android.Module {
	module := &importedFileGroup{}
	module.AddProperties(&module.properties)

	android.InitAndroidModule(module)
	return module
}

var _ MultitreeImportedModuleInterface = (*importedFileGroup)(nil)

func (ifg *importedFileGroup) GetMultitreeImportedModuleName() string {
	// The base module name of the imported filegroup is used as the imported module name
	return ifg.BaseModuleName()
}

var _ android.SourceFileProducer = (*importedFileGroup)(nil)

func (ifg *importedFileGroup) Srcs() android.Paths {
	return ifg.srcs
}

func (ifg *importedFileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	// srcs from this module must not be used. Adding a dot path to avoid the empty
	// source failure. Still soong returns error when a module wants to build against
	// this source, which is intended.
	ifg.srcs = android.PathsForModuleSrc(ctx, []string{"."})
}

func RegisterMultitreePreArchMutators(ctx android.RegisterMutatorsContext) {
	ctx.BottomUp("multitree_imported_rename", MultitreeImportedRenameMutator).Parallel()
}

func MultitreeImportedRenameMutator(ctx android.BottomUpMutatorContext) {
	if m, ok := ctx.Module().(MultitreeImportedModuleInterface); ok {
		name := m.GetMultitreeImportedModuleName()
		if !ctx.OtherModuleExists(name) {
			// Provide an empty filegroup not to break the build while updating the metadata.
			// In other cases, soong will report an error to guide users to run 'm update-meta'
			// first.
			if !ctx.Config().TargetMultitreeUpdateMeta() {
				ctx.ModuleErrorf("\"%s\" filegroup must be imported.\nRun 'm update-meta' first to import the filegroup.", name)
			}
			ctx.Rename(name)
		}
	}
}