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