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