blob: 940b0ffb6cf7357cc1a1f12a0cae35773ec4a2d5 [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) {
84 mutators := registerMutatorsContext{}
85 mutators.BottomUp("deps", depsMutator).Parallel()
86 registerMutatorsToContext(ctx, mutators.mutators)
87}
88
89type registerMutatorsContext struct {
90 mutators []*mutator
91}
Colin Cross1e676be2016-10-12 14:38:15 -070092
93type RegisterMutatorsContext interface {
94 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
95 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
96}
97
98type RegisterMutatorFunc func(RegisterMutatorsContext)
99
100var preArch, preDeps, postDeps []RegisterMutatorFunc
101
102func PreArchMutators(f RegisterMutatorFunc) {
103 preArch = append(preArch, f)
104}
105
106func PreDepsMutators(f RegisterMutatorFunc) {
107 preDeps = append(preDeps, f)
108}
109
110func PostDepsMutators(f RegisterMutatorFunc) {
111 postDeps = append(postDeps, f)
112}
113
Colin Cross635c3b02016-05-18 15:37:25 -0700114type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700115
Colin Cross635c3b02016-05-18 15:37:25 -0700116type TopDownMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700117 blueprint.TopDownMutatorContext
118 androidBaseContext
119}
120
121type androidTopDownMutatorContext struct {
122 blueprint.TopDownMutatorContext
123 androidBaseContextImpl
124}
125
Colin Cross635c3b02016-05-18 15:37:25 -0700126type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700127
Colin Cross635c3b02016-05-18 15:37:25 -0700128type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700129 blueprint.BottomUpMutatorContext
130 androidBaseContext
131}
132
133type androidBottomUpMutatorContext struct {
134 blueprint.BottomUpMutatorContext
135 androidBaseContextImpl
136}
137
Colin Cross795c3772017-03-16 16:50:10 -0700138func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700139 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700140 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700141 actx := &androidBottomUpMutatorContext{
142 BottomUpMutatorContext: ctx,
143 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
144 }
Colin Cross798bfce2016-10-12 14:28:16 -0700145 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700146 }
Colin Cross798bfce2016-10-12 14:28:16 -0700147 }
148 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700149 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700150 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700151}
152
Colin Cross795c3772017-03-16 16:50:10 -0700153func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700154 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700155 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700156 actx := &androidTopDownMutatorContext{
157 TopDownMutatorContext: ctx,
158 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
159 }
Colin Cross798bfce2016-10-12 14:28:16 -0700160 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700161 }
Colin Cross798bfce2016-10-12 14:28:16 -0700162 }
163 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700164 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700165 return mutator
166}
167
168type MutatorHandle interface {
169 Parallel() MutatorHandle
170}
171
172func (mutator *mutator) Parallel() MutatorHandle {
173 mutator.parallel = true
174 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700175}
Colin Cross1e676be2016-10-12 14:38:15 -0700176
177func depsMutator(ctx BottomUpMutatorContext) {
178 if m, ok := ctx.Module().(Module); ok {
179 m.DepsMutator(ctx)
180 }
181}