blob: c8f3e8fee441258179c16a1b2f67b13431ee29a9 [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
Colin Cross1e676be2016-10-12 14:38:15 -070021// Mutator phases:
22// Pre-arch
23// Arch
24// Pre-deps
25// Deps
26// PostDeps
27
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 },
77 registerPrebuiltsPreArchMutators,
Colin Cross89536d42017-07-07 14:35:50 -070078 RegisterDefaultsPreArchMutators,
Colin Crosscec81712017-07-13 14:43:27 -070079}
80
81var preDeps = []RegisterMutatorFunc{
82 func(ctx RegisterMutatorsContext) {
83 ctx.BottomUp("arch", archMutator).Parallel()
84 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
85 },
86}
87
88var postDeps = []RegisterMutatorFunc{
89 registerPrebuiltsPostDepsMutators,
90}
Colin Cross1e676be2016-10-12 14:38:15 -070091
92func PreArchMutators(f RegisterMutatorFunc) {
93 preArch = append(preArch, f)
94}
95
96func PreDepsMutators(f RegisterMutatorFunc) {
97 preDeps = append(preDeps, f)
98}
99
100func PostDepsMutators(f RegisterMutatorFunc) {
101 postDeps = append(postDeps, f)
102}
103
Colin Cross635c3b02016-05-18 15:37:25 -0700104type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700105
Colin Cross635c3b02016-05-18 15:37:25 -0700106type TopDownMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700107 blueprint.TopDownMutatorContext
108 androidBaseContext
109}
110
111type androidTopDownMutatorContext struct {
112 blueprint.TopDownMutatorContext
113 androidBaseContextImpl
114}
115
Colin Cross635c3b02016-05-18 15:37:25 -0700116type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700117
Colin Cross635c3b02016-05-18 15:37:25 -0700118type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700119 blueprint.BottomUpMutatorContext
120 androidBaseContext
121}
122
123type androidBottomUpMutatorContext struct {
124 blueprint.BottomUpMutatorContext
125 androidBaseContextImpl
126}
127
Colin Cross795c3772017-03-16 16:50:10 -0700128func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700129 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700130 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700131 actx := &androidBottomUpMutatorContext{
132 BottomUpMutatorContext: ctx,
133 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
134 }
Colin Cross798bfce2016-10-12 14:28:16 -0700135 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700136 }
Colin Cross798bfce2016-10-12 14:28:16 -0700137 }
138 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700139 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700140 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700141}
142
Colin Cross795c3772017-03-16 16:50:10 -0700143func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700144 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700145 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700146 actx := &androidTopDownMutatorContext{
147 TopDownMutatorContext: ctx,
148 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
149 }
Colin Cross798bfce2016-10-12 14:28:16 -0700150 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700151 }
Colin Cross798bfce2016-10-12 14:28:16 -0700152 }
153 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700154 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700155 return mutator
156}
157
158type MutatorHandle interface {
159 Parallel() MutatorHandle
160}
161
162func (mutator *mutator) Parallel() MutatorHandle {
163 mutator.parallel = true
164 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700165}
Colin Cross1e676be2016-10-12 14:38:15 -0700166
167func depsMutator(ctx BottomUpMutatorContext) {
168 if m, ok := ctx.Module().(Module); ok {
169 m.DepsMutator(ctx)
170 }
171}