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" |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 24 | "strings" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 25 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 26 | "android/soong/bazel" |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 27 | "android/soong/bazel/cquery" |
Sam Delmerico | 4e27229 | 2022-01-06 20:03:51 +0000 | [diff] [blame] | 28 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 29 | "github.com/google/blueprint" |
Colin Cross | 76b5f0c | 2017-08-29 16:02:06 -0700 | [diff] [blame] | 30 | "github.com/google/blueprint/proptools" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 31 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 32 | "android/soong/android" |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 33 | "android/soong/cc" |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 34 | "android/soong/dexpreopt" |
Colin Cross | 3e3e72d | 2017-06-22 17:20:19 -0700 | [diff] [blame] | 35 | "android/soong/java/config" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 36 | "android/soong/tradefed" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 37 | ) |
| 38 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 39 | func init() { |
Paul Duffin | 535e0a1 | 2021-03-30 23:34:32 +0100 | [diff] [blame] | 40 | registerJavaBuildComponents(android.InitRegistrationContext) |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 41 | |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 42 | RegisterJavaSdkMemberTypes() |
| 43 | } |
| 44 | |
Paul Duffin | 535e0a1 | 2021-03-30 23:34:32 +0100 | [diff] [blame] | 45 | func registerJavaBuildComponents(ctx android.RegistrationContext) { |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 46 | ctx.RegisterModuleType("java_defaults", DefaultsFactory) |
| 47 | |
| 48 | ctx.RegisterModuleType("java_library", LibraryFactory) |
| 49 | ctx.RegisterModuleType("java_library_static", LibraryStaticFactory) |
| 50 | ctx.RegisterModuleType("java_library_host", LibraryHostFactory) |
| 51 | ctx.RegisterModuleType("java_binary", BinaryFactory) |
| 52 | ctx.RegisterModuleType("java_binary_host", BinaryHostFactory) |
| 53 | ctx.RegisterModuleType("java_test", TestFactory) |
| 54 | ctx.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory) |
| 55 | ctx.RegisterModuleType("java_test_host", TestHostFactory) |
| 56 | ctx.RegisterModuleType("java_test_import", JavaTestImportFactory) |
| 57 | ctx.RegisterModuleType("java_import", ImportFactory) |
| 58 | ctx.RegisterModuleType("java_import_host", ImportFactoryHost) |
| 59 | ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory) |
| 60 | ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) |
| 61 | ctx.RegisterModuleType("dex_import", DexImportFactory) |
| 62 | |
Martin Stjernholm | 0e4cceb | 2021-05-13 02:38:35 +0100 | [diff] [blame] | 63 | // This mutator registers dependencies on dex2oat for modules that should be |
| 64 | // dexpreopted. This is done late when the final variants have been |
| 65 | // established, to not get the dependencies split into the wrong variants and |
| 66 | // to support the checks in dexpreoptDisabled(). |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 67 | ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 68 | ctx.BottomUp("dexpreopt_tool_deps", dexpreoptToolDepsMutator).Parallel() |
| 69 | }) |
| 70 | |
| 71 | ctx.RegisterSingletonType("logtags", LogtagsSingleton) |
| 72 | ctx.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory) |
| 73 | } |
| 74 | |
| 75 | func RegisterJavaSdkMemberTypes() { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 76 | // Register sdk member types. |
Paul Duffin | 7b81f5e | 2020-01-13 21:03:22 +0000 | [diff] [blame] | 77 | android.RegisterSdkMemberType(javaHeaderLibsSdkMemberType) |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 78 | android.RegisterSdkMemberType(javaLibsSdkMemberType) |
| 79 | android.RegisterSdkMemberType(javaBootLibsSdkMemberType) |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 80 | android.RegisterSdkMemberType(javaSystemserverLibsSdkMemberType) |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 81 | android.RegisterSdkMemberType(javaTestSdkMemberType) |
| 82 | } |
| 83 | |
| 84 | var ( |
| 85 | // Supports adding java header libraries to module_exports and sdk. |
| 86 | javaHeaderLibsSdkMemberType = &librarySdkMemberType{ |
| 87 | android.SdkMemberTypeBase{ |
| 88 | PropertyName: "java_header_libs", |
| 89 | SupportsSdk: true, |
| 90 | }, |
| 91 | func(_ android.SdkMemberContext, j *Library) android.Path { |
| 92 | headerJars := j.HeaderJars() |
| 93 | if len(headerJars) != 1 { |
| 94 | panic(fmt.Errorf("there must be only one header jar from %q", j.Name())) |
| 95 | } |
| 96 | |
| 97 | return headerJars[0] |
| 98 | }, |
| 99 | sdkSnapshotFilePathForJar, |
| 100 | copyEverythingToSnapshot, |
| 101 | } |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 102 | |
Paul Duffin | 22ff0aa | 2021-02-04 11:15:34 +0000 | [diff] [blame] | 103 | // Export implementation classes jar as part of the sdk. |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 104 | exportImplementationClassesJar = func(_ android.SdkMemberContext, j *Library) android.Path { |
Paul Duffin | 22ff0aa | 2021-02-04 11:15:34 +0000 | [diff] [blame] | 105 | implementationJars := j.ImplementationAndResourcesJars() |
| 106 | if len(implementationJars) != 1 { |
| 107 | panic(fmt.Errorf("there must be only one implementation jar from %q", j.Name())) |
| 108 | } |
| 109 | return implementationJars[0] |
| 110 | } |
| 111 | |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 112 | // Supports adding java implementation libraries to module_exports but not sdk. |
| 113 | javaLibsSdkMemberType = &librarySdkMemberType{ |
Paul Duffin | f5c0a9c | 2020-02-28 14:39:53 +0000 | [diff] [blame] | 114 | android.SdkMemberTypeBase{ |
| 115 | PropertyName: "java_libs", |
| 116 | }, |
Paul Duffin | 22ff0aa | 2021-02-04 11:15:34 +0000 | [diff] [blame] | 117 | exportImplementationClassesJar, |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 118 | sdkSnapshotFilePathForJar, |
| 119 | copyEverythingToSnapshot, |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 120 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 121 | |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 122 | snapshotRequiresImplementationJar = func(ctx android.SdkMemberContext) bool { |
| 123 | // In the S build the build will break if updatable-media does not provide a full implementation |
| 124 | // jar. That issue was fixed in Tiramisu by b/229932396. |
| 125 | if ctx.IsTargetBuildBeforeTiramisu() && ctx.Name() == "updatable-media" { |
| 126 | return true |
| 127 | } |
| 128 | |
| 129 | return false |
| 130 | } |
| 131 | |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 132 | // Supports adding java boot libraries to module_exports and sdk. |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 133 | // |
| 134 | // The build has some implicit dependencies (via the boot jars configuration) on a number of |
| 135 | // modules, e.g. core-oj, apache-xml, that are part of the java boot class path and which are |
| 136 | // provided by mainline modules (e.g. art, conscrypt, runtime-i18n) but which are not otherwise |
| 137 | // used outside those mainline modules. |
| 138 | // |
| 139 | // As they are not needed outside the mainline modules adding them to the sdk/module-exports as |
| 140 | // either java_libs, or java_header_libs would end up exporting more information than was strictly |
| 141 | // necessary. The java_boot_libs property to allow those modules to be exported as part of the |
| 142 | // sdk/module_exports without exposing any unnecessary information. |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 143 | javaBootLibsSdkMemberType = &librarySdkMemberType{ |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 144 | android.SdkMemberTypeBase{ |
| 145 | PropertyName: "java_boot_libs", |
| 146 | SupportsSdk: true, |
| 147 | }, |
Paul Duffin | 5c21145 | 2021-07-15 12:42:44 +0100 | [diff] [blame] | 148 | func(ctx android.SdkMemberContext, j *Library) android.Path { |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 149 | if snapshotRequiresImplementationJar(ctx) { |
| 150 | return exportImplementationClassesJar(ctx, j) |
| 151 | } |
| 152 | |
Paul Duffin | 5c21145 | 2021-07-15 12:42:44 +0100 | [diff] [blame] | 153 | // Java boot libs are only provided in the SDK to provide access to their dex implementation |
| 154 | // jar for use by dexpreopting and boot jars package check. They do not need to provide an |
| 155 | // actual implementation jar but the java_import will need a file that exists so just copy an |
| 156 | // empty file. Any attempt to use that file as a jar will cause a build error. |
| 157 | return ctx.SnapshotBuilder().EmptyFile() |
| 158 | }, |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 159 | func(ctx android.SdkMemberContext, osPrefix, name string) string { |
| 160 | if snapshotRequiresImplementationJar(ctx) { |
| 161 | return sdkSnapshotFilePathForJar(ctx, osPrefix, name) |
| 162 | } |
| 163 | |
Paul Duffin | 5c21145 | 2021-07-15 12:42:44 +0100 | [diff] [blame] | 164 | // Create a special name for the implementation jar to try and provide some useful information |
| 165 | // to a developer that attempts to compile against this. |
| 166 | // TODO(b/175714559): Provide a proper error message in Soong not ninja. |
| 167 | return filepath.Join(osPrefix, "java_boot_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix) |
| 168 | }, |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 169 | onlyCopyJarToSnapshot, |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 170 | } |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 171 | |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 172 | // Supports adding java systemserver libraries to module_exports and sdk. |
| 173 | // |
| 174 | // The build has some implicit dependencies (via the systemserver jars configuration) on a number |
| 175 | // of modules that are part of the java systemserver classpath and which are provided by mainline |
| 176 | // modules but which are not otherwise used outside those mainline modules. |
| 177 | // |
| 178 | // As they are not needed outside the mainline modules adding them to the sdk/module-exports as |
| 179 | // either java_libs, or java_header_libs would end up exporting more information than was strictly |
| 180 | // necessary. The java_systemserver_libs property to allow those modules to be exported as part of |
| 181 | // the sdk/module_exports without exposing any unnecessary information. |
| 182 | javaSystemserverLibsSdkMemberType = &librarySdkMemberType{ |
| 183 | android.SdkMemberTypeBase{ |
| 184 | PropertyName: "java_systemserver_libs", |
| 185 | SupportsSdk: true, |
Paul Duffin | f861df7 | 2022-07-01 15:56:06 +0000 | [diff] [blame] | 186 | |
| 187 | // This was only added in Tiramisu. |
| 188 | SupportedBuildReleaseSpecification: "Tiramisu+", |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 189 | }, |
| 190 | func(ctx android.SdkMemberContext, j *Library) android.Path { |
| 191 | // Java systemserver libs are only provided in the SDK to provide access to their dex |
| 192 | // implementation jar for use by dexpreopting. They do not need to provide an actual |
| 193 | // implementation jar but the java_import will need a file that exists so just copy an empty |
| 194 | // file. Any attempt to use that file as a jar will cause a build error. |
| 195 | return ctx.SnapshotBuilder().EmptyFile() |
| 196 | }, |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 197 | func(_ android.SdkMemberContext, osPrefix, name string) string { |
Jiakai Zhang | ea18033 | 2021-09-26 08:58:02 +0000 | [diff] [blame] | 198 | // Create a special name for the implementation jar to try and provide some useful information |
| 199 | // to a developer that attempts to compile against this. |
| 200 | // TODO(b/175714559): Provide a proper error message in Soong not ninja. |
| 201 | return filepath.Join(osPrefix, "java_systemserver_libs", "snapshot", "jars", "are", "invalid", name+jarFileSuffix) |
| 202 | }, |
| 203 | onlyCopyJarToSnapshot, |
| 204 | } |
| 205 | |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 206 | // Supports adding java test libraries to module_exports but not sdk. |
| 207 | javaTestSdkMemberType = &testSdkMemberType{ |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 208 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 209 | PropertyName: "java_tests", |
| 210 | }, |
Paul Duffin | 2da0424 | 2021-04-23 19:43:28 +0100 | [diff] [blame] | 211 | } |
| 212 | ) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 213 | |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 214 | // JavaInfo contains information about a java module for use by modules that depend on it. |
| 215 | type JavaInfo struct { |
| 216 | // HeaderJars is a list of jars that can be passed as the javac classpath in order to link |
| 217 | // against this module. If empty, ImplementationJars should be used instead. |
| 218 | HeaderJars android.Paths |
| 219 | |
| 220 | // ImplementationAndResourceJars is a list of jars that contain the implementations of classes |
| 221 | // in the module as well as any resources included in the module. |
| 222 | ImplementationAndResourcesJars android.Paths |
| 223 | |
| 224 | // ImplementationJars is a list of jars that contain the implementations of classes in the |
| 225 | //module. |
| 226 | ImplementationJars android.Paths |
| 227 | |
| 228 | // ResourceJars is a list of jars that contain the resources included in the module. |
| 229 | ResourceJars android.Paths |
| 230 | |
| 231 | // AidlIncludeDirs is a list of directories that should be passed to the aidl tool when |
| 232 | // depending on this module. |
| 233 | AidlIncludeDirs android.Paths |
| 234 | |
| 235 | // SrcJarArgs is a list of arguments to pass to soong_zip to package the sources of this |
| 236 | // module. |
| 237 | SrcJarArgs []string |
| 238 | |
| 239 | // SrcJarDeps is a list of paths to depend on when packaging the sources of this module. |
| 240 | SrcJarDeps android.Paths |
| 241 | |
| 242 | // ExportedPlugins is a list of paths that should be used as annotation processors for any |
| 243 | // module that depends on this module. |
| 244 | ExportedPlugins android.Paths |
| 245 | |
| 246 | // ExportedPluginClasses is a list of classes that should be run as annotation processors for |
| 247 | // any module that depends on this module. |
| 248 | ExportedPluginClasses []string |
| 249 | |
| 250 | // ExportedPluginDisableTurbine is true if this module's annotation processors generate APIs, |
| 251 | // requiring disbling turbine for any modules that depend on it. |
| 252 | ExportedPluginDisableTurbine bool |
| 253 | |
| 254 | // JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be |
| 255 | // instrumented by jacoco. |
| 256 | JacocoReportClassesFile android.Path |
| 257 | } |
| 258 | |
| 259 | var JavaInfoProvider = blueprint.NewProvider(JavaInfo{}) |
| 260 | |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 261 | // SyspropPublicStubInfo contains info about the sysprop public stub library that corresponds to |
| 262 | // the sysprop implementation library. |
| 263 | type SyspropPublicStubInfo struct { |
| 264 | // JavaInfo is the JavaInfoProvider of the sysprop public stub library that corresponds to |
| 265 | // the sysprop implementation library. |
| 266 | JavaInfo JavaInfo |
| 267 | } |
| 268 | |
| 269 | var SyspropPublicStubInfoProvider = blueprint.NewProvider(SyspropPublicStubInfo{}) |
| 270 | |
Paul Duffin | 44b481b | 2020-06-17 16:59:43 +0100 | [diff] [blame] | 271 | // Methods that need to be implemented for a module that is added to apex java_libs property. |
| 272 | type ApexDependency interface { |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 273 | HeaderJars() android.Paths |
Paul Duffin | 44b481b | 2020-06-17 16:59:43 +0100 | [diff] [blame] | 274 | ImplementationAndResourcesJars() android.Paths |
| 275 | } |
| 276 | |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 277 | // Provides build path and install path to DEX jars. |
| 278 | type UsesLibraryDependency interface { |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 279 | DexJarBuildPath() OptionalDexJarPath |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 280 | DexJarInstallPath() android.Path |
Ulya Trafimovich | dbf3166 | 2020-12-17 12:07:54 +0000 | [diff] [blame] | 281 | ClassLoaderContexts() dexpreopt.ClassLoaderContextMap |
Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Jaewoong Jung | 2634264 | 2021-03-17 15:56:23 -0700 | [diff] [blame] | 284 | // TODO(jungjw): Move this to kythe.go once it's created. |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 285 | type xref interface { |
| 286 | XrefJavaFiles() android.Paths |
| 287 | } |
| 288 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 289 | func (j *Module) XrefJavaFiles() android.Paths { |
| 290 | return j.kytheFiles |
| 291 | } |
| 292 | |
Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 293 | type dependencyTag struct { |
| 294 | blueprint.BaseDependencyTag |
| 295 | name string |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 296 | |
| 297 | // True if the dependency is relinked at runtime. |
| 298 | runtimeLinked bool |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 299 | |
| 300 | // True if the dependency is a toolchain, for example an annotation processor. |
| 301 | toolchain bool |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 304 | // installDependencyTag is a dependency tag that is annotated to cause the installed files of the |
| 305 | // dependency to be installed when the parent module is installed. |
| 306 | type installDependencyTag struct { |
| 307 | blueprint.BaseDependencyTag |
| 308 | android.InstallAlwaysNeededDependencyTag |
| 309 | name string |
| 310 | } |
| 311 | |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 312 | func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation { |
| 313 | if d.runtimeLinked { |
| 314 | return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 315 | } else if d.toolchain { |
| 316 | return []android.LicenseAnnotation{android.LicenseAnnotationToolchain} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 317 | } |
| 318 | return nil |
| 319 | } |
| 320 | |
| 321 | var _ android.LicenseAnnotationsDependencyTag = dependencyTag{} |
| 322 | |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 323 | type usesLibraryDependencyTag struct { |
| 324 | dependencyTag |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 325 | sdkVersion int // SDK version in which the library appared as a standalone library. |
| 326 | optional bool // If the dependency is optional or required. |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 329 | func makeUsesLibraryDependencyTag(sdkVersion int, optional bool) usesLibraryDependencyTag { |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 330 | return usesLibraryDependencyTag{ |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 331 | dependencyTag: dependencyTag{ |
| 332 | name: fmt.Sprintf("uses-library-%d", sdkVersion), |
| 333 | runtimeLinked: true, |
| 334 | }, |
| 335 | sdkVersion: sdkVersion, |
| 336 | optional: optional, |
Ulya Trafimovich | b521811 | 2020-10-07 15:11:32 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
Jiyong Park | 8be103b | 2019-11-08 15:53:48 +0900 | [diff] [blame] | 340 | func IsJniDepTag(depTag blueprint.DependencyTag) bool { |
Colin Cross | de78d13 | 2020-10-09 18:59:49 -0700 | [diff] [blame] | 341 | return depTag == jniLibTag |
Jiyong Park | 8be103b | 2019-11-08 15:53:48 +0900 | [diff] [blame] | 342 | } |
| 343 | |
Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 344 | var ( |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 345 | dataNativeBinsTag = dependencyTag{name: "dataNativeBins"} |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 346 | dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 347 | staticLibTag = dependencyTag{name: "staticlib"} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 348 | libTag = dependencyTag{name: "javalib", runtimeLinked: true} |
| 349 | java9LibTag = dependencyTag{name: "java9lib", runtimeLinked: true} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 350 | pluginTag = dependencyTag{name: "plugin", toolchain: true} |
| 351 | errorpronePluginTag = dependencyTag{name: "errorprone-plugin", toolchain: true} |
| 352 | exportedPluginTag = dependencyTag{name: "exported-plugin", toolchain: true} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 353 | bootClasspathTag = dependencyTag{name: "bootclasspath", runtimeLinked: true} |
| 354 | systemModulesTag = dependencyTag{name: "system modules", runtimeLinked: true} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 355 | frameworkResTag = dependencyTag{name: "framework-res"} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 356 | kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib", runtimeLinked: true} |
| 357 | kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations", runtimeLinked: true} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 358 | kotlinPluginTag = dependencyTag{name: "kotlin-plugin", toolchain: true} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 359 | proguardRaiseTag = dependencyTag{name: "proguard-raise"} |
| 360 | certificateTag = dependencyTag{name: "certificate"} |
| 361 | instrumentationForTag = dependencyTag{name: "instrumentation_for"} |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 362 | extraLintCheckTag = dependencyTag{name: "extra-lint-check", toolchain: true} |
Colin Cross | 65cb314 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 363 | jniLibTag = dependencyTag{name: "jnilib", runtimeLinked: true} |
Colin Cross | 75ce9ec | 2021-02-26 16:20:32 -0800 | [diff] [blame] | 364 | syspropPublicStubDepTag = dependencyTag{name: "sysprop public stub"} |
| 365 | jniInstallTag = installDependencyTag{name: "jni install"} |
| 366 | binaryInstallTag = installDependencyTag{name: "binary install"} |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 367 | usesLibReqTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion, false) |
| 368 | usesLibOptTag = makeUsesLibraryDependencyTag(dexpreopt.AnySdkVersion, true) |
| 369 | usesLibCompat28OptTag = makeUsesLibraryDependencyTag(28, true) |
| 370 | usesLibCompat29ReqTag = makeUsesLibraryDependencyTag(29, false) |
| 371 | usesLibCompat30OptTag = makeUsesLibraryDependencyTag(30, true) |
Colin Cross | be1da47 | 2017-07-07 15:59:46 -0700 | [diff] [blame] | 372 | ) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 373 | |
Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 374 | func IsLibDepTag(depTag blueprint.DependencyTag) bool { |
| 375 | return depTag == libTag |
| 376 | } |
| 377 | |
| 378 | func IsStaticLibDepTag(depTag blueprint.DependencyTag) bool { |
| 379 | return depTag == staticLibTag |
| 380 | } |
| 381 | |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 382 | type sdkDep struct { |
Pete Gillin | e3d44b2 | 2020-06-29 11:28:51 +0100 | [diff] [blame] | 383 | useModule, useFiles, invalidVersion bool |
Colin Cross | 47ff252 | 2017-10-02 14:22:08 -0700 | [diff] [blame] | 384 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 385 | // The modules that will be added to the bootclasspath when targeting 1.8 or lower |
| 386 | bootclasspath []string |
Paul Duffin | e25c644 | 2019-10-11 13:50:28 +0100 | [diff] [blame] | 387 | |
| 388 | // The default system modules to use. Will be an empty string if no system |
| 389 | // modules are to be used. |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 390 | systemModules string |
| 391 | |
Pete Gillin | e3d44b2 | 2020-06-29 11:28:51 +0100 | [diff] [blame] | 392 | // The modules that will be added to the classpath regardless of the Java language level targeted |
| 393 | classpath []string |
| 394 | |
Colin Cross | 6cef481 | 2019-10-17 14:23:50 -0700 | [diff] [blame] | 395 | // 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] | 396 | // (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] | 397 | java9Classpath []string |
| 398 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 399 | frameworkResModule string |
| 400 | |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 401 | jars android.Paths |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 402 | aidl android.OptionalPath |
Paul Duffin | 250e619 | 2019-06-07 10:44:37 +0100 | [diff] [blame] | 403 | |
| 404 | noStandardLibs, noFrameworksLibs bool |
| 405 | } |
| 406 | |
| 407 | func (s sdkDep) hasStandardLibs() bool { |
| 408 | return !s.noStandardLibs |
| 409 | } |
| 410 | |
| 411 | func (s sdkDep) hasFrameworkLibs() bool { |
| 412 | return !s.noStandardLibs && !s.noFrameworksLibs |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 415 | type jniLib struct { |
Colin Cross | 403cc15 | 2020-07-06 14:15:24 -0700 | [diff] [blame] | 416 | name string |
| 417 | path android.Path |
| 418 | target android.Target |
| 419 | coverageFile android.OptionalPath |
| 420 | unstrippedFile android.Path |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 423 | func sdkDeps(ctx android.BottomUpMutatorContext, sdkContext android.SdkContext, d dexer) { |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 424 | sdkDep := decodeSdkDep(ctx, sdkContext) |
| 425 | if sdkDep.useModule { |
| 426 | ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.bootclasspath...) |
| 427 | ctx.AddVariationDependencies(nil, java9LibTag, sdkDep.java9Classpath...) |
| 428 | ctx.AddVariationDependencies(nil, libTag, sdkDep.classpath...) |
| 429 | if d.effectiveOptimizeEnabled() && sdkDep.hasStandardLibs() { |
| 430 | ctx.AddVariationDependencies(nil, proguardRaiseTag, config.LegacyCorePlatformBootclasspathLibraries...) |
| 431 | } |
| 432 | if d.effectiveOptimizeEnabled() && sdkDep.hasFrameworkLibs() { |
| 433 | ctx.AddVariationDependencies(nil, proguardRaiseTag, config.FrameworkLibraries...) |
| 434 | } |
| 435 | } |
| 436 | if sdkDep.systemModules != "" { |
| 437 | ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules) |
| 438 | } |
| 439 | } |
| 440 | |
Colin Cross | 32f676a | 2017-09-06 13:41:06 -0700 | [diff] [blame] | 441 | type deps struct { |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 442 | // bootClasspath is the list of jars that form the boot classpath (generally the java.* and |
| 443 | // android.* classes) for tools that still use it. javac targeting 1.9 or higher uses |
| 444 | // systemModules and java9Classpath instead. |
| 445 | bootClasspath classpath |
| 446 | |
| 447 | // classpath is the list of jars that form the classpath for javac and kotlinc rules. It |
| 448 | // contains header jars for all static and non-static dependencies. |
| 449 | classpath classpath |
| 450 | |
| 451 | // dexClasspath is the list of jars that form the classpath for d8 and r8 rules. It contains |
| 452 | // header jars for all non-static dependencies. Static dependencies have already been |
| 453 | // combined into the program jar. |
| 454 | dexClasspath classpath |
| 455 | |
| 456 | // java9Classpath is the list of jars that will be added to the classpath when targeting |
| 457 | // 1.9 or higher. It generally contains the android.* classes, while the java.* classes |
| 458 | // are provided by systemModules. |
| 459 | java9Classpath classpath |
| 460 | |
Colin Cross | 748b2d8 | 2020-11-19 13:52:06 -0800 | [diff] [blame] | 461 | processorPath classpath |
| 462 | errorProneProcessorPath classpath |
| 463 | processorClasses []string |
| 464 | staticJars android.Paths |
| 465 | staticHeaderJars android.Paths |
| 466 | staticResourceJars android.Paths |
| 467 | aidlIncludeDirs android.Paths |
| 468 | srcs android.Paths |
| 469 | srcJars android.Paths |
| 470 | systemModules *systemModules |
| 471 | aidlPreprocess android.OptionalPath |
| 472 | kotlinStdlib android.Paths |
| 473 | kotlinAnnotations android.Paths |
Colin Cross | a1ff7c6 | 2021-09-17 14:11:52 -0700 | [diff] [blame] | 474 | kotlinPlugins android.Paths |
Colin Cross | be9cdb8 | 2019-01-21 21:37:16 -0800 | [diff] [blame] | 475 | |
| 476 | disableTurbine bool |
Colin Cross | 32f676a | 2017-09-06 13:41:06 -0700 | [diff] [blame] | 477 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 478 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 479 | func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) { |
| 480 | for _, f := range dep.Srcs() { |
| 481 | if f.Ext() != ".jar" { |
| 482 | ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency", |
| 483 | ctx.OtherModuleName(dep.(blueprint.Module))) |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 488 | func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext android.SdkContext) javaVersion { |
Nan Zhang | 357466b | 2018-04-17 17:38:36 -0700 | [diff] [blame] | 489 | if javaVersion != "" { |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 490 | return normalizeJavaVersion(ctx, javaVersion) |
Colin Cross | 17dec17 | 2020-05-14 18:05:32 -0700 | [diff] [blame] | 491 | } else if ctx.Device() { |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 492 | return defaultJavaLanguageVersion(ctx, sdkContext.SdkVersion(ctx)) |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 493 | } else if ctx.Config().TargetsJava17() { |
| 494 | // Temporary experimental flag to be able to try and build with |
| 495 | // java version 17 options. The flag, if used, just sets Java |
| 496 | // 17 as the default version, leaving any components that |
| 497 | // target an older version intact. |
| 498 | return JAVA_VERSION_17 |
Sorin Basca | 8d3e0bb | 2022-01-20 15:21:51 +0000 | [diff] [blame] | 499 | } else { |
Sorin Basca | 18ecf61 | 2022-01-23 09:01:07 +0000 | [diff] [blame] | 500 | return JAVA_VERSION_11 |
Nan Zhang | 357466b | 2018-04-17 17:38:36 -0700 | [diff] [blame] | 501 | } |
Nan Zhang | 357466b | 2018-04-17 17:38:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 504 | type javaVersion int |
| 505 | |
| 506 | const ( |
| 507 | JAVA_VERSION_UNSUPPORTED = 0 |
| 508 | JAVA_VERSION_6 = 6 |
| 509 | JAVA_VERSION_7 = 7 |
| 510 | JAVA_VERSION_8 = 8 |
| 511 | JAVA_VERSION_9 = 9 |
Sorin Basca | c0244da | 2021-11-26 17:26:33 +0000 | [diff] [blame] | 512 | JAVA_VERSION_11 = 11 |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 513 | JAVA_VERSION_17 = 17 |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 514 | ) |
| 515 | |
| 516 | func (v javaVersion) String() string { |
| 517 | switch v { |
| 518 | case JAVA_VERSION_6: |
| 519 | return "1.6" |
| 520 | case JAVA_VERSION_7: |
| 521 | return "1.7" |
| 522 | case JAVA_VERSION_8: |
| 523 | return "1.8" |
| 524 | case JAVA_VERSION_9: |
| 525 | return "1.9" |
Sorin Basca | c0244da | 2021-11-26 17:26:33 +0000 | [diff] [blame] | 526 | case JAVA_VERSION_11: |
| 527 | return "11" |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 528 | case JAVA_VERSION_17: |
| 529 | return "17" |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 530 | default: |
| 531 | return "unsupported" |
| 532 | } |
| 533 | } |
| 534 | |
Cole Faust | d96eebf | 2022-06-28 14:41:27 -0700 | [diff] [blame] | 535 | func (v javaVersion) StringForKotlinc() string { |
| 536 | // $ ./external/kotlinc/bin/kotlinc -jvm-target foo |
| 537 | // error: unknown JVM target version: foo |
| 538 | // Supported versions: 1.6, 1.8, 9, 10, 11, 12, 13, 14, 15, 16, 17 |
| 539 | switch v { |
| 540 | case JAVA_VERSION_7: |
| 541 | return "1.6" |
| 542 | case JAVA_VERSION_9: |
| 543 | return "9" |
| 544 | default: |
| 545 | return v.String() |
| 546 | } |
| 547 | } |
| 548 | |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 549 | // Returns true if javac targeting this version uses system modules instead of a bootclasspath. |
| 550 | func (v javaVersion) usesJavaModules() bool { |
| 551 | return v >= 9 |
| 552 | } |
| 553 | |
| 554 | func normalizeJavaVersion(ctx android.BaseModuleContext, javaVersion string) javaVersion { |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 555 | switch javaVersion { |
| 556 | case "1.6", "6": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 557 | return JAVA_VERSION_6 |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 558 | case "1.7", "7": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 559 | return JAVA_VERSION_7 |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 560 | case "1.8", "8": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 561 | return JAVA_VERSION_8 |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 562 | case "1.9", "9": |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 563 | return JAVA_VERSION_9 |
Sorin Basca | c0244da | 2021-11-26 17:26:33 +0000 | [diff] [blame] | 564 | case "11": |
| 565 | return JAVA_VERSION_11 |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 566 | case "17": |
Sorin Basca | 5938fed | 2022-06-22 12:53:51 +0100 | [diff] [blame] | 567 | return JAVA_VERSION_17 |
Sorin Basca | ce720c3 | 2022-05-24 12:13:50 +0100 | [diff] [blame] | 568 | case "10", "12", "13", "14", "15", "16": |
| 569 | ctx.PropertyErrorf("java_version", "Java language level %s is not supported", javaVersion) |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 570 | return JAVA_VERSION_UNSUPPORTED |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 571 | default: |
| 572 | ctx.PropertyErrorf("java_version", "Unrecognized Java language level") |
Colin Cross | 1e74385 | 2019-10-28 11:37:20 -0700 | [diff] [blame] | 573 | return JAVA_VERSION_UNSUPPORTED |
Pete Gillin | 4e8b48a | 2019-07-12 13:16:17 +0100 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 577 | // |
| 578 | // Java libraries (.jar file) |
| 579 | // |
| 580 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 581 | type Library struct { |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 582 | Module |
Colin Cross | f0f2e2c | 2019-10-15 16:36:40 -0700 | [diff] [blame] | 583 | |
| 584 | InstallMixin func(ctx android.ModuleContext, installPath android.Path) (extraInstallDeps android.Paths) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 587 | var _ android.ApexModule = (*Library)(nil) |
| 588 | |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 589 | // Provides access to the list of permitted packages from apex boot jars. |
Paul Duffin | e739f1e | 2020-05-29 11:24:51 +0100 | [diff] [blame] | 590 | type PermittedPackagesForUpdatableBootJars interface { |
| 591 | PermittedPackagesForUpdatableBootJars() []string |
| 592 | } |
| 593 | |
| 594 | var _ PermittedPackagesForUpdatableBootJars = (*Library)(nil) |
| 595 | |
| 596 | func (j *Library) PermittedPackagesForUpdatableBootJars() []string { |
| 597 | return j.properties.Permitted_packages |
| 598 | } |
| 599 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 600 | func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool { |
Ulya Trafimovich | f491dde | 2020-01-24 12:19:45 +0000 | [diff] [blame] | 601 | // Store uncompressed (and aligned) any dex files from jars in APEXes. |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 602 | if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() { |
Ulya Trafimovich | f491dde | 2020-01-24 12:19:45 +0000 | [diff] [blame] | 603 | return true |
| 604 | } |
| 605 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 606 | // Store uncompressed (and do not strip) dex files from boot class path jars. |
| 607 | if inList(ctx.ModuleName(), ctx.Config().BootJars()) { |
| 608 | return true |
| 609 | } |
| 610 | |
| 611 | // Store uncompressed dex files that are preopted on /system. |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 612 | if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !dexpreopter.odexOnSystemOther(ctx, dexpreopter.installPath)) { |
Vladimir Marko | e8b00d6 | 2018-12-21 15:54:16 +0000 | [diff] [blame] | 613 | return true |
| 614 | } |
Colin Cross | 083a2aa | 2019-02-06 16:37:12 -0800 | [diff] [blame] | 615 | if ctx.Config().UncompressPrivAppDex() && |
| 616 | inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) { |
| 617 | return true |
| 618 | } |
| 619 | |
Colin Cross | 2fc72f6 | 2018-12-21 12:59:54 -0800 | [diff] [blame] | 620 | return false |
| 621 | } |
| 622 | |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 623 | // Sets `dexer.dexProperties.Uncompress_dex` to the proper value. |
| 624 | func setUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter, dexer *dexer) { |
| 625 | if dexer.dexProperties.Uncompress_dex == nil { |
| 626 | // If the value was not force-set by the user, use reasonable default based on the module. |
| 627 | dexer.dexProperties.Uncompress_dex = proptools.BoolPtr(shouldUncompressDex(ctx, dexpreopter)) |
| 628 | } |
| 629 | } |
| 630 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 631 | func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 632 | j.sdkVersion = j.SdkVersion(ctx) |
| 633 | j.minSdkVersion = j.MinSdkVersion(ctx) |
satayev | 0a420e7 | 2021-11-29 17:25:52 +0000 | [diff] [blame] | 634 | j.maxSdkVersion = j.MaxSdkVersion(ctx) |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 635 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 636 | apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 637 | if !apexInfo.IsForPlatform() { |
| 638 | j.hideApexVariantFromMake = true |
| 639 | } |
| 640 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 641 | j.checkSdkVersions(ctx) |
Colin Cross | 61df14a | 2021-12-01 13:04:46 -0800 | [diff] [blame] | 642 | if ctx.Device() { |
| 643 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath( |
| 644 | ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")) |
| 645 | j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary |
| 646 | setUncompressDex(ctx, &j.dexpreopter, &j.dexer) |
| 647 | j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex |
| 648 | j.classLoaderContexts = j.usesLibrary.classLoaderContextForUsesLibDeps(ctx) |
| 649 | } |
Jaewoong Jung | a24af3b | 2019-05-13 09:23:20 -0700 | [diff] [blame] | 650 | j.compile(ctx, nil) |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 651 | |
bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 652 | // Collect the module directory for IDE info in java/jdeps.go. |
| 653 | j.modulePaths = append(j.modulePaths, ctx.ModuleDir()) |
| 654 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 655 | exclusivelyForApex := !apexInfo.IsForPlatform() |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 656 | if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex { |
Colin Cross | f0f2e2c | 2019-10-15 16:36:40 -0700 | [diff] [blame] | 657 | var extraInstallDeps android.Paths |
| 658 | if j.InstallMixin != nil { |
| 659 | extraInstallDeps = j.InstallMixin(ctx, j.outputFile) |
| 660 | } |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 661 | hostDexNeeded := Bool(j.deviceProperties.Hostdex) && !ctx.Host() |
| 662 | if hostDexNeeded { |
Colin Cross | 3108ce1 | 2021-11-10 14:38:50 -0800 | [diff] [blame] | 663 | j.hostdexInstallFile = ctx.InstallFile( |
| 664 | android.PathForHostDexInstall(ctx, "framework"), |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 665 | j.Stem()+"-hostdex.jar", j.outputFile) |
| 666 | } |
| 667 | var installDir android.InstallPath |
| 668 | if ctx.InstallInTestcases() { |
| 669 | var archDir string |
| 670 | if !ctx.Host() { |
| 671 | archDir = ctx.DeviceConfig().DeviceArch() |
| 672 | } |
| 673 | installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir) |
| 674 | } else { |
| 675 | installDir = android.PathForModuleInstall(ctx, "framework") |
| 676 | } |
| 677 | j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...) |
Colin Cross | 2c429dc | 2017-08-31 16:45:16 -0700 | [diff] [blame] | 678 | } |
Colin Cross | b7a6324 | 2015-04-16 14:09:14 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 681 | func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 682 | j.deps(ctx) |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 683 | j.usesLibrary.deps(ctx, false) |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 686 | const ( |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 687 | aidlIncludeDir = "aidl" |
| 688 | javaDir = "java" |
| 689 | jarFileSuffix = ".jar" |
| 690 | testConfigSuffix = "-AndroidTest.xml" |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 691 | ) |
| 692 | |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 693 | // 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] | 694 | func sdkSnapshotFilePathForJar(_ android.SdkMemberContext, osPrefix, name string) string { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 695 | return sdkSnapshotFilePathForMember(osPrefix, name, jarFileSuffix) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 698 | func sdkSnapshotFilePathForMember(osPrefix, name string, suffix string) string { |
| 699 | return filepath.Join(javaDir, osPrefix, name+suffix) |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 702 | type librarySdkMemberType struct { |
Paul Duffin | 255f18e | 2019-12-13 11:22:16 +0000 | [diff] [blame] | 703 | android.SdkMemberTypeBase |
Paul Duffin | f5c0a9c | 2020-02-28 14:39:53 +0000 | [diff] [blame] | 704 | |
| 705 | // Function to retrieve the appropriate output jar (implementation or header) from |
| 706 | // the library. |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 707 | jarToExportGetter func(ctx android.SdkMemberContext, j *Library) android.Path |
| 708 | |
| 709 | // Function to compute the snapshot relative path to which the named library's |
| 710 | // jar should be copied. |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 711 | snapshotPathGetter func(ctx android.SdkMemberContext, osPrefix, name string) string |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 712 | |
| 713 | // True if only the jar should be copied to the snapshot, false if the jar plus any additional |
| 714 | // files like aidl files should also be copied. |
| 715 | onlyCopyJarToSnapshot bool |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 718 | const ( |
| 719 | onlyCopyJarToSnapshot = true |
| 720 | copyEverythingToSnapshot = false |
| 721 | ) |
| 722 | |
Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 723 | func (mt *librarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { |
| 724 | ctx.AddVariationDependencies(nil, dependencyTag, names...) |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | func (mt *librarySdkMemberType) IsInstance(module android.Module) bool { |
| 728 | _, ok := module.(*Library) |
| 729 | return ok |
| 730 | } |
| 731 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 732 | func (mt *librarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { |
| 733 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_import") |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 734 | } |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 735 | |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 736 | func (mt *librarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 737 | return &librarySdkMemberProperties{} |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | type librarySdkMemberProperties struct { |
| 741 | android.SdkMemberPropertiesBase |
| 742 | |
Paul Duffin | 864e1b4 | 2020-05-06 10:23:19 +0100 | [diff] [blame] | 743 | JarToExport android.Path `android:"arch_variant"` |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 744 | AidlIncludeDirs android.Paths |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 745 | |
| 746 | // The list of permitted packages that need to be passed to the prebuilts as they are used to |
| 747 | // create the updatable-bcp-packages.txt file. |
| 748 | PermittedPackages []string |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 751 | func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { |
Paul Duffin | 1387957 | 2019-11-28 14:31:38 +0000 | [diff] [blame] | 752 | j := variant.(*Library) |
| 753 | |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 754 | p.JarToExport = ctx.MemberType().(*librarySdkMemberType).jarToExportGetter(ctx, j) |
| 755 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 756 | p.AidlIncludeDirs = j.AidlIncludeDirs() |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 757 | |
| 758 | p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars() |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 759 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 760 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 761 | func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 762 | builder := ctx.SnapshotBuilder() |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 763 | |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 764 | memberType := ctx.MemberType().(*librarySdkMemberType) |
| 765 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 766 | exportedJar := p.JarToExport |
| 767 | if exportedJar != nil { |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 768 | // Delegate the creation of the snapshot relative path to the member type. |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 769 | snapshotRelativeJavaLibPath := memberType.snapshotPathGetter(ctx, p.OsPrefix(), ctx.Name()) |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 770 | |
| 771 | // Copy the exported jar to the snapshot. |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 772 | builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath) |
| 773 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 774 | propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath}) |
| 775 | } |
| 776 | |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 777 | if len(p.PermittedPackages) > 0 { |
| 778 | propertySet.AddProperty("permitted_packages", p.PermittedPackages) |
| 779 | } |
| 780 | |
Paul Duffin | db170e4 | 2020-12-08 17:48:25 +0000 | [diff] [blame] | 781 | // Do not copy anything else to the snapshot. |
| 782 | if memberType.onlyCopyJarToSnapshot { |
| 783 | return |
| 784 | } |
| 785 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 786 | aidlIncludeDirs := p.AidlIncludeDirs |
| 787 | if len(aidlIncludeDirs) != 0 { |
| 788 | sdkModuleContext := ctx.SdkModuleContext() |
| 789 | for _, dir := range aidlIncludeDirs { |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 790 | // TODO(jiyong): copy parcelable declarations only |
| 791 | aidlFiles, _ := sdkModuleContext.GlobWithDeps(dir.String()+"/**/*.aidl", nil) |
| 792 | for _, file := range aidlFiles { |
| 793 | builder.CopyToSnapshot(android.PathForSource(sdkModuleContext, file), filepath.Join(aidlIncludeDir, file)) |
| 794 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 795 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 796 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 797 | // TODO(b/151933053) - add aidl include dirs property |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 798 | } |
Paul Duffin | 0e0cf1d | 2019-11-12 19:39:25 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 801 | // java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well. |
| 802 | // |
| 803 | // By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were |
| 804 | // compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used |
| 805 | // as a `static_libs` dependency of another module. |
| 806 | // |
| 807 | // Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on |
| 808 | // a device. |
| 809 | // |
| 810 | // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one |
| 811 | // compiled against the host bootclasspath. |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 812 | func LibraryFactory() android.Module { |
| 813 | module := &Library{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 814 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 815 | module.addHostAndDeviceProperties() |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 816 | |
Paul Duffin | 71b33cc | 2021-06-23 11:39:47 +0100 | [diff] [blame] | 817 | module.initModuleAndImport(module) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 818 | |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 819 | android.InitApexModule(module) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 820 | android.InitSdkAwareModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 821 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 822 | InitJavaModule(module, android.HostAndDeviceSupported) |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 823 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 826 | // java_library_static is an obsolete alias for java_library. |
| 827 | func LibraryStaticFactory() android.Module { |
| 828 | return LibraryFactory() |
| 829 | } |
| 830 | |
| 831 | // java_library_host builds and links sources into a `.jar` file for the host. |
| 832 | // |
| 833 | // A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were |
| 834 | // compiled against the host bootclasspath. |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 835 | func LibraryHostFactory() android.Module { |
| 836 | module := &Library{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 837 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 838 | module.addHostProperties() |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 839 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 840 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 841 | |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 842 | android.InitApexModule(module) |
Paul Duffin | b6b89a4 | 2021-05-06 16:33:43 +0100 | [diff] [blame] | 843 | android.InitSdkAwareModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 844 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 845 | InitJavaModule(module, android.HostSupported) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 846 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | // |
Colin Cross | b628ea5 | 2018-08-14 16:42:33 -0700 | [diff] [blame] | 850 | // Java Tests |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 851 | // |
| 852 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 853 | // Test option struct. |
| 854 | type TestOptions struct { |
| 855 | // a list of extra test configuration files that should be installed with the module. |
| 856 | Extra_test_configs []string `android:"path,arch_variant"` |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 857 | |
| 858 | // If the test is a hostside(no device required) unittest that shall be run during presubmit check. |
| 859 | Unit_test *bool |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 862 | type testProperties struct { |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 863 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 864 | // installed into. |
| 865 | Test_suites []string `android:"arch_variant"` |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 866 | |
| 867 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 868 | // installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 869 | Test_config *string `android:"path,arch_variant"` |
Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 870 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 871 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 872 | // should be installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 873 | Test_config_template *string `android:"path,arch_variant"` |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 874 | |
Colin Cross | d96ca35 | 2018-08-10 16:06:24 -0700 | [diff] [blame] | 875 | // list of files or filegroup modules that provide data that should be installed alongside |
| 876 | // the test |
Jiyong Park | 2b0e490 | 2021-02-16 06:52:39 +0900 | [diff] [blame] | 877 | Data []string `android:"path"` |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 878 | |
| 879 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 880 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 881 | // explicitly. |
| 882 | Auto_gen_config *bool |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 883 | |
| 884 | // Add parameterized mainline modules to auto generated test config. The options will be |
| 885 | // handled by TradeFed to do downloading and installing the specified modules on the device. |
| 886 | Test_mainline_modules []string |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 887 | |
| 888 | // Test options. |
| 889 | Test_options TestOptions |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 890 | |
| 891 | // Names of modules containing JNI libraries that should be installed alongside the test. |
| 892 | Jni_libs []string |
Colin Cross | cfb0f5e | 2021-09-24 15:47:17 -0700 | [diff] [blame] | 893 | |
| 894 | // Install the test into a folder named for the module in all test suites. |
| 895 | Per_testcase_directory *bool |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 898 | type hostTestProperties struct { |
| 899 | // list of native binary modules that should be installed alongside the test |
| 900 | Data_native_bins []string `android:"arch_variant"` |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 901 | |
| 902 | // list of device binary modules that should be installed alongside the test |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 903 | // This property only adds the first variant of the dependency |
| 904 | Data_device_bins_first []string `android:"arch_variant"` |
| 905 | |
| 906 | // list of device binary modules that should be installed alongside the test |
| 907 | // This property adds 64bit AND 32bit variants of the dependency |
| 908 | Data_device_bins_both []string `android:"arch_variant"` |
| 909 | |
| 910 | // list of device binary modules that should be installed alongside the test |
| 911 | // This property only adds 64bit variants of the dependency |
| 912 | Data_device_bins_64 []string `android:"arch_variant"` |
| 913 | |
| 914 | // list of device binary modules that should be installed alongside the test |
| 915 | // This property adds 32bit variants of the dependency if available, or else |
| 916 | // defaults to the 64bit variant |
| 917 | Data_device_bins_prefer32 []string `android:"arch_variant"` |
| 918 | |
| 919 | // list of device binary modules that should be installed alongside the test |
| 920 | // This property only adds 32bit variants of the dependency |
| 921 | Data_device_bins_32 []string `android:"arch_variant"` |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 924 | type testHelperLibraryProperties struct { |
| 925 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 926 | // installed into. |
| 927 | Test_suites []string `android:"arch_variant"` |
Colin Cross | cfb0f5e | 2021-09-24 15:47:17 -0700 | [diff] [blame] | 928 | |
| 929 | // Install the test into a folder named for the module in all test suites. |
| 930 | Per_testcase_directory *bool |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 933 | type prebuiltTestProperties struct { |
| 934 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 935 | // installed into. |
| 936 | Test_suites []string `android:"arch_variant"` |
| 937 | |
| 938 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 939 | // installed with the module. |
| 940 | Test_config *string `android:"path,arch_variant"` |
| 941 | } |
| 942 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 943 | type Test struct { |
| 944 | Library |
| 945 | |
| 946 | testProperties testProperties |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 947 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 948 | testConfig android.Path |
| 949 | extraTestConfigs android.Paths |
| 950 | data android.Paths |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 953 | type TestHost struct { |
| 954 | Test |
| 955 | |
| 956 | testHostProperties hostTestProperties |
| 957 | } |
| 958 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 959 | type TestHelperLibrary struct { |
| 960 | Library |
| 961 | |
| 962 | testHelperLibraryProperties testHelperLibraryProperties |
| 963 | } |
| 964 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 965 | type JavaTestImport struct { |
| 966 | Import |
| 967 | |
| 968 | prebuiltTestProperties prebuiltTestProperties |
| 969 | |
| 970 | testConfig android.Path |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 971 | dexJarFile android.Path |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Colin Cross | 24cc4be6 | 2021-11-03 14:09:41 -0700 | [diff] [blame] | 974 | func (j *Test) InstallInTestcases() bool { |
| 975 | // Host java tests install into $(HOST_OUT_JAVA_LIBRARIES), and then are copied into |
| 976 | // testcases by base_rules.mk. |
| 977 | return !j.Host() |
| 978 | } |
| 979 | |
| 980 | func (j *TestHelperLibrary) InstallInTestcases() bool { |
| 981 | return true |
| 982 | } |
| 983 | |
| 984 | func (j *JavaTestImport) InstallInTestcases() bool { |
| 985 | return true |
| 986 | } |
| 987 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 988 | func (j *TestHost) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext) { |
| 989 | if len(j.testHostProperties.Data_device_bins_first) > 0 { |
| 990 | deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations() |
| 991 | ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_first...) |
| 992 | } |
| 993 | |
| 994 | var maybeAndroid32Target *android.Target |
| 995 | var maybeAndroid64Target *android.Target |
| 996 | android32TargetList := android.FirstTarget(ctx.Config().Targets[android.Android], "lib32") |
| 997 | android64TargetList := android.FirstTarget(ctx.Config().Targets[android.Android], "lib64") |
| 998 | if len(android32TargetList) > 0 { |
| 999 | maybeAndroid32Target = &android32TargetList[0] |
| 1000 | } |
| 1001 | if len(android64TargetList) > 0 { |
| 1002 | maybeAndroid64Target = &android64TargetList[0] |
| 1003 | } |
| 1004 | |
| 1005 | if len(j.testHostProperties.Data_device_bins_both) > 0 { |
| 1006 | if maybeAndroid32Target == nil && maybeAndroid64Target == nil { |
| 1007 | ctx.PropertyErrorf("data_device_bins_both", "no device targets available. Targets: %q", ctx.Config().Targets) |
| 1008 | return |
| 1009 | } |
| 1010 | if maybeAndroid32Target != nil { |
| 1011 | ctx.AddFarVariationDependencies( |
| 1012 | maybeAndroid32Target.Variations(), |
| 1013 | dataDeviceBinsTag, |
| 1014 | j.testHostProperties.Data_device_bins_both..., |
| 1015 | ) |
| 1016 | } |
| 1017 | if maybeAndroid64Target != nil { |
| 1018 | ctx.AddFarVariationDependencies( |
| 1019 | maybeAndroid64Target.Variations(), |
| 1020 | dataDeviceBinsTag, |
| 1021 | j.testHostProperties.Data_device_bins_both..., |
| 1022 | ) |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if len(j.testHostProperties.Data_device_bins_prefer32) > 0 { |
| 1027 | if maybeAndroid32Target != nil { |
| 1028 | ctx.AddFarVariationDependencies( |
| 1029 | maybeAndroid32Target.Variations(), |
| 1030 | dataDeviceBinsTag, |
| 1031 | j.testHostProperties.Data_device_bins_prefer32..., |
| 1032 | ) |
| 1033 | } else { |
| 1034 | if maybeAndroid64Target == nil { |
| 1035 | ctx.PropertyErrorf("data_device_bins_prefer32", "no device targets available. Targets: %q", ctx.Config().Targets) |
| 1036 | return |
| 1037 | } |
| 1038 | ctx.AddFarVariationDependencies( |
| 1039 | maybeAndroid64Target.Variations(), |
| 1040 | dataDeviceBinsTag, |
| 1041 | j.testHostProperties.Data_device_bins_prefer32..., |
| 1042 | ) |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | if len(j.testHostProperties.Data_device_bins_32) > 0 { |
| 1047 | if maybeAndroid32Target == nil { |
| 1048 | ctx.PropertyErrorf("data_device_bins_32", "cannot find 32bit device target. Targets: %q", ctx.Config().Targets) |
| 1049 | return |
| 1050 | } |
| 1051 | deviceVariations := maybeAndroid32Target.Variations() |
| 1052 | ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_32...) |
| 1053 | } |
| 1054 | |
| 1055 | if len(j.testHostProperties.Data_device_bins_64) > 0 { |
| 1056 | if maybeAndroid64Target == nil { |
| 1057 | ctx.PropertyErrorf("data_device_bins_64", "cannot find 64bit device target. Targets: %q", ctx.Config().Targets) |
| 1058 | return |
| 1059 | } |
| 1060 | deviceVariations := maybeAndroid64Target.Variations() |
| 1061 | ctx.AddFarVariationDependencies(deviceVariations, dataDeviceBinsTag, j.testHostProperties.Data_device_bins_64...) |
| 1062 | } |
| 1063 | } |
| 1064 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1065 | func (j *TestHost) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 1066 | if len(j.testHostProperties.Data_native_bins) > 0 { |
| 1067 | for _, target := range ctx.MultiTargets() { |
| 1068 | ctx.AddVariationDependencies(target.Variations(), dataNativeBinsTag, j.testHostProperties.Data_native_bins...) |
| 1069 | } |
| 1070 | } |
| 1071 | |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 1072 | if len(j.testProperties.Jni_libs) > 0 { |
| 1073 | for _, target := range ctx.MultiTargets() { |
| 1074 | sharedLibVariations := append(target.Variations(), blueprint.Variation{Mutator: "link", Variation: "shared"}) |
| 1075 | ctx.AddFarVariationDependencies(sharedLibVariations, jniLibTag, j.testProperties.Jni_libs...) |
| 1076 | } |
| 1077 | } |
| 1078 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1079 | j.addDataDeviceBinsDeps(ctx) |
| 1080 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1081 | j.deps(ctx) |
| 1082 | } |
| 1083 | |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1084 | func (j *TestHost) AddExtraResource(p android.Path) { |
| 1085 | j.extraResources = append(j.extraResources, p) |
| 1086 | } |
| 1087 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1088 | func (j *TestHost) dataDeviceBins() []string { |
| 1089 | ret := make([]string, 0, |
| 1090 | len(j.testHostProperties.Data_device_bins_first)+ |
| 1091 | len(j.testHostProperties.Data_device_bins_both)+ |
| 1092 | len(j.testHostProperties.Data_device_bins_prefer32)+ |
| 1093 | len(j.testHostProperties.Data_device_bins_32)+ |
| 1094 | len(j.testHostProperties.Data_device_bins_64), |
| 1095 | ) |
| 1096 | |
| 1097 | ret = append(ret, j.testHostProperties.Data_device_bins_first...) |
| 1098 | ret = append(ret, j.testHostProperties.Data_device_bins_both...) |
| 1099 | ret = append(ret, j.testHostProperties.Data_device_bins_prefer32...) |
| 1100 | ret = append(ret, j.testHostProperties.Data_device_bins_32...) |
| 1101 | ret = append(ret, j.testHostProperties.Data_device_bins_64...) |
| 1102 | |
| 1103 | return ret |
| 1104 | } |
| 1105 | |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1106 | func (j *TestHost) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1107 | var configs []tradefed.Config |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1108 | dataDeviceBins := j.dataDeviceBins() |
| 1109 | if len(dataDeviceBins) > 0 { |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1110 | // add Tradefed configuration to push device bins to device for testing |
| 1111 | remoteDir := filepath.Join("/data/local/tests/unrestricted/", j.Name()) |
| 1112 | options := []tradefed.Option{{Name: "cleanup", Value: "true"}} |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1113 | for _, bin := range dataDeviceBins { |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1114 | fullPath := filepath.Join(remoteDir, bin) |
| 1115 | options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: fullPath}) |
| 1116 | } |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1117 | configs = append(configs, tradefed.Object{ |
| 1118 | Type: "target_preparer", |
| 1119 | Class: "com.android.tradefed.targetprep.PushFilePreparer", |
| 1120 | Options: options, |
| 1121 | }) |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | j.Test.generateAndroidBuildActionsWithConfig(ctx, configs) |
| 1125 | } |
| 1126 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1127 | func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1128 | j.generateAndroidBuildActionsWithConfig(ctx, nil) |
| 1129 | } |
| 1130 | |
| 1131 | func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext, configs []tradefed.Config) { |
Julien Desprez | b216661 | 2021-03-05 18:08:36 +0000 | [diff] [blame] | 1132 | if j.testProperties.Test_options.Unit_test == nil && ctx.Host() { |
| 1133 | // TODO(b/): Clean temporary heuristic to avoid unexpected onboarding. |
Julien Desprez | f666b15 | 2021-03-15 13:07:53 -0700 | [diff] [blame] | 1134 | defaultUnitTest := !inList("tradefed", j.properties.Libs) && !inList("cts", j.testProperties.Test_suites) |
Julien Desprez | b216661 | 2021-03-05 18:08:36 +0000 | [diff] [blame] | 1135 | j.testProperties.Test_options.Unit_test = proptools.BoolPtr(defaultUnitTest) |
| 1136 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1137 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 1138 | j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1139 | j.testProperties.Test_suites, configs, j.testProperties.Auto_gen_config, j.testProperties.Test_options.Unit_test) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1140 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1141 | j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1142 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 1143 | j.extraTestConfigs = android.PathsForModuleSrc(ctx, j.testProperties.Test_options.Extra_test_configs) |
| 1144 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1145 | ctx.VisitDirectDepsWithTag(dataNativeBinsTag, func(dep android.Module) { |
| 1146 | j.data = append(j.data, android.OutputFileForModule(ctx, dep, "")) |
| 1147 | }) |
| 1148 | |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1149 | ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) { |
| 1150 | j.data = append(j.data, android.OutputFileForModule(ctx, dep, "")) |
| 1151 | }) |
| 1152 | |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 1153 | ctx.VisitDirectDepsWithTag(jniLibTag, func(dep android.Module) { |
| 1154 | sharedLibInfo := ctx.OtherModuleProvider(dep, cc.SharedLibraryInfoProvider).(cc.SharedLibraryInfo) |
| 1155 | if sharedLibInfo.SharedLibrary != nil { |
| 1156 | // Copy to an intermediate output directory to append "lib[64]" to the path, |
| 1157 | // so that it's compatible with the default rpath values. |
| 1158 | var relPath string |
| 1159 | if sharedLibInfo.Target.Arch.ArchType.Multilib == "lib64" { |
| 1160 | relPath = filepath.Join("lib64", sharedLibInfo.SharedLibrary.Base()) |
| 1161 | } else { |
| 1162 | relPath = filepath.Join("lib", sharedLibInfo.SharedLibrary.Base()) |
| 1163 | } |
| 1164 | relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath) |
| 1165 | ctx.Build(pctx, android.BuildParams{ |
| 1166 | Rule: android.Cp, |
| 1167 | Input: sharedLibInfo.SharedLibrary, |
| 1168 | Output: relocatedLib, |
| 1169 | }) |
| 1170 | j.data = append(j.data, relocatedLib) |
| 1171 | } else { |
| 1172 | ctx.PropertyErrorf("jni_libs", "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep)) |
| 1173 | } |
| 1174 | }) |
| 1175 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1176 | j.Library.GenerateAndroidBuildActions(ctx) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1179 | func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1180 | j.Library.GenerateAndroidBuildActions(ctx) |
| 1181 | } |
| 1182 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1183 | func (j *JavaTestImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1184 | j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.prebuiltTestProperties.Test_config, nil, |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1185 | j.prebuiltTestProperties.Test_suites, nil, nil, nil) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1186 | |
| 1187 | j.Import.GenerateAndroidBuildActions(ctx) |
| 1188 | } |
| 1189 | |
| 1190 | type testSdkMemberType struct { |
| 1191 | android.SdkMemberTypeBase |
| 1192 | } |
| 1193 | |
Paul Duffin | 296701e | 2021-07-14 10:29:36 +0100 | [diff] [blame] | 1194 | func (mt *testSdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { |
| 1195 | ctx.AddVariationDependencies(nil, dependencyTag, names...) |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | func (mt *testSdkMemberType) IsInstance(module android.Module) bool { |
| 1199 | _, ok := module.(*Test) |
| 1200 | return ok |
| 1201 | } |
| 1202 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1203 | func (mt *testSdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { |
| 1204 | return ctx.SnapshotBuilder().AddPrebuiltModule(member, "java_test_import") |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1205 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1206 | |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1207 | func (mt *testSdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { |
| 1208 | return &testSdkMemberProperties{} |
| 1209 | } |
| 1210 | |
| 1211 | type testSdkMemberProperties struct { |
| 1212 | android.SdkMemberPropertiesBase |
| 1213 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1214 | JarToExport android.Path |
| 1215 | TestConfig android.Path |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1218 | func (p *testSdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1219 | test := variant.(*Test) |
| 1220 | |
| 1221 | implementationJars := test.ImplementationJars() |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1222 | if len(implementationJars) != 1 { |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1223 | 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] | 1224 | } |
| 1225 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1226 | p.JarToExport = implementationJars[0] |
| 1227 | p.TestConfig = test.testConfig |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1228 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1229 | |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1230 | func (p *testSdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1231 | builder := ctx.SnapshotBuilder() |
Paul Duffin | 3a4eb50 | 2020-03-19 16:11:18 +0000 | [diff] [blame] | 1232 | |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1233 | exportedJar := p.JarToExport |
| 1234 | if exportedJar != nil { |
Paul Duffin | 1364891 | 2022-07-15 13:12:35 +0000 | [diff] [blame] | 1235 | snapshotRelativeJavaLibPath := sdkSnapshotFilePathForJar(ctx, p.OsPrefix(), ctx.Name()) |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1236 | builder.CopyToSnapshot(exportedJar, snapshotRelativeJavaLibPath) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1237 | |
| 1238 | propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath}) |
Paul Duffin | a551a1c | 2020-03-17 21:04:24 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | testConfig := p.TestConfig |
| 1242 | if testConfig != nil { |
| 1243 | snapshotRelativeTestConfigPath := sdkSnapshotFilePathForMember(p.OsPrefix(), ctx.Name(), testConfigSuffix) |
| 1244 | builder.CopyToSnapshot(testConfig, snapshotRelativeTestConfigPath) |
Paul Duffin | 14eb467 | 2020-03-02 11:33:02 +0000 | [diff] [blame] | 1245 | propertySet.AddProperty("test_config", snapshotRelativeTestConfigPath) |
| 1246 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1249 | // java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and |
| 1250 | // creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file. |
| 1251 | // |
| 1252 | // By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were |
| 1253 | // compiled against the device bootclasspath. |
| 1254 | // |
| 1255 | // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one |
| 1256 | // compiled against the host bootclasspath. |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1257 | func TestFactory() android.Module { |
| 1258 | module := &Test{} |
| 1259 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1260 | module.addHostAndDeviceProperties() |
| 1261 | module.AddProperties(&module.testProperties) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1262 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1263 | module.Module.properties.Installable = proptools.BoolPtr(true) |
Colin Cross | e302687 | 2019-01-05 22:30:13 -0800 | [diff] [blame] | 1264 | module.Module.dexpreopter.isTest = true |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 1265 | module.Module.linter.test = true |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1266 | |
Paul Duffin | b6b89a4 | 2021-05-06 16:33:43 +0100 | [diff] [blame] | 1267 | android.InitSdkAwareModule(module) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1268 | InitJavaModule(module, android.HostAndDeviceSupported) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1269 | return module |
| 1270 | } |
| 1271 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1272 | // java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite. |
| 1273 | func TestHelperLibraryFactory() android.Module { |
| 1274 | module := &TestHelperLibrary{} |
| 1275 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1276 | module.addHostAndDeviceProperties() |
| 1277 | module.AddProperties(&module.testHelperLibraryProperties) |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1278 | |
Colin Cross | 9a4abed | 2019-04-24 13:19:28 -0700 | [diff] [blame] | 1279 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 1280 | module.Module.dexpreopter.isTest = true |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 1281 | module.Module.linter.test = true |
Colin Cross | 9a4abed | 2019-04-24 13:19:28 -0700 | [diff] [blame] | 1282 | |
Paul Duffin | 42df144 | 2019-03-20 12:45:53 +0000 | [diff] [blame] | 1283 | InitJavaModule(module, android.HostAndDeviceSupported) |
| 1284 | return module |
| 1285 | } |
| 1286 | |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 1287 | // java_test_import imports one or more `.jar` files into the build graph as if they were built by a java_test module |
| 1288 | // and makes sure that it is added to the appropriate test suite. |
| 1289 | // |
| 1290 | // By default, a java_test_import has a single variant that expects a `.jar` file containing `.class` files that were |
| 1291 | // compiled against an Android classpath. |
| 1292 | // |
| 1293 | // Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one |
| 1294 | // for host modules. |
| 1295 | func JavaTestImportFactory() android.Module { |
| 1296 | module := &JavaTestImport{} |
| 1297 | |
| 1298 | module.AddProperties( |
| 1299 | &module.Import.properties, |
| 1300 | &module.prebuiltTestProperties) |
| 1301 | |
| 1302 | module.Import.properties.Installable = proptools.BoolPtr(true) |
| 1303 | |
| 1304 | android.InitPrebuiltModule(module, &module.properties.Jars) |
| 1305 | android.InitApexModule(module) |
| 1306 | android.InitSdkAwareModule(module) |
| 1307 | InitJavaModule(module, android.HostAndDeviceSupported) |
| 1308 | return module |
| 1309 | } |
| 1310 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1311 | // java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to |
| 1312 | // allow running the test with `atest` or a `TEST_MAPPING` file. |
| 1313 | // |
| 1314 | // A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were |
| 1315 | // compiled against the host bootclasspath. |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1316 | func TestHostFactory() android.Module { |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1317 | module := &TestHost{} |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1318 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1319 | module.addHostProperties() |
| 1320 | module.AddProperties(&module.testProperties) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1321 | module.AddProperties(&module.testHostProperties) |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1322 | |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1323 | InitTestHost( |
| 1324 | module, |
| 1325 | proptools.BoolPtr(true), |
| 1326 | nil, |
| 1327 | nil) |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1328 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1329 | InitJavaModuleMultiTargets(module, android.HostSupported) |
Julien Desprez | b216661 | 2021-03-05 18:08:36 +0000 | [diff] [blame] | 1330 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1331 | return module |
| 1332 | } |
| 1333 | |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1334 | func InitTestHost(th *TestHost, installable *bool, testSuites []string, autoGenConfig *bool) { |
| 1335 | th.properties.Installable = installable |
| 1336 | th.testProperties.Auto_gen_config = autoGenConfig |
| 1337 | th.testProperties.Test_suites = testSuites |
| 1338 | } |
| 1339 | |
Colin Cross | 05638fc | 2018-04-09 18:40:24 -0700 | [diff] [blame] | 1340 | // |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1341 | // Java Binaries (.jar file plus wrapper script) |
| 1342 | // |
| 1343 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1344 | type binaryProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1345 | // installable script to execute the resulting jar |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1346 | Wrapper *string `android:"path,arch_variant"` |
Colin Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 1347 | |
| 1348 | // Name of the class containing main to be inserted into the manifest as Main-Class. |
| 1349 | Main_class *string |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 1350 | |
| 1351 | // Names of modules containing JNI libraries that should be installed alongside the host |
| 1352 | // variant of the binary. |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1353 | Jni_libs []string `android:"arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1356 | type Binary struct { |
| 1357 | Library |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1358 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1359 | binaryProperties binaryProperties |
Colin Cross | 10a0349 | 2017-08-10 17:09:43 -0700 | [diff] [blame] | 1360 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1361 | isWrapperVariant bool |
| 1362 | |
Colin Cross | c331599 | 2017-12-08 19:12:36 -0800 | [diff] [blame] | 1363 | wrapperFile android.Path |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1364 | binaryFile android.InstallPath |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Alex Light | 2423717 | 2017-10-26 09:46:21 -0700 | [diff] [blame] | 1367 | func (j *Binary) HostToolPath() android.OptionalPath { |
| 1368 | return android.OptionalPathForPath(j.binaryFile) |
| 1369 | } |
| 1370 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1371 | func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1372 | if ctx.Arch().ArchType == android.Common { |
| 1373 | // Compile the jar |
Colin Cross | 094054a | 2018-10-17 15:10:48 -0700 | [diff] [blame] | 1374 | if j.binaryProperties.Main_class != nil { |
| 1375 | if j.properties.Manifest != nil { |
| 1376 | ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set") |
| 1377 | } |
| 1378 | manifestFile := android.PathForModuleOut(ctx, "manifest.txt") |
| 1379 | GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class)) |
| 1380 | j.overrideManifest = android.OptionalPathForPath(manifestFile) |
| 1381 | } |
| 1382 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1383 | j.Library.GenerateAndroidBuildActions(ctx) |
Nan Zhang | 3c807db | 2017-11-03 14:53:31 -0700 | [diff] [blame] | 1384 | } else { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1385 | // Handle the binary wrapper |
| 1386 | j.isWrapperVariant = true |
| 1387 | |
Colin Cross | 366938f | 2017-12-11 16:29:02 -0800 | [diff] [blame] | 1388 | if j.binaryProperties.Wrapper != nil { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1389 | j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1390 | } else { |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1391 | if ctx.Windows() { |
| 1392 | ctx.PropertyErrorf("wrapper", "wrapper is required for Windows") |
| 1393 | } |
| 1394 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1395 | j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh") |
| 1396 | } |
| 1397 | |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1398 | ext := "" |
| 1399 | if ctx.Windows() { |
| 1400 | ext = ".bat" |
| 1401 | } |
| 1402 | |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 1403 | // 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] | 1404 | // of the wrapper variant, which will include the common variant's jar file and any JNI |
| 1405 | // libraries. This is verified by TestBinary. |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1406 | j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"), |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1407 | ctx.ModuleName()+ext, j.wrapperFile) |
| 1408 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1411 | func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) { |
Liz Kammer | 3bf97bd | 2022-04-26 09:38:20 -0400 | [diff] [blame] | 1412 | if ctx.Arch().ArchType == android.Common { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1413 | j.deps(ctx) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1414 | } |
Liz Kammer | 3bf97bd | 2022-04-26 09:38:20 -0400 | [diff] [blame] | 1415 | if ctx.Arch().ArchType != android.Common { |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 1416 | // These dependencies ensure the host installation rules will install the jar file and |
| 1417 | // the jni libraries when the wrapper is installed. |
| 1418 | ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...) |
| 1419 | ctx.AddVariationDependencies( |
| 1420 | []blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}}, |
| 1421 | binaryInstallTag, ctx.ModuleName()) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1422 | } |
Colin Cross | 46c9b8b | 2017-06-22 16:51:17 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1425 | // java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host |
| 1426 | // as well. |
| 1427 | // |
| 1428 | // By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were |
| 1429 | // compiled against the device bootclasspath. |
| 1430 | // |
| 1431 | // Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one |
| 1432 | // compiled against the host bootclasspath. |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1433 | func BinaryFactory() android.Module { |
| 1434 | module := &Binary{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1435 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1436 | module.addHostAndDeviceProperties() |
| 1437 | module.AddProperties(&module.binaryProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1438 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1439 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 1440 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1441 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst) |
| 1442 | android.InitDefaultableModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 1443 | android.InitBazelModule(module) |
| 1444 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1445 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1448 | // java_binary_host builds a `.jar` file and a shell script that executes it for the host. |
| 1449 | // |
| 1450 | // A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were |
| 1451 | // compiled against the host bootclasspath. |
Colin Cross | f506d87 | 2017-07-19 15:53:04 -0700 | [diff] [blame] | 1452 | func BinaryHostFactory() android.Module { |
| 1453 | module := &Binary{} |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1454 | |
Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 1455 | module.addHostProperties() |
| 1456 | module.AddProperties(&module.binaryProperties) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1457 | |
Colin Cross | 9ae1b92 | 2018-06-26 17:59:05 -0700 | [diff] [blame] | 1458 | module.Module.properties.Installable = proptools.BoolPtr(true) |
| 1459 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 1460 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst) |
| 1461 | android.InitDefaultableModule(module) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 1462 | android.InitBazelModule(module) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1463 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | // |
| 1467 | // Java prebuilts |
| 1468 | // |
| 1469 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1470 | type ImportProperties struct { |
Paul Duffin | a04c107 | 2020-03-02 10:16:35 +0000 | [diff] [blame] | 1471 | Jars []string `android:"path,arch_variant"` |
Colin Cross | 461bd1a | 2017-10-20 13:59:18 -0700 | [diff] [blame] | 1472 | |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1473 | // The version of the SDK that the source prebuilt file was built against. Defaults to the |
| 1474 | // current version if not specified. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 1475 | Sdk_version *string |
Colin Cross | 535e2cf | 2017-10-20 17:57:49 -0700 | [diff] [blame] | 1476 | |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1477 | // The minimum version of the SDK that this module supports. Defaults to sdk_version if not |
| 1478 | // specified. |
| 1479 | Min_sdk_version *string |
| 1480 | |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 1481 | // The max sdk version placeholder used to replace maxSdkVersion attributes on permission |
| 1482 | // and uses-permission tags in manifest_fixer. |
| 1483 | Replace_max_sdk_version_placeholder *string |
| 1484 | |
Colin Cross | 535e2cf | 2017-10-20 17:57:49 -0700 | [diff] [blame] | 1485 | Installable *bool |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1486 | |
Paul Duffin | 869de14 | 2021-07-15 14:14:41 +0100 | [diff] [blame] | 1487 | // 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] | 1488 | Permitted_packages []string |
| 1489 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1490 | // List of shared java libs that this module has dependencies to |
| 1491 | Libs []string |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 1492 | |
| 1493 | // List of files to remove from the jar file(s) |
| 1494 | Exclude_files []string |
| 1495 | |
| 1496 | // List of directories to remove from the jar file(s) |
| 1497 | Exclude_dirs []string |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 1498 | |
| 1499 | // if set to true, run Jetifier against .jar file. Defaults to false. |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 1500 | Jetifier *bool |
Jiyong Park | 4c4c024 | 2019-10-21 14:53:15 +0900 | [diff] [blame] | 1501 | |
| 1502 | // set the name of the output |
| 1503 | Stem *string |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 1504 | |
| 1505 | Aidl struct { |
| 1506 | // directories that should be added as include directories for any aidl sources of modules |
| 1507 | // that depend on this module, as well as to aidl for this module. |
| 1508 | Export_include_dirs []string |
| 1509 | } |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | type Import struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1513 | android.ModuleBase |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 1514 | android.DefaultableModuleBase |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 1515 | android.ApexModuleBase |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 1516 | android.BazelModuleBase |
Colin Cross | ec7a042 | 2017-07-07 14:47:12 -0700 | [diff] [blame] | 1517 | prebuilt android.Prebuilt |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 1518 | android.SdkBase |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1519 | |
Paul Duffin | 0d3c2e1 | 2020-05-17 08:34:50 +0100 | [diff] [blame] | 1520 | // Functionality common to Module and Import. |
| 1521 | embeddableInModuleAndImport |
| 1522 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1523 | hiddenAPI |
| 1524 | dexer |
Bill Peckham | ff89ffa | 2020-12-23 16:13:04 -0800 | [diff] [blame] | 1525 | dexpreopter |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1526 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1527 | properties ImportProperties |
| 1528 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1529 | // output file containing classes.dex and resources |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 1530 | dexJarFile OptionalDexJarPath |
Jeongik Cha | d5fe878 | 2021-07-08 01:13:11 +0900 | [diff] [blame] | 1531 | dexJarInstallFile android.Path |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1532 | |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 1533 | combinedClasspathFile android.Path |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 1534 | classLoaderContexts dexpreopt.ClassLoaderContextMap |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 1535 | exportAidlIncludeDirs android.Paths |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 1536 | |
| 1537 | hideApexVariantFromMake bool |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1538 | |
| 1539 | sdkVersion android.SdkSpec |
| 1540 | minSdkVersion android.SdkSpec |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Paul Duffin | 630b11e | 2021-07-15 13:35:26 +0100 | [diff] [blame] | 1543 | var _ PermittedPackagesForUpdatableBootJars = (*Import)(nil) |
| 1544 | |
| 1545 | func (j *Import) PermittedPackagesForUpdatableBootJars() []string { |
| 1546 | return j.properties.Permitted_packages |
| 1547 | } |
| 1548 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1549 | func (j *Import) SdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { |
| 1550 | return android.SdkSpecFrom(ctx, String(j.properties.Sdk_version)) |
Liz Kammer | 2d2fd85 | 2020-08-12 14:42:30 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1553 | func (j *Import) SystemModules() string { |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1554 | return "none" |
| 1555 | } |
| 1556 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1557 | func (j *Import) MinSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1558 | if j.properties.Min_sdk_version != nil { |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1559 | return android.SdkSpecFrom(ctx, *j.properties.Min_sdk_version) |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1560 | } |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1561 | return j.SdkVersion(ctx) |
Colin Cross | 83bb316 | 2018-06-25 15:48:06 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
William Loh | 5a082f9 | 2022-05-17 20:21:50 +0000 | [diff] [blame] | 1564 | func (j *Import) ReplaceMaxSdkVersionPlaceholder(ctx android.EarlyModuleContext) android.SdkSpec { |
| 1565 | if j.properties.Replace_max_sdk_version_placeholder != nil { |
| 1566 | return android.SdkSpecFrom(ctx, *j.properties.Replace_max_sdk_version_placeholder) |
| 1567 | } |
| 1568 | return android.SdkSpecFrom(ctx, "") |
| 1569 | } |
| 1570 | |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1571 | func (j *Import) TargetSdkVersion(ctx android.EarlyModuleContext) android.SdkSpec { |
| 1572 | return j.SdkVersion(ctx) |
Artur Satayev | 480e25b | 2020-04-27 18:53:18 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1575 | func (j *Import) Prebuilt() *android.Prebuilt { |
Colin Cross | ec7a042 | 2017-07-07 14:47:12 -0700 | [diff] [blame] | 1576 | return &j.prebuilt |
| 1577 | } |
| 1578 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1579 | func (j *Import) PrebuiltSrcs() []string { |
| 1580 | return j.properties.Jars |
| 1581 | } |
| 1582 | |
| 1583 | func (j *Import) Name() string { |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 1584 | return j.prebuilt.Name(j.ModuleBase.Name()) |
| 1585 | } |
| 1586 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 1587 | func (j *Import) Stem() string { |
| 1588 | return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name()) |
| 1589 | } |
| 1590 | |
Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 1591 | func (a *Import) JacocoReportClassesFile() android.Path { |
| 1592 | return nil |
| 1593 | } |
| 1594 | |
Bill Peckham | a41a696 | 2021-01-11 10:58:54 -0800 | [diff] [blame] | 1595 | func (j *Import) LintDepSets() LintDepSets { |
| 1596 | return LintDepSets{} |
| 1597 | } |
| 1598 | |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 1599 | func (j *Import) getStrictUpdatabilityLinting() bool { |
| 1600 | return false |
| 1601 | } |
| 1602 | |
| 1603 | func (j *Import) setStrictUpdatabilityLinting(bool) { |
| 1604 | } |
| 1605 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1606 | func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 42d48b7 | 2018-08-29 14:10:52 -0700 | [diff] [blame] | 1607 | ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1608 | |
| 1609 | if ctx.Device() && Bool(j.dexProperties.Compile_dex) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1610 | sdkDeps(ctx, android.SdkContext(j), j.dexer) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1611 | } |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 1614 | func (j *Import) commonBuildActions(ctx android.ModuleContext) { |
| 1615 | //TODO(b/231322772) these should come from Bazel once available |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1616 | j.sdkVersion = j.SdkVersion(ctx) |
| 1617 | j.minSdkVersion = j.MinSdkVersion(ctx) |
| 1618 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 1619 | if !ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).IsForPlatform() { |
| 1620 | j.hideApexVariantFromMake = true |
| 1621 | } |
| 1622 | |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1623 | if ctx.Windows() { |
| 1624 | j.HideFromMake() |
| 1625 | } |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1629 | j.commonBuildActions(ctx) |
Dan Willemsen | 8e6b371 | 2021-09-20 23:11:24 -0700 | [diff] [blame] | 1630 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1631 | jars := android.PathsForModuleSrc(ctx, j.properties.Jars) |
Colin Cross | e1d62a8 | 2015-04-03 16:53:05 -0700 | [diff] [blame] | 1632 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 1633 | jarName := j.Stem() + ".jar" |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 1634 | outputFile := android.PathForModuleOut(ctx, "combined", jarName) |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 1635 | TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{}, |
| 1636 | false, j.properties.Exclude_files, j.properties.Exclude_dirs) |
Colin Cross | 1001a79 | 2019-03-21 22:21:39 -0700 | [diff] [blame] | 1637 | if Bool(j.properties.Jetifier) { |
Nan Zhang | 4c819fb | 2018-08-27 18:31:46 -0700 | [diff] [blame] | 1638 | inputFile := outputFile |
| 1639 | outputFile = android.PathForModuleOut(ctx, "jetifier", jarName) |
| 1640 | TransformJetifier(ctx, outputFile, inputFile) |
| 1641 | } |
Colin Cross | e9a275b | 2017-10-16 17:09:48 -0700 | [diff] [blame] | 1642 | j.combinedClasspathFile = outputFile |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 1643 | j.classLoaderContexts = make(dexpreopt.ClassLoaderContextMap) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1644 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1645 | var flags javaBuilderFlags |
| 1646 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1647 | ctx.VisitDirectDeps(func(module android.Module) { |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1648 | tag := ctx.OtherModuleDependencyTag(module) |
| 1649 | |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1650 | if ctx.OtherModuleHasProvider(module, JavaInfoProvider) { |
| 1651 | dep := ctx.OtherModuleProvider(module, JavaInfoProvider).(JavaInfo) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1652 | switch tag { |
Colin Cross | 9bb9bfb | 2022-03-17 11:12:32 -0700 | [diff] [blame] | 1653 | case libTag: |
| 1654 | flags.classpath = append(flags.classpath, dep.HeaderJars...) |
| 1655 | flags.dexClasspath = append(flags.dexClasspath, dep.HeaderJars...) |
| 1656 | case staticLibTag: |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1657 | flags.classpath = append(flags.classpath, dep.HeaderJars...) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1658 | case bootClasspathTag: |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1659 | flags.bootClasspath = append(flags.bootClasspath, dep.HeaderJars...) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1660 | } |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1661 | } else if dep, ok := module.(SdkLibraryDependency); ok { |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1662 | switch tag { |
| 1663 | case libTag: |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1664 | flags.classpath = append(flags.classpath, dep.SdkHeaderJars(ctx, j.SdkVersion(ctx))...) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1665 | } |
| 1666 | } |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 1667 | |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 1668 | addCLCFromDep(ctx, module, j.classLoaderContexts) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1669 | }) |
| 1670 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 1671 | j.maybeInstall(ctx, jarName, outputFile) |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 1672 | |
| 1673 | j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.properties.Aidl.Export_include_dirs) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1674 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1675 | if ctx.Device() { |
| 1676 | // If this is a variant created for a prebuilt_apex then use the dex implementation jar |
| 1677 | // obtained from the associated deapexer module. |
| 1678 | ai := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 1679 | if ai.ForPrebuiltApex { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1680 | // Get the path of the dex implementation jar from the `deapexer` module. |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 1681 | di := android.FindDeapexerProviderForModule(ctx) |
| 1682 | if di == nil { |
| 1683 | return // An error has been reported by FindDeapexerProviderForModule. |
| 1684 | } |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 1685 | if dexOutputPath := di.PrebuiltExportPath(apexRootRelativePathToJavaLib(j.BaseModuleName())); dexOutputPath != nil { |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 1686 | dexJarFile := makeDexJarPathFromPath(dexOutputPath) |
| 1687 | j.dexJarFile = dexJarFile |
Jiakai Zhang | 5b24f72 | 2021-09-30 09:32:57 +0000 | [diff] [blame] | 1688 | installPath := android.PathForModuleInPartitionInstall(ctx, "apex", ai.ApexVariationName, apexRootRelativePathToJavaLib(j.BaseModuleName())) |
| 1689 | j.dexJarInstallFile = installPath |
Paul Duffin | 74d18d1 | 2021-05-14 14:18:47 +0100 | [diff] [blame] | 1690 | |
Jiakai Zhang | 5b24f72 | 2021-09-30 09:32:57 +0000 | [diff] [blame] | 1691 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath(ctx, installPath) |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 1692 | setUncompressDex(ctx, &j.dexpreopter, &j.dexer) |
Jiakai Zhang | 5b24f72 | 2021-09-30 09:32:57 +0000 | [diff] [blame] | 1693 | j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex |
| 1694 | j.dexpreopt(ctx, dexOutputPath) |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 1695 | |
| 1696 | // Initialize the hiddenapi structure. |
| 1697 | j.initHiddenAPI(ctx, dexJarFile, outputFile, j.dexProperties.Uncompress_dex) |
Paul Duffin | 9d67ca6 | 2021-02-03 20:06:33 +0000 | [diff] [blame] | 1698 | } else { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1699 | // This should never happen as a variant for a prebuilt_apex is only created if the |
| 1700 | // prebuilt_apex has been configured to export the java library dex file. |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 1701 | 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] | 1702 | } |
| 1703 | } else if Bool(j.dexProperties.Compile_dex) { |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1704 | sdkDep := decodeSdkDep(ctx, android.SdkContext(j)) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1705 | if sdkDep.invalidVersion { |
| 1706 | ctx.AddMissingDependencies(sdkDep.bootclasspath) |
| 1707 | ctx.AddMissingDependencies(sdkDep.java9Classpath) |
| 1708 | } else if sdkDep.useFiles { |
| 1709 | // sdkDep.jar is actually equivalent to turbine header.jar. |
| 1710 | flags.classpath = append(flags.classpath, sdkDep.jars...) |
| 1711 | } |
| 1712 | |
| 1713 | // Dex compilation |
| 1714 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 1715 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath( |
| 1716 | ctx, android.PathForModuleInstall(ctx, "framework", jarName)) |
Jiakai Zhang | 22450f2 | 2021-10-11 03:05:20 +0000 | [diff] [blame] | 1717 | setUncompressDex(ctx, &j.dexpreopter, &j.dexer) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1718 | j.dexpreopter.uncompressedDex = *j.dexProperties.Uncompress_dex |
| 1719 | |
Paul Duffin | 612e610 | 2021-02-02 13:38:13 +0000 | [diff] [blame] | 1720 | var dexOutputFile android.OutputPath |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1721 | dexOutputFile = j.dexer.compileDex(ctx, flags, j.MinSdkVersion(ctx), outputFile, jarName) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1722 | if ctx.Failed() { |
| 1723 | return |
| 1724 | } |
| 1725 | |
Paul Duffin | 74d18d1 | 2021-05-14 14:18:47 +0100 | [diff] [blame] | 1726 | // Initialize the hiddenapi structure. |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 1727 | j.initHiddenAPI(ctx, makeDexJarPathFromPath(dexOutputFile), outputFile, j.dexProperties.Uncompress_dex) |
Paul Duffin | afaa47c | 2021-05-14 13:04:04 +0100 | [diff] [blame] | 1728 | |
| 1729 | // Encode hidden API flags in dex file. |
Paul Duffin | 1bbd062 | 2021-05-14 15:52:25 +0100 | [diff] [blame] | 1730 | dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1731 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 1732 | j.dexJarFile = makeDexJarPathFromPath(dexOutputFile) |
Jeongik Cha | d5fe878 | 2021-07-08 01:13:11 +0900 | [diff] [blame] | 1733 | j.dexJarInstallFile = android.PathForModuleInstall(ctx, "framework", jarName) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1734 | } |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1735 | } |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 1736 | |
| 1737 | ctx.SetProvider(JavaInfoProvider, JavaInfo{ |
| 1738 | HeaderJars: android.PathsIfNonNil(j.combinedClasspathFile), |
| 1739 | ImplementationAndResourcesJars: android.PathsIfNonNil(j.combinedClasspathFile), |
| 1740 | ImplementationJars: android.PathsIfNonNil(j.combinedClasspathFile), |
| 1741 | AidlIncludeDirs: j.exportAidlIncludeDirs, |
| 1742 | }) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 1745 | func (j *Import) maybeInstall(ctx android.ModuleContext, jarName string, outputFile android.Path) { |
| 1746 | if !Bool(j.properties.Installable) { |
| 1747 | return |
| 1748 | } |
| 1749 | |
| 1750 | var installDir android.InstallPath |
| 1751 | if ctx.InstallInTestcases() { |
| 1752 | var archDir string |
| 1753 | if !ctx.Host() { |
| 1754 | archDir = ctx.DeviceConfig().DeviceArch() |
| 1755 | } |
| 1756 | installDir = android.PathForModuleInstall(ctx, ctx.ModuleName(), archDir) |
| 1757 | } else { |
| 1758 | installDir = android.PathForModuleInstall(ctx, "framework") |
| 1759 | } |
| 1760 | ctx.InstallFile(installDir, jarName, outputFile) |
| 1761 | } |
| 1762 | |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1763 | func (j *Import) OutputFiles(tag string) (android.Paths, error) { |
| 1764 | switch tag { |
Saeid Farivar Asanjan | 128fe5c | 2020-10-15 17:54:40 +0000 | [diff] [blame] | 1765 | case "", ".jar": |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1766 | return android.Paths{j.combinedClasspathFile}, nil |
| 1767 | default: |
| 1768 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | var _ android.OutputFileProducer = (*Import)(nil) |
| 1773 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 1774 | func (j *Import) HeaderJars() android.Paths { |
albaltai | 36ff7dc | 2018-12-25 14:35:23 +0800 | [diff] [blame] | 1775 | if j.combinedClasspathFile == nil { |
| 1776 | return nil |
| 1777 | } |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 1778 | return android.Paths{j.combinedClasspathFile} |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 1781 | func (j *Import) ImplementationAndResourcesJars() android.Paths { |
albaltai | 36ff7dc | 2018-12-25 14:35:23 +0800 | [diff] [blame] | 1782 | if j.combinedClasspathFile == nil { |
| 1783 | return nil |
| 1784 | } |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 1785 | return android.Paths{j.combinedClasspathFile} |
| 1786 | } |
| 1787 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 1788 | func (j *Import) DexJarBuildPath() OptionalDexJarPath { |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1789 | return j.dexJarFile |
Colin Cross | f24a22a | 2019-01-31 14:12:44 -0800 | [diff] [blame] | 1790 | } |
| 1791 | |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 1792 | func (j *Import) DexJarInstallPath() android.Path { |
Jeongik Cha | d5fe878 | 2021-07-08 01:13:11 +0900 | [diff] [blame] | 1793 | return j.dexJarInstallFile |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 1794 | } |
| 1795 | |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 1796 | func (j *Import) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { |
| 1797 | return j.classLoaderContexts |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1798 | } |
| 1799 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 1800 | var _ android.ApexModule = (*Import)(nil) |
| 1801 | |
| 1802 | // Implements android.ApexModule |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 1803 | func (j *Import) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool { |
Paul Duffin | 0d3c2e1 | 2020-05-17 08:34:50 +0100 | [diff] [blame] | 1804 | return j.depIsInSameApex(ctx, dep) |
Jiyong Park | 0f80c18 | 2020-01-31 02:49:53 +0900 | [diff] [blame] | 1805 | } |
| 1806 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 1807 | // Implements android.ApexModule |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 1808 | func (j *Import) ShouldSupportSdkVersion(ctx android.BaseModuleContext, |
| 1809 | sdkVersion android.ApiLevel) error { |
Jiyong Park | 9231537 | 2021-04-02 08:45:46 +0900 | [diff] [blame] | 1810 | sdkSpec := j.MinSdkVersion(ctx) |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1811 | if !sdkSpec.Specified() { |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1812 | return fmt.Errorf("min_sdk_version is not specified") |
| 1813 | } |
Jiyong Park | f1691d2 | 2021-03-29 20:11:58 +0900 | [diff] [blame] | 1814 | if sdkSpec.Kind == android.SdkCore { |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1815 | return nil |
| 1816 | } |
Jooyung Han | 4c4da06 | 2021-06-23 10:23:16 +0900 | [diff] [blame] | 1817 | if sdkSpec.ApiLevel.GreaterThan(sdkVersion) { |
| 1818 | return fmt.Errorf("newer SDK(%v)", sdkSpec.ApiLevel) |
Jaewoong Jung | 56e12db | 2021-04-02 00:38:25 +0000 | [diff] [blame] | 1819 | } |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 1820 | return nil |
| 1821 | } |
| 1822 | |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 1823 | // requiredFilesFromPrebuiltApexForImport returns information about the files that a java_import or |
| 1824 | // java_sdk_library_import with the specified base module name requires to be exported from a |
| 1825 | // prebuilt_apex/apex_set. |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 1826 | func requiredFilesFromPrebuiltApexForImport(name string) []string { |
| 1827 | // Add the dex implementation jar to the set of exported files. |
| 1828 | return []string{ |
| 1829 | apexRootRelativePathToJavaLib(name), |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 1833 | // apexRootRelativePathToJavaLib returns the path, relative to the root of the apex's contents, for |
| 1834 | // the java library with the specified name. |
| 1835 | func apexRootRelativePathToJavaLib(name string) string { |
| 1836 | return filepath.Join("javalib", name+".jar") |
| 1837 | } |
| 1838 | |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 1839 | var _ android.RequiredFilesFromPrebuiltApex = (*Import)(nil) |
| 1840 | |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 1841 | func (j *Import) RequiredFilesFromPrebuiltApex(_ android.BaseModuleContext) []string { |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 1842 | name := j.BaseModuleName() |
| 1843 | return requiredFilesFromPrebuiltApexForImport(name) |
| 1844 | } |
| 1845 | |
albaltai | 36ff7dc | 2018-12-25 14:35:23 +0800 | [diff] [blame] | 1846 | // Add compile time check for interface implementation |
| 1847 | var _ android.IDEInfo = (*Import)(nil) |
| 1848 | var _ android.IDECustomizedModuleName = (*Import)(nil) |
| 1849 | |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 1850 | // Collect information for opening IDE project files in java/jdeps.go. |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 1851 | |
| 1852 | func (j *Import) IDEInfo(dpInfo *android.IdeInfo) { |
| 1853 | dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...) |
| 1854 | } |
| 1855 | |
| 1856 | func (j *Import) IDECustomizedModuleName() string { |
| 1857 | // TODO(b/113562217): Extract the base module name from the Import name, often the Import name |
| 1858 | // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better |
| 1859 | // solution to get the Import name. |
Ulya Trafimovich | 497a093 | 2021-07-14 16:35:33 +0100 | [diff] [blame] | 1860 | return android.RemoveOptionalPrebuiltPrefix(j.Name()) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1863 | var _ android.PrebuiltInterface = (*Import)(nil) |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1864 | |
Bill Peckham | ff89ffa | 2020-12-23 16:13:04 -0800 | [diff] [blame] | 1865 | func (j *Import) IsInstallable() bool { |
| 1866 | return Bool(j.properties.Installable) |
| 1867 | } |
| 1868 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 1869 | var _ DexpreopterInterface = (*Import)(nil) |
Bill Peckham | ff89ffa | 2020-12-23 16:13:04 -0800 | [diff] [blame] | 1870 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1871 | // java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module. |
| 1872 | // |
| 1873 | // By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were |
| 1874 | // compiled against an Android classpath. |
| 1875 | // |
| 1876 | // Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one |
| 1877 | // for host modules. |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1878 | func ImportFactory() android.Module { |
| 1879 | module := &Import{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1880 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1881 | module.AddProperties( |
| 1882 | &module.properties, |
| 1883 | &module.dexer.dexProperties, |
| 1884 | ) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1885 | |
Paul Duffin | 71b33cc | 2021-06-23 11:39:47 +0100 | [diff] [blame] | 1886 | module.initModuleAndImport(module) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1887 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1888 | module.dexProperties.Optimize.EnabledByDefault = false |
| 1889 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1890 | android.InitPrebuiltModule(module, &module.properties.Jars) |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 1891 | android.InitApexModule(module) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 1892 | android.InitSdkAwareModule(module) |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 1893 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 1894 | InitJavaModule(module, android.HostAndDeviceSupported) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 1895 | return module |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 1898 | // java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host |
| 1899 | // module. |
| 1900 | // |
| 1901 | // A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were |
| 1902 | // compiled against a host bootclasspath. |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1903 | func ImportFactoryHost() android.Module { |
| 1904 | module := &Import{} |
| 1905 | |
| 1906 | module.AddProperties(&module.properties) |
| 1907 | |
| 1908 | android.InitPrebuiltModule(module, &module.properties.Jars) |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 1909 | android.InitApexModule(module) |
Sam Delmerico | 5f83b49 | 2022-02-28 18:50:56 +0000 | [diff] [blame] | 1910 | android.InitBazelModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 1911 | InitJavaModule(module, android.HostSupported) |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 1912 | return module |
| 1913 | } |
| 1914 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1915 | // dex_import module |
| 1916 | |
| 1917 | type DexImportProperties struct { |
Colin Cross | 5cfc70d | 2019-07-15 13:36:55 -0700 | [diff] [blame] | 1918 | Jars []string `android:"path"` |
Jiyong Park | 4c4c024 | 2019-10-21 14:53:15 +0900 | [diff] [blame] | 1919 | |
| 1920 | // set the name of the output |
| 1921 | Stem *string |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | type DexImport struct { |
| 1925 | android.ModuleBase |
| 1926 | android.DefaultableModuleBase |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 1927 | android.ApexModuleBase |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1928 | prebuilt android.Prebuilt |
| 1929 | |
| 1930 | properties DexImportProperties |
| 1931 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 1932 | dexJarFile OptionalDexJarPath |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1933 | |
| 1934 | dexpreopter |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 1935 | |
| 1936 | hideApexVariantFromMake bool |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | func (j *DexImport) Prebuilt() *android.Prebuilt { |
| 1940 | return &j.prebuilt |
| 1941 | } |
| 1942 | |
| 1943 | func (j *DexImport) PrebuiltSrcs() []string { |
| 1944 | return j.properties.Jars |
| 1945 | } |
| 1946 | |
| 1947 | func (j *DexImport) Name() string { |
| 1948 | return j.prebuilt.Name(j.ModuleBase.Name()) |
| 1949 | } |
| 1950 | |
Jiyong Park | 0b23875 | 2019-10-29 11:23:10 +0900 | [diff] [blame] | 1951 | func (j *DexImport) Stem() string { |
| 1952 | return proptools.StringDefault(j.properties.Stem, j.ModuleBase.Name()) |
| 1953 | } |
| 1954 | |
Jiyong Park | 77acec6 | 2020-06-01 21:39:15 +0900 | [diff] [blame] | 1955 | func (a *DexImport) JacocoReportClassesFile() android.Path { |
| 1956 | return nil |
| 1957 | } |
| 1958 | |
Colin Cross | 08dca38 | 2020-07-21 20:31:17 -0700 | [diff] [blame] | 1959 | func (a *DexImport) LintDepSets() LintDepSets { |
| 1960 | return LintDepSets{} |
| 1961 | } |
| 1962 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 1963 | func (j *DexImport) IsInstallable() bool { |
| 1964 | return true |
| 1965 | } |
| 1966 | |
Jaewoong Jung | 476b9d6 | 2021-05-10 15:30:00 -0700 | [diff] [blame] | 1967 | func (j *DexImport) getStrictUpdatabilityLinting() bool { |
| 1968 | return false |
| 1969 | } |
| 1970 | |
| 1971 | func (j *DexImport) setStrictUpdatabilityLinting(bool) { |
| 1972 | } |
| 1973 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1974 | func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1975 | if len(j.properties.Jars) != 1 { |
| 1976 | ctx.PropertyErrorf("jars", "exactly one jar must be provided") |
| 1977 | } |
| 1978 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 1979 | apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 1980 | if !apexInfo.IsForPlatform() { |
| 1981 | j.hideApexVariantFromMake = true |
| 1982 | } |
| 1983 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 1984 | j.dexpreopter.installPath = j.dexpreopter.getInstallPath( |
| 1985 | ctx, android.PathForModuleInstall(ctx, "framework", j.Stem()+".jar")) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1986 | j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter) |
| 1987 | |
| 1988 | inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars") |
| 1989 | dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar") |
| 1990 | |
| 1991 | if j.dexpreopter.uncompressedDex { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1992 | rule := android.NewRuleBuilder(pctx, ctx) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 1993 | |
| 1994 | temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned") |
| 1995 | rule.Temporary(temporary) |
| 1996 | |
| 1997 | // use zip2zip to uncompress classes*.dex files |
| 1998 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 1999 | BuiltTool("zip2zip"). |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2000 | FlagWithInput("-i ", inputJar). |
| 2001 | FlagWithOutput("-o ", temporary). |
| 2002 | FlagWithArg("-0 ", "'classes*.dex'") |
| 2003 | |
| 2004 | // use zipalign to align uncompressed classes*.dex files |
| 2005 | rule.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 2006 | BuiltTool("zipalign"). |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2007 | Flag("-f"). |
| 2008 | Text("4"). |
| 2009 | Input(temporary). |
| 2010 | Output(dexOutputFile) |
| 2011 | |
| 2012 | rule.DeleteTemporaryFiles() |
| 2013 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 2014 | rule.Build("uncompress_dex", "uncompress dex") |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2015 | } else { |
| 2016 | ctx.Build(pctx, android.BuildParams{ |
| 2017 | Rule: android.Cp, |
| 2018 | Input: inputJar, |
| 2019 | Output: dexOutputFile, |
| 2020 | }) |
| 2021 | } |
| 2022 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2023 | j.dexJarFile = makeDexJarPathFromPath(dexOutputFile) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2024 | |
Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 2025 | j.dexpreopt(ctx, dexOutputFile) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2026 | |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 2027 | if apexInfo.IsForPlatform() { |
Jiyong Park | 01bca75 | 2020-06-08 19:24:09 +0900 | [diff] [blame] | 2028 | ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"), |
| 2029 | j.Stem()+".jar", dexOutputFile) |
| 2030 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2033 | func (j *DexImport) DexJarBuildPath() OptionalDexJarPath { |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2034 | return j.dexJarFile |
| 2035 | } |
| 2036 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 2037 | var _ android.ApexModule = (*DexImport)(nil) |
| 2038 | |
| 2039 | // Implements android.ApexModule |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 2040 | func (j *DexImport) ShouldSupportSdkVersion(ctx android.BaseModuleContext, |
| 2041 | sdkVersion android.ApiLevel) error { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 2042 | // we don't check prebuilt modules for sdk_version |
| 2043 | return nil |
| 2044 | } |
| 2045 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2046 | // dex_import imports a `.jar` file containing classes.dex files. |
| 2047 | // |
| 2048 | // A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed |
| 2049 | // to the device. |
| 2050 | func DexImportFactory() android.Module { |
| 2051 | module := &DexImport{} |
| 2052 | |
| 2053 | module.AddProperties(&module.properties) |
| 2054 | |
| 2055 | android.InitPrebuiltModule(module, &module.properties.Jars) |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2056 | android.InitApexModule(module) |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 2057 | InitJavaModule(module, android.DeviceSupported) |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2058 | return module |
| 2059 | } |
| 2060 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2061 | // Defaults |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2062 | type Defaults struct { |
| 2063 | android.ModuleBase |
| 2064 | android.DefaultsModuleBase |
Jiyong Park | 7f7766d | 2019-07-25 22:02:35 +0900 | [diff] [blame] | 2065 | android.ApexModuleBase |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2068 | // java_defaults provides a set of properties that can be inherited by other java or android modules. |
| 2069 | // |
| 2070 | // A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each |
| 2071 | // property in the defaults module that exists in the depending module will be prepended to the depending module's |
| 2072 | // value for that property. |
| 2073 | // |
| 2074 | // Example: |
| 2075 | // |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2076 | // java_defaults { |
| 2077 | // name: "example_defaults", |
| 2078 | // srcs: ["common/**/*.java"], |
| 2079 | // javacflags: ["-Xlint:all"], |
| 2080 | // aaptflags: ["--auto-add-overlay"], |
| 2081 | // } |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2082 | // |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2083 | // java_library { |
| 2084 | // name: "example", |
| 2085 | // defaults: ["example_defaults"], |
| 2086 | // srcs: ["example/**/*.java"], |
| 2087 | // } |
Colin Cross | 1b16b0e | 2019-02-12 14:41:32 -0800 | [diff] [blame] | 2088 | // |
| 2089 | // is functionally identical to: |
| 2090 | // |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2091 | // java_library { |
| 2092 | // name: "example", |
| 2093 | // srcs: [ |
| 2094 | // "common/**/*.java", |
| 2095 | // "example/**/*.java", |
| 2096 | // ], |
| 2097 | // javacflags: ["-Xlint:all"], |
| 2098 | // } |
Paul Duffin | 4735766 | 2019-12-05 14:07:14 +0000 | [diff] [blame] | 2099 | func DefaultsFactory() android.Module { |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2100 | module := &Defaults{} |
| 2101 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2102 | module.AddProperties( |
Jaewoong Jung | bc15e3a | 2021-03-10 17:02:43 -0800 | [diff] [blame] | 2103 | &CommonProperties{}, |
| 2104 | &DeviceProperties{}, |
Jooyung Han | 01d80d8 | 2022-01-08 12:16:32 +0900 | [diff] [blame] | 2105 | &OverridableDeviceProperties{}, |
Liz Kammer | a7a64f3 | 2020-07-09 15:16:41 -0700 | [diff] [blame] | 2106 | &DexProperties{}, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2107 | &DexpreoptProperties{}, |
Dan Willemsen | 6424d17 | 2018-03-08 13:27:59 -0800 | [diff] [blame] | 2108 | &android.ProtoProperties{}, |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 2109 | &aaptProperties{}, |
| 2110 | &androidLibraryProperties{}, |
| 2111 | &appProperties{}, |
| 2112 | &appTestProperties{}, |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 2113 | &overridableAppProperties{}, |
Roland Levillain | b5b0ff3 | 2020-02-04 15:45:49 +0000 | [diff] [blame] | 2114 | &testProperties{}, |
Colin Cross | 48de9a4 | 2018-10-02 13:53:33 -0700 | [diff] [blame] | 2115 | &ImportProperties{}, |
| 2116 | &AARImportProperties{}, |
| 2117 | &sdkLibraryProperties{}, |
Paul Duffin | 1b1e806 | 2020-05-08 13:44:43 +0100 | [diff] [blame] | 2118 | &commonToSdkLibraryAndImportProperties{}, |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 2119 | &DexImportProperties{}, |
Jooyung Han | 18020ea | 2019-11-13 10:50:48 +0900 | [diff] [blame] | 2120 | &android.ApexProperties{}, |
Jaewoong Jung | bf13546 | 2020-04-26 15:10:51 -0700 | [diff] [blame] | 2121 | &RuntimeResourceOverlayProperties{}, |
Colin Cross | 014489c | 2020-06-02 20:09:13 -0700 | [diff] [blame] | 2122 | &LintProperties{}, |
Colin Cross | cbce0b0 | 2021-02-09 10:38:30 -0800 | [diff] [blame] | 2123 | &appTestHelperAppProperties{}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2124 | ) |
| 2125 | |
| 2126 | android.InitDefaultsModule(module) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 2127 | return module |
| 2128 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 2129 | |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 2130 | func kytheExtractJavaFactory() android.Singleton { |
| 2131 | return &kytheExtractJavaSingleton{} |
| 2132 | } |
| 2133 | |
| 2134 | type kytheExtractJavaSingleton struct { |
| 2135 | } |
| 2136 | |
| 2137 | func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 2138 | var xrefTargets android.Paths |
| 2139 | ctx.VisitAllModules(func(module android.Module) { |
| 2140 | if javaModule, ok := module.(xref); ok { |
| 2141 | xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...) |
| 2142 | } |
| 2143 | }) |
| 2144 | // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets |
| 2145 | if len(xrefTargets) > 0 { |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 2146 | ctx.Phony("xref_java", xrefTargets...) |
Sasha Smundak | 2a4549e | 2018-11-05 16:49:08 -0800 | [diff] [blame] | 2147 | } |
| 2148 | } |
| 2149 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 2150 | var Bool = proptools.Bool |
Colin Cross | 38b40df | 2018-04-10 16:14:46 -0700 | [diff] [blame] | 2151 | var BoolDefault = proptools.BoolDefault |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 2152 | var String = proptools.String |
Colin Cross | 0d0ba59 | 2018-02-20 13:33:42 -0800 | [diff] [blame] | 2153 | var inList = android.InList |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2154 | |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2155 | // Add class loader context (CLC) of a given dependency to the current CLC. |
| 2156 | func addCLCFromDep(ctx android.ModuleContext, depModule android.Module, |
| 2157 | clcMap dexpreopt.ClassLoaderContextMap) { |
| 2158 | |
| 2159 | dep, ok := depModule.(UsesLibraryDependency) |
| 2160 | if !ok { |
| 2161 | return |
| 2162 | } |
| 2163 | |
Ulya Trafimovich | 840efb6 | 2021-07-15 14:34:40 +0100 | [diff] [blame] | 2164 | depName := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(depModule)) |
| 2165 | |
| 2166 | var sdkLib *string |
| 2167 | if lib, ok := depModule.(SdkLibraryDependency); ok && lib.sharedLibrary() { |
| 2168 | // A shared SDK library. This should be added as a top-level CLC element. |
| 2169 | sdkLib = &depName |
| 2170 | } else if ulib, ok := depModule.(ProvidesUsesLib); ok { |
| 2171 | // A non-SDK library disguised as an SDK library by the means of `provides_uses_lib` |
| 2172 | // property. This should be handled in the same way as a shared SDK library. |
| 2173 | sdkLib = ulib.ProvidesUsesLib() |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2174 | } |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2175 | |
| 2176 | depTag := ctx.OtherModuleDependencyTag(depModule) |
Ulya Trafimovich | fc0f6e3 | 2021-08-12 16:16:11 +0100 | [diff] [blame] | 2177 | if depTag == libTag { |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2178 | // Ok, propagate <uses-library> through non-static library dependencies. |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 2179 | } else if tag, ok := depTag.(usesLibraryDependencyTag); ok && tag.sdkVersion == dexpreopt.AnySdkVersion { |
| 2180 | // Ok, propagate <uses-library> through non-compatibility <uses-library> dependencies. |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2181 | } else if depTag == staticLibTag { |
| 2182 | // Propagate <uses-library> through static library dependencies, unless it is a component |
| 2183 | // library (such as stubs). Component libraries have a dependency on their SDK library, |
| 2184 | // which should not be pulled just because of a static component library. |
Ulya Trafimovich | 840efb6 | 2021-07-15 14:34:40 +0100 | [diff] [blame] | 2185 | if sdkLib != nil { |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2186 | return |
| 2187 | } |
| 2188 | } else { |
| 2189 | // Don't propagate <uses-library> for other dependency tags. |
| 2190 | return |
| 2191 | } |
| 2192 | |
Ulya Trafimovich | 840efb6 | 2021-07-15 14:34:40 +0100 | [diff] [blame] | 2193 | // If this is an SDK (or SDK-like) library, then it should be added as a node in the CLC tree, |
| 2194 | // and its CLC should be added as subtree of that node. Otherwise the library is not a |
| 2195 | // <uses_library> and should not be added to CLC, but the transitive <uses-library> dependencies |
| 2196 | // from its CLC should be added to the current CLC. |
| 2197 | if sdkLib != nil { |
Ulya Trafimovich | f5d91bb | 2022-05-04 12:00:02 +0100 | [diff] [blame] | 2198 | clcMap.AddContext(ctx, dexpreopt.AnySdkVersion, *sdkLib, false, |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 2199 | dep.DexJarBuildPath().PathOrNil(), dep.DexJarInstallPath(), dep.ClassLoaderContexts()) |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2200 | } else { |
Ulya Trafimovich | 88bb6f6 | 2020-12-16 16:16:11 +0000 | [diff] [blame] | 2201 | clcMap.AddContextMap(dep.ClassLoaderContexts(), depName) |
| 2202 | } |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2203 | } |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2204 | |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2205 | type javaResourcesAttributes struct { |
| 2206 | Resources bazel.LabelListAttribute |
| 2207 | Resource_strip_prefix *string |
| 2208 | } |
| 2209 | |
| 2210 | func (m *Library) convertJavaResourcesAttributes(ctx android.TopDownMutatorContext) *javaResourcesAttributes { |
| 2211 | var resources bazel.LabelList |
| 2212 | var resourceStripPrefix *string |
| 2213 | |
| 2214 | if m.properties.Java_resources != nil { |
| 2215 | resources.Append(android.BazelLabelForModuleSrc(ctx, m.properties.Java_resources)) |
| 2216 | } |
| 2217 | |
| 2218 | //TODO(b/179889880) handle case where glob includes files outside package |
| 2219 | resDeps := ResourceDirsToFiles( |
| 2220 | ctx, |
| 2221 | m.properties.Java_resource_dirs, |
| 2222 | m.properties.Exclude_java_resource_dirs, |
| 2223 | m.properties.Exclude_java_resources, |
| 2224 | ) |
| 2225 | |
| 2226 | for i, resDep := range resDeps { |
| 2227 | dir, files := resDep.dir, resDep.files |
| 2228 | |
| 2229 | resources.Append(bazel.MakeLabelList(android.RootToModuleRelativePaths(ctx, files))) |
| 2230 | |
| 2231 | // Bazel includes the relative path from the WORKSPACE root when placing the resource |
| 2232 | // inside the JAR file, so we need to remove that prefix |
| 2233 | resourceStripPrefix = proptools.StringPtr(dir.String()) |
| 2234 | if i > 0 { |
| 2235 | // TODO(b/226423379) allow multiple resource prefixes |
| 2236 | ctx.ModuleErrorf("bp2build does not support more than one directory in java_resource_dirs (b/226423379)") |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | return &javaResourcesAttributes{ |
| 2241 | Resources: bazel.MakeLabelListAttribute(resources), |
| 2242 | Resource_strip_prefix: resourceStripPrefix, |
| 2243 | } |
| 2244 | } |
| 2245 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2246 | type javaCommonAttributes struct { |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2247 | *javaResourcesAttributes |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2248 | Srcs bazel.LabelListAttribute |
Sam Delmerico | 77267c7 | 2022-03-18 14:11:07 +0000 | [diff] [blame] | 2249 | Plugins bazel.LabelListAttribute |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2250 | Javacopts bazel.StringListAttribute |
| 2251 | } |
| 2252 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2253 | type javaDependencyLabels struct { |
| 2254 | // Dependencies which DO NOT contribute to the API visible to upstream dependencies. |
| 2255 | Deps bazel.LabelListAttribute |
| 2256 | // Dependencies which DO contribute to the API visible to upstream dependencies. |
| 2257 | StaticDeps bazel.LabelListAttribute |
| 2258 | } |
| 2259 | |
| 2260 | // convertLibraryAttrsBp2Build converts a few shared attributes from java_* modules |
| 2261 | // and also separates dependencies into dynamic dependencies and static dependencies. |
| 2262 | // Each corresponding Bazel target type, can have a different method for handling |
| 2263 | // dynamic vs. static dependencies, and so these are returned to the calling function. |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2264 | type eventLogTagsAttributes struct { |
| 2265 | Srcs bazel.LabelListAttribute |
| 2266 | } |
| 2267 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2268 | func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext) (*javaCommonAttributes, *javaDependencyLabels) { |
Sam Delmerico | e91d030 | 2022-02-23 15:28:33 +0000 | [diff] [blame] | 2269 | var srcs bazel.LabelListAttribute |
| 2270 | archVariantProps := m.GetArchVariantProperties(ctx, &CommonProperties{}) |
| 2271 | for axis, configToProps := range archVariantProps { |
| 2272 | for config, _props := range configToProps { |
| 2273 | if archProps, ok := _props.(*CommonProperties); ok { |
| 2274 | archSrcs := android.BazelLabelForModuleSrcExcludes(ctx, archProps.Srcs, archProps.Exclude_srcs) |
| 2275 | srcs.SetSelectValue(axis, config, archSrcs) |
| 2276 | } |
| 2277 | } |
| 2278 | } |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2279 | |
| 2280 | javaSrcPartition := "java" |
| 2281 | protoSrcPartition := "proto" |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2282 | logtagSrcPartition := "logtag" |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2283 | srcPartitions := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{ |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2284 | javaSrcPartition: bazel.LabelPartition{Extensions: []string{".java"}, Keep_remainder: true}, |
| 2285 | logtagSrcPartition: bazel.LabelPartition{Extensions: []string{".logtags", ".logtag"}}, |
| 2286 | protoSrcPartition: android.ProtoSrcLabelPartition, |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2287 | }) |
| 2288 | |
Sam Delmerico | 24da73c | 2022-03-16 20:36:54 +0000 | [diff] [blame] | 2289 | javaSrcs := srcPartitions[javaSrcPartition] |
| 2290 | |
| 2291 | var logtagsSrcs bazel.LabelList |
| 2292 | if !srcPartitions[logtagSrcPartition].IsEmpty() { |
| 2293 | logtagsLibName := m.Name() + "_logtags" |
| 2294 | logtagsSrcs = bazel.MakeLabelList([]bazel.Label{{Label: ":" + logtagsLibName}}) |
| 2295 | ctx.CreateBazelTargetModule( |
| 2296 | bazel.BazelTargetModuleProperties{ |
| 2297 | Rule_class: "event_log_tags", |
| 2298 | Bzl_load_location: "//build/make/tools:event_log_tags.bzl", |
| 2299 | }, |
| 2300 | android.CommonAttributes{Name: logtagsLibName}, |
| 2301 | &eventLogTagsAttributes{ |
| 2302 | Srcs: srcPartitions[logtagSrcPartition], |
| 2303 | }, |
| 2304 | ) |
| 2305 | } |
| 2306 | javaSrcs.Append(bazel.MakeLabelListAttribute(logtagsSrcs)) |
| 2307 | |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame] | 2308 | var javacopts []string |
| 2309 | if m.properties.Javacflags != nil { |
| 2310 | javacopts = append(javacopts, m.properties.Javacflags...) |
| 2311 | } |
Vinh Tran | 3ac6daf | 2022-04-22 19:09:58 -0400 | [diff] [blame] | 2312 | if m.properties.Java_version != nil { |
| 2313 | javaVersion := normalizeJavaVersion(ctx, *m.properties.Java_version).String() |
| 2314 | javacopts = append(javacopts, fmt.Sprintf("-source %s -target %s", javaVersion, javaVersion)) |
| 2315 | } |
| 2316 | |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame] | 2317 | epEnabled := m.properties.Errorprone.Enabled |
| 2318 | //TODO(b/227504307) add configuration that depends on RUN_ERROR_PRONE environment variable |
| 2319 | if Bool(epEnabled) { |
| 2320 | javacopts = append(javacopts, m.properties.Errorprone.Javacflags...) |
| 2321 | } |
| 2322 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2323 | commonAttrs := &javaCommonAttributes{ |
Sam Delmerico | 7998581 | 2022-03-23 20:20:42 +0000 | [diff] [blame] | 2324 | Srcs: javaSrcs, |
| 2325 | javaResourcesAttributes: m.convertJavaResourcesAttributes(ctx), |
Sam Delmerico | 77267c7 | 2022-03-18 14:11:07 +0000 | [diff] [blame] | 2326 | Plugins: bazel.MakeLabelListAttribute( |
| 2327 | android.BazelLabelForModuleDeps(ctx, m.properties.Plugins), |
| 2328 | ), |
Sam Delmerico | 58614c0 | 2022-03-15 21:02:09 +0000 | [diff] [blame] | 2329 | Javacopts: bazel.MakeStringListAttribute(javacopts), |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2330 | } |
| 2331 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2332 | depLabels := &javaDependencyLabels{} |
| 2333 | |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 2334 | var deps bazel.LabelList |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2335 | if m.properties.Libs != nil { |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 2336 | deps.Append(android.BazelLabelForModuleDeps(ctx, m.properties.Libs)) |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2337 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2338 | |
| 2339 | var staticDeps bazel.LabelList |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 2340 | if m.properties.Static_libs != nil { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2341 | staticDeps.Append(android.BazelLabelForModuleDeps(ctx, m.properties.Static_libs)) |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 2342 | } |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2343 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2344 | protoDepLabel := bp2buildProto(ctx, &m.Module, srcPartitions[protoSrcPartition]) |
| 2345 | // Soong does not differentiate between a java_library and the Bazel equivalent of |
| 2346 | // a java_proto_library + proto_library pair. Instead, in Soong proto sources are |
| 2347 | // listed directly in the srcs of a java_library, and the classes produced |
| 2348 | // by protoc are included directly in the resulting JAR. Thus upstream dependencies |
| 2349 | // that depend on a java_library with proto sources can link directly to the protobuf API, |
| 2350 | // and so this should be a static dependency. |
| 2351 | staticDeps.Add(protoDepLabel) |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 2352 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2353 | depLabels.Deps = bazel.MakeLabelListAttribute(deps) |
| 2354 | depLabels.StaticDeps = bazel.MakeLabelListAttribute(staticDeps) |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 2355 | |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2356 | return commonAttrs, depLabels |
| 2357 | } |
| 2358 | |
| 2359 | type javaLibraryAttributes struct { |
| 2360 | *javaCommonAttributes |
| 2361 | Deps bazel.LabelListAttribute |
| 2362 | Exports bazel.LabelListAttribute |
Sam Delmerico | fde9fb5 | 2022-01-28 20:53:38 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | func javaLibraryBp2Build(ctx android.TopDownMutatorContext, m *Library) { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2366 | commonAttrs, depLabels := m.convertLibraryAttrsBp2Build(ctx) |
| 2367 | |
| 2368 | deps := depLabels.Deps |
| 2369 | if !commonAttrs.Srcs.IsEmpty() { |
| 2370 | deps.Append(depLabels.StaticDeps) // we should only append these if there are sources to use them |
| 2371 | |
| 2372 | sdkVersion := m.SdkVersion(ctx) |
| 2373 | if sdkVersion.Kind == android.SdkPublic && sdkVersion.ApiLevel == android.FutureApiLevel { |
| 2374 | // TODO(b/220869005) remove forced dependency on current public android.jar |
| 2375 | deps.Add(bazel.MakeLabelAttribute("//prebuilts/sdk:public_current_android_sdk_java_import")) |
| 2376 | } |
| 2377 | } else if !depLabels.Deps.IsEmpty() { |
| 2378 | ctx.ModuleErrorf("Module has direct dependencies but no sources. Bazel will not allow this.") |
| 2379 | } |
| 2380 | |
| 2381 | attrs := &javaLibraryAttributes{ |
| 2382 | javaCommonAttributes: commonAttrs, |
| 2383 | Deps: deps, |
| 2384 | Exports: depLabels.StaticDeps, |
| 2385 | } |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2386 | |
| 2387 | props := bazel.BazelTargetModuleProperties{ |
| 2388 | Rule_class: "java_library", |
| 2389 | Bzl_load_location: "//build/bazel/rules/java:library.bzl", |
| 2390 | } |
| 2391 | |
| 2392 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
| 2393 | } |
| 2394 | |
| 2395 | type javaBinaryHostAttributes struct { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2396 | *javaCommonAttributes |
| 2397 | Deps bazel.LabelListAttribute |
| 2398 | Runtime_deps bazel.LabelListAttribute |
| 2399 | Main_class string |
| 2400 | Jvm_flags bazel.StringListAttribute |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | // JavaBinaryHostBp2Build is for java_binary_host bp2build. |
| 2404 | func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) { |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2405 | commonAttrs, depLabels := m.convertLibraryAttrsBp2Build(ctx) |
| 2406 | |
| 2407 | deps := depLabels.Deps |
| 2408 | deps.Append(depLabels.StaticDeps) |
| 2409 | if m.binaryProperties.Jni_libs != nil { |
| 2410 | deps.Append(bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs))) |
| 2411 | } |
| 2412 | |
| 2413 | var runtimeDeps bazel.LabelListAttribute |
| 2414 | if commonAttrs.Srcs.IsEmpty() { |
| 2415 | // if there are no sources, then the dependencies can only be used at runtime |
| 2416 | runtimeDeps = deps |
| 2417 | deps = bazel.LabelListAttribute{} |
| 2418 | } |
| 2419 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2420 | mainClass := "" |
| 2421 | if m.binaryProperties.Main_class != nil { |
| 2422 | mainClass = *m.binaryProperties.Main_class |
| 2423 | } |
| 2424 | if m.properties.Manifest != nil { |
| 2425 | mainClassInManifest, err := android.GetMainClassInManifest(ctx.Config(), android.PathForModuleSrc(ctx, *m.properties.Manifest).String()) |
| 2426 | if err != nil { |
| 2427 | return |
| 2428 | } |
| 2429 | mainClass = mainClassInManifest |
| 2430 | } |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2431 | |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2432 | attrs := &javaBinaryHostAttributes{ |
Sam Delmerico | c016143 | 2022-02-25 21:34:51 +0000 | [diff] [blame] | 2433 | javaCommonAttributes: commonAttrs, |
| 2434 | Deps: deps, |
| 2435 | Runtime_deps: runtimeDeps, |
| 2436 | Main_class: mainClass, |
Wei Li | bafb6d6 | 2021-12-10 03:14:59 -0800 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | // Attribute jvm_flags |
| 2440 | if m.binaryProperties.Jni_libs != nil { |
| 2441 | jniLibPackages := map[string]bool{} |
| 2442 | for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes { |
| 2443 | jniLibPackage := jniLibLabel.Label |
| 2444 | indexOfColon := strings.Index(jniLibLabel.Label, ":") |
| 2445 | if indexOfColon > 0 { |
| 2446 | // JNI lib from other package |
| 2447 | jniLibPackage = jniLibLabel.Label[2:indexOfColon] |
| 2448 | } else if indexOfColon == 0 { |
| 2449 | // JNI lib in the same package of java_binary |
| 2450 | packageOfCurrentModule := m.GetBazelLabel(ctx, m) |
| 2451 | jniLibPackage = packageOfCurrentModule[2:strings.Index(packageOfCurrentModule, ":")] |
| 2452 | } |
| 2453 | if _, inMap := jniLibPackages[jniLibPackage]; !inMap { |
| 2454 | jniLibPackages[jniLibPackage] = true |
| 2455 | } |
| 2456 | } |
| 2457 | jniLibPaths := []string{} |
| 2458 | for jniLibPackage, _ := range jniLibPackages { |
| 2459 | // See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH |
| 2460 | jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage) |
| 2461 | } |
| 2462 | attrs.Jvm_flags = bazel.MakeStringListAttribute([]string{"-Djava.library.path=" + strings.Join(jniLibPaths, ":")}) |
| 2463 | } |
| 2464 | |
| 2465 | props := bazel.BazelTargetModuleProperties{ |
| 2466 | Rule_class: "java_binary", |
| 2467 | } |
| 2468 | |
| 2469 | // Create the BazelTargetModule. |
| 2470 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
| 2471 | } |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 2472 | |
| 2473 | type bazelJavaImportAttributes struct { |
| 2474 | Jars bazel.LabelListAttribute |
| 2475 | } |
| 2476 | |
| 2477 | // java_import bp2Build converter. |
| 2478 | func (i *Import) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
Sam Delmerico | 4898316 | 2022-02-22 21:41:33 +0000 | [diff] [blame] | 2479 | var jars bazel.LabelListAttribute |
| 2480 | archVariantProps := i.GetArchVariantProperties(ctx, &ImportProperties{}) |
| 2481 | for axis, configToProps := range archVariantProps { |
| 2482 | for config, _props := range configToProps { |
| 2483 | if archProps, ok := _props.(*ImportProperties); ok { |
| 2484 | archJars := android.BazelLabelForModuleSrcExcludes(ctx, archProps.Jars, []string(nil)) |
| 2485 | jars.SetSelectValue(axis, config, archJars) |
| 2486 | } |
| 2487 | } |
| 2488 | } |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 2489 | |
| 2490 | attrs := &bazelJavaImportAttributes{ |
| 2491 | Jars: jars, |
| 2492 | } |
| 2493 | props := bazel.BazelTargetModuleProperties{Rule_class: "java_import"} |
| 2494 | |
| 2495 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: android.RemoveOptionalPrebuiltPrefix(i.Name())}, attrs) |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2496 | } |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 2497 | |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 2498 | var _ android.MixedBuildBuildable = (*Import)(nil) |
| 2499 | |
| 2500 | func (i *Import) getBazelModuleLabel(ctx android.BaseModuleContext) string { |
| 2501 | return android.RemoveOptionalPrebuiltPrefixFromBazelLabel(i.GetBazelLabel(ctx, i)) |
| 2502 | } |
| 2503 | |
| 2504 | func (i *Import) ProcessBazelQueryResponse(ctx android.ModuleContext) { |
| 2505 | i.commonBuildActions(ctx) |
| 2506 | |
| 2507 | bazelCtx := ctx.Config().BazelContext |
| 2508 | filePaths, err := bazelCtx.GetOutputFiles(i.getBazelModuleLabel(ctx), android.GetConfigKey(ctx)) |
| 2509 | if err != nil { |
| 2510 | ctx.ModuleErrorf(err.Error()) |
| 2511 | return |
| 2512 | } |
| 2513 | |
| 2514 | bazelJars := android.Paths{} |
| 2515 | for _, bazelOutputFile := range filePaths { |
| 2516 | bazelJars = append(bazelJars, android.PathForBazelOut(ctx, bazelOutputFile)) |
| 2517 | } |
| 2518 | |
| 2519 | jarName := android.RemoveOptionalPrebuiltPrefix(i.Name()) + ".jar" |
| 2520 | outputFile := android.PathForModuleOut(ctx, "bazelCombined", jarName) |
| 2521 | TransformJarsToJar(ctx, outputFile, "combine prebuilt jars", bazelJars, |
| 2522 | android.OptionalPath{}, // manifest |
| 2523 | false, // stripDirEntries |
| 2524 | []string{}, // filesToStrip |
| 2525 | []string{}, // dirsToStrip |
| 2526 | ) |
| 2527 | i.combinedClasspathFile = outputFile |
| 2528 | |
| 2529 | ctx.SetProvider(JavaInfoProvider, JavaInfo{ |
| 2530 | HeaderJars: android.PathsIfNonNil(i.combinedClasspathFile), |
| 2531 | ImplementationAndResourcesJars: android.PathsIfNonNil(i.combinedClasspathFile), |
| 2532 | ImplementationJars: android.PathsIfNonNil(i.combinedClasspathFile), |
| 2533 | //TODO(b/240308299) include AIDL information from Bazel |
| 2534 | }) |
| 2535 | |
| 2536 | i.maybeInstall(ctx, jarName, outputFile) |
| 2537 | } |
| 2538 | |
| 2539 | func (i *Import) QueueBazelCall(ctx android.BaseModuleContext) { |
| 2540 | bazelCtx := ctx.Config().BazelContext |
| 2541 | bazelCtx.QueueBazelRequest(i.getBazelModuleLabel(ctx), cquery.GetOutputFiles, android.GetConfigKey(ctx)) |
| 2542 | } |
| 2543 | |
| 2544 | func (i *Import) IsMixedBuildSupported(ctx android.BaseModuleContext) bool { |
| 2545 | return true |
Romain Jobredeaux | 428a366 | 2022-01-28 11:12:52 -0500 | [diff] [blame] | 2546 | } |