blob: 3b6c80bc6b8f22fe884855102d4beca0527d811c [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)
120
121 InitJavaModule(module, android.DeviceSupported)
122 android.InitDefaultableModule(module)
123
124 return module
125}
126
127func (r *ravenwoodTest) InstallInTestcases() bool { return true }
128func (r *ravenwoodTest) InstallForceOS() (*android.OsType, *android.ArchType) {
129 return &r.forceOSType, &r.forceArchType
130}
131func (r *ravenwoodTest) TestSuites() []string {
132 return r.testProperties.Test_suites
133}
134
135func (r *ravenwoodTest) DepsMutator(ctx android.BottomUpMutatorContext) {
136 r.Library.DepsMutator(ctx)
137
138 // Generically depend on the runtime so that it's installed together with us
Makoto Onuki2ca84272024-02-10 00:15:21 +0000139 ctx.AddVariationDependencies(nil, ravenwoodRuntimeTag, ravenwoodRuntimeName)
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700140
141 // Directly depend on any utils so that we link against them
Makoto Onuki2ca84272024-02-10 00:15:21 +0000142 utils := ctx.AddVariationDependencies(nil, ravenwoodUtilsTag, ravenwoodUtilsName)[0]
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700143 if utils != nil {
144 for _, lib := range utils.(*ravenwoodLibgroup).ravenwoodLibgroupProperties.Libs {
145 ctx.AddVariationDependencies(nil, libTag, lib)
146 }
147 }
Makoto Onuki68676572024-02-02 13:29:01 -0800148
149 // Add jni libs
Jihoon Kang371a0372024-10-01 16:44:41 +0000150 for _, lib := range r.ravenwoodTestProperties.Jni_libs.GetOrDefault(ctx, nil) {
Makoto Onuki2ca84272024-02-10 00:15:21 +0000151 ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), jniLibTag, lib)
Makoto Onuki68676572024-02-02 13:29:01 -0800152 }
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700153
154 // Resources APK
155 if resourceApk := proptools.String(r.ravenwoodTestProperties.Resource_apk); resourceApk != "" {
156 ctx.AddVariationDependencies(nil, ravenwoodTestResourceApkTag, resourceApk)
157 }
Makoto Onukied392f72024-09-17 09:56:33 -0700158
159 if resourceApk := proptools.String(r.ravenwoodTestProperties.Inst_resource_apk); resourceApk != "" {
160 ctx.AddVariationDependencies(nil, ravenwoodTestInstResourceApkTag, resourceApk)
161 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700162}
163
164func (r *ravenwoodTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
165 r.forceOSType = ctx.Config().BuildOS
166 r.forceArchType = ctx.Config().BuildArch
167
168 r.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
169 TestConfigProp: r.testProperties.Test_config,
170 TestConfigTemplateProp: r.testProperties.Test_config_template,
171 TestSuites: r.testProperties.Test_suites,
172 AutoGenConfig: r.testProperties.Auto_gen_config,
173 DeviceTemplate: "${RavenwoodTestConfigTemplate}",
174 HostTemplate: "${RavenwoodTestConfigTemplate}",
175 })
176
Makoto Onuki7ded3822024-03-28 14:42:20 -0700177 // Always enable Ravenizer for ravenwood tests.
178 r.Library.ravenizer.enabled = true
179
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700180 r.Library.GenerateAndroidBuildActions(ctx)
181
Makoto Onuki2ca84272024-02-10 00:15:21 +0000182 // Start by depending on all files installed by dependencies
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700183 var installDeps android.InstallPaths
Makoto Onuki2ca84272024-02-10 00:15:21 +0000184
185 // All JNI libraries included in the runtime
186 var runtimeJniModuleNames map[string]bool
187
Yu Liu7eebf8b2025-01-17 00:23:57 +0000188 utils := ctx.GetDirectDepsProxyWithTag(ravenwoodUtilsTag)[0]
189 for _, installFile := range android.OtherModuleProviderOrDefault(
190 ctx, utils, android.InstallFilesProvider).InstallFiles {
191 installDeps = append(installDeps, installFile)
192 }
193 jniDeps, ok := android.OtherModuleProvider(ctx, utils, ravenwoodLibgroupJniDepProvider)
194 if ok {
195 runtimeJniModuleNames = jniDeps.names
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700196 }
197
Yu Liu7eebf8b2025-01-17 00:23:57 +0000198 runtime := ctx.GetDirectDepsProxyWithTag(ravenwoodRuntimeTag)[0]
199 for _, installFile := range android.OtherModuleProviderOrDefault(
200 ctx, runtime, android.InstallFilesProvider).InstallFiles {
201 installDeps = append(installDeps, installFile)
202 }
203 jniDeps, ok = android.OtherModuleProvider(ctx, runtime, ravenwoodLibgroupJniDepProvider)
204 if ok {
205 runtimeJniModuleNames = jniDeps.names
Makoto Onuki2ca84272024-02-10 00:15:21 +0000206 }
207
208 // Also remember what JNI libs are in the runtime.
209
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700210 // Also depend on our config
211 installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
212 installConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
213 installDeps = append(installDeps, installConfig)
214
Makoto Onuki2ca84272024-02-10 00:15:21 +0000215 // Depend on the JNI libraries, but don't install the ones that the runtime already
216 // contains.
Makoto Onuki68676572024-02-02 13:29:01 -0800217 soInstallPath := installPath.Join(ctx, getLibPath(r.forceArchType))
Makoto Onuki2ca84272024-02-10 00:15:21 +0000218 for _, jniLib := range collectTransitiveJniDeps(ctx) {
219 if _, ok := runtimeJniModuleNames[jniLib.name]; ok {
220 continue // Runtime already includes it.
221 }
222 installJni := ctx.InstallFile(soInstallPath, jniLib.path.Base(), jniLib.path)
Makoto Onuki68676572024-02-02 13:29:01 -0800223 installDeps = append(installDeps, installJni)
224 }
225
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700226 resApkInstallPath := installPath.Join(ctx, "ravenwood-res-apks")
Makoto Onukied392f72024-09-17 09:56:33 -0700227
228 copyResApk := func(tag blueprint.DependencyTag, toFileName string) {
Yu Liu7eebf8b2025-01-17 00:23:57 +0000229 if resApk := ctx.GetDirectDepsProxyWithTag(tag); len(resApk) > 0 {
Makoto Onukied392f72024-09-17 09:56:33 -0700230 installFile := android.OutputFileForModule(ctx, resApk[0], "")
231 installResApk := ctx.InstallFile(resApkInstallPath, toFileName, installFile)
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700232 installDeps = append(installDeps, installResApk)
233 }
234 }
Makoto Onukied392f72024-09-17 09:56:33 -0700235 copyResApk(ravenwoodTestResourceApkTag, "ravenwood-res.apk")
236 copyResApk(ravenwoodTestInstResourceApkTag, "ravenwood-inst-res.apk")
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700237
John Wufe462932024-11-10 02:55:53 +0000238 // Generate manifest properties
239 propertiesOutputPath := android.PathForModuleGen(ctx, "ravenwood.properties")
240
241 targetSdkVersion := proptools.StringDefault(r.deviceProperties.Target_sdk_version, "")
242 targetSdkVersionInt := r.TargetSdkVersion(ctx).FinalOrFutureInt() // FinalOrFutureInt may be 10000.
243 packageName := proptools.StringDefault(r.ravenwoodTestProperties.Package_name, "")
244 instPackageName := proptools.StringDefault(r.ravenwoodTestProperties.Inst_package_name, "")
245 ctx.Build(pctx, android.BuildParams{
246 Rule: genManifestProperties,
247 Description: "genManifestProperties",
248 Output: propertiesOutputPath,
249 Args: map[string]string{
250 "targetSdkVersionInt": strconv.Itoa(targetSdkVersionInt),
251 "targetSdkVersionRaw": targetSdkVersion,
252 "packageName": packageName,
253 "instPackageName": instPackageName,
254 },
255 })
256 installProps := ctx.InstallFile(installPath, "ravenwood.properties", propertiesOutputPath)
257 installDeps = append(installDeps, installProps)
258
Makoto Onuki68676572024-02-02 13:29:01 -0800259 // Install our JAR with all dependencies
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700260 ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.outputFile, installDeps...)
Jihoon Kangd4063812025-01-24 00:25:30 +0000261
262 moduleInfoJSON := ctx.ModuleInfoJSON()
263 if _, ok := r.testConfig.(android.WritablePath); ok {
264 moduleInfoJSON.AutoTestConfig = []string{"true"}
265 }
266 if r.testConfig != nil {
267 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, r.testConfig.String())
268 }
269 moduleInfoJSON.CompatibilitySuites = []string{"general-tests", "ravenwood-tests"}
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700270}
271
272func (r *ravenwoodTest) AndroidMkEntries() []android.AndroidMkEntries {
273 entriesList := r.Library.AndroidMkEntries()
274 entries := &entriesList[0]
275 entries.ExtraEntries = append(entries.ExtraEntries,
276 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
277 entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
Jeff Sharkey4bbf86f2023-11-30 09:29:50 -0700278 entries.AddStrings("LOCAL_COMPATIBILITY_SUITE",
279 "general-tests", "ravenwood-tests")
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700280 if r.testConfig != nil {
281 entries.SetPath("LOCAL_FULL_TEST_CONFIG", r.testConfig)
282 }
283 })
284 return entriesList
285}
286
287type ravenwoodLibgroupProperties struct {
288 Libs []string
Makoto Onuki68676572024-02-02 13:29:01 -0800289
Jihoon Kang371a0372024-10-01 16:44:41 +0000290 Jni_libs proptools.Configurable[[]string]
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700291
292 // We use this to copy framework-res.apk to the ravenwood runtime directory.
John Wu680cd732024-09-13 20:59:05 +0000293 Data []string `android:"path,arch_variant"`
294
295 // We use this to copy font files to the ravenwood runtime directory.
296 Fonts []string `android:"path,arch_variant"`
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700297}
298
299type ravenwoodLibgroup struct {
300 android.ModuleBase
301
302 ravenwoodLibgroupProperties ravenwoodLibgroupProperties
303
304 forceOSType android.OsType
305 forceArchType android.ArchType
306}
307
308func ravenwoodLibgroupFactory() android.Module {
309 module := &ravenwoodLibgroup{}
310 module.AddProperties(&module.ravenwoodLibgroupProperties)
311
312 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
313 return module
314}
315
316func (r *ravenwoodLibgroup) InstallInTestcases() bool { return true }
317func (r *ravenwoodLibgroup) InstallForceOS() (*android.OsType, *android.ArchType) {
318 return &r.forceOSType, &r.forceArchType
319}
320func (r *ravenwoodLibgroup) TestSuites() []string {
Jeff Sharkey4bbf86f2023-11-30 09:29:50 -0700321 return []string{
322 "general-tests",
323 "ravenwood-tests",
324 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700325}
326
327func (r *ravenwoodLibgroup) DepsMutator(ctx android.BottomUpMutatorContext) {
328 // Always depends on our underlying libs
329 for _, lib := range r.ravenwoodLibgroupProperties.Libs {
Makoto Onuki2ca84272024-02-10 00:15:21 +0000330 ctx.AddVariationDependencies(nil, ravenwoodLibContentTag, lib)
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700331 }
Jihoon Kang371a0372024-10-01 16:44:41 +0000332 for _, lib := range r.ravenwoodLibgroupProperties.Jni_libs.GetOrDefault(ctx, nil) {
Makoto Onuki2ca84272024-02-10 00:15:21 +0000333 ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), jniLibTag, lib)
Makoto Onuki68676572024-02-02 13:29:01 -0800334 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700335}
336
337func (r *ravenwoodLibgroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
338 r.forceOSType = ctx.Config().BuildOS
339 r.forceArchType = ctx.Config().BuildArch
340
Makoto Onuki2ca84272024-02-10 00:15:21 +0000341 // Collect the JNI dependencies, including the transitive deps.
342 jniDepNames := make(map[string]bool)
343 jniLibs := collectTransitiveJniDeps(ctx)
344
345 for _, jni := range jniLibs {
346 jniDepNames[jni.name] = true
347 }
348 android.SetProvider(ctx, ravenwoodLibgroupJniDepProvider, ravenwoodLibgroupJniDepProviderInfo{
349 names: jniDepNames,
350 })
351
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700352 // Install our runtime into expected location for packaging
353 installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
354 for _, lib := range r.ravenwoodLibgroupProperties.Libs {
Yu Liu7eebf8b2025-01-17 00:23:57 +0000355 libModule := ctx.GetDirectDepProxyWithTag(lib, ravenwoodLibContentTag)
Jerome Gaillard44fc5bf2024-07-30 16:11:59 +0000356 if libModule == nil {
357 if ctx.Config().AllowMissingDependencies() {
358 ctx.AddMissingDependencies([]string{lib})
359 } else {
360 ctx.PropertyErrorf("lib", "missing dependency %q", lib)
361 }
362 continue
363 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700364 libJar := android.OutputFileForModule(ctx, libModule, "")
365 ctx.InstallFile(installPath, lib+".jar", libJar)
366 }
Makoto Onuki68676572024-02-02 13:29:01 -0800367 soInstallPath := android.PathForModuleInstall(ctx, r.BaseModuleName()).Join(ctx, getLibPath(r.forceArchType))
Makoto Onuki2ca84272024-02-10 00:15:21 +0000368
369 for _, jniLib := range jniLibs {
370 ctx.InstallFile(soInstallPath, jniLib.path.Base(), jniLib.path)
Makoto Onuki68676572024-02-02 13:29:01 -0800371 }
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700372
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700373 dataInstallPath := installPath.Join(ctx, "ravenwood-data")
John Wu680cd732024-09-13 20:59:05 +0000374 data := android.PathsForModuleSrc(ctx, r.ravenwoodLibgroupProperties.Data)
375 for _, file := range data {
Makoto Onuki3380f6d2024-05-10 16:00:28 -0700376 ctx.InstallFile(dataInstallPath, file.Base(), file)
377 }
378
John Wu680cd732024-09-13 20:59:05 +0000379 fontsInstallPath := installPath.Join(ctx, "fonts")
380 fonts := android.PathsForModuleSrc(ctx, r.ravenwoodLibgroupProperties.Fonts)
381 for _, file := range fonts {
382 ctx.InstallFile(fontsInstallPath, file.Base(), file)
383 }
384
Makoto Onuki4a9869d2023-10-20 10:42:47 -0700385 // Normal build should perform install steps
386 ctx.Phony(r.BaseModuleName(), android.PathForPhony(ctx, r.BaseModuleName()+"-install"))
387}
Makoto Onuki2ca84272024-02-10 00:15:21 +0000388
389// collectTransitiveJniDeps returns all JNI dependencies, including transitive
390// ones, including NDK / stub libs. (Because Ravenwood has no "preinstalled" libraries)
391func collectTransitiveJniDeps(ctx android.ModuleContext) []jniLib {
392 libs, _ := collectJniDeps(ctx, true, false, nil)
393 return libs
394}