| 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 |  | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 49 | m.base().licenseMetadataFile = PathForOutput(ctx, "meta_lic") | 
| mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 50 | var defaultDistPaths Paths | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 51 |  | 
| Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 52 | // If the dist_output_file: true then create an output file that is stored in | 
|  | 53 | // the OutputFile property of the AndroidMkEntry. | 
|  | 54 | if proptools.BoolDefault(m.properties.Dist_output_file, true) { | 
| Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 55 | path := PathForTesting("dist-output-file.out") | 
|  | 56 | m.outputFile = OptionalPathForPath(path) | 
|  | 57 |  | 
|  | 58 | // Previous code would prioritize the DistFiles property over the OutputFile | 
|  | 59 | // property in AndroidMkEntry when determining the default dist paths. | 
|  | 60 | // Setting this first allows it to be overridden based on the | 
|  | 61 | // default_dist_files setting replicating that previous behavior. | 
| mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 62 | defaultDistPaths = Paths{path} | 
| Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 | // Based on the setting of the default_dist_files property possibly create a | 
|  | 66 | // TaggedDistFiles structure that will be stored in the DistFiles property of | 
|  | 67 | // the AndroidMkEntry. | 
|  | 68 | defaultDistFiles := proptools.StringDefault(m.properties.Default_dist_files, defaultDistFiles_Tagged) | 
|  | 69 | switch defaultDistFiles { | 
|  | 70 | case defaultDistFiles_None: | 
| mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 71 | m.setOutputFiles(ctx, defaultDistPaths) | 
| Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 72 |  | 
|  | 73 | case defaultDistFiles_Default: | 
| Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 74 | path := PathForTesting("default-dist.out") | 
| mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 75 | defaultDistPaths = Paths{path} | 
|  | 76 | m.setOutputFiles(ctx, defaultDistPaths) | 
| Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 77 | m.distFiles = MakeDefaultDistFiles(path) | 
| Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 78 |  | 
|  | 79 | case defaultDistFiles_Tagged: | 
| Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 80 | // Module types that set AndroidMkEntry.DistFiles to the result of calling | 
|  | 81 | // GenerateTaggedDistFiles(ctx) relied on no tag being treated as "" which | 
| mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 82 | // meant that the default dist paths would be the same as empty-string-tag | 
|  | 83 | // output files. In order to preserve that behavior when treating no tag | 
|  | 84 | // as being equal to DefaultDistTag this ensures that DefaultDistTag output | 
|  | 85 | // will be the same as empty-string-tag output. | 
|  | 86 | defaultDistPaths = PathsForTesting("one.out") | 
|  | 87 | m.setOutputFiles(ctx, defaultDistPaths) | 
| Paul Duffin | 74f0559 | 2020-11-25 16:37:46 +0000 | [diff] [blame] | 88 |  | 
|  | 89 | // This must be called after setting defaultDistPaths/outputFile as | 
| mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 90 | // GenerateTaggedDistFiles calls into outputFiles property which may use | 
|  | 91 | // those fields. | 
| Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 92 | m.distFiles = m.GenerateTaggedDistFiles(ctx) | 
|  | 93 | } | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| mrziwang | 01715ca | 2024-07-01 11:50:18 -0700 | [diff] [blame] | 96 | func (m *customModule) setOutputFiles(ctx ModuleContext, defaultDistPaths Paths) { | 
|  | 97 | ctx.SetOutputFiles(PathsForTesting("one.out"), "") | 
|  | 98 | ctx.SetOutputFiles(PathsForTesting("two.out", "three/four.out"), ".multiple") | 
|  | 99 | ctx.SetOutputFiles(PathsForTesting("another.out"), ".another-tag") | 
|  | 100 | if defaultDistPaths != nil { | 
|  | 101 | ctx.SetOutputFiles(defaultDistPaths, DefaultDistTag) | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 105 | func (m *customModule) AndroidMk() AndroidMkData { | 
|  | 106 | return AndroidMkData{ | 
|  | 107 | Custom: func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) { | 
|  | 108 | m.data = data | 
|  | 109 | }, | 
|  | 110 | } | 
|  | 111 | } | 
|  | 112 |  | 
| Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 113 | func (m *customModule) AndroidMkEntries() []AndroidMkEntries { | 
|  | 114 | return []AndroidMkEntries{ | 
|  | 115 | { | 
| Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 116 | Class:      "CUSTOM_MODULE", | 
|  | 117 | DistFiles:  m.distFiles, | 
|  | 118 | OutputFile: m.outputFile, | 
| Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 119 | }, | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 |  | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 123 | func customModuleFactory() Module { | 
|  | 124 | module := &customModule{} | 
| Paul Duffin | 6226949 | 2020-11-26 20:18:42 +0000 | [diff] [blame] | 125 |  | 
|  | 126 | module.AddProperties(&module.properties) | 
|  | 127 |  | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 128 | InitAndroidModule(module) | 
|  | 129 | return module | 
|  | 130 | } | 
|  | 131 |  | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 132 | // buildContextAndCustomModuleFoo creates a config object, processes the supplied | 
| Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 133 | // 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] | 134 | func buildContextAndCustomModuleFoo(t *testing.T, bp string) (*TestContext, *customModule) { | 
| Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 135 | t.Helper() | 
| Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 136 | result := GroupFixturePreparers( | 
| Paul Duffin | 9ca14c1 | 2021-03-16 17:30:13 +0000 | [diff] [blame] | 137 | // Enable androidmk Singleton | 
|  | 138 | PrepareForTestWithAndroidMk, | 
|  | 139 | FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
|  | 140 | ctx.RegisterModuleType("custom", customModuleFactory) | 
|  | 141 | }), | 
| Trevor Radcliffe | 90727f4 | 2022-03-21 19:34:02 +0000 | [diff] [blame] | 142 | FixtureModifyProductVariables(func(variables FixtureProductVariables) { | 
|  | 143 | variables.DeviceProduct = proptools.StringPtr("bar") | 
|  | 144 | }), | 
| Paul Duffin | 9ca14c1 | 2021-03-16 17:30:13 +0000 | [diff] [blame] | 145 | FixtureWithRootAndroidBp(bp), | 
| Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 146 | ).RunTest(t) | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 147 |  | 
| Paul Duffin | 9ca14c1 | 2021-03-16 17:30:13 +0000 | [diff] [blame] | 148 | module := result.ModuleForTests("foo", "").Module().(*customModule) | 
|  | 149 | return result.TestContext, module | 
| Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | func TestAndroidMkSingleton_PassesUpdatedAndroidMkDataToCustomCallback(t *testing.T) { | 
| Dan Willemsen | def7b5d | 2021-10-17 00:22:33 -0700 | [diff] [blame] | 153 | if runtime.GOOS == "darwin" { | 
|  | 154 | // Device modules are not exported on Mac, so this test doesn't work. | 
|  | 155 | t.SkipNow() | 
|  | 156 | } | 
|  | 157 |  | 
| Paul Duffin | 103aaae | 2020-11-26 17:36:46 +0000 | [diff] [blame] | 158 | bp := ` | 
|  | 159 | custom { | 
|  | 160 | name: "foo", | 
|  | 161 | required: ["bar"], | 
|  | 162 | host_required: ["baz"], | 
|  | 163 | target_required: ["qux"], | 
|  | 164 | } | 
|  | 165 | ` | 
|  | 166 |  | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 167 | _, m := buildContextAndCustomModuleFoo(t, bp) | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 168 |  | 
|  | 169 | assertEqual := func(expected interface{}, actual interface{}) { | 
|  | 170 | if !reflect.DeepEqual(expected, actual) { | 
|  | 171 | t.Errorf("%q expected, but got %q", expected, actual) | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 | assertEqual([]string{"bar"}, m.data.Required) | 
|  | 175 | assertEqual([]string{"baz"}, m.data.Host_required) | 
|  | 176 | assertEqual([]string{"qux"}, m.data.Target_required) | 
|  | 177 | } | 
| Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 178 |  | 
| Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 179 | func TestGenerateDistContributionsForMake(t *testing.T) { | 
|  | 180 | dc := &distContributions{ | 
|  | 181 | copiesForGoals: []*copiesForGoals{ | 
|  | 182 | { | 
|  | 183 | goals: "my_goal", | 
|  | 184 | copies: []distCopy{ | 
|  | 185 | distCopyForTest("one.out", "one.out"), | 
|  | 186 | distCopyForTest("two.out", "other.out"), | 
|  | 187 | }, | 
|  | 188 | }, | 
|  | 189 | }, | 
|  | 190 | } | 
|  | 191 |  | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 192 | dc.licenseMetadataFile = PathForTesting("meta_lic") | 
| Paul Duffin | 8b0349c | 2020-11-26 14:33:21 +0000 | [diff] [blame] | 193 | makeOutput := generateDistContributionsForMake(dc) | 
|  | 194 |  | 
|  | 195 | assertStringEquals(t, `.PHONY: my_goal | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 196 | $(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] | 197 | $(call dist-for-goals,my_goal,one.out:one.out) | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 198 | $(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] | 199 | $(call dist-for-goals,my_goal,two.out:other.out) | 
|  | 200 | `, strings.Join(makeOutput, "")) | 
|  | 201 | } | 
|  | 202 |  | 
| Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 203 | func TestGetDistForGoals(t *testing.T) { | 
| Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 204 | bp := ` | 
| Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 205 | custom { | 
|  | 206 | name: "foo", | 
|  | 207 | dist: { | 
|  | 208 | targets: ["my_goal", "my_other_goal"], | 
|  | 209 | tag: ".multiple", | 
|  | 210 | }, | 
|  | 211 | dists: [ | 
|  | 212 | { | 
|  | 213 | targets: ["my_second_goal"], | 
|  | 214 | tag: ".multiple", | 
|  | 215 | }, | 
|  | 216 | { | 
|  | 217 | targets: ["my_third_goal"], | 
|  | 218 | dir: "test/dir", | 
|  | 219 | }, | 
|  | 220 | { | 
|  | 221 | targets: ["my_fourth_goal"], | 
|  | 222 | suffix: ".suffix", | 
|  | 223 | }, | 
|  | 224 | { | 
|  | 225 | targets: ["my_fifth_goal"], | 
|  | 226 | dest: "new-name", | 
|  | 227 | }, | 
|  | 228 | { | 
|  | 229 | targets: ["my_sixth_goal"], | 
|  | 230 | dest: "new-name", | 
|  | 231 | dir: "some/dir", | 
|  | 232 | suffix: ".suffix", | 
|  | 233 | }, | 
|  | 234 | ], | 
|  | 235 | } | 
| Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 236 | ` | 
|  | 237 |  | 
|  | 238 | expectedAndroidMkLines := []string{ | 
|  | 239 | ".PHONY: my_second_goal\n", | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 240 | "$(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] | 241 | "$(call dist-for-goals,my_second_goal,two.out:two.out)\n", | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 242 | "$(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] | 243 | "$(call dist-for-goals,my_second_goal,three/four.out:four.out)\n", | 
|  | 244 | ".PHONY: my_third_goal\n", | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 245 | "$(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] | 246 | "$(call dist-for-goals,my_third_goal,one.out:test/dir/one.out)\n", | 
|  | 247 | ".PHONY: my_fourth_goal\n", | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 248 | "$(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] | 249 | "$(call dist-for-goals,my_fourth_goal,one.out:one.suffix.out)\n", | 
|  | 250 | ".PHONY: my_fifth_goal\n", | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 251 | "$(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] | 252 | "$(call dist-for-goals,my_fifth_goal,one.out:new-name)\n", | 
|  | 253 | ".PHONY: my_sixth_goal\n", | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 254 | "$(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] | 255 | "$(call dist-for-goals,my_sixth_goal,one.out:some/dir/new-name.suffix)\n", | 
|  | 256 | ".PHONY: my_goal my_other_goal\n", | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 257 | "$(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] | 258 | "$(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] | 259 | "$(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] | 260 | "$(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] | 261 | } | 
|  | 262 |  | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 263 | ctx, module := buildContextAndCustomModuleFoo(t, bp) | 
|  | 264 | entries := AndroidMkEntriesForTest(t, ctx, module) | 
| Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 265 | if len(entries) != 1 { | 
|  | 266 | t.Errorf("Expected a single AndroidMk entry, got %d", len(entries)) | 
|  | 267 | } | 
|  | 268 | androidMkLines := entries[0].GetDistForGoals(module) | 
| Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 269 |  | 
| Paul Duffin | d83988d | 2020-11-26 17:29:35 +0000 | [diff] [blame] | 270 | if len(androidMkLines) != len(expectedAndroidMkLines) { | 
|  | 271 | t.Errorf( | 
|  | 272 | "Expected %d AndroidMk lines, got %d:\n%v", | 
|  | 273 | len(expectedAndroidMkLines), | 
|  | 274 | len(androidMkLines), | 
|  | 275 | androidMkLines, | 
|  | 276 | ) | 
|  | 277 | } | 
|  | 278 | for idx, line := range androidMkLines { | 
| Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame] | 279 | expectedLine := strings.ReplaceAll(expectedAndroidMkLines[idx], "meta_lic", module.base().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 | } |