blob: b485940ef169888eae33b303c7addd983f88e599 [file] [log] [blame]
Colin Cross6362e272015-10-29 15:25:03 -07001// Copyright 2015 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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross6362e272015-10-29 15:25:03 -070016
Colin Cross795c3772017-03-16 16:50:10 -070017import (
Colin Cross795c3772017-03-16 16:50:10 -070018 "github.com/google/blueprint"
19)
Colin Cross6362e272015-10-29 15:25:03 -070020
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070021// Phases:
22// run Pre-arch mutators
23// run archMutator
24// run Pre-deps mutators
25// run depsMutator
26// run PostDeps mutators
27// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070028
Colin Cross795c3772017-03-16 16:50:10 -070029func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
30 for _, t := range mutators {
31 var handle blueprint.MutatorHandle
32 if t.bottomUpMutator != nil {
33 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
34 } else if t.topDownMutator != nil {
35 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
36 }
37 if t.parallel {
38 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070039 }
40 }
Colin Cross1e676be2016-10-12 14:38:15 -070041}
42
Colin Crosscec81712017-07-13 14:43:27 -070043func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) {
44 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080045
46 register := func(funcs []RegisterMutatorFunc) {
47 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070048 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080049 }
50 }
51
Colin Crosscec81712017-07-13 14:43:27 -070052 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080053
Colin Crosscec81712017-07-13 14:43:27 -070054 register(preDeps)
55
56 mctx.BottomUp("deps", depsMutator).Parallel()
57
58 register(postDeps)
59
60 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070061}
62
63type registerMutatorsContext struct {
64 mutators []*mutator
65}
Colin Cross1e676be2016-10-12 14:38:15 -070066
67type RegisterMutatorsContext interface {
68 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
69 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
70}
71
72type RegisterMutatorFunc func(RegisterMutatorsContext)
73
Colin Crosscec81712017-07-13 14:43:27 -070074var preArch = []RegisterMutatorFunc{
75 func(ctx RegisterMutatorsContext) {
76 ctx.TopDown("load_hooks", loadHookMutator).Parallel()
77 },
Colin Cross5ea9bcc2017-07-27 15:41:32 -070078 RegisterPrebuiltsPreArchMutators,
Colin Cross89536d42017-07-07 14:35:50 -070079 RegisterDefaultsPreArchMutators,
Colin Crosscec81712017-07-13 14:43:27 -070080}
81
Colin Crossae4c6182017-09-15 17:33:55 -070082func registerArchMutator(ctx RegisterMutatorsContext) {
83 ctx.BottomUp("arch", archMutator).Parallel()
84 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
85}
86
Colin Crosscec81712017-07-13 14:43:27 -070087var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070088 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070089}
90
91var postDeps = []RegisterMutatorFunc{
Colin Cross5ea9bcc2017-07-27 15:41:32 -070092 RegisterPrebuiltsPostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -070093}
Colin Cross1e676be2016-10-12 14:38:15 -070094
95func PreArchMutators(f RegisterMutatorFunc) {
96 preArch = append(preArch, f)
97}
98
99func PreDepsMutators(f RegisterMutatorFunc) {
100 preDeps = append(preDeps, f)
101}
102
103func PostDepsMutators(f RegisterMutatorFunc) {
104 postDeps = append(postDeps, f)
105}
106
Colin Cross635c3b02016-05-18 15:37:25 -0700107type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700108
Colin Cross635c3b02016-05-18 15:37:25 -0700109type TopDownMutatorContext interface {
Colin Cross3f68a132017-10-23 17:10:29 -0700110 blueprint.BaseModuleContext
Colin Cross6362e272015-10-29 15:25:03 -0700111 androidBaseContext
Colin Cross3f68a132017-10-23 17:10:29 -0700112
113 OtherModuleExists(name string) bool
114 Rename(name string)
115 Module() blueprint.Module
116
117 OtherModuleName(m blueprint.Module) string
118 OtherModuleErrorf(m blueprint.Module, fmt string, args ...interface{})
119 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
120
121 CreateModule(blueprint.ModuleFactory, ...interface{})
122
123 GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module
124 GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag)
125
126 VisitDirectDeps(visit func(blueprint.Module))
127 VisitDirectDepsIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
128 VisitDepsDepthFirst(visit func(blueprint.Module))
129 VisitDepsDepthFirstIf(pred func(blueprint.Module) bool, visit func(blueprint.Module))
130 WalkDeps(visit func(blueprint.Module, blueprint.Module) bool)
Colin Cross6362e272015-10-29 15:25:03 -0700131}
132
133type androidTopDownMutatorContext struct {
134 blueprint.TopDownMutatorContext
135 androidBaseContextImpl
136}
137
Colin Cross635c3b02016-05-18 15:37:25 -0700138type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700139
Colin Cross635c3b02016-05-18 15:37:25 -0700140type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700141 blueprint.BottomUpMutatorContext
142 androidBaseContext
143}
144
145type androidBottomUpMutatorContext struct {
146 blueprint.BottomUpMutatorContext
147 androidBaseContextImpl
148}
149
Colin Cross795c3772017-03-16 16:50:10 -0700150func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700151 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700152 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700153 actx := &androidBottomUpMutatorContext{
154 BottomUpMutatorContext: ctx,
155 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
156 }
Colin Cross798bfce2016-10-12 14:28:16 -0700157 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700158 }
Colin Cross798bfce2016-10-12 14:28:16 -0700159 }
160 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700161 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700162 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700163}
164
Colin Cross795c3772017-03-16 16:50:10 -0700165func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700166 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700167 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700168 actx := &androidTopDownMutatorContext{
169 TopDownMutatorContext: ctx,
170 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
171 }
Colin Cross798bfce2016-10-12 14:28:16 -0700172 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700173 }
Colin Cross798bfce2016-10-12 14:28:16 -0700174 }
175 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700176 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700177 return mutator
178}
179
180type MutatorHandle interface {
181 Parallel() MutatorHandle
182}
183
184func (mutator *mutator) Parallel() MutatorHandle {
185 mutator.parallel = true
186 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700187}
Colin Cross1e676be2016-10-12 14:38:15 -0700188
189func depsMutator(ctx BottomUpMutatorContext) {
190 if m, ok := ctx.Module().(Module); ok {
191 m.DepsMutator(ctx)
192 }
193}