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" |
Dan Willemsen | def7b5d | 2021-10-17 00:22:33 -0700 | [diff] [blame] | 21 | "runtime" |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 22 | "strings" |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 23 | "testing" |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint/proptools" |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type customModule struct { |
| 29 | ModuleBase |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 30 | |
| 31 | properties struct { |
| 32 | Default_dist_files *string |
| 33 | Dist_output_file *bool |
| 34 | } |
| 35 | |
| 36 | data AndroidMkData |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 37 | outputFile OptionalPath |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 38 | } |
| 39 | |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 40 | const ( |
| 41 | defaultDistFiles_None = "none" |
| 42 | defaultDistFiles_Default = "default" |
| 43 | defaultDistFiles_Tagged = "tagged" |
| 44 | ) |
| 45 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 46 | func (m *customModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 47 | |
mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 48 | var defaultDistPaths Paths |
Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 49 | |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 50 | // If the dist_output_file: true then create an output file that is stored in |
| 51 | // the OutputFile property of the AndroidMkEntry. |
| 52 | if proptools.BoolDefault(m.properties.Dist_output_file, true) { |
Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 53 | path := PathForTesting("dist-output-file.out") |
| 54 | m.outputFile = OptionalPathForPath(path) |
| 55 | |
| 56 | // Previous code would prioritize the DistFiles property over the OutputFile |
| 57 | // property in AndroidMkEntry when determining the default dist paths. |
| 58 | // Setting this first allows it to be overridden based on the |
| 59 | // default_dist_files setting replicating that previous behavior. |
mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 60 | defaultDistPaths = Paths{path} |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Based on the setting of the default_dist_files property possibly create a |
| 64 | // TaggedDistFiles structure that will be stored in the DistFiles property of |
| 65 | // the AndroidMkEntry. |
| 66 | defaultDistFiles := proptools.StringDefault(m.properties.Default_dist_files, defaultDistFiles_Tagged) |
| 67 | switch defaultDistFiles { |
| 68 | case defaultDistFiles_None: |
mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 69 | m.setOutputFiles(ctx, defaultDistPaths) |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 70 | |
| 71 | case defaultDistFiles_Default: |
Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 72 | path := PathForTesting("default-dist.out") |
mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 73 | defaultDistPaths = Paths{path} |
| 74 | m.setOutputFiles(ctx, defaultDistPaths) |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 75 | |
| 76 | case defaultDistFiles_Tagged: |
Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 77 | // Module types that set AndroidMkEntry.DistFiles to the result of calling |
| 78 | // GenerateTaggedDistFiles(ctx) relied on no tag being treated as "" which |
mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 79 | // meant that the default dist paths would be the same as empty-string-tag |
| 80 | // output files. In order to preserve that behavior when treating no tag |
| 81 | // as being equal to DefaultDistTag this ensures that DefaultDistTag output |
| 82 | // will be the same as empty-string-tag output. |
| 83 | defaultDistPaths = PathsForTesting("one.out") |
| 84 | m.setOutputFiles(ctx, defaultDistPaths) |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 85 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 86 | } |
| 87 | |
mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 88 | func (m *customModule) setOutputFiles(ctx ModuleContext, defaultDistPaths Paths) { |
| 89 | ctx.SetOutputFiles(PathsForTesting("one.out"), "") |
| 90 | ctx.SetOutputFiles(PathsForTesting("two.out", "three/four.out"), ".multiple") |
| 91 | ctx.SetOutputFiles(PathsForTesting("another.out"), ".another-tag") |
| 92 | if defaultDistPaths != nil { |
| 93 | ctx.SetOutputFiles(defaultDistPaths, DefaultDistTag) |
| 94 | } |
| 95 | } |
| 96 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 97 | func (m *customModule) AndroidMk() AndroidMkData { |
| 98 | return AndroidMkData{ |
| 99 | Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) { |
| 100 | m.data = data |
| 101 | }, |
| 102 | } |
| 103 | } |
| 104 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 105 | func (m *customModule) AndroidMkEntries() []AndroidMkEntries { |
| 106 | return []AndroidMkEntries{ |
| 107 | { |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 108 | Class: "CUSTOM_MODULE", |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 109 | OutputFile: m.outputFile, |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 110 | }, |
| 111 | } |
| 112 | } |
| 113 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 114 | func customModuleFactory() Module { |
| 115 | module := &customModule{} |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 116 | |
| 117 | module.AddProperties(&module.properties) |
| 118 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 119 | InitAndroidModule(module) |
| 120 | return module |
| 121 | } |
| 122 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 123 | // buildContextAndCustomModuleFoo creates a config object, processes the supplied |
Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 124 | // bp module and then returns the config and the custom module called "foo". |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 125 | func buildContextAndCustomModuleFoo(t *testing.T, bp string) (*TestContext, *customModule) { |
Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 126 | t.Helper() |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 127 | result := GroupFixturePreparers( |
Paul Duffin | 9ca14c1 | 2021-03-16 17:30:13 +0000 | [diff] [blame] | 128 | // Enable androidmk Singleton |
| 129 | PrepareForTestWithAndroidMk, |
| 130 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 131 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 132 | }), |
Trevor Radcliffe | 90727f4 | 2022-03-21 19:34:02 +0000 | [diff] [blame] | 133 | FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 134 | variables.DeviceProduct = proptools.StringPtr("bar") |
| 135 | }), |
Paul Duffin | 9ca14c1 | 2021-03-16 17:30:13 +0000 | [diff] [blame] | 136 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 137 | ).RunTest(t) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 138 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame] | 139 | module := result.ModuleForTests(t, "foo", "").Module().(*customModule) |
Paul Duffin | 9ca14c1 | 2021-03-16 17:30:13 +0000 | [diff] [blame] | 140 | return result.TestContext, module |
Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | func TestAndroidMkSingleton_PassesUpdatedAndroidMkDataToCustomCallback(t *testing.T) { |
Dan Willemsen | def7b5d | 2021-10-17 00:22:33 -0700 | [diff] [blame] | 144 | if runtime.GOOS == "darwin" { |
| 145 | // Device modules are not exported on Mac, so this test doesn't work. |
| 146 | t.SkipNow() |
| 147 | } |
| 148 | |
Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 149 | bp := ` |
| 150 | custom { |
| 151 | name: "foo", |
| 152 | required: ["bar"], |
| 153 | host_required: ["baz"], |
| 154 | target_required: ["qux"], |
| 155 | } |
| 156 | ` |
| 157 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 158 | _, m := buildContextAndCustomModuleFoo(t, bp) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 159 | |
| 160 | assertEqual := func(expected interface{}, actual interface{}) { |
| 161 | if !reflect.DeepEqual(expected, actual) { |
| 162 | t.Errorf("%q expected, but got %q", expected, actual) |
| 163 | } |
| 164 | } |
| 165 | assertEqual([]string{"bar"}, m.data.Required) |
| 166 | assertEqual([]string{"baz"}, m.data.Host_required) |
| 167 | assertEqual([]string{"qux"}, m.data.Target_required) |
| 168 | } |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 169 | |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 170 | func TestGenerateDistContributionsForMake(t *testing.T) { |
| 171 | dc := &distContributions{ |
| 172 | copiesForGoals: []*copiesForGoals{ |
| 173 | { |
| 174 | goals: "my_goal", |
| 175 | copies: []distCopy{ |
| 176 | distCopyForTest("one.out", "one.out"), |
| 177 | distCopyForTest("two.out", "other.out"), |
| 178 | }, |
| 179 | }, |
| 180 | }, |
| 181 | } |
| 182 | |
Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 183 | dc.licenseMetadataFile = PathForTesting("meta_lic") |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 184 | makeOutput := generateDistContributionsForMake(dc) |
| 185 | |
| 186 | assertStringEquals(t, `.PHONY: my_goal |
Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 187 | $(if $(strip $(ALL_TARGETS.one.out.META_LIC)),,$(eval ALL_TARGETS.one.out.META_LIC := meta_lic)) |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 188 | $(call dist-for-goals,my_goal,one.out:one.out) |
Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 189 | $(if $(strip $(ALL_TARGETS.two.out.META_LIC)),,$(eval ALL_TARGETS.two.out.META_LIC := meta_lic)) |
Yu Liu | e70976d | 2024-10-15 20:45:35 +0000 | [diff] [blame] | 190 | $(call dist-for-goals,my_goal,two.out:other.out)`, strings.Join(makeOutput, "\n")) |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 193 | func TestGetDistForGoals(t *testing.T) { |
Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 194 | bp := ` |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 195 | custom { |
| 196 | name: "foo", |
| 197 | dist: { |
| 198 | targets: ["my_goal", "my_other_goal"], |
| 199 | tag: ".multiple", |
| 200 | }, |
| 201 | dists: [ |
| 202 | { |
| 203 | targets: ["my_second_goal"], |
| 204 | tag: ".multiple", |
| 205 | }, |
| 206 | { |
| 207 | targets: ["my_third_goal"], |
| 208 | dir: "test/dir", |
| 209 | }, |
| 210 | { |
| 211 | targets: ["my_fourth_goal"], |
| 212 | suffix: ".suffix", |
| 213 | }, |
| 214 | { |
| 215 | targets: ["my_fifth_goal"], |
| 216 | dest: "new-name", |
| 217 | }, |
| 218 | { |
| 219 | targets: ["my_sixth_goal"], |
| 220 | dest: "new-name", |
| 221 | dir: "some/dir", |
| 222 | suffix: ".suffix", |
| 223 | }, |
| 224 | ], |
| 225 | } |
Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 226 | ` |
| 227 | |
| 228 | expectedAndroidMkLines := []string{ |
Yu Liu | e70976d | 2024-10-15 20:45:35 +0000 | [diff] [blame] | 229 | ".PHONY: my_second_goal", |
| 230 | "$(if $(strip $(ALL_TARGETS.two.out.META_LIC)),,$(eval ALL_TARGETS.two.out.META_LIC := meta_lic))", |
| 231 | "$(call dist-for-goals,my_second_goal,two.out:two.out)", |
| 232 | "$(if $(strip $(ALL_TARGETS.three/four.out.META_LIC)),,$(eval ALL_TARGETS.three/four.out.META_LIC := meta_lic))", |
| 233 | "$(call dist-for-goals,my_second_goal,three/four.out:four.out)", |
| 234 | ".PHONY: my_third_goal", |
| 235 | "$(if $(strip $(ALL_TARGETS.one.out.META_LIC)),,$(eval ALL_TARGETS.one.out.META_LIC := meta_lic))", |
| 236 | "$(call dist-for-goals,my_third_goal,one.out:test/dir/one.out)", |
| 237 | ".PHONY: my_fourth_goal", |
| 238 | "$(if $(strip $(ALL_TARGETS.one.out.META_LIC)),,$(eval ALL_TARGETS.one.out.META_LIC := meta_lic))", |
| 239 | "$(call dist-for-goals,my_fourth_goal,one.out:one.suffix.out)", |
| 240 | ".PHONY: my_fifth_goal", |
| 241 | "$(if $(strip $(ALL_TARGETS.one.out.META_LIC)),,$(eval ALL_TARGETS.one.out.META_LIC := meta_lic))", |
| 242 | "$(call dist-for-goals,my_fifth_goal,one.out:new-name)", |
| 243 | ".PHONY: my_sixth_goal", |
| 244 | "$(if $(strip $(ALL_TARGETS.one.out.META_LIC)),,$(eval ALL_TARGETS.one.out.META_LIC := meta_lic))", |
| 245 | "$(call dist-for-goals,my_sixth_goal,one.out:some/dir/new-name.suffix)", |
| 246 | ".PHONY: my_goal my_other_goal", |
| 247 | "$(if $(strip $(ALL_TARGETS.two.out.META_LIC)),,$(eval ALL_TARGETS.two.out.META_LIC := meta_lic))", |
| 248 | "$(call dist-for-goals,my_goal my_other_goal,two.out:two.out)", |
| 249 | "$(if $(strip $(ALL_TARGETS.three/four.out.META_LIC)),,$(eval ALL_TARGETS.three/four.out.META_LIC := meta_lic))", |
| 250 | "$(call dist-for-goals,my_goal my_other_goal,three/four.out:four.out)", |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 253 | ctx, module := buildContextAndCustomModuleFoo(t, bp) |
| 254 | entries := AndroidMkEntriesForTest(t, ctx, module) |
Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 255 | if len(entries) != 1 { |
| 256 | t.Errorf("Expected a single AndroidMk entry, got %d", len(entries)) |
| 257 | } |
| 258 | androidMkLines := entries[0].GetDistForGoals(module) |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 259 | |
Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 260 | if len(androidMkLines) != len(expectedAndroidMkLines) { |
| 261 | t.Errorf( |
| 262 | "Expected %d AndroidMk lines, got %d:\n%v", |
| 263 | len(expectedAndroidMkLines), |
| 264 | len(androidMkLines), |
| 265 | androidMkLines, |
| 266 | ) |
| 267 | } |
| 268 | for idx, line := range androidMkLines { |
Yu Liu | ec81054 | 2024-08-26 18:09:15 +0000 | [diff] [blame] | 269 | expectedLine := strings.ReplaceAll(expectedAndroidMkLines[idx], "meta_lic", |
| 270 | OtherModuleProviderOrDefault(ctx, module, InstallFilesProvider).LicenseMetadataFile.String()) |
Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 271 | if line != expectedLine { |
| 272 | t.Errorf( |
| 273 | "Expected AndroidMk line to be '%s', got '%s'", |
| 274 | expectedLine, |
| 275 | line, |
| 276 | ) |
| 277 | } |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 280 | |
| 281 | func distCopyForTest(from, to string) distCopy { |
| 282 | return distCopy{PathForTesting(from), to} |
| 283 | } |
| 284 | |
| 285 | func TestGetDistContributions(t *testing.T) { |
| 286 | compareContributions := func(d1 *distContributions, d2 *distContributions) error { |
| 287 | if d1 == nil || d2 == nil { |
| 288 | if d1 != d2 { |
| 289 | return fmt.Errorf("pointer mismatch, expected both to be nil but they were %p and %p", d1, d2) |
| 290 | } else { |
| 291 | return nil |
| 292 | } |
| 293 | } |
| 294 | if expected, actual := len(d1.copiesForGoals), len(d2.copiesForGoals); expected != actual { |
| 295 | return fmt.Errorf("length mismatch, expected %d found %d", expected, actual) |
| 296 | } |
| 297 | |
| 298 | for i, copies1 := range d1.copiesForGoals { |
| 299 | copies2 := d2.copiesForGoals[i] |
| 300 | if expected, actual := copies1.goals, copies2.goals; expected != actual { |
| 301 | return fmt.Errorf("goals mismatch at position %d: expected %q found %q", i, expected, actual) |
| 302 | } |
| 303 | |
| 304 | if expected, actual := len(copies1.copies), len(copies2.copies); expected != actual { |
| 305 | return fmt.Errorf("length mismatch in copy instructions at position %d, expected %d found %d", i, expected, actual) |
| 306 | } |
| 307 | |
| 308 | for j, c1 := range copies1.copies { |
| 309 | c2 := copies2.copies[j] |
| 310 | if expected, actual := NormalizePathForTesting(c1.from), NormalizePathForTesting(c2.from); expected != actual { |
| 311 | return fmt.Errorf("paths mismatch at position %d.%d: expected %q found %q", i, j, expected, actual) |
| 312 | } |
| 313 | |
| 314 | if expected, actual := c1.dest, c2.dest; expected != actual { |
| 315 | return fmt.Errorf("dest mismatch at position %d.%d: expected %q found %q", i, j, expected, actual) |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return nil |
| 321 | } |
| 322 | |
| 323 | formatContributions := func(d *distContributions) string { |
| 324 | buf := &strings.Builder{} |
| 325 | if d == nil { |
| 326 | fmt.Fprint(buf, "nil") |
| 327 | } else { |
| 328 | for _, copiesForGoals := range d.copiesForGoals { |
| 329 | fmt.Fprintf(buf, " Goals: %q {\n", copiesForGoals.goals) |
| 330 | for _, c := range copiesForGoals.copies { |
| 331 | fmt.Fprintf(buf, " %s -> %s\n", NormalizePathForTesting(c.from), c.dest) |
| 332 | } |
| 333 | fmt.Fprint(buf, " }\n") |
| 334 | } |
| 335 | } |
| 336 | return buf.String() |
| 337 | } |
| 338 | |
| 339 | testHelper := func(t *testing.T, name, bp string, expectedContributions *distContributions) { |
| 340 | t.Helper() |
| 341 | t.Run(name, func(t *testing.T) { |
| 342 | t.Helper() |
| 343 | |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 344 | ctx, module := buildContextAndCustomModuleFoo(t, bp) |
| 345 | entries := AndroidMkEntriesForTest(t, ctx, module) |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 346 | if len(entries) != 1 { |
| 347 | t.Errorf("Expected a single AndroidMk entry, got %d", len(entries)) |
| 348 | } |
| 349 | distContributions := entries[0].getDistContributions(module) |
| 350 | |
| 351 | if err := compareContributions(expectedContributions, distContributions); err != nil { |
| 352 | t.Errorf("%s\nExpected Contributions\n%sActualContributions\n%s", |
| 353 | err, |
| 354 | formatContributions(expectedContributions), |
| 355 | formatContributions(distContributions)) |
| 356 | } |
| 357 | }) |
| 358 | } |
| 359 | |
| 360 | testHelper(t, "dist-without-tag", ` |
| 361 | custom { |
| 362 | name: "foo", |
| 363 | dist: { |
| 364 | targets: ["my_goal"] |
| 365 | } |
| 366 | } |
| 367 | `, |
| 368 | &distContributions{ |
| 369 | copiesForGoals: []*copiesForGoals{ |
| 370 | { |
| 371 | goals: "my_goal", |
| 372 | copies: []distCopy{ |
| 373 | distCopyForTest("one.out", "one.out"), |
| 374 | }, |
| 375 | }, |
| 376 | }, |
| 377 | }) |
| 378 | |
| 379 | testHelper(t, "dist-with-tag", ` |
| 380 | custom { |
| 381 | name: "foo", |
| 382 | dist: { |
| 383 | targets: ["my_goal"], |
| 384 | tag: ".another-tag", |
| 385 | } |
| 386 | } |
| 387 | `, |
| 388 | &distContributions{ |
| 389 | copiesForGoals: []*copiesForGoals{ |
| 390 | { |
| 391 | goals: "my_goal", |
| 392 | copies: []distCopy{ |
| 393 | distCopyForTest("another.out", "another.out"), |
| 394 | }, |
| 395 | }, |
| 396 | }, |
| 397 | }) |
| 398 | |
Trevor Radcliffe | 90727f4 | 2022-03-21 19:34:02 +0000 | [diff] [blame] | 399 | testHelper(t, "append-artifact-with-product", ` |
| 400 | custom { |
| 401 | name: "foo", |
| 402 | dist: { |
| 403 | targets: ["my_goal"], |
| 404 | append_artifact_with_product: true, |
| 405 | } |
| 406 | } |
| 407 | `, &distContributions{ |
| 408 | copiesForGoals: []*copiesForGoals{ |
| 409 | { |
| 410 | goals: "my_goal", |
| 411 | copies: []distCopy{ |
| 412 | distCopyForTest("one.out", "one_bar.out"), |
| 413 | }, |
| 414 | }, |
| 415 | }, |
| 416 | }) |
| 417 | |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 418 | testHelper(t, "dists-with-tag", ` |
| 419 | custom { |
| 420 | name: "foo", |
| 421 | dists: [ |
| 422 | { |
| 423 | targets: ["my_goal"], |
| 424 | tag: ".another-tag", |
| 425 | }, |
| 426 | ], |
| 427 | } |
| 428 | `, |
| 429 | &distContributions{ |
| 430 | copiesForGoals: []*copiesForGoals{ |
| 431 | { |
| 432 | goals: "my_goal", |
| 433 | copies: []distCopy{ |
| 434 | distCopyForTest("another.out", "another.out"), |
| 435 | }, |
| 436 | }, |
| 437 | }, |
| 438 | }) |
| 439 | |
| 440 | testHelper(t, "multiple-dists-with-and-without-tag", ` |
| 441 | custom { |
| 442 | name: "foo", |
| 443 | dists: [ |
| 444 | { |
| 445 | targets: ["my_goal"], |
| 446 | }, |
| 447 | { |
| 448 | targets: ["my_second_goal", "my_third_goal"], |
| 449 | }, |
| 450 | ], |
| 451 | } |
| 452 | `, |
| 453 | &distContributions{ |
| 454 | copiesForGoals: []*copiesForGoals{ |
| 455 | { |
| 456 | goals: "my_goal", |
| 457 | copies: []distCopy{ |
| 458 | distCopyForTest("one.out", "one.out"), |
| 459 | }, |
| 460 | }, |
| 461 | { |
| 462 | goals: "my_second_goal my_third_goal", |
| 463 | copies: []distCopy{ |
| 464 | distCopyForTest("one.out", "one.out"), |
| 465 | }, |
| 466 | }, |
| 467 | }, |
| 468 | }) |
| 469 | |
| 470 | testHelper(t, "dist-plus-dists-without-tags", ` |
| 471 | custom { |
| 472 | name: "foo", |
| 473 | dist: { |
| 474 | targets: ["my_goal"], |
| 475 | }, |
| 476 | dists: [ |
| 477 | { |
| 478 | targets: ["my_second_goal", "my_third_goal"], |
| 479 | }, |
| 480 | ], |
| 481 | } |
| 482 | `, |
| 483 | &distContributions{ |
| 484 | copiesForGoals: []*copiesForGoals{ |
| 485 | { |
| 486 | goals: "my_second_goal my_third_goal", |
| 487 | copies: []distCopy{ |
| 488 | distCopyForTest("one.out", "one.out"), |
| 489 | }, |
| 490 | }, |
| 491 | { |
| 492 | goals: "my_goal", |
| 493 | copies: []distCopy{ |
| 494 | distCopyForTest("one.out", "one.out"), |
| 495 | }, |
| 496 | }, |
| 497 | }, |
| 498 | }) |
| 499 | |
| 500 | testHelper(t, "dist-plus-dists-with-tags", ` |
| 501 | custom { |
| 502 | name: "foo", |
| 503 | dist: { |
| 504 | targets: ["my_goal", "my_other_goal"], |
| 505 | tag: ".multiple", |
| 506 | }, |
| 507 | dists: [ |
| 508 | { |
| 509 | targets: ["my_second_goal"], |
| 510 | tag: ".multiple", |
| 511 | }, |
| 512 | { |
| 513 | targets: ["my_third_goal"], |
| 514 | dir: "test/dir", |
| 515 | }, |
| 516 | { |
| 517 | targets: ["my_fourth_goal"], |
| 518 | suffix: ".suffix", |
| 519 | }, |
| 520 | { |
| 521 | targets: ["my_fifth_goal"], |
| 522 | dest: "new-name", |
| 523 | }, |
| 524 | { |
| 525 | targets: ["my_sixth_goal"], |
| 526 | dest: "new-name", |
| 527 | dir: "some/dir", |
| 528 | suffix: ".suffix", |
| 529 | }, |
| 530 | ], |
| 531 | } |
| 532 | `, |
| 533 | &distContributions{ |
| 534 | copiesForGoals: []*copiesForGoals{ |
| 535 | { |
| 536 | goals: "my_second_goal", |
| 537 | copies: []distCopy{ |
| 538 | distCopyForTest("two.out", "two.out"), |
| 539 | distCopyForTest("three/four.out", "four.out"), |
| 540 | }, |
| 541 | }, |
| 542 | { |
| 543 | goals: "my_third_goal", |
| 544 | copies: []distCopy{ |
| 545 | distCopyForTest("one.out", "test/dir/one.out"), |
| 546 | }, |
| 547 | }, |
| 548 | { |
| 549 | goals: "my_fourth_goal", |
| 550 | copies: []distCopy{ |
| 551 | distCopyForTest("one.out", "one.suffix.out"), |
| 552 | }, |
| 553 | }, |
| 554 | { |
| 555 | goals: "my_fifth_goal", |
| 556 | copies: []distCopy{ |
| 557 | distCopyForTest("one.out", "new-name"), |
| 558 | }, |
| 559 | }, |
| 560 | { |
| 561 | goals: "my_sixth_goal", |
| 562 | copies: []distCopy{ |
| 563 | distCopyForTest("one.out", "some/dir/new-name.suffix"), |
| 564 | }, |
| 565 | }, |
| 566 | { |
| 567 | goals: "my_goal my_other_goal", |
| 568 | copies: []distCopy{ |
| 569 | distCopyForTest("two.out", "two.out"), |
| 570 | distCopyForTest("three/four.out", "four.out"), |
| 571 | }, |
| 572 | }, |
| 573 | }, |
| 574 | }) |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 575 | |
| 576 | // The above test the default values of default_dist_files and use_output_file. |
| 577 | |
| 578 | // The following tests explicitly test the different combinations of those settings. |
| 579 | testHelper(t, "tagged-dist-files-no-output", ` |
| 580 | custom { |
| 581 | name: "foo", |
| 582 | default_dist_files: "tagged", |
| 583 | dist_output_file: false, |
| 584 | dists: [ |
| 585 | { |
| 586 | targets: ["my_goal"], |
| 587 | }, |
| 588 | { |
| 589 | targets: ["my_goal"], |
| 590 | tag: ".multiple", |
| 591 | }, |
| 592 | ], |
| 593 | } |
| 594 | `, &distContributions{ |
| 595 | copiesForGoals: []*copiesForGoals{ |
| 596 | { |
| 597 | goals: "my_goal", |
| 598 | copies: []distCopy{ |
| 599 | distCopyForTest("one.out", "one.out"), |
| 600 | }, |
| 601 | }, |
| 602 | { |
| 603 | goals: "my_goal", |
| 604 | copies: []distCopy{ |
| 605 | distCopyForTest("two.out", "two.out"), |
| 606 | distCopyForTest("three/four.out", "four.out"), |
| 607 | }, |
| 608 | }, |
| 609 | }, |
| 610 | }) |
| 611 | |
| 612 | testHelper(t, "default-dist-files-no-output", ` |
| 613 | custom { |
| 614 | name: "foo", |
| 615 | default_dist_files: "default", |
| 616 | dist_output_file: false, |
| 617 | dists: [ |
| 618 | { |
| 619 | targets: ["my_goal"], |
| 620 | }, |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 621 | { |
| 622 | targets: ["my_goal"], |
| 623 | tag: ".multiple", |
| 624 | }, |
| 625 | ], |
| 626 | } |
| 627 | `, &distContributions{ |
| 628 | copiesForGoals: []*copiesForGoals{ |
| 629 | { |
| 630 | goals: "my_goal", |
| 631 | copies: []distCopy{ |
| 632 | distCopyForTest("default-dist.out", "default-dist.out"), |
| 633 | }, |
| 634 | }, |
Paul Duffin | af970a2 | 2020-11-23 23:32:56 +0000 | [diff] [blame] | 635 | { |
| 636 | goals: "my_goal", |
| 637 | copies: []distCopy{ |
| 638 | distCopyForTest("two.out", "two.out"), |
| 639 | distCopyForTest("three/four.out", "four.out"), |
| 640 | }, |
| 641 | }, |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 642 | }, |
| 643 | }) |
| 644 | |
| 645 | testHelper(t, "no-dist-files-no-output", ` |
| 646 | custom { |
| 647 | name: "foo", |
| 648 | default_dist_files: "none", |
| 649 | dist_output_file: false, |
| 650 | dists: [ |
| 651 | // The following is silently ignored because there is not default file |
| 652 | // in either the dist files or the output file. |
| 653 | { |
| 654 | targets: ["my_goal"], |
| 655 | }, |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 656 | { |
| 657 | targets: ["my_goal"], |
| 658 | tag: ".multiple", |
| 659 | }, |
| 660 | ], |
| 661 | } |
Paul Duffin | af970a2 | 2020-11-23 23:32:56 +0000 | [diff] [blame] | 662 | `, &distContributions{ |
| 663 | copiesForGoals: []*copiesForGoals{ |
| 664 | { |
| 665 | goals: "my_goal", |
| 666 | copies: []distCopy{ |
| 667 | distCopyForTest("two.out", "two.out"), |
| 668 | distCopyForTest("three/four.out", "four.out"), |
| 669 | }, |
| 670 | }, |
| 671 | }, |
| 672 | }) |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 673 | |
| 674 | testHelper(t, "tagged-dist-files-default-output", ` |
| 675 | custom { |
| 676 | name: "foo", |
| 677 | default_dist_files: "tagged", |
| 678 | dist_output_file: true, |
| 679 | dists: [ |
| 680 | { |
| 681 | targets: ["my_goal"], |
| 682 | }, |
| 683 | { |
| 684 | targets: ["my_goal"], |
| 685 | tag: ".multiple", |
| 686 | }, |
| 687 | ], |
| 688 | } |
| 689 | `, &distContributions{ |
| 690 | copiesForGoals: []*copiesForGoals{ |
| 691 | { |
| 692 | goals: "my_goal", |
| 693 | copies: []distCopy{ |
| 694 | distCopyForTest("one.out", "one.out"), |
| 695 | }, |
| 696 | }, |
| 697 | { |
| 698 | goals: "my_goal", |
| 699 | copies: []distCopy{ |
| 700 | distCopyForTest("two.out", "two.out"), |
| 701 | distCopyForTest("three/four.out", "four.out"), |
| 702 | }, |
| 703 | }, |
| 704 | }, |
| 705 | }) |
| 706 | |
| 707 | testHelper(t, "default-dist-files-default-output", ` |
| 708 | custom { |
| 709 | name: "foo", |
| 710 | default_dist_files: "default", |
| 711 | dist_output_file: true, |
| 712 | dists: [ |
| 713 | { |
| 714 | targets: ["my_goal"], |
| 715 | }, |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 716 | { |
| 717 | targets: ["my_goal"], |
| 718 | tag: ".multiple", |
| 719 | }, |
| 720 | ], |
| 721 | } |
| 722 | `, &distContributions{ |
| 723 | copiesForGoals: []*copiesForGoals{ |
| 724 | { |
| 725 | goals: "my_goal", |
| 726 | copies: []distCopy{ |
| 727 | distCopyForTest("default-dist.out", "default-dist.out"), |
| 728 | }, |
| 729 | }, |
Paul Duffin | af970a2 | 2020-11-23 23:32:56 +0000 | [diff] [blame] | 730 | { |
| 731 | goals: "my_goal", |
| 732 | copies: []distCopy{ |
| 733 | distCopyForTest("two.out", "two.out"), |
| 734 | distCopyForTest("three/four.out", "four.out"), |
| 735 | }, |
| 736 | }, |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 737 | }, |
| 738 | }) |
| 739 | |
| 740 | testHelper(t, "no-dist-files-default-output", ` |
| 741 | custom { |
| 742 | name: "foo", |
| 743 | default_dist_files: "none", |
| 744 | dist_output_file: true, |
| 745 | dists: [ |
| 746 | { |
| 747 | targets: ["my_goal"], |
| 748 | }, |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 749 | { |
| 750 | targets: ["my_goal"], |
| 751 | tag: ".multiple", |
| 752 | }, |
| 753 | ], |
| 754 | } |
| 755 | `, &distContributions{ |
| 756 | copiesForGoals: []*copiesForGoals{ |
| 757 | { |
| 758 | goals: "my_goal", |
| 759 | copies: []distCopy{ |
| 760 | distCopyForTest("dist-output-file.out", "dist-output-file.out"), |
| 761 | }, |
| 762 | }, |
Paul Duffin | af970a2 | 2020-11-23 23:32:56 +0000 | [diff] [blame] | 763 | { |
| 764 | goals: "my_goal", |
| 765 | copies: []distCopy{ |
| 766 | distCopyForTest("two.out", "two.out"), |
| 767 | distCopyForTest("three/four.out", "four.out"), |
| 768 | }, |
| 769 | }, |
Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 770 | }, |
| 771 | }) |
Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 772 | } |