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