blob: 9c2d89951b3fdaa319afa66c4d6ab2127ff83fae [file] [log] [blame]
Paul Duffinb432df92021-03-22 22:09:42 +00001// Copyright (C) 2021 The Android Open Source Project
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 apex
16
17import (
Paul Duffinffa83752021-06-09 14:32:53 +010018 "fmt"
19 "strings"
Paul Duffinb432df92021-03-22 22:09:42 +000020 "testing"
21
22 "android/soong/android"
Jiakai Zhangb95998b2023-05-11 16:39:27 +010023 "android/soong/dexpreopt"
Paul Duffinb432df92021-03-22 22:09:42 +000024 "android/soong/java"
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +010025
Paul Duffinb432df92021-03-22 22:09:42 +000026 "github.com/google/blueprint"
Paul Duffin7487a7a2021-05-19 09:36:09 +010027 "github.com/google/blueprint/proptools"
Paul Duffinb432df92021-03-22 22:09:42 +000028)
29
30// Contains tests for platform_bootclasspath logic from java/platform_bootclasspath.go that requires
31// apexes.
32
33var prepareForTestWithPlatformBootclasspath = android.GroupFixturePreparers(
Jiakai Zhangb95998b2023-05-11 16:39:27 +010034 java.PrepareForTestWithJavaDefaultModules,
Paul Duffinb432df92021-03-22 22:09:42 +000035 PrepareForTestWithApexBuildComponents,
36)
37
Paul Duffinffa83752021-06-09 14:32:53 +010038func TestPlatformBootclasspath_Fragments(t *testing.T) {
39 result := android.GroupFixturePreparers(
40 prepareForTestWithPlatformBootclasspath,
Paul Duffindc3f9562021-06-09 15:31:05 +010041 prepareForTestWithMyapex,
Paul Duffinffa83752021-06-09 14:32:53 +010042 java.PrepareForTestWithJavaSdkLibraryFiles,
43 java.FixtureWithLastReleaseApis("foo"),
satayevabcd5972021-08-06 17:49:46 +010044 java.FixtureConfigureApexBootJars("myapex:bar"),
Paul Duffinffa83752021-06-09 14:32:53 +010045 android.FixtureWithRootAndroidBp(`
46 platform_bootclasspath {
47 name: "platform-bootclasspath",
48 fragments: [
Paul Duffindc3f9562021-06-09 15:31:05 +010049 {
50 apex: "myapex",
51 module:"bar-fragment",
52 },
Paul Duffinffa83752021-06-09 14:32:53 +010053 ],
54 hidden_api: {
55 unsupported: [
56 "unsupported.txt",
57 ],
58 removed: [
59 "removed.txt",
60 ],
61 max_target_r_low_priority: [
62 "max-target-r-low-priority.txt",
63 ],
64 max_target_q: [
65 "max-target-q.txt",
66 ],
67 max_target_p: [
68 "max-target-p.txt",
69 ],
70 max_target_o_low_priority: [
71 "max-target-o-low-priority.txt",
72 ],
73 blocked: [
74 "blocked.txt",
75 ],
76 unsupported_packages: [
77 "unsupported-packages.txt",
78 ],
79 },
80 }
81
Paul Duffindc3f9562021-06-09 15:31:05 +010082 apex {
83 name: "myapex",
84 key: "myapex.key",
85 bootclasspath_fragments: [
86 "bar-fragment",
87 ],
88 updatable: false,
Spandan Das38c64f62024-02-12 15:00:15 +000089 min_sdk_version: "30", // R
Paul Duffindc3f9562021-06-09 15:31:05 +010090 }
91
92 apex_key {
93 name: "myapex.key",
94 public_key: "testkey.avbpubkey",
95 private_key: "testkey.pem",
96 }
97
Paul Duffinffa83752021-06-09 14:32:53 +010098 bootclasspath_fragment {
99 name: "bar-fragment",
100 contents: ["bar"],
Paul Duffindc3f9562021-06-09 15:31:05 +0100101 apex_available: ["myapex"],
Paul Duffinffa83752021-06-09 14:32:53 +0100102 api: {
103 stub_libs: ["foo"],
104 },
105 hidden_api: {
106 unsupported: [
107 "bar-unsupported.txt",
108 ],
109 removed: [
110 "bar-removed.txt",
111 ],
112 max_target_r_low_priority: [
113 "bar-max-target-r-low-priority.txt",
114 ],
115 max_target_q: [
116 "bar-max-target-q.txt",
117 ],
118 max_target_p: [
119 "bar-max-target-p.txt",
120 ],
121 max_target_o_low_priority: [
122 "bar-max-target-o-low-priority.txt",
123 ],
124 blocked: [
125 "bar-blocked.txt",
126 ],
127 unsupported_packages: [
128 "bar-unsupported-packages.txt",
129 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100130 split_packages: ["*"],
Paul Duffinffa83752021-06-09 14:32:53 +0100131 },
132 }
133
134 java_library {
135 name: "bar",
Paul Duffindc3f9562021-06-09 15:31:05 +0100136 apex_available: ["myapex"],
Paul Duffinffa83752021-06-09 14:32:53 +0100137 srcs: ["a.java"],
138 system_modules: "none",
139 sdk_version: "none",
140 compile_dex: true,
Paul Duffindc3f9562021-06-09 15:31:05 +0100141 permitted_packages: ["bar"],
Spandan Das38c64f62024-02-12 15:00:15 +0000142 min_sdk_version: "30", // R
Paul Duffinffa83752021-06-09 14:32:53 +0100143 }
144
145 java_sdk_library {
146 name: "foo",
147 srcs: ["a.java"],
148 public: {
149 enabled: true,
150 },
151 compile_dex: true,
152 }
153 `),
154 ).RunTest(t)
155
156 pbcp := result.Module("platform-bootclasspath", "android_common")
Yu Liu663e4502024-08-12 18:23:59 +0000157 info, _ := android.OtherModuleProvider(result, pbcp, java.MonolithicHiddenAPIInfoProvider)
Paul Duffinffa83752021-06-09 14:32:53 +0100158
159 for _, category := range java.HiddenAPIFlagFileCategories {
Cole Faust22e8abc2024-01-23 17:52:13 -0800160 name := category.PropertyName()
Paul Duffinffa83752021-06-09 14:32:53 +0100161 message := fmt.Sprintf("category %s", name)
162 filename := strings.ReplaceAll(name, "_", "-")
163 expected := []string{fmt.Sprintf("%s.txt", filename), fmt.Sprintf("bar-%s.txt", filename)}
164 android.AssertPathsRelativeToTopEquals(t, message, expected, info.FlagsFilesByCategory[category])
165 }
166
Spandan Das38c64f62024-02-12 15:00:15 +0000167 android.AssertPathsRelativeToTopEquals(t, "annotation flags", []string{"out/soong/.intermediates/bar-fragment/android_common_apex30/modular-hiddenapi/annotation-flags.csv"}, info.AnnotationFlagsPaths)
168 android.AssertPathsRelativeToTopEquals(t, "metadata flags", []string{"out/soong/.intermediates/bar-fragment/android_common_apex30/modular-hiddenapi/metadata.csv"}, info.MetadataPaths)
169 android.AssertPathsRelativeToTopEquals(t, "index flags", []string{"out/soong/.intermediates/bar-fragment/android_common_apex30/modular-hiddenapi/index.csv"}, info.IndexPaths)
Paul Duffin67b9d612021-07-21 17:38:47 +0100170
Spandan Das38c64f62024-02-12 15:00:15 +0000171 android.AssertArrayString(t, "stub flags", []string{"out/soong/.intermediates/bar-fragment/android_common_apex30/modular-hiddenapi/filtered-stub-flags.csv:out/soong/.intermediates/bar-fragment/android_common_apex30/modular-hiddenapi/signature-patterns.csv"}, info.StubFlagSubsets.RelativeToTop())
172 android.AssertArrayString(t, "all flags", []string{"out/soong/.intermediates/bar-fragment/android_common_apex30/modular-hiddenapi/filtered-flags.csv:out/soong/.intermediates/bar-fragment/android_common_apex30/modular-hiddenapi/signature-patterns.csv"}, info.FlagSubsets.RelativeToTop())
Paul Duffinffa83752021-06-09 14:32:53 +0100173}
174
Paul Duffin191be3a2021-08-10 16:14:16 +0100175// TestPlatformBootclasspath_LegacyPrebuiltFragment verifies that the
176// prebuilt_bootclasspath_fragment falls back to using the complete stub-flags/all-flags if the
177// filtered files are not provided.
178//
179// TODO: Remove once all prebuilts use the filtered_... properties.
180func TestPlatformBootclasspath_LegacyPrebuiltFragment(t *testing.T) {
181 result := android.GroupFixturePreparers(
182 prepareForTestWithPlatformBootclasspath,
183 java.FixtureConfigureApexBootJars("myapex:foo"),
184 java.PrepareForTestWithJavaSdkLibraryFiles,
185 ).RunTestWithBp(t, `
186 prebuilt_apex {
187 name: "myapex",
188 src: "myapex.apex",
189 exported_bootclasspath_fragments: ["mybootclasspath-fragment"],
190 }
191
192 // A prebuilt java_sdk_library_import that is not preferred by default but will be preferred
193 // because AlwaysUsePrebuiltSdks() is true.
194 java_sdk_library_import {
195 name: "foo",
196 prefer: false,
197 shared_library: false,
198 permitted_packages: ["foo"],
199 public: {
200 jars: ["sdk_library/public/foo-stubs.jar"],
201 stub_srcs: ["sdk_library/public/foo_stub_sources"],
202 current_api: "sdk_library/public/foo.txt",
203 removed_api: "sdk_library/public/foo-removed.txt",
204 sdk_version: "current",
205 },
206 apex_available: ["myapex"],
207 }
208
209 prebuilt_bootclasspath_fragment {
210 name: "mybootclasspath-fragment",
211 apex_available: [
212 "myapex",
213 ],
214 contents: [
215 "foo",
216 ],
217 hidden_api: {
218 stub_flags: "prebuilt-stub-flags.csv",
219 annotation_flags: "prebuilt-annotation-flags.csv",
220 metadata: "prebuilt-metadata.csv",
221 index: "prebuilt-index.csv",
222 all_flags: "prebuilt-all-flags.csv",
223 },
224 }
225
226 platform_bootclasspath {
227 name: "myplatform-bootclasspath",
228 fragments: [
229 {
230 apex: "myapex",
231 module:"mybootclasspath-fragment",
232 },
233 ],
234 }
235`,
236 )
237
238 pbcp := result.Module("myplatform-bootclasspath", "android_common")
Yu Liu663e4502024-08-12 18:23:59 +0000239 info, _ := android.OtherModuleProvider(result, pbcp, java.MonolithicHiddenAPIInfoProvider)
Paul Duffin191be3a2021-08-10 16:14:16 +0100240
241 android.AssertArrayString(t, "stub flags", []string{"prebuilt-stub-flags.csv:out/soong/.intermediates/mybootclasspath-fragment/android_common_myapex/modular-hiddenapi/signature-patterns.csv"}, info.StubFlagSubsets.RelativeToTop())
242 android.AssertArrayString(t, "all flags", []string{"prebuilt-all-flags.csv:out/soong/.intermediates/mybootclasspath-fragment/android_common_myapex/modular-hiddenapi/signature-patterns.csv"}, info.FlagSubsets.RelativeToTop())
243}
244
Paul Duffinb432df92021-03-22 22:09:42 +0000245func TestPlatformBootclasspathDependencies(t *testing.T) {
246 result := android.GroupFixturePreparers(
247 prepareForTestWithPlatformBootclasspath,
248 prepareForTestWithArtApex,
249 prepareForTestWithMyapex,
250 // Configure some libraries in the art and framework boot images.
Paul Duffin60264a02021-04-12 20:02:36 +0100251 java.FixtureConfigureBootJars("com.android.art:baz", "com.android.art:quuz", "platform:foo"),
satayevd604b212021-07-21 14:23:52 +0100252 java.FixtureConfigureApexBootJars("myapex:bar"),
Paul Duffinb432df92021-03-22 22:09:42 +0000253 java.PrepareForTestWithJavaSdkLibraryFiles,
254 java.FixtureWithLastReleaseApis("foo"),
Jiakai Zhangb95998b2023-05-11 16:39:27 +0100255 java.PrepareForTestWithDexpreopt,
256 dexpreopt.FixtureDisableDexpreoptBootImages(false),
Colin Crossa66b4632024-08-08 15:50:47 -0700257 android.PrepareForTestWithBuildFlag("RELEASE_HIDDEN_API_EXPORTABLE_STUBS", "true"),
Paul Duffinb432df92021-03-22 22:09:42 +0000258 ).RunTestWithBp(t, `
259 apex {
260 name: "com.android.art",
261 key: "com.android.art.key",
262 bootclasspath_fragments: [
263 "art-bootclasspath-fragment",
264 ],
265 updatable: false,
266 }
267
268 apex_key {
269 name: "com.android.art.key",
270 public_key: "com.android.art.avbpubkey",
271 private_key: "com.android.art.pem",
272 }
273
274 bootclasspath_fragment {
275 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +0100276 image_name: "art",
Paul Duffinb432df92021-03-22 22:09:42 +0000277 apex_available: [
278 "com.android.art",
279 ],
280 contents: [
281 "baz",
282 "quuz",
283 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100284 hidden_api: {
285 split_packages: ["*"],
286 },
Paul Duffinb432df92021-03-22 22:09:42 +0000287 }
288
289 java_library {
290 name: "baz",
291 apex_available: [
292 "com.android.art",
293 ],
294 srcs: ["b.java"],
295 installable: true,
Jihoon Kang85bc1932024-07-01 17:04:46 +0000296 sdk_version: "core_current",
Paul Duffinb432df92021-03-22 22:09:42 +0000297 }
298
299 // Add a java_import that is not preferred and so won't have an appropriate apex variant created
300 // for it to make sure that the platform_bootclasspath doesn't try and add a dependency onto it.
301 java_import {
302 name: "baz",
303 apex_available: [
304 "com.android.art",
305 ],
306 jars: ["b.jar"],
307 }
308
309 java_library {
310 name: "quuz",
311 apex_available: [
312 "com.android.art",
313 ],
314 srcs: ["b.java"],
315 installable: true,
316 }
317
318 apex {
319 name: "myapex",
320 key: "myapex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +0100321 bootclasspath_fragments: [
322 "my-bootclasspath-fragment",
Paul Duffinb432df92021-03-22 22:09:42 +0000323 ],
324 updatable: false,
325 }
326
Paul Duffin89f570a2021-06-16 01:42:33 +0100327 bootclasspath_fragment {
328 name: "my-bootclasspath-fragment",
329 contents: ["bar"],
330 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100331 hidden_api: {
332 split_packages: ["*"],
333 },
Paul Duffin89f570a2021-06-16 01:42:33 +0100334 }
335
Paul Duffinb432df92021-03-22 22:09:42 +0000336 apex_key {
337 name: "myapex.key",
338 public_key: "testkey.avbpubkey",
339 private_key: "testkey.pem",
340 }
341
342 java_sdk_library {
343 name: "foo",
344 srcs: ["b.java"],
345 }
346
347 java_library {
348 name: "bar",
349 srcs: ["b.java"],
350 installable: true,
351 apex_available: ["myapex"],
352 permitted_packages: ["bar"],
353 }
354
355 platform_bootclasspath {
356 name: "myplatform-bootclasspath",
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100357
358 fragments: [
359 {
360 apex: "com.android.art",
361 module: "art-bootclasspath-fragment",
362 },
Paul Duffin89f570a2021-06-16 01:42:33 +0100363 {
364 apex: "myapex",
365 module: "my-bootclasspath-fragment",
366 },
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100367 ],
Paul Duffinb432df92021-03-22 22:09:42 +0000368 }
369`,
370 )
371
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100372 java.CheckPlatformBootclasspathModules(t, result, "myplatform-bootclasspath", []string{
Paul Duffin74431d52021-04-21 14:10:42 +0100373 // The configured contents of BootJars.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100374 "com.android.art:baz",
375 "com.android.art:quuz",
376 "platform:foo",
Paul Duffin74431d52021-04-21 14:10:42 +0100377
satayevd604b212021-07-21 14:23:52 +0100378 // The configured contents of ApexBootJars.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100379 "myapex:bar",
380 })
381
382 java.CheckPlatformBootclasspathFragments(t, result, "myplatform-bootclasspath", []string{
Paul Duffin89f570a2021-06-16 01:42:33 +0100383 "com.android.art:art-bootclasspath-fragment",
384 "myapex:my-bootclasspath-fragment",
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100385 })
386
Paul Duffinb432df92021-03-22 22:09:42 +0000387 // Make sure that the myplatform-bootclasspath has the correct dependencies.
388 CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{
Spandan Das64c9e0c2023-12-20 20:13:34 +0000389 // source vs prebuilt selection metadata module
390 `platform:all_apex_contributions`,
391
Paul Duffin74431d52021-04-21 14:10:42 +0100392 // The following are stubs.
Jihoon Kangbd093452023-12-26 19:08:01 +0000393 `platform:android_stubs_current_exportable`,
394 `platform:android_system_stubs_current_exportable`,
395 `platform:android_test_stubs_current_exportable`,
396 `platform:legacy.core.platform.api.stubs.exportable`,
Paul Duffin74431d52021-04-21 14:10:42 +0100397
398 // Needed for generating the boot image.
Paul Duffinb432df92021-03-22 22:09:42 +0000399 `platform:dex2oatd`,
Paul Duffin74431d52021-04-21 14:10:42 +0100400
401 // The configured contents of BootJars.
Paul Duffinb432df92021-03-22 22:09:42 +0000402 `com.android.art:baz`,
403 `com.android.art:quuz`,
404 `platform:foo`,
Paul Duffin74431d52021-04-21 14:10:42 +0100405
satayevd604b212021-07-21 14:23:52 +0100406 // The configured contents of ApexBootJars.
Paul Duffinb432df92021-03-22 22:09:42 +0000407 `myapex:bar`,
Paul Duffin74431d52021-04-21 14:10:42 +0100408
409 // The fragments.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100410 `com.android.art:art-bootclasspath-fragment`,
Paul Duffin89f570a2021-06-16 01:42:33 +0100411 `myapex:my-bootclasspath-fragment`,
Paul Duffinb432df92021-03-22 22:09:42 +0000412 })
413}
414
Paul Duffin7487a7a2021-05-19 09:36:09 +0100415// TestPlatformBootclasspath_AlwaysUsePrebuiltSdks verifies that the build does not fail when
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100416// AlwaysUsePrebuiltSdk() returns true.
Paul Duffin7487a7a2021-05-19 09:36:09 +0100417func TestPlatformBootclasspath_AlwaysUsePrebuiltSdks(t *testing.T) {
418 result := android.GroupFixturePreparers(
419 prepareForTestWithPlatformBootclasspath,
420 prepareForTestWithMyapex,
421 // Configure two libraries, the first is a java_sdk_library whose prebuilt will be used because
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100422 // of AlwaysUsePrebuiltsSdk(). The second is a normal library that is unaffected. The order
423 // matters, so that the dependencies resolved by the platform_bootclasspath matches the
424 // configured list.
satayevd604b212021-07-21 14:23:52 +0100425 java.FixtureConfigureApexBootJars("myapex:foo", "myapex:bar"),
Paul Duffin7487a7a2021-05-19 09:36:09 +0100426 java.PrepareForTestWithJavaSdkLibraryFiles,
427 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
428 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
429 }),
Colin Crossa66b4632024-08-08 15:50:47 -0700430 android.PrepareForTestWithBuildFlag("RELEASE_HIDDEN_API_EXPORTABLE_STUBS", "true"),
431
Paul Duffin7487a7a2021-05-19 09:36:09 +0100432 java.FixtureWithPrebuiltApis(map[string][]string{
433 "current": {},
434 "30": {"foo"},
435 }),
436 ).RunTestWithBp(t, `
437 apex {
438 name: "myapex",
439 key: "myapex.key",
440 bootclasspath_fragments: [
441 "mybootclasspath-fragment",
442 ],
443 updatable: false,
444 }
445
446 apex_key {
447 name: "myapex.key",
448 public_key: "testkey.avbpubkey",
449 private_key: "testkey.pem",
450 }
451
452 java_library {
453 name: "bar",
454 srcs: ["b.java"],
455 installable: true,
456 apex_available: ["myapex"],
457 permitted_packages: ["bar"],
458 }
459
460 java_sdk_library {
461 name: "foo",
462 srcs: ["b.java"],
463 shared_library: false,
464 public: {
465 enabled: true,
466 },
467 apex_available: ["myapex"],
468 permitted_packages: ["foo"],
469 }
470
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100471 prebuilt_apex {
472 name: "myapex",
473 src: "myapex.apex",
474 exported_bootclasspath_fragments: ["mybootclasspath-fragment"],
475 }
476
Paul Duffin7487a7a2021-05-19 09:36:09 +0100477 // A prebuilt java_sdk_library_import that is not preferred by default but will be preferred
478 // because AlwaysUsePrebuiltSdks() is true.
479 java_sdk_library_import {
480 name: "foo",
481 prefer: false,
482 shared_library: false,
Paul Duffin630b11e2021-07-15 13:35:26 +0100483 permitted_packages: ["foo"],
Paul Duffin7487a7a2021-05-19 09:36:09 +0100484 public: {
485 jars: ["sdk_library/public/foo-stubs.jar"],
486 stub_srcs: ["sdk_library/public/foo_stub_sources"],
487 current_api: "sdk_library/public/foo.txt",
488 removed_api: "sdk_library/public/foo-removed.txt",
489 sdk_version: "current",
490 },
491 apex_available: ["myapex"],
492 }
493
494 // This always depends on the source foo module, its dependencies are not affected by the
495 // AlwaysUsePrebuiltSdks().
496 bootclasspath_fragment {
497 name: "mybootclasspath-fragment",
498 apex_available: [
499 "myapex",
500 ],
501 contents: [
502 "foo", "bar",
503 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100504 hidden_api: {
505 split_packages: ["*"],
506 },
Paul Duffin7487a7a2021-05-19 09:36:09 +0100507 }
508
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100509 prebuilt_bootclasspath_fragment {
510 name: "mybootclasspath-fragment",
511 apex_available: [
512 "myapex",
513 ],
514 contents: [
515 "foo",
516 ],
517 hidden_api: {
518 stub_flags: "",
519 annotation_flags: "",
520 metadata: "",
521 index: "",
522 all_flags: "",
523 },
524 }
525
Paul Duffin7487a7a2021-05-19 09:36:09 +0100526 platform_bootclasspath {
527 name: "myplatform-bootclasspath",
Paul Duffin89f570a2021-06-16 01:42:33 +0100528 fragments: [
529 {
530 apex: "myapex",
531 module:"mybootclasspath-fragment",
532 },
533 ],
Paul Duffin7487a7a2021-05-19 09:36:09 +0100534 }
535`,
536 )
537
538 java.CheckPlatformBootclasspathModules(t, result, "myplatform-bootclasspath", []string{
539 // The configured contents of BootJars.
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100540 "myapex:prebuilt_foo",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100541 "myapex:bar",
542 })
543
544 // Make sure that the myplatform-bootclasspath has the correct dependencies.
545 CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{
Spandan Das64c9e0c2023-12-20 20:13:34 +0000546 // source vs prebuilt selection metadata module
547 `platform:all_apex_contributions`,
548
Paul Duffin7487a7a2021-05-19 09:36:09 +0100549 // The following are stubs.
550 "platform:prebuilt_sdk_public_current_android",
551 "platform:prebuilt_sdk_system_current_android",
552 "platform:prebuilt_sdk_test_current_android",
553
554 // Not a prebuilt as no prebuilt existed when it was added.
Jihoon Kangbd093452023-12-26 19:08:01 +0000555 "platform:legacy.core.platform.api.stubs.exportable",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100556
Paul Duffin7487a7a2021-05-19 09:36:09 +0100557 // The platform_bootclasspath intentionally adds dependencies on both source and prebuilt
558 // modules when available as it does not know which one will be preferred.
Paul Duffin7487a7a2021-05-19 09:36:09 +0100559 "myapex:foo",
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100560 "myapex:prebuilt_foo",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100561
562 // Only a source module exists.
563 "myapex:bar",
Paul Duffin89f570a2021-06-16 01:42:33 +0100564
565 // The fragments.
566 "myapex:mybootclasspath-fragment",
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100567 "myapex:prebuilt_mybootclasspath-fragment",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100568 })
569}
570
Paul Duffinb432df92021-03-22 22:09:42 +0000571// CheckModuleDependencies checks the dependencies of the selected module against the expected list.
572//
573// The expected list must be a list of strings of the form "<apex>:<module>", where <apex> is the
574// name of the apex, or platform is it is not part of an apex and <module> is the module name.
575func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
576 t.Helper()
577 module := ctx.ModuleForTests(name, variant).Module()
578 modules := []android.Module{}
579 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
580 modules = append(modules, m.(android.Module))
581 })
582
583 pairs := java.ApexNamePairsFromModules(ctx, modules)
584 android.AssertDeepEquals(t, "module dependencies", expected, pairs)
585}
satayevb3090502021-06-15 17:49:10 +0100586
587// TestPlatformBootclasspath_IncludesRemainingApexJars verifies that any apex boot jar is present in
588// platform_bootclasspath's classpaths.proto config, if the apex does not generate its own config
589// by setting generate_classpaths_proto property to false.
590func TestPlatformBootclasspath_IncludesRemainingApexJars(t *testing.T) {
591 result := android.GroupFixturePreparers(
592 prepareForTestWithPlatformBootclasspath,
593 prepareForTestWithMyapex,
satayevd604b212021-07-21 14:23:52 +0100594 java.FixtureConfigureApexBootJars("myapex:foo"),
satayevb3090502021-06-15 17:49:10 +0100595 android.FixtureWithRootAndroidBp(`
596 platform_bootclasspath {
597 name: "platform-bootclasspath",
598 fragments: [
599 {
600 apex: "myapex",
601 module:"foo-fragment",
602 },
603 ],
604 }
605
606 apex {
607 name: "myapex",
608 key: "myapex.key",
609 bootclasspath_fragments: ["foo-fragment"],
610 updatable: false,
611 }
612
613 apex_key {
614 name: "myapex.key",
615 public_key: "testkey.avbpubkey",
616 private_key: "testkey.pem",
617 }
618
619 bootclasspath_fragment {
620 name: "foo-fragment",
621 generate_classpaths_proto: false,
622 contents: ["foo"],
623 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100624 hidden_api: {
625 split_packages: ["*"],
626 },
satayevb3090502021-06-15 17:49:10 +0100627 }
628
629 java_library {
630 name: "foo",
631 srcs: ["a.java"],
632 system_modules: "none",
633 sdk_version: "none",
634 compile_dex: true,
635 apex_available: ["myapex"],
636 permitted_packages: ["foo"],
637 }
638 `),
639 ).RunTest(t)
640
641 java.CheckClasspathFragmentProtoContentInfoProvider(t, result,
642 true, // proto should be generated
643 "myapex:foo", // apex doesn't generate its own config, so must be in platform_bootclasspath
644 "bootclasspath.pb",
645 "out/soong/target/product/test_device/system/etc/classpaths",
646 )
647}
satayevd34eb0c2021-08-06 13:20:28 +0100648
649func TestBootJarNotInApex(t *testing.T) {
650 android.GroupFixturePreparers(
651 prepareForTestWithPlatformBootclasspath,
652 PrepareForTestWithApexBuildComponents,
653 prepareForTestWithMyapex,
654 java.FixtureConfigureApexBootJars("myapex:foo"),
655 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
656 `dependency "foo" of "myplatform-bootclasspath" missing variant`)).
657 RunTestWithBp(t, `
658 apex {
659 name: "myapex",
660 key: "myapex.key",
661 updatable: false,
662 }
663
664 apex_key {
665 name: "myapex.key",
666 public_key: "testkey.avbpubkey",
667 private_key: "testkey.pem",
668 }
669
670 java_library {
671 name: "foo",
672 srcs: ["b.java"],
673 installable: true,
674 apex_available: [
675 "myapex",
676 ],
677 }
678
679 bootclasspath_fragment {
680 name: "not-in-apex-fragment",
681 contents: [
682 "foo",
683 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100684 hidden_api: {
685 split_packages: ["*"],
686 },
satayevd34eb0c2021-08-06 13:20:28 +0100687 }
688
689 platform_bootclasspath {
690 name: "myplatform-bootclasspath",
691 }
692 `)
693}
694
695func TestBootFragmentNotInApex(t *testing.T) {
696 android.GroupFixturePreparers(
697 prepareForTestWithPlatformBootclasspath,
698 PrepareForTestWithApexBuildComponents,
699 prepareForTestWithMyapex,
700 java.FixtureConfigureApexBootJars("myapex:foo"),
701 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
702 `library foo.*have no corresponding fragment.*`)).RunTestWithBp(t, `
703 apex {
704 name: "myapex",
705 key: "myapex.key",
706 java_libs: ["foo"],
707 updatable: false,
708 }
709
710 apex_key {
711 name: "myapex.key",
712 public_key: "testkey.avbpubkey",
713 private_key: "testkey.pem",
714 }
715
716 java_library {
717 name: "foo",
718 srcs: ["b.java"],
719 installable: true,
720 apex_available: ["myapex"],
721 permitted_packages: ["foo"],
722 }
723
724 bootclasspath_fragment {
725 name: "not-in-apex-fragment",
726 contents: ["foo"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100727 hidden_api: {
728 split_packages: ["*"],
729 },
satayevd34eb0c2021-08-06 13:20:28 +0100730 }
731
732 platform_bootclasspath {
733 name: "myplatform-bootclasspath",
734 }
735 `)
736}
737
738func TestNonBootJarInFragment(t *testing.T) {
739 android.GroupFixturePreparers(
740 prepareForTestWithPlatformBootclasspath,
741 PrepareForTestWithApexBuildComponents,
742 prepareForTestWithMyapex,
743 java.FixtureConfigureApexBootJars("myapex:foo"),
744 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
745 `in contents must also be declared in PRODUCT_APEX_BOOT_JARS`)).
746 RunTestWithBp(t, `
747 apex {
748 name: "myapex",
749 key: "myapex.key",
750 bootclasspath_fragments: ["apex-fragment"],
751 updatable: false,
752 }
753
754 apex_key {
755 name: "myapex.key",
756 public_key: "testkey.avbpubkey",
757 private_key: "testkey.pem",
758 }
759
760 java_library {
761 name: "foo",
762 srcs: ["b.java"],
763 installable: true,
764 apex_available: ["myapex"],
765 permitted_packages: ["foo"],
766 }
767
768 java_library {
769 name: "bar",
770 srcs: ["b.java"],
771 installable: true,
772 apex_available: ["myapex"],
773 permitted_packages: ["bar"],
774 }
775
776 bootclasspath_fragment {
777 name: "apex-fragment",
778 contents: ["foo", "bar"],
779 apex_available:[ "myapex" ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100780 hidden_api: {
781 split_packages: ["*"],
782 },
satayevd34eb0c2021-08-06 13:20:28 +0100783 }
784
785 platform_bootclasspath {
786 name: "myplatform-bootclasspath",
787 fragments: [{
788 apex: "myapex",
789 module:"apex-fragment",
790 }],
791 }
792 `)
793}
Spandan Das3d0d31a2024-05-03 21:36:48 +0000794
Spandan Dased7a0302024-08-26 18:06:25 +0000795// Skip bcp_fragment content validation of source apexes if prebuilts are active.
796func TestNonBootJarInPrebuilts(t *testing.T) {
797 testCases := []struct {
798 description string
799 selectedApexContributions string
800 expectedError string
801 }{
802 {
803 description: "source is active",
804 selectedApexContributions: "",
805 expectedError: "in contents must also be declared in PRODUCT_APEX_BOOT_JARS",
806 },
807 {
808 description: "prebuilts are active",
809 selectedApexContributions: "myapex.prebuilt.contributions",
810 expectedError: "", // skip content validation of source bcp fragment
811 },
812 }
813 bp := `
814// Source
815apex {
816 name: "myapex",
817 key: "myapex.key",
818 bootclasspath_fragments: ["apex-fragment"],
819 updatable: false,
820 min_sdk_version: "29",
821}
822
823override_apex {
824 name: "myapex.override", // overrides the min_sdk_version, thereby creating different variants of its transitive deps
825 base: "myapex",
826 min_sdk_version: "34",
827}
828
829apex_key {
830 name: "myapex.key",
831 public_key: "testkey.avbpubkey",
832 private_key: "testkey.pem",
833}
834
835java_library {
836 name: "foo",
837 srcs: ["b.java"],
838 installable: true,
839 apex_available: ["myapex"],
840 permitted_packages: ["foo"],
841 min_sdk_version: "29",
842}
843
844java_library {
845 name: "bar",
846 srcs: ["b.java"],
847 installable: true,
848 apex_available: ["myapex"],
849 permitted_packages: ["bar"],
850 min_sdk_version: "29",
851}
852
853bootclasspath_fragment {
854 name: "apex-fragment",
855 contents: ["foo", "bar"],
856 apex_available:[ "myapex" ],
857 hidden_api: {
858 split_packages: ["*"],
859 },
860}
861
862platform_bootclasspath {
863 name: "myplatform-bootclasspath",
864 fragments: [{
865 apex: "myapex",
866 module:"apex-fragment",
867 }],
868}
869
870// prebuilts
871prebuilt_apex {
872 name: "myapex",
873 apex_name: "myapex",
874 src: "myapex.apex",
875 exported_bootclasspath_fragments: ["apex-fragment"],
876 }
877
878 prebuilt_bootclasspath_fragment {
879 name: "apex-fragment",
880 contents: ["foo"],
881 hidden_api: {
882 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
883 metadata: "my-bootclasspath-fragment/metadata.csv",
884 index: "my-bootclasspath-fragment/index.csv",
885 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
886 all_flags: "my-bootclasspath-fragment/all-flags.csv",
887 },
888 }
889 java_import {
890 name: "foo",
891 jars: ["foo.jar"],
892 }
893
894apex_contributions {
895 name: "myapex.prebuilt.contributions",
896 api_domain: "myapex",
897 contents: ["prebuilt_myapex"],
898}
899`
900
901 for _, tc := range testCases {
902 fixture := android.GroupFixturePreparers(
903 prepareForTestWithPlatformBootclasspath,
904 PrepareForTestWithApexBuildComponents,
905 prepareForTestWithMyapex,
906 java.FixtureConfigureApexBootJars("myapex:foo"),
907 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ADSERVICES", tc.selectedApexContributions),
908 )
909 if tc.expectedError != "" {
910 fixture = fixture.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedError))
911 }
912 fixture.RunTestWithBp(t, bp)
913 }
914
915}
916
Spandan Das3d0d31a2024-05-03 21:36:48 +0000917// Source and prebuilt apex provide different set of boot jars
918func TestNonBootJarMissingInPrebuiltFragment(t *testing.T) {
919 bp := `
920 apex {
921 name: "myapex",
922 key: "myapex.key",
923 bootclasspath_fragments: ["apex-fragment"],
924 updatable: false,
925 }
926
927 apex_key {
928 name: "myapex.key",
929 public_key: "testkey.avbpubkey",
930 private_key: "testkey.pem",
931 }
932
933 java_library {
934 name: "foo",
935 srcs: ["b.java"],
936 installable: true,
937 apex_available: ["myapex"],
938 permitted_packages: ["foo"],
939 }
940
941 java_library {
942 name: "bar",
943 srcs: ["b.java"],
944 installable: true,
945 apex_available: ["myapex"],
946 permitted_packages: ["bar"],
947 }
948
949 bootclasspath_fragment {
950 name: "apex-fragment",
951 contents: ["foo", "bar"],
952 apex_available:[ "myapex" ],
953 hidden_api: {
954 split_packages: ["*"],
955 },
956 }
957
958 prebuilt_apex {
959 name: "com.google.android.myapex", // mainline prebuilt selection logic in soong relies on the naming convention com.google.android
960 apex_name: "myapex",
961 source_apex_name: "myapex",
962 src: "myapex.apex",
963 exported_bootclasspath_fragments: ["apex-fragment"],
964 }
965
966 java_import {
967 name: "foo",
968 jars: ["foo.jar"],
969 apex_available: ["myapex"],
970 permitted_packages: ["foo"],
971 }
972
973 prebuilt_bootclasspath_fragment {
974 name: "apex-fragment",
975 contents: ["foo"], // Unlike the source fragment, this is missing bar
976 apex_available:[ "myapex" ],
977 hidden_api: {
978 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
979 metadata: "my-bootclasspath-fragment/metadata.csv",
980 index: "my-bootclasspath-fragment/index.csv",
981 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
982 all_flags: "my-bootclasspath-fragment/all-flags.csv",
983 },
984 }
985
Spandan Das5f1f9402024-05-21 18:59:23 +0000986 // Another prebuilt apex, but this is not selected during the build.
987 prebuilt_apex {
988 name: "com.google.android.myapex.v2", // mainline prebuilt selection logic in soong relies on the naming convention com.google.android
989 apex_name: "myapex",
990 source_apex_name: "myapex",
991 src: "myapex.apex",
992 exported_bootclasspath_fragments: ["apex-fragment.v2"],
993 }
994
995 java_import {
996 name: "bar",
997 jars: ["bar.jar"],
998 apex_available: ["myapex"],
999 permitted_packages: ["bar"],
1000 }
1001
1002 prebuilt_bootclasspath_fragment {
1003 name: "apex-fragment.v2",
1004 contents: ["bar"], // Unlike the source fragment, this is missing foo
1005 apex_available:[ "myapex" ],
1006 hidden_api: {
1007 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
1008 metadata: "my-bootclasspath-fragment/metadata.csv",
1009 index: "my-bootclasspath-fragment/index.csv",
1010 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
1011 all_flags: "my-bootclasspath-fragment/all-flags.csv",
1012 },
1013 }
1014
1015
Spandan Das3d0d31a2024-05-03 21:36:48 +00001016 apex_contributions {
1017 name: "my_apex_contributions",
1018 api_domain: "myapex",
1019 contents: [%v],
1020 }
1021 `
1022 testCases := []struct {
1023 desc string
1024 configuredBootJars []string
1025 apexContributionContents string
1026 errorExpected bool
1027 }{
1028 {
1029 desc: "Source apex is selected, and APEX_BOOT_JARS is correctly configured for source apex builds",
1030 configuredBootJars: []string{"myapex:foo", "myapex:bar"},
1031 },
1032 {
1033 desc: "Source apex is selected, and APEX_BOOT_JARS is missing bar",
1034 configuredBootJars: []string{"myapex:foo"},
1035 errorExpected: true,
1036 },
1037 {
1038 desc: "Prebuilt apex is selected, and APEX_BOOT_JARS is correctly configured for prebuilt apex build",
1039 configuredBootJars: []string{"myapex:foo"},
1040 apexContributionContents: `"prebuilt_com.google.android.myapex"`,
1041 },
1042 {
1043 desc: "Prebuilt apex is selected, and APEX_BOOT_JARS is missing foo",
1044 configuredBootJars: []string{"myapex:bar"},
1045 apexContributionContents: `"prebuilt_com.google.android.myapex"`,
1046 errorExpected: true,
1047 },
1048 }
1049
1050 for _, tc := range testCases {
1051 fixture := android.GroupFixturePreparers(
1052 prepareForTestWithPlatformBootclasspath,
1053 PrepareForTestWithApexBuildComponents,
1054 prepareForTestWithMyapex,
1055 java.FixtureConfigureApexBootJars(tc.configuredBootJars...),
Colin Crossa66b4632024-08-08 15:50:47 -07001056 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ART", "my_apex_contributions"),
Spandan Das3d0d31a2024-05-03 21:36:48 +00001057 )
1058 if tc.errorExpected {
1059 fixture = fixture.ExtendWithErrorHandler(
1060 android.FixtureExpectsAtLeastOneErrorMatchingPattern(`in contents.*must also be declared in PRODUCT_APEX_BOOT_JARS`),
1061 )
1062 }
1063 fixture.RunTestWithBp(t, fmt.Sprintf(bp, tc.apexContributionContents))
1064 }
1065}