blob: 04407eb32ae2f638599a5f97eaa092a2452bc163 [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 Gaston7b6118b2017-10-04 21:07:42 +000021// Mutator phases:
22// Pre-arch
23// Arch
24// Pre-deps
25// Deps
26// PostDeps
Colin Cross1e676be2016-10-12 14:38:15 -070027
Colin Cross795c3772017-03-16 16:50:10 -070028func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
29 for _, t := range mutators {
30 var handle blueprint.MutatorHandle
31 if t.bottomUpMutator != nil {
32 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
33 } else if t.topDownMutator != nil {
34 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
35 }
36 if t.parallel {
37 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070038 }
39 }
Colin Cross1e676be2016-10-12 14:38:15 -070040}
41
Colin Crosscec81712017-07-13 14:43:27 -070042func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) {
43 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080044
45 register := func(funcs []RegisterMutatorFunc) {
46 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070047 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080048 }
49 }
50
Colin Crosscec81712017-07-13 14:43:27 -070051 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080052
Colin Crosscec81712017-07-13 14:43:27 -070053 register(preDeps)
54
55 mctx.BottomUp("deps", depsMutator).Parallel()
56
57 register(postDeps)
58
59 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070060}
61
62type registerMutatorsContext struct {
63 mutators []*mutator
64}
Colin Cross1e676be2016-10-12 14:38:15 -070065
66type RegisterMutatorsContext interface {
67 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
68 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
69}
70
71type RegisterMutatorFunc func(RegisterMutatorsContext)
72
Colin Crosscec81712017-07-13 14:43:27 -070073var preArch = []RegisterMutatorFunc{
74 func(ctx RegisterMutatorsContext) {
75 ctx.TopDown("load_hooks", loadHookMutator).Parallel()
76 },
Colin Cross5ea9bcc2017-07-27 15:41:32 -070077 RegisterPrebuiltsPreArchMutators,
Colin Cross89536d42017-07-07 14:35:50 -070078 RegisterDefaultsPreArchMutators,
Colin Crosscec81712017-07-13 14:43:27 -070079}
80
Colin Crossae4c6182017-09-15 17:33:55 -070081func registerArchMutator(ctx RegisterMutatorsContext) {
82 ctx.BottomUp("arch", archMutator).Parallel()
83 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
84}
85
Colin Crosscec81712017-07-13 14:43:27 -070086var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070087 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070088}
89
90var postDeps = []RegisterMutatorFunc{
Colin Cross5ea9bcc2017-07-27 15:41:32 -070091 RegisterPrebuiltsPostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -070092}
Colin Cross1e676be2016-10-12 14:38:15 -070093
94func PreArchMutators(f RegisterMutatorFunc) {
95 preArch = append(preArch, f)
96}
97
98func PreDepsMutators(f RegisterMutatorFunc) {
99 preDeps = append(preDeps, f)
100}
101
102func PostDepsMutators(f RegisterMutatorFunc) {
103 postDeps = append(postDeps, f)
104}
105
Colin Cross635c3b02016-05-18 15:37:25 -0700106type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700107
Colin Cross635c3b02016-05-18 15:37:25 -0700108type TopDownMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700109 blueprint.TopDownMutatorContext
110 androidBaseContext
111}
112
113type androidTopDownMutatorContext struct {
114 blueprint.TopDownMutatorContext
115 androidBaseContextImpl
116}
117
Colin Cross635c3b02016-05-18 15:37:25 -0700118type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700119
Colin Cross635c3b02016-05-18 15:37:25 -0700120type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700121 blueprint.BottomUpMutatorContext
122 androidBaseContext
123}
124
125type androidBottomUpMutatorContext struct {
126 blueprint.BottomUpMutatorContext
127 androidBaseContextImpl
128}
129
Colin Cross795c3772017-03-16 16:50:10 -0700130func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700131 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700132 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700133 actx := &androidBottomUpMutatorContext{
134 BottomUpMutatorContext: ctx,
135 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
136 }
Colin Cross798bfce2016-10-12 14:28:16 -0700137 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700138 }
Colin Cross798bfce2016-10-12 14:28:16 -0700139 }
140 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700141 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700142 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700143}
144
Colin Cross795c3772017-03-16 16:50:10 -0700145func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700146 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700147 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700148 actx := &androidTopDownMutatorContext{
149 TopDownMutatorContext: ctx,
150 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
151 }
Colin Cross798bfce2016-10-12 14:28:16 -0700152 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700153 }
Colin Cross798bfce2016-10-12 14:28:16 -0700154 }
155 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700156 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700157 return mutator
158}
159
160type MutatorHandle interface {
161 Parallel() MutatorHandle
162}
163
164func (mutator *mutator) Parallel() MutatorHandle {
165 mutator.parallel = true
166 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700167}
Colin Cross1e676be2016-10-12 14:38:15 -0700168
169func depsMutator(ctx BottomUpMutatorContext) {
170 if m, ok := ctx.Module().(Module); ok {
171 m.DepsMutator(ctx)
172 }
173}