blob: ef62b79ba6af3f82cd6b57f2831dff4c4444b55c [file] [log] [blame]
Jiyong Parkd1063c12019-07-17 20:08:41 +09001// 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
15package sdk
16
17import (
Martin Stjernholm3ff2e662020-07-15 14:38:15 +010018 "android/soong/android"
19 "log"
20 "os"
Jiyong Parkd1063c12019-07-17 20:08:41 +090021 "testing"
Paul Duffinb07fa512020-03-10 22:17:04 +000022
23 "github.com/google/blueprint/proptools"
Jiyong Parkd1063c12019-07-17 20:08:41 +090024)
25
Paul Duffin82d90432019-11-30 09:24:33 +000026// Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE.
27func TestMain(m *testing.M) {
Martin Stjernholm3ff2e662020-07-15 14:38:15 +010028 if android.BuildOs != android.Linux {
29 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
30 log.Printf("Skipping as sdk snapshot generation is only supported on %s not %s", android.Linux, android.BuildOs)
31 os.Exit(0)
32 }
33
Paul Duffin82d90432019-11-30 09:24:33 +000034 runTestWithBuildDir(m)
Jiyong Parkd1063c12019-07-17 20:08:41 +090035}
36
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090037func TestDepNotInRequiredSdks(t *testing.T) {
38 testSdkError(t, `module "myjavalib".*depends on "otherlib".*that isn't part of the required SDKs:.*`, `
39 sdk {
40 name: "mysdk",
Paul Duffina0dbf432019-12-05 11:25:53 +000041 java_header_libs: ["sdkmember"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090042 }
43
44 sdk_snapshot {
45 name: "mysdk@1",
Paul Duffina0dbf432019-12-05 11:25:53 +000046 java_header_libs: ["sdkmember_mysdk_1"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090047 }
48
49 java_import {
50 name: "sdkmember",
51 prefer: false,
52 host_supported: true,
53 }
54
55 java_import {
56 name: "sdkmember_mysdk_1",
57 sdk_member_name: "sdkmember",
58 host_supported: true,
59 }
60
61 java_library {
62 name: "myjavalib",
63 srcs: ["Test.java"],
64 libs: [
65 "sdkmember",
66 "otherlib",
67 ],
68 system_modules: "none",
69 sdk_version: "none",
70 compile_dex: true,
71 host_supported: true,
Jooyung Han5e9013b2020-03-10 06:23:13 +090072 apex_available: ["myapex"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090073 }
74
75 // this lib is no in mysdk
76 java_library {
77 name: "otherlib",
78 srcs: ["Test.java"],
79 system_modules: "none",
80 sdk_version: "none",
81 compile_dex: true,
82 host_supported: true,
83 }
84
85 apex {
86 name: "myapex",
87 java_libs: ["myjavalib"],
88 uses_sdks: ["mysdk@1"],
89 key: "myapex.key",
90 certificate: ":myapex.cert",
91 }
92 `)
93}
Paul Duffin593b3c92019-12-05 14:31:48 +000094
95// Ensure that prebuilt modules have the same effective visibility as the source
96// modules.
97func TestSnapshotVisibility(t *testing.T) {
98 packageBp := `
99 package {
100 default_visibility: ["//other/foo"],
101 }
102
103 sdk {
104 name: "mysdk",
105 visibility: [
106 "//other/foo",
107 // This short form will be replaced with //package:__subpackages__ in the
108 // generated sdk_snapshot.
109 ":__subpackages__",
110 ],
111 java_header_libs: [
112 "myjavalib",
113 "mypublicjavalib",
114 "mydefaultedjavalib",
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100115 "myprivatejavalib",
Paul Duffin593b3c92019-12-05 14:31:48 +0000116 ],
117 }
118
119 java_library {
120 name: "myjavalib",
121 // Uses package default visibility
122 srcs: ["Test.java"],
123 system_modules: "none",
124 sdk_version: "none",
125 }
126
Paul Duffin44885e22020-02-19 16:10:09 +0000127 java_defaults {
128 name: "java-defaults",
Jooyung Han5e9013b2020-03-10 06:23:13 +0900129 visibility: ["//other/bar"],
Paul Duffin44885e22020-02-19 16:10:09 +0000130 }
131
Paul Duffin593b3c92019-12-05 14:31:48 +0000132 java_library {
133 name: "mypublicjavalib",
Paul Duffin44885e22020-02-19 16:10:09 +0000134 defaults: ["java-defaults"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000135 visibility: ["//visibility:public"],
136 srcs: ["Test.java"],
137 system_modules: "none",
138 sdk_version: "none",
139 }
140
141 java_defaults {
142 name: "myjavadefaults",
143 visibility: ["//other/bar"],
144 }
145
146 java_library {
147 name: "mydefaultedjavalib",
148 defaults: ["myjavadefaults"],
149 srcs: ["Test.java"],
150 system_modules: "none",
151 sdk_version: "none",
152 }
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100153
154 java_library {
155 name: "myprivatejavalib",
156 srcs: ["Test.java"],
157 visibility: ["//visibility:private"],
158 system_modules: "none",
159 sdk_version: "none",
160 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000161 `
162
163 result := testSdkWithFs(t, ``,
164 map[string][]byte{
165 "package/Test.java": nil,
166 "package/Android.bp": []byte(packageBp),
167 })
168
Paul Duffin1356d8c2020-02-25 19:26:33 +0000169 result.CheckSnapshot("mysdk", "package",
Paul Duffin593b3c92019-12-05 14:31:48 +0000170 checkAndroidBpContents(`
171// This is auto-generated. DO NOT EDIT.
172
173java_import {
174 name: "mysdk_myjavalib@current",
175 sdk_member_name: "myjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100176 visibility: [
177 "//other/foo",
178 "//package",
179 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000180 jars: ["java/myjavalib.jar"],
181}
182
183java_import {
184 name: "myjavalib",
185 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100186 visibility: [
187 "//other/foo",
188 "//package",
189 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000190 jars: ["java/myjavalib.jar"],
191}
192
193java_import {
194 name: "mysdk_mypublicjavalib@current",
195 sdk_member_name: "mypublicjavalib",
196 visibility: ["//visibility:public"],
197 jars: ["java/mypublicjavalib.jar"],
198}
199
200java_import {
201 name: "mypublicjavalib",
202 prefer: false,
203 visibility: ["//visibility:public"],
204 jars: ["java/mypublicjavalib.jar"],
205}
206
207java_import {
208 name: "mysdk_mydefaultedjavalib@current",
209 sdk_member_name: "mydefaultedjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100210 visibility: [
211 "//other/bar",
212 "//package",
213 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000214 jars: ["java/mydefaultedjavalib.jar"],
215}
216
217java_import {
218 name: "mydefaultedjavalib",
219 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100220 visibility: [
221 "//other/bar",
222 "//package",
223 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000224 jars: ["java/mydefaultedjavalib.jar"],
225}
226
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100227java_import {
228 name: "mysdk_myprivatejavalib@current",
229 sdk_member_name: "myprivatejavalib",
230 visibility: ["//package"],
231 jars: ["java/myprivatejavalib.jar"],
232}
233
234java_import {
235 name: "myprivatejavalib",
236 prefer: false,
237 visibility: ["//package"],
238 jars: ["java/myprivatejavalib.jar"],
239}
240
Paul Duffin593b3c92019-12-05 14:31:48 +0000241sdk_snapshot {
242 name: "mysdk@current",
243 visibility: [
Martin Stjernholm01407c52020-05-13 01:54:21 +0100244 "//other/foo",
Paul Duffin593b3c92019-12-05 14:31:48 +0000245 "//package:__subpackages__",
246 ],
247 java_header_libs: [
248 "mysdk_myjavalib@current",
249 "mysdk_mypublicjavalib@current",
250 "mysdk_mydefaultedjavalib@current",
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100251 "mysdk_myprivatejavalib@current",
Paul Duffin593b3c92019-12-05 14:31:48 +0000252 ],
253}
254`))
255}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000256
257func TestSDkInstall(t *testing.T) {
258 sdk := `
259 sdk {
260 name: "mysdk",
261 }
262 `
263 result := testSdkWithFs(t, ``,
264 map[string][]byte{
265 "Android.bp": []byte(sdk),
266 })
267
268 result.CheckSnapshot("mysdk", "",
269 checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`),
270 )
271}
Paul Duffinb07fa512020-03-10 22:17:04 +0000272
273type EmbeddedPropertiesStruct struct {
Paul Duffin864e1b42020-05-06 10:23:19 +0100274 S_Embedded_Common string `android:"arch_variant"`
275 S_Embedded_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000276}
277
278type testPropertiesStruct struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100279 name string
Paul Duffinb07fa512020-03-10 22:17:04 +0000280 private string
281 Public_Kept string `sdk:"keep"`
282 S_Common string
Paul Duffin864e1b42020-05-06 10:23:19 +0100283 S_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000284 A_Common []string
Paul Duffin864e1b42020-05-06 10:23:19 +0100285 A_Different []string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000286 F_Common *bool
Paul Duffin864e1b42020-05-06 10:23:19 +0100287 F_Different *bool `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000288 EmbeddedPropertiesStruct
289}
290
Paul Duffinf34f6d82020-04-30 15:48:31 +0100291func (p *testPropertiesStruct) optimizableProperties() interface{} {
292 return p
293}
294
Paul Duffin4b8b7932020-05-06 12:35:38 +0100295func (p *testPropertiesStruct) String() string {
296 return p.name
297}
298
299var _ propertiesContainer = (*testPropertiesStruct)(nil)
300
Paul Duffinb07fa512020-03-10 22:17:04 +0000301func TestCommonValueOptimization(t *testing.T) {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100302 common := &testPropertiesStruct{name: "common"}
Paul Duffinf34f6d82020-04-30 15:48:31 +0100303 structs := []propertiesContainer{
Paul Duffinb07fa512020-03-10 22:17:04 +0000304 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100305 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000306 private: "common",
307 Public_Kept: "common",
308 S_Common: "common",
309 S_Different: "upper",
310 A_Common: []string{"first", "second"},
311 A_Different: []string{"alpha", "beta"},
312 F_Common: proptools.BoolPtr(false),
313 F_Different: proptools.BoolPtr(false),
314 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
315 S_Embedded_Common: "embedded_common",
316 S_Embedded_Different: "embedded_upper",
317 },
318 },
319 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100320 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000321 private: "common",
322 Public_Kept: "common",
323 S_Common: "common",
324 S_Different: "lower",
325 A_Common: []string{"first", "second"},
326 A_Different: []string{"alpha", "delta"},
327 F_Common: proptools.BoolPtr(false),
328 F_Different: proptools.BoolPtr(true),
329 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
330 S_Embedded_Common: "embedded_common",
331 S_Embedded_Different: "embedded_lower",
332 },
333 },
334 }
335
336 extractor := newCommonValueExtractor(common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000337
338 h := TestHelper{t}
Paul Duffinc459f892020-04-30 18:08:29 +0100339
340 err := extractor.extractCommonProperties(common, structs)
341 h.AssertDeepEquals("unexpected error", nil, err)
342
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100343 h.AssertDeepEquals("common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000344 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100345 name: "common",
Paul Duffinb07fa512020-03-10 22:17:04 +0000346 private: "",
347 Public_Kept: "",
348 S_Common: "common",
349 S_Different: "",
350 A_Common: []string{"first", "second"},
351 A_Different: []string(nil),
352 F_Common: proptools.BoolPtr(false),
353 F_Different: nil,
354 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
355 S_Embedded_Common: "embedded_common",
356 S_Embedded_Different: "",
357 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100358 },
359 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000360
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100361 h.AssertDeepEquals("updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000362 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100363 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000364 private: "common",
365 Public_Kept: "common",
366 S_Common: "",
367 S_Different: "upper",
368 A_Common: nil,
369 A_Different: []string{"alpha", "beta"},
370 F_Common: nil,
371 F_Different: proptools.BoolPtr(false),
372 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
373 S_Embedded_Common: "",
374 S_Embedded_Different: "embedded_upper",
375 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100376 },
377 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000378
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100379 h.AssertDeepEquals("updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000380 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100381 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000382 private: "common",
383 Public_Kept: "common",
384 S_Common: "",
385 S_Different: "lower",
386 A_Common: nil,
387 A_Different: []string{"alpha", "delta"},
388 F_Common: nil,
389 F_Different: proptools.BoolPtr(true),
390 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
391 S_Embedded_Common: "",
392 S_Embedded_Different: "embedded_lower",
393 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100394 },
395 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000396}
Paul Duffin864e1b42020-05-06 10:23:19 +0100397
398func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) {
399 common := &testPropertiesStruct{name: "common"}
400 structs := []propertiesContainer{
401 &testPropertiesStruct{
402 name: "struct-0",
403 S_Common: "should-be-but-is-not-common0",
404 },
405 &testPropertiesStruct{
406 name: "struct-1",
407 S_Common: "should-be-but-is-not-common1",
408 },
409 }
410
411 extractor := newCommonValueExtractor(common)
412
413 h := TestHelper{t}
414
415 err := extractor.extractCommonProperties(common, structs)
416 h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties:
417 "struct-0" has value "should-be-but-is-not-common0"
418 "struct-1" has value "should-be-but-is-not-common1"`, err)
419}