Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [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 ( |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 18 | "fmt" |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 19 | "io" |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | type customModule struct { |
| 25 | ModuleBase |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 26 | data AndroidMkData |
| 27 | distFiles TaggedDistFiles |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | func (m *customModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 31 | m.distFiles = m.GenerateTaggedDistFiles(ctx) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | func (m *customModule) AndroidMk() AndroidMkData { |
| 35 | return AndroidMkData{ |
| 36 | Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) { |
| 37 | m.data = data |
| 38 | }, |
| 39 | } |
| 40 | } |
| 41 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 42 | func (m *customModule) OutputFiles(tag string) (Paths, error) { |
| 43 | switch tag { |
| 44 | case "": |
| 45 | return PathsForTesting("one.out"), nil |
| 46 | case ".multiple": |
| 47 | return PathsForTesting("two.out", "three/four.out"), nil |
Jingwen Chen | 8481186 | 2020-07-21 11:32:19 +0000 | [diff] [blame] | 48 | case ".another-tag": |
| 49 | return PathsForTesting("another.out"), nil |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 50 | default: |
| 51 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func (m *customModule) AndroidMkEntries() []AndroidMkEntries { |
| 56 | return []AndroidMkEntries{ |
| 57 | { |
| 58 | Class: "CUSTOM_MODULE", |
| 59 | DistFiles: m.distFiles, |
| 60 | }, |
| 61 | } |
| 62 | } |
| 63 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 64 | func customModuleFactory() Module { |
| 65 | module := &customModule{} |
| 66 | InitAndroidModule(module) |
| 67 | return module |
| 68 | } |
| 69 | |
| 70 | func TestAndroidMkSingleton_PassesUpdatedAndroidMkDataToCustomCallback(t *testing.T) { |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 71 | bp := ` |
| 72 | custom { |
| 73 | name: "foo", |
| 74 | required: ["bar"], |
| 75 | host_required: ["baz"], |
| 76 | target_required: ["qux"], |
| 77 | } |
| 78 | ` |
| 79 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 80 | config := TestConfig(buildDir, nil, bp, nil) |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 81 | config.katiEnabled = true // Enable androidmk Singleton |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 82 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 83 | ctx := NewTestContext(config) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 84 | ctx.RegisterSingletonType("androidmk", AndroidMkSingleton) |
| 85 | ctx.RegisterModuleType("custom", customModuleFactory) |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 86 | ctx.Register() |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 87 | |
| 88 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 89 | FailIfErrored(t, errs) |
| 90 | _, errs = ctx.PrepareBuildActions(config) |
| 91 | FailIfErrored(t, errs) |
| 92 | |
| 93 | m := ctx.ModuleForTests("foo", "").Module().(*customModule) |
| 94 | |
| 95 | assertEqual := func(expected interface{}, actual interface{}) { |
| 96 | if !reflect.DeepEqual(expected, actual) { |
| 97 | t.Errorf("%q expected, but got %q", expected, actual) |
| 98 | } |
| 99 | } |
| 100 | assertEqual([]string{"bar"}, m.data.Required) |
| 101 | assertEqual([]string{"baz"}, m.data.Host_required) |
| 102 | assertEqual([]string{"qux"}, m.data.Target_required) |
| 103 | } |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 104 | |
| 105 | func TestGetDistForGoals(t *testing.T) { |
| 106 | testCases := []struct { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 107 | name string |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 108 | bp string |
| 109 | expectedAndroidMkLines []string |
| 110 | }{ |
| 111 | { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 112 | name: "dist-without-tag", |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 113 | bp: ` |
| 114 | custom { |
| 115 | name: "foo", |
| 116 | dist: { |
| 117 | targets: ["my_goal"] |
| 118 | } |
| 119 | } |
| 120 | `, |
| 121 | expectedAndroidMkLines: []string{ |
| 122 | ".PHONY: my_goal\n", |
| 123 | "$(call dist-for-goals,my_goal,one.out:one.out)\n", |
| 124 | }, |
| 125 | }, |
| 126 | { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 127 | name: "dist-with-tag", |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 128 | bp: ` |
| 129 | custom { |
| 130 | name: "foo", |
Jingwen Chen | 8481186 | 2020-07-21 11:32:19 +0000 | [diff] [blame] | 131 | dist: { |
| 132 | targets: ["my_goal"], |
| 133 | tag: ".another-tag", |
| 134 | } |
| 135 | } |
| 136 | `, |
| 137 | expectedAndroidMkLines: []string{ |
| 138 | ".PHONY: my_goal\n", |
| 139 | "$(call dist-for-goals,my_goal,another.out:another.out)\n", |
| 140 | }, |
| 141 | }, |
| 142 | { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 143 | name: "dists-with-tag", |
Jingwen Chen | 8481186 | 2020-07-21 11:32:19 +0000 | [diff] [blame] | 144 | bp: ` |
| 145 | custom { |
| 146 | name: "foo", |
| 147 | dists: [ |
| 148 | { |
| 149 | targets: ["my_goal"], |
| 150 | tag: ".another-tag", |
| 151 | }, |
| 152 | ], |
| 153 | } |
| 154 | `, |
| 155 | expectedAndroidMkLines: []string{ |
| 156 | ".PHONY: my_goal\n", |
| 157 | "$(call dist-for-goals,my_goal,another.out:another.out)\n", |
| 158 | }, |
| 159 | }, |
| 160 | { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 161 | name: "multiple-dists-with-and-without-tag", |
Jingwen Chen | 8481186 | 2020-07-21 11:32:19 +0000 | [diff] [blame] | 162 | bp: ` |
| 163 | custom { |
| 164 | name: "foo", |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 165 | dists: [ |
| 166 | { |
| 167 | targets: ["my_goal"], |
| 168 | }, |
| 169 | { |
| 170 | targets: ["my_second_goal", "my_third_goal"], |
| 171 | }, |
| 172 | ], |
| 173 | } |
| 174 | `, |
| 175 | expectedAndroidMkLines: []string{ |
| 176 | ".PHONY: my_goal\n", |
| 177 | "$(call dist-for-goals,my_goal,one.out:one.out)\n", |
| 178 | ".PHONY: my_second_goal my_third_goal\n", |
| 179 | "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n", |
| 180 | }, |
| 181 | }, |
| 182 | { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 183 | name: "dist-plus-dists-without-tags", |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 184 | bp: ` |
| 185 | custom { |
| 186 | name: "foo", |
| 187 | dist: { |
| 188 | targets: ["my_goal"], |
| 189 | }, |
| 190 | dists: [ |
| 191 | { |
| 192 | targets: ["my_second_goal", "my_third_goal"], |
| 193 | }, |
| 194 | ], |
| 195 | } |
| 196 | `, |
| 197 | expectedAndroidMkLines: []string{ |
| 198 | ".PHONY: my_second_goal my_third_goal\n", |
| 199 | "$(call dist-for-goals,my_second_goal my_third_goal,one.out:one.out)\n", |
| 200 | ".PHONY: my_goal\n", |
| 201 | "$(call dist-for-goals,my_goal,one.out:one.out)\n", |
| 202 | }, |
| 203 | }, |
| 204 | { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 205 | name: "dist-plus-dists-with-tags", |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 206 | bp: ` |
| 207 | custom { |
| 208 | name: "foo", |
| 209 | dist: { |
| 210 | targets: ["my_goal", "my_other_goal"], |
| 211 | tag: ".multiple", |
| 212 | }, |
| 213 | dists: [ |
| 214 | { |
| 215 | targets: ["my_second_goal"], |
| 216 | tag: ".multiple", |
| 217 | }, |
| 218 | { |
| 219 | targets: ["my_third_goal"], |
| 220 | dir: "test/dir", |
| 221 | }, |
| 222 | { |
| 223 | targets: ["my_fourth_goal"], |
| 224 | suffix: ".suffix", |
| 225 | }, |
| 226 | { |
| 227 | targets: ["my_fifth_goal"], |
| 228 | dest: "new-name", |
| 229 | }, |
| 230 | { |
| 231 | targets: ["my_sixth_goal"], |
| 232 | dest: "new-name", |
| 233 | dir: "some/dir", |
| 234 | suffix: ".suffix", |
| 235 | }, |
| 236 | ], |
| 237 | } |
| 238 | `, |
| 239 | expectedAndroidMkLines: []string{ |
| 240 | ".PHONY: my_second_goal\n", |
| 241 | "$(call dist-for-goals,my_second_goal,two.out:two.out)\n", |
| 242 | "$(call dist-for-goals,my_second_goal,three/four.out:four.out)\n", |
| 243 | ".PHONY: my_third_goal\n", |
| 244 | "$(call dist-for-goals,my_third_goal,one.out:test/dir/one.out)\n", |
| 245 | ".PHONY: my_fourth_goal\n", |
| 246 | "$(call dist-for-goals,my_fourth_goal,one.out:one.suffix.out)\n", |
| 247 | ".PHONY: my_fifth_goal\n", |
| 248 | "$(call dist-for-goals,my_fifth_goal,one.out:new-name)\n", |
| 249 | ".PHONY: my_sixth_goal\n", |
| 250 | "$(call dist-for-goals,my_sixth_goal,one.out:some/dir/new-name.suffix)\n", |
| 251 | ".PHONY: my_goal my_other_goal\n", |
| 252 | "$(call dist-for-goals,my_goal my_other_goal,two.out:two.out)\n", |
| 253 | "$(call dist-for-goals,my_goal my_other_goal,three/four.out:four.out)\n", |
| 254 | }, |
| 255 | }, |
| 256 | } |
| 257 | |
| 258 | for _, testCase := range testCases { |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 259 | t.Run(testCase.name, func(t *testing.T) { |
| 260 | config := TestConfig(buildDir, nil, testCase.bp, nil) |
| 261 | config.katiEnabled = true // Enable androidmk Singleton |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 262 | |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 263 | ctx := NewTestContext(config) |
| 264 | ctx.RegisterSingletonType("androidmk", AndroidMkSingleton) |
| 265 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 266 | ctx.Register() |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 267 | |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 268 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 269 | FailIfErrored(t, errs) |
| 270 | _, errs = ctx.PrepareBuildActions(config) |
| 271 | FailIfErrored(t, errs) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 272 | |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 273 | module := ctx.ModuleForTests("foo", "").Module().(*customModule) |
| 274 | entries := AndroidMkEntriesForTest(t, config, "", module) |
| 275 | if len(entries) != 1 { |
| 276 | t.Errorf("Expected a single AndroidMk entry, got %d", len(entries)) |
| 277 | } |
| 278 | androidMkLines := entries[0].GetDistForGoals(module) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 279 | |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 280 | if len(androidMkLines) != len(testCase.expectedAndroidMkLines) { |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 281 | t.Errorf( |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 282 | "Expected %d AndroidMk lines, got %d:\n%v", |
| 283 | len(testCase.expectedAndroidMkLines), |
| 284 | len(androidMkLines), |
| 285 | androidMkLines, |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 286 | ) |
| 287 | } |
Paul Duffin | 0cc047a | 2020-11-25 16:39:30 +0000 | [diff] [blame^] | 288 | for idx, line := range androidMkLines { |
| 289 | expectedLine := testCase.expectedAndroidMkLines[idx] |
| 290 | if line != expectedLine { |
| 291 | t.Errorf( |
| 292 | "Expected AndroidMk line to be '%s', got '%s'", |
| 293 | expectedLine, |
| 294 | line, |
| 295 | ) |
| 296 | } |
| 297 | } |
| 298 | }) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 299 | } |
| 300 | } |