blob: 2e6c62a9bf587edd81017f653c23b796bed857f3 [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 ],
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 Duffin1356d8c2020-02-25 19:26:33 +0000172 result.CheckSnapshot("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 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000184 jars: ["java/myjavalib.jar"],
185}
186
187java_import {
188 name: "myjavalib",
189 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100190 visibility: [
191 "//other/foo",
192 "//package",
Paul Duffin157f40f2020-09-29 16:01:08 +0100193 "//prebuilts/mysdk",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100194 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000195 jars: ["java/myjavalib.jar"],
196}
197
198java_import {
199 name: "mysdk_mypublicjavalib@current",
200 sdk_member_name: "mypublicjavalib",
201 visibility: ["//visibility:public"],
202 jars: ["java/mypublicjavalib.jar"],
203}
204
205java_import {
206 name: "mypublicjavalib",
207 prefer: false,
208 visibility: ["//visibility:public"],
209 jars: ["java/mypublicjavalib.jar"],
210}
211
212java_import {
213 name: "mysdk_mydefaultedjavalib@current",
214 sdk_member_name: "mydefaultedjavalib",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100215 visibility: [
216 "//other/bar",
217 "//package",
Paul Duffin157f40f2020-09-29 16:01:08 +0100218 "//prebuilts/mysdk",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100219 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000220 jars: ["java/mydefaultedjavalib.jar"],
221}
222
223java_import {
224 name: "mydefaultedjavalib",
225 prefer: false,
Martin Stjernholm0641d182020-05-13 02:20:06 +0100226 visibility: [
227 "//other/bar",
228 "//package",
Paul Duffin157f40f2020-09-29 16:01:08 +0100229 "//prebuilts/mysdk",
Martin Stjernholm0641d182020-05-13 02:20:06 +0100230 ],
Paul Duffin593b3c92019-12-05 14:31:48 +0000231 jars: ["java/mydefaultedjavalib.jar"],
232}
233
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100234java_import {
235 name: "mysdk_myprivatejavalib@current",
236 sdk_member_name: "myprivatejavalib",
Paul Duffin157f40f2020-09-29 16:01:08 +0100237 visibility: [
238 "//package",
239 "//prebuilts/mysdk",
240 ],
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100241 jars: ["java/myprivatejavalib.jar"],
242}
243
244java_import {
245 name: "myprivatejavalib",
246 prefer: false,
Paul Duffin157f40f2020-09-29 16:01:08 +0100247 visibility: [
248 "//package",
249 "//prebuilts/mysdk",
250 ],
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100251 jars: ["java/myprivatejavalib.jar"],
252}
253
Paul Duffin593b3c92019-12-05 14:31:48 +0000254sdk_snapshot {
255 name: "mysdk@current",
256 visibility: [
Martin Stjernholm01407c52020-05-13 01:54:21 +0100257 "//other/foo",
Paul Duffin593b3c92019-12-05 14:31:48 +0000258 "//package:__subpackages__",
259 ],
260 java_header_libs: [
261 "mysdk_myjavalib@current",
262 "mysdk_mypublicjavalib@current",
263 "mysdk_mydefaultedjavalib@current",
Martin Stjernholm64aeaad2020-05-13 22:11:40 +0100264 "mysdk_myprivatejavalib@current",
Paul Duffin593b3c92019-12-05 14:31:48 +0000265 ],
266}
267`))
268}
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000269
Paul Duffin157f40f2020-09-29 16:01:08 +0100270func TestPrebuiltVisibilityProperty_IsValidated(t *testing.T) {
271 testSdkError(t, `prebuilt_visibility: cannot mix "//visibility:private" with any other visibility rules`, `
272 sdk {
273 name: "mysdk",
274 prebuilt_visibility: [
275 "//foo",
276 "//visibility:private",
277 ],
278 }
279`)
280}
281
282func TestPrebuiltVisibilityProperty_AddPrivate(t *testing.T) {
283 testSdkError(t, `prebuilt_visibility: "//visibility:private" does not widen the visibility`, `
284 sdk {
285 name: "mysdk",
286 prebuilt_visibility: [
287 "//visibility:private",
288 ],
289 java_header_libs: [
290 "myjavalib",
291 ],
292 }
293
294 java_library {
295 name: "myjavalib",
296 // Uses package default visibility
297 srcs: ["Test.java"],
298 system_modules: "none",
299 sdk_version: "none",
300 }
301`)
302}
303
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +0000304func TestSDkInstall(t *testing.T) {
305 sdk := `
306 sdk {
307 name: "mysdk",
308 }
309 `
310 result := testSdkWithFs(t, ``,
311 map[string][]byte{
312 "Android.bp": []byte(sdk),
313 })
314
315 result.CheckSnapshot("mysdk", "",
316 checkAllOtherCopyRules(`.intermediates/mysdk/common_os/mysdk-current.zip -> mysdk-current.zip`),
317 )
318}
Paul Duffinb07fa512020-03-10 22:17:04 +0000319
320type EmbeddedPropertiesStruct struct {
Paul Duffin864e1b42020-05-06 10:23:19 +0100321 S_Embedded_Common string `android:"arch_variant"`
322 S_Embedded_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000323}
324
325type testPropertiesStruct struct {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100326 name string
Paul Duffinb07fa512020-03-10 22:17:04 +0000327 private string
328 Public_Kept string `sdk:"keep"`
329 S_Common string
Paul Duffin864e1b42020-05-06 10:23:19 +0100330 S_Different string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000331 A_Common []string
Paul Duffin864e1b42020-05-06 10:23:19 +0100332 A_Different []string `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000333 F_Common *bool
Paul Duffin864e1b42020-05-06 10:23:19 +0100334 F_Different *bool `android:"arch_variant"`
Paul Duffinb07fa512020-03-10 22:17:04 +0000335 EmbeddedPropertiesStruct
336}
337
Paul Duffinf34f6d82020-04-30 15:48:31 +0100338func (p *testPropertiesStruct) optimizableProperties() interface{} {
339 return p
340}
341
Paul Duffin4b8b7932020-05-06 12:35:38 +0100342func (p *testPropertiesStruct) String() string {
343 return p.name
344}
345
346var _ propertiesContainer = (*testPropertiesStruct)(nil)
347
Paul Duffinb07fa512020-03-10 22:17:04 +0000348func TestCommonValueOptimization(t *testing.T) {
Paul Duffin4b8b7932020-05-06 12:35:38 +0100349 common := &testPropertiesStruct{name: "common"}
Paul Duffinf34f6d82020-04-30 15:48:31 +0100350 structs := []propertiesContainer{
Paul Duffinb07fa512020-03-10 22:17:04 +0000351 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100352 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000353 private: "common",
354 Public_Kept: "common",
355 S_Common: "common",
356 S_Different: "upper",
357 A_Common: []string{"first", "second"},
358 A_Different: []string{"alpha", "beta"},
359 F_Common: proptools.BoolPtr(false),
360 F_Different: proptools.BoolPtr(false),
361 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
362 S_Embedded_Common: "embedded_common",
363 S_Embedded_Different: "embedded_upper",
364 },
365 },
366 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100367 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000368 private: "common",
369 Public_Kept: "common",
370 S_Common: "common",
371 S_Different: "lower",
372 A_Common: []string{"first", "second"},
373 A_Different: []string{"alpha", "delta"},
374 F_Common: proptools.BoolPtr(false),
375 F_Different: proptools.BoolPtr(true),
376 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
377 S_Embedded_Common: "embedded_common",
378 S_Embedded_Different: "embedded_lower",
379 },
380 },
381 }
382
383 extractor := newCommonValueExtractor(common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000384
385 h := TestHelper{t}
Paul Duffinc459f892020-04-30 18:08:29 +0100386
387 err := extractor.extractCommonProperties(common, structs)
388 h.AssertDeepEquals("unexpected error", nil, err)
389
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100390 h.AssertDeepEquals("common properties not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000391 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100392 name: "common",
Paul Duffinb07fa512020-03-10 22:17:04 +0000393 private: "",
394 Public_Kept: "",
395 S_Common: "common",
396 S_Different: "",
397 A_Common: []string{"first", "second"},
398 A_Different: []string(nil),
399 F_Common: proptools.BoolPtr(false),
400 F_Different: nil,
401 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
402 S_Embedded_Common: "embedded_common",
403 S_Embedded_Different: "",
404 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100405 },
406 common)
Paul Duffinb07fa512020-03-10 22:17:04 +0000407
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100408 h.AssertDeepEquals("updated properties[0] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000409 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100410 name: "struct-0",
Paul Duffinb07fa512020-03-10 22:17:04 +0000411 private: "common",
412 Public_Kept: "common",
413 S_Common: "",
414 S_Different: "upper",
415 A_Common: nil,
416 A_Different: []string{"alpha", "beta"},
417 F_Common: nil,
418 F_Different: proptools.BoolPtr(false),
419 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
420 S_Embedded_Common: "",
421 S_Embedded_Different: "embedded_upper",
422 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100423 },
424 structs[0])
Paul Duffinb07fa512020-03-10 22:17:04 +0000425
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100426 h.AssertDeepEquals("updated properties[1] not correct",
Paul Duffinb07fa512020-03-10 22:17:04 +0000427 &testPropertiesStruct{
Paul Duffin4b8b7932020-05-06 12:35:38 +0100428 name: "struct-1",
Paul Duffinb07fa512020-03-10 22:17:04 +0000429 private: "common",
430 Public_Kept: "common",
431 S_Common: "",
432 S_Different: "lower",
433 A_Common: nil,
434 A_Different: []string{"alpha", "delta"},
435 F_Common: nil,
436 F_Different: proptools.BoolPtr(true),
437 EmbeddedPropertiesStruct: EmbeddedPropertiesStruct{
438 S_Embedded_Common: "",
439 S_Embedded_Different: "embedded_lower",
440 },
Paul Duffin1d6c0df2020-05-06 12:50:19 +0100441 },
442 structs[1])
Paul Duffinb07fa512020-03-10 22:17:04 +0000443}
Paul Duffin864e1b42020-05-06 10:23:19 +0100444
445func TestCommonValueOptimization_InvalidArchSpecificVariants(t *testing.T) {
446 common := &testPropertiesStruct{name: "common"}
447 structs := []propertiesContainer{
448 &testPropertiesStruct{
449 name: "struct-0",
450 S_Common: "should-be-but-is-not-common0",
451 },
452 &testPropertiesStruct{
453 name: "struct-1",
454 S_Common: "should-be-but-is-not-common1",
455 },
456 }
457
458 extractor := newCommonValueExtractor(common)
459
460 h := TestHelper{t}
461
462 err := extractor.extractCommonProperties(common, structs)
463 h.AssertErrorMessageEquals("unexpected error", `field "S_Common" is not tagged as "arch_variant" but has arch specific properties:
464 "struct-0" has value "should-be-but-is-not-common0"
465 "struct-1" has value "should-be-but-is-not-common1"`, err)
466}