Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame^] | 1 | // Copyright 2020 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 ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | // Module to be packaged |
| 23 | type componentTestModule struct { |
| 24 | ModuleBase |
| 25 | props struct { |
| 26 | Deps []string |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func componentTestModuleFactory() Module { |
| 31 | m := &componentTestModule{} |
| 32 | m.AddProperties(&m.props) |
| 33 | InitAndroidArchModule(m, HostAndDeviceSupported, MultilibBoth) |
| 34 | return m |
| 35 | } |
| 36 | |
| 37 | func (m *componentTestModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 38 | ctx.AddDependency(ctx.Module(), nil, m.props.Deps...) |
| 39 | } |
| 40 | |
| 41 | func (m *componentTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 42 | builtFile := PathForModuleOut(ctx, m.Name()) |
| 43 | dir := ctx.Target().Arch.ArchType.Multilib |
| 44 | installDir := PathForModuleInstall(ctx, dir) |
| 45 | ctx.InstallFile(installDir, m.Name(), builtFile) |
| 46 | } |
| 47 | |
| 48 | // Module that itself is a package |
| 49 | type packageTestModule struct { |
| 50 | ModuleBase |
| 51 | PackagingBase |
| 52 | |
| 53 | entries []string |
| 54 | } |
| 55 | |
| 56 | func packageTestModuleFactory() Module { |
| 57 | module := &packageTestModule{} |
| 58 | InitPackageModule(module) |
| 59 | InitAndroidMultiTargetsArchModule(module, DeviceSupported, MultilibCommon) |
| 60 | return module |
| 61 | } |
| 62 | |
| 63 | func (m *packageTestModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 64 | m.AddDeps(ctx) |
| 65 | } |
| 66 | |
| 67 | func (m *packageTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 68 | zipFile := PathForModuleOut(ctx, "myzip.zip").OutputPath |
| 69 | m.entries = m.CopyDepsToZip(ctx, zipFile) |
| 70 | } |
| 71 | |
| 72 | func runPackagingTest(t *testing.T, bp string, expected []string) { |
| 73 | t.Helper() |
| 74 | |
| 75 | config := TestArchConfig(buildDir, nil, bp, nil) |
| 76 | |
| 77 | ctx := NewTestArchContext(config) |
| 78 | ctx.RegisterModuleType("component", componentTestModuleFactory) |
| 79 | ctx.RegisterModuleType("package_module", packageTestModuleFactory) |
| 80 | ctx.Register() |
| 81 | |
| 82 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 83 | FailIfErrored(t, errs) |
| 84 | _, errs = ctx.PrepareBuildActions(config) |
| 85 | FailIfErrored(t, errs) |
| 86 | |
| 87 | p := ctx.ModuleForTests("package", "android_common").Module().(*packageTestModule) |
| 88 | actual := p.entries |
| 89 | actual = SortedUniqueStrings(actual) |
| 90 | expected = SortedUniqueStrings(expected) |
| 91 | if !reflect.DeepEqual(actual, expected) { |
| 92 | t.Errorf("\ngot: %v\nexpected: %v\n", actual, expected) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestPackagingBase(t *testing.T) { |
| 97 | runPackagingTest(t, |
| 98 | ` |
| 99 | component { |
| 100 | name: "foo", |
| 101 | } |
| 102 | |
| 103 | package_module { |
| 104 | name: "package", |
| 105 | deps: ["foo"], |
| 106 | } |
| 107 | `, []string{"lib64/foo"}) |
| 108 | |
| 109 | runPackagingTest(t, |
| 110 | ` |
| 111 | component { |
| 112 | name: "foo", |
| 113 | deps: ["bar"], |
| 114 | } |
| 115 | |
| 116 | component { |
| 117 | name: "bar", |
| 118 | } |
| 119 | |
| 120 | package_module { |
| 121 | name: "package", |
| 122 | deps: ["foo"], |
| 123 | } |
| 124 | `, []string{"lib64/foo", "lib64/bar"}) |
| 125 | |
| 126 | runPackagingTest(t, |
| 127 | ` |
| 128 | component { |
| 129 | name: "foo", |
| 130 | deps: ["bar"], |
| 131 | } |
| 132 | |
| 133 | component { |
| 134 | name: "bar", |
| 135 | } |
| 136 | |
| 137 | package_module { |
| 138 | name: "package", |
| 139 | deps: ["foo"], |
| 140 | compile_multilib: "both", |
| 141 | } |
| 142 | `, []string{"lib32/foo", "lib32/bar", "lib64/foo", "lib64/bar"}) |
| 143 | |
| 144 | runPackagingTest(t, |
| 145 | ` |
| 146 | component { |
| 147 | name: "foo", |
| 148 | } |
| 149 | |
| 150 | component { |
| 151 | name: "bar", |
| 152 | compile_multilib: "32", |
| 153 | } |
| 154 | |
| 155 | package_module { |
| 156 | name: "package", |
| 157 | deps: ["foo"], |
| 158 | multilib: { |
| 159 | lib32: { |
| 160 | deps: ["bar"], |
| 161 | }, |
| 162 | }, |
| 163 | compile_multilib: "both", |
| 164 | } |
| 165 | `, []string{"lib32/foo", "lib32/bar", "lib64/foo"}) |
| 166 | |
| 167 | runPackagingTest(t, |
| 168 | ` |
| 169 | component { |
| 170 | name: "foo", |
| 171 | } |
| 172 | |
| 173 | component { |
| 174 | name: "bar", |
| 175 | } |
| 176 | |
| 177 | package_module { |
| 178 | name: "package", |
| 179 | deps: ["foo"], |
| 180 | multilib: { |
| 181 | first: { |
| 182 | deps: ["bar"], |
| 183 | }, |
| 184 | }, |
| 185 | compile_multilib: "both", |
| 186 | } |
| 187 | `, []string{"lib32/foo", "lib64/foo", "lib64/bar"}) |
| 188 | } |