blob: bb494878db9d43f9a3e74e31639038c67d4f0752 [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 (
18 "sync"
19
20 "github.com/google/blueprint"
21)
Colin Cross6362e272015-10-29 15:25:03 -070022
Colin Cross1e676be2016-10-12 14:38:15 -070023// Mutator phases:
24// Pre-arch
25// Arch
26// Pre-deps
27// Deps
28// PostDeps
29
Colin Cross795c3772017-03-16 16:50:10 -070030var registerMutatorsOnce sync.Once
31var registeredMutators []*mutator
Colin Cross1e676be2016-10-12 14:38:15 -070032
Colin Cross795c3772017-03-16 16:50:10 -070033func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
34 for _, t := range mutators {
35 var handle blueprint.MutatorHandle
36 if t.bottomUpMutator != nil {
37 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
38 } else if t.topDownMutator != nil {
39 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
40 }
41 if t.parallel {
42 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070043 }
44 }
Colin Cross1e676be2016-10-12 14:38:15 -070045}
46
Colin Cross795c3772017-03-16 16:50:10 -070047func registerMutators(ctx *blueprint.Context) {
48
49 registerMutatorsOnce.Do(func() {
50 ctx := &registerMutatorsContext{}
51
52 register := func(funcs []RegisterMutatorFunc) {
53 for _, f := range funcs {
54 f(ctx)
55 }
56 }
57
58 ctx.TopDown("load_hooks", loadHookMutator).Parallel()
59 ctx.BottomUp("prebuilts", prebuiltMutator).Parallel()
60 ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
61 ctx.TopDown("defaults", defaultsMutator).Parallel()
62
63 register(preArch)
64
65 ctx.BottomUp("arch", archMutator).Parallel()
66 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
67
68 register(preDeps)
69
70 ctx.BottomUp("deps", depsMutator).Parallel()
71
72 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
73 ctx.BottomUp("prebuilt_replace", PrebuiltReplaceMutator).Parallel()
74
75 register(postDeps)
76
77 registeredMutators = ctx.mutators
78 })
79
80 registerMutatorsToContext(ctx, registeredMutators)
81}
82
83func RegisterTestMutators(ctx *blueprint.Context) {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080084 mutators := &registerMutatorsContext{}
85
86 register := func(funcs []RegisterMutatorFunc) {
87 for _, f := range funcs {
88 f(mutators)
89 }
90 }
91
92 register(testPreDeps)
Colin Cross795c3772017-03-16 16:50:10 -070093 mutators.BottomUp("deps", depsMutator).Parallel()
Nan Zhangdb0b9a32017-02-27 10:12:13 -080094 register(testPostDeps)
95
Colin Cross795c3772017-03-16 16:50:10 -070096 registerMutatorsToContext(ctx, mutators.mutators)
97}
98
99type registerMutatorsContext struct {
100 mutators []*mutator
101}
Colin Cross1e676be2016-10-12 14:38:15 -0700102
103type RegisterMutatorsContext interface {
104 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
105 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
106}
107
108type RegisterMutatorFunc func(RegisterMutatorsContext)
109
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800110var preArch, preDeps, postDeps, testPreDeps, testPostDeps []RegisterMutatorFunc
Colin Cross1e676be2016-10-12 14:38:15 -0700111
112func PreArchMutators(f RegisterMutatorFunc) {
113 preArch = append(preArch, f)
114}
115
116func PreDepsMutators(f RegisterMutatorFunc) {
117 preDeps = append(preDeps, f)
118}
119
120func PostDepsMutators(f RegisterMutatorFunc) {
121 postDeps = append(postDeps, f)
122}
123
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124func TestPreDepsMutators(f RegisterMutatorFunc) {
125 testPreDeps = append(testPreDeps, f)
126}
127
128func TeststPostDepsMutators(f RegisterMutatorFunc) {
129 testPostDeps = append(testPostDeps, f)
130}
131
Colin Cross635c3b02016-05-18 15:37:25 -0700132type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700133
Colin Cross635c3b02016-05-18 15:37:25 -0700134type TopDownMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700135 blueprint.TopDownMutatorContext
136 androidBaseContext
137}
138
139type androidTopDownMutatorContext struct {
140 blueprint.TopDownMutatorContext
141 androidBaseContextImpl
142}
143
Colin Cross635c3b02016-05-18 15:37:25 -0700144type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700145
Colin Cross635c3b02016-05-18 15:37:25 -0700146type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700147 blueprint.BottomUpMutatorContext
148 androidBaseContext
149}
150
151type androidBottomUpMutatorContext struct {
152 blueprint.BottomUpMutatorContext
153 androidBaseContextImpl
154}
155
Colin Cross795c3772017-03-16 16:50:10 -0700156func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700157 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700158 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700159 actx := &androidBottomUpMutatorContext{
160 BottomUpMutatorContext: ctx,
161 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
162 }
Colin Cross798bfce2016-10-12 14:28:16 -0700163 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700164 }
Colin Cross798bfce2016-10-12 14:28:16 -0700165 }
166 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700167 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700168 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700169}
170
Colin Cross795c3772017-03-16 16:50:10 -0700171func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700172 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700173 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700174 actx := &androidTopDownMutatorContext{
175 TopDownMutatorContext: ctx,
176 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
177 }
Colin Cross798bfce2016-10-12 14:28:16 -0700178 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700179 }
Colin Cross798bfce2016-10-12 14:28:16 -0700180 }
181 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700182 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700183 return mutator
184}
185
186type MutatorHandle interface {
187 Parallel() MutatorHandle
188}
189
190func (mutator *mutator) Parallel() MutatorHandle {
191 mutator.parallel = true
192 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700193}
Colin Cross1e676be2016-10-12 14:38:15 -0700194
195func depsMutator(ctx BottomUpMutatorContext) {
196 if m, ok := ctx.Module().(Module); ok {
197 m.DepsMutator(ctx)
198 }
199}