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