blob: d179f9d4b9a81190694d5f499c7ba2c7b4b4ae93 [file] [log] [blame]
Colin Crossdc35e212019-06-06 16:13:11 -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
15package android
16
17import (
Colin Crossdc35e212019-06-06 16:13:11 -070018 "reflect"
19 "testing"
20
21 "github.com/google/blueprint/proptools"
22)
23
24type mutatorTestModule struct {
25 ModuleBase
26 props struct {
Colin Cross9a362232019-07-01 15:32:45 -070027 Deps_missing_deps []string
28 Mutator_missing_deps []string
Colin Crossdc35e212019-06-06 16:13:11 -070029 }
30
31 missingDeps []string
32}
33
34func mutatorTestModuleFactory() Module {
35 module := &mutatorTestModule{}
36 module.AddProperties(&module.props)
37 InitAndroidModule(module)
38 return module
39}
40
41func (m *mutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
42 ctx.Build(pctx, BuildParams{
43 Rule: Touch,
44 Output: PathForModuleOut(ctx, "output"),
45 })
46
47 m.missingDeps = ctx.GetMissingDependencies()
48}
49
50func (m *mutatorTestModule) DepsMutator(ctx BottomUpMutatorContext) {
Colin Cross9a362232019-07-01 15:32:45 -070051 ctx.AddDependency(ctx.Module(), nil, m.props.Deps_missing_deps...)
Colin Crossdc35e212019-06-06 16:13:11 -070052}
53
54func addMissingDependenciesMutator(ctx TopDownMutatorContext) {
Colin Cross9a362232019-07-01 15:32:45 -070055 ctx.AddMissingDependencies(ctx.Module().(*mutatorTestModule).props.Mutator_missing_deps)
Colin Crossdc35e212019-06-06 16:13:11 -070056}
57
58func TestMutatorAddMissingDependencies(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -080059 bp := `
60 test {
61 name: "foo",
62 deps_missing_deps: ["regular_missing_dep"],
63 mutator_missing_deps: ["added_missing_dep"],
64 }
65 `
66
67 config := TestConfig(buildDir, nil, bp, nil)
Colin Crossdc35e212019-06-06 16:13:11 -070068 config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
69
70 ctx := NewTestContext()
71 ctx.SetAllowMissingDependencies(true)
72
Colin Cross4b49b762019-11-22 15:25:03 -080073 ctx.RegisterModuleType("test", mutatorTestModuleFactory)
Colin Crossdc35e212019-06-06 16:13:11 -070074 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
75 ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator)
76 })
77
Colin Cross98be1bb2019-12-13 20:41:13 -080078 ctx.Register(config)
Colin Crossdc35e212019-06-06 16:13:11 -070079 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
80 FailIfErrored(t, errs)
81 _, errs = ctx.PrepareBuildActions(config)
82 FailIfErrored(t, errs)
83
84 foo := ctx.ModuleForTests("foo", "").Module().(*mutatorTestModule)
85
86 if g, w := foo.missingDeps, []string{"added_missing_dep", "regular_missing_dep"}; !reflect.DeepEqual(g, w) {
87 t.Errorf("want foo missing deps %q, got %q", w, g)
88 }
89}
Colin Cross9a362232019-07-01 15:32:45 -070090
91func TestModuleString(t *testing.T) {
92 ctx := NewTestContext()
93
94 var moduleStrings []string
95
96 ctx.PreArchMutators(func(ctx RegisterMutatorsContext) {
97 ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) {
98 moduleStrings = append(moduleStrings, ctx.Module().String())
99 ctx.CreateVariations("a", "b")
100 })
101 ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) {
102 moduleStrings = append(moduleStrings, ctx.Module().String())
103 ctx.Rename(ctx.Module().base().Name() + "_renamed1")
104 })
105 })
106
107 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
108 ctx.BottomUp("pre_deps", func(ctx BottomUpMutatorContext) {
109 moduleStrings = append(moduleStrings, ctx.Module().String())
110 ctx.CreateVariations("c", "d")
111 })
112 })
113
114 ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) {
115 ctx.BottomUp("post_deps", func(ctx BottomUpMutatorContext) {
116 moduleStrings = append(moduleStrings, ctx.Module().String())
117 ctx.CreateLocalVariations("e", "f")
118 })
119 ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) {
120 moduleStrings = append(moduleStrings, ctx.Module().String())
121 ctx.Rename(ctx.Module().base().Name() + "_renamed2")
122 })
123 ctx.BottomUp("final", func(ctx BottomUpMutatorContext) {
124 moduleStrings = append(moduleStrings, ctx.Module().String())
125 })
126 })
127
Colin Cross4b49b762019-11-22 15:25:03 -0800128 ctx.RegisterModuleType("test", mutatorTestModuleFactory)
Colin Cross9a362232019-07-01 15:32:45 -0700129
130 bp := `
131 test {
132 name: "foo",
133 }
134 `
135
Colin Cross98be1bb2019-12-13 20:41:13 -0800136 config := TestConfig(buildDir, nil, bp, nil)
Colin Cross9a362232019-07-01 15:32:45 -0700137
Colin Cross98be1bb2019-12-13 20:41:13 -0800138 ctx.Register(config)
Colin Cross9a362232019-07-01 15:32:45 -0700139
140 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
141 FailIfErrored(t, errs)
142 _, errs = ctx.PrepareBuildActions(config)
143 FailIfErrored(t, errs)
144
145 want := []string{
146 // Initial name.
147 "foo{}",
148
149 // After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order).
150 "foo{pre_arch:b}",
151 "foo{pre_arch:a}",
152
153 // After rename_top_down.
154 "foo_renamed1{pre_arch:a}",
155 "foo_renamed1{pre_arch:b}",
156
157 // After pre_deps.
158 "foo_renamed1{pre_arch:a,pre_deps:c}",
159 "foo_renamed1{pre_arch:a,pre_deps:d}",
160 "foo_renamed1{pre_arch:b,pre_deps:c}",
161 "foo_renamed1{pre_arch:b,pre_deps:d}",
162
163 // After post_deps.
164 "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}",
165 "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}",
166 "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}",
167 "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}",
168 "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}",
169 "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}",
170 "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}",
171 "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}",
172
173 // After rename_bottom_up.
174 "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:e}",
175 "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:f}",
176 "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:e}",
177 "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:f}",
178 "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:e}",
179 "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:f}",
180 "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:e}",
181 "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}",
182 }
183
184 if !reflect.DeepEqual(moduleStrings, want) {
185 t.Errorf("want module String() values:\n%q\ngot:\n%q", want, moduleStrings)
186 }
187}