Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1 | // Copyright 2015 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. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | // This file contains the module types for compiling Java for Android, and converts the properties |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 18 | // into the flags and filenames necessary to pass to the Module. The final creation of the rules |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 19 | // is handled in builder.go |
| 20 | |
| 21 | import ( |
Colin Cross | f19b9bb | 2018-03-26 14:42:44 -0700 | [diff] [blame] | 22 | "fmt" |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 23 | "path/filepath" |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 24 | "sort" |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 25 | "strings" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 26 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 27 | "android/soong/bazel" |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 28 | "android/soong/bazel/cquery" |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 29 | "android/soong/remoteexec" |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 30 | "android/soong/ui/metrics/bp2build_metrics_proto" |
Sam Delmerico | 4e27229 | 2022-01-06 20:03:51 +0000 | [diff] [blame] | 31 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 32 | "github.com/google/blueprint" |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 33 | "github.com/google/blueprint/proptools" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 34 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 35 | "android/soong/android" |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 36 | "android/soong/cc" |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 37 | "android/soong/dexpreopt" |
Colin Cross | 3e3e72d | 2017-06-22 17:20:19 -0700 | [diff] [blame] | 38 | "android/soong/java/config" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 39 | "android/soong/tradefed" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 42 | func init() { |
Paul Duffin | 535e0a1 | 2021-03-30 23:34:32 +0100 | [diff] [blame] | 43 | registerJavaBuildComponents(android.InitRegistrationContext) |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 44 | |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 45 | RegisterJavaSdkMemberTypes() |
| 46 | } |
| 47 | |
Paul Duffin | 535e0a1 | 2021-03-30 23:34:32 +0100 | [diff] [blame] | 48 | func registerJavaBuildComponents(ctx android.RegistrationContext) { |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 49 | ctx.RegisterModuleType("java_defaults", DefaultsFactory) |
| 50 | |
| 51 | ctx.RegisterModuleType("java_library", LibraryFactory) |
| 52 | ctx.RegisterModuleType("java_library_static", LibraryStaticFactory) |
| 53 | ctx.RegisterModuleType("java_library_host", LibraryHostFactory) |
| 54 | ctx.RegisterModuleType("java_binary", BinaryFactory) |
| 55 | ctx.RegisterModuleType("java_binary_host", BinaryHostFactory) |
| 56 | ctx.RegisterModuleType("java_test", TestFactory) |
| 57 | ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory) |
| 58 | ctx.RegisterModuleType("java_test_host", TestHostFactory) |
| 59 | ctx.RegisterModuleType("java_test_import", JavaTestImportFactory) |
| 60 | ctx.RegisterModuleType("java_import", ImportFactory) |
| 61 | ctx.RegisterModuleType("java_import_host", ImportFactoryHost) |
| 62 | ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory) |
| 63 | ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) |
| 64 | ctx.RegisterModuleType("dex_import", DexImportFactory) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 65 | ctx.RegisterModuleType("java_api_library", ApiLibraryFactory) |
| 66 | ctx.RegisterModuleType("java_api_contribution", ApiContributionFactory) |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 67 | |
Martin Stjernholm | 0e4cceb | 2021-05-13 02:38:35 +0100 | [diff] [blame] | 68 | // This mutator registers dependencies on dex2oat for modules that should be |
| 69 | // dexpreopted. This is done late when the final variants have been |
| 70 | // established, to not get the dependencies split into the wrong variants and |
| 71 | // to support the checks in dexpreoptDisabled(). |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 72 | ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 73 | ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel() |
Sam Delmerico | 1e3f78f | 2022-09-07 12:07:07 -0400 | [diff] [blame] | 74 | // needs access to ApexInfoProvider which is available after variant creation |
| 75 | ctx.BottomUp("jacoco_deps", jacocoDepsMutator).Parallel() |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 76 | }) |
| 77 | |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 78 | ctx.RegisterParallelSingletonType("logtags", LogtagsSingleton) |
| 79 | ctx.RegisterParallelSingletonType("kythe_java_extract", kytheExtractJavaFactory) |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func RegisterJavaSdkMemberTypes() { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 83 | // Register sdk member types. |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 84 | android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType) |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 85 | android.RegisterSdkMemberType(javaLibsSdkMemberType) |
| 86 | android.RegisterSdkMemberType(javaBootLibsSdkMemberType) |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 87 | android.RegisterSdkMemberType(javaSystemserverLibsSdkMemberType) |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 88 | android.RegisterSdkMemberType(javaTestSdkMemberType) |
| 89 | } |
| 90 | |
| 91 | var ( |
| 92 | // Supports adding java header libraries to module_exports and sdk. |
| 93 | javaHeaderLibsSdkMemberType = &librarySdkMemberType{ |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 94 | android.SdkMemberTypeBase{ |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 95 | PropertyName: "java_header_libs", |
| 96 | SupportsSdk: true, |
| 97 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 98 | func(_ android.SdkMemberContext, j *Library) android.Path { |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 99 | headerJars := j.HeaderJars() |
| 100 | if len(headerJars) != 1 { |
| 101 | panic(fmt.Errorf("there must be only one header jar from %q", j.Name())) |
| 102 | } |
| 103 | |
| 104 | return headerJars[0] |
| 105 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 106 | sdkSnapshotFilePathForJar, |
| 107 | copyEverythingToSnapshot, |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 108 | } |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 109 | |
Paul Duffin | 22ff0aa | 2021-02-04 11:15:34 +0000 | [diff] [blame] | 110 | // Export implementation classes jar as part of the sdk. |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 111 | exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path { |
Paul Duffin | 22ff0aa | 2021-02-04 11:15:34 +0000 | [diff] [blame] | 112 | implementationJars := j.ImplementationAndResourcesJars() |
| 113 | if len(implementationJars) != 1 { |
| 114 | panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name())) |
| 115 | } |
| 116 | return implementationJars[0] |
| 117 | } |
| 118 | |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 119 | // Supports adding java implementation libraries to module_exports but not sdk. |
| 120 | javaLibsSdkMemberType = &librarySdkMemberType{ |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 121 | android.SdkMemberTypeBase{ |
Paul Duffin | f5c0a9c | 2020-02-28 14:39:53 +0000 | [diff] [blame] | 122 | PropertyName: "java_libs", |
| 123 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 124 | exportImplementationClassesJar, |
| 125 | sdkSnapshotFilePathForJar, |
| 126 | copyEverythingToSnapshot, |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 127 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 128 | |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 129 | snapshotRequiresImplementationJar = func(ctx android.SdkMemberContext) bool { |
| 130 | // In the S build the build will break if updatable-media does not provide a full implementation |
| 131 | // jar. That issue was fixed in Tiramisu by b/229932396. |
| 132 | if ctx.IsTargetBuildBeforeTiramisu() && ctx.Name() == "updatable-media" { |
| 133 | return true |
| 134 | } |
| 135 | |
| 136 | return false |
| 137 | } |
| 138 | |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 139 | // Supports adding java boot libraries to module_exports and sdk. |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 140 | // |
| 141 | // The build has some implicit dependencies (via the boot jars configuration) on a number of |
| 142 | // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are |
| 143 | // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise |
| 144 | // used outside those mainline modules. |
| 145 | // |
| 146 | // As they are not needed outside the mainline modules adding them to the sdk/module-exports as |
| 147 | // either java_libs, or java_header_libs would end up exporting more information than was strictly |
| 148 | // necessary. The java_boot_libs property to allow those modules to be exported as part of the |
| 149 | // sdk/module_exports without exposing any unnecessary information. |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 150 | javaBootLibsSdkMemberType = &librarySdkMemberType{ |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 151 | android.SdkMemberTypeBase{ |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 152 | PropertyName: "java_boot_libs", |
| 153 | SupportsSdk: true, |
| 154 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 155 | func(ctx android.SdkMemberContext, j *Library) android.Path { |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 156 | if snapshotRequiresImplementationJar(ctx) { |
| 157 | return exportImplementationClassesJar(ctx, j) |
| 158 | } |
| 159 | |
Paul Duffin | 5c21145 | 2021-07-15 12:42:44 +0100 | [diff] [blame] | 160 | // Java boot libs are only provided in the SDK to provide access to their dex implementation |
| 161 | // jar for use by dexpreopting and boot jars package check. They do not need to provide an |
| 162 | // actual implementation jar but the java_import will need a file that exists so just copy an |
| 163 | // empty file. Any attempt to use that file as a jar will cause a build error. |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 164 | return ctx.SnapshotBuilder().EmptyFile() |
Paul Duffin | 5c21145 | 2021-07-15 12:42:44 +0100 | [diff] [blame] | 165 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 166 | func(ctx android.SdkMemberContext, osPrefix, name string) string { |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 167 | if snapshotRequiresImplementationJar(ctx) { |
| 168 | return sdkSnapshotFilePathForJar(ctx, osPrefix, name) |
| 169 | } |
| 170 | |
Paul Duffin | 5c21145 | 2021-07-15 12:42:44 +0100 | [diff] [blame] | 171 | // Create a special name for the implementation jar to try and provide some useful information |
| 172 | // to a developer that attempts to compile against this. |
| 173 | // TODO(b/175714559): Provide a proper error message in Soong not ninja. |
| 174 | return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix) |
| 175 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 176 | onlyCopyJarToSnapshot, |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 177 | } |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 178 | |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 179 | // Supports adding java systemserver libraries to module_exports and sdk. |
| 180 | // |
| 181 | // The build has some implicit dependencies (via the systemserver jars configuration) on a number |
| 182 | // of modules that are part of the java systemserver classpath and which are provided by mainline |
| 183 | // modules but which are not otherwise used outside those mainline modules. |
| 184 | // |
| 185 | // As they are not needed outside the mainline modules adding them to the sdk/module-exports as |
| 186 | // either java_libs, or java_header_libs would end up exporting more information than was strictly |
| 187 | // necessary. The java_systemserver_libs property to allow those modules to be exported as part of |
| 188 | // the sdk/module_exports without exposing any unnecessary information. |
| 189 | javaSystemserverLibsSdkMemberType = &librarySdkMemberType{ |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 190 | android.SdkMemberTypeBase{ |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 191 | PropertyName: "java_systemserver_libs", |
| 192 | SupportsSdk: true, |
Paul Duffin | f861df7 | 2022-07-01 15:56:06 +0000 | [diff] [blame] | 193 | |
| 194 | // This was only added in Tiramisu. |
| 195 | SupportedBuildReleaseSpecification: "Tiramisu+", |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 196 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 197 | func(ctx android.SdkMemberContext, j *Library) android.Path { |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 198 | // Java systemserver libs are only provided in the SDK to provide access to their dex |
| 199 | // implementation jar for use by dexpreopting. They do not need to provide an actual |
| 200 | // implementation jar but the java_import will need a file that exists so just copy an empty |
| 201 | // file. Any attempt to use that file as a jar will cause a build error. |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 202 | return ctx.SnapshotBuilder().EmptyFile() |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 203 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 204 | func(_ android.SdkMemberContext, osPrefix, name string) string { |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 205 | // Create a special name for the implementation jar to try and provide some useful information |
| 206 | // to a developer that attempts to compile against this. |
| 207 | // TODO(b/175714559): Provide a proper error message in Soong not ninja. |
| 208 | return filepath.Join(osPrefix, "java_systemserver_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix) |
| 209 | }, |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 210 | onlyCopyJarToSnapshot, |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 213 | // Supports adding java test libraries to module_exports but not sdk. |
| 214 | javaTestSdkMemberType = &testSdkMemberType{ |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 215 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 216 | PropertyName: "java_tests", |
| 217 | }, |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 218 | } |
Zi Wang | ca65b40 | 2022-10-10 13:45:06 -0700 | [diff] [blame] | 219 | |
| 220 | // Rule for generating device binary default wrapper |
| 221 | deviceBinaryWrapper = pctx.StaticRule("deviceBinaryWrapper", blueprint.RuleParams{ |
| 222 | Command: `echo -e '#!/system/bin/sh\n` + |
| 223 | `export CLASSPATH=/system/framework/$jar_name\n` + |
| 224 | `exec app_process /$partition/bin $main_class "$$@"'> ${out}`, |
| 225 | Description: "Generating device binary wrapper ${jar_name}", |
| 226 | }, "jar_name", "partition", "main_class") |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 227 | ) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 228 | |
Sam Delmerico | 95d7094 | 2023-08-02 18:00:35 -0400 | [diff] [blame] | 229 | type ProguardSpecInfo struct { |
| 230 | // If true, proguard flags files will be exported to reverse dependencies across libs edges |
| 231 | // If false, proguard flags files will only be exported to reverse dependencies across |
| 232 | // static_libs edges. |
| 233 | Export_proguard_flags_files bool |
| 234 | |
| 235 | // TransitiveDepsProguardSpecFiles is a depset of paths to proguard flags files that are exported from |
| 236 | // all transitive deps. This list includes all proguard flags files from transitive static dependencies, |
| 237 | // and all proguard flags files from transitive libs dependencies which set `export_proguard_spec: true`. |
| 238 | ProguardFlagsFiles *android.DepSet[android.Path] |
| 239 | |
| 240 | // implementation detail to store transitive proguard flags files from exporting shared deps |
| 241 | UnconditionallyExportedProguardFlags *android.DepSet[android.Path] |
| 242 | } |
| 243 | |
| 244 | var ProguardSpecInfoProvider = blueprint.NewProvider(ProguardSpecInfo{}) |
| 245 | |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 246 | // JavaInfo contains information about a java module for use by modules that depend on it. |
| 247 | type JavaInfo struct { |
| 248 | // HeaderJars is a list of jars that can be passed as the javac classpath in order to link |
| 249 | // against this module. If empty, ImplementationJars should be used instead. |
| 250 | HeaderJars android.Paths |
| 251 | |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 252 | // set of header jars for all transitive libs deps |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 253 | TransitiveLibsHeaderJars *android.DepSet[android.Path] |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 254 | |
| 255 | // set of header jars for all transitive static libs deps |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 256 | TransitiveStaticLibsHeaderJars *android.DepSet[android.Path] |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 257 | |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 258 | // ImplementationAndResourceJars is a list of jars that contain the implementations of classes |
| 259 | // in the module as well as any resources included in the module. |
| 260 | ImplementationAndResourcesJars android.Paths |
| 261 | |
| 262 | // ImplementationJars is a list of jars that contain the implementations of classes in the |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 263 | //module. |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 264 | ImplementationJars android.Paths |
| 265 | |
| 266 | // ResourceJars is a list of jars that contain the resources included in the module. |
| 267 | ResourceJars android.Paths |
| 268 | |
| 269 | // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when |
| 270 | // depending on this module. |
| 271 | AidlIncludeDirs android.Paths |
| 272 | |
| 273 | // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this |
| 274 | // module. |
| 275 | SrcJarArgs []string |
| 276 | |
| 277 | // SrcJarDeps is a list of paths to depend on when packaging the sources of this module. |
| 278 | SrcJarDeps android.Paths |
| 279 | |
| 280 | // ExportedPlugins is a list of paths that should be used as annotation processors for any |
| 281 | // module that depends on this module. |
| 282 | ExportedPlugins android.Paths |
| 283 | |
| 284 | // ExportedPluginClasses is a list of classes that should be run as annotation processors for |
| 285 | // any module that depends on this module. |
| 286 | ExportedPluginClasses []string |
| 287 | |
| 288 | // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs, |
| 289 | // requiring disbling turbine for any modules that depend on it. |
| 290 | ExportedPluginDisableTurbine bool |
| 291 | |
| 292 | // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be |
| 293 | // instrumented by jacoco. |
| 294 | JacocoReportClassesFile android.Path |
Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 295 | |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 296 | // set of aconfig flags for all transitive libs deps |
| 297 | // TODO(joeo): It would be nice if this were over in the aconfig package instead of here. |
| 298 | // In order to do that, generated_java_library would need a way doing |
| 299 | // collectTransitiveAconfigFiles with one of the callbacks, and having that automatically |
| 300 | // propagated. If we were to clean up more of the stuff on JavaInfo that's not part of |
| 301 | // core java rules (e.g. AidlIncludeDirs), then maybe adding more framework to do that would be |
| 302 | // worth it. |
| 303 | TransitiveAconfigFiles *android.DepSet[android.Path] |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | var JavaInfoProvider = blueprint.NewProvider(JavaInfo{}) |
| 307 | |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 308 | // SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to |
| 309 | // the sysprop implementation library. |
| 310 | type SyspropPublicStubInfo struct { |
| 311 | // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to |
| 312 | // the sysprop implementation library. |
| 313 | JavaInfo JavaInfo |
| 314 | } |
| 315 | |
| 316 | var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{}) |
| 317 | |
Paul Duffin | 44b481b | 2020-06-17 16:59:43 +0100 | [diff] [blame] | 318 | // Methods that need to be implemented for a module that is added to apex java_libs property. |
| 319 | type ApexDependency interface { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 320 | HeaderJars() android.Paths |
Paul Duffin | 44b481b | 2020-06-17 16:59:43 +0100 | [diff] [blame] | 321 | ImplementationAndResourcesJars() android.Paths |
| 322 | } |
| 323 | |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 324 | // Provides build path and install path to DEX jars. |
| 325 | type UsesLibraryDependency interface { |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 326 | DexJarBuildPath() OptionalDexJarPath |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 327 | DexJarInstallPath() android.Path |
Ulya Trafimovich | dbf3166 | 2020-12-17 12:07:54 +0000 | [diff] [blame] | 328 | ClassLoaderContexts() dexpreopt.ClassLoaderContextMap |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Jaewoong Jung | 2634264 | 2021-03-17 15:56:23 -0700 | [diff] [blame] | 331 | // TODO(jungjw): Move this to kythe.go once it's created. |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 332 | type xref interface { |
| 333 | XrefJavaFiles() android.Paths |
| 334 | } |
| 335 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 336 | func (j *Module) XrefJavaFiles() android.Paths { |
| 337 | return j.kytheFiles |
| 338 | } |
| 339 | |
Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 340 | type dependencyTag struct { |
| 341 | blueprint.BaseDependencyTag |
| 342 | name string |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 343 | |
| 344 | // True if the dependency is relinked at runtime. |
| 345 | runtimeLinked bool |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 346 | |
| 347 | // True if the dependency is a toolchain, for example an annotation processor. |
| 348 | toolchain bool |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 351 | // installDependencyTag is a dependency tag that is annotated to cause the installed files of the |
| 352 | // dependency to be installed when the parent module is installed. |
| 353 | type installDependencyTag struct { |
| 354 | blueprint.BaseDependencyTag |
| 355 | android.InstallAlwaysNeededDependencyTag |
| 356 | name string |
| 357 | } |
| 358 | |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 359 | func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation { |
| 360 | if d.runtimeLinked { |
| 361 | return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 362 | } else if d.toolchain { |
| 363 | return []android.LicenseAnnotation{android.LicenseAnnotationToolchain} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 364 | } |
| 365 | return nil |
| 366 | } |
| 367 | |
| 368 | var _ android.LicenseAnnotationsDependencyTag = dependencyTag{} |
| 369 | |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 370 | type usesLibraryDependencyTag struct { |
| 371 | dependencyTag |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 372 | sdkVersion int // SDK version in which the library appared as a standalone library. |
| 373 | optional bool // If the dependency is optional or required. |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 374 | } |
| 375 | |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 376 | func makeUsesLibraryDependencyTag(sdkVersion int, optional bool) usesLibraryDependencyTag { |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 377 | return usesLibraryDependencyTag{ |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 378 | dependencyTag: dependencyTag{ |
| 379 | name: fmt.Sprintf("uses-library-%d", sdkVersion), |
| 380 | runtimeLinked: true, |
| 381 | }, |
| 382 | sdkVersion: sdkVersion, |
| 383 | optional: optional, |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Jiyong Park | 8be103b | 2019-11-08 15:53:48 +0900 | [diff] [blame] | 387 | func IsJniDepTag(depTag blueprint.DependencyTag) bool { |
Colin Cross | de78d13 | 2020-10-09 18:59:49 -0700 | [diff] [blame] | 388 | return depTag == jniLibTag |
Jiyong Park | 8be103b | 2019-11-08 15:53:48 +0900 | [diff] [blame] | 389 | } |
| 390 | |
Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 391 | var ( |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 392 | dataNativeBinsTag = dependencyTag{name: "dataNativeBins"} |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 393 | dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 394 | staticLibTag = dependencyTag{name: "staticlib"} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 395 | libTag = dependencyTag{name: "javalib", runtimeLinked: true} |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 396 | sdkLibTag = dependencyTag{name: "sdklib", runtimeLinked: true} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 397 | java9LibTag = dependencyTag{name: "java9lib", runtimeLinked: true} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 398 | pluginTag = dependencyTag{name: "plugin", toolchain: true} |
| 399 | errorpronePluginTag = dependencyTag{name: "errorprone-plugin", toolchain: true} |
| 400 | exportedPluginTag = dependencyTag{name: "exported-plugin", toolchain: true} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 401 | bootClasspathTag = dependencyTag{name: "bootclasspath", runtimeLinked: true} |
| 402 | systemModulesTag = dependencyTag{name: "system modules", runtimeLinked: true} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 403 | frameworkResTag = dependencyTag{name: "framework-res"} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 404 | kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib", runtimeLinked: true} |
| 405 | kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations", runtimeLinked: true} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 406 | kotlinPluginTag = dependencyTag{name: "kotlin-plugin", toolchain: true} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 407 | proguardRaiseTag = dependencyTag{name: "proguard-raise"} |
| 408 | certificateTag = dependencyTag{name: "certificate"} |
| 409 | instrumentationForTag = dependencyTag{name: "instrumentation_for"} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 410 | extraLintCheckTag = dependencyTag{name: "extra-lint-check", toolchain: true} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 411 | jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true} |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 412 | r8LibraryJarTag = dependencyTag{name: "r8-libraryjar", runtimeLinked: true} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 413 | syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"} |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 414 | javaApiContributionTag = dependencyTag{name: "java-api-contribution"} |
| 415 | depApiSrcsTag = dependencyTag{name: "dep-api-srcs"} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 416 | jniInstallTag = installDependencyTag{name: "jni install"} |
| 417 | binaryInstallTag = installDependencyTag{name: "binary install"} |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 418 | usesLibReqTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion, false) |
| 419 | usesLibOptTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion, true) |
| 420 | usesLibCompat28OptTag = makeUsesLibraryDependencyTag(28, true) |
| 421 | usesLibCompat29ReqTag = makeUsesLibraryDependencyTag(29, false) |
| 422 | usesLibCompat30OptTag = makeUsesLibraryDependencyTag(30, true) |
Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 423 | ) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 424 | |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 425 | func IsLibDepTag(depTag blueprint.DependencyTag) bool { |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 426 | return depTag == libTag || depTag == sdkLibTag |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool { |
| 430 | return depTag == staticLibTag |
| 431 | } |
| 432 | |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 433 | type sdkDep struct { |
Pete Gillin | e3d44b2 | 2020-06-29 11:28:51 +0100 | [diff] [blame] | 434 | useModule, useFiles, invalidVersion bool |
Colin Cross | 47ff252 | 2017-10-02 14:22:08 -0700 | [diff] [blame] | 435 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 436 | // The modules that will be added to the bootclasspath when targeting 1.8 or lower |
| 437 | bootclasspath []string |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 438 | |
| 439 | // The default system modules to use. Will be an empty string if no system |
| 440 | // modules are to be used. |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 441 | systemModules string |
| 442 | |
Pete Gillin | e3d44b2 | 2020-06-29 11:28:51 +0100 | [diff] [blame] | 443 | // The modules that will be added to the classpath regardless of the Java language level targeted |
| 444 | classpath []string |
| 445 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 446 | // The modules that will be added ot the classpath when targeting 1.9 or higher |
Pete Gillin | e3d44b2 | 2020-06-29 11:28:51 +0100 | [diff] [blame] | 447 | // (normally these will be on the bootclasspath when targeting 1.8 or lower) |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 448 | java9Classpath []string |
| 449 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 450 | frameworkResModule string |
| 451 | |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 452 | jars android.Paths |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 453 | aidl android.OptionalPath |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 454 | |
| 455 | noStandardLibs, noFrameworksLibs bool |
| 456 | } |
| 457 | |
| 458 | func (s sdkDep) hasStandardLibs() bool { |
| 459 | return !s.noStandardLibs |
| 460 | } |
| 461 | |
| 462 | func (s sdkDep) hasFrameworkLibs() bool { |
| 463 | return !s.noStandardLibs && !s.noFrameworksLibs |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 466 | type jniLib struct { |
Colin Cross | 403cc15 | 2020-07-06 14:15:24 -0700 | [diff] [blame] | 467 | name string |
| 468 | path android.Path |
| 469 | target android.Target |
| 470 | coverageFile android.OptionalPath |
| 471 | unstrippedFile android.Path |
Jihoon Kang | f78a890 | 2022-09-01 22:47:07 +0000 | [diff] [blame] | 472 | partition string |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 475 | func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) { |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 476 | sdkDep := decodeSdkDep(ctx, sdkContext) |
| 477 | if sdkDep.useModule { |
| 478 | ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...) |
| 479 | ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...) |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 480 | ctx.AddVariationDependencies(nil, sdkLibTag, sdkDep.classpath...) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 481 | if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() { |
Jihoon Kang | b507831 | 2023-03-29 23:25:49 +0000 | [diff] [blame] | 482 | ctx.AddVariationDependencies(nil, proguardRaiseTag, |
Jihoon Kang | 6c0df88 | 2023-06-14 22:43:25 +0000 | [diff] [blame] | 483 | config.LegacyCorePlatformBootclasspathLibraries..., |
Jihoon Kang | b507831 | 2023-03-29 23:25:49 +0000 | [diff] [blame] | 484 | ) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 485 | } |
| 486 | if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() { |
| 487 | ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...) |
| 488 | } |
| 489 | } |
| 490 | if sdkDep.systemModules != "" { |
| 491 | ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) |
| 492 | } |
| 493 | } |
| 494 | |
Colin Cross | 32f676a | 2017-09-06 13:41:06 -0700 | [diff] [blame] | 495 | type deps struct { |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 496 | // bootClasspath is the list of jars that form the boot classpath (generally the java.* and |
| 497 | // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses |
| 498 | // systemModules and java9Classpath instead. |
| 499 | bootClasspath classpath |
| 500 | |
| 501 | // classpath is the list of jars that form the classpath for javac and kotlinc rules. It |
| 502 | // contains header jars for all static and non-static dependencies. |
| 503 | classpath classpath |
| 504 | |
| 505 | // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains |
| 506 | // header jars for all non-static dependencies. Static dependencies have already been |
| 507 | // combined into the program jar. |
| 508 | dexClasspath classpath |
| 509 | |
| 510 | // java9Classpath is the list of jars that will be added to the classpath when targeting |
| 511 | // 1.9 or higher. It generally contains the android.* classes, while the java.* classes |
| 512 | // are provided by systemModules. |
| 513 | java9Classpath classpath |
| 514 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 515 | processorPath classpath |
| 516 | errorProneProcessorPath classpath |
| 517 | processorClasses []string |
| 518 | staticJars android.Paths |
| 519 | staticHeaderJars android.Paths |
| 520 | staticResourceJars android.Paths |
| 521 | aidlIncludeDirs android.Paths |
| 522 | srcs android.Paths |
| 523 | srcJars android.Paths |
| 524 | systemModules *systemModules |
| 525 | aidlPreprocess android.OptionalPath |
| 526 | kotlinStdlib android.Paths |
| 527 | kotlinAnnotations android.Paths |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 528 | kotlinPlugins android.Paths |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 529 | |
| 530 | disableTurbine bool |
Colin Cross | 32f676a | 2017-09-06 13:41:06 -0700 | [diff] [blame] | 531 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 532 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 533 | func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) { |
| 534 | for _, f := range dep.Srcs() { |
| 535 | if f.Ext() != ".jar" { |
| 536 | ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency", |
| 537 | ctx.OtherModuleName(dep.(blueprint.Module))) |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 542 | func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion { |
Nan Zhang | 357466b | 2018-04-17 17:38:36 -0700 | [diff] [blame] | 543 | if javaVersion != "" { |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 544 | return normalizeJavaVersion(ctx, javaVersion) |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 545 | } else if ctx.Device() { |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 546 | return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx)) |
Sorin Basca | 384250c | 2023-02-02 17:56:19 +0000 | [diff] [blame] | 547 | } else { |
Sorin Basca | be30273 | 2023-02-15 17:52:27 +0000 | [diff] [blame] | 548 | return JAVA_VERSION_17 |
Nan Zhang | 357466b | 2018-04-17 17:38:36 -0700 | [diff] [blame] | 549 | } |
Nan Zhang | 357466b | 2018-04-17 17:38:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Jihoon Kang | ff878bf | 2022-12-22 21:26:06 +0000 | [diff] [blame] | 552 | // Java version for stubs generation |
| 553 | func getStubsJavaVersion() javaVersion { |
| 554 | return JAVA_VERSION_8 |
| 555 | } |
| 556 | |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 557 | type javaVersion int |
| 558 | |
| 559 | const ( |
| 560 | JAVA_VERSION_UNSUPPORTED = 0 |
| 561 | JAVA_VERSION_6 = 6 |
| 562 | JAVA_VERSION_7 = 7 |
| 563 | JAVA_VERSION_8 = 8 |
| 564 | JAVA_VERSION_9 = 9 |
Sorin Basca | c0244da | 2021-11-26 17:26:33 +0000 | [diff] [blame] | 565 | JAVA_VERSION_11 = 11 |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 566 | JAVA_VERSION_17 = 17 |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 567 | ) |
| 568 | |
| 569 | func (v javaVersion) String() string { |
| 570 | switch v { |
| 571 | case JAVA_VERSION_6: |
| 572 | return "1.6" |
| 573 | case JAVA_VERSION_7: |
| 574 | return "1.7" |
| 575 | case JAVA_VERSION_8: |
| 576 | return "1.8" |
| 577 | case JAVA_VERSION_9: |
| 578 | return "1.9" |
Sorin Basca | c0244da | 2021-11-26 17:26:33 +0000 | [diff] [blame] | 579 | case JAVA_VERSION_11: |
| 580 | return "11" |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 581 | case JAVA_VERSION_17: |
| 582 | return "17" |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 583 | default: |
| 584 | return "unsupported" |
| 585 | } |
| 586 | } |
| 587 | |
Cole Faust | d96eebf | 2022-06-28 14:41:27 -0700 | [diff] [blame] | 588 | func (v javaVersion) StringForKotlinc() string { |
| 589 | // $ ./external/kotlinc/bin/kotlinc -jvm-target foo |
| 590 | // error: unknown JVM target version: foo |
| 591 | // Supported versions: 1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17 |
| 592 | switch v { |
| 593 | case JAVA_VERSION_7: |
| 594 | return "1.6" |
| 595 | case JAVA_VERSION_9: |
| 596 | return "9" |
| 597 | default: |
| 598 | return v.String() |
| 599 | } |
| 600 | } |
| 601 | |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 602 | // Returns true if javac targeting this version uses system modules instead of a bootclasspath. |
| 603 | func (v javaVersion) usesJavaModules() bool { |
| 604 | return v >= 9 |
| 605 | } |
| 606 | |
| 607 | func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion { |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 608 | switch javaVersion { |
| 609 | case "1.6", "6": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 610 | return JAVA_VERSION_6 |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 611 | case "1.7", "7": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 612 | return JAVA_VERSION_7 |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 613 | case "1.8", "8": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 614 | return JAVA_VERSION_8 |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 615 | case "1.9", "9": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 616 | return JAVA_VERSION_9 |
Sorin Basca | c0244da | 2021-11-26 17:26:33 +0000 | [diff] [blame] | 617 | case "11": |
| 618 | return JAVA_VERSION_11 |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 619 | case "17": |
Sorin Basca | 5938fed | 2022-06-22 12:53:51 +0100 | [diff] [blame] | 620 | return JAVA_VERSION_17 |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 621 | case "10", "12", "13", "14", "15", "16": |
| 622 | ctx.PropertyErrorf("java_version", "Java language level %s is not supported", javaVersion) |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 623 | return JAVA_VERSION_UNSUPPORTED |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 624 | default: |
| 625 | ctx.PropertyErrorf("java_version", "Unrecognized Java language level") |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 626 | return JAVA_VERSION_UNSUPPORTED |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 630 | // |
| 631 | // Java libraries (.jar file) |
| 632 | // |
| 633 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 634 | type Library struct { |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 635 | Module |
Colin Cross | f0f2e2c | 2019-10-15 16:36:40 -0700 | [diff] [blame] | 636 | |
Jared Duke | 5979b30 | 2022-12-19 21:08:39 +0000 | [diff] [blame] | 637 | exportedProguardFlagFiles android.Paths |
| 638 | |
Colin Cross | f0f2e2c | 2019-10-15 16:36:40 -0700 | [diff] [blame] | 639 | InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 642 | var _ android.ApexModule = (*Library)(nil) |
| 643 | |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 644 | // Provides access to the list of permitted packages from apex boot jars. |
Paul Duffin | e739f1e | 2020-05-29 11:24:51 +0100 | [diff] [blame] | 645 | type PermittedPackagesForUpdatableBootJars interface { |
| 646 | PermittedPackagesForUpdatableBootJars() []string |
| 647 | } |
| 648 | |
| 649 | var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil) |
| 650 | |
| 651 | func (j *Library) PermittedPackagesForUpdatableBootJars() []string { |
| 652 | return j.properties.Permitted_packages |
| 653 | } |
| 654 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 655 | func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool { |
Ulya Trafimovich | f491dde | 2020-01-24 12:19:45 +0000 | [diff] [blame] | 656 | // Store uncompressed (and aligned) any dex files from jars in APEXes. |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 657 | if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() { |
Ulya Trafimovich | f491dde | 2020-01-24 12:19:45 +0000 | [diff] [blame] | 658 | return true |
| 659 | } |
| 660 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 661 | // Store uncompressed (and do not strip) dex files from boot class path jars. |
| 662 | if inList(ctx.ModuleName(), ctx.Config().BootJars()) { |
| 663 | return true |
| 664 | } |
| 665 | |
| 666 | // Store uncompressed dex files that are preopted on /system. |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 667 | if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) { |
Vladimir Marko | e8b00d6 | 2018-12-21 15:54:16 +0000 | [diff] [blame] | 668 | return true |
| 669 | } |
Colin Cross | 083a2aa | 2019-02-06 16:37:12 -0800 | [diff] [blame] | 670 | if ctx.Config().UncompressPrivAppDex() && |
| 671 | inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) { |
| 672 | return true |
| 673 | } |
| 674 | |
Colin Cross | 2fc72f6 | 2018-12-21 12:59:54 -0800 | [diff] [blame] | 675 | return false |
| 676 | } |
| 677 | |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 678 | // Sets `dexer.dexProperties.Uncompress_dex` to the proper value. |
| 679 | func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) { |
| 680 | if dexer.dexProperties.Uncompress_dex == nil { |
| 681 | // If the value was not force-set by the user, use reasonable default based on the module. |
| 682 | dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter)) |
| 683 | } |
| 684 | } |
| 685 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 686 | func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Paul Duffin | 3f1ae0b | 2022-07-27 16:27:42 +0000 | [diff] [blame] | 687 | |
| 688 | j.provideHiddenAPIPropertyInfo(ctx) |
| 689 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 690 | j.sdkVersion = j.SdkVersion(ctx) |
| 691 | j.minSdkVersion = j.MinSdkVersion(ctx) |
satayev | 0a420e7 | 2021-11-29 17:25:52 +0000 | [diff] [blame] | 692 | j.maxSdkVersion = j.MaxSdkVersion(ctx) |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 693 | |
Jihoon Kang | 1bfb6f2 | 2023-07-01 00:13:47 +0000 | [diff] [blame] | 694 | j.stem = proptools.StringDefault(j.overridableDeviceProperties.Stem, ctx.ModuleName()) |
| 695 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 696 | apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 697 | if !apexInfo.IsForPlatform() { |
| 698 | j.hideApexVariantFromMake = true |
| 699 | } |
| 700 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 701 | j.checkSdkVersions(ctx) |
Mark White | a15790a | 2023-08-22 21:28:11 +0000 | [diff] [blame] | 702 | j.checkHeadersOnly(ctx) |
Colin Cross | 61df14a | 2021-12-01 13:04:46 -0800 | [diff] [blame] | 703 | if ctx.Device() { |
| 704 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath( |
| 705 | ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")) |
| 706 | j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary |
| 707 | setUncompressDex(ctx, &j.dexpreopter, &j.dexer) |
| 708 | j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex |
| 709 | j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx) |
| 710 | } |
Colin Cross | 4eae06d | 2023-06-20 22:40:02 -0700 | [diff] [blame] | 711 | j.compile(ctx, nil, nil, nil) |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 712 | |
bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 713 | // Collect the module directory for IDE info in java/jdeps.go. |
| 714 | j.modulePaths = append(j.modulePaths, ctx.ModuleDir()) |
| 715 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 716 | exclusivelyForApex := !apexInfo.IsForPlatform() |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 717 | if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex { |
Colin Cross | f0f2e2c | 2019-10-15 16:36:40 -0700 | [diff] [blame] | 718 | var extraInstallDeps android.Paths |
| 719 | if j.InstallMixin != nil { |
| 720 | extraInstallDeps = j.InstallMixin(ctx, j.outputFile) |
| 721 | } |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 722 | hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host() |
| 723 | if hostDexNeeded { |
Colin Cross | 3108ce1 | 2021-11-10 14:38:50 -0800 | [diff] [blame] | 724 | j.hostdexInstallFile = ctx.InstallFile( |
| 725 | android.PathForHostDexInstall(ctx, "framework"), |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 726 | j.Stem()+"-hostdex.jar", j.outputFile) |
| 727 | } |
| 728 | var installDir android.InstallPath |
| 729 | if ctx.InstallInTestcases() { |
| 730 | var archDir string |
| 731 | if !ctx.Host() { |
| 732 | archDir = ctx.DeviceConfig().DeviceArch() |
| 733 | } |
| 734 | installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir) |
| 735 | } else { |
| 736 | installDir = android.PathForModuleInstall(ctx, "framework") |
| 737 | } |
| 738 | j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...) |
Colin Cross | 2c429dc | 2017-08-31 16:45:16 -0700 | [diff] [blame] | 739 | } |
Jared Duke | 5979b30 | 2022-12-19 21:08:39 +0000 | [diff] [blame] | 740 | |
Sam Delmerico | 95d7094 | 2023-08-02 18:00:35 -0400 | [diff] [blame] | 741 | proguardSpecInfo := j.collectProguardSpecInfo(ctx) |
| 742 | ctx.SetProvider(ProguardSpecInfoProvider, proguardSpecInfo) |
| 743 | j.exportedProguardFlagFiles = proguardSpecInfo.ProguardFlagsFiles.ToList() |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 746 | func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 747 | j.deps(ctx) |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 748 | j.usesLibrary.deps(ctx, false) |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 751 | const ( |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 752 | aidlIncludeDir = "aidl" |
| 753 | javaDir = "java" |
| 754 | jarFileSuffix = ".jar" |
| 755 | testConfigSuffix = "-AndroidTest.xml" |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 756 | ) |
| 757 | |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 758 | // path to the jar file of a java library. Relative to <sdk_root>/<api_dir> |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 759 | func sdkSnapshotFilePathForJar(_ android.SdkMemberContext, osPrefix, name string) string { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 760 | return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 763 | func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string { |
| 764 | return filepath.Join(javaDir, osPrefix, name+suffix) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 767 | type librarySdkMemberType struct { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 768 | android.SdkMemberTypeBase |
Paul Duffin | f5c0a9c | 2020-02-28 14:39:53 +0000 | [diff] [blame] | 769 | |
| 770 | // Function to retrieve the appropriate output jar (implementation or header) from |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 771 | // the library. |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 772 | jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path |
| 773 | |
| 774 | // Function to compute the snapshot relative path to which the named library's |
| 775 | // jar should be copied. |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 776 | snapshotPathGetter func(ctx android.SdkMemberContext, osPrefix, name string) string |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 777 | |
| 778 | // True if only the jar should be copied to the snapshot, false if the jar plus any additional |
| 779 | // files like aidl files should also be copied. |
| 780 | onlyCopyJarToSnapshot bool |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 783 | const ( |
| 784 | onlyCopyJarToSnapshot = true |
| 785 | copyEverythingToSnapshot = false |
| 786 | ) |
| 787 | |
Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 788 | func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { |
| 789 | ctx.AddVariationDependencies(nil, dependencyTag, names...) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | func (mt *librarySdkMemberType) IsInstance(module android.Module) bool { |
| 793 | _, ok := module.(*Library) |
| 794 | return ok |
| 795 | } |
| 796 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 797 | func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { |
| 798 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import") |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 799 | } |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 800 | |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 801 | func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 802 | return &librarySdkMemberProperties{} |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | type librarySdkMemberProperties struct { |
| 806 | android.SdkMemberPropertiesBase |
| 807 | |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 808 | JarToExport android.Path `android:"arch_variant"` |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 809 | AidlIncludeDirs android.Paths |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 810 | |
| 811 | // The list of permitted packages that need to be passed to the prebuilts as they are used to |
| 812 | // create the updatable-bcp-packages.txt file. |
| 813 | PermittedPackages []string |
Paul Duffin | bb638eb | 2022-11-26 13:36:38 +0000 | [diff] [blame] | 814 | |
| 815 | // The value of the min_sdk_version property, translated into a number where possible. |
| 816 | MinSdkVersion *string `supported_build_releases:"Tiramisu+"` |
Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame] | 817 | |
| 818 | DexPreoptProfileGuided *bool `supported_build_releases:"UpsideDownCake+"` |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 821 | func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 822 | j := variant.(*Library) |
| 823 | |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 824 | p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j) |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 825 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 826 | p.AidlIncludeDirs = j.AidlIncludeDirs() |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 827 | |
| 828 | p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars() |
Paul Duffin | bb638eb | 2022-11-26 13:36:38 +0000 | [diff] [blame] | 829 | |
| 830 | // If the min_sdk_version was set then add the canonical representation of the API level to the |
| 831 | // snapshot. |
| 832 | if j.deviceProperties.Min_sdk_version != nil { |
Cole Faust | 3486740 | 2023-04-28 12:32:27 -0700 | [diff] [blame] | 833 | canonical, err := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.String()) |
| 834 | if err != nil { |
| 835 | ctx.ModuleErrorf("%s", err) |
| 836 | } |
Paul Duffin | bb638eb | 2022-11-26 13:36:38 +0000 | [diff] [blame] | 837 | p.MinSdkVersion = proptools.StringPtr(canonical) |
| 838 | } |
Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame] | 839 | |
| 840 | if j.dexpreopter.dexpreoptProperties.Dex_preopt_result.Profile_guided { |
| 841 | p.DexPreoptProfileGuided = proptools.BoolPtr(true) |
| 842 | } |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 843 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 844 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 845 | func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 846 | builder := ctx.SnapshotBuilder() |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 847 | |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 848 | memberType := ctx.MemberType().(*librarySdkMemberType) |
| 849 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 850 | exportedJar := p.JarToExport |
| 851 | if exportedJar != nil { |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 852 | // Delegate the creation of the snapshot relative path to the member type. |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 853 | snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(ctx, p.OsPrefix(), ctx.Name()) |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 854 | |
| 855 | // Copy the exported jar to the snapshot. |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 856 | builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath) |
| 857 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 858 | propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath}) |
| 859 | } |
| 860 | |
Paul Duffin | bb638eb | 2022-11-26 13:36:38 +0000 | [diff] [blame] | 861 | if p.MinSdkVersion != nil { |
| 862 | propertySet.AddProperty("min_sdk_version", *p.MinSdkVersion) |
| 863 | } |
| 864 | |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 865 | if len(p.PermittedPackages) > 0 { |
| 866 | propertySet.AddProperty("permitted_packages", p.PermittedPackages) |
| 867 | } |
| 868 | |
Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame] | 869 | dexPreoptSet := propertySet.AddPropertySet("dex_preopt") |
| 870 | if p.DexPreoptProfileGuided != nil { |
| 871 | dexPreoptSet.AddProperty("profile_guided", proptools.Bool(p.DexPreoptProfileGuided)) |
| 872 | } |
| 873 | |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 874 | // Do not copy anything else to the snapshot. |
| 875 | if memberType.onlyCopyJarToSnapshot { |
| 876 | return |
| 877 | } |
| 878 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 879 | aidlIncludeDirs := p.AidlIncludeDirs |
| 880 | if len(aidlIncludeDirs) != 0 { |
| 881 | sdkModuleContext := ctx.SdkModuleContext() |
| 882 | for _, dir := range aidlIncludeDirs { |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 883 | // TODO(jiyong): copy parcelable declarations only |
| 884 | aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil) |
| 885 | for _, file := range aidlFiles { |
| 886 | builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file)) |
| 887 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 888 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 889 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 890 | // TODO(b/151933053) - add aidl include dirs property |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 891 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 894 | // java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well. |
| 895 | // |
| 896 | // By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were |
| 897 | // compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used |
| 898 | // as a `static_libs` dependency of another module. |
| 899 | // |
| 900 | // Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on |
| 901 | // a device. |
| 902 | // |
| 903 | // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one |
| 904 | // compiled against the host bootclasspath. |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 905 | func LibraryFactory() android.Module { |
| 906 | module := &Library{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 907 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 908 | module.addHostAndDeviceProperties() |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 909 | |
Paul Duffin | 71b33cc | 2021-06-23 11:39:47 +0100 | [diff] [blame] | 910 | module.initModuleAndImport(module) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 911 | |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 912 | android.InitApexModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 913 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 914 | InitJavaModule(module, android.HostAndDeviceSupported) |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 915 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 918 | // java_library_static is an obsolete alias for java_library. |
| 919 | func LibraryStaticFactory() android.Module { |
| 920 | return LibraryFactory() |
| 921 | } |
| 922 | |
| 923 | // java_library_host builds and links sources into a `.jar` file for the host. |
| 924 | // |
| 925 | // A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were |
| 926 | // compiled against the host bootclasspath. |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 927 | func LibraryHostFactory() android.Module { |
| 928 | module := &Library{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 929 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 930 | module.addHostProperties() |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 931 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 932 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 933 | |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 934 | android.InitApexModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 935 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 936 | InitJavaModule(module, android.HostSupported) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 937 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | // |
Colin Cross | b628ea5 | 2018-08-14 16:42:33 -0700 | [diff] [blame] | 941 | // Java Tests |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 942 | // |
| 943 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 944 | // Test option struct. |
| 945 | type TestOptions struct { |
Zhenhuang Wang | 0ac5a43 | 2022-08-12 18:49:20 +0800 | [diff] [blame] | 946 | android.CommonTestOptions |
| 947 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 948 | // a list of extra test configuration files that should be installed with the module. |
| 949 | Extra_test_configs []string `android:"path,arch_variant"` |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 950 | |
| 951 | // Extra <option> tags to add to the auto generated test xml file. The "key" |
| 952 | // is optional in each of these. |
| 953 | Tradefed_options []tradefed.Option |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 954 | |
| 955 | // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., AndroidJunitTest. |
| 956 | // The "key" is optional in each of these. |
| 957 | Test_runner_options []tradefed.Option |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 960 | type testProperties struct { |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 961 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 962 | // installed into. |
| 963 | Test_suites []string `android:"arch_variant"` |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 964 | |
| 965 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 966 | // installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 967 | Test_config *string `android:"path,arch_variant"` |
Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 968 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 969 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 970 | // should be installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 971 | Test_config_template *string `android:"path,arch_variant"` |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 972 | |
Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 973 | // list of files or filegroup modules that provide data that should be installed alongside |
| 974 | // the test |
Jiyong Park | 2b0e490 | 2021-02-16 06:52:39 +0900 | [diff] [blame] | 975 | Data []string `android:"path"` |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 976 | |
| 977 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 978 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 979 | // explicitly. |
| 980 | Auto_gen_config *bool |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 981 | |
| 982 | // Add parameterized mainline modules to auto generated test config. The options will be |
| 983 | // handled by TradeFed to do downloading and installing the specified modules on the device. |
| 984 | Test_mainline_modules []string |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 985 | |
| 986 | // Test options. |
| 987 | Test_options TestOptions |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 988 | |
| 989 | // Names of modules containing JNI libraries that should be installed alongside the test. |
| 990 | Jni_libs []string |
Colin Cross | cfb0f5e | 2021-09-24 15:47:17 -0700 | [diff] [blame] | 991 | |
| 992 | // Install the test into a folder named for the module in all test suites. |
| 993 | Per_testcase_directory *bool |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 996 | type hostTestProperties struct { |
| 997 | // list of native binary modules that should be installed alongside the test |
| 998 | Data_native_bins []string `android:"arch_variant"` |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 999 | |
| 1000 | // list of device binary modules that should be installed alongside the test |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1001 | // This property only adds the first variant of the dependency |
| 1002 | Data_device_bins_first []string `android:"arch_variant"` |
| 1003 | |
| 1004 | // list of device binary modules that should be installed alongside the test |
| 1005 | // This property adds 64bit AND 32bit variants of the dependency |
| 1006 | Data_device_bins_both []string `android:"arch_variant"` |
| 1007 | |
| 1008 | // list of device binary modules that should be installed alongside the test |
| 1009 | // This property only adds 64bit variants of the dependency |
| 1010 | Data_device_bins_64 []string `android:"arch_variant"` |
| 1011 | |
| 1012 | // list of device binary modules that should be installed alongside the test |
| 1013 | // This property adds 32bit variants of the dependency if available, or else |
| 1014 | // defaults to the 64bit variant |
| 1015 | Data_device_bins_prefer32 []string `android:"arch_variant"` |
| 1016 | |
| 1017 | // list of device binary modules that should be installed alongside the test |
| 1018 | // This property only adds 32bit variants of the dependency |
| 1019 | Data_device_bins_32 []string `android:"arch_variant"` |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1022 | type testHelperLibraryProperties struct { |
| 1023 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 1024 | // installed into. |
| 1025 | Test_suites []string `android:"arch_variant"` |
Colin Cross | cfb0f5e | 2021-09-24 15:47:17 -0700 | [diff] [blame] | 1026 | |
| 1027 | // Install the test into a folder named for the module in all test suites. |
| 1028 | Per_testcase_directory *bool |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1031 | type prebuiltTestProperties struct { |
| 1032 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 1033 | // installed into. |
| 1034 | Test_suites []string `android:"arch_variant"` |
| 1035 | |
| 1036 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 1037 | // installed with the module. |
| 1038 | Test_config *string `android:"path,arch_variant"` |
| 1039 | } |
| 1040 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1041 | type Test struct { |
| 1042 | Library |
| 1043 | |
| 1044 | testProperties testProperties |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1045 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 1046 | testConfig android.Path |
| 1047 | extraTestConfigs android.Paths |
| 1048 | data android.Paths |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1051 | type TestHost struct { |
| 1052 | Test |
| 1053 | |
| 1054 | testHostProperties hostTestProperties |
| 1055 | } |
| 1056 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1057 | type TestHelperLibrary struct { |
| 1058 | Library |
| 1059 | |
| 1060 | testHelperLibraryProperties testHelperLibraryProperties |
| 1061 | } |
| 1062 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1063 | type JavaTestImport struct { |
| 1064 | Import |
| 1065 | |
| 1066 | prebuiltTestProperties prebuiltTestProperties |
| 1067 | |
| 1068 | testConfig android.Path |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1069 | dexJarFile android.Path |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Colin Cross | 24cc4be6 | 2021-11-03 14:09:41 -0700 | [diff] [blame] | 1072 | func (j *Test) InstallInTestcases() bool { |
| 1073 | // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into |
| 1074 | // testcases by base_rules.mk. |
| 1075 | return !j.Host() |
| 1076 | } |
| 1077 | |
| 1078 | func (j *TestHelperLibrary) InstallInTestcases() bool { |
| 1079 | return true |
| 1080 | } |
| 1081 | |
| 1082 | func (j *JavaTestImport) InstallInTestcases() bool { |
| 1083 | return true |
| 1084 | } |
| 1085 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1086 | func (j *TestHost) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext) { |
| 1087 | if len(j.testHostProperties.Data_device_bins_first) > 0 { |
| 1088 | deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations() |
| 1089 | ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_first...) |
| 1090 | } |
| 1091 | |
| 1092 | var maybeAndroid32Target *android.Target |
| 1093 | var maybeAndroid64Target *android.Target |
| 1094 | android32TargetList := android.FirstTarget(ctx.Config().Targets[android.Android], "lib32") |
| 1095 | android64TargetList := android.FirstTarget(ctx.Config().Targets[android.Android], "lib64") |
| 1096 | if len(android32TargetList) > 0 { |
| 1097 | maybeAndroid32Target = &android32TargetList[0] |
| 1098 | } |
| 1099 | if len(android64TargetList) > 0 { |
| 1100 | maybeAndroid64Target = &android64TargetList[0] |
| 1101 | } |
| 1102 | |
| 1103 | if len(j.testHostProperties.Data_device_bins_both) > 0 { |
| 1104 | if maybeAndroid32Target == nil && maybeAndroid64Target == nil { |
| 1105 | ctx.PropertyErrorf("data_device_bins_both", "no device targets available. Targets: %q", ctx.Config().Targets) |
| 1106 | return |
| 1107 | } |
| 1108 | if maybeAndroid32Target != nil { |
| 1109 | ctx.AddFarVariationDependencies( |
| 1110 | maybeAndroid32Target.Variations(), |
| 1111 | dataDeviceBinsTag, |
| 1112 | j.testHostProperties.Data_device_bins_both..., |
| 1113 | ) |
| 1114 | } |
| 1115 | if maybeAndroid64Target != nil { |
| 1116 | ctx.AddFarVariationDependencies( |
| 1117 | maybeAndroid64Target.Variations(), |
| 1118 | dataDeviceBinsTag, |
| 1119 | j.testHostProperties.Data_device_bins_both..., |
| 1120 | ) |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | if len(j.testHostProperties.Data_device_bins_prefer32) > 0 { |
| 1125 | if maybeAndroid32Target != nil { |
| 1126 | ctx.AddFarVariationDependencies( |
| 1127 | maybeAndroid32Target.Variations(), |
| 1128 | dataDeviceBinsTag, |
| 1129 | j.testHostProperties.Data_device_bins_prefer32..., |
| 1130 | ) |
| 1131 | } else { |
| 1132 | if maybeAndroid64Target == nil { |
| 1133 | ctx.PropertyErrorf("data_device_bins_prefer32", "no device targets available. Targets: %q", ctx.Config().Targets) |
| 1134 | return |
| 1135 | } |
| 1136 | ctx.AddFarVariationDependencies( |
| 1137 | maybeAndroid64Target.Variations(), |
| 1138 | dataDeviceBinsTag, |
| 1139 | j.testHostProperties.Data_device_bins_prefer32..., |
| 1140 | ) |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | if len(j.testHostProperties.Data_device_bins_32) > 0 { |
| 1145 | if maybeAndroid32Target == nil { |
| 1146 | ctx.PropertyErrorf("data_device_bins_32", "cannot find 32bit device target. Targets: %q", ctx.Config().Targets) |
| 1147 | return |
| 1148 | } |
| 1149 | deviceVariations := maybeAndroid32Target.Variations() |
| 1150 | ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_32...) |
| 1151 | } |
| 1152 | |
| 1153 | if len(j.testHostProperties.Data_device_bins_64) > 0 { |
| 1154 | if maybeAndroid64Target == nil { |
| 1155 | ctx.PropertyErrorf("data_device_bins_64", "cannot find 64bit device target. Targets: %q", ctx.Config().Targets) |
| 1156 | return |
| 1157 | } |
| 1158 | deviceVariations := maybeAndroid64Target.Variations() |
| 1159 | ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_64...) |
| 1160 | } |
| 1161 | } |
| 1162 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1163 | func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 1164 | if len(j.testHostProperties.Data_native_bins) > 0 { |
| 1165 | for _, target := range ctx.MultiTargets() { |
| 1166 | ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...) |
| 1167 | } |
| 1168 | } |
| 1169 | |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 1170 | if len(j.testProperties.Jni_libs) > 0 { |
| 1171 | for _, target := range ctx.MultiTargets() { |
| 1172 | sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 1173 | ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...) |
| 1174 | } |
| 1175 | } |
| 1176 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1177 | j.addDataDeviceBinsDeps(ctx) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1178 | j.deps(ctx) |
| 1179 | } |
| 1180 | |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1181 | func (j *TestHost) AddExtraResource(p android.Path) { |
| 1182 | j.extraResources = append(j.extraResources, p) |
| 1183 | } |
| 1184 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1185 | func (j *TestHost) dataDeviceBins() []string { |
| 1186 | ret := make([]string, 0, |
| 1187 | len(j.testHostProperties.Data_device_bins_first)+ |
| 1188 | len(j.testHostProperties.Data_device_bins_both)+ |
| 1189 | len(j.testHostProperties.Data_device_bins_prefer32)+ |
| 1190 | len(j.testHostProperties.Data_device_bins_32)+ |
| 1191 | len(j.testHostProperties.Data_device_bins_64), |
| 1192 | ) |
| 1193 | |
| 1194 | ret = append(ret, j.testHostProperties.Data_device_bins_first...) |
| 1195 | ret = append(ret, j.testHostProperties.Data_device_bins_both...) |
| 1196 | ret = append(ret, j.testHostProperties.Data_device_bins_prefer32...) |
| 1197 | ret = append(ret, j.testHostProperties.Data_device_bins_32...) |
| 1198 | ret = append(ret, j.testHostProperties.Data_device_bins_64...) |
| 1199 | |
| 1200 | return ret |
| 1201 | } |
| 1202 | |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1203 | func (j *TestHost) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1204 | var configs []tradefed.Config |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1205 | dataDeviceBins := j.dataDeviceBins() |
| 1206 | if len(dataDeviceBins) > 0 { |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1207 | // add Tradefed configuration to push device bins to device for testing |
| 1208 | remoteDir := filepath.Join("/data/local/tests/unrestricted/", j.Name()) |
| 1209 | options := []tradefed.Option{{Name: "cleanup", Value: "true"}} |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1210 | for _, bin := range dataDeviceBins { |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1211 | fullPath := filepath.Join(remoteDir, bin) |
| 1212 | options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: fullPath}) |
| 1213 | } |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1214 | configs = append(configs, tradefed.Object{ |
| 1215 | Type: "target_preparer", |
| 1216 | Class: "com.android.tradefed.targetprep.PushFilePreparer", |
| 1217 | Options: options, |
| 1218 | }) |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | j.Test.generateAndroidBuildActionsWithConfig(ctx, configs) |
| 1222 | } |
| 1223 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1224 | func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1225 | j.generateAndroidBuildActionsWithConfig(ctx, nil) |
| 1226 | } |
| 1227 | |
| 1228 | func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext, configs []tradefed.Config) { |
Julien Desprez | b216661 | 2021-03-05 18:08:36 +0000 | [diff] [blame] | 1229 | if j.testProperties.Test_options.Unit_test == nil && ctx.Host() { |
| 1230 | // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding. |
Julien Desprez | f666b15 | 2021-03-15 13:07:53 -0700 | [diff] [blame] | 1231 | defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites) |
Julien Desprez | b216661 | 2021-03-05 18:08:36 +0000 | [diff] [blame] | 1232 | j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest) |
| 1233 | } |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 1234 | j.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 1235 | TestConfigProp: j.testProperties.Test_config, |
| 1236 | TestConfigTemplateProp: j.testProperties.Test_config_template, |
| 1237 | TestSuites: j.testProperties.Test_suites, |
| 1238 | Config: configs, |
| 1239 | OptionsForAutogenerated: j.testProperties.Test_options.Tradefed_options, |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 1240 | TestRunnerOptions: j.testProperties.Test_options.Test_runner_options, |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 1241 | AutoGenConfig: j.testProperties.Auto_gen_config, |
| 1242 | UnitTest: j.testProperties.Test_options.Unit_test, |
| 1243 | DeviceTemplate: "${JavaTestConfigTemplate}", |
| 1244 | HostTemplate: "${JavaHostTestConfigTemplate}", |
| 1245 | HostUnitTestTemplate: "${JavaHostUnitTestConfigTemplate}", |
| 1246 | }) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1247 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1248 | j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1249 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 1250 | j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs) |
| 1251 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1252 | ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) { |
| 1253 | j.data = append(j.data, android.OutputFileForModule(ctx, dep, "")) |
| 1254 | }) |
| 1255 | |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1256 | ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) { |
| 1257 | j.data = append(j.data, android.OutputFileForModule(ctx, dep, "")) |
| 1258 | }) |
| 1259 | |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 1260 | ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) { |
| 1261 | sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo) |
| 1262 | if sharedLibInfo.SharedLibrary != nil { |
| 1263 | // Copy to an intermediate output directory to append "lib[64]" to the path, |
| 1264 | // so that it's compatible with the default rpath values. |
| 1265 | var relPath string |
| 1266 | if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" { |
| 1267 | relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base()) |
| 1268 | } else { |
| 1269 | relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base()) |
| 1270 | } |
| 1271 | relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath) |
| 1272 | ctx.Build(pctx, android.BuildParams{ |
| 1273 | Rule: android.Cp, |
| 1274 | Input: sharedLibInfo.SharedLibrary, |
| 1275 | Output: relocatedLib, |
| 1276 | }) |
| 1277 | j.data = append(j.data, relocatedLib) |
| 1278 | } else { |
| 1279 | ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep)) |
| 1280 | } |
| 1281 | }) |
| 1282 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1283 | j.Library.GenerateAndroidBuildActions(ctx) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1286 | func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1287 | j.Library.GenerateAndroidBuildActions(ctx) |
| 1288 | } |
| 1289 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1290 | func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 1291 | j.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 1292 | TestConfigProp: j.prebuiltTestProperties.Test_config, |
| 1293 | TestSuites: j.prebuiltTestProperties.Test_suites, |
| 1294 | DeviceTemplate: "${JavaTestConfigTemplate}", |
| 1295 | HostTemplate: "${JavaHostTestConfigTemplate}", |
| 1296 | HostUnitTestTemplate: "${JavaHostUnitTestConfigTemplate}", |
| 1297 | }) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1298 | |
| 1299 | j.Import.GenerateAndroidBuildActions(ctx) |
| 1300 | } |
| 1301 | |
| 1302 | type testSdkMemberType struct { |
| 1303 | android.SdkMemberTypeBase |
| 1304 | } |
| 1305 | |
Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 1306 | func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { |
| 1307 | ctx.AddVariationDependencies(nil, dependencyTag, names...) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | func (mt *testSdkMemberType) IsInstance(module android.Module) bool { |
| 1311 | _, ok := module.(*Test) |
| 1312 | return ok |
| 1313 | } |
| 1314 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1315 | func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { |
| 1316 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import") |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1317 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1318 | |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1319 | func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
| 1320 | return &testSdkMemberProperties{} |
| 1321 | } |
| 1322 | |
| 1323 | type testSdkMemberProperties struct { |
| 1324 | android.SdkMemberPropertiesBase |
| 1325 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1326 | JarToExport android.Path |
| 1327 | TestConfig android.Path |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1330 | func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1331 | test := variant.(*Test) |
| 1332 | |
| 1333 | implementationJars := test.ImplementationJars() |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1334 | if len(implementationJars) != 1 { |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1335 | panic(fmt.Errorf("there must be only one implementation jar from %q", test.Name())) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1338 | p.JarToExport = implementationJars[0] |
| 1339 | p.TestConfig = test.testConfig |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1340 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1341 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1342 | func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1343 | builder := ctx.SnapshotBuilder() |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1344 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1345 | exportedJar := p.JarToExport |
| 1346 | if exportedJar != nil { |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 1347 | snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(ctx, p.OsPrefix(), ctx.Name()) |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1348 | builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1349 | |
| 1350 | propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath}) |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | testConfig := p.TestConfig |
| 1354 | if testConfig != nil { |
| 1355 | snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix) |
| 1356 | builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1357 | propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath) |
| 1358 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1361 | // java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and |
| 1362 | // creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file. |
| 1363 | // |
| 1364 | // By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were |
| 1365 | // compiled against the device bootclasspath. |
| 1366 | // |
| 1367 | // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one |
| 1368 | // compiled against the host bootclasspath. |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1369 | func TestFactory() android.Module { |
| 1370 | module := &Test{} |
| 1371 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1372 | module.addHostAndDeviceProperties() |
| 1373 | module.AddProperties(&module.testProperties) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1374 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1375 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | e302687 | 2019-01-05 22:30:13 -0800 | [diff] [blame] | 1376 | module.Module.dexpreopter.isTest = true |
Cole Faust | d57e8b2 | 2022-08-11 11:59:04 -0700 | [diff] [blame] | 1377 | module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1378 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1379 | InitJavaModule(module, android.HostAndDeviceSupported) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1380 | return module |
| 1381 | } |
| 1382 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1383 | // java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite. |
| 1384 | func TestHelperLibraryFactory() android.Module { |
| 1385 | module := &TestHelperLibrary{} |
| 1386 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1387 | module.addHostAndDeviceProperties() |
| 1388 | module.AddProperties(&module.testHelperLibraryProperties) |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1389 | |
Colin Cross | 9a4abed | 2019-04-24 13:19:28 -0700 | [diff] [blame] | 1390 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 1391 | module.Module.dexpreopter.isTest = true |
Cole Faust | d57e8b2 | 2022-08-11 11:59:04 -0700 | [diff] [blame] | 1392 | module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) |
Colin Cross | 9a4abed | 2019-04-24 13:19:28 -0700 | [diff] [blame] | 1393 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1394 | InitJavaModule(module, android.HostAndDeviceSupported) |
| 1395 | return module |
| 1396 | } |
| 1397 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1398 | // java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module |
| 1399 | // and makes sure that it is added to the appropriate test suite. |
| 1400 | // |
| 1401 | // By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were |
| 1402 | // compiled against an Android classpath. |
| 1403 | // |
| 1404 | // Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one |
| 1405 | // for host modules. |
| 1406 | func JavaTestImportFactory() android.Module { |
| 1407 | module := &JavaTestImport{} |
| 1408 | |
| 1409 | module.AddProperties( |
| 1410 | &module.Import.properties, |
| 1411 | &module.prebuiltTestProperties) |
| 1412 | |
| 1413 | module.Import.properties.Installable = proptools.BoolPtr(true) |
| 1414 | |
| 1415 | android.InitPrebuiltModule(module, &module.properties.Jars) |
| 1416 | android.InitApexModule(module) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1417 | InitJavaModule(module, android.HostAndDeviceSupported) |
| 1418 | return module |
| 1419 | } |
| 1420 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1421 | // java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to |
| 1422 | // allow running the test with `atest` or a `TEST_MAPPING` file. |
| 1423 | // |
| 1424 | // A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were |
| 1425 | // compiled against the host bootclasspath. |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1426 | func TestHostFactory() android.Module { |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1427 | module := &TestHost{} |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1428 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1429 | module.addHostProperties() |
| 1430 | module.AddProperties(&module.testProperties) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1431 | module.AddProperties(&module.testHostProperties) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1432 | |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1433 | InitTestHost( |
| 1434 | module, |
| 1435 | proptools.BoolPtr(true), |
| 1436 | nil, |
| 1437 | nil) |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1438 | |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 1439 | android.InitBazelModule(module) |
| 1440 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1441 | InitJavaModuleMultiTargets(module, android.HostSupported) |
Julien Desprez | b216661 | 2021-03-05 18:08:36 +0000 | [diff] [blame] | 1442 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1443 | return module |
| 1444 | } |
| 1445 | |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1446 | func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) { |
| 1447 | th.properties.Installable = installable |
| 1448 | th.testProperties.Auto_gen_config = autoGenConfig |
| 1449 | th.testProperties.Test_suites = testSuites |
| 1450 | } |
| 1451 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1452 | // |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1453 | // Java Binaries (.jar file plus wrapper script) |
| 1454 | // |
| 1455 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1456 | type binaryProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1457 | // installable script to execute the resulting jar |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1458 | Wrapper *string `android:"path,arch_variant"` |
Colin Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 1459 | |
| 1460 | // Name of the class containing main to be inserted into the manifest as Main-Class. |
| 1461 | Main_class *string |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 1462 | |
| 1463 | // Names of modules containing JNI libraries that should be installed alongside the host |
| 1464 | // variant of the binary. |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1465 | Jni_libs []string `android:"arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1466 | } |
| 1467 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1468 | type Binary struct { |
| 1469 | Library |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1470 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1471 | binaryProperties binaryProperties |
Colin Cross | 10a0349 | 2017-08-10 17:09:43 -0700 | [diff] [blame] | 1472 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1473 | isWrapperVariant bool |
| 1474 | |
Colin Cross | c331599 | 2017-12-08 19:12:36 -0800 | [diff] [blame] | 1475 | wrapperFile android.Path |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1476 | binaryFile android.InstallPath |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Alex Light | 2423717 | 2017-10-26 09:46:21 -0700 | [diff] [blame] | 1479 | func (j *Binary) HostToolPath() android.OptionalPath { |
| 1480 | return android.OptionalPathForPath(j.binaryFile) |
| 1481 | } |
| 1482 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1483 | func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jihoon Kang | 1bfb6f2 | 2023-07-01 00:13:47 +0000 | [diff] [blame] | 1484 | j.stem = proptools.StringDefault(j.overridableDeviceProperties.Stem, ctx.ModuleName()) |
| 1485 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1486 | if ctx.Arch().ArchType == android.Common { |
| 1487 | // Compile the jar |
Colin Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 1488 | if j.binaryProperties.Main_class != nil { |
| 1489 | if j.properties.Manifest != nil { |
| 1490 | ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set") |
| 1491 | } |
| 1492 | manifestFile := android.PathForModuleOut(ctx, "manifest.txt") |
| 1493 | GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class)) |
| 1494 | j.overrideManifest = android.OptionalPathForPath(manifestFile) |
| 1495 | } |
| 1496 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1497 | j.Library.GenerateAndroidBuildActions(ctx) |
Nan Zhang | 3c807db | 2017-11-03 14:53:31 -0700 | [diff] [blame] | 1498 | } else { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1499 | // Handle the binary wrapper |
| 1500 | j.isWrapperVariant = true |
| 1501 | |
Colin Cross | 366938f | 2017-12-11 16:29:02 -0800 | [diff] [blame] | 1502 | if j.binaryProperties.Wrapper != nil { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1503 | j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1504 | } else { |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1505 | if ctx.Windows() { |
| 1506 | ctx.PropertyErrorf("wrapper", "wrapper is required for Windows") |
| 1507 | } |
| 1508 | |
Zi Wang | ca65b40 | 2022-10-10 13:45:06 -0700 | [diff] [blame] | 1509 | if ctx.Device() { |
| 1510 | // device binary should have a main_class property if it does not |
| 1511 | // have a specific wrapper, so that a default wrapper can |
| 1512 | // be generated for it. |
| 1513 | if j.binaryProperties.Main_class == nil { |
| 1514 | ctx.PropertyErrorf("main_class", "main_class property "+ |
| 1515 | "is required for device binary if no default wrapper is assigned") |
| 1516 | } else { |
| 1517 | wrapper := android.PathForModuleOut(ctx, ctx.ModuleName()+".sh") |
| 1518 | jarName := j.Stem() + ".jar" |
| 1519 | partition := j.PartitionTag(ctx.DeviceConfig()) |
| 1520 | ctx.Build(pctx, android.BuildParams{ |
| 1521 | Rule: deviceBinaryWrapper, |
| 1522 | Output: wrapper, |
| 1523 | Args: map[string]string{ |
| 1524 | "jar_name": jarName, |
| 1525 | "partition": partition, |
| 1526 | "main_class": String(j.binaryProperties.Main_class), |
| 1527 | }, |
| 1528 | }) |
| 1529 | j.wrapperFile = wrapper |
| 1530 | } |
| 1531 | } else { |
| 1532 | j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh") |
| 1533 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1534 | } |
| 1535 | |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1536 | ext := "" |
| 1537 | if ctx.Windows() { |
| 1538 | ext = ".bat" |
| 1539 | } |
| 1540 | |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 1541 | // The host installation rules make the installed wrapper depend on all the dependencies |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 1542 | // of the wrapper variant, which will include the common variant's jar file and any JNI |
| 1543 | // libraries. This is verified by TestBinary. |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1544 | j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"), |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1545 | ctx.ModuleName()+ext, j.wrapperFile) |
| 1546 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1549 | func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) { |
Liz Kammer | 3bf97bd | 2022-04-26 09:38:20 -0400 | [diff] [blame] | 1550 | if ctx.Arch().ArchType == android.Common { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1551 | j.deps(ctx) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1552 | } |
Liz Kammer | 3bf97bd | 2022-04-26 09:38:20 -0400 | [diff] [blame] | 1553 | if ctx.Arch().ArchType != android.Common { |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 1554 | // These dependencies ensure the host installation rules will install the jar file and |
| 1555 | // the jni libraries when the wrapper is installed. |
| 1556 | ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...) |
| 1557 | ctx.AddVariationDependencies( |
| 1558 | []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}}, |
| 1559 | binaryInstallTag, ctx.ModuleName()) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1560 | } |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1563 | // java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host |
| 1564 | // as well. |
| 1565 | // |
| 1566 | // By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were |
| 1567 | // compiled against the device bootclasspath. |
| 1568 | // |
| 1569 | // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one |
| 1570 | // compiled against the host bootclasspath. |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1571 | func BinaryFactory() android.Module { |
| 1572 | module := &Binary{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1573 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1574 | module.addHostAndDeviceProperties() |
| 1575 | module.AddProperties(&module.binaryProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1576 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1577 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 1578 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1579 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst) |
| 1580 | android.InitDefaultableModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 1581 | android.InitBazelModule(module) |
| 1582 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1583 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1586 | // java_binary_host builds a `.jar` file and a shell script that executes it for the host. |
| 1587 | // |
| 1588 | // A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were |
| 1589 | // compiled against the host bootclasspath. |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1590 | func BinaryHostFactory() android.Module { |
| 1591 | module := &Binary{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1592 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1593 | module.addHostProperties() |
| 1594 | module.AddProperties(&module.binaryProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1595 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1596 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 1597 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1598 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst) |
| 1599 | android.InitDefaultableModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 1600 | android.InitBazelModule(module) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1601 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1604 | type JavaApiContribution struct { |
| 1605 | android.ModuleBase |
| 1606 | android.DefaultableModuleBase |
| 1607 | |
| 1608 | properties struct { |
| 1609 | // name of the API surface |
| 1610 | Api_surface *string |
| 1611 | |
| 1612 | // relative path to the API signature text file |
| 1613 | Api_file *string `android:"path"` |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | func ApiContributionFactory() android.Module { |
| 1618 | module := &JavaApiContribution{} |
| 1619 | android.InitAndroidModule(module) |
| 1620 | android.InitDefaultableModule(module) |
| 1621 | module.AddProperties(&module.properties) |
| 1622 | return module |
| 1623 | } |
| 1624 | |
| 1625 | type JavaApiImportInfo struct { |
| 1626 | ApiFile android.Path |
| 1627 | } |
| 1628 | |
| 1629 | var JavaApiImportProvider = blueprint.NewProvider(JavaApiImportInfo{}) |
| 1630 | |
| 1631 | func (ap *JavaApiContribution) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jihoon Kang | 3198f3c | 2023-01-26 08:08:52 +0000 | [diff] [blame] | 1632 | var apiFile android.Path = nil |
| 1633 | if apiFileString := ap.properties.Api_file; apiFileString != nil { |
| 1634 | apiFile = android.PathForModuleSrc(ctx, String(apiFileString)) |
| 1635 | } |
| 1636 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1637 | ctx.SetProvider(JavaApiImportProvider, JavaApiImportInfo{ |
| 1638 | ApiFile: apiFile, |
| 1639 | }) |
| 1640 | } |
| 1641 | |
| 1642 | type ApiLibrary struct { |
| 1643 | android.ModuleBase |
| 1644 | android.DefaultableModuleBase |
| 1645 | |
Spandan Das | cb368ea | 2023-03-22 04:27:05 +0000 | [diff] [blame] | 1646 | hiddenAPI |
| 1647 | dexer |
| 1648 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1649 | properties JavaApiLibraryProperties |
| 1650 | |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1651 | stubsSrcJar android.WritablePath |
| 1652 | stubsJar android.WritablePath |
| 1653 | stubsJarWithoutStaticLibs android.WritablePath |
| 1654 | extractedSrcJar android.WritablePath |
Spandan Das | cb368ea | 2023-03-22 04:27:05 +0000 | [diff] [blame] | 1655 | // .dex of stubs, used for hiddenapi processing |
| 1656 | dexJarFile OptionalDexJarPath |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | type JavaApiLibraryProperties struct { |
| 1660 | // name of the API surface |
| 1661 | Api_surface *string |
| 1662 | |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 1663 | // list of Java API contribution modules that consists this API surface |
Spandan Das | c082eb8 | 2022-12-01 21:43:06 +0000 | [diff] [blame] | 1664 | // This is a list of Soong modules |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 1665 | Api_contributions []string |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1666 | |
Spandan Das | c082eb8 | 2022-12-01 21:43:06 +0000 | [diff] [blame] | 1667 | // list of api.txt files relative to this directory that contribute to the |
| 1668 | // API surface. |
| 1669 | // This is a list of relative paths |
Jihoon Kang | 795319f | 2023-05-17 00:23:38 +0000 | [diff] [blame] | 1670 | Api_files []string `android:"path"` |
Spandan Das | c082eb8 | 2022-12-01 21:43:06 +0000 | [diff] [blame] | 1671 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1672 | // List of flags to be passed to the javac compiler to generate jar file |
| 1673 | Javacflags []string |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1674 | |
| 1675 | // List of shared java libs that this module has dependencies to and |
| 1676 | // should be passed as classpath in javac invocation |
| 1677 | Libs []string |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 1678 | |
| 1679 | // List of java libs that this module has static dependencies to and will be |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1680 | // merge zipped after metalava invocation |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 1681 | Static_libs []string |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1682 | |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1683 | // Java Api library to provide the full API surface stub jar file. |
| 1684 | // If this property is set, the stub jar of this module is created by |
| 1685 | // extracting the compiled class files provided by the |
| 1686 | // full_api_surface_stub module. |
| 1687 | Full_api_surface_stub *string |
Jihoon Kang | 862da6f | 2023-08-01 06:28:51 +0000 | [diff] [blame] | 1688 | |
| 1689 | // Version of previously released API file for compatibility check. |
| 1690 | Previous_api *string `android:"path"` |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | func ApiLibraryFactory() android.Module { |
| 1694 | module := &ApiLibrary{} |
| 1695 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1696 | module.AddProperties(&module.properties) |
Jihoon Kang | 1c51f50 | 2023-01-09 23:42:40 +0000 | [diff] [blame] | 1697 | android.InitDefaultableModule(module) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1698 | return module |
| 1699 | } |
| 1700 | |
| 1701 | func (al *ApiLibrary) ApiSurface() *string { |
| 1702 | return al.properties.Api_surface |
| 1703 | } |
| 1704 | |
| 1705 | func (al *ApiLibrary) StubsJar() android.Path { |
| 1706 | return al.stubsJar |
| 1707 | } |
| 1708 | |
| 1709 | func metalavaStubCmd(ctx android.ModuleContext, rule *android.RuleBuilder, |
| 1710 | srcs android.Paths, homeDir android.WritablePath) *android.RuleBuilderCommand { |
| 1711 | rule.Command().Text("rm -rf").Flag(homeDir.String()) |
| 1712 | rule.Command().Text("mkdir -p").Flag(homeDir.String()) |
| 1713 | |
| 1714 | cmd := rule.Command() |
| 1715 | cmd.FlagWithArg("ANDROID_PREFS_ROOT=", homeDir.String()) |
| 1716 | |
| 1717 | if metalavaUseRbe(ctx) { |
| 1718 | rule.Remoteable(android.RemoteRuleSupports{RBE: true}) |
| 1719 | execStrategy := ctx.Config().GetenvWithDefault("RBE_METALAVA_EXEC_STRATEGY", remoteexec.LocalExecStrategy) |
| 1720 | labels := map[string]string{"type": "tool", "name": "metalava"} |
| 1721 | |
| 1722 | pool := ctx.Config().GetenvWithDefault("RBE_METALAVA_POOL", "java16") |
| 1723 | rule.Rewrapper(&remoteexec.REParams{ |
| 1724 | Labels: labels, |
| 1725 | ExecStrategy: execStrategy, |
| 1726 | ToolchainInputs: []string{config.JavaCmd(ctx).String()}, |
| 1727 | Platform: map[string]string{remoteexec.PoolKey: pool}, |
| 1728 | }) |
| 1729 | } |
| 1730 | |
| 1731 | cmd.BuiltTool("metalava").ImplicitTool(ctx.Config().HostJavaToolPath(ctx, "metalava.jar")). |
| 1732 | Flag(config.JavacVmFlags). |
| 1733 | Flag("-J--add-opens=java.base/java.util=ALL-UNNAMED"). |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1734 | FlagWithInputList("--source-files ", srcs, " ") |
| 1735 | |
MÃ¥rten Kongstad | bd26244 | 2023-07-12 14:01:49 +0200 | [diff] [blame] | 1736 | cmd.Flag("--color"). |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1737 | Flag("--quiet"). |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 1738 | Flag("--include-annotations"). |
| 1739 | // The flag makes nullability issues as warnings rather than errors by replacing |
| 1740 | // @Nullable/@NonNull in the listed packages APIs with @RecentlyNullable/@RecentlyNonNull, |
| 1741 | // and these packages are meant to have everything annotated |
| 1742 | // @RecentlyNullable/@RecentlyNonNull. |
| 1743 | FlagWithArg("--force-convert-to-warning-nullability-annotations ", "+*:-android.*:+android.icu.*:-dalvik.*"). |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1744 | FlagWithArg("--repeat-errors-max ", "10"). |
| 1745 | FlagWithArg("--hide ", "UnresolvedImport"). |
| 1746 | FlagWithArg("--hide ", "InvalidNullabilityOverride"). |
| 1747 | FlagWithArg("--hide ", "ChangedDefault") |
| 1748 | |
Paul Duffin | 336b16a | 2023-08-15 23:10:13 +0100 | [diff] [blame] | 1749 | // The main purpose of the `--api-class-resolution api` option is to force metalava to ignore |
| 1750 | // classes on the classpath when an API file contains missing classes. However, as this command |
| 1751 | // does not specify `--classpath` this is not needed for that. However, this is also used as a |
| 1752 | // signal to the special metalava code for generating stubs from text files that it needs to add |
| 1753 | // some additional items into the API (e.g. default constructors). |
Paul Duffin | 5b7035f | 2023-05-31 17:51:33 +0100 | [diff] [blame] | 1754 | cmd.FlagWithArg("--api-class-resolution ", "api") |
| 1755 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1756 | return cmd |
| 1757 | } |
| 1758 | |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 1759 | func (al *ApiLibrary) HeaderJars() android.Paths { |
| 1760 | return android.Paths{al.stubsJar} |
| 1761 | } |
| 1762 | |
| 1763 | func (al *ApiLibrary) OutputDirAndDeps() (android.Path, android.Paths) { |
| 1764 | return nil, nil |
| 1765 | } |
| 1766 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1767 | func (al *ApiLibrary) stubsFlags(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, stubsDir android.OptionalPath) { |
| 1768 | if stubsDir.Valid() { |
| 1769 | cmd.FlagWithArg("--stubs ", stubsDir.String()) |
| 1770 | } |
| 1771 | } |
| 1772 | |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1773 | // This method extracts the stub class files from the stub jar file provided |
| 1774 | // from full_api_surface_stub module instead of compiling the srcjar generated from invoking metalava. |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1775 | // This method is used because metalava can generate compilable from-text stubs only when |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1776 | // the codebase encompasses all classes listed in the input API text file, and a class can extend |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1777 | // a class that is not within the same API domain. |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1778 | func (al *ApiLibrary) extractApiSrcs(ctx android.ModuleContext, rule *android.RuleBuilder, stubsDir android.OptionalPath, fullApiSurfaceStubJar android.Path) { |
| 1779 | classFilesList := android.PathForModuleOut(ctx, "metalava", "classes.txt") |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1780 | unzippedSrcJarDir := android.PathForModuleOut(ctx, "metalava", "unzipDir") |
| 1781 | |
| 1782 | rule.Command(). |
| 1783 | BuiltTool("list_files"). |
| 1784 | Text(stubsDir.String()). |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1785 | FlagWithOutput("--out ", classFilesList). |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1786 | FlagWithArg("--extensions ", ".java"). |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1787 | FlagWithArg("--root ", unzippedSrcJarDir.String()). |
| 1788 | Flag("--classes") |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1789 | |
| 1790 | rule.Command(). |
| 1791 | Text("unzip"). |
| 1792 | Flag("-q"). |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1793 | Input(fullApiSurfaceStubJar). |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1794 | FlagWithArg("-d ", unzippedSrcJarDir.String()) |
| 1795 | |
| 1796 | rule.Command(). |
| 1797 | BuiltTool("soong_zip"). |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1798 | Flag("-jar"). |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1799 | Flag("-write_if_changed"). |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1800 | Flag("-ignore_missing_files"). |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1801 | FlagWithArg("-C ", unzippedSrcJarDir.String()). |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1802 | FlagWithInput("-l ", classFilesList). |
| 1803 | FlagWithOutput("-o ", al.stubsJarWithoutStaticLibs) |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1804 | } |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1805 | |
| 1806 | func (al *ApiLibrary) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 1807 | apiContributions := al.properties.Api_contributions |
| 1808 | for _, apiContributionName := range apiContributions { |
| 1809 | ctx.AddDependency(ctx.Module(), javaApiContributionTag, apiContributionName) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1810 | } |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1811 | ctx.AddVariationDependencies(nil, libTag, al.properties.Libs...) |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 1812 | ctx.AddVariationDependencies(nil, staticLibTag, al.properties.Static_libs...) |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1813 | if al.properties.Full_api_surface_stub != nil { |
| 1814 | ctx.AddVariationDependencies(nil, depApiSrcsTag, String(al.properties.Full_api_surface_stub)) |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1815 | } |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Jihoon Kang | 478ca5b | 2023-08-11 23:33:05 +0000 | [diff] [blame] | 1818 | // API signature file names sorted from |
| 1819 | // the narrowest api scope to the widest api scope |
| 1820 | var scopeOrderedSourceFileNames = allApiScopes.Strings( |
| 1821 | func(s *apiScope) string { return s.apiFilePrefix + "current.txt" }) |
| 1822 | |
Jihoon Kang | 84473f5 | 2023-08-11 22:36:33 +0000 | [diff] [blame] | 1823 | func (al *ApiLibrary) sortApiFilesByApiScope(ctx android.ModuleContext, srcFiles android.Paths) android.Paths { |
| 1824 | sortedSrcFiles := android.Paths{} |
| 1825 | |
Jihoon Kang | 84473f5 | 2023-08-11 22:36:33 +0000 | [diff] [blame] | 1826 | for _, scopeSourceFileName := range scopeOrderedSourceFileNames { |
| 1827 | for _, sourceFileName := range srcFiles { |
| 1828 | if sourceFileName.Base() == scopeSourceFileName { |
| 1829 | sortedSrcFiles = append(sortedSrcFiles, sourceFileName) |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | if len(srcFiles) != len(sortedSrcFiles) { |
| 1834 | ctx.ModuleErrorf("Unrecognizable source file found within %s", srcFiles) |
| 1835 | } |
| 1836 | |
| 1837 | return sortedSrcFiles |
| 1838 | } |
| 1839 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1840 | func (al *ApiLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1841 | |
| 1842 | rule := android.NewRuleBuilder(pctx, ctx) |
| 1843 | |
| 1844 | rule.Sbox(android.PathForModuleOut(ctx, "metalava"), |
| 1845 | android.PathForModuleOut(ctx, "metalava.sbox.textproto")). |
| 1846 | SandboxInputs() |
| 1847 | |
| 1848 | var stubsDir android.OptionalPath |
| 1849 | stubsDir = android.OptionalPathForPath(android.PathForModuleOut(ctx, "metalava", "stubsDir")) |
| 1850 | rule.Command().Text("rm -rf").Text(stubsDir.String()) |
| 1851 | rule.Command().Text("mkdir -p").Text(stubsDir.String()) |
| 1852 | |
| 1853 | homeDir := android.PathForModuleOut(ctx, "metalava", "home") |
| 1854 | |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1855 | var srcFiles android.Paths |
| 1856 | var classPaths android.Paths |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 1857 | var staticLibs android.Paths |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1858 | var depApiSrcsStubsJar android.Path |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1859 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 1860 | tag := ctx.OtherModuleDependencyTag(dep) |
| 1861 | switch tag { |
| 1862 | case javaApiContributionTag: |
| 1863 | provider := ctx.OtherModuleProvider(dep, JavaApiImportProvider).(JavaApiImportInfo) |
Jihoon Kang | 3198f3c | 2023-01-26 08:08:52 +0000 | [diff] [blame] | 1864 | providerApiFile := provider.ApiFile |
Jihoon Kang | 160634c | 2023-05-25 05:28:29 +0000 | [diff] [blame] | 1865 | if providerApiFile == nil && !ctx.Config().AllowMissingDependencies() { |
Jihoon Kang | 3198f3c | 2023-01-26 08:08:52 +0000 | [diff] [blame] | 1866 | ctx.ModuleErrorf("Error: %s has an empty api file.", dep.Name()) |
| 1867 | } |
| 1868 | srcFiles = append(srcFiles, android.PathForSource(ctx, providerApiFile.String())) |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1869 | case libTag: |
| 1870 | provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) |
| 1871 | classPaths = append(classPaths, provider.HeaderJars...) |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 1872 | case staticLibTag: |
| 1873 | provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) |
| 1874 | staticLibs = append(staticLibs, provider.HeaderJars...) |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1875 | case depApiSrcsTag: |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1876 | provider := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) |
| 1877 | depApiSrcsStubsJar = provider.HeaderJars[0] |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1878 | } |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 1879 | }) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1880 | |
Spandan Das | c082eb8 | 2022-12-01 21:43:06 +0000 | [diff] [blame] | 1881 | // Add the api_files inputs |
| 1882 | for _, api := range al.properties.Api_files { |
Jihoon Kang | 795319f | 2023-05-17 00:23:38 +0000 | [diff] [blame] | 1883 | srcFiles = append(srcFiles, android.PathForModuleSrc(ctx, api)) |
Spandan Das | c082eb8 | 2022-12-01 21:43:06 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Jihoon Kang | 160634c | 2023-05-25 05:28:29 +0000 | [diff] [blame] | 1886 | if srcFiles == nil && !ctx.Config().AllowMissingDependencies() { |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1887 | ctx.ModuleErrorf("Error: %s has an empty api file.", ctx.ModuleName()) |
| 1888 | } |
| 1889 | |
Jihoon Kang | 84473f5 | 2023-08-11 22:36:33 +0000 | [diff] [blame] | 1890 | srcFiles = al.sortApiFilesByApiScope(ctx, srcFiles) |
| 1891 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1892 | cmd := metalavaStubCmd(ctx, rule, srcFiles, homeDir) |
| 1893 | |
| 1894 | al.stubsFlags(ctx, cmd, stubsDir) |
| 1895 | |
Jihoon Kang | 862da6f | 2023-08-01 06:28:51 +0000 | [diff] [blame] | 1896 | migratingNullability := String(al.properties.Previous_api) != "" |
| 1897 | if migratingNullability { |
| 1898 | previousApi := android.PathForModuleSrc(ctx, String(al.properties.Previous_api)) |
| 1899 | cmd.FlagWithInput("--migrate-nullness ", previousApi) |
| 1900 | } |
| 1901 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1902 | al.stubsSrcJar = android.PathForModuleOut(ctx, "metalava", ctx.ModuleName()+"-"+"stubs.srcjar") |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1903 | al.stubsJarWithoutStaticLibs = android.PathForModuleOut(ctx, "metalava", "stubs.jar") |
| 1904 | al.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName(), fmt.Sprintf("%s.jar", ctx.ModuleName())) |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1905 | |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1906 | if depApiSrcsStubsJar != nil { |
| 1907 | al.extractApiSrcs(ctx, rule, stubsDir, depApiSrcsStubsJar) |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1908 | } |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1909 | rule.Command(). |
| 1910 | BuiltTool("soong_zip"). |
| 1911 | Flag("-write_if_changed"). |
| 1912 | Flag("-jar"). |
| 1913 | FlagWithOutput("-o ", al.stubsSrcJar). |
| 1914 | FlagWithArg("-C ", stubsDir.String()). |
| 1915 | FlagWithArg("-D ", stubsDir.String()) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1916 | |
Paul Duffin | 336b16a | 2023-08-15 23:10:13 +0100 | [diff] [blame] | 1917 | rule.Build("metalava", "metalava merged text") |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1918 | |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1919 | if depApiSrcsStubsJar == nil { |
| 1920 | var flags javaBuilderFlags |
| 1921 | flags.javaVersion = getStubsJavaVersion() |
| 1922 | flags.javacFlags = strings.Join(al.properties.Javacflags, " ") |
| 1923 | flags.classpath = classpath(classPaths) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1924 | |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 1925 | annoSrcJar := android.PathForModuleOut(ctx, ctx.ModuleName(), "anno.srcjar") |
| 1926 | |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1927 | TransformJavaToClasses(ctx, al.stubsJarWithoutStaticLibs, 0, android.Paths{}, |
Vadim Spivak | 3c496f0 | 2023-06-08 06:14:59 +0000 | [diff] [blame] | 1928 | android.Paths{al.stubsSrcJar}, annoSrcJar, flags, android.Paths{}) |
Jihoon Kang | ca198c2 | 2023-06-22 23:13:51 +0000 | [diff] [blame] | 1929 | } |
Jihoon Kang | 423d229 | 2022-11-29 23:10:10 +0000 | [diff] [blame] | 1930 | |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 1931 | builder := android.NewRuleBuilder(pctx, ctx) |
| 1932 | builder.Command(). |
| 1933 | BuiltTool("merge_zips"). |
| 1934 | Output(al.stubsJar). |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 1935 | Inputs(android.Paths{al.stubsJarWithoutStaticLibs}). |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 1936 | Inputs(staticLibs) |
| 1937 | builder.Build("merge_zips", "merge jar files") |
| 1938 | |
Spandan Das | cb368ea | 2023-03-22 04:27:05 +0000 | [diff] [blame] | 1939 | // compile stubs to .dex for hiddenapi processing |
| 1940 | dexParams := &compileDexParams{ |
| 1941 | flags: javaBuilderFlags{}, |
| 1942 | sdkVersion: al.SdkVersion(ctx), |
| 1943 | minSdkVersion: al.MinSdkVersion(ctx), |
| 1944 | classesJar: al.stubsJar, |
| 1945 | jarName: ctx.ModuleName() + ".jar", |
| 1946 | } |
| 1947 | dexOutputFile := al.dexer.compileDex(ctx, dexParams) |
| 1948 | uncompressed := true |
| 1949 | al.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), al.stubsJar, &uncompressed) |
| 1950 | dexOutputFile = al.hiddenAPIEncodeDex(ctx, dexOutputFile) |
| 1951 | al.dexJarFile = makeDexJarPathFromPath(dexOutputFile) |
| 1952 | |
Jihoon Kang | 423d229 | 2022-11-29 23:10:10 +0000 | [diff] [blame] | 1953 | ctx.Phony(ctx.ModuleName(), al.stubsJar) |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1954 | |
| 1955 | ctx.SetProvider(JavaInfoProvider, JavaInfo{ |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 1956 | HeaderJars: android.PathsIfNonNil(al.stubsJar), |
| 1957 | ImplementationAndResourcesJars: android.PathsIfNonNil(al.stubsJar), |
| 1958 | ImplementationJars: android.PathsIfNonNil(al.stubsJar), |
| 1959 | AidlIncludeDirs: android.Paths{}, |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 1960 | // No aconfig libraries on api libraries |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1961 | }) |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Spandan Das | cb368ea | 2023-03-22 04:27:05 +0000 | [diff] [blame] | 1964 | func (al *ApiLibrary) DexJarBuildPath() OptionalDexJarPath { |
| 1965 | return al.dexJarFile |
| 1966 | } |
| 1967 | |
| 1968 | func (al *ApiLibrary) DexJarInstallPath() android.Path { |
| 1969 | return al.dexJarFile.Path() |
| 1970 | } |
| 1971 | |
| 1972 | func (al *ApiLibrary) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { |
| 1973 | return nil |
| 1974 | } |
| 1975 | |
| 1976 | // java_api_library constitutes the sdk, and does not build against one |
| 1977 | func (al *ApiLibrary) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { |
| 1978 | return android.SdkSpecNone |
| 1979 | } |
| 1980 | |
| 1981 | // java_api_library is always at "current". Return FutureApiLevel |
| 1982 | func (al *ApiLibrary) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { |
| 1983 | return android.FutureApiLevel |
| 1984 | } |
| 1985 | |
| 1986 | // implement the following interfaces for hiddenapi processing |
| 1987 | var _ hiddenAPIModule = (*ApiLibrary)(nil) |
| 1988 | var _ UsesLibraryDependency = (*ApiLibrary)(nil) |
| 1989 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1990 | // |
| 1991 | // Java prebuilts |
| 1992 | // |
| 1993 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1994 | type ImportProperties struct { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1995 | Jars []string `android:"path,arch_variant"` |
Colin Cross | 461bd1a | 2017-10-20 13:59:18 -0700 | [diff] [blame] | 1996 | |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1997 | // The version of the SDK that the source prebuilt file was built against. Defaults to the |
| 1998 | // current version if not specified. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 1999 | Sdk_version *string |
Colin Cross | 535e2cf | 2017-10-20 17:57:49 -0700 | [diff] [blame] | 2000 | |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 2001 | // The minimum version of the SDK that this module supports. Defaults to sdk_version if not |
| 2002 | // specified. |
| 2003 | Min_sdk_version *string |
| 2004 | |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 2005 | // The max sdk version placeholder used to replace maxSdkVersion attributes on permission |
| 2006 | // and uses-permission tags in manifest_fixer. |
| 2007 | Replace_max_sdk_version_placeholder *string |
| 2008 | |
Colin Cross | 535e2cf | 2017-10-20 17:57:49 -0700 | [diff] [blame] | 2009 | Installable *bool |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2010 | |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 2011 | // If not empty, classes are restricted to the specified packages and their sub-packages. |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 2012 | Permitted_packages []string |
| 2013 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2014 | // List of shared java libs that this module has dependencies to |
| 2015 | Libs []string |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 2016 | |
| 2017 | // List of files to remove from the jar file(s) |
| 2018 | Exclude_files []string |
| 2019 | |
| 2020 | // List of directories to remove from the jar file(s) |
| 2021 | Exclude_dirs []string |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 2022 | |
| 2023 | // if set to true, run Jetifier against .jar file. Defaults to false. |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 2024 | Jetifier *bool |
Jiyong Park | 4c4c024 | 2019-10-21 14:53:15 +0900 | [diff] [blame] | 2025 | |
| 2026 | // set the name of the output |
| 2027 | Stem *string |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 2028 | |
| 2029 | Aidl struct { |
| 2030 | // directories that should be added as include directories for any aidl sources of modules |
| 2031 | // that depend on this module, as well as to aidl for this module. |
| 2032 | Export_include_dirs []string |
| 2033 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | type Import struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 2037 | android.ModuleBase |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 2038 | android.DefaultableModuleBase |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2039 | android.ApexModuleBase |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 2040 | android.BazelModuleBase |
Colin Cross | ec7a042 | 2017-07-07 14:47:12 -0700 | [diff] [blame] | 2041 | prebuilt android.Prebuilt |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 2042 | |
Paul Duffin | 0d3c2e1 | 2020-05-17 08:34:50 +0100 | [diff] [blame] | 2043 | // Functionality common to Module and Import. |
| 2044 | embeddableInModuleAndImport |
| 2045 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2046 | hiddenAPI |
| 2047 | dexer |
Bill Peckham | ff89ffa | 2020-12-23 16:13:04 -0800 | [diff] [blame] | 2048 | dexpreopter |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2049 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2050 | properties ImportProperties |
| 2051 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2052 | // output file containing classes.dex and resources |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2053 | dexJarFile OptionalDexJarPath |
Jeongik Cha | d5fe878 | 2021-07-08 01:13:11 +0900 | [diff] [blame] | 2054 | dexJarInstallFile android.Path |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2055 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 2056 | combinedClasspathFile android.Path |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 2057 | classLoaderContexts dexpreopt.ClassLoaderContextMap |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 2058 | exportAidlIncludeDirs android.Paths |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 2059 | |
| 2060 | hideApexVariantFromMake bool |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 2061 | |
| 2062 | sdkVersion android.SdkSpec |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 2063 | minSdkVersion android.ApiLevel |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Paul Duffin | 630b11e | 2021-07-15 13:35:26 +0100 | [diff] [blame] | 2066 | var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil) |
| 2067 | |
| 2068 | func (j *Import) PermittedPackagesForUpdatableBootJars() []string { |
| 2069 | return j.properties.Permitted_packages |
| 2070 | } |
| 2071 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 2072 | func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { |
| 2073 | return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version)) |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 2076 | func (j *Import) SystemModules() string { |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2077 | return "none" |
| 2078 | } |
| 2079 | |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 2080 | func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 2081 | if j.properties.Min_sdk_version != nil { |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 2082 | return android.ApiLevelFrom(ctx, *j.properties.Min_sdk_version) |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 2083 | } |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 2084 | return j.SdkVersion(ctx).ApiLevel |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 2087 | func (j *Import) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.ApiLevel { |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 2088 | if j.properties.Replace_max_sdk_version_placeholder != nil { |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 2089 | return android.ApiLevelFrom(ctx, *j.properties.Replace_max_sdk_version_placeholder) |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 2090 | } |
Spandan Das | a26eda7 | 2023-03-02 00:56:06 +0000 | [diff] [blame] | 2091 | // Default is PrivateApiLevel |
| 2092 | return android.SdkSpecPrivate.ApiLevel |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Spandan Das | ca70fc4 | 2023-03-01 23:38:49 +0000 | [diff] [blame] | 2095 | func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.ApiLevel { |
| 2096 | return j.SdkVersion(ctx).ApiLevel |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2099 | func (j *Import) Prebuilt() *android.Prebuilt { |
Colin Cross | ec7a042 | 2017-07-07 14:47:12 -0700 | [diff] [blame] | 2100 | return &j.prebuilt |
| 2101 | } |
| 2102 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2103 | func (j *Import) PrebuiltSrcs() []string { |
| 2104 | return j.properties.Jars |
| 2105 | } |
| 2106 | |
| 2107 | func (j *Import) Name() string { |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 2108 | return j.prebuilt.Name(j.ModuleBase.Name()) |
| 2109 | } |
| 2110 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 2111 | func (j *Import) Stem() string { |
| 2112 | return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name()) |
| 2113 | } |
| 2114 | |
Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 2115 | func (a *Import) JacocoReportClassesFile() android.Path { |
| 2116 | return nil |
| 2117 | } |
| 2118 | |
Bill Peckham | a41a696 | 2021-01-11 10:58:54 -0800 | [diff] [blame] | 2119 | func (j *Import) LintDepSets() LintDepSets { |
| 2120 | return LintDepSets{} |
| 2121 | } |
| 2122 | |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 2123 | func (j *Import) getStrictUpdatabilityLinting() bool { |
| 2124 | return false |
| 2125 | } |
| 2126 | |
| 2127 | func (j *Import) setStrictUpdatabilityLinting(bool) { |
| 2128 | } |
| 2129 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2130 | func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 2131 | ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2132 | |
| 2133 | if ctx.Device() && Bool(j.dexProperties.Compile_dex) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 2134 | sdkDeps(ctx, android.SdkContext(j), j.dexer) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2135 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2138 | func (j *Import) commonBuildActions(ctx android.ModuleContext) { |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 2139 | //TODO(b/231322772) these should come from Bazel once available |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 2140 | j.sdkVersion = j.SdkVersion(ctx) |
| 2141 | j.minSdkVersion = j.MinSdkVersion(ctx) |
| 2142 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 2143 | if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() { |
| 2144 | j.hideApexVariantFromMake = true |
| 2145 | } |
| 2146 | |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 2147 | if ctx.Windows() { |
| 2148 | j.HideFromMake() |
| 2149 | } |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 2153 | j.commonBuildActions(ctx) |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 2154 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 2155 | jars := android.PathsForModuleSrc(ctx, j.properties.Jars) |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 2156 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 2157 | jarName := j.Stem() + ".jar" |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 2158 | outputFile := android.PathForModuleOut(ctx, "combined", jarName) |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 2159 | TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{}, |
| 2160 | false, j.properties.Exclude_files, j.properties.Exclude_dirs) |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 2161 | if Bool(j.properties.Jetifier) { |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 2162 | inputFile := outputFile |
| 2163 | outputFile = android.PathForModuleOut(ctx, "jetifier", jarName) |
| 2164 | TransformJetifier(ctx, outputFile, inputFile) |
| 2165 | } |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 2166 | j.combinedClasspathFile = outputFile |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 2167 | j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2168 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2169 | var flags javaBuilderFlags |
| 2170 | |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 2171 | j.collectTransitiveHeaderJars(ctx) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2172 | ctx.VisitDirectDeps(func(module android.Module) { |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2173 | tag := ctx.OtherModuleDependencyTag(module) |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2174 | if ctx.OtherModuleHasProvider(module, JavaInfoProvider) { |
| 2175 | dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2176 | switch tag { |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 2177 | case libTag, sdkLibTag: |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 2178 | flags.classpath = append(flags.classpath, dep.HeaderJars...) |
| 2179 | flags.dexClasspath = append(flags.dexClasspath, dep.HeaderJars...) |
| 2180 | case staticLibTag: |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2181 | flags.classpath = append(flags.classpath, dep.HeaderJars...) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2182 | case bootClasspathTag: |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2183 | flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2184 | } |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2185 | } else if dep, ok := module.(SdkLibraryDependency); ok { |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2186 | switch tag { |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 2187 | case libTag, sdkLibTag: |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 2188 | flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2189 | } |
| 2190 | } |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2191 | |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2192 | addCLCFromDep(ctx, module, j.classLoaderContexts) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2193 | }) |
| 2194 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2195 | j.maybeInstall(ctx, jarName, outputFile) |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 2196 | |
| 2197 | j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2198 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2199 | if ctx.Device() { |
| 2200 | // If this is a variant created for a prebuilt_apex then use the dex implementation jar |
| 2201 | // obtained from the associated deapexer module. |
| 2202 | ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 2203 | if ai.ForPrebuiltApex { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2204 | // Get the path of the dex implementation jar from the `deapexer` module. |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 2205 | di := android.FindDeapexerProviderForModule(ctx) |
| 2206 | if di == nil { |
| 2207 | return // An error has been reported by FindDeapexerProviderForModule. |
| 2208 | } |
Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 2209 | dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(j.BaseModuleName()) |
| 2210 | if dexOutputPath := di.PrebuiltExportPath(dexJarFileApexRootRelative); dexOutputPath != nil { |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2211 | dexJarFile := makeDexJarPathFromPath(dexOutputPath) |
| 2212 | j.dexJarFile = dexJarFile |
Jiakai Zhang | 5b24f72 | 2021-09-30 09:32:57 +0000 | [diff] [blame] | 2213 | installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName())) |
| 2214 | j.dexJarInstallFile = installPath |
Paul Duffin | 74d18d1 | 2021-05-14 14:18:47 +0100 | [diff] [blame] | 2215 | |
Jiakai Zhang | 5b24f72 | 2021-09-30 09:32:57 +0000 | [diff] [blame] | 2216 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath) |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 2217 | setUncompressDex(ctx, &j.dexpreopter, &j.dexer) |
Jiakai Zhang | 5b24f72 | 2021-09-30 09:32:57 +0000 | [diff] [blame] | 2218 | j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex |
Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 2219 | |
| 2220 | if profilePath := di.PrebuiltExportPath(dexJarFileApexRootRelative + ".prof"); profilePath != nil { |
| 2221 | j.dexpreopter.inputProfilePathOnHost = profilePath |
| 2222 | } |
| 2223 | |
Jiakai Zhang | 5b24f72 | 2021-09-30 09:32:57 +0000 | [diff] [blame] | 2224 | j.dexpreopt(ctx, dexOutputPath) |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 2225 | |
| 2226 | // Initialize the hiddenapi structure. |
| 2227 | j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex) |
Paul Duffin | 9d67ca6 | 2021-02-03 20:06:33 +0000 | [diff] [blame] | 2228 | } else { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2229 | // This should never happen as a variant for a prebuilt_apex is only created if the |
| 2230 | // prebuilt_apex has been configured to export the java library dex file. |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 2231 | ctx.ModuleErrorf("internal error: no dex implementation jar available from prebuilt APEX %s", di.ApexModuleName()) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2232 | } |
| 2233 | } else if Bool(j.dexProperties.Compile_dex) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 2234 | sdkDep := decodeSdkDep(ctx, android.SdkContext(j)) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2235 | if sdkDep.invalidVersion { |
| 2236 | ctx.AddMissingDependencies(sdkDep.bootclasspath) |
| 2237 | ctx.AddMissingDependencies(sdkDep.java9Classpath) |
| 2238 | } else if sdkDep.useFiles { |
| 2239 | // sdkDep.jar is actually equivalent to turbine header.jar. |
| 2240 | flags.classpath = append(flags.classpath, sdkDep.jars...) |
| 2241 | } |
| 2242 | |
| 2243 | // Dex compilation |
| 2244 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 2245 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath( |
| 2246 | ctx, android.PathForModuleInstall(ctx, "framework", jarName)) |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 2247 | setUncompressDex(ctx, &j.dexpreopter, &j.dexer) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2248 | j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex |
| 2249 | |
Paul Duffin | 612e610 | 2021-02-02 13:38:13 +0000 | [diff] [blame] | 2250 | var dexOutputFile android.OutputPath |
Spandan Das | c404cc7 | 2023-02-23 18:05:05 +0000 | [diff] [blame] | 2251 | dexParams := &compileDexParams{ |
| 2252 | flags: flags, |
| 2253 | sdkVersion: j.SdkVersion(ctx), |
| 2254 | minSdkVersion: j.MinSdkVersion(ctx), |
| 2255 | classesJar: outputFile, |
| 2256 | jarName: jarName, |
| 2257 | } |
| 2258 | |
| 2259 | dexOutputFile = j.dexer.compileDex(ctx, dexParams) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2260 | if ctx.Failed() { |
| 2261 | return |
| 2262 | } |
| 2263 | |
Paul Duffin | 74d18d1 | 2021-05-14 14:18:47 +0100 | [diff] [blame] | 2264 | // Initialize the hiddenapi structure. |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2265 | j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex) |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 2266 | |
| 2267 | // Encode hidden API flags in dex file. |
Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 2268 | dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 2269 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2270 | j.dexJarFile = makeDexJarPathFromPath(dexOutputFile) |
Jeongik Cha | d5fe878 | 2021-07-08 01:13:11 +0900 | [diff] [blame] | 2271 | j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2272 | } |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2273 | } |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2274 | |
| 2275 | ctx.SetProvider(JavaInfoProvider, JavaInfo{ |
| 2276 | HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile), |
Sam Delmerico | 9f9c0a2 | 2022-11-29 11:19:37 -0500 | [diff] [blame] | 2277 | TransitiveLibsHeaderJars: j.transitiveLibsHeaderJars, |
| 2278 | TransitiveStaticLibsHeaderJars: j.transitiveStaticLibsHeaderJars, |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2279 | ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile), |
| 2280 | ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile), |
| 2281 | AidlIncludeDirs: j.exportAidlIncludeDirs, |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 2282 | // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2283 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2286 | func (j *Import) maybeInstall(ctx android.ModuleContext, jarName string, outputFile android.Path) { |
| 2287 | if !Bool(j.properties.Installable) { |
| 2288 | return |
| 2289 | } |
| 2290 | |
| 2291 | var installDir android.InstallPath |
| 2292 | if ctx.InstallInTestcases() { |
| 2293 | var archDir string |
| 2294 | if !ctx.Host() { |
| 2295 | archDir = ctx.DeviceConfig().DeviceArch() |
| 2296 | } |
| 2297 | installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir) |
| 2298 | } else { |
| 2299 | installDir = android.PathForModuleInstall(ctx, "framework") |
| 2300 | } |
| 2301 | ctx.InstallFile(installDir, jarName, outputFile) |
| 2302 | } |
| 2303 | |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 2304 | func (j *Import) OutputFiles(tag string) (android.Paths, error) { |
| 2305 | switch tag { |
Saeid Farivar Asanjan | 128fe5c | 2020-10-15 17:54:40 +0000 | [diff] [blame] | 2306 | case "", ".jar": |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 2307 | return android.Paths{j.combinedClasspathFile}, nil |
| 2308 | default: |
| 2309 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | var _ android.OutputFileProducer = (*Import)(nil) |
| 2314 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 2315 | func (j *Import) HeaderJars() android.Paths { |
albaltai | 36ff7dc | 2018-12-25 14:35:23 +0800 | [diff] [blame] | 2316 | if j.combinedClasspathFile == nil { |
| 2317 | return nil |
| 2318 | } |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 2319 | return android.Paths{j.combinedClasspathFile} |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 2322 | func (j *Import) ImplementationAndResourcesJars() android.Paths { |
albaltai | 36ff7dc | 2018-12-25 14:35:23 +0800 | [diff] [blame] | 2323 | if j.combinedClasspathFile == nil { |
| 2324 | return nil |
| 2325 | } |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 2326 | return android.Paths{j.combinedClasspathFile} |
| 2327 | } |
| 2328 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2329 | func (j *Import) DexJarBuildPath() OptionalDexJarPath { |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2330 | return j.dexJarFile |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 2331 | } |
| 2332 | |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 2333 | func (j *Import) DexJarInstallPath() android.Path { |
Jeongik Cha | d5fe878 | 2021-07-08 01:13:11 +0900 | [diff] [blame] | 2334 | return j.dexJarInstallFile |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 2335 | } |
| 2336 | |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 2337 | func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { |
| 2338 | return j.classLoaderContexts |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 2339 | } |
| 2340 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 2341 | var _ android.ApexModule = (*Import)(nil) |
| 2342 | |
| 2343 | // Implements android.ApexModule |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 2344 | func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { |
Paul Duffin | 0d3c2e1 | 2020-05-17 08:34:50 +0100 | [diff] [blame] | 2345 | return j.depIsInSameApex(ctx, dep) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 2346 | } |
| 2347 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 2348 | // Implements android.ApexModule |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 2349 | func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext, |
| 2350 | sdkVersion android.ApiLevel) error { |
Spandan Das | 7fa982c | 2023-02-24 18:38:56 +0000 | [diff] [blame] | 2351 | sdkVersionSpec := j.SdkVersion(ctx) |
Spandan Das | 8c9ae7e | 2023-03-03 21:20:36 +0000 | [diff] [blame] | 2352 | minSdkVersion := j.MinSdkVersion(ctx) |
| 2353 | if !minSdkVersion.Specified() { |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 2354 | return fmt.Errorf("min_sdk_version is not specified") |
| 2355 | } |
Spandan Das | 7fa982c | 2023-02-24 18:38:56 +0000 | [diff] [blame] | 2356 | // If the module is compiling against core (via sdk_version), skip comparison check. |
| 2357 | if sdkVersionSpec.Kind == android.SdkCore { |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 2358 | return nil |
| 2359 | } |
Spandan Das | 7fa982c | 2023-02-24 18:38:56 +0000 | [diff] [blame] | 2360 | if minSdkVersion.GreaterThan(sdkVersion) { |
| 2361 | return fmt.Errorf("newer SDK(%v)", minSdkVersion) |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 2362 | } |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 2363 | return nil |
| 2364 | } |
| 2365 | |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 2366 | // requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or |
| 2367 | // java_sdk_library_import with the specified base module name requires to be exported from a |
| 2368 | // prebuilt_apex/apex_set. |
Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 2369 | func requiredFilesFromPrebuiltApexForImport(name string, d *dexpreopter) []string { |
| 2370 | dexJarFileApexRootRelative := apexRootRelativePathToJavaLib(name) |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 2371 | // Add the dex implementation jar to the set of exported files. |
Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 2372 | files := []string{ |
| 2373 | dexJarFileApexRootRelative, |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 2374 | } |
Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 2375 | if BoolDefault(d.importDexpreoptProperties.Dex_preopt.Profile_guided, false) { |
| 2376 | files = append(files, dexJarFileApexRootRelative+".prof") |
| 2377 | } |
| 2378 | return files |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 2379 | } |
| 2380 | |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 2381 | // apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for |
| 2382 | // the java library with the specified name. |
| 2383 | func apexRootRelativePathToJavaLib(name string) string { |
| 2384 | return filepath.Join("javalib", name+".jar") |
| 2385 | } |
| 2386 | |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 2387 | var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil) |
| 2388 | |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 2389 | func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string { |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 2390 | name := j.BaseModuleName() |
Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 2391 | return requiredFilesFromPrebuiltApexForImport(name, &j.dexpreopter) |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 2392 | } |
| 2393 | |
albaltai | 36ff7dc | 2018-12-25 14:35:23 +0800 | [diff] [blame] | 2394 | // Add compile time check for interface implementation |
| 2395 | var _ android.IDEInfo = (*Import)(nil) |
| 2396 | var _ android.IDECustomizedModuleName = (*Import)(nil) |
| 2397 | |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 2398 | // Collect information for opening IDE project files in java/jdeps.go. |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 2399 | |
| 2400 | func (j *Import) IDEInfo(dpInfo *android.IdeInfo) { |
| 2401 | dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...) |
| 2402 | } |
| 2403 | |
| 2404 | func (j *Import) IDECustomizedModuleName() string { |
| 2405 | // TODO(b/113562217): Extract the base module name from the Import name, often the Import name |
| 2406 | // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better |
| 2407 | // solution to get the Import name. |
Ulya Trafimovich | 497a093 | 2021-07-14 16:35:33 +0100 | [diff] [blame] | 2408 | return android.RemoveOptionalPrebuiltPrefix(j.Name()) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 2409 | } |
| 2410 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2411 | var _ android.PrebuiltInterface = (*Import)(nil) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 2412 | |
Bill Peckham | ff89ffa | 2020-12-23 16:13:04 -0800 | [diff] [blame] | 2413 | func (j *Import) IsInstallable() bool { |
| 2414 | return Bool(j.properties.Installable) |
| 2415 | } |
| 2416 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 2417 | var _ DexpreopterInterface = (*Import)(nil) |
Bill Peckham | ff89ffa | 2020-12-23 16:13:04 -0800 | [diff] [blame] | 2418 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2419 | // java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module. |
| 2420 | // |
| 2421 | // By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were |
| 2422 | // compiled against an Android classpath. |
| 2423 | // |
| 2424 | // Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one |
| 2425 | // for host modules. |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2426 | func ImportFactory() android.Module { |
| 2427 | module := &Import{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 2428 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2429 | module.AddProperties( |
| 2430 | &module.properties, |
| 2431 | &module.dexer.dexProperties, |
Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame] | 2432 | &module.importDexpreoptProperties, |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2433 | ) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2434 | |
Paul Duffin | 71b33cc | 2021-06-23 11:39:47 +0100 | [diff] [blame] | 2435 | module.initModuleAndImport(module) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2436 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 2437 | module.dexProperties.Optimize.EnabledByDefault = false |
| 2438 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2439 | android.InitPrebuiltModule(module, &module.properties.Jars) |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2440 | android.InitApexModule(module) |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 2441 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 2442 | InitJavaModule(module, android.HostAndDeviceSupported) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 2443 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2446 | // java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host |
| 2447 | // module. |
| 2448 | // |
| 2449 | // A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were |
| 2450 | // compiled against a host bootclasspath. |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2451 | func ImportFactoryHost() android.Module { |
| 2452 | module := &Import{} |
| 2453 | |
| 2454 | module.AddProperties(&module.properties) |
| 2455 | |
| 2456 | android.InitPrebuiltModule(module, &module.properties.Jars) |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2457 | android.InitApexModule(module) |
Sam Delmerico | 5f83b49 | 2022-02-28 18:50:56 +0000 | [diff] [blame] | 2458 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 2459 | InitJavaModule(module, android.HostSupported) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 2460 | return module |
| 2461 | } |
| 2462 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2463 | // dex_import module |
| 2464 | |
| 2465 | type DexImportProperties struct { |
Colin Cross | 5cfc70d | 2019-07-15 13:36:55 -0700 | [diff] [blame] | 2466 | Jars []string `android:"path"` |
Jiyong Park | 4c4c024 | 2019-10-21 14:53:15 +0900 | [diff] [blame] | 2467 | |
| 2468 | // set the name of the output |
| 2469 | Stem *string |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | type DexImport struct { |
| 2473 | android.ModuleBase |
| 2474 | android.DefaultableModuleBase |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2475 | android.ApexModuleBase |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2476 | prebuilt android.Prebuilt |
| 2477 | |
| 2478 | properties DexImportProperties |
| 2479 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2480 | dexJarFile OptionalDexJarPath |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2481 | |
| 2482 | dexpreopter |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 2483 | |
| 2484 | hideApexVariantFromMake bool |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | func (j *DexImport) Prebuilt() *android.Prebuilt { |
| 2488 | return &j.prebuilt |
| 2489 | } |
| 2490 | |
| 2491 | func (j *DexImport) PrebuiltSrcs() []string { |
| 2492 | return j.properties.Jars |
| 2493 | } |
| 2494 | |
| 2495 | func (j *DexImport) Name() string { |
| 2496 | return j.prebuilt.Name(j.ModuleBase.Name()) |
| 2497 | } |
| 2498 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 2499 | func (j *DexImport) Stem() string { |
| 2500 | return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name()) |
| 2501 | } |
| 2502 | |
Jiyong Park | 77acec6 | 2020-06-01 21:39:15 +0900 | [diff] [blame] | 2503 | func (a *DexImport) JacocoReportClassesFile() android.Path { |
| 2504 | return nil |
| 2505 | } |
| 2506 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 2507 | func (a *DexImport) LintDepSets() LintDepSets { |
| 2508 | return LintDepSets{} |
| 2509 | } |
| 2510 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 2511 | func (j *DexImport) IsInstallable() bool { |
| 2512 | return true |
| 2513 | } |
| 2514 | |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 2515 | func (j *DexImport) getStrictUpdatabilityLinting() bool { |
| 2516 | return false |
| 2517 | } |
| 2518 | |
| 2519 | func (j *DexImport) setStrictUpdatabilityLinting(bool) { |
| 2520 | } |
| 2521 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2522 | func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 2523 | if len(j.properties.Jars) != 1 { |
| 2524 | ctx.PropertyErrorf("jars", "exactly one jar must be provided") |
| 2525 | } |
| 2526 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 2527 | apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 2528 | if !apexInfo.IsForPlatform() { |
| 2529 | j.hideApexVariantFromMake = true |
| 2530 | } |
| 2531 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 2532 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath( |
| 2533 | ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2534 | j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter) |
| 2535 | |
| 2536 | inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars") |
| 2537 | dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar") |
| 2538 | |
| 2539 | if j.dexpreopter.uncompressedDex { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 2540 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2541 | |
| 2542 | temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned") |
| 2543 | rule.Temporary(temporary) |
| 2544 | |
| 2545 | // use zip2zip to uncompress classes*.dex files |
| 2546 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 2547 | BuiltTool("zip2zip"). |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2548 | FlagWithInput("-i ", inputJar). |
| 2549 | FlagWithOutput("-o ", temporary). |
| 2550 | FlagWithArg("-0 ", "'classes*.dex'") |
| 2551 | |
| 2552 | // use zipalign to align uncompressed classes*.dex files |
| 2553 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 2554 | BuiltTool("zipalign"). |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2555 | Flag("-f"). |
| 2556 | Text("4"). |
| 2557 | Input(temporary). |
| 2558 | Output(dexOutputFile) |
| 2559 | |
| 2560 | rule.DeleteTemporaryFiles() |
| 2561 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 2562 | rule.Build("uncompress_dex", "uncompress dex") |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2563 | } else { |
| 2564 | ctx.Build(pctx, android.BuildParams{ |
| 2565 | Rule: android.Cp, |
| 2566 | Input: inputJar, |
| 2567 | Output: dexOutputFile, |
| 2568 | }) |
| 2569 | } |
| 2570 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2571 | j.dexJarFile = makeDexJarPathFromPath(dexOutputFile) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2572 | |
Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 2573 | j.dexpreopt(ctx, dexOutputFile) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2574 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 2575 | if apexInfo.IsForPlatform() { |
Jiyong Park | 01bca75 | 2020-06-08 19:24:09 +0900 | [diff] [blame] | 2576 | ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), |
| 2577 | j.Stem()+".jar", dexOutputFile) |
| 2578 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2581 | func (j *DexImport) DexJarBuildPath() OptionalDexJarPath { |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2582 | return j.dexJarFile |
| 2583 | } |
| 2584 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 2585 | var _ android.ApexModule = (*DexImport)(nil) |
| 2586 | |
| 2587 | // Implements android.ApexModule |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 2588 | func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, |
| 2589 | sdkVersion android.ApiLevel) error { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 2590 | // we don't check prebuilt modules for sdk_version |
| 2591 | return nil |
| 2592 | } |
| 2593 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2594 | // dex_import imports a `.jar` file containing classes.dex files. |
| 2595 | // |
| 2596 | // A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed |
| 2597 | // to the device. |
| 2598 | func DexImportFactory() android.Module { |
| 2599 | module := &DexImport{} |
| 2600 | |
| 2601 | module.AddProperties(&module.properties) |
| 2602 | |
| 2603 | android.InitPrebuiltModule(module, &module.properties.Jars) |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2604 | android.InitApexModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 2605 | InitJavaModule(module, android.DeviceSupported) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2606 | return module |
| 2607 | } |
| 2608 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2609 | // Defaults |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2610 | type Defaults struct { |
| 2611 | android.ModuleBase |
| 2612 | android.DefaultsModuleBase |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2613 | android.ApexModuleBase |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2614 | } |
| 2615 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2616 | // java_defaults provides a set of properties that can be inherited by other java or android modules. |
| 2617 | // |
| 2618 | // A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each |
| 2619 | // property in the defaults module that exists in the depending module will be prepended to the depending module's |
| 2620 | // value for that property. |
| 2621 | // |
| 2622 | // Example: |
| 2623 | // |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2624 | // java_defaults { |
| 2625 | // name: "example_defaults", |
| 2626 | // srcs: ["common/**/*.java"], |
| 2627 | // javacflags: ["-Xlint:all"], |
| 2628 | // aaptflags: ["--auto-add-overlay"], |
| 2629 | // } |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2630 | // |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2631 | // java_library { |
| 2632 | // name: "example", |
| 2633 | // defaults: ["example_defaults"], |
| 2634 | // srcs: ["example/**/*.java"], |
| 2635 | // } |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2636 | // |
| 2637 | // is functionally identical to: |
| 2638 | // |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2639 | // java_library { |
| 2640 | // name: "example", |
| 2641 | // srcs: [ |
| 2642 | // "common/**/*.java", |
| 2643 | // "example/**/*.java", |
| 2644 | // ], |
| 2645 | // javacflags: ["-Xlint:all"], |
| 2646 | // } |
Paul Duffin | 4735766 | 2019-12-05 14:07:14 +0000 | [diff] [blame] | 2647 | func DefaultsFactory() android.Module { |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2648 | module := &Defaults{} |
| 2649 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2650 | module.AddProperties( |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 2651 | &CommonProperties{}, |
| 2652 | &DeviceProperties{}, |
Jooyung Han | 01d80d8 | 2022-01-08 12:16:32 +0900 | [diff] [blame] | 2653 | &OverridableDeviceProperties{}, |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 2654 | &DexProperties{}, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2655 | &DexpreoptProperties{}, |
Dan Willemsen | 6424d17 | 2018-03-08 13:27:59 -0800 | [diff] [blame] | 2656 | &android.ProtoProperties{}, |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 2657 | &aaptProperties{}, |
| 2658 | &androidLibraryProperties{}, |
| 2659 | &appProperties{}, |
| 2660 | &appTestProperties{}, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 2661 | &overridableAppProperties{}, |
Kun Niu | bd0fd20 | 2023-05-23 17:51:44 +0000 | [diff] [blame] | 2662 | &hostTestProperties{}, |
Roland Levillain | b5b0ff3 | 2020-02-04 15:45:49 +0000 | [diff] [blame] | 2663 | &testProperties{}, |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 2664 | &ImportProperties{}, |
| 2665 | &AARImportProperties{}, |
| 2666 | &sdkLibraryProperties{}, |
Paul Duffin | 1b1e806 | 2020-05-08 13:44:43 +0100 | [diff] [blame] | 2667 | &commonToSdkLibraryAndImportProperties{}, |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2668 | &DexImportProperties{}, |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 2669 | &android.ApexProperties{}, |
Jaewoong Jung | bf13546 | 2020-04-26 15:10:51 -0700 | [diff] [blame] | 2670 | &RuntimeResourceOverlayProperties{}, |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 2671 | &LintProperties{}, |
Colin Cross | cbce0b0 | 2021-02-09 10:38:30 -0800 | [diff] [blame] | 2672 | &appTestHelperAppProperties{}, |
Jihoon Kang | 1c51f50 | 2023-01-09 23:42:40 +0000 | [diff] [blame] | 2673 | &JavaApiLibraryProperties{}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2674 | ) |
| 2675 | |
| 2676 | android.InitDefaultsModule(module) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2677 | return module |
| 2678 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 2679 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 2680 | func kytheExtractJavaFactory() android.Singleton { |
| 2681 | return &kytheExtractJavaSingleton{} |
| 2682 | } |
| 2683 | |
| 2684 | type kytheExtractJavaSingleton struct { |
| 2685 | } |
| 2686 | |
| 2687 | func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 2688 | var xrefTargets android.Paths |
| 2689 | ctx.VisitAllModules(func(module android.Module) { |
| 2690 | if javaModule, ok := module.(xref); ok { |
| 2691 | xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...) |
| 2692 | } |
| 2693 | }) |
| 2694 | // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets |
| 2695 | if len(xrefTargets) > 0 { |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 2696 | ctx.Phony("xref_java", xrefTargets...) |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 2697 | } |
| 2698 | } |
| 2699 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 2700 | var Bool = proptools.Bool |
Colin Cross | 38b40df | 2018-04-10 16:14:46 -0700 | [diff] [blame] | 2701 | var BoolDefault = proptools.BoolDefault |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 2702 | var String = proptools.String |
Sam Delmerico | 1717b3b | 2023-07-18 15:07:24 -0400 | [diff] [blame] | 2703 | var inList = android.InList[string] |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2704 | |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2705 | // Add class loader context (CLC) of a given dependency to the current CLC. |
| 2706 | func addCLCFromDep(ctx android.ModuleContext, depModule android.Module, |
| 2707 | clcMap dexpreopt.ClassLoaderContextMap) { |
| 2708 | |
| 2709 | dep, ok := depModule.(UsesLibraryDependency) |
| 2710 | if !ok { |
| 2711 | return |
| 2712 | } |
| 2713 | |
Ulya Trafimovich | 840efb6 | 2021-07-15 14:34:40 +0100 | [diff] [blame] | 2714 | depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule)) |
| 2715 | |
| 2716 | var sdkLib *string |
| 2717 | if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() { |
| 2718 | // A shared SDK library. This should be added as a top-level CLC element. |
| 2719 | sdkLib = &depName |
| 2720 | } else if ulib, ok := depModule.(ProvidesUsesLib); ok { |
| 2721 | // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib` |
| 2722 | // property. This should be handled in the same way as a shared SDK library. |
| 2723 | sdkLib = ulib.ProvidesUsesLib() |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2724 | } |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2725 | |
| 2726 | depTag := ctx.OtherModuleDependencyTag(depModule) |
Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 2727 | if IsLibDepTag(depTag) { |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2728 | // Ok, propagate <uses-library> through non-static library dependencies. |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 2729 | } else if tag, ok := depTag.(usesLibraryDependencyTag); ok && tag.sdkVersion == dexpreopt.AnySdkVersion { |
| 2730 | // Ok, propagate <uses-library> through non-compatibility <uses-library> dependencies. |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2731 | } else if depTag == staticLibTag { |
| 2732 | // Propagate <uses-library> through static library dependencies, unless it is a component |
| 2733 | // library (such as stubs). Component libraries have a dependency on their SDK library, |
| 2734 | // which should not be pulled just because of a static component library. |
Ulya Trafimovich | 840efb6 | 2021-07-15 14:34:40 +0100 | [diff] [blame] | 2735 | if sdkLib != nil { |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2736 | return |
| 2737 | } |
| 2738 | } else { |
| 2739 | // Don't propagate <uses-library> for other dependency tags. |
| 2740 | return |
| 2741 | } |
| 2742 | |
Ulya Trafimovich | 840efb6 | 2021-07-15 14:34:40 +0100 | [diff] [blame] | 2743 | // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree, |
| 2744 | // and its CLC should be added as subtree of that node. Otherwise the library is not a |
| 2745 | // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies |
| 2746 | // from its CLC should be added to the current CLC. |
| 2747 | if sdkLib != nil { |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 2748 | clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2749 | dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts()) |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2750 | } else { |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2751 | clcMap.AddContextMap(dep.ClassLoaderContexts(), depName) |
| 2752 | } |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2753 | } |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2754 | |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2755 | type javaResourcesAttributes struct { |
| 2756 | Resources bazel.LabelListAttribute |
| 2757 | Resource_strip_prefix *string |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2758 | Additional_resources bazel.LabelListAttribute |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2761 | func (m *Library) getResourceFilegroupStripPrefix(ctx android.TopDownMutatorContext, resourceFilegroup string) (*string, bool) { |
| 2762 | if otherM, ok := ctx.ModuleFromName(resourceFilegroup); ok { |
Alix | b29a3cd | 2023-05-09 20:37:49 +0000 | [diff] [blame] | 2763 | if fg, isFilegroup := otherM.(android.FileGroupPath); isFilegroup { |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2764 | return proptools.StringPtr(filepath.Join(ctx.OtherModuleDir(otherM), fg.GetPath(ctx))), true |
Alix | b29a3cd | 2023-05-09 20:37:49 +0000 | [diff] [blame] | 2765 | } |
| 2766 | } |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2767 | return proptools.StringPtr(""), false |
Alix | b29a3cd | 2023-05-09 20:37:49 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2770 | func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorContext) *javaResourcesAttributes { |
| 2771 | var resources bazel.LabelList |
| 2772 | var resourceStripPrefix *string |
| 2773 | |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2774 | additionalJavaResourcesMap := make(map[string]*javaResourcesAttributes) |
Romain Jobredeaux | d5fe133 | 2023-05-04 14:54:45 -0400 | [diff] [blame] | 2775 | |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2776 | if m.properties.Java_resources != nil { |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2777 | for _, res := range m.properties.Java_resources { |
| 2778 | if prefix, isFilegroup := m.getResourceFilegroupStripPrefix(ctx, res); isFilegroup { |
| 2779 | otherM, _ := ctx.ModuleFromName(res) |
| 2780 | resourcesTargetName := ctx.ModuleName() + "_filegroup_resources_" + otherM.Name() |
| 2781 | additionalJavaResourcesMap[resourcesTargetName] = &javaResourcesAttributes{ |
| 2782 | Resources: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, []string{res})), |
| 2783 | Resource_strip_prefix: prefix, |
| 2784 | } |
| 2785 | } else { |
| 2786 | resources.Append(android.BazelLabelForModuleSrc(ctx, []string{res})) |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | if !resources.IsEmpty() { |
Alix | b29a3cd | 2023-05-09 20:37:49 +0000 | [diff] [blame] | 2791 | resourceStripPrefix = proptools.StringPtr(ctx.ModuleDir()) |
| 2792 | } |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Paul Duffin | 7ed6ff8 | 2022-11-21 10:57:30 +0000 | [diff] [blame] | 2795 | //TODO(b/179889880) handle case where glob includes files outside package |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2796 | resDeps := ResourceDirsToFiles( |
| 2797 | ctx, |
| 2798 | m.properties.Java_resource_dirs, |
| 2799 | m.properties.Exclude_java_resource_dirs, |
| 2800 | m.properties.Exclude_java_resources, |
| 2801 | ) |
| 2802 | |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2803 | for _, resDep := range resDeps { |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2804 | dir, files := resDep.dir, resDep.files |
| 2805 | |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2806 | // Bazel includes the relative path from the WORKSPACE root when placing the resource |
| 2807 | // inside the JAR file, so we need to remove that prefix |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2808 | prefix := proptools.StringPtr(dir.String()) |
| 2809 | resourcesTargetName := ctx.ModuleName() + "_resource_dir_" + dir.String() |
| 2810 | additionalJavaResourcesMap[resourcesTargetName] = &javaResourcesAttributes{ |
| 2811 | Resources: bazel.MakeLabelListAttribute(bazel.MakeLabelList(android.RootToModuleRelativePaths(ctx, files))), |
| 2812 | Resource_strip_prefix: prefix, |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2816 | var additionalResourceLabels bazel.LabelList |
| 2817 | if len(additionalJavaResourcesMap) > 0 { |
| 2818 | var additionalResources []string |
| 2819 | for resName, _ := range additionalJavaResourcesMap { |
| 2820 | additionalResources = append(additionalResources, resName) |
| 2821 | } |
| 2822 | sort.Strings(additionalResources) |
| 2823 | |
| 2824 | for i, resName := range additionalResources { |
| 2825 | resAttr := additionalJavaResourcesMap[resName] |
| 2826 | if resourceStripPrefix == nil && i == 0 { |
| 2827 | resourceStripPrefix = resAttr.Resource_strip_prefix |
| 2828 | resources = resAttr.Resources.Value |
| 2829 | } else { |
| 2830 | ctx.CreateBazelTargetModule( |
| 2831 | bazel.BazelTargetModuleProperties{ |
| 2832 | Rule_class: "java_resources", |
| 2833 | Bzl_load_location: "//build/bazel/rules/java:java_resources.bzl", |
| 2834 | }, |
| 2835 | android.CommonAttributes{Name: resName}, |
| 2836 | resAttr, |
| 2837 | ) |
| 2838 | additionalResourceLabels.Append(android.BazelLabelForModuleSrc(ctx, []string{resName})) |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | } |
| 2843 | |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2844 | return &javaResourcesAttributes{ |
| 2845 | Resources: bazel.MakeLabelListAttribute(resources), |
| 2846 | Resource_strip_prefix: resourceStripPrefix, |
Alix | 289e9c6 | 2023-08-16 15:06:31 +0000 | [diff] [blame] | 2847 | Additional_resources: bazel.MakeLabelListAttribute(additionalResourceLabels), |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2851 | type javaCommonAttributes struct { |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2852 | *javaResourcesAttributes |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 2853 | *kotlinAttributes |
Alix | b1e5c6a | 2023-06-20 21:00:40 +0000 | [diff] [blame] | 2854 | Srcs bazel.LabelListAttribute |
| 2855 | Plugins bazel.LabelListAttribute |
| 2856 | Javacopts bazel.StringListAttribute |
| 2857 | Sdk_version bazel.StringAttribute |
| 2858 | Java_version bazel.StringAttribute |
| 2859 | Errorprone_force_enable bazel.BoolAttribute |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2860 | } |
| 2861 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2862 | type javaDependencyLabels struct { |
| 2863 | // Dependencies which DO NOT contribute to the API visible to upstream dependencies. |
| 2864 | Deps bazel.LabelListAttribute |
| 2865 | // Dependencies which DO contribute to the API visible to upstream dependencies. |
| 2866 | StaticDeps bazel.LabelListAttribute |
| 2867 | } |
| 2868 | |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2869 | type eventLogTagsAttributes struct { |
| 2870 | Srcs bazel.LabelListAttribute |
| 2871 | } |
| 2872 | |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2873 | type aidlLibraryAttributes struct { |
| 2874 | Srcs bazel.LabelListAttribute |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 2875 | Tags bazel.StringListAttribute |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | type javaAidlLibraryAttributes struct { |
| 2879 | Deps bazel.LabelListAttribute |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 2880 | Tags bazel.StringListAttribute |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2881 | } |
| 2882 | |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 2883 | // bp2BuildJavaInfo has information needed for the conversion of java*_modules |
| 2884 | // that is needed bor Bp2Build conversion but that requires different handling |
| 2885 | // depending on the module type. |
| 2886 | type bp2BuildJavaInfo struct { |
| 2887 | // separates dependencies into dynamic dependencies and static dependencies. |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 2888 | DepLabels *javaDependencyLabels |
| 2889 | hasKotlin bool |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 2892 | func javaXsdTargetName(xsd android.XsdConfigBp2buildTargets) string { |
| 2893 | return xsd.JavaBp2buildTargetName() |
Spandan Das | 370f13c | 2023-05-31 22:03:33 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 2896 | // convertLibraryAttrsBp2Build returns a javaCommonAttributes struct with |
| 2897 | // converted attributes shared across java_* modules and a bp2BuildJavaInfo struct |
| 2898 | // which has other non-attribute information needed for bp2build conversion |
| 2899 | // that needs different handling depending on the module types, and thus needs |
| 2900 | // to be returned to the calling function. |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 2901 | func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) (*javaCommonAttributes, *bp2BuildJavaInfo, bool) { |
Sam Delmerico | e91d030 | 2022-02-23 15:28:33 +0000 | [diff] [blame] | 2902 | var srcs bazel.LabelListAttribute |
Alix | 495cc8a | 2023-01-12 18:20:32 +0000 | [diff] [blame] | 2903 | var deps bazel.LabelListAttribute |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 2904 | var staticDeps bazel.LabelListAttribute |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2905 | |
Liz Kammer | 0291440 | 2023-08-07 13:38:18 -0400 | [diff] [blame] | 2906 | if proptools.String(m.deviceProperties.Sdk_version) == "" && m.DeviceSupported() { |
Liz Kammer | 65942c8 | 2023-09-07 15:59:42 -0400 | [diff] [blame^] | 2907 | // TODO(b/297356704): handle platform apis in bp2build |
Liz Kammer | 0291440 | 2023-08-07 13:38:18 -0400 | [diff] [blame] | 2908 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "sdk_version unset") |
| 2909 | return &javaCommonAttributes{}, &bp2BuildJavaInfo{}, false |
Liz Kammer | 65942c8 | 2023-09-07 15:59:42 -0400 | [diff] [blame^] | 2910 | } else if proptools.String(m.deviceProperties.Sdk_version) == "core_platform" { |
| 2911 | // TODO(b/297356582): handle core_platform in bp2build |
| 2912 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "sdk_version core_platform") |
| 2913 | return &javaCommonAttributes{}, &bp2BuildJavaInfo{}, false |
Liz Kammer | 0291440 | 2023-08-07 13:38:18 -0400 | [diff] [blame] | 2914 | } |
| 2915 | |
Sam Delmerico | e91d030 | 2022-02-23 15:28:33 +0000 | [diff] [blame] | 2916 | archVariantProps := m.GetArchVariantProperties(ctx, &CommonProperties{}) |
| 2917 | for axis, configToProps := range archVariantProps { |
| 2918 | for config, _props := range configToProps { |
| 2919 | if archProps, ok := _props.(*CommonProperties); ok { |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 2920 | archSrcs := android.BazelLabelForModuleSrcExcludes(ctx, archProps.Srcs, archProps.Exclude_srcs) |
Sam Delmerico | e91d030 | 2022-02-23 15:28:33 +0000 | [diff] [blame] | 2921 | srcs.SetSelectValue(axis, config, archSrcs) |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 2922 | if archProps.Jarjar_rules != nil { |
| 2923 | ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "jarjar_rules") |
| 2924 | return &javaCommonAttributes{}, &bp2BuildJavaInfo{}, false |
| 2925 | } |
Sam Delmerico | e91d030 | 2022-02-23 15:28:33 +0000 | [diff] [blame] | 2926 | } |
| 2927 | } |
| 2928 | } |
Romain Jobredeaux | 3eaf747 | 2023-03-08 16:34:17 -0500 | [diff] [blame] | 2929 | srcs.ResolveExcludes() |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2930 | |
| 2931 | javaSrcPartition := "java" |
| 2932 | protoSrcPartition := "proto" |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 2933 | xsdSrcPartition := "xsd" |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2934 | logtagSrcPartition := "logtag" |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2935 | aidlSrcPartition := "aidl" |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 2936 | kotlinPartition := "kotlin" |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2937 | srcPartitions := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{ |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2938 | javaSrcPartition: bazel.LabelPartition{Extensions: []string{".java"}, Keep_remainder: true}, |
| 2939 | logtagSrcPartition: bazel.LabelPartition{Extensions: []string{".logtags", ".logtag"}}, |
| 2940 | protoSrcPartition: android.ProtoSrcLabelPartition, |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2941 | aidlSrcPartition: android.AidlSrcLabelPartition, |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 2942 | xsdSrcPartition: bazel.LabelPartition{LabelMapper: android.XsdLabelMapper(javaXsdTargetName)}, |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 2943 | kotlinPartition: bazel.LabelPartition{Extensions: []string{".kt"}}, |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2944 | }) |
| 2945 | |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2946 | javaSrcs := srcPartitions[javaSrcPartition] |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 2947 | kotlinSrcs := srcPartitions[kotlinPartition] |
| 2948 | javaSrcs.Append(kotlinSrcs) |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2949 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 2950 | staticDeps.Append(srcPartitions[xsdSrcPartition]) |
| 2951 | |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2952 | if !srcPartitions[logtagSrcPartition].IsEmpty() { |
| 2953 | logtagsLibName := m.Name() + "_logtags" |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2954 | ctx.CreateBazelTargetModule( |
| 2955 | bazel.BazelTargetModuleProperties{ |
| 2956 | Rule_class: "event_log_tags", |
Sam Delmerico | 48af73b | 2022-04-05 14:03:11 +0000 | [diff] [blame] | 2957 | Bzl_load_location: "//build/bazel/rules/java:event_log_tags.bzl", |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2958 | }, |
| 2959 | android.CommonAttributes{Name: logtagsLibName}, |
| 2960 | &eventLogTagsAttributes{ |
| 2961 | Srcs: srcPartitions[logtagSrcPartition], |
| 2962 | }, |
| 2963 | ) |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2964 | |
| 2965 | logtagsSrcs := bazel.MakeLabelList([]bazel.Label{{Label: ":" + logtagsLibName}}) |
| 2966 | javaSrcs.Append(bazel.MakeLabelListAttribute(logtagsSrcs)) |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2967 | } |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2968 | |
| 2969 | if !srcPartitions[aidlSrcPartition].IsEmpty() { |
| 2970 | aidlLibs, aidlSrcs := srcPartitions[aidlSrcPartition].Partition(func(src bazel.Label) bool { |
| 2971 | return android.IsConvertedToAidlLibrary(ctx, src.OriginalModuleName) |
| 2972 | }) |
| 2973 | |
Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 2974 | apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx, ctx.Module()) |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 2975 | |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2976 | if !aidlSrcs.IsEmpty() { |
| 2977 | aidlLibName := m.Name() + "_aidl_library" |
| 2978 | ctx.CreateBazelTargetModule( |
| 2979 | bazel.BazelTargetModuleProperties{ |
| 2980 | Rule_class: "aidl_library", |
Sam Delmerico | e55bf08 | 2023-03-31 09:47:28 -0400 | [diff] [blame] | 2981 | Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl", |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2982 | }, |
| 2983 | android.CommonAttributes{Name: aidlLibName}, |
| 2984 | &aidlLibraryAttributes{ |
| 2985 | Srcs: aidlSrcs, |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 2986 | Tags: apexAvailableTags, |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2987 | }, |
| 2988 | ) |
| 2989 | aidlLibs.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + aidlLibName}}) |
| 2990 | } |
| 2991 | |
| 2992 | javaAidlLibName := m.Name() + "_java_aidl_library" |
| 2993 | ctx.CreateBazelTargetModule( |
| 2994 | bazel.BazelTargetModuleProperties{ |
| 2995 | Rule_class: "java_aidl_library", |
Sam Delmerico | e55bf08 | 2023-03-31 09:47:28 -0400 | [diff] [blame] | 2996 | Bzl_load_location: "//build/bazel/rules/java:java_aidl_library.bzl", |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 2997 | }, |
| 2998 | android.CommonAttributes{Name: javaAidlLibName}, |
| 2999 | &javaAidlLibraryAttributes{ |
| 3000 | Deps: aidlLibs, |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 3001 | Tags: apexAvailableTags, |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 3002 | }, |
| 3003 | ) |
| 3004 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 3005 | staticDeps.Append(bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + javaAidlLibName})) |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 3006 | } |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 3007 | |
Alix | b1e5c6a | 2023-06-20 21:00:40 +0000 | [diff] [blame] | 3008 | var javacopts bazel.StringListAttribute //[]string |
| 3009 | plugins := bazel.MakeLabelListAttribute( |
| 3010 | android.BazelLabelForModuleDeps(ctx, m.properties.Plugins), |
| 3011 | ) |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame] | 3012 | if m.properties.Javacflags != nil { |
Alix | b1e5c6a | 2023-06-20 21:00:40 +0000 | [diff] [blame] | 3013 | javacopts = bazel.MakeStringListAttribute(m.properties.Javacflags) |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame] | 3014 | } |
Vinh Tran | 3ac6daf | 2022-04-22 19:09:58 -0400 | [diff] [blame] | 3015 | |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame] | 3016 | epEnabled := m.properties.Errorprone.Enabled |
Alix | b1e5c6a | 2023-06-20 21:00:40 +0000 | [diff] [blame] | 3017 | epJavacflags := m.properties.Errorprone.Javacflags |
| 3018 | var errorproneForceEnable bazel.BoolAttribute |
| 3019 | if epEnabled == nil { |
| 3020 | //TODO(b/227504307) add configuration that depends on RUN_ERROR_PRONE environment variable |
| 3021 | } else if *epEnabled { |
| 3022 | plugins.Append(bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.properties.Errorprone.Extra_check_modules))) |
| 3023 | javacopts.Append(bazel.MakeStringListAttribute(epJavacflags)) |
| 3024 | errorproneForceEnable.Value = epEnabled |
| 3025 | } else { |
| 3026 | javacopts.Append(bazel.MakeStringListAttribute([]string{"-XepDisableAllChecks"})) |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3029 | commonAttrs := &javaCommonAttributes{ |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 3030 | Srcs: javaSrcs, |
| 3031 | javaResourcesAttributes: m.convertJavaResourcesAttributes(ctx), |
Alix | b1e5c6a | 2023-06-20 21:00:40 +0000 | [diff] [blame] | 3032 | Plugins: plugins, |
| 3033 | Javacopts: javacopts, |
| 3034 | Java_version: bazel.StringAttribute{Value: m.properties.Java_version}, |
| 3035 | Sdk_version: bazel.StringAttribute{Value: m.deviceProperties.Sdk_version}, |
| 3036 | Errorprone_force_enable: errorproneForceEnable, |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3037 | } |
| 3038 | |
Alix | 495cc8a | 2023-01-12 18:20:32 +0000 | [diff] [blame] | 3039 | for axis, configToProps := range archVariantProps { |
| 3040 | for config, _props := range configToProps { |
| 3041 | if archProps, ok := _props.(*CommonProperties); ok { |
| 3042 | var libLabels []bazel.Label |
| 3043 | for _, d := range archProps.Libs { |
| 3044 | neverlinkLabel := android.BazelLabelForModuleDepSingle(ctx, d) |
| 3045 | neverlinkLabel.Label = neverlinkLabel.Label + "-neverlink" |
| 3046 | libLabels = append(libLabels, neverlinkLabel) |
| 3047 | } |
| 3048 | deps.SetSelectValue(axis, config, bazel.MakeLabelList(libLabels)) |
| 3049 | } |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 3050 | } |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3051 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3052 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3053 | protoDepLabel := bp2buildProto(ctx, &m.Module, srcPartitions[protoSrcPartition]) |
| 3054 | // Soong does not differentiate between a java_library and the Bazel equivalent of |
| 3055 | // a java_proto_library + proto_library pair. Instead, in Soong proto sources are |
| 3056 | // listed directly in the srcs of a java_library, and the classes produced |
| 3057 | // by protoc are included directly in the resulting JAR. Thus upstream dependencies |
| 3058 | // that depend on a java_library with proto sources can link directly to the protobuf API, |
| 3059 | // and so this should be a static dependency. |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 3060 | if protoDepLabel != nil { |
| 3061 | staticDeps.Append(bazel.MakeSingleLabelListAttribute(*protoDepLabel)) |
| 3062 | } |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 3063 | |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 3064 | depLabels := &javaDependencyLabels{} |
Alix | 495cc8a | 2023-01-12 18:20:32 +0000 | [diff] [blame] | 3065 | depLabels.Deps = deps |
Romain Jobredeaux | eb711b9 | 2023-04-03 22:53:34 -0400 | [diff] [blame] | 3066 | |
| 3067 | for axis, configToProps := range archVariantProps { |
| 3068 | for config, _props := range configToProps { |
| 3069 | if archProps, ok := _props.(*CommonProperties); ok { |
| 3070 | archStaticLibs := android.BazelLabelForModuleDeps( |
| 3071 | ctx, |
| 3072 | android.LastUniqueStrings(android.CopyOf(archProps.Static_libs))) |
| 3073 | depLabels.StaticDeps.SetSelectValue(axis, config, archStaticLibs) |
| 3074 | } |
| 3075 | } |
| 3076 | } |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 3077 | depLabels.StaticDeps.Append(staticDeps) |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 3078 | |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 3079 | hasKotlin := !kotlinSrcs.IsEmpty() |
Alix | f848bf8 | 2023-03-06 19:43:55 +0000 | [diff] [blame] | 3080 | commonAttrs.kotlinAttributes = &kotlinAttributes{ |
| 3081 | Kotlincflags: &m.properties.Kotlincflags, |
| 3082 | } |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 3083 | if len(m.properties.Common_srcs) != 0 { |
| 3084 | hasKotlin = true |
Alix | f848bf8 | 2023-03-06 19:43:55 +0000 | [diff] [blame] | 3085 | commonAttrs.kotlinAttributes.Common_srcs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Common_srcs)) |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 3088 | bp2BuildInfo := &bp2BuildJavaInfo{ |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 3089 | DepLabels: depLabels, |
| 3090 | hasKotlin: hasKotlin, |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 3093 | return commonAttrs, bp2BuildInfo, true |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
| 3096 | type javaLibraryAttributes struct { |
| 3097 | *javaCommonAttributes |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 3098 | Deps bazel.LabelListAttribute |
| 3099 | Exports bazel.LabelListAttribute |
| 3100 | Neverlink bazel.BoolAttribute |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 3103 | type kotlinAttributes struct { |
Alix | f848bf8 | 2023-03-06 19:43:55 +0000 | [diff] [blame] | 3104 | Common_srcs bazel.LabelListAttribute |
| 3105 | Kotlincflags *[]string |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3108 | func ktJvmLibraryBazelTargetModuleProperties() bazel.BazelTargetModuleProperties { |
| 3109 | return bazel.BazelTargetModuleProperties{ |
| 3110 | Rule_class: "kt_jvm_library", |
Alix | a28acb5 | 2023-05-08 15:41:01 +0000 | [diff] [blame] | 3111 | Bzl_load_location: "//build/bazel/rules/kotlin:kt_jvm_library.bzl", |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | func javaLibraryBazelTargetModuleProperties() bazel.BazelTargetModuleProperties { |
| 3116 | return bazel.BazelTargetModuleProperties{ |
| 3117 | Rule_class: "java_library", |
Alix | 83a8bc4 | 2023-05-04 18:03:23 +0000 | [diff] [blame] | 3118 | Bzl_load_location: "//build/bazel/rules/java:library.bzl", |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3119 | } |
| 3120 | } |
| 3121 | |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 3122 | func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 3123 | commonAttrs, bp2BuildInfo, supported := m.convertLibraryAttrsBp2Build(ctx) |
| 3124 | if !supported { |
| 3125 | return |
| 3126 | } |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 3127 | depLabels := bp2BuildInfo.DepLabels |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3128 | |
| 3129 | deps := depLabels.Deps |
| 3130 | if !commonAttrs.Srcs.IsEmpty() { |
| 3131 | deps.Append(depLabels.StaticDeps) // we should only append these if there are sources to use them |
Sam Delmerico | 97bd127 | 2022-08-25 14:45:31 -0400 | [diff] [blame] | 3132 | } else if !deps.IsEmpty() { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3133 | ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.") |
| 3134 | } |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 3135 | var props bazel.BazelTargetModuleProperties |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3136 | attrs := &javaLibraryAttributes{ |
| 3137 | javaCommonAttributes: commonAttrs, |
| 3138 | Deps: deps, |
| 3139 | Exports: depLabels.StaticDeps, |
| 3140 | } |
Alix | 341484b | 2022-10-31 19:08:18 +0000 | [diff] [blame] | 3141 | name := m.Name() |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3142 | |
Alix | 75db784 | 2023-03-06 22:08:49 +0000 | [diff] [blame] | 3143 | if !bp2BuildInfo.hasKotlin { |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3144 | props = javaLibraryBazelTargetModuleProperties() |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 3145 | } else { |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3146 | props = ktJvmLibraryBazelTargetModuleProperties() |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3147 | } |
| 3148 | |
Alix | 266bbf2 | 2022-12-15 06:12:46 +0000 | [diff] [blame] | 3149 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) |
| 3150 | neverlinkProp := true |
| 3151 | neverLinkAttrs := &javaLibraryAttributes{ |
| 3152 | Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}), |
| 3153 | Neverlink: bazel.BoolAttribute{Value: &neverlinkProp}, |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 3154 | javaCommonAttributes: &javaCommonAttributes{ |
| 3155 | Sdk_version: bazel.StringAttribute{Value: m.deviceProperties.Sdk_version}, |
| 3156 | Java_version: bazel.StringAttribute{Value: m.properties.Java_version}, |
| 3157 | }, |
Alix | 266bbf2 | 2022-12-15 06:12:46 +0000 | [diff] [blame] | 3158 | } |
| 3159 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name + "-neverlink"}, neverLinkAttrs) |
| 3160 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | type javaBinaryHostAttributes struct { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3164 | *javaCommonAttributes |
| 3165 | Deps bazel.LabelListAttribute |
| 3166 | Runtime_deps bazel.LabelListAttribute |
| 3167 | Main_class string |
| 3168 | Jvm_flags bazel.StringListAttribute |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | // JavaBinaryHostBp2Build is for java_binary_host bp2build. |
| 3172 | func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) { |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 3173 | commonAttrs, bp2BuildInfo, supported := m.convertLibraryAttrsBp2Build(ctx) |
| 3174 | if !supported { |
| 3175 | return |
| 3176 | } |
Alix | 8062f4d | 2022-11-14 21:38:07 +0000 | [diff] [blame] | 3177 | depLabels := bp2BuildInfo.DepLabels |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3178 | |
| 3179 | deps := depLabels.Deps |
| 3180 | deps.Append(depLabels.StaticDeps) |
| 3181 | if m.binaryProperties.Jni_libs != nil { |
| 3182 | deps.Append(bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs))) |
| 3183 | } |
| 3184 | |
| 3185 | var runtimeDeps bazel.LabelListAttribute |
| 3186 | if commonAttrs.Srcs.IsEmpty() { |
| 3187 | // if there are no sources, then the dependencies can only be used at runtime |
| 3188 | runtimeDeps = deps |
| 3189 | deps = bazel.LabelListAttribute{} |
| 3190 | } |
| 3191 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3192 | mainClass := "" |
| 3193 | if m.binaryProperties.Main_class != nil { |
| 3194 | mainClass = *m.binaryProperties.Main_class |
| 3195 | } |
| 3196 | if m.properties.Manifest != nil { |
| 3197 | mainClassInManifest, err := android.GetMainClassInManifest(ctx.Config(), android.PathForModuleSrc(ctx, *m.properties.Manifest).String()) |
| 3198 | if err != nil { |
| 3199 | return |
| 3200 | } |
| 3201 | mainClass = mainClassInManifest |
| 3202 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 3203 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3204 | // Attribute jvm_flags |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 3205 | var jvmFlags bazel.StringListAttribute |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3206 | if m.binaryProperties.Jni_libs != nil { |
Spandan Das | 5dba0ec | 2023-06-01 02:48:09 +0000 | [diff] [blame] | 3207 | jniLibPackages := []string{} |
| 3208 | for _, jniLib := range m.binaryProperties.Jni_libs { |
| 3209 | if jniLibModule, exists := ctx.ModuleFromName(jniLib); exists { |
| 3210 | otherDir := ctx.OtherModuleDir(jniLibModule) |
| 3211 | jniLibPackages = append(jniLibPackages, filepath.Join(otherDir, jniLib)) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3212 | } |
| 3213 | } |
| 3214 | jniLibPaths := []string{} |
Spandan Das | 5dba0ec | 2023-06-01 02:48:09 +0000 | [diff] [blame] | 3215 | for _, jniLibPackage := range jniLibPackages { |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3216 | // See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH |
| 3217 | jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage) |
| 3218 | } |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 3219 | jvmFlags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")}) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | props := bazel.BazelTargetModuleProperties{ |
Alix | 82acaf5 | 2023-03-09 20:43:10 +0000 | [diff] [blame] | 3223 | Rule_class: "java_binary", |
Alix | 83a8bc4 | 2023-05-04 18:03:23 +0000 | [diff] [blame] | 3224 | Bzl_load_location: "@rules_java//java:defs.bzl", |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3225 | } |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 3226 | binAttrs := &javaBinaryHostAttributes{ |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 3227 | Runtime_deps: runtimeDeps, |
| 3228 | Main_class: mainClass, |
| 3229 | Jvm_flags: jvmFlags, |
| 3230 | } |
| 3231 | |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 3232 | if commonAttrs.Srcs.IsEmpty() { |
| 3233 | binAttrs.javaCommonAttributes = commonAttrs |
| 3234 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, binAttrs) |
| 3235 | return |
| 3236 | } |
| 3237 | |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3238 | libInfo := libraryCreationInfo{ |
Spandan Das | 5dba0ec | 2023-06-01 02:48:09 +0000 | [diff] [blame] | 3239 | deps: deps, |
| 3240 | attrs: commonAttrs, |
| 3241 | baseName: m.Name(), |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3242 | hasKotlin: bp2BuildInfo.hasKotlin, |
| 3243 | } |
| 3244 | libName := createLibraryTarget(ctx, libInfo) |
| 3245 | binAttrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + libName}}) |
| 3246 | |
| 3247 | // Create the BazelTargetModule. |
| 3248 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, binAttrs) |
| 3249 | } |
| 3250 | |
| 3251 | type javaTestHostAttributes struct { |
| 3252 | *javaCommonAttributes |
Zi Wang | 7873f61 | 2023-07-17 16:36:19 -0700 | [diff] [blame] | 3253 | Srcs bazel.LabelListAttribute |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3254 | Deps bazel.LabelListAttribute |
| 3255 | Runtime_deps bazel.LabelListAttribute |
| 3256 | } |
| 3257 | |
| 3258 | // javaTestHostBp2Build is for java_test_host bp2build. |
| 3259 | func javaTestHostBp2Build(ctx android.TopDownMutatorContext, m *TestHost) { |
Liz Kammer | 7f37586 | 2023-08-04 16:37:42 -0400 | [diff] [blame] | 3260 | commonAttrs, bp2BuildInfo, supported := m.convertLibraryAttrsBp2Build(ctx) |
| 3261 | if !supported { |
| 3262 | return |
| 3263 | } |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3264 | depLabels := bp2BuildInfo.DepLabels |
| 3265 | |
| 3266 | deps := depLabels.Deps |
| 3267 | deps.Append(depLabels.StaticDeps) |
| 3268 | |
| 3269 | var runtimeDeps bazel.LabelListAttribute |
| 3270 | attrs := &javaTestHostAttributes{ |
| 3271 | Runtime_deps: runtimeDeps, |
| 3272 | } |
| 3273 | props := bazel.BazelTargetModuleProperties{ |
| 3274 | Rule_class: "java_test", |
| 3275 | Bzl_load_location: "//build/bazel/rules/java:test.bzl", |
| 3276 | } |
| 3277 | |
| 3278 | if commonAttrs.Srcs.IsEmpty() { |
| 3279 | // if there are no sources, then the dependencies can only be used at runtime |
| 3280 | attrs.Runtime_deps = deps |
| 3281 | attrs.javaCommonAttributes = commonAttrs |
| 3282 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
| 3283 | return |
| 3284 | } |
| 3285 | |
| 3286 | libInfo := libraryCreationInfo{ |
Spandan Das | 5dba0ec | 2023-06-01 02:48:09 +0000 | [diff] [blame] | 3287 | deps: deps, |
| 3288 | attrs: commonAttrs, |
| 3289 | baseName: m.Name(), |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3290 | hasKotlin: bp2BuildInfo.hasKotlin, |
| 3291 | } |
| 3292 | libName := createLibraryTarget(ctx, libInfo) |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3293 | |
Zi Wang | 7873f61 | 2023-07-17 16:36:19 -0700 | [diff] [blame] | 3294 | attrs.Srcs = commonAttrs.Srcs |
| 3295 | attrs.Deps = deps |
| 3296 | attrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + libName}}) |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3297 | // Create the BazelTargetModule. |
| 3298 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
| 3299 | } |
| 3300 | |
| 3301 | // libraryCreationInfo encapsulates the info needed to create java_library target from |
| 3302 | // java_binary_host or java_test_host. |
| 3303 | type libraryCreationInfo struct { |
Spandan Das | 5dba0ec | 2023-06-01 02:48:09 +0000 | [diff] [blame] | 3304 | deps bazel.LabelListAttribute |
| 3305 | attrs *javaCommonAttributes |
| 3306 | baseName string |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3307 | hasKotlin bool |
| 3308 | } |
| 3309 | |
| 3310 | // helper function that creates java_library target from java_binary_host or java_test_host, |
| 3311 | // and returns the library target name, |
| 3312 | func createLibraryTarget(ctx android.TopDownMutatorContext, libInfo libraryCreationInfo) string { |
| 3313 | libName := libInfo.baseName + "_lib" |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 3314 | var libProps bazel.BazelTargetModuleProperties |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3315 | if libInfo.hasKotlin { |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3316 | libProps = ktJvmLibraryBazelTargetModuleProperties() |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 3317 | } else { |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3318 | libProps = javaLibraryBazelTargetModuleProperties() |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 3319 | } |
| 3320 | libAttrs := &javaLibraryAttributes{ |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3321 | Deps: libInfo.deps, |
| 3322 | javaCommonAttributes: libInfo.attrs, |
Alix | 7c8eaeb | 2022-11-16 15:44:55 +0000 | [diff] [blame] | 3323 | } |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3324 | |
Romain Jobredeaux | f5f6073 | 2023-03-16 11:00:36 -0400 | [diff] [blame] | 3325 | ctx.CreateBazelTargetModule(libProps, android.CommonAttributes{Name: libName}, libAttrs) |
Zi Wang | 65b3672 | 2023-05-23 15:18:33 -0700 | [diff] [blame] | 3326 | return libName |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 3327 | } |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 3328 | |
| 3329 | type bazelJavaImportAttributes struct { |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 3330 | Jars bazel.LabelListAttribute |
| 3331 | Exports bazel.LabelListAttribute |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | // java_import bp2Build converter. |
| 3335 | func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 3336 | var jars bazel.LabelListAttribute |
| 3337 | archVariantProps := i.GetArchVariantProperties(ctx, &ImportProperties{}) |
| 3338 | for axis, configToProps := range archVariantProps { |
| 3339 | for config, _props := range configToProps { |
| 3340 | if archProps, ok := _props.(*ImportProperties); ok { |
| 3341 | archJars := android.BazelLabelForModuleSrcExcludes(ctx, archProps.Jars, []string(nil)) |
| 3342 | jars.SetSelectValue(axis, config, archJars) |
| 3343 | } |
| 3344 | } |
| 3345 | } |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 3346 | |
| 3347 | attrs := &bazelJavaImportAttributes{ |
| 3348 | Jars: jars, |
| 3349 | } |
Alix | 82acaf5 | 2023-03-09 20:43:10 +0000 | [diff] [blame] | 3350 | props := bazel.BazelTargetModuleProperties{ |
| 3351 | Rule_class: "java_import", |
Alix | 83a8bc4 | 2023-05-04 18:03:23 +0000 | [diff] [blame] | 3352 | Bzl_load_location: "//build/bazel/rules/java:import.bzl", |
Alix | 82acaf5 | 2023-03-09 20:43:10 +0000 | [diff] [blame] | 3353 | } |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 3354 | |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 3355 | name := android.RemoveOptionalPrebuiltPrefix(i.Name()) |
| 3356 | |
| 3357 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) |
| 3358 | |
| 3359 | neverlink := true |
| 3360 | neverlinkAttrs := &javaLibraryAttributes{ |
| 3361 | Neverlink: bazel.BoolAttribute{Value: &neverlink}, |
| 3362 | Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + name}), |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 3363 | javaCommonAttributes: &javaCommonAttributes{ |
| 3364 | Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")}, |
| 3365 | }, |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 3366 | } |
Alix | 82acaf5 | 2023-03-09 20:43:10 +0000 | [diff] [blame] | 3367 | ctx.CreateBazelTargetModule( |
Alix | 3254002 | 2023-03-16 21:06:13 +0000 | [diff] [blame] | 3368 | javaLibraryBazelTargetModuleProperties(), |
Alix | 82acaf5 | 2023-03-09 20:43:10 +0000 | [diff] [blame] | 3369 | android.CommonAttributes{Name: name + "-neverlink"}, |
| 3370 | neverlinkAttrs) |
Alix | b4e09a0 | 2022-09-27 15:36:01 +0000 | [diff] [blame] | 3371 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 3372 | } |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 3373 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 3374 | var _ android.MixedBuildBuildable = (*Import)(nil) |
| 3375 | |
| 3376 | func (i *Import) getBazelModuleLabel(ctx android.BaseModuleContext) string { |
| 3377 | return android.RemoveOptionalPrebuiltPrefixFromBazelLabel(i.GetBazelLabel(ctx, i)) |
| 3378 | } |
| 3379 | |
| 3380 | func (i *Import) ProcessBazelQueryResponse(ctx android.ModuleContext) { |
| 3381 | i.commonBuildActions(ctx) |
| 3382 | |
| 3383 | bazelCtx := ctx.Config().BazelContext |
| 3384 | filePaths, err := bazelCtx.GetOutputFiles(i.getBazelModuleLabel(ctx), android.GetConfigKey(ctx)) |
| 3385 | if err != nil { |
| 3386 | ctx.ModuleErrorf(err.Error()) |
| 3387 | return |
| 3388 | } |
| 3389 | |
| 3390 | bazelJars := android.Paths{} |
| 3391 | for _, bazelOutputFile := range filePaths { |
| 3392 | bazelJars = append(bazelJars, android.PathForBazelOut(ctx, bazelOutputFile)) |
| 3393 | } |
| 3394 | |
| 3395 | jarName := android.RemoveOptionalPrebuiltPrefix(i.Name()) + ".jar" |
| 3396 | outputFile := android.PathForModuleOut(ctx, "bazelCombined", jarName) |
| 3397 | TransformJarsToJar(ctx, outputFile, "combine prebuilt jars", bazelJars, |
| 3398 | android.OptionalPath{}, // manifest |
| 3399 | false, // stripDirEntries |
| 3400 | []string{}, // filesToStrip |
| 3401 | []string{}, // dirsToStrip |
| 3402 | ) |
| 3403 | i.combinedClasspathFile = outputFile |
| 3404 | |
| 3405 | ctx.SetProvider(JavaInfoProvider, JavaInfo{ |
| 3406 | HeaderJars: android.PathsIfNonNil(i.combinedClasspathFile), |
| 3407 | ImplementationAndResourcesJars: android.PathsIfNonNil(i.combinedClasspathFile), |
| 3408 | ImplementationJars: android.PathsIfNonNil(i.combinedClasspathFile), |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 3409 | // TODO(b/240308299) include AIDL information from Bazel |
| 3410 | // TODO: aconfig files? |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 3411 | }) |
| 3412 | |
| 3413 | i.maybeInstall(ctx, jarName, outputFile) |
| 3414 | } |
| 3415 | |
| 3416 | func (i *Import) QueueBazelCall(ctx android.BaseModuleContext) { |
| 3417 | bazelCtx := ctx.Config().BazelContext |
| 3418 | bazelCtx.QueueBazelRequest(i.getBazelModuleLabel(ctx), cquery.GetOutputFiles, android.GetConfigKey(ctx)) |
| 3419 | } |
| 3420 | |
| 3421 | func (i *Import) IsMixedBuildSupported(ctx android.BaseModuleContext) bool { |
| 3422 | return true |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 3423 | } |