blob: 9f1e1e16f85cb2da6adf9a8e2a1a069f1cda7eb4 [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),
Jihoon Kangbd093452023-12-26 19:08:01 +0000257 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
258 variables.BuildFlags = map[string]string{
259 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
260 }
261 }),
Paul Duffinb432df92021-03-22 22:09:42 +0000262 ).RunTestWithBp(t, `
263 apex {
264 name: "com.android.art",
265 key: "com.android.art.key",
266 bootclasspath_fragments: [
267 "art-bootclasspath-fragment",
268 ],
269 updatable: false,
270 }
271
272 apex_key {
273 name: "com.android.art.key",
274 public_key: "com.android.art.avbpubkey",
275 private_key: "com.android.art.pem",
276 }
277
278 bootclasspath_fragment {
279 name: "art-bootclasspath-fragment",
satayevabcd5972021-08-06 17:49:46 +0100280 image_name: "art",
Paul Duffinb432df92021-03-22 22:09:42 +0000281 apex_available: [
282 "com.android.art",
283 ],
284 contents: [
285 "baz",
286 "quuz",
287 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100288 hidden_api: {
289 split_packages: ["*"],
290 },
Paul Duffinb432df92021-03-22 22:09:42 +0000291 }
292
293 java_library {
294 name: "baz",
295 apex_available: [
296 "com.android.art",
297 ],
298 srcs: ["b.java"],
299 installable: true,
300 }
301
302 // Add a java_import that is not preferred and so won't have an appropriate apex variant created
303 // for it to make sure that the platform_bootclasspath doesn't try and add a dependency onto it.
304 java_import {
305 name: "baz",
306 apex_available: [
307 "com.android.art",
308 ],
309 jars: ["b.jar"],
310 }
311
312 java_library {
313 name: "quuz",
314 apex_available: [
315 "com.android.art",
316 ],
317 srcs: ["b.java"],
318 installable: true,
319 }
320
321 apex {
322 name: "myapex",
323 key: "myapex.key",
Paul Duffin89f570a2021-06-16 01:42:33 +0100324 bootclasspath_fragments: [
325 "my-bootclasspath-fragment",
Paul Duffinb432df92021-03-22 22:09:42 +0000326 ],
327 updatable: false,
328 }
329
Paul Duffin89f570a2021-06-16 01:42:33 +0100330 bootclasspath_fragment {
331 name: "my-bootclasspath-fragment",
332 contents: ["bar"],
333 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100334 hidden_api: {
335 split_packages: ["*"],
336 },
Paul Duffin89f570a2021-06-16 01:42:33 +0100337 }
338
Paul Duffinb432df92021-03-22 22:09:42 +0000339 apex_key {
340 name: "myapex.key",
341 public_key: "testkey.avbpubkey",
342 private_key: "testkey.pem",
343 }
344
345 java_sdk_library {
346 name: "foo",
347 srcs: ["b.java"],
348 }
349
350 java_library {
351 name: "bar",
352 srcs: ["b.java"],
353 installable: true,
354 apex_available: ["myapex"],
355 permitted_packages: ["bar"],
356 }
357
358 platform_bootclasspath {
359 name: "myplatform-bootclasspath",
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100360
361 fragments: [
362 {
363 apex: "com.android.art",
364 module: "art-bootclasspath-fragment",
365 },
Paul Duffin89f570a2021-06-16 01:42:33 +0100366 {
367 apex: "myapex",
368 module: "my-bootclasspath-fragment",
369 },
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100370 ],
Paul Duffinb432df92021-03-22 22:09:42 +0000371 }
372`,
373 )
374
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100375 java.CheckPlatformBootclasspathModules(t, result, "myplatform-bootclasspath", []string{
Paul Duffin74431d52021-04-21 14:10:42 +0100376 // The configured contents of BootJars.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100377 "com.android.art:baz",
378 "com.android.art:quuz",
379 "platform:foo",
Paul Duffin74431d52021-04-21 14:10:42 +0100380
satayevd604b212021-07-21 14:23:52 +0100381 // The configured contents of ApexBootJars.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100382 "myapex:bar",
383 })
384
385 java.CheckPlatformBootclasspathFragments(t, result, "myplatform-bootclasspath", []string{
Paul Duffin89f570a2021-06-16 01:42:33 +0100386 "com.android.art:art-bootclasspath-fragment",
387 "myapex:my-bootclasspath-fragment",
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100388 })
389
Paul Duffinb432df92021-03-22 22:09:42 +0000390 // Make sure that the myplatform-bootclasspath has the correct dependencies.
391 CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{
Spandan Das64c9e0c2023-12-20 20:13:34 +0000392 // source vs prebuilt selection metadata module
393 `platform:all_apex_contributions`,
394
Paul Duffin74431d52021-04-21 14:10:42 +0100395 // The following are stubs.
Jihoon Kangbd093452023-12-26 19:08:01 +0000396 `platform:android_stubs_current_exportable`,
397 `platform:android_system_stubs_current_exportable`,
398 `platform:android_test_stubs_current_exportable`,
399 `platform:legacy.core.platform.api.stubs.exportable`,
Paul Duffin74431d52021-04-21 14:10:42 +0100400
401 // Needed for generating the boot image.
Paul Duffinb432df92021-03-22 22:09:42 +0000402 `platform:dex2oatd`,
Paul Duffin74431d52021-04-21 14:10:42 +0100403
404 // The configured contents of BootJars.
Paul Duffinb432df92021-03-22 22:09:42 +0000405 `com.android.art:baz`,
406 `com.android.art:quuz`,
407 `platform:foo`,
Paul Duffin74431d52021-04-21 14:10:42 +0100408
satayevd604b212021-07-21 14:23:52 +0100409 // The configured contents of ApexBootJars.
Paul Duffinb432df92021-03-22 22:09:42 +0000410 `myapex:bar`,
Paul Duffin74431d52021-04-21 14:10:42 +0100411
412 // The fragments.
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100413 `com.android.art:art-bootclasspath-fragment`,
Paul Duffin89f570a2021-06-16 01:42:33 +0100414 `myapex:my-bootclasspath-fragment`,
Paul Duffinb432df92021-03-22 22:09:42 +0000415 })
416}
417
Paul Duffin7487a7a2021-05-19 09:36:09 +0100418// TestPlatformBootclasspath_AlwaysUsePrebuiltSdks verifies that the build does not fail when
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100419// AlwaysUsePrebuiltSdk() returns true.
Paul Duffin7487a7a2021-05-19 09:36:09 +0100420func TestPlatformBootclasspath_AlwaysUsePrebuiltSdks(t *testing.T) {
421 result := android.GroupFixturePreparers(
422 prepareForTestWithPlatformBootclasspath,
423 prepareForTestWithMyapex,
424 // Configure two libraries, the first is a java_sdk_library whose prebuilt will be used because
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100425 // of AlwaysUsePrebuiltsSdk(). The second is a normal library that is unaffected. The order
426 // matters, so that the dependencies resolved by the platform_bootclasspath matches the
427 // configured list.
satayevd604b212021-07-21 14:23:52 +0100428 java.FixtureConfigureApexBootJars("myapex:foo", "myapex:bar"),
Paul Duffin7487a7a2021-05-19 09:36:09 +0100429 java.PrepareForTestWithJavaSdkLibraryFiles,
430 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
431 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true)
Jihoon Kangbd093452023-12-26 19:08:01 +0000432 variables.BuildFlags = map[string]string{
433 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
434 }
Paul Duffin7487a7a2021-05-19 09:36:09 +0100435 }),
436 java.FixtureWithPrebuiltApis(map[string][]string{
437 "current": {},
438 "30": {"foo"},
439 }),
440 ).RunTestWithBp(t, `
441 apex {
442 name: "myapex",
443 key: "myapex.key",
444 bootclasspath_fragments: [
445 "mybootclasspath-fragment",
446 ],
447 updatable: false,
448 }
449
450 apex_key {
451 name: "myapex.key",
452 public_key: "testkey.avbpubkey",
453 private_key: "testkey.pem",
454 }
455
456 java_library {
457 name: "bar",
458 srcs: ["b.java"],
459 installable: true,
460 apex_available: ["myapex"],
461 permitted_packages: ["bar"],
462 }
463
464 java_sdk_library {
465 name: "foo",
466 srcs: ["b.java"],
467 shared_library: false,
468 public: {
469 enabled: true,
470 },
471 apex_available: ["myapex"],
472 permitted_packages: ["foo"],
473 }
474
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100475 prebuilt_apex {
476 name: "myapex",
477 src: "myapex.apex",
478 exported_bootclasspath_fragments: ["mybootclasspath-fragment"],
479 }
480
Paul Duffin7487a7a2021-05-19 09:36:09 +0100481 // A prebuilt java_sdk_library_import that is not preferred by default but will be preferred
482 // because AlwaysUsePrebuiltSdks() is true.
483 java_sdk_library_import {
484 name: "foo",
485 prefer: false,
486 shared_library: false,
Paul Duffin630b11e2021-07-15 13:35:26 +0100487 permitted_packages: ["foo"],
Paul Duffin7487a7a2021-05-19 09:36:09 +0100488 public: {
489 jars: ["sdk_library/public/foo-stubs.jar"],
490 stub_srcs: ["sdk_library/public/foo_stub_sources"],
491 current_api: "sdk_library/public/foo.txt",
492 removed_api: "sdk_library/public/foo-removed.txt",
493 sdk_version: "current",
494 },
495 apex_available: ["myapex"],
496 }
497
498 // This always depends on the source foo module, its dependencies are not affected by the
499 // AlwaysUsePrebuiltSdks().
500 bootclasspath_fragment {
501 name: "mybootclasspath-fragment",
502 apex_available: [
503 "myapex",
504 ],
505 contents: [
506 "foo", "bar",
507 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100508 hidden_api: {
509 split_packages: ["*"],
510 },
Paul Duffin7487a7a2021-05-19 09:36:09 +0100511 }
512
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100513 prebuilt_bootclasspath_fragment {
514 name: "mybootclasspath-fragment",
515 apex_available: [
516 "myapex",
517 ],
518 contents: [
519 "foo",
520 ],
521 hidden_api: {
522 stub_flags: "",
523 annotation_flags: "",
524 metadata: "",
525 index: "",
526 all_flags: "",
527 },
528 }
529
Paul Duffin7487a7a2021-05-19 09:36:09 +0100530 platform_bootclasspath {
531 name: "myplatform-bootclasspath",
Paul Duffin89f570a2021-06-16 01:42:33 +0100532 fragments: [
533 {
534 apex: "myapex",
535 module:"mybootclasspath-fragment",
536 },
537 ],
Paul Duffin7487a7a2021-05-19 09:36:09 +0100538 }
539`,
540 )
541
542 java.CheckPlatformBootclasspathModules(t, result, "myplatform-bootclasspath", []string{
543 // The configured contents of BootJars.
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100544 "myapex:prebuilt_foo",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100545 "myapex:bar",
546 })
547
548 // Make sure that the myplatform-bootclasspath has the correct dependencies.
549 CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{
Spandan Das64c9e0c2023-12-20 20:13:34 +0000550 // source vs prebuilt selection metadata module
551 `platform:all_apex_contributions`,
552
Paul Duffin7487a7a2021-05-19 09:36:09 +0100553 // The following are stubs.
554 "platform:prebuilt_sdk_public_current_android",
555 "platform:prebuilt_sdk_system_current_android",
556 "platform:prebuilt_sdk_test_current_android",
557
558 // Not a prebuilt as no prebuilt existed when it was added.
Jihoon Kangbd093452023-12-26 19:08:01 +0000559 "platform:legacy.core.platform.api.stubs.exportable",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100560
Paul Duffin7487a7a2021-05-19 09:36:09 +0100561 // The platform_bootclasspath intentionally adds dependencies on both source and prebuilt
562 // modules when available as it does not know which one will be preferred.
Paul Duffin7487a7a2021-05-19 09:36:09 +0100563 "myapex:foo",
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100564 "myapex:prebuilt_foo",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100565
566 // Only a source module exists.
567 "myapex:bar",
Paul Duffin89f570a2021-06-16 01:42:33 +0100568
569 // The fragments.
570 "myapex:mybootclasspath-fragment",
Martin Stjernholmb1e61cb2021-09-08 21:56:18 +0100571 "myapex:prebuilt_mybootclasspath-fragment",
Paul Duffin7487a7a2021-05-19 09:36:09 +0100572 })
573}
574
Paul Duffinb432df92021-03-22 22:09:42 +0000575// CheckModuleDependencies checks the dependencies of the selected module against the expected list.
576//
577// The expected list must be a list of strings of the form "<apex>:<module>", where <apex> is the
578// name of the apex, or platform is it is not part of an apex and <module> is the module name.
579func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
580 t.Helper()
581 module := ctx.ModuleForTests(name, variant).Module()
582 modules := []android.Module{}
583 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
584 modules = append(modules, m.(android.Module))
585 })
586
587 pairs := java.ApexNamePairsFromModules(ctx, modules)
588 android.AssertDeepEquals(t, "module dependencies", expected, pairs)
589}
satayevb3090502021-06-15 17:49:10 +0100590
591// TestPlatformBootclasspath_IncludesRemainingApexJars verifies that any apex boot jar is present in
592// platform_bootclasspath's classpaths.proto config, if the apex does not generate its own config
593// by setting generate_classpaths_proto property to false.
594func TestPlatformBootclasspath_IncludesRemainingApexJars(t *testing.T) {
595 result := android.GroupFixturePreparers(
596 prepareForTestWithPlatformBootclasspath,
597 prepareForTestWithMyapex,
satayevd604b212021-07-21 14:23:52 +0100598 java.FixtureConfigureApexBootJars("myapex:foo"),
satayevb3090502021-06-15 17:49:10 +0100599 android.FixtureWithRootAndroidBp(`
600 platform_bootclasspath {
601 name: "platform-bootclasspath",
602 fragments: [
603 {
604 apex: "myapex",
605 module:"foo-fragment",
606 },
607 ],
608 }
609
610 apex {
611 name: "myapex",
612 key: "myapex.key",
613 bootclasspath_fragments: ["foo-fragment"],
614 updatable: false,
615 }
616
617 apex_key {
618 name: "myapex.key",
619 public_key: "testkey.avbpubkey",
620 private_key: "testkey.pem",
621 }
622
623 bootclasspath_fragment {
624 name: "foo-fragment",
625 generate_classpaths_proto: false,
626 contents: ["foo"],
627 apex_available: ["myapex"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100628 hidden_api: {
629 split_packages: ["*"],
630 },
satayevb3090502021-06-15 17:49:10 +0100631 }
632
633 java_library {
634 name: "foo",
635 srcs: ["a.java"],
636 system_modules: "none",
637 sdk_version: "none",
638 compile_dex: true,
639 apex_available: ["myapex"],
640 permitted_packages: ["foo"],
641 }
642 `),
643 ).RunTest(t)
644
645 java.CheckClasspathFragmentProtoContentInfoProvider(t, result,
646 true, // proto should be generated
647 "myapex:foo", // apex doesn't generate its own config, so must be in platform_bootclasspath
648 "bootclasspath.pb",
649 "out/soong/target/product/test_device/system/etc/classpaths",
650 )
651}
satayevd34eb0c2021-08-06 13:20:28 +0100652
653func TestBootJarNotInApex(t *testing.T) {
654 android.GroupFixturePreparers(
655 prepareForTestWithPlatformBootclasspath,
656 PrepareForTestWithApexBuildComponents,
657 prepareForTestWithMyapex,
658 java.FixtureConfigureApexBootJars("myapex:foo"),
659 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
660 `dependency "foo" of "myplatform-bootclasspath" missing variant`)).
661 RunTestWithBp(t, `
662 apex {
663 name: "myapex",
664 key: "myapex.key",
665 updatable: false,
666 }
667
668 apex_key {
669 name: "myapex.key",
670 public_key: "testkey.avbpubkey",
671 private_key: "testkey.pem",
672 }
673
674 java_library {
675 name: "foo",
676 srcs: ["b.java"],
677 installable: true,
678 apex_available: [
679 "myapex",
680 ],
681 }
682
683 bootclasspath_fragment {
684 name: "not-in-apex-fragment",
685 contents: [
686 "foo",
687 ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100688 hidden_api: {
689 split_packages: ["*"],
690 },
satayevd34eb0c2021-08-06 13:20:28 +0100691 }
692
693 platform_bootclasspath {
694 name: "myplatform-bootclasspath",
695 }
696 `)
697}
698
699func TestBootFragmentNotInApex(t *testing.T) {
700 android.GroupFixturePreparers(
701 prepareForTestWithPlatformBootclasspath,
702 PrepareForTestWithApexBuildComponents,
703 prepareForTestWithMyapex,
704 java.FixtureConfigureApexBootJars("myapex:foo"),
705 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
706 `library foo.*have no corresponding fragment.*`)).RunTestWithBp(t, `
707 apex {
708 name: "myapex",
709 key: "myapex.key",
710 java_libs: ["foo"],
711 updatable: false,
712 }
713
714 apex_key {
715 name: "myapex.key",
716 public_key: "testkey.avbpubkey",
717 private_key: "testkey.pem",
718 }
719
720 java_library {
721 name: "foo",
722 srcs: ["b.java"],
723 installable: true,
724 apex_available: ["myapex"],
725 permitted_packages: ["foo"],
726 }
727
728 bootclasspath_fragment {
729 name: "not-in-apex-fragment",
730 contents: ["foo"],
Paul Duffin9fd56472022-03-31 15:42:30 +0100731 hidden_api: {
732 split_packages: ["*"],
733 },
satayevd34eb0c2021-08-06 13:20:28 +0100734 }
735
736 platform_bootclasspath {
737 name: "myplatform-bootclasspath",
738 }
739 `)
740}
741
742func TestNonBootJarInFragment(t *testing.T) {
743 android.GroupFixturePreparers(
744 prepareForTestWithPlatformBootclasspath,
745 PrepareForTestWithApexBuildComponents,
746 prepareForTestWithMyapex,
747 java.FixtureConfigureApexBootJars("myapex:foo"),
748 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(
749 `in contents must also be declared in PRODUCT_APEX_BOOT_JARS`)).
750 RunTestWithBp(t, `
751 apex {
752 name: "myapex",
753 key: "myapex.key",
754 bootclasspath_fragments: ["apex-fragment"],
755 updatable: false,
756 }
757
758 apex_key {
759 name: "myapex.key",
760 public_key: "testkey.avbpubkey",
761 private_key: "testkey.pem",
762 }
763
764 java_library {
765 name: "foo",
766 srcs: ["b.java"],
767 installable: true,
768 apex_available: ["myapex"],
769 permitted_packages: ["foo"],
770 }
771
772 java_library {
773 name: "bar",
774 srcs: ["b.java"],
775 installable: true,
776 apex_available: ["myapex"],
777 permitted_packages: ["bar"],
778 }
779
780 bootclasspath_fragment {
781 name: "apex-fragment",
782 contents: ["foo", "bar"],
783 apex_available:[ "myapex" ],
Paul Duffin9fd56472022-03-31 15:42:30 +0100784 hidden_api: {
785 split_packages: ["*"],
786 },
satayevd34eb0c2021-08-06 13:20:28 +0100787 }
788
789 platform_bootclasspath {
790 name: "myplatform-bootclasspath",
791 fragments: [{
792 apex: "myapex",
793 module:"apex-fragment",
794 }],
795 }
796 `)
797}
Spandan Das3d0d31a2024-05-03 21:36:48 +0000798
799// Source and prebuilt apex provide different set of boot jars
800func TestNonBootJarMissingInPrebuiltFragment(t *testing.T) {
801 bp := `
802 apex {
803 name: "myapex",
804 key: "myapex.key",
805 bootclasspath_fragments: ["apex-fragment"],
806 updatable: false,
807 }
808
809 apex_key {
810 name: "myapex.key",
811 public_key: "testkey.avbpubkey",
812 private_key: "testkey.pem",
813 }
814
815 java_library {
816 name: "foo",
817 srcs: ["b.java"],
818 installable: true,
819 apex_available: ["myapex"],
820 permitted_packages: ["foo"],
821 }
822
823 java_library {
824 name: "bar",
825 srcs: ["b.java"],
826 installable: true,
827 apex_available: ["myapex"],
828 permitted_packages: ["bar"],
829 }
830
831 bootclasspath_fragment {
832 name: "apex-fragment",
833 contents: ["foo", "bar"],
834 apex_available:[ "myapex" ],
835 hidden_api: {
836 split_packages: ["*"],
837 },
838 }
839
840 prebuilt_apex {
841 name: "com.google.android.myapex", // mainline prebuilt selection logic in soong relies on the naming convention com.google.android
842 apex_name: "myapex",
843 source_apex_name: "myapex",
844 src: "myapex.apex",
845 exported_bootclasspath_fragments: ["apex-fragment"],
846 }
847
848 java_import {
849 name: "foo",
850 jars: ["foo.jar"],
851 apex_available: ["myapex"],
852 permitted_packages: ["foo"],
853 }
854
855 prebuilt_bootclasspath_fragment {
856 name: "apex-fragment",
857 contents: ["foo"], // Unlike the source fragment, this is missing bar
858 apex_available:[ "myapex" ],
859 hidden_api: {
860 annotation_flags: "my-bootclasspath-fragment/annotation-flags.csv",
861 metadata: "my-bootclasspath-fragment/metadata.csv",
862 index: "my-bootclasspath-fragment/index.csv",
863 stub_flags: "my-bootclasspath-fragment/stub-flags.csv",
864 all_flags: "my-bootclasspath-fragment/all-flags.csv",
865 },
866 }
867
868 apex_contributions {
869 name: "my_apex_contributions",
870 api_domain: "myapex",
871 contents: [%v],
872 }
873 `
874 testCases := []struct {
875 desc string
876 configuredBootJars []string
877 apexContributionContents string
878 errorExpected bool
879 }{
880 {
881 desc: "Source apex is selected, and APEX_BOOT_JARS is correctly configured for source apex builds",
882 configuredBootJars: []string{"myapex:foo", "myapex:bar"},
883 },
884 {
885 desc: "Source apex is selected, and APEX_BOOT_JARS is missing bar",
886 configuredBootJars: []string{"myapex:foo"},
887 errorExpected: true,
888 },
889 {
890 desc: "Prebuilt apex is selected, and APEX_BOOT_JARS is correctly configured for prebuilt apex build",
891 configuredBootJars: []string{"myapex:foo"},
892 apexContributionContents: `"prebuilt_com.google.android.myapex"`,
893 },
894 {
895 desc: "Prebuilt apex is selected, and APEX_BOOT_JARS is missing foo",
896 configuredBootJars: []string{"myapex:bar"},
897 apexContributionContents: `"prebuilt_com.google.android.myapex"`,
898 errorExpected: true,
899 },
900 }
901
902 for _, tc := range testCases {
903 fixture := android.GroupFixturePreparers(
904 prepareForTestWithPlatformBootclasspath,
905 PrepareForTestWithApexBuildComponents,
906 prepareForTestWithMyapex,
907 java.FixtureConfigureApexBootJars(tc.configuredBootJars...),
908 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
909 variables.BuildFlags = map[string]string{
910 "RELEASE_APEX_CONTRIBUTIONS_ART": "my_apex_contributions",
911 }
912 }),
913 )
914 if tc.errorExpected {
915 fixture = fixture.ExtendWithErrorHandler(
916 android.FixtureExpectsAtLeastOneErrorMatchingPattern(`in contents.*must also be declared in PRODUCT_APEX_BOOT_JARS`),
917 )
918 }
919 fixture.RunTestWithBp(t, fmt.Sprintf(bp, tc.apexContributionContents))
920 }
921}