blob: 645eebb13be9dc7ac310ec73d40880e0b99c5437 [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")
Colin Cross5a377182023-12-14 14:46:23 -0800157 info, _ := android.SingletonModuleProvider(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")
Colin Cross5a377182023-12-14 14:46:23 -0800239 info, _ := android.SingletonModuleProvider(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,
296 }
297
298 // Add a java_import that is not preferred and so won't have an appropriate apex variant created
299 // for it to make sure that the platform_bootclasspath doesn't try and add a dependency onto it.
300 java_import {
301 name: "baz",
302 apex_available: [
303 "com.android.art",
304 ],
305 jars: ["b.jar"],
306 }
307
308 java_library {
309 name: "quuz",
310 apex_available: [
311 "com.android.art",
312 ],
313 srcs: ["b.java"],
314 installable: true,
315 }
316
317 apex {
318 name: "myapex",
319 key: "myapex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +0100320 bootclasspath_fragments: [
321 "my-bootclasspath-fragment",
Paul Duffinb432df92021-03-22 22:09:42 +0000322 ],
323 updatable: false,
324 }
325
Paul Duffin89f570a2021-06-16 01:42:33 +0100326 bootclasspath_fragment {
327 name: "my-bootclasspath-fragment",
328 contents: ["bar"],
329 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100330 hidden_api: {
331 split_packages: ["*"],
332 },
Paul Duffin89f570a2021-06-16 01:42:33 +0100333 }
334
Paul Duffinb432df92021-03-22 22:09:42 +0000335 apex_key {
336 name: "myapex.key",
337 public_key: "testkey.avbpubkey",
338 private_key: "testkey.pem",
339 }
340
341 java_sdk_library {
342 name: "foo",
343 srcs: ["b.java"],
344 }
345
346 java_library {
347 name: "bar",
348 srcs: ["b.java"],
349 installable: true,
350 apex_available: ["myapex"],
351 permitted_packages: ["bar"],
352 }
353
354 platform_bootclasspath {
355 name: "myplatform-bootclasspath",
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100356
357 fragments: [
358 {
359 apex: "com.android.art",
360 module: "art-bootclasspath-fragment",
361 },
Paul Duffin89f570a2021-06-16 01:42:33 +0100362 {
363 apex: "myapex",
364 module: "my-bootclasspath-fragment",
365 },
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100366 ],
Paul Duffinb432df92021-03-22 22:09:42 +0000367 }
368`,
369 )
370
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100371 java.CheckPlatformBootclasspathModules(t, result, "myplatform-bootclasspath", []string{
Paul Duffin74431d52021-04-21 14:10:42 +0100372 // The configured contents of BootJars.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100373 "com.android.art:baz",
374 "com.android.art:quuz",
375 "platform:foo",
Paul Duffin74431d52021-04-21 14:10:42 +0100376
satayevd604b212021-07-21 14:23:52 +0100377 // The configured contents of ApexBootJars.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100378 "myapex:bar",
379 })
380
381 java.CheckPlatformBootclasspathFragments(t, result, "myplatform-bootclasspath", []string{
Paul Duffin89f570a2021-06-16 01:42:33 +0100382 "com.android.art:art-bootclasspath-fragment",
383 "myapex:my-bootclasspath-fragment",
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100384 })
385
Paul Duffinb432df92021-03-22 22:09:42 +0000386 // Make sure that the myplatform-bootclasspath has the correct dependencies.
387 CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{
Spandan Das64c9e0c2023-12-20 20:13:34 +0000388 // source vs prebuilt selection metadata module
389 `platform:all_apex_contributions`,
390
Paul Duffin74431d52021-04-21 14:10:42 +0100391 // The following are stubs.
Jihoon Kangbd093452023-12-26 19:08:01 +0000392 `platform:android_stubs_current_exportable`,
393 `platform:android_system_stubs_current_exportable`,
394 `platform:android_test_stubs_current_exportable`,
395 `platform:legacy.core.platform.api.stubs.exportable`,
Paul Duffin74431d52021-04-21 14:10:42 +0100396
397 // Needed for generating the boot image.
Paul Duffinb432df92021-03-22 22:09:42 +0000398 `platform:dex2oatd`,
Paul Duffin74431d52021-04-21 14:10:42 +0100399
400 // The configured contents of BootJars.
Paul Duffinb432df92021-03-22 22:09:42 +0000401 `com.android.art:baz`,
402 `com.android.art:quuz`,
403 `platform:foo`,
Paul Duffin74431d52021-04-21 14:10:42 +0100404
satayevd604b212021-07-21 14:23:52 +0100405 // The configured contents of ApexBootJars.
Paul Duffinb432df92021-03-22 22:09:42 +0000406 `myapex:bar`,
Paul Duffin74431d52021-04-21 14:10:42 +0100407
408 // The fragments.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100409 `com.android.art:art-bootclasspath-fragment`,
Paul Duffin89f570a2021-06-16 01:42:33 +0100410 `myapex:my-bootclasspath-fragment`,
Paul Duffinb432df92021-03-22 22:09:42 +0000411 })
412}
413
Paul Duffin7487a7a2021-05-19 09:36:09 +0100414// TestPlatformBootclasspath_AlwaysUsePrebuiltSdks verifies that the build does not fail when
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100415// AlwaysUsePrebuiltSdk() returns true.
Paul Duffin7487a7a2021-05-19 09:36:09 +0100416func TestPlatformBootclasspath_AlwaysUsePrebuiltSdks(t *testing.T) {
417 result := android.GroupFixturePreparers(
418 prepareForTestWithPlatformBootclasspath,
419 prepareForTestWithMyapex,
420 // Configure two libraries, the first is a java_sdk_library whose prebuilt will be used because
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100421 // of AlwaysUsePrebuiltsSdk(). The second is a normal library that is unaffected. The order
422 // matters, so that the dependencies resolved by the platform_bootclasspath matches the
423 // configured list.
satayevd604b212021-07-21 14:23:52 +0100424 java.FixtureConfigureApexBootJars("myapex:foo", "myapex:bar"),
Paul Duffin7487a7a2021-05-19 09:36:09 +0100425 java.PrepareForTestWithJavaSdkLibraryFiles,
426 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
427 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
428 }),
Colin Crossa66b4632024-08-08 15:50:47 -0700429 android.PrepareForTestWithBuildFlag("RELEASE_HIDDEN_API_EXPORTABLE_STUBS", "true"),
430
Paul Duffin7487a7a2021-05-19 09:36:09 +0100431 java.FixtureWithPrebuiltApis(map[string][]string{
432 "current": {},
433 "30": {"foo"},
434 }),
435 ).RunTestWithBp(t, `
436 apex {
437 name: "myapex",
438 key: "myapex.key",
439 bootclasspath_fragments: [
440 "mybootclasspath-fragment",
441 ],
442 updatable: false,
443 }
444
445 apex_key {
446 name: "myapex.key",
447 public_key: "testkey.avbpubkey",
448 private_key: "testkey.pem",
449 }
450
451 java_library {
452 name: "bar",
453 srcs: ["b.java"],
454 installable: true,
455 apex_available: ["myapex"],
456 permitted_packages: ["bar"],
457 }
458
459 java_sdk_library {
460 name: "foo",
461 srcs: ["b.java"],
462 shared_library: false,
463 public: {
464 enabled: true,
465 },
466 apex_available: ["myapex"],
467 permitted_packages: ["foo"],
468 }
469
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100470 prebuilt_apex {
471 name: "myapex",
472 src: "myapex.apex",
473 exported_bootclasspath_fragments: ["mybootclasspath-fragment"],
474 }
475
Paul Duffin7487a7a2021-05-19 09:36:09 +0100476 // A prebuilt java_sdk_library_import that is not preferred by default but will be preferred
477 // because AlwaysUsePrebuiltSdks() is true.
478 java_sdk_library_import {
479 name: "foo",
480 prefer: false,
481 shared_library: false,
Paul Duffin630b11e2021-07-15 13:35:26 +0100482 permitted_packages: ["foo"],
Paul Duffin7487a7a2021-05-19 09:36:09 +0100483 public: {
484 jars: ["sdk_library/public/foo-stubs.jar"],
485 stub_srcs: ["sdk_library/public/foo_stub_sources"],
486 current_api: "sdk_library/public/foo.txt",
487 removed_api: "sdk_library/public/foo-removed.txt",
488 sdk_version: "current",
489 },
490 apex_available: ["myapex"],
491 }
492
493 // This always depends on the source foo module, its dependencies are not affected by the
494 // AlwaysUsePrebuiltSdks().
495 bootclasspath_fragment {
496 name: "mybootclasspath-fragment",
497 apex_available: [
498 "myapex",
499 ],
500 contents: [
501 "foo", "bar",
502 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100503 hidden_api: {
504 split_packages: ["*"],
505 },
Paul Duffin7487a7a2021-05-19 09:36:09 +0100506 }
507
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100508 prebuilt_bootclasspath_fragment {
509 name: "mybootclasspath-fragment",
510 apex_available: [
511 "myapex",
512 ],
513 contents: [
514 "foo",
515 ],
516 hidden_api: {
517 stub_flags: "",
518 annotation_flags: "",
519 metadata: "",
520 index: "",
521 all_flags: "",
522 },
523 }
524
Paul Duffin7487a7a2021-05-19 09:36:09 +0100525 platform_bootclasspath {
526 name: "myplatform-bootclasspath",
Paul Duffin89f570a2021-06-16 01:42:33 +0100527 fragments: [
528 {
529 apex: "myapex",
530 module:"mybootclasspath-fragment",
531 },
532 ],
Paul Duffin7487a7a2021-05-19 09:36:09 +0100533 }
534`,
535 )
536
537 java.CheckPlatformBootclasspathModules(t, result, "myplatform-bootclasspath", []string{
538 // The configured contents of BootJars.
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100539 "myapex:prebuilt_foo",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100540 "myapex:bar",
541 })
542
543 // Make sure that the myplatform-bootclasspath has the correct dependencies.
544 CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{
Spandan Das64c9e0c2023-12-20 20:13:34 +0000545 // source vs prebuilt selection metadata module
546 `platform:all_apex_contributions`,
547
Paul Duffin7487a7a2021-05-19 09:36:09 +0100548 // The following are stubs.
549 "platform:prebuilt_sdk_public_current_android",
550 "platform:prebuilt_sdk_system_current_android",
551 "platform:prebuilt_sdk_test_current_android",
552
553 // Not a prebuilt as no prebuilt existed when it was added.
Jihoon Kangbd093452023-12-26 19:08:01 +0000554 "platform:legacy.core.platform.api.stubs.exportable",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100555
Paul Duffin7487a7a2021-05-19 09:36:09 +0100556 // The platform_bootclasspath intentionally adds dependencies on both source and prebuilt
557 // modules when available as it does not know which one will be preferred.
Paul Duffin7487a7a2021-05-19 09:36:09 +0100558 "myapex:foo",
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100559 "myapex:prebuilt_foo",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100560
561 // Only a source module exists.
562 "myapex:bar",
Paul Duffin89f570a2021-06-16 01:42:33 +0100563
564 // The fragments.
565 "myapex:mybootclasspath-fragment",
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100566 "myapex:prebuilt_mybootclasspath-fragment",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100567 })
568}
569
Paul Duffinb432df92021-03-22 22:09:42 +0000570// CheckModuleDependencies checks the dependencies of the selected module against the expected list.
571//
572// The expected list must be a list of strings of the form "<apex>:<module>", where <apex> is the
573// name of the apex, or platform is it is not part of an apex and <module> is the module name.
574func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
575 t.Helper()
576 module := ctx.ModuleForTests(name, variant).Module()
577 modules := []android.Module{}
578 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
579 modules = append(modules, m.(android.Module))
580 })
581
582 pairs := java.ApexNamePairsFromModules(ctx, modules)
583 android.AssertDeepEquals(t, "module dependencies", expected, pairs)
584}
satayevb3090502021-06-15 17:49:10 +0100585
586// TestPlatformBootclasspath_IncludesRemainingApexJars verifies that any apex boot jar is present in
587// platform_bootclasspath's classpaths.proto config, if the apex does not generate its own config
588// by setting generate_classpaths_proto property to false.
589func TestPlatformBootclasspath_IncludesRemainingApexJars(t *testing.T) {
590 result := android.GroupFixturePreparers(
591 prepareForTestWithPlatformBootclasspath,
592 prepareForTestWithMyapex,
satayevd604b212021-07-21 14:23:52 +0100593 java.FixtureConfigureApexBootJars("myapex:foo"),
satayevb3090502021-06-15 17:49:10 +0100594 android.FixtureWithRootAndroidBp(`
595 platform_bootclasspath {
596 name: "platform-bootclasspath",
597 fragments: [
598 {
599 apex: "myapex",
600 module:"foo-fragment",
601 },
602 ],
603 }
604
605 apex {
606 name: "myapex",
607 key: "myapex.key",
608 bootclasspath_fragments: ["foo-fragment"],
609 updatable: false,
610 }
611
612 apex_key {
613 name: "myapex.key",
614 public_key: "testkey.avbpubkey",
615 private_key: "testkey.pem",
616 }
617
618 bootclasspath_fragment {
619 name: "foo-fragment",
620 generate_classpaths_proto: false,
621 contents: ["foo"],
622 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100623 hidden_api: {
624 split_packages: ["*"],
625 },
satayevb3090502021-06-15 17:49:10 +0100626 }
627
628 java_library {
629 name: "foo",
630 srcs: ["a.java"],
631 system_modules: "none",
632 sdk_version: "none",
633 compile_dex: true,
634 apex_available: ["myapex"],
635 permitted_packages: ["foo"],
636 }
637 `),
638 ).RunTest(t)
639
640 java.CheckClasspathFragmentProtoContentInfoProvider(t, result,
641 true, // proto should be generated
642 "myapex:foo", // apex doesn't generate its own config, so must be in platform_bootclasspath
643 "bootclasspath.pb",
644 "out/soong/target/product/test_device/system/etc/classpaths",
645 )
646}
satayevd34eb0c2021-08-06 13:20:28 +0100647
648func TestBootJarNotInApex(t *testing.T) {
649 android.GroupFixturePreparers(
650 prepareForTestWithPlatformBootclasspath,
651 PrepareForTestWithApexBuildComponents,
652 prepareForTestWithMyapex,
653 java.FixtureConfigureApexBootJars("myapex:foo"),
654 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
655 `dependency "foo" of "myplatform-bootclasspath" missing variant`)).
656 RunTestWithBp(t, `
657 apex {
658 name: "myapex",
659 key: "myapex.key",
660 updatable: false,
661 }
662
663 apex_key {
664 name: "myapex.key",
665 public_key: "testkey.avbpubkey",
666 private_key: "testkey.pem",
667 }
668
669 java_library {
670 name: "foo",
671 srcs: ["b.java"],
672 installable: true,
673 apex_available: [
674 "myapex",
675 ],
676 }
677
678 bootclasspath_fragment {
679 name: "not-in-apex-fragment",
680 contents: [
681 "foo",
682 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100683 hidden_api: {
684 split_packages: ["*"],
685 },
satayevd34eb0c2021-08-06 13:20:28 +0100686 }
687
688 platform_bootclasspath {
689 name: "myplatform-bootclasspath",
690 }
691 `)
692}
693
694func TestBootFragmentNotInApex(t *testing.T) {
695 android.GroupFixturePreparers(
696 prepareForTestWithPlatformBootclasspath,
697 PrepareForTestWithApexBuildComponents,
698 prepareForTestWithMyapex,
699 java.FixtureConfigureApexBootJars("myapex:foo"),
700 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
701 `library foo.*have no corresponding fragment.*`)).RunTestWithBp(t, `
702 apex {
703 name: "myapex",
704 key: "myapex.key",
705 java_libs: ["foo"],
706 updatable: false,
707 }
708
709 apex_key {
710 name: "myapex.key",
711 public_key: "testkey.avbpubkey",
712 private_key: "testkey.pem",
713 }
714
715 java_library {
716 name: "foo",
717 srcs: ["b.java"],
718 installable: true,
719 apex_available: ["myapex"],
720 permitted_packages: ["foo"],
721 }
722
723 bootclasspath_fragment {
724 name: "not-in-apex-fragment",
725 contents: ["foo"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100726 hidden_api: {
727 split_packages: ["*"],
728 },
satayevd34eb0c2021-08-06 13:20:28 +0100729 }
730
731 platform_bootclasspath {
732 name: "myplatform-bootclasspath",
733 }
734 `)
735}
736
737func TestNonBootJarInFragment(t *testing.T) {
738 android.GroupFixturePreparers(
739 prepareForTestWithPlatformBootclasspath,
740 PrepareForTestWithApexBuildComponents,
741 prepareForTestWithMyapex,
742 java.FixtureConfigureApexBootJars("myapex:foo"),
743 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
744 `in contents must also be declared in PRODUCT_APEX_BOOT_JARS`)).
745 RunTestWithBp(t, `
746 apex {
747 name: "myapex",
748 key: "myapex.key",
749 bootclasspath_fragments: ["apex-fragment"],
750 updatable: false,
751 }
752
753 apex_key {
754 name: "myapex.key",
755 public_key: "testkey.avbpubkey",
756 private_key: "testkey.pem",
757 }
758
759 java_library {
760 name: "foo",
761 srcs: ["b.java"],
762 installable: true,
763 apex_available: ["myapex"],
764 permitted_packages: ["foo"],
765 }
766
767 java_library {
768 name: "bar",
769 srcs: ["b.java"],
770 installable: true,
771 apex_available: ["myapex"],
772 permitted_packages: ["bar"],
773 }
774
775 bootclasspath_fragment {
776 name: "apex-fragment",
777 contents: ["foo", "bar"],
778 apex_available:[ "myapex" ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100779 hidden_api: {
780 split_packages: ["*"],
781 },
satayevd34eb0c2021-08-06 13:20:28 +0100782 }
783
784 platform_bootclasspath {
785 name: "myplatform-bootclasspath",
786 fragments: [{
787 apex: "myapex",
788 module:"apex-fragment",
789 }],
790 }
791 `)
792}
Spandan Das3d0d31a2024-05-03 21:36:48 +0000793
794// Source and prebuilt apex provide different set of boot jars
795func TestNonBootJarMissingInPrebuiltFragment(t *testing.T) {
796 bp := `
797 apex {
798 name: "myapex",
799 key: "myapex.key",
800 bootclasspath_fragments: ["apex-fragment"],
801 updatable: false,
802 }
803
804 apex_key {
805 name: "myapex.key",
806 public_key: "testkey.avbpubkey",
807 private_key: "testkey.pem",
808 }
809
810 java_library {
811 name: "foo",
812 srcs: ["b.java"],
813 installable: true,
814 apex_available: ["myapex"],
815 permitted_packages: ["foo"],
816 }
817
818 java_library {
819 name: "bar",
820 srcs: ["b.java"],
821 installable: true,
822 apex_available: ["myapex"],
823 permitted_packages: ["bar"],
824 }
825
826 bootclasspath_fragment {
827 name: "apex-fragment",
828 contents: ["foo", "bar"],
829 apex_available:[ "myapex" ],
830 hidden_api: {
831 split_packages: ["*"],
832 },
833 }
834
835 prebuilt_apex {
836 name: "com.google.android.myapex", // mainline prebuilt selection logic in soong relies on the naming convention com.google.android
837 apex_name: "myapex",
838 source_apex_name: "myapex",
839 src: "myapex.apex",
840 exported_bootclasspath_fragments: ["apex-fragment"],
841 }
842
843 java_import {
844 name: "foo",
845 jars: ["foo.jar"],
846 apex_available: ["myapex"],
847 permitted_packages: ["foo"],
848 }
849
850 prebuilt_bootclasspath_fragment {
851 name: "apex-fragment",
852 contents: ["foo"], // Unlike the source fragment, this is missing bar
853 apex_available:[ "myapex" ],
854 hidden_api: {
855 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
856 metadata: "my-bootclasspath-fragment/metadata.csv",
857 index: "my-bootclasspath-fragment/index.csv",
858 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
859 all_flags: "my-bootclasspath-fragment/all-flags.csv",
860 },
861 }
862
Spandan Das5f1f9402024-05-21 18:59:23 +0000863 // Another prebuilt apex, but this is not selected during the build.
864 prebuilt_apex {
865 name: "com.google.android.myapex.v2", // mainline prebuilt selection logic in soong relies on the naming convention com.google.android
866 apex_name: "myapex",
867 source_apex_name: "myapex",
868 src: "myapex.apex",
869 exported_bootclasspath_fragments: ["apex-fragment.v2"],
870 }
871
872 java_import {
873 name: "bar",
874 jars: ["bar.jar"],
875 apex_available: ["myapex"],
876 permitted_packages: ["bar"],
877 }
878
879 prebuilt_bootclasspath_fragment {
880 name: "apex-fragment.v2",
881 contents: ["bar"], // Unlike the source fragment, this is missing foo
882 apex_available:[ "myapex" ],
883 hidden_api: {
884 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
885 metadata: "my-bootclasspath-fragment/metadata.csv",
886 index: "my-bootclasspath-fragment/index.csv",
887 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
888 all_flags: "my-bootclasspath-fragment/all-flags.csv",
889 },
890 }
891
892
Spandan Das3d0d31a2024-05-03 21:36:48 +0000893 apex_contributions {
894 name: "my_apex_contributions",
895 api_domain: "myapex",
896 contents: [%v],
897 }
898 `
899 testCases := []struct {
900 desc string
901 configuredBootJars []string
902 apexContributionContents string
903 errorExpected bool
904 }{
905 {
906 desc: "Source apex is selected, and APEX_BOOT_JARS is correctly configured for source apex builds",
907 configuredBootJars: []string{"myapex:foo", "myapex:bar"},
908 },
909 {
910 desc: "Source apex is selected, and APEX_BOOT_JARS is missing bar",
911 configuredBootJars: []string{"myapex:foo"},
912 errorExpected: true,
913 },
914 {
915 desc: "Prebuilt apex is selected, and APEX_BOOT_JARS is correctly configured for prebuilt apex build",
916 configuredBootJars: []string{"myapex:foo"},
917 apexContributionContents: `"prebuilt_com.google.android.myapex"`,
918 },
919 {
920 desc: "Prebuilt apex is selected, and APEX_BOOT_JARS is missing foo",
921 configuredBootJars: []string{"myapex:bar"},
922 apexContributionContents: `"prebuilt_com.google.android.myapex"`,
923 errorExpected: true,
924 },
925 }
926
927 for _, tc := range testCases {
928 fixture := android.GroupFixturePreparers(
929 prepareForTestWithPlatformBootclasspath,
930 PrepareForTestWithApexBuildComponents,
931 prepareForTestWithMyapex,
932 java.FixtureConfigureApexBootJars(tc.configuredBootJars...),
Colin Crossa66b4632024-08-08 15:50:47 -0700933 android.PrepareForTestWithBuildFlag("RELEASE_APEX_CONTRIBUTIONS_ART", "my_apex_contributions"),
Spandan Das3d0d31a2024-05-03 21:36:48 +0000934 )
935 if tc.errorExpected {
936 fixture = fixture.ExtendWithErrorHandler(
937 android.FixtureExpectsAtLeastOneErrorMatchingPattern(`in contents.*must also be declared in PRODUCT_APEX_BOOT_JARS`),
938 )
939 }
940 fixture.RunTestWithBp(t, fmt.Sprintf(bp, tc.apexContributionContents))
941 }
942}