blob: b7da95cae3438140e385136b281cb48cf02ef823 [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 Duffinabbf63d2021-03-18 01:47:31 +000034 os.Exit(m.Run())
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 ],
Paul Duffin157f40f2020-09-29 16:01:08 +0100111 prebuilt_visibility: [
112 "//prebuilts/mysdk",
113 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000114 java_header_libs: [
115 "myjavalib",
116 "mypublicjavalib",
117 "mydefaultedjavalib",
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100118 "myprivatejavalib",
Paul Duffin593b3c92019-12-05 14:31:48 +0000119 ],
120 }
121
122 java_library {
123 name: "myjavalib",
124 // Uses package default visibility
125 srcs: ["Test.java"],
126 system_modules: "none",
127 sdk_version: "none",
128 }
129
Paul Duffin44885e22020-02-19 16:10:09 +0000130 java_defaults {
131 name: "java-defaults",
Jooyung Han5e9013b2020-03-10 06:23:13 +0900132 visibility: ["//other/bar"],
Paul Duffin44885e22020-02-19 16:10:09 +0000133 }
134
Paul Duffin593b3c92019-12-05 14:31:48 +0000135 java_library {
136 name: "mypublicjavalib",
Paul Duffin44885e22020-02-19 16:10:09 +0000137 defaults: ["java-defaults"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000138 visibility: ["//visibility:public"],
139 srcs: ["Test.java"],
140 system_modules: "none",
141 sdk_version: "none",
142 }
143
144 java_defaults {
145 name: "myjavadefaults",
146 visibility: ["//other/bar"],
147 }
148
149 java_library {
150 name: "mydefaultedjavalib",
151 defaults: ["myjavadefaults"],
152 srcs: ["Test.java"],
153 system_modules: "none",
154 sdk_version: "none",
155 }
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100156
157 java_library {
158 name: "myprivatejavalib",
159 srcs: ["Test.java"],
160 visibility: ["//visibility:private"],
161 system_modules: "none",
162 sdk_version: "none",
163 }
Paul Duffin593b3c92019-12-05 14:31:48 +0000164 `
165
166 result := testSdkWithFs(t, ``,
167 map[string][]byte{
168 "package/Test.java": nil,
169 "package/Android.bp": []byte(packageBp),
170 })
171
Paul Duffin36474d32021-03-12 12:19:43 +0000172 CheckSnapshot(t, result, "mysdk", "package",
Paul Duffin593b3c92019-12-05 14:31:48 +0000173 checkAndroidBpContents(`
174// This is auto-generated. DO NOT EDIT.
175
176java_import {
177 name: "mysdk_myjavalib@current",
178 sdk_member_name: "myjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100179 visibility: [
180 "//other/foo",
181 "//package",
Paul Duffin157f40f2020-09-29 16:01:08 +0100182 "//prebuilts/mysdk",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100183 ],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000184 apex_available: ["//apex_available:platform"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000185 jars: ["java/myjavalib.jar"],
186}
187
188java_import {
189 name: "myjavalib",
190 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100191 visibility: [
192 "//other/foo",
193 "//package",
Paul Duffin157f40f2020-09-29 16:01:08 +0100194 "//prebuilts/mysdk",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100195 ],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000196 apex_available: ["//apex_available:platform"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000197 jars: ["java/myjavalib.jar"],
198}
199
200java_import {
201 name: "mysdk_mypublicjavalib@current",
202 sdk_member_name: "mypublicjavalib",
203 visibility: ["//visibility:public"],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000204 apex_available: ["//apex_available:platform"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000205 jars: ["java/mypublicjavalib.jar"],
206}
207
208java_import {
209 name: "mypublicjavalib",
210 prefer: false,
211 visibility: ["//visibility:public"],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000212 apex_available: ["//apex_available:platform"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000213 jars: ["java/mypublicjavalib.jar"],
214}
215
216java_import {
217 name: "mysdk_mydefaultedjavalib@current",
218 sdk_member_name: "mydefaultedjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100219 visibility: [
220 "//other/bar",
221 "//package",
Paul Duffin157f40f2020-09-29 16:01:08 +0100222 "//prebuilts/mysdk",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100223 ],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000224 apex_available: ["//apex_available:platform"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000225 jars: ["java/mydefaultedjavalib.jar"],
226}
227
228java_import {
229 name: "mydefaultedjavalib",
230 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100231 visibility: [
232 "//other/bar",
233 "//package",
Paul Duffin157f40f2020-09-29 16:01:08 +0100234 "//prebuilts/mysdk",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100235 ],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000236 apex_available: ["//apex_available:platform"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000237 jars: ["java/mydefaultedjavalib.jar"],
238}
239
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100240java_import {
241 name: "mysdk_myprivatejavalib@current",
242 sdk_member_name: "myprivatejavalib",
Paul Duffin157f40f2020-09-29 16:01:08 +0100243 visibility: [
244 "//package",
245 "//prebuilts/mysdk",
246 ],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000247 apex_available: ["//apex_available:platform"],
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100248 jars: ["java/myprivatejavalib.jar"],
249}
250
251java_import {
252 name: "myprivatejavalib",
253 prefer: false,
Paul Duffin157f40f2020-09-29 16:01:08 +0100254 visibility: [
255 "//package",
256 "//prebuilts/mysdk",
257 ],
Martin Stjernholm1e041092020-11-03 00:11:09 +0000258 apex_available: ["//apex_available:platform"],
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100259 jars: ["java/myprivatejavalib.jar"],
260}
261
Paul Duffin593b3c92019-12-05 14:31:48 +0000262sdk_snapshot {
263 name: "mysdk@current",
264 visibility: [
Martin Stjernholm01407c52020-05-13 01:54:21 +0100265 "//other/foo",
Paul Duffin593b3c92019-12-05 14:31:48 +0000266 "//package:__subpackages__",
267 ],
268 java_header_libs: [
269 "mysdk_myjavalib@current",
270 "mysdk_mypublicjavalib@current",
271 "mysdk_mydefaultedjavalib@current",
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100272 "mysdk_myprivatejavalib@current",
Paul Duffin593b3c92019-12-05 14:31:48 +0000273 ],
274}
275`))
276}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000277
Paul Duffin157f40f2020-09-29 16:01:08 +0100278func TestPrebuiltVisibilityProperty_IsValidated(t *testing.T) {
279 testSdkError(t, `prebuilt_visibility: cannot mix "//visibility:private" with any other visibility rules`, `
280 sdk {
281 name: "mysdk",
282 prebuilt_visibility: [
283 "//foo",
284 "//visibility:private",
285 ],
286 }
287`)
288}
289
290func TestPrebuiltVisibilityProperty_AddPrivate(t *testing.T) {
291 testSdkError(t, `prebuilt_visibility: "//visibility:private" does not widen the visibility`, `
292 sdk {
293 name: "mysdk",
294 prebuilt_visibility: [
295 "//visibility:private",
296 ],
297 java_header_libs: [
298 "myjavalib",
299 ],
300 }
301
302 java_library {
303 name: "myjavalib",
304 // Uses package default visibility
305 srcs: ["Test.java"],
306 system_modules: "none",
307 sdk_version: "none",
308 }
309`)
310}
311
Paul Duffin8edc99c2021-03-09 23:02:20 +0000312func TestSdkInstall(t *testing.T) {
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000313 sdk := `
314 sdk {
315 name: "mysdk",
316 }
317 `
Paul Duffin8edc99c2021-03-09 23:02:20 +0000318 result := testSdkWithFs(t, sdk, nil)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000319
Paul Duffin36474d32021-03-12 12:19:43 +0000320 CheckSnapshot(t, result, "mysdk", "",
321 checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`))
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000322}
Paul Duffinb07fa512020-03-10 22:17:04 +0000323
324type EmbeddedPropertiesStruct struct {
Paul Duffin864e1b42020-05-06 10:23:19 +0100325 S_Embedded_Common string `android:"arch_variant"`
326 S_Embedded_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000327}
328
329type testPropertiesStruct struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100330 name string
Paul Duffinb07fa512020-03-10 22:17:04 +0000331 private string
332 Public_Kept string `sdk:"keep"`
333 S_Common string
Paul Duffin864e1b42020-05-06 10:23:19 +0100334 S_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000335 A_Common []string
Paul Duffin864e1b42020-05-06 10:23:19 +0100336 A_Different []string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000337 F_Common *bool
Paul Duffin864e1b42020-05-06 10:23:19 +0100338 F_Different *bool `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000339 EmbeddedPropertiesStruct
340}
341
Paul Duffinf34f6d82020-04-30 15:48:31 +0100342func (p *testPropertiesStruct) optimizableProperties() interface{} {
343 return p
344}
345
Paul Duffin4b8b7932020-05-06 12:35:38 +0100346func (p *testPropertiesStruct) String() string {
347 return p.name
348}
349
350var _ propertiesContainer = (*testPropertiesStruct)(nil)
351
Paul Duffinb07fa512020-03-10 22:17:04 +0000352func TestCommonValueOptimization(t *testing.T) {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100353 common := &testPropertiesStruct{name: "common"}
Paul Duffinf34f6d82020-04-30 15:48:31 +0100354 structs := []propertiesContainer{
Paul Duffinb07fa512020-03-10 22:17:04 +0000355 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100356 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000357 private: "common",
358 Public_Kept: "common",
359 S_Common: "common",
360 S_Different: "upper",
361 A_Common: []string{"first", "second"},
362 A_Different: []string{"alpha", "beta"},
363 F_Common: proptools.BoolPtr(false),
364 F_Different: proptools.BoolPtr(false),
365 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
366 S_Embedded_Common: "embedded_common",
367 S_Embedded_Different: "embedded_upper",
368 },
369 },
370 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100371 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000372 private: "common",
373 Public_Kept: "common",
374 S_Common: "common",
375 S_Different: "lower",
376 A_Common: []string{"first", "second"},
377 A_Different: []string{"alpha", "delta"},
378 F_Common: proptools.BoolPtr(false),
379 F_Different: proptools.BoolPtr(true),
380 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
381 S_Embedded_Common: "embedded_common",
382 S_Embedded_Different: "embedded_lower",
383 },
384 },
385 }
386
387 extractor := newCommonValueExtractor(common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000388
Paul Duffinc459f892020-04-30 18:08:29 +0100389 err := extractor.extractCommonProperties(common, structs)
Paul Duffin36474d32021-03-12 12:19:43 +0000390 android.AssertDeepEquals(t, "unexpected error", nil, err)
Paul Duffinc459f892020-04-30 18:08:29 +0100391
Paul Duffin36474d32021-03-12 12:19:43 +0000392 android.AssertDeepEquals(t, "common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000393 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100394 name: "common",
Paul Duffinb07fa512020-03-10 22:17:04 +0000395 private: "",
396 Public_Kept: "",
397 S_Common: "common",
398 S_Different: "",
399 A_Common: []string{"first", "second"},
400 A_Different: []string(nil),
401 F_Common: proptools.BoolPtr(false),
402 F_Different: nil,
403 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
404 S_Embedded_Common: "embedded_common",
405 S_Embedded_Different: "",
406 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100407 },
408 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000409
Paul Duffin36474d32021-03-12 12:19:43 +0000410 android.AssertDeepEquals(t, "updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000411 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100412 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000413 private: "common",
414 Public_Kept: "common",
415 S_Common: "",
416 S_Different: "upper",
417 A_Common: nil,
418 A_Different: []string{"alpha", "beta"},
419 F_Common: nil,
420 F_Different: proptools.BoolPtr(false),
421 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
422 S_Embedded_Common: "",
423 S_Embedded_Different: "embedded_upper",
424 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100425 },
426 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000427
Paul Duffin36474d32021-03-12 12:19:43 +0000428 android.AssertDeepEquals(t, "updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000429 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100430 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000431 private: "common",
432 Public_Kept: "common",
433 S_Common: "",
434 S_Different: "lower",
435 A_Common: nil,
436 A_Different: []string{"alpha", "delta"},
437 F_Common: nil,
438 F_Different: proptools.BoolPtr(true),
439 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
440 S_Embedded_Common: "",
441 S_Embedded_Different: "embedded_lower",
442 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100443 },
444 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000445}
Paul Duffin864e1b42020-05-06 10:23:19 +0100446
447func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) {
448 common := &testPropertiesStruct{name: "common"}
449 structs := []propertiesContainer{
450 &testPropertiesStruct{
451 name: "struct-0",
452 S_Common: "should-be-but-is-not-common0",
453 },
454 &testPropertiesStruct{
455 name: "struct-1",
456 S_Common: "should-be-but-is-not-common1",
457 },
458 }
459
460 extractor := newCommonValueExtractor(common)
461
Paul Duffin864e1b42020-05-06 10:23:19 +0100462 err := extractor.extractCommonProperties(common, structs)
Paul Duffin36474d32021-03-12 12:19:43 +0000463 android.AssertErrorMessageEquals(t, "unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties:
Paul Duffin864e1b42020-05-06 10:23:19 +0100464 "struct-0" has value "should-be-but-is-not-common0"
465 "struct-1" has value "should-be-but-is-not-common1"`, err)
466}