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