blob: a942dc653dc00c771e065af9a5c4d5d22880366a [file] [log] [blame]
Makoto Onuki4a9869d2023-10-20 10:42:47 -07001// Copyright 2023 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package java
15
16import (
John Wufe462932024-11-10 02:55:53 +000017 "strconv"
18
Makoto Onuki4a9869d2023-10-20 10:42:47 -070019 "android/soong/android"
20 "android/soong/tradefed"
21
Makoto Onuki2ca84272024-02-10 00:15:21 +000022 "github.com/google/blueprint"
Makoto Onuki4a9869d2023-10-20 10:42:47 -070023 "github.com/google/blueprint/proptools"
24)
25
26func init() {
27 RegisterRavenwoodBuildComponents(android.InitRegistrationContext)
28}
29
30func RegisterRavenwoodBuildComponents(ctx android.RegistrationContext) {
31 ctx.RegisterModuleType("android_ravenwood_test", ravenwoodTestFactory)
32 ctx.RegisterModuleType("android_ravenwood_libgroup", ravenwoodLibgroupFactory)
33}
34
Makoto Onuki2ca84272024-02-10 00:15:21 +000035var ravenwoodLibContentTag = dependencyTag{name: "ravenwoodlibcontent"}
36var ravenwoodUtilsTag = dependencyTag{name: "ravenwoodutils"}
37var ravenwoodRuntimeTag = dependencyTag{name: "ravenwoodruntime"}
Makoto Onuki3380f6d2024-05-10 16:00:28 -070038var ravenwoodTestResourceApkTag = dependencyTag{name: "ravenwoodtestresapk"}
Makoto Onukied392f72024-09-17 09:56:33 -070039var ravenwoodTestInstResourceApkTag = dependencyTag{name: "ravenwoodtest-inst-res-apk"}
Makoto Onuki4a9869d2023-10-20 10:42:47 -070040
John Wufe462932024-11-10 02:55:53 +000041var genManifestProperties = pctx.AndroidStaticRule("genManifestProperties",
42 blueprint.RuleParams{
43 Command: "echo targetSdkVersionInt=$targetSdkVersionInt > $out && " +
44 "echo targetSdkVersionRaw=$targetSdkVersionRaw >> $out && " +
45 "echo packageName=$packageName >> $out && " +
46 "echo instPackageName=$instPackageName >> $out",
47 }, "targetSdkVersionInt", "targetSdkVersionRaw", "packageName", "instPackageName")
48
Makoto Onuki4a9869d2023-10-20 10:42:47 -070049const ravenwoodUtilsName = "ravenwood-utils"
50const ravenwoodRuntimeName = "ravenwood-runtime"
51
Makoto Onuki2ca84272024-02-10 00:15:21 +000052type ravenwoodLibgroupJniDepProviderInfo struct {
53 // All the jni_libs module names with transient dependencies.
54 names map[string]bool
55}
56
57var ravenwoodLibgroupJniDepProvider = blueprint.NewProvider[ravenwoodLibgroupJniDepProviderInfo]()
58
Makoto Onuki68676572024-02-02 13:29:01 -080059func getLibPath(archType android.ArchType) string {
60 if archType.Multilib == "lib64" {
61 return "lib64"
62 }
63 return "lib"
64}
65
66type ravenwoodTestProperties struct {
Jihoon Kang371a0372024-10-01 16:44:41 +000067 Jni_libs proptools.Configurable[[]string]
Makoto Onuki3380f6d2024-05-10 16:00:28 -070068
69 // Specify another android_app module here to copy it to the test directory, so that
Makoto Onukied392f72024-09-17 09:56:33 -070070 // the ravenwood test can access it. This APK will be loaded as resources of the test
71 // target app.
Makoto Onuki3380f6d2024-05-10 16:00:28 -070072 // TODO: For now, we simply refer to another android_app module and copy it to the
73 // test directory. Eventually, android_ravenwood_test should support all the resource
74 // related properties and build resources from the `res/` directory.
75 Resource_apk *string
Makoto Onukied392f72024-09-17 09:56:33 -070076
77 // Specify another android_app module here to copy it to the test directory, so that
78 // the ravenwood test can access it. This APK will be loaded as resources of the test
79 // instrumentation app itself.
80 Inst_resource_apk *string
John Wufe462932024-11-10 02:55:53 +000081
82 // Specify the package name of the test target apk.
83 // This will be set to the target Context's package name.
84 // (i.e. Instrumentation.getTargetContext().getPackageName())
85 // If this is omitted, Package_name will be used.
86 Package_name *string
87
88 // Specify the package name of this test module.
89 // This will be set to the test Context's package name.
90 //(i.e. Instrumentation.getContext().getPackageName())
91 Inst_package_name *string
Makoto Onuki68676572024-02-02 13:29:01 -080092}
93
Makoto Onuki4a9869d2023-10-20 10:42:47 -070094type ravenwoodTest struct {
95 Library
96
Makoto Onuki68676572024-02-02 13:29:01 -080097 ravenwoodTestProperties ravenwoodTestProperties
98
Makoto Onuki4a9869d2023-10-20 10:42:47 -070099 testProperties testProperties
100 testConfig android.Path
101
102 forceOSType android.OsType
103 forceArchType android.ArchType
104}
105
106func ravenwoodTestFactory() android.Module {
107 module := &ravenwoodTest{}
108
109 module.addHostAndDeviceProperties()
Makoto Onuki68676572024-02-02 13:29:01 -0800110 module.AddProperties(&module.testProperties, &module.ravenwoodTestProperties)
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700111
112 module.Module.dexpreopter.isTest = true
Cole Faustc7315282025-01-10 15:37:01 -0800113 module.Module.linter.properties.Lint.Test_module_type = proptools.BoolPtr(true)
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700114
Jeff Sharkey4bbf86f2023-11-30 09:29:50 -0700115 module.testProperties.Test_suites = []string{
116 "general-tests",
117 "ravenwood-tests",
118 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700119 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(false)
Ronald Braunstein238a3e32025-03-12 17:03:08 -0700120 module.Module.sourceProperties.Test_only = proptools.BoolPtr(true)
121 module.Module.sourceProperties.Top_level_test_target = true
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700122
123 InitJavaModule(module, android.DeviceSupported)
124 android.InitDefaultableModule(module)
125
126 return module
127}
128
129func (r *ravenwoodTest) InstallInTestcases() bool { return true }
130func (r *ravenwoodTest) InstallForceOS() (*android.OsType, *android.ArchType) {
131 return &r.forceOSType, &r.forceArchType
132}
133func (r *ravenwoodTest) TestSuites() []string {
134 return r.testProperties.Test_suites
135}
136
137func (r *ravenwoodTest) DepsMutator(ctx android.BottomUpMutatorContext) {
138 r.Library.DepsMutator(ctx)
139
140 // Generically depend on the runtime so that it's installed together with us
Makoto Onuki2ca84272024-02-10 00:15:21 +0000141 ctx.AddVariationDependencies(nil, ravenwoodRuntimeTag, ravenwoodRuntimeName)
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700142
143 // Directly depend on any utils so that we link against them
Makoto Onuki2ca84272024-02-10 00:15:21 +0000144 utils := ctx.AddVariationDependencies(nil, ravenwoodUtilsTag, ravenwoodUtilsName)[0]
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700145 if utils != nil {
146 for _, lib := range utils.(*ravenwoodLibgroup).ravenwoodLibgroupProperties.Libs {
147 ctx.AddVariationDependencies(nil, libTag, lib)
148 }
149 }
Makoto Onuki68676572024-02-02 13:29:01 -0800150
151 // Add jni libs
Jihoon Kang371a0372024-10-01 16:44:41 +0000152 for _, lib := range r.ravenwoodTestProperties.Jni_libs.GetOrDefault(ctx, nil) {
Makoto Onuki2ca84272024-02-10 00:15:21 +0000153 ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), jniLibTag, lib)
Makoto Onuki68676572024-02-02 13:29:01 -0800154 }
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700155
156 // Resources APK
157 if resourceApk := proptools.String(r.ravenwoodTestProperties.Resource_apk); resourceApk != "" {
158 ctx.AddVariationDependencies(nil, ravenwoodTestResourceApkTag, resourceApk)
159 }
Makoto Onukied392f72024-09-17 09:56:33 -0700160
161 if resourceApk := proptools.String(r.ravenwoodTestProperties.Inst_resource_apk); resourceApk != "" {
162 ctx.AddVariationDependencies(nil, ravenwoodTestInstResourceApkTag, resourceApk)
163 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700164}
165
166func (r *ravenwoodTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
167 r.forceOSType = ctx.Config().BuildOS
168 r.forceArchType = ctx.Config().BuildArch
169
170 r.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
171 TestConfigProp: r.testProperties.Test_config,
172 TestConfigTemplateProp: r.testProperties.Test_config_template,
173 TestSuites: r.testProperties.Test_suites,
174 AutoGenConfig: r.testProperties.Auto_gen_config,
175 DeviceTemplate: "${RavenwoodTestConfigTemplate}",
176 HostTemplate: "${RavenwoodTestConfigTemplate}",
177 })
178
Makoto Onuki7ded3822024-03-28 14:42:20 -0700179 // Always enable Ravenizer for ravenwood tests.
180 r.Library.ravenizer.enabled = true
181
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700182 r.Library.GenerateAndroidBuildActions(ctx)
183
Makoto Onuki2ca84272024-02-10 00:15:21 +0000184 // Start by depending on all files installed by dependencies
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700185 var installDeps android.InstallPaths
Makoto Onuki2ca84272024-02-10 00:15:21 +0000186
187 // All JNI libraries included in the runtime
188 var runtimeJniModuleNames map[string]bool
189
Yu Liu7eebf8b2025-01-17 00:23:57 +0000190 utils := ctx.GetDirectDepsProxyWithTag(ravenwoodUtilsTag)[0]
191 for _, installFile := range android.OtherModuleProviderOrDefault(
192 ctx, utils, android.InstallFilesProvider).InstallFiles {
193 installDeps = append(installDeps, installFile)
194 }
195 jniDeps, ok := android.OtherModuleProvider(ctx, utils, ravenwoodLibgroupJniDepProvider)
196 if ok {
197 runtimeJniModuleNames = jniDeps.names
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700198 }
199
Yu Liu7eebf8b2025-01-17 00:23:57 +0000200 runtime := ctx.GetDirectDepsProxyWithTag(ravenwoodRuntimeTag)[0]
201 for _, installFile := range android.OtherModuleProviderOrDefault(
202 ctx, runtime, android.InstallFilesProvider).InstallFiles {
203 installDeps = append(installDeps, installFile)
204 }
205 jniDeps, ok = android.OtherModuleProvider(ctx, runtime, ravenwoodLibgroupJniDepProvider)
206 if ok {
207 runtimeJniModuleNames = jniDeps.names
Makoto Onuki2ca84272024-02-10 00:15:21 +0000208 }
209
210 // Also remember what JNI libs are in the runtime.
211
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700212 // Also depend on our config
213 installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
214 installConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
215 installDeps = append(installDeps, installConfig)
216
Makoto Onuki2ca84272024-02-10 00:15:21 +0000217 // Depend on the JNI libraries, but don't install the ones that the runtime already
218 // contains.
Makoto Onuki68676572024-02-02 13:29:01 -0800219 soInstallPath := installPath.Join(ctx, getLibPath(r.forceArchType))
Makoto Onuki2ca84272024-02-10 00:15:21 +0000220 for _, jniLib := range collectTransitiveJniDeps(ctx) {
221 if _, ok := runtimeJniModuleNames[jniLib.name]; ok {
222 continue // Runtime already includes it.
223 }
224 installJni := ctx.InstallFile(soInstallPath, jniLib.path.Base(), jniLib.path)
Makoto Onuki68676572024-02-02 13:29:01 -0800225 installDeps = append(installDeps, installJni)
226 }
227
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700228 resApkInstallPath := installPath.Join(ctx, "ravenwood-res-apks")
Makoto Onukied392f72024-09-17 09:56:33 -0700229
230 copyResApk := func(tag blueprint.DependencyTag, toFileName string) {
Yu Liu7eebf8b2025-01-17 00:23:57 +0000231 if resApk := ctx.GetDirectDepsProxyWithTag(tag); len(resApk) > 0 {
Makoto Onukied392f72024-09-17 09:56:33 -0700232 installFile := android.OutputFileForModule(ctx, resApk[0], "")
233 installResApk := ctx.InstallFile(resApkInstallPath, toFileName, installFile)
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700234 installDeps = append(installDeps, installResApk)
235 }
236 }
Makoto Onukied392f72024-09-17 09:56:33 -0700237 copyResApk(ravenwoodTestResourceApkTag, "ravenwood-res.apk")
238 copyResApk(ravenwoodTestInstResourceApkTag, "ravenwood-inst-res.apk")
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700239
John Wufe462932024-11-10 02:55:53 +0000240 // Generate manifest properties
241 propertiesOutputPath := android.PathForModuleGen(ctx, "ravenwood.properties")
242
243 targetSdkVersion := proptools.StringDefault(r.deviceProperties.Target_sdk_version, "")
244 targetSdkVersionInt := r.TargetSdkVersion(ctx).FinalOrFutureInt() // FinalOrFutureInt may be 10000.
245 packageName := proptools.StringDefault(r.ravenwoodTestProperties.Package_name, "")
246 instPackageName := proptools.StringDefault(r.ravenwoodTestProperties.Inst_package_name, "")
247 ctx.Build(pctx, android.BuildParams{
248 Rule: genManifestProperties,
249 Description: "genManifestProperties",
250 Output: propertiesOutputPath,
251 Args: map[string]string{
252 "targetSdkVersionInt": strconv.Itoa(targetSdkVersionInt),
253 "targetSdkVersionRaw": targetSdkVersion,
254 "packageName": packageName,
255 "instPackageName": instPackageName,
256 },
257 })
258 installProps := ctx.InstallFile(installPath, "ravenwood.properties", propertiesOutputPath)
259 installDeps = append(installDeps, installProps)
260
Makoto Onuki68676572024-02-02 13:29:01 -0800261 // Install our JAR with all dependencies
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700262 ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.outputFile, installDeps...)
Jihoon Kangd4063812025-01-24 00:25:30 +0000263
264 moduleInfoJSON := ctx.ModuleInfoJSON()
265 if _, ok := r.testConfig.(android.WritablePath); ok {
266 moduleInfoJSON.AutoTestConfig = []string{"true"}
267 }
268 if r.testConfig != nil {
269 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, r.testConfig.String())
270 }
271 moduleInfoJSON.CompatibilitySuites = []string{"general-tests", "ravenwood-tests"}
Yu Liu367827f2025-02-15 00:18:33 +0000272
273 android.SetProvider(ctx, android.TestSuiteInfoProvider, android.TestSuiteInfo{
274 TestSuites: r.TestSuites(),
275 })
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700276}
277
278func (r *ravenwoodTest) AndroidMkEntries() []android.AndroidMkEntries {
279 entriesList := r.Library.AndroidMkEntries()
280 entries := &entriesList[0]
281 entries.ExtraEntries = append(entries.ExtraEntries,
282 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
283 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
Jeff Sharkey4bbf86f2023-11-30 09:29:50 -0700284 entries.AddStrings("LOCAL_COMPATIBILITY_SUITE",
285 "general-tests", "ravenwood-tests")
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700286 if r.testConfig != nil {
287 entries.SetPath("LOCAL_FULL_TEST_CONFIG", r.testConfig)
288 }
289 })
290 return entriesList
291}
292
293type ravenwoodLibgroupProperties struct {
294 Libs []string
Makoto Onuki68676572024-02-02 13:29:01 -0800295
Jihoon Kang371a0372024-10-01 16:44:41 +0000296 Jni_libs proptools.Configurable[[]string]
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700297
298 // We use this to copy framework-res.apk to the ravenwood runtime directory.
John Wu680cd732024-09-13 20:59:05 +0000299 Data []string `android:"path,arch_variant"`
300
301 // We use this to copy font files to the ravenwood runtime directory.
302 Fonts []string `android:"path,arch_variant"`
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700303}
304
305type ravenwoodLibgroup struct {
306 android.ModuleBase
307
308 ravenwoodLibgroupProperties ravenwoodLibgroupProperties
309
310 forceOSType android.OsType
311 forceArchType android.ArchType
312}
313
314func ravenwoodLibgroupFactory() android.Module {
315 module := &ravenwoodLibgroup{}
316 module.AddProperties(&module.ravenwoodLibgroupProperties)
317
318 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
319 return module
320}
321
322func (r *ravenwoodLibgroup) InstallInTestcases() bool { return true }
323func (r *ravenwoodLibgroup) InstallForceOS() (*android.OsType, *android.ArchType) {
324 return &r.forceOSType, &r.forceArchType
325}
326func (r *ravenwoodLibgroup) TestSuites() []string {
Jeff Sharkey4bbf86f2023-11-30 09:29:50 -0700327 return []string{
328 "general-tests",
329 "ravenwood-tests",
330 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700331}
332
333func (r *ravenwoodLibgroup) DepsMutator(ctx android.BottomUpMutatorContext) {
334 // Always depends on our underlying libs
335 for _, lib := range r.ravenwoodLibgroupProperties.Libs {
Makoto Onuki2ca84272024-02-10 00:15:21 +0000336 ctx.AddVariationDependencies(nil, ravenwoodLibContentTag, lib)
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700337 }
Jihoon Kang371a0372024-10-01 16:44:41 +0000338 for _, lib := range r.ravenwoodLibgroupProperties.Jni_libs.GetOrDefault(ctx, nil) {
Makoto Onuki2ca84272024-02-10 00:15:21 +0000339 ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), jniLibTag, lib)
Makoto Onuki68676572024-02-02 13:29:01 -0800340 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700341}
342
343func (r *ravenwoodLibgroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
344 r.forceOSType = ctx.Config().BuildOS
345 r.forceArchType = ctx.Config().BuildArch
346
Makoto Onuki2ca84272024-02-10 00:15:21 +0000347 // Collect the JNI dependencies, including the transitive deps.
348 jniDepNames := make(map[string]bool)
349 jniLibs := collectTransitiveJniDeps(ctx)
350
351 for _, jni := range jniLibs {
352 jniDepNames[jni.name] = true
353 }
354 android.SetProvider(ctx, ravenwoodLibgroupJniDepProvider, ravenwoodLibgroupJniDepProviderInfo{
355 names: jniDepNames,
356 })
357
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700358 // Install our runtime into expected location for packaging
359 installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
360 for _, lib := range r.ravenwoodLibgroupProperties.Libs {
Yu Liu7eebf8b2025-01-17 00:23:57 +0000361 libModule := ctx.GetDirectDepProxyWithTag(lib, ravenwoodLibContentTag)
Jerome Gaillard44fc5bf2024-07-30 16:11:59 +0000362 if libModule == nil {
363 if ctx.Config().AllowMissingDependencies() {
364 ctx.AddMissingDependencies([]string{lib})
365 } else {
366 ctx.PropertyErrorf("lib", "missing dependency %q", lib)
367 }
368 continue
369 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700370 libJar := android.OutputFileForModule(ctx, libModule, "")
371 ctx.InstallFile(installPath, lib+".jar", libJar)
372 }
Makoto Onuki68676572024-02-02 13:29:01 -0800373 soInstallPath := android.PathForModuleInstall(ctx, r.BaseModuleName()).Join(ctx, getLibPath(r.forceArchType))
Makoto Onuki2ca84272024-02-10 00:15:21 +0000374
375 for _, jniLib := range jniLibs {
376 ctx.InstallFile(soInstallPath, jniLib.path.Base(), jniLib.path)
Makoto Onuki68676572024-02-02 13:29:01 -0800377 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700378
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700379 dataInstallPath := installPath.Join(ctx, "ravenwood-data")
John Wu680cd732024-09-13 20:59:05 +0000380 data := android.PathsForModuleSrc(ctx, r.ravenwoodLibgroupProperties.Data)
381 for _, file := range data {
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700382 ctx.InstallFile(dataInstallPath, file.Base(), file)
383 }
384
John Wu680cd732024-09-13 20:59:05 +0000385 fontsInstallPath := installPath.Join(ctx, "fonts")
386 fonts := android.PathsForModuleSrc(ctx, r.ravenwoodLibgroupProperties.Fonts)
387 for _, file := range fonts {
388 ctx.InstallFile(fontsInstallPath, file.Base(), file)
389 }
390
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700391 // Normal build should perform install steps
392 ctx.Phony(r.BaseModuleName(), android.PathForPhony(ctx, r.BaseModuleName()+"-install"))
Yu Liu367827f2025-02-15 00:18:33 +0000393
394 android.SetProvider(ctx, android.TestSuiteInfoProvider, android.TestSuiteInfo{
395 TestSuites: r.TestSuites(),
396 })
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700397}
Makoto Onuki2ca84272024-02-10 00:15:21 +0000398
399// collectTransitiveJniDeps returns all JNI dependencies, including transitive
400// ones, including NDK / stub libs. (Because Ravenwood has no "preinstalled" libraries)
401func collectTransitiveJniDeps(ctx android.ModuleContext) []jniLib {
402 libs, _ := collectJniDeps(ctx, true, false, nil)
403 return libs
404}