blob: 66a1bad13c7d52cd8ee6be2cbd0f13a33c59a870 [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 Gastonaf3cc2d2017-09-27 17:01:44 -070021// Phases:
22// run Pre-arch mutators
23// run archMutator
24// run Pre-deps mutators
25// run depsMutator
26// run PostDeps mutators
27// continue on to GenerateAndroidBuildActions
Colin Cross1e676be2016-10-12 14:38:15 -070028
Colin Cross795c3772017-03-16 16:50:10 -070029func registerMutatorsToContext(ctx *blueprint.Context, mutators []*mutator) {
30 for _, t := range mutators {
31 var handle blueprint.MutatorHandle
32 if t.bottomUpMutator != nil {
33 handle = ctx.RegisterBottomUpMutator(t.name, t.bottomUpMutator)
34 } else if t.topDownMutator != nil {
35 handle = ctx.RegisterTopDownMutator(t.name, t.topDownMutator)
36 }
37 if t.parallel {
38 handle.Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070039 }
40 }
Colin Cross1e676be2016-10-12 14:38:15 -070041}
42
Colin Crosscec81712017-07-13 14:43:27 -070043func registerMutators(ctx *blueprint.Context, preArch, preDeps, postDeps []RegisterMutatorFunc) {
44 mctx := &registerMutatorsContext{}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080045
46 register := func(funcs []RegisterMutatorFunc) {
47 for _, f := range funcs {
Colin Crosscec81712017-07-13 14:43:27 -070048 f(mctx)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080049 }
50 }
51
Colin Crosscec81712017-07-13 14:43:27 -070052 register(preArch)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080053
Colin Crosscec81712017-07-13 14:43:27 -070054 register(preDeps)
55
56 mctx.BottomUp("deps", depsMutator).Parallel()
57
58 register(postDeps)
59
60 registerMutatorsToContext(ctx, mctx.mutators)
Colin Cross795c3772017-03-16 16:50:10 -070061}
62
63type registerMutatorsContext struct {
64 mutators []*mutator
65}
Colin Cross1e676be2016-10-12 14:38:15 -070066
67type RegisterMutatorsContext interface {
68 TopDown(name string, m AndroidTopDownMutator) MutatorHandle
69 BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle
70}
71
72type RegisterMutatorFunc func(RegisterMutatorsContext)
73
Colin Crosscec81712017-07-13 14:43:27 -070074var preArch = []RegisterMutatorFunc{
75 func(ctx RegisterMutatorsContext) {
76 ctx.TopDown("load_hooks", loadHookMutator).Parallel()
77 },
Colin Cross5ea9bcc2017-07-27 15:41:32 -070078 RegisterPrebuiltsPreArchMutators,
Colin Cross89536d42017-07-07 14:35:50 -070079 RegisterDefaultsPreArchMutators,
Colin Crosscec81712017-07-13 14:43:27 -070080}
81
Colin Crossae4c6182017-09-15 17:33:55 -070082func registerArchMutator(ctx RegisterMutatorsContext) {
83 ctx.BottomUp("arch", archMutator).Parallel()
84 ctx.TopDown("arch_hooks", archHookMutator).Parallel()
85}
86
Colin Crosscec81712017-07-13 14:43:27 -070087var preDeps = []RegisterMutatorFunc{
Colin Crossae4c6182017-09-15 17:33:55 -070088 registerArchMutator,
Colin Crosscec81712017-07-13 14:43:27 -070089}
90
91var postDeps = []RegisterMutatorFunc{
Colin Cross5ea9bcc2017-07-27 15:41:32 -070092 RegisterPrebuiltsPostDepsMutators,
Colin Crosscec81712017-07-13 14:43:27 -070093}
Colin Cross1e676be2016-10-12 14:38:15 -070094
95func PreArchMutators(f RegisterMutatorFunc) {
96 preArch = append(preArch, f)
97}
98
99func PreDepsMutators(f RegisterMutatorFunc) {
100 preDeps = append(preDeps, f)
101}
102
103func PostDepsMutators(f RegisterMutatorFunc) {
104 postDeps = append(postDeps, f)
105}
106
Colin Cross635c3b02016-05-18 15:37:25 -0700107type AndroidTopDownMutator func(TopDownMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700108
Colin Cross635c3b02016-05-18 15:37:25 -0700109type TopDownMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700110 blueprint.TopDownMutatorContext
111 androidBaseContext
112}
113
114type androidTopDownMutatorContext struct {
115 blueprint.TopDownMutatorContext
116 androidBaseContextImpl
117}
118
Colin Cross635c3b02016-05-18 15:37:25 -0700119type AndroidBottomUpMutator func(BottomUpMutatorContext)
Colin Cross6362e272015-10-29 15:25:03 -0700120
Colin Cross635c3b02016-05-18 15:37:25 -0700121type BottomUpMutatorContext interface {
Colin Cross6362e272015-10-29 15:25:03 -0700122 blueprint.BottomUpMutatorContext
123 androidBaseContext
124}
125
126type androidBottomUpMutatorContext struct {
127 blueprint.BottomUpMutatorContext
128 androidBaseContextImpl
129}
130
Colin Cross795c3772017-03-16 16:50:10 -0700131func (x *registerMutatorsContext) BottomUp(name string, m AndroidBottomUpMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700132 f := func(ctx blueprint.BottomUpMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700133 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700134 actx := &androidBottomUpMutatorContext{
135 BottomUpMutatorContext: ctx,
136 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
137 }
Colin Cross798bfce2016-10-12 14:28:16 -0700138 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700139 }
Colin Cross798bfce2016-10-12 14:28:16 -0700140 }
141 mutator := &mutator{name: name, bottomUpMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700142 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700143 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700144}
145
Colin Cross795c3772017-03-16 16:50:10 -0700146func (x *registerMutatorsContext) TopDown(name string, m AndroidTopDownMutator) MutatorHandle {
Colin Cross798bfce2016-10-12 14:28:16 -0700147 f := func(ctx blueprint.TopDownMutatorContext) {
Colin Cross635c3b02016-05-18 15:37:25 -0700148 if a, ok := ctx.Module().(Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700149 actx := &androidTopDownMutatorContext{
150 TopDownMutatorContext: ctx,
151 androidBaseContextImpl: a.base().androidBaseContextFactory(ctx),
152 }
Colin Cross798bfce2016-10-12 14:28:16 -0700153 m(actx)
Colin Cross6362e272015-10-29 15:25:03 -0700154 }
Colin Cross798bfce2016-10-12 14:28:16 -0700155 }
156 mutator := &mutator{name: name, topDownMutator: f}
Colin Cross795c3772017-03-16 16:50:10 -0700157 x.mutators = append(x.mutators, mutator)
Colin Cross798bfce2016-10-12 14:28:16 -0700158 return mutator
159}
160
161type MutatorHandle interface {
162 Parallel() MutatorHandle
163}
164
165func (mutator *mutator) Parallel() MutatorHandle {
166 mutator.parallel = true
167 return mutator
Colin Cross6362e272015-10-29 15:25:03 -0700168}
Colin Cross1e676be2016-10-12 14:38:15 -0700169
170func depsMutator(ctx BottomUpMutatorContext) {
171 if m, ok := ctx.Module().(Module); ok {
172 m.DepsMutator(ctx)
173 }
174}