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 |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame^] | 29 | V *struct { |
| 30 | W string `android:"path"` |
| 31 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 34 | // A second property struct with a duplicate property name |
| 35 | props2 struct { |
| 36 | Foo string `android:"path"` |
| 37 | } |
| 38 | |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 39 | // nested slices of struct |
| 40 | props3 struct { |
| 41 | X []struct { |
| 42 | Y []struct { |
| 43 | Z []string `android:"path"` |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 48 | sourceDeps []string |
| 49 | } |
| 50 | |
| 51 | func pathDepsMutatorTestModuleFactory() Module { |
| 52 | module := &pathDepsMutatorTestModule{} |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 53 | module.AddProperties(&module.props, &module.props2, &module.props3) |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 54 | InitAndroidArchModule(module, DeviceSupported, MultilibBoth) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 55 | return module |
| 56 | } |
| 57 | |
| 58 | func (p *pathDepsMutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 59 | ctx.VisitDirectDeps(func(dep Module) { |
| 60 | if _, ok := ctx.OtherModuleDependencyTag(dep).(sourceOrOutputDependencyTag); ok { |
| 61 | p.sourceDeps = append(p.sourceDeps, ctx.OtherModuleName(dep)) |
| 62 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 63 | }) |
Colin Cross | 527f3e5 | 2019-07-15 13:35:21 -0700 | [diff] [blame] | 64 | |
| 65 | if p.props.Foo != "" { |
| 66 | // Make sure there is only one dependency on a module listed in a property present in multiple property structs |
| 67 | if ctx.GetDirectDepWithTag(SrcIsModule(p.props.Foo), sourceOrOutputDepTag("")) == nil { |
| 68 | ctx.ModuleErrorf("GetDirectDepWithTag failed") |
| 69 | } |
| 70 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | func TestPathDepsMutator(t *testing.T) { |
| 74 | tests := []struct { |
| 75 | name string |
| 76 | bp string |
| 77 | deps []string |
| 78 | }{ |
| 79 | { |
| 80 | name: "all", |
| 81 | bp: ` |
| 82 | test { |
| 83 | name: "foo", |
| 84 | foo: ":a", |
| 85 | bar: [":b"], |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 86 | baz: ":c{.bar}", |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 87 | qux: ":d", |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 88 | x: [ |
| 89 | { |
| 90 | y: [ |
| 91 | { |
| 92 | z: [":x", ":y"], |
| 93 | }, |
| 94 | { |
| 95 | z: [":z"], |
| 96 | }, |
| 97 | ], |
| 98 | }, |
| 99 | ], |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame^] | 100 | v: { |
| 101 | w: ":w", |
| 102 | }, |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 103 | }`, |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame^] | 104 | deps: []string{"a", "b", "c", "w", "x", "y", "z"}, |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 105 | }, |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 106 | { |
| 107 | name: "arch variant", |
| 108 | bp: ` |
| 109 | test { |
| 110 | name: "foo", |
| 111 | arch: { |
| 112 | arm64: { |
| 113 | bar: [":a"], |
| 114 | }, |
| 115 | arm: { |
| 116 | bar: [":b"], |
| 117 | }, |
| 118 | }, |
| 119 | bar: [":c"], |
| 120 | }`, |
| 121 | deps: []string{"c", "a"}, |
| 122 | }, |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 125 | for _, test := range tests { |
| 126 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 127 | bp := test.bp + ` |
| 128 | filegroup { |
| 129 | name: "a", |
| 130 | } |
| 131 | |
| 132 | filegroup { |
| 133 | name: "b", |
| 134 | } |
| 135 | |
| 136 | filegroup { |
| 137 | name: "c", |
| 138 | } |
| 139 | |
| 140 | filegroup { |
| 141 | name: "d", |
| 142 | } |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 143 | |
| 144 | filegroup { |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame^] | 145 | name: "w", |
| 146 | } |
| 147 | |
| 148 | filegroup { |
Jiyong Park | 66dd5c0 | 2021-02-24 01:22:57 +0900 | [diff] [blame] | 149 | name: "x", |
| 150 | } |
| 151 | |
| 152 | filegroup { |
| 153 | name: "y", |
| 154 | } |
| 155 | |
| 156 | filegroup { |
| 157 | name: "z", |
| 158 | } |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 159 | ` |
| 160 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 161 | config := TestArchConfig(buildDir, nil, bp, nil) |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 162 | ctx := NewTestArchContext(config) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 163 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 164 | ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory) |
| 165 | ctx.RegisterModuleType("filegroup", FileGroupFactory) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 166 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 167 | ctx.Register() |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 168 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 169 | FailIfErrored(t, errs) |
| 170 | _, errs = ctx.PrepareBuildActions(config) |
| 171 | FailIfErrored(t, errs) |
| 172 | |
Colin Cross | a3a9741 | 2019-03-18 12:24:29 -0700 | [diff] [blame] | 173 | m := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*pathDepsMutatorTestModule) |
Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 174 | |
| 175 | if g, w := m.sourceDeps, test.deps; !reflect.DeepEqual(g, w) { |
| 176 | t.Errorf("want deps %q, got %q", w, g) |
| 177 | } |
| 178 | }) |
| 179 | } |
| 180 | } |