blob: 34202807d4f430e34298368d0459c83bfe1091b9 [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 Cross798bfce2016-10-12 14:28:16 -070017import "github.com/google/blueprint"
Colin Cross6362e272015-10-29 15:25:03 -070018
Colin Cross1e676be2016-10-12 14:38:15 -070019// Mutator phases:
20// Pre-arch
21// Arch
22// Pre-deps
23// Deps
24// PostDeps
25
26func registerMutators() {
27 ctx := registerMutatorsContext{}
28
29 register := func(funcs []RegisterMutatorFunc) {
30 for _, f := range funcs {
31 f(ctx)
32 }
33 }
34
35 ctx.TopDown("load_hooks", loadHookMutator).Parallel()
Colin Crossce75d2c2016-10-06 16:12:58 -070036 ctx.BottomUp("prebuilts", prebuiltMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070037 ctx.BottomUp("defaults_deps", defaultsDepsMutator).Parallel()
38 ctx.TopDown("defaults", defaultsMutator).Parallel()
39
40 register(preArch)
41
42 ctx.BottomUp("arch", archMutator).Parallel()
43 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
44
45 register(preDeps)
46
47 ctx.BottomUp("deps", depsMutator).Parallel()
48
Colin Crossa2f296f2016-11-29 15:16:18 -080049 ctx.TopDown("prebuilt_select", PrebuiltSelectModuleMutator).Parallel()
Colin Crossce75d2c2016-10-06 16:12:58 -070050 ctx.BottomUp("prebuilt_replace", PrebuiltReplaceMutator).Parallel()
Colin Crossce75d2c2016-10-06 16:12:58 -070051
Colin Cross1e676be2016-10-12 14:38:15 -070052 register(postDeps)
53}
54
55type registerMutatorsContext struct{}
56
57type RegisterMutatorsContext interface {
58 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
59 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
60}
61
62type RegisterMutatorFunc func(RegisterMutatorsContext)
63
64var preArch, preDeps, postDeps []RegisterMutatorFunc
65
66func PreArchMutators(f RegisterMutatorFunc) {
67 preArch = append(preArch, f)
68}
69
70func PreDepsMutators(f RegisterMutatorFunc) {
71 preDeps = append(preDeps, f)
72}
73
74func PostDepsMutators(f RegisterMutatorFunc) {
75 postDeps = append(postDeps, f)
76}
77
Colin Cross635c3b02016-05-18 15:37:25 -070078type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -070079
Colin Cross635c3b02016-05-18 15:37:25 -070080type TopDownMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -070081 blueprint.TopDownMutatorContext
82 androidBaseContext
83}
84
85type androidTopDownMutatorContext struct {
86 blueprint.TopDownMutatorContext
87 androidBaseContextImpl
88}
89
Colin Cross635c3b02016-05-18 15:37:25 -070090type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -070091
Colin Cross635c3b02016-05-18 15:37:25 -070092type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -070093 blueprint.BottomUpMutatorContext
94 androidBaseContext
95}
96
97type androidBottomUpMutatorContext struct {
98 blueprint.BottomUpMutatorContext
99 androidBaseContextImpl
100}
101
Colin Cross1e676be2016-10-12 14:38:15 -0700102func (registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700103 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700104 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700105 actx := &androidBottomUpMutatorContext{
106 BottomUpMutatorContext: ctx,
107 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
108 }
Colin Cross798bfce2016-10-12 14:28:16 -0700109 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700110 }
Colin Cross798bfce2016-10-12 14:28:16 -0700111 }
112 mutator := &mutator{name: name, bottomUpMutator: f}
113 mutators = append(mutators, mutator)
114 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700115}
116
Colin Cross1e676be2016-10-12 14:38:15 -0700117func (registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700118 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700119 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700120 actx := &androidTopDownMutatorContext{
121 TopDownMutatorContext: ctx,
122 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
123 }
Colin Cross798bfce2016-10-12 14:28:16 -0700124 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700125 }
Colin Cross798bfce2016-10-12 14:28:16 -0700126 }
127 mutator := &mutator{name: name, topDownMutator: f}
128 mutators = append(mutators, mutator)
129 return mutator
130}
131
132type MutatorHandle interface {
133 Parallel() MutatorHandle
134}
135
136func (mutator *mutator) Parallel() MutatorHandle {
137 mutator.parallel = true
138 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700139}
Colin Cross1e676be2016-10-12 14:38:15 -0700140
141func depsMutator(ctx BottomUpMutatorContext) {
142 if m, ok := ctx.Module().(Module); ok {
143 m.DepsMutator(ctx)
144 }
145}