Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | type pathDepsMutatorTestModule struct { |
| 23 | ModuleBase |
| 24 | props struct { |
| 25 | Foo string `android:"path"` |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 26 | Bar []string `android:"path,arch_variant"` |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 27 | Baz *string `android:"path"` |
| 28 | Qux string |
| 29 | } |
| 30 | |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 31 | // A second property struct with a duplicate property name |
| 32 | props2 struct { |
| 33 | Foo string `android:"path"` |
| 34 | } |
| 35 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 36 | sourceDeps []string |
| 37 | } |
| 38 | |
| 39 | func pathDepsMutatorTestModuleFactory() Module { |
| 40 | module := &pathDepsMutatorTestModule{} |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 41 | module.AddProperties(&module.props, &module.props2) |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 42 | InitAndroidArchModule(module, DeviceSupported, MultilibBoth) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 43 | return module |
| 44 | } |
| 45 | |
| 46 | func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 47 | ctx.VisitDirectDeps(func(dep Module) { |
| 48 | if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok { |
| 49 | p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep)) |
| 50 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 51 | }) |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 52 | |
| 53 | if p.props.Foo != "" { |
| 54 | // Make sure there is only one dependency on a module listed in a property present in multiple property structs |
| 55 | if ctx.GetDirectDepWithTag(SrcIsModule(p.props.Foo), sourceOrOutputDepTag("")) == nil { |
| 56 | ctx.ModuleErrorf("GetDirectDepWithTag failed") |
| 57 | } |
| 58 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func TestPathDepsMutator(t *testing.T) { |
| 62 | tests := []struct { |
| 63 | name string |
| 64 | bp string |
| 65 | deps []string |
| 66 | }{ |
| 67 | { |
| 68 | name: "all", |
| 69 | bp: ` |
| 70 | test { |
| 71 | name: "foo", |
| 72 | foo: ":a", |
| 73 | bar: [":b"], |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 74 | baz: ":c{.bar}", |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 75 | qux: ":d", |
| 76 | }`, |
| 77 | deps: []string{"a", "b", "c"}, |
| 78 | }, |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 79 | { |
| 80 | name: "arch variant", |
| 81 | bp: ` |
| 82 | test { |
| 83 | name: "foo", |
| 84 | arch: { |
| 85 | arm64: { |
| 86 | bar: [":a"], |
| 87 | }, |
| 88 | arm: { |
| 89 | bar: [":b"], |
| 90 | }, |
| 91 | }, |
| 92 | bar: [":c"], |
| 93 | }`, |
| 94 | deps: []string{"c", "a"}, |
| 95 | }, |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 98 | for _, test := range tests { |
| 99 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 100 | bp := test.bp + ` |
| 101 | filegroup { |
| 102 | name: "a", |
| 103 | } |
| 104 | |
| 105 | filegroup { |
| 106 | name: "b", |
| 107 | } |
| 108 | |
| 109 | filegroup { |
| 110 | name: "c", |
| 111 | } |
| 112 | |
| 113 | filegroup { |
| 114 | name: "d", |
| 115 | } |
| 116 | ` |
| 117 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 118 | config := TestArchConfig(buildDir, nil, bp, nil) |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 119 | ctx := NewTestArchContext(config) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 120 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 121 | ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory) |
| 122 | ctx.RegisterModuleType("filegroup", FileGroupFactory) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 123 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 124 | ctx.Register() |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 125 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 126 | FailIfErrored(t, errs) |
| 127 | _, errs = ctx.PrepareBuildActions(config) |
| 128 | FailIfErrored(t, errs) |
| 129 | |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 130 | m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 131 | |
| 132 | if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) { |
| 133 | t.Errorf("want deps %q, got %q", w, g) |
| 134 | } |
| 135 | }) |
| 136 | } |
| 137 | } |