blob: fb2ddf917b4d89c166ed32b0cdab8c441c1bfd93 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// 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
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Sasha Smundak2a4549e2018-11-05 16:49:08 -080053 android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Colin Cross2fe66872015-03-30 17:20:39 -070056// TODO:
57// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070058// Renderscript
59// Post-jar passes:
60// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070061// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070062// DroidDoc
63// Findbugs
64
Colin Cross89536d42017-07-07 14:35:50 -070065type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070066 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
67 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080068 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070069
70 // list of source files that should not be used to build the Java module.
71 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080072 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070073
74 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070075 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070076
Colin Cross86a63ff2017-09-27 17:33:10 -070077 // list of directories that should be excluded from java_resource_dirs
78 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070079
Colin Cross0f37af02017-09-27 17:42:05 -070080 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080081 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070082
Colin Crosscedd4762018-09-13 11:26:19 -070083 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080084 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070085
Colin Cross7d5136f2015-05-11 13:39:40 -070086 // list of module-specific flags that will be used for javac compiles
87 Javacflags []string `android:"arch_variant"`
88
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020089 // list of module-specific flags that will be used for kotlinc compiles
90 Kotlincflags []string `android:"arch_variant"`
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -080099 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
Colin Cross540eff82017-06-22 17:01:52 -0700101 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800102 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700103
104 // If not blank, set the java version passed to javac as -source and -target
105 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700106
Colin Cross9ae1b922018-06-26 17:59:05 -0700107 // If set to true, allow this module to be dexed and installed on devices. Has no
108 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700109 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700110
Colin Cross0f37af02017-09-27 17:42:05 -0700111 // If set to true, include sources used to compile the module in to the final jar
112 Include_srcs *bool
113
Vladimir Marko0975ee02019-04-02 10:29:55 +0100114 // If not empty, classes are restricted to the specified packages and their sub-packages.
115 // This restriction is checked after applying jarjar rules and including static libs.
116 Permitted_packages []string
117
Colin Crossbe9cdb82019-01-21 21:37:16 -0800118 // List of modules to use as annotation processors
119 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700120
Nan Zhang61eaedb2017-11-02 13:28:15 -0700121 // The number of Java source entries each Javac instance can process
122 Javac_shard_size *int64
123
Nan Zhang5f8cb422018-02-06 10:34:32 -0800124 // Add host jdk tools.jar to bootclasspath
125 Use_tools_jar *bool
126
Colin Cross1369cdb2017-09-29 17:58:17 -0700127 Openjdk9 struct {
128 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800129 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700130
131 // List of javac flags that should only be used when passing -source 1.9
132 Javacflags []string
133 }
Colin Crosscb933592017-11-22 13:49:43 -0800134
Colin Cross81440082018-08-15 20:21:55 -0700135 // When compiling language level 9+ .java code in packages that are part of
136 // a system module, patch_module names the module that your sources and
137 // dependencies should be patched into. The Android runtime currently
138 // doesn't implement the JEP 261 module system so this option is only
139 // supported at compile time. It should only be needed to compile tests in
140 // packages that exist in libcore and which are inconvenient to move
141 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100142 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700143
Colin Crosscb933592017-11-22 13:49:43 -0800144 Jacoco struct {
145 // List of classes to include for instrumentation with jacoco to collect coverage
146 // information at runtime when building with coverage enabled. If unset defaults to all
147 // classes.
148 // Supports '*' as the last character of an entry in the list as a wildcard match.
149 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
150 // it matches classes in the package that have the class name as a prefix.
151 Include_filter []string
152
153 // List of classes to exclude from instrumentation with jacoco to collect coverage
154 // information at runtime when building with coverage enabled. Overrides classes selected
155 // by the include_filter property.
156 // Supports '*' as the last character of an entry in the list as a wildcard match.
157 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
158 // it matches classes in the package that have the class name as a prefix.
159 Exclude_filter []string
160 }
161
Andreas Gampef3e5b552018-01-22 21:27:21 -0800162 Errorprone struct {
163 // List of javac flags that should only be used when running errorprone.
164 Javacflags []string
165 }
166
Colin Cross0f2ee152017-12-14 15:22:43 -0800167 Proto struct {
168 // List of extra options that will be passed to the proto generator.
169 Output_params []string
170 }
171
Colin Crosscb933592017-11-22 13:49:43 -0800172 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800173
174 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800175 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700176}
177
Colin Cross89536d42017-07-07 14:35:50 -0700178type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700179 // list of module-specific flags that will be used for dex compiles
180 Dxflags []string `android:"arch_variant"`
181
Colin Cross83bb3162018-06-25 15:48:06 -0700182 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
183 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800184 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700185
Colin Cross83bb3162018-06-25 15:48:06 -0700186 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
187 // Defaults to sdk_version if not set.
188 Min_sdk_version *string
189
Dan Willemsen419290a2018-10-31 15:28:47 -0700190 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
191 // Defaults to sdk_version if not set.
192 Target_sdk_version *string
193
Colin Cross6af2e492018-05-22 11:12:33 -0700194 // if true, compile against the platform APIs instead of an SDK.
195 Platform_apis *bool
196
Colin Crossebe1a512017-11-14 13:12:14 -0800197 Aidl struct {
198 // Top level directories to pass to aidl tool
199 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700200
Colin Crossebe1a512017-11-14 13:12:14 -0800201 // Directories rooted at the Android.bp file to pass to aidl tool
202 Local_include_dirs []string
203
204 // directories that should be added as include directories for any aidl sources of modules
205 // that depend on this module, as well as to aidl for this module.
206 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100207
208 // whether to generate traces (for systrace) for this interface
209 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100210
211 // whether to generate Binder#GetTransaction name method.
212 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800213 }
Colin Cross92430102017-10-09 14:59:32 -0700214
215 // If true, export a copy of the module as a -hostdex module for host testing.
216 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700217
Colin Cross7f87f4f2019-04-24 13:41:45 -0700218 Target struct {
219 Hostdex struct {
220 // Additional required dependencies to add to -hostdex modules.
221 Required []string
222 }
223 }
224
David Brazdil17ef5632018-06-27 10:27:45 +0100225 // If set to true, compile dex regardless of installable. Defaults to false.
226 Compile_dex *bool
227
Colin Cross66dbc0b2017-12-28 12:23:20 -0800228 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700229 // If false, disable all optimization. Defaults to true for android_app and android_test
230 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800231 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700232 // True if the module containing this has it set by default.
233 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800234
235 // If true, optimize for size by removing unused code. Defaults to true for apps,
236 // false for libraries and tests.
237 Shrink *bool
238
239 // If true, optimize bytecode. Defaults to false.
240 Optimize *bool
241
242 // If true, obfuscate bytecode. Defaults to false.
243 Obfuscate *bool
244
245 // If true, do not use the flag files generated by aapt that automatically keep
246 // classes referenced by the app manifest. Defaults to false.
247 No_aapt_flags *bool
248
249 // Flags to pass to proguard.
250 Proguard_flags []string
251
252 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800253 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800254 }
255
Colin Cross1369cdb2017-09-29 17:58:17 -0700256 // When targeting 1.9, override the modules to use with --system
257 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700258
259 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800260 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700261}
262
Sasha Smundak2057f822019-04-16 17:16:58 -0700263func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
264 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
265}
266
Colin Cross46c9b8b2017-06-22 16:51:17 -0700267// Module contains the properties and members used by all java module types
268type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700269 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700270 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900271 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700272
Colin Cross89536d42017-07-07 14:35:50 -0700273 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700274 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700275 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700276
Colin Cross331a1212018-08-15 20:40:52 -0700277 // jar file containing header classes including static library dependencies, suitable for
278 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700279 headerJarFile android.Path
280
Colin Cross331a1212018-08-15 20:40:52 -0700281 // jar file containing implementation classes including static library dependencies but no
282 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700284
Colin Cross331a1212018-08-15 20:40:52 -0700285 // jar file containing only resources including from static library dependencies
286 resourceJar android.Path
287
Colin Cross0c4ce212019-05-03 15:28:19 -0700288 // args and dependencies to package source files into a srcjar
289 srcJarArgs []string
290 srcJarDeps android.Paths
291
Colin Cross331a1212018-08-15 20:40:52 -0700292 // jar file containing implementation classes and resources including static library
293 // dependencies
294 implementationAndResourcesJar android.Path
295
296 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700297 dexJarFile android.Path
298
Colin Cross43f08db2018-11-12 10:13:39 -0800299 // output file that contains classes.dex if it should be in the output file
300 maybeStrippedDexJarFile android.Path
301
Colin Crosscb933592017-11-22 13:49:43 -0800302 // output file containing uninstrumented classes that will be instrumented by jacoco
303 jacocoReportClassesFile android.Path
304
Colin Cross66dbc0b2017-12-28 12:23:20 -0800305 // output file containing mapping of obfuscated names
306 proguardDictionary android.Path
307
Colin Cross331a1212018-08-15 20:40:52 -0700308 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700309 outputFile android.Path
310 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700311
Colin Cross635c3b02016-05-18 15:37:25 -0700312 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700313
Colin Cross635c3b02016-05-18 15:37:25 -0700314 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700315
Colin Cross2fe66872015-03-30 17:20:39 -0700316 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700317 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800318
319 // list of .java files and srcjars that was passed to javac
320 compiledJavaSrcs android.Paths
321 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800322
323 // list of extra progurad flag files
324 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900325
Colin Cross094054a2018-10-17 15:10:48 -0700326 // manifest file to use instead of properties.Manifest
327 overrideManifest android.OptionalPath
328
Jiyong Park1be96912018-05-28 18:02:19 +0900329 // list of SDK lib names that this java moudule is exporting
330 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700331
332 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
333 // filter out Exclude_srcs, will be used by android.IDEInfo struct
334 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800335
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800336 // expanded Jarjar_rules
337 expandJarjarRules android.Path
338
Vladimir Marko0975ee02019-04-02 10:29:55 +0100339 // list of additional targets for checkbuild
340 additionalCheckedModules android.Paths
341
Colin Cross988708c2019-05-06 14:04:11 -0700342 // Extra files generated by the module type to be added as java resources.
343 extraResources android.Paths
344
Colin Crossf24a22a2019-01-31 14:12:44 -0800345 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800346 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800347
348 // list of the xref extraction files
349 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700350}
351
Colin Cross41955e82019-05-29 14:40:35 -0700352func (j *Module) OutputFiles(tag string) (android.Paths, error) {
353 switch tag {
354 case "":
355 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700356 case ".jar":
357 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700358 default:
359 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
360 }
Colin Cross54250902017-12-05 09:28:08 -0800361}
362
Jiyong Park8fd61922018-11-08 02:50:25 +0900363func (j *Module) DexJarFile() android.Path {
364 return j.dexJarFile
365}
366
Colin Cross41955e82019-05-29 14:40:35 -0700367var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800368
Colin Crossf506d872017-07-19 15:53:04 -0700369type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700370 HeaderJars() android.Paths
371 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700372 ResourceJars() android.Paths
373 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800374 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700375 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900376 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700377 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700378}
379
Jiyong Parkc678ad32018-04-10 13:07:10 +0900380type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700381 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
382 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900383}
384
Nan Zhangb2b33de2018-02-23 11:18:47 -0800385type SrcDependency interface {
386 CompiledSrcs() android.Paths
387 CompiledSrcJars() android.Paths
388}
389
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800390type xref interface {
391 XrefJavaFiles() android.Paths
392}
393
Nan Zhangb2b33de2018-02-23 11:18:47 -0800394func (j *Module) CompiledSrcs() android.Paths {
395 return j.compiledJavaSrcs
396}
397
398func (j *Module) CompiledSrcJars() android.Paths {
399 return j.compiledSrcJars
400}
401
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800402func (j *Module) XrefJavaFiles() android.Paths {
403 return j.kytheFiles
404}
405
Nan Zhangb2b33de2018-02-23 11:18:47 -0800406var _ SrcDependency = (*Module)(nil)
407
Colin Cross89536d42017-07-07 14:35:50 -0700408func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
409 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
410 android.InitDefaultableModule(module)
411}
412
Colin Crossbe1da472017-07-07 15:59:46 -0700413type dependencyTag struct {
414 blueprint.BaseDependencyTag
415 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700416}
417
Colin Crossa4f08812018-10-02 22:03:40 -0700418type jniDependencyTag struct {
419 blueprint.BaseDependencyTag
420 target android.Target
421}
422
Colin Crossbe1da472017-07-07 15:59:46 -0700423var (
Colin Cross4b964c02018-10-15 16:18:06 -0700424 staticLibTag = dependencyTag{name: "staticlib"}
425 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800426 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700427 bootClasspathTag = dependencyTag{name: "bootclasspath"}
428 systemModulesTag = dependencyTag{name: "system modules"}
429 frameworkResTag = dependencyTag{name: "framework-res"}
430 frameworkApkTag = dependencyTag{name: "framework-apk"}
431 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800432 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700433 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
434 certificateTag = dependencyTag{name: "certificate"}
435 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700436 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700437)
Colin Cross2fe66872015-03-30 17:20:39 -0700438
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900439func defaultSdkVersion(ctx checkVendorModuleContext) string {
440 if ctx.SocSpecific() || ctx.DeviceSpecific() {
441 return "system_current"
442 }
443 return ""
444}
445
446type checkVendorModuleContext interface {
447 SocSpecific() bool
448 DeviceSpecific() bool
449}
450
Colin Crossfc3674a2017-09-18 17:41:52 -0700451type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700452 useModule, useFiles, useDefaultLibs, invalidVersion bool
453
Colin Cross86a60ae2018-05-29 14:44:55 -0700454 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700455 systemModules string
456
Colin Crossa97c5d32018-03-28 14:58:31 -0700457 frameworkResModule string
458
Colin Cross86a60ae2018-05-29 14:44:55 -0700459 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700460 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100461
462 noStandardLibs, noFrameworksLibs bool
463}
464
465func (s sdkDep) hasStandardLibs() bool {
466 return !s.noStandardLibs
467}
468
469func (s sdkDep) hasFrameworkLibs() bool {
470 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700471}
472
Colin Crossa4f08812018-10-02 22:03:40 -0700473type jniLib struct {
474 name string
475 path android.Path
476 target android.Target
477}
478
Colin Cross0ea8ba82019-06-06 14:33:29 -0700479func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800480 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
481}
482
Colin Cross0ea8ba82019-06-06 14:33:29 -0700483func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800484 return j.shouldInstrument(ctx) &&
485 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
486 ctx.Config().UnbundledBuild())
487}
488
Colin Cross83bb3162018-06-25 15:48:06 -0700489func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900490 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700491}
492
493func (j *Module) minSdkVersion() string {
494 if j.deviceProperties.Min_sdk_version != nil {
495 return *j.deviceProperties.Min_sdk_version
496 }
497 return j.sdkVersion()
498}
499
Dan Willemsen419290a2018-10-31 15:28:47 -0700500func (j *Module) targetSdkVersion() string {
501 if j.deviceProperties.Target_sdk_version != nil {
502 return *j.deviceProperties.Target_sdk_version
503 }
504 return j.sdkVersion()
505}
506
Colin Crossbe1da472017-07-07 15:59:46 -0700507func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700508 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100509 sdkDep := decodeSdkDep(ctx, sdkContext(j))
510 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700511 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700512 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100513 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100514 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700515 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700516 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700517 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100518 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700519 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700520 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700521 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
522 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800523 }
Colin Crossbe1da472017-07-07 15:59:46 -0700524 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700525 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100526 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100527 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100528 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700529 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700530 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100531 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700532 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800533 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800534 if ctx.ModuleName() == "android_stubs_current" ||
535 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700536 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700537 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800538 }
Colin Cross2fe66872015-03-30 17:20:39 -0700539 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700540
Colin Cross42d48b72018-08-29 14:10:52 -0700541 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
542 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700543
Colin Crossbe9cdb82019-01-21 21:37:16 -0800544 ctx.AddFarVariationDependencies([]blueprint.Variation{
545 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
546 }, pluginTag, j.properties.Plugins...)
547
Colin Crossfe17f6f2019-03-28 19:30:56 -0700548 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700549 if j.hasSrcExt(".proto") {
550 protoDeps(ctx, &j.protoProperties)
551 }
Colin Cross93e85952017-08-15 13:34:18 -0700552
553 if j.hasSrcExt(".kt") {
554 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
555 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700556 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
557 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800558 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800559 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
560 }
Colin Cross93e85952017-08-15 13:34:18 -0700561 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800562
563 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700564 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800565 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700566}
567
568func hasSrcExt(srcs []string, ext string) bool {
569 for _, src := range srcs {
570 if filepath.Ext(src) == ext {
571 return true
572 }
573 }
574
575 return false
576}
577
Nan Zhang61eaedb2017-11-02 13:28:15 -0700578func shardPaths(paths android.Paths, shardSize int) []android.Paths {
579 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
580 for len(paths) > shardSize {
581 ret = append(ret, paths[0:shardSize])
582 paths = paths[shardSize:]
583 }
584 if len(paths) > 0 {
585 ret = append(ret, paths)
586 }
587 return ret
588}
589
Colin Cross6af17aa2017-09-20 12:59:05 -0700590func (j *Module) hasSrcExt(ext string) bool {
591 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700592}
593
Colin Cross46c9b8b2017-06-22 16:51:17 -0700594func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700595 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700596
Colin Crossebe1a512017-11-14 13:12:14 -0800597 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
598 aidlIncludes = append(aidlIncludes,
599 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
600 aidlIncludes = append(aidlIncludes,
601 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700602
Colin Cross3047fa22019-04-18 10:56:44 -0700603 var flags []string
604 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700605
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700606 if aidlPreprocess.Valid() {
607 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700608 deps = append(deps, aidlPreprocess.Path())
609 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700610 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700611 }
612
Colin Cross3047fa22019-04-18 10:56:44 -0700613 if len(j.exportAidlIncludeDirs) > 0 {
614 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
615 }
616
617 if len(aidlIncludes) > 0 {
618 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
619 }
620
Colin Cross635c3b02016-05-18 15:37:25 -0700621 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800622 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700623 flags = append(flags, "-I"+src.String())
624 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700625
Martijn Coeneneab15642018-03-09 09:29:59 +0100626 if Bool(j.deviceProperties.Aidl.Generate_traces) {
627 flags = append(flags, "-t")
628 }
629
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100630 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
631 flags = append(flags, "--transaction_names")
632 }
633
Colin Cross3047fa22019-04-18 10:56:44 -0700634 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700635}
636
Colin Cross32f676a2017-09-06 13:41:06 -0700637type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800638 classpath classpath
639 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700640 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800641 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700642 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700643 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700644 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700645 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800646 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700647 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700648 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700649 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700650 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800651 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800652
653 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700654}
Colin Cross2fe66872015-03-30 17:20:39 -0700655
Colin Cross54250902017-12-05 09:28:08 -0800656func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
657 for _, f := range dep.Srcs() {
658 if f.Ext() != ".jar" {
659 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
660 ctx.OtherModuleName(dep.(blueprint.Module)))
661 }
662 }
663}
664
Jiyong Park2d492942018-03-05 17:44:10 +0900665type linkType int
666
667const (
668 javaCore linkType = iota
669 javaSdk
670 javaSystem
671 javaPlatform
672)
673
Jiyong Park46f78fb2018-10-20 16:33:17 +0900674func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700675 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700676 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900677 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
678 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100679 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900680 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100681 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900682 return javaCore, false
683 case name == "android_system_stubs_current":
684 return javaSystem, true
685 case strings.HasPrefix(ver, "system_"):
686 return javaSystem, false
687 case name == "android_test_stubs_current":
688 return javaSystem, true
689 case strings.HasPrefix(ver, "test_"):
690 return javaPlatform, false
691 case name == "android_stubs_current":
692 return javaSdk, true
693 case ver == "current":
694 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100695 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900696 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700697 default:
698 if _, err := strconv.Atoi(ver); err != nil {
699 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
700 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900701 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900702 }
703}
704
Jiyong Park750e5572018-01-31 00:20:13 +0900705func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700706 if ctx.Host() {
707 return
708 }
709
Jiyong Park46f78fb2018-10-20 16:33:17 +0900710 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
711 if stubs {
712 return
713 }
714 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900715 commonMessage := "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source."
716
717 switch myLinkType {
718 case javaCore:
719 if otherLinkType != javaCore {
720 ctx.ModuleErrorf("compiles against core Java API, but dependency %q is compiling against non-core Java APIs."+commonMessage,
Jiyong Park750e5572018-01-31 00:20:13 +0900721 ctx.OtherModuleName(to))
722 }
Jiyong Park2d492942018-03-05 17:44:10 +0900723 break
724 case javaSdk:
725 if otherLinkType != javaCore && otherLinkType != javaSdk {
726 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
727 ctx.OtherModuleName(to))
728 }
729 break
730 case javaSystem:
731 if otherLinkType == javaPlatform {
732 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
733 ctx.OtherModuleName(to))
734 }
735 break
736 case javaPlatform:
737 // no restriction on link-type
738 break
Jiyong Park750e5572018-01-31 00:20:13 +0900739 }
740}
741
Colin Cross32f676a2017-09-06 13:41:06 -0700742func (j *Module) collectDeps(ctx android.ModuleContext) deps {
743 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700744
Colin Cross300f0382018-03-06 13:11:51 -0800745 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700746 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800747 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700748 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800749 } else if sdkDep.useFiles {
750 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700751 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700752 deps.aidlPreprocess = sdkDep.aidl
753 } else {
754 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800755 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700756 }
757
Colin Crossd11fcda2017-10-23 17:59:01 -0700758 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700759 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700760 tag := ctx.OtherModuleDependencyTag(module)
761
Colin Crossa4f08812018-10-02 22:03:40 -0700762 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700763 // Handled by AndroidApp.collectAppDeps
764 return
765 }
766 if tag == certificateTag {
767 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700768 return
769 }
770
Jiyong Park750e5572018-01-31 00:20:13 +0900771 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700772 switch tag {
773 case bootClasspathTag, libTag, staticLibTag:
774 checkLinkType(ctx, j, to, tag.(dependencyTag))
775 }
Jiyong Park750e5572018-01-31 00:20:13 +0900776 }
Colin Cross54250902017-12-05 09:28:08 -0800777 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800778 case SdkLibraryDependency:
779 switch tag {
780 case libTag:
781 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
782 // names of sdk libs that are directly depended are exported
783 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700784 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800785 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
786 }
Colin Cross54250902017-12-05 09:28:08 -0800787 case Dependency:
788 switch tag {
789 case bootClasspathTag:
790 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700791 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800792 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900793 // sdk lib names from dependencies are re-exported
794 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700795 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800796 case staticLibTag:
797 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
798 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
799 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700800 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900801 // sdk lib names from dependencies are re-exported
802 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700803 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800804 case pluginTag:
805 if plugin, ok := dep.(*Plugin); ok {
806 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
807 if plugin.pluginProperties.Processor_class != nil {
808 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
809 }
810 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
811 } else {
812 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
813 }
Colin Cross54250902017-12-05 09:28:08 -0800814 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100815 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800816 // framework.jar has a one-off dependency on the R.java and Manifest.java files
817 // generated by framework-res.apk
818 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
819 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800820 case frameworkApkTag:
821 if ctx.ModuleName() == "android_stubs_current" ||
822 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700823 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800824 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
825 // resource files out of there for aapt.
826 //
827 // Normally the package rule runs aapt, which includes the resource,
828 // but we're not running that in our package rule so just copy in the
829 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700830 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800831 }
Colin Cross54250902017-12-05 09:28:08 -0800832 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700833 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800834 case kotlinAnnotationsTag:
835 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800836 }
837
Colin Cross54250902017-12-05 09:28:08 -0800838 case android.SourceFileProducer:
839 switch tag {
840 case libTag:
841 checkProducesJars(ctx, dep)
842 deps.classpath = append(deps.classpath, dep.Srcs()...)
843 case staticLibTag:
844 checkProducesJars(ctx, dep)
845 deps.classpath = append(deps.classpath, dep.Srcs()...)
846 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
847 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800848 }
849 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700850 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700851 case systemModulesTag:
852 if deps.systemModules != nil {
853 panic("Found two system module dependencies")
854 }
855 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000856 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700857 panic("Missing directory for system module dependency")
858 }
Colin Crossb77043e2019-07-16 13:57:13 -0700859 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700860 }
Colin Crossec7a0422017-07-07 14:47:12 -0700861 }
Colin Cross2fe66872015-03-30 17:20:39 -0700862 })
863
Jiyong Park1be96912018-05-28 18:02:19 +0900864 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
865
Colin Cross32f676a2017-09-06 13:41:06 -0700866 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700867}
868
Colin Cross83bb3162018-06-25 15:48:06 -0700869func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700870 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800871 v := sdkContext.sdkVersion()
872 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100873 if ctx.Config().IsPdkBuild() &&
874 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700875 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800876 latestSdkVersion := 0
877 if len(sdkVersions) > 0 {
878 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
879 }
880 v = strconv.Itoa(latestSdkVersion)
881 }
882
883 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700884 if err != nil {
885 ctx.PropertyErrorf("sdk_version", "%s", err)
886 }
Nan Zhang357466b2018-04-17 17:38:36 -0700887 if javaVersion != "" {
888 ret = javaVersion
889 } else if ctx.Device() && sdk <= 23 {
890 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100891 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700892 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100893 } else if ctx.Device() &&
894 sdkContext.sdkVersion() != "" &&
895 sdkContext.sdkVersion() != "none" &&
896 sdkContext.sdkVersion() != "core_platform" &&
897 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700898 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
899 ret = "1.8"
900 } else {
901 ret = "1.9"
902 }
903
904 return ret
905}
906
Nan Zhanged19fc32017-10-19 13:06:22 -0700907func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700908
Colin Crossf03c82b2015-04-13 13:53:40 -0700909 var flags javaBuilderFlags
910
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100911 // javaVersion flag.
912 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
913
Nan Zhanged19fc32017-10-19 13:06:22 -0700914 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700915 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100916 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700917 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700918 }
Colin Cross6510f912017-11-29 00:27:14 -0800919 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700920 // Override the -g flag passed globally to remove local variable debug info to reduce
921 // disk and memory usage.
922 javacFlags = append(javacFlags, "-g:source,lines")
923 }
Colin Cross64162712017-08-08 13:17:59 -0700924
Colin Cross66548102018-06-19 22:47:35 -0700925 if ctx.Config().RunErrorProne() {
926 if config.ErrorProneClasspath == nil {
927 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
928 }
929
930 errorProneFlags := []string{
931 "-Xplugin:ErrorProne",
932 "${config.ErrorProneChecks}",
933 }
934 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
935
936 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
937 "'" + strings.Join(errorProneFlags, " ") + "'"
938 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800939 }
940
Nan Zhanged19fc32017-10-19 13:06:22 -0700941 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800942 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
943 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700944 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800945
Colin Crossbe9cdb82019-01-21 21:37:16 -0800946 flags.processor = strings.Join(deps.processorClasses, ",")
947
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100948 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100949 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800950 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
951 // Give host-side tools a version of OpenJDK's standard libraries
952 // close to what they're targeting. As of Dec 2017, AOSP is only
953 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
954 //
955 // When building with OpenJDK 8, the following should have no
956 // effect since those jars would be available by default.
957 //
958 // When building with OpenJDK 9 but targeting a version < 1.8,
959 // putting them on the bootclasspath means that:
960 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
961 // b) references to existing APIs are not reinterpreted in an
962 // OpenJDK 9-specific way, eg. calls to subclasses of
963 // java.nio.Buffer as in http://b/70862583
964 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
965 flags.bootClasspath = append(flags.bootClasspath,
966 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
967 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800968 if Bool(j.properties.Use_tools_jar) {
969 flags.bootClasspath = append(flags.bootClasspath,
970 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
971 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800972 }
973
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100974 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800975 // Manually specify build directory in case it is not under the repo root.
976 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
977 // just adding a symlink under the root doesn't help.)
978 patchPaths := ".:" + ctx.Config().BuildDir()
979 classPath := flags.classpath.FormJavaClassPath("")
980 if classPath != "" {
981 patchPaths += ":" + classPath
982 }
983 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700984 }
985
Nan Zhanged19fc32017-10-19 13:06:22 -0700986 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -0700987 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700988
Nan Zhanged19fc32017-10-19 13:06:22 -0700989 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700990 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700991
Colin Cross81440082018-08-15 20:21:55 -0700992 if len(javacFlags) > 0 {
993 // optimization.
994 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
995 flags.javacFlags = "$javacFlags"
996 }
997
Nan Zhanged19fc32017-10-19 13:06:22 -0700998 return flags
999}
Colin Crossc0b06f12015-04-08 13:03:43 -07001000
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001001func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
1002
Colin Crossebe1a512017-11-14 13:12:14 -08001003 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001004
1005 deps := j.collectDeps(ctx)
1006 flags := j.collectBuilderFlags(ctx, deps)
1007
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001008 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001009 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1010 }
Colin Cross8a497952019-03-05 22:25:09 -08001011 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001012 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001013 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001014 }
1015
Colin Crossaf050172017-11-15 23:01:59 -08001016 srcFiles = j.genSources(ctx, srcFiles, flags)
1017
1018 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001019 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001020 if aaptSrcJar != nil {
1021 srcJars = append(srcJars, aaptSrcJar)
1022 }
Colin Crossb7a63242015-04-16 14:09:14 -07001023
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001024 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1025 // that IDEInfo struct will use
1026 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1027
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001028 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001029 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001030 }
1031
Colin Cross1ee23172017-10-18 14:44:18 -07001032 jarName := ctx.ModuleName() + ".jar"
1033
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001034 javaSrcFiles := srcFiles.FilterByExt(".java")
1035 var uniqueSrcFiles android.Paths
1036 set := make(map[string]bool)
1037 for _, v := range javaSrcFiles {
1038 if _, found := set[v.String()]; !found {
1039 set[v.String()] = true
1040 uniqueSrcFiles = append(uniqueSrcFiles, v)
1041 }
1042 }
1043
Colin Cross55f63ea2018-08-27 12:37:09 -07001044 var kotlinJars android.Paths
1045
Colin Cross93e85952017-08-15 13:34:18 -07001046 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001047 // user defined kotlin flags.
1048 kotlincFlags := j.properties.Kotlincflags
1049 CheckKotlincFlags(ctx, kotlincFlags)
1050
Colin Cross93e85952017-08-15 13:34:18 -07001051 // If there are kotlin files, compile them first but pass all the kotlin and java files
1052 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1053 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001054 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001055 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001056 kotlincFlags = append(kotlincFlags, "-no-jdk")
1057 }
1058 if len(kotlincFlags) > 0 {
1059 // optimization.
1060 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1061 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001062 }
1063
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001064 var kotlinSrcFiles android.Paths
1065 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1066 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1067
Colin Crossafbb1732019-01-17 15:42:52 -08001068 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1069 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1070
1071 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1072 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1073
1074 if len(flags.processorPath) > 0 {
1075 // Use kapt for annotation processing
1076 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1077 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1078 srcJars = append(srcJars, kaptSrcJar)
1079 // Disable annotation processing in javac, it's already been handled by kapt
1080 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001081 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001082 }
Colin Cross93e85952017-08-15 13:34:18 -07001083
Colin Cross1ee23172017-10-18 14:44:18 -07001084 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001085 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001086 if ctx.Failed() {
1087 return
1088 }
1089
1090 // Make javac rule depend on the kotlinc rule
1091 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001092
Colin Cross93e85952017-08-15 13:34:18 -07001093 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001094 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001095 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001096 }
1097
Colin Cross55f63ea2018-08-27 12:37:09 -07001098 jars := append(android.Paths(nil), kotlinJars...)
1099
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001100 // Store the list of .java files that was passed to javac
1101 j.compiledJavaSrcs = uniqueSrcFiles
1102 j.compiledSrcJars = srcJars
1103
Nan Zhang61eaedb2017-11-02 13:28:15 -07001104 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001105 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001106 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1107 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001108 // Formerly, there was a check here that prevented annotation processors
1109 // from being used when sharding was enabled, as some annotation processors
1110 // do not function correctly in sharded environments. It was removed to
1111 // allow for the use of annotation processors that do function correctly
1112 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001113 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001114 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001115 if ctx.Failed() {
1116 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001117 }
1118 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001119 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001120 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001121 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001122 // If error-prone is enabled, add an additional rule to compile the java files into
1123 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001124 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001125 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1126 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001127 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001128 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001129 extraJarDeps = append(extraJarDeps, errorprone)
1130 }
1131
Nan Zhang61eaedb2017-11-02 13:28:15 -07001132 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001133 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001134 shardSize := int(*(j.properties.Javac_shard_size))
1135 var shardSrcs []android.Paths
1136 if len(uniqueSrcFiles) > 0 {
1137 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1138 for idx, shardSrc := range shardSrcs {
1139 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1140 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1141 jars = append(jars, classes)
1142 }
1143 }
1144 if len(srcJars) > 0 {
1145 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1146 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1147 jars = append(jars, classes)
1148 }
1149 } else {
1150 classes := android.PathForModuleOut(ctx, "javac", jarName)
1151 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1152 jars = append(jars, classes)
1153 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001154 if ctx.Config().EmitXrefRules() {
1155 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1156 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1157 j.kytheFiles = append(j.kytheFiles, extractionFile)
1158
1159 }
Colin Crossd6891432017-09-27 17:39:56 -07001160 if ctx.Failed() {
1161 return
1162 }
Colin Cross2fe66872015-03-30 17:20:39 -07001163 }
1164
Colin Cross0c4ce212019-05-03 15:28:19 -07001165 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1166
1167 var includeSrcJar android.WritablePath
1168 if Bool(j.properties.Include_srcs) {
1169 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1170 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1171 }
1172
Colin Crosscedd4762018-09-13 11:26:19 -07001173 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1174 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001175 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001176 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001177
1178 var resArgs []string
1179 var resDeps android.Paths
1180
1181 resArgs = append(resArgs, dirArgs...)
1182 resDeps = append(resDeps, dirDeps...)
1183
1184 resArgs = append(resArgs, fileArgs...)
1185 resDeps = append(resDeps, fileDeps...)
1186
Colin Cross988708c2019-05-06 14:04:11 -07001187 resArgs = append(resArgs, extraArgs...)
1188 resDeps = append(resDeps, extraDeps...)
1189
Colin Cross40a36712017-09-27 17:41:35 -07001190 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001191 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001192 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001193 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001194 if ctx.Failed() {
1195 return
1196 }
1197 }
1198
Colin Cross0c4ce212019-05-03 15:28:19 -07001199 var resourceJars android.Paths
1200 if j.resourceJar != nil {
1201 resourceJars = append(resourceJars, j.resourceJar)
1202 }
1203 if Bool(j.properties.Include_srcs) {
1204 resourceJars = append(resourceJars, includeSrcJar)
1205 }
1206 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001207
Colin Cross0c4ce212019-05-03 15:28:19 -07001208 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001209 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001210 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001211 false, nil, nil)
1212 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001213 } else if len(resourceJars) == 1 {
1214 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001215 }
1216
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001217 if len(deps.staticJars) > 0 {
1218 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001219 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001220
Colin Cross094054a2018-10-17 15:10:48 -07001221 manifest := j.overrideManifest
1222 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001223 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001224 }
Colin Cross635acc92017-09-12 22:50:46 -07001225
Colin Cross8a497952019-03-05 22:25:09 -08001226 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001227 if len(services) > 0 {
1228 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1229 var zipargs []string
1230 for _, file := range services {
1231 serviceFile := file.String()
1232 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1233 }
1234 ctx.Build(pctx, android.BuildParams{
1235 Rule: zip,
1236 Output: servicesJar,
1237 Implicits: services,
1238 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001239 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001240 },
1241 })
1242 jars = append(jars, servicesJar)
1243 }
1244
Colin Cross0a6e0072017-08-30 14:24:55 -07001245 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001246 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001247 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001248
1249 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001250 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1251 // Optimization: skip the combine step if there is nothing to do
1252 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1253 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1254 // any if len(jars) == 1.
1255 outputFile = moduleOutPath
1256 } else {
1257 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1258 ctx.Build(pctx, android.BuildParams{
1259 Rule: android.Cp,
1260 Input: jars[0],
1261 Output: combinedJar,
1262 })
1263 outputFile = combinedJar
1264 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001265 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001266 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001267 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001268 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001269 outputFile = combinedJar
1270 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001271
Colin Cross331a1212018-08-15 20:40:52 -07001272 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001273 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001274 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001275 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001276 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001277 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001278
1279 // jarjar resource jar if necessary
1280 if j.resourceJar != nil {
1281 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001282 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001283 j.resourceJar = resourceJarJarFile
1284 }
1285
Colin Cross0a6e0072017-08-30 14:24:55 -07001286 if ctx.Failed() {
1287 return
1288 }
1289 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001290
1291 // Check package restrictions if necessary.
1292 if len(j.properties.Permitted_packages) > 0 {
1293 // Check packages and copy to package-checked file.
1294 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1295 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1296 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1297
1298 if ctx.Failed() {
1299 return
1300 }
1301 }
1302
Nan Zhanged19fc32017-10-19 13:06:22 -07001303 j.implementationJarFile = outputFile
1304 if j.headerJarFile == nil {
1305 j.headerJarFile = j.implementationJarFile
1306 }
Colin Cross2fe66872015-03-30 17:20:39 -07001307
Colin Cross6510f912017-11-29 00:27:14 -08001308 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001309 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1310 j.properties.Instrument = true
1311 }
1312 }
1313
Colin Cross3144dfc2018-01-03 15:06:47 -08001314 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001315 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1316 }
1317
Colin Cross331a1212018-08-15 20:40:52 -07001318 // merge implementation jar with resources if necessary
1319 implementationAndResourcesJar := outputFile
1320 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001321 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001322 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001323 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001324 false, nil, nil)
1325 implementationAndResourcesJar = combinedJar
1326 }
1327
1328 j.implementationAndResourcesJar = implementationAndResourcesJar
1329
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001330 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001331 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001332 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001333 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001334 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001335 if ctx.Failed() {
1336 return
1337 }
Colin Cross331a1212018-08-15 20:40:52 -07001338
Jiyong Park09cb6292019-07-15 15:29:23 +09001339 // Hidden API CSV generation and dex encoding
1340 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1341 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001342
Colin Cross331a1212018-08-15 20:40:52 -07001343 // merge dex jar with resources if necessary
1344 if j.resourceJar != nil {
1345 jars := android.Paths{dexOutputFile, j.resourceJar}
1346 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1347 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1348 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001349 if j.deviceProperties.UncompressDex {
1350 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1351 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1352 dexOutputFile = combinedAlignedJar
1353 } else {
1354 dexOutputFile = combinedJar
1355 }
Colin Cross331a1212018-08-15 20:40:52 -07001356 }
1357
1358 j.dexJarFile = dexOutputFile
1359
Colin Cross8faf8fc2019-01-16 15:15:52 -08001360 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001361 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1362
1363 j.maybeStrippedDexJarFile = dexOutputFile
1364
Colin Cross3063b782018-08-15 11:19:12 -07001365 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001366
1367 if ctx.Failed() {
1368 return
1369 }
Colin Cross331a1212018-08-15 20:40:52 -07001370 } else {
1371 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001372 }
Colin Cross331a1212018-08-15 20:40:52 -07001373
Colin Crossb7a63242015-04-16 14:09:14 -07001374 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001375
1376 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1377 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001378}
1379
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001380// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1381// since some of these flags may be used internally.
1382func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1383 for _, flag := range flags {
1384 flag = strings.TrimSpace(flag)
1385
1386 if !strings.HasPrefix(flag, "-") {
1387 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1388 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1389 ctx.PropertyErrorf("kotlincflags",
1390 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1391 } else if inList(flag, config.KotlincIllegalFlags) {
1392 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1393 } else if flag == "-include-runtime" {
1394 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1395 } else {
1396 args := strings.Split(flag, " ")
1397 if args[0] == "-kotlin-home" {
1398 ctx.PropertyErrorf("kotlincflags",
1399 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1400 }
1401 }
1402 }
1403}
1404
Colin Cross8eadbf02017-10-24 17:46:00 -07001405func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001406 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001407
1408 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001409 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001410 // Compile java sources into turbine.jar.
1411 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1412 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1413 if ctx.Failed() {
1414 return nil
1415 }
1416 jars = append(jars, turbineJar)
1417 }
1418
Colin Cross55f63ea2018-08-27 12:37:09 -07001419 jars = append(jars, extraJars...)
1420
Nan Zhanged19fc32017-10-19 13:06:22 -07001421 // Combine any static header libraries into classes-header.jar. If there is only
1422 // one input jar this step will be skipped.
1423 var headerJar android.Path
1424 jars = append(jars, deps.staticHeaderJars...)
1425
Colin Cross5c6ecc12017-10-23 18:12:27 -07001426 // we cannot skip the combine step for now if there is only one jar
1427 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1428 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001429 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001430 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001431 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001432
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001433 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001434 // Transform classes.jar into classes-jarjar.jar
1435 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001436 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001437 headerJar = jarjarFile
1438 if ctx.Failed() {
1439 return nil
1440 }
1441 }
1442
1443 return headerJar
1444}
1445
Colin Crosscb933592017-11-22 13:49:43 -08001446func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001447 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001448
Colin Cross7a3139e2017-12-19 13:57:50 -08001449 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001450
Colin Cross84c38822018-01-03 15:59:46 -08001451 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001452 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1453
1454 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1455
1456 j.jacocoReportClassesFile = jacocoReportClassesFile
1457
1458 return instrumentedJar
1459}
1460
albaltai36ff7dc2018-12-25 14:35:23 +08001461var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001462
Nan Zhanged19fc32017-10-19 13:06:22 -07001463func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001464 if j.headerJarFile == nil {
1465 return nil
1466 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001467 return android.Paths{j.headerJarFile}
1468}
1469
1470func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001471 if j.implementationJarFile == nil {
1472 return nil
1473 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001474 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001475}
1476
Colin Crossf24a22a2019-01-31 14:12:44 -08001477func (j *Module) DexJar() android.Path {
1478 return j.dexJarFile
1479}
1480
Colin Cross331a1212018-08-15 20:40:52 -07001481func (j *Module) ResourceJars() android.Paths {
1482 if j.resourceJar == nil {
1483 return nil
1484 }
1485 return android.Paths{j.resourceJar}
1486}
1487
1488func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001489 if j.implementationAndResourcesJar == nil {
1490 return nil
1491 }
Colin Cross331a1212018-08-15 20:40:52 -07001492 return android.Paths{j.implementationAndResourcesJar}
1493}
1494
Colin Cross46c9b8b2017-06-22 16:51:17 -07001495func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001496 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001497 return j.exportAidlIncludeDirs
1498}
1499
Jiyong Park1be96912018-05-28 18:02:19 +09001500func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001501 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001502 return j.exportedSdkLibs
1503}
1504
Colin Cross0c4ce212019-05-03 15:28:19 -07001505func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1506 return j.srcJarArgs, j.srcJarDeps
1507}
1508
Colin Cross46c9b8b2017-06-22 16:51:17 -07001509var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001510
Colin Cross46c9b8b2017-06-22 16:51:17 -07001511func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001512 return j.logtagsSrcs
1513}
1514
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001515// Collect information for opening IDE project files in java/jdeps.go.
1516func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1517 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1518 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001519 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001520 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001521 if j.expandJarjarRules != nil {
1522 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001523 }
1524}
1525
1526func (j *Module) CompilerDeps() []string {
1527 jdeps := []string{}
1528 jdeps = append(jdeps, j.properties.Libs...)
1529 jdeps = append(jdeps, j.properties.Static_libs...)
1530 return jdeps
1531}
1532
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001533func (j *Module) hasCode(ctx android.ModuleContext) bool {
1534 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1535 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1536}
1537
Colin Cross2fe66872015-03-30 17:20:39 -07001538//
1539// Java libraries (.jar file)
1540//
1541
Colin Crossf506d872017-07-19 15:53:04 -07001542type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001543 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001544}
1545
Colin Cross42be7612019-02-21 18:12:14 -08001546func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001547 // Store uncompressed (and do not strip) dex files from boot class path jars.
1548 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1549 return true
1550 }
1551
1552 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001553 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001554 return true
1555 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001556 if ctx.Config().UncompressPrivAppDex() &&
1557 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1558 return true
1559 }
1560
Colin Cross2fc72f62018-12-21 12:59:54 -08001561 return false
1562}
1563
Colin Crossf506d872017-07-19 15:53:04 -07001564func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001565 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1566 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001567 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001568 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001569 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001570 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001571
Jiyong Park7f7766d2019-07-25 22:02:35 +09001572 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1573 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001574 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1575 ctx.ModuleName()+".jar", j.outputFile)
1576 }
Colin Crossb7a63242015-04-16 14:09:14 -07001577}
1578
Colin Crossf506d872017-07-19 15:53:04 -07001579func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001580 j.deps(ctx)
1581}
1582
Colin Cross1b16b0e2019-02-12 14:41:32 -08001583// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1584//
1585// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1586// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1587// as a `static_libs` dependency of another module.
1588//
1589// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1590// a device.
1591//
1592// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1593// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001594func LibraryFactory() android.Module {
1595 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001596
Colin Cross9ae1b922018-06-26 17:59:05 -07001597 module.AddProperties(
1598 &module.Module.properties,
1599 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001600 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001601 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001602
Colin Cross9ae1b922018-06-26 17:59:05 -07001603 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001604 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001605 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001606}
1607
Colin Cross1b16b0e2019-02-12 14:41:32 -08001608// java_library_static is an obsolete alias for java_library.
1609func LibraryStaticFactory() android.Module {
1610 return LibraryFactory()
1611}
1612
1613// java_library_host builds and links sources into a `.jar` file for the host.
1614//
1615// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1616// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001617func LibraryHostFactory() android.Module {
1618 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001619
Colin Cross6af17aa2017-09-20 12:59:05 -07001620 module.AddProperties(
1621 &module.Module.properties,
1622 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001623
Colin Cross9ae1b922018-06-26 17:59:05 -07001624 module.Module.properties.Installable = proptools.BoolPtr(true)
1625
Colin Cross89536d42017-07-07 14:35:50 -07001626 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001627 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001628 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001629}
1630
1631//
Colin Crossb628ea52018-08-14 16:42:33 -07001632// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001633//
1634
1635type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001636 // list of compatibility suites (for example "cts", "vts") that the module should be
1637 // installed into.
1638 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001639
1640 // the name of the test configuration (for example "AndroidTest.xml") that should be
1641 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001642 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001643
Jack He33338892018-09-19 02:21:28 -07001644 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1645 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001646 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001647
Colin Crossd96ca352018-08-10 16:06:24 -07001648 // list of files or filegroup modules that provide data that should be installed alongside
1649 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001650 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001651}
1652
Paul Duffin42df1442019-03-20 12:45:53 +00001653type testHelperLibraryProperties struct {
1654 // list of compatibility suites (for example "cts", "vts") that the module should be
1655 // installed into.
1656 Test_suites []string `android:"arch_variant"`
1657}
1658
Colin Cross05638fc2018-04-09 18:40:24 -07001659type Test struct {
1660 Library
1661
1662 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001663
1664 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001665 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001666}
1667
Paul Duffin42df1442019-03-20 12:45:53 +00001668type TestHelperLibrary struct {
1669 Library
1670
1671 testHelperLibraryProperties testHelperLibraryProperties
1672}
1673
Colin Cross303e21f2018-08-07 16:49:25 -07001674func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001675 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, j.testProperties.Test_suites)
Colin Cross8a497952019-03-05 22:25:09 -08001676 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001677
1678 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001679}
1680
Paul Duffin42df1442019-03-20 12:45:53 +00001681func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1682 j.Library.GenerateAndroidBuildActions(ctx)
1683}
1684
Colin Cross1b16b0e2019-02-12 14:41:32 -08001685// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1686// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1687//
1688// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1689// compiled against the device bootclasspath.
1690//
1691// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1692// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001693func TestFactory() android.Module {
1694 module := &Test{}
1695
1696 module.AddProperties(
1697 &module.Module.properties,
1698 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001699 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001700 &module.Module.protoProperties,
1701 &module.testProperties)
1702
Colin Cross9ae1b922018-06-26 17:59:05 -07001703 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001704 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001705
Colin Cross05638fc2018-04-09 18:40:24 -07001706 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001707 return module
1708}
1709
Paul Duffin42df1442019-03-20 12:45:53 +00001710// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1711func TestHelperLibraryFactory() android.Module {
1712 module := &TestHelperLibrary{}
1713
1714 module.AddProperties(
1715 &module.Module.properties,
1716 &module.Module.deviceProperties,
1717 &module.Module.dexpreoptProperties,
1718 &module.Module.protoProperties,
1719 &module.testHelperLibraryProperties)
1720
Colin Cross9a4abed2019-04-24 13:19:28 -07001721 module.Module.properties.Installable = proptools.BoolPtr(true)
1722 module.Module.dexpreopter.isTest = true
1723
Paul Duffin42df1442019-03-20 12:45:53 +00001724 InitJavaModule(module, android.HostAndDeviceSupported)
1725 return module
1726}
1727
Colin Cross1b16b0e2019-02-12 14:41:32 -08001728// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1729// allow running the test with `atest` or a `TEST_MAPPING` file.
1730//
1731// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1732// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001733func TestHostFactory() android.Module {
1734 module := &Test{}
1735
1736 module.AddProperties(
1737 &module.Module.properties,
1738 &module.Module.protoProperties,
1739 &module.testProperties)
1740
Colin Cross9ae1b922018-06-26 17:59:05 -07001741 module.Module.properties.Installable = proptools.BoolPtr(true)
1742
Colin Cross05638fc2018-04-09 18:40:24 -07001743 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001744 return module
1745}
1746
1747//
Colin Cross2fe66872015-03-30 17:20:39 -07001748// Java Binaries (.jar file plus wrapper script)
1749//
1750
Colin Crossf506d872017-07-19 15:53:04 -07001751type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001752 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001753 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001754
1755 // Name of the class containing main to be inserted into the manifest as Main-Class.
1756 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001757}
1758
Colin Crossf506d872017-07-19 15:53:04 -07001759type Binary struct {
1760 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001761
Colin Crossf506d872017-07-19 15:53:04 -07001762 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001763
Colin Cross6b4a32d2017-12-05 13:42:45 -08001764 isWrapperVariant bool
1765
Colin Crossc3315992017-12-08 19:12:36 -08001766 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001767 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001768}
1769
Alex Light24237172017-10-26 09:46:21 -07001770func (j *Binary) HostToolPath() android.OptionalPath {
1771 return android.OptionalPathForPath(j.binaryFile)
1772}
1773
Colin Crossf506d872017-07-19 15:53:04 -07001774func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001775 if ctx.Arch().ArchType == android.Common {
1776 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001777 if j.binaryProperties.Main_class != nil {
1778 if j.properties.Manifest != nil {
1779 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1780 }
1781 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1782 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1783 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1784 }
1785
Colin Cross6b4a32d2017-12-05 13:42:45 -08001786 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001787 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001788 // Handle the binary wrapper
1789 j.isWrapperVariant = true
1790
Colin Cross366938f2017-12-11 16:29:02 -08001791 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001792 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001793 } else {
1794 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1795 }
1796
1797 // Depend on the installed jar so that the wrapper doesn't get executed by
1798 // another build rule before the jar has been installed.
1799 jarFile := ctx.PrimaryModule().(*Binary).installFile
1800
1801 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1802 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001803 }
Colin Cross2fe66872015-03-30 17:20:39 -07001804}
1805
Colin Crossf506d872017-07-19 15:53:04 -07001806func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001807 if ctx.Arch().ArchType == android.Common {
1808 j.deps(ctx)
1809 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001810}
1811
Colin Cross1b16b0e2019-02-12 14:41:32 -08001812// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1813// as well.
1814//
1815// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1816// compiled against the device bootclasspath.
1817//
1818// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1819// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001820func BinaryFactory() android.Module {
1821 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001822
Colin Cross36242852017-06-23 15:06:31 -07001823 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001824 &module.Module.properties,
1825 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001826 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001827 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001828 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001829
Colin Cross9ae1b922018-06-26 17:59:05 -07001830 module.Module.properties.Installable = proptools.BoolPtr(true)
1831
Colin Cross6b4a32d2017-12-05 13:42:45 -08001832 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1833 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001834 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001835}
1836
Colin Cross1b16b0e2019-02-12 14:41:32 -08001837// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1838//
1839// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1840// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001841func BinaryHostFactory() android.Module {
1842 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001843
Colin Cross36242852017-06-23 15:06:31 -07001844 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001845 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001846 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001847 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001848
Colin Cross9ae1b922018-06-26 17:59:05 -07001849 module.Module.properties.Installable = proptools.BoolPtr(true)
1850
Colin Cross6b4a32d2017-12-05 13:42:45 -08001851 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1852 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001853 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001854}
1855
1856//
1857// Java prebuilts
1858//
1859
Colin Cross74d73e22017-08-02 11:05:49 -07001860type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001861 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001862
Nan Zhangea568a42017-11-08 21:20:04 -08001863 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001864
1865 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001866
1867 // List of shared java libs that this module has dependencies to
1868 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001869
1870 // List of files to remove from the jar file(s)
1871 Exclude_files []string
1872
1873 // List of directories to remove from the jar file(s)
1874 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001875
1876 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001877 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001878}
1879
1880type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001881 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001882 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001883 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001884 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001885
Colin Cross74d73e22017-08-02 11:05:49 -07001886 properties ImportProperties
1887
Colin Cross0a6e0072017-08-30 14:24:55 -07001888 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001889 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001890}
1891
Colin Cross83bb3162018-06-25 15:48:06 -07001892func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001893 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001894}
1895
1896func (j *Import) minSdkVersion() string {
1897 return j.sdkVersion()
1898}
1899
Colin Cross74d73e22017-08-02 11:05:49 -07001900func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001901 return &j.prebuilt
1902}
1903
Colin Cross74d73e22017-08-02 11:05:49 -07001904func (j *Import) PrebuiltSrcs() []string {
1905 return j.properties.Jars
1906}
1907
1908func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001909 return j.prebuilt.Name(j.ModuleBase.Name())
1910}
1911
Colin Cross74d73e22017-08-02 11:05:49 -07001912func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001913 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001914}
1915
Colin Cross74d73e22017-08-02 11:05:49 -07001916func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001917 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001918
Nan Zhang4c819fb2018-08-27 18:31:46 -07001919 jarName := ctx.ModuleName() + ".jar"
1920 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001921 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1922 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001923 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001924 inputFile := outputFile
1925 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1926 TransformJetifier(ctx, outputFile, inputFile)
1927 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001928 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001929
1930 ctx.VisitDirectDeps(func(module android.Module) {
1931 otherName := ctx.OtherModuleName(module)
1932 tag := ctx.OtherModuleDependencyTag(module)
1933
1934 switch dep := module.(type) {
1935 case Dependency:
1936 switch tag {
1937 case libTag, staticLibTag:
1938 // sdk lib names from dependencies are re-exported
1939 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1940 }
1941 case SdkLibraryDependency:
1942 switch tag {
1943 case libTag:
1944 // names of sdk libs that are directly depended are exported
1945 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1946 }
1947 }
1948 })
1949
1950 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001951 if Bool(j.properties.Installable) {
1952 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1953 ctx.ModuleName()+".jar", outputFile)
1954 }
Colin Cross2fe66872015-03-30 17:20:39 -07001955}
1956
Colin Cross74d73e22017-08-02 11:05:49 -07001957var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001958
Nan Zhanged19fc32017-10-19 13:06:22 -07001959func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001960 if j.combinedClasspathFile == nil {
1961 return nil
1962 }
Colin Cross37f6d792018-07-12 12:28:41 -07001963 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001964}
1965
1966func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001967 if j.combinedClasspathFile == nil {
1968 return nil
1969 }
Colin Cross37f6d792018-07-12 12:28:41 -07001970 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001971}
1972
Colin Cross331a1212018-08-15 20:40:52 -07001973func (j *Import) ResourceJars() android.Paths {
1974 return nil
1975}
1976
1977func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001978 if j.combinedClasspathFile == nil {
1979 return nil
1980 }
Colin Cross331a1212018-08-15 20:40:52 -07001981 return android.Paths{j.combinedClasspathFile}
1982}
1983
Colin Crossf24a22a2019-01-31 14:12:44 -08001984func (j *Import) DexJar() android.Path {
1985 return nil
1986}
1987
Colin Cross74d73e22017-08-02 11:05:49 -07001988func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001989 return nil
1990}
1991
Jiyong Park1be96912018-05-28 18:02:19 +09001992func (j *Import) ExportedSdkLibs() []string {
1993 return j.exportedSdkLibs
1994}
1995
Colin Cross0c4ce212019-05-03 15:28:19 -07001996func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1997 return nil, nil
1998}
1999
albaltai36ff7dc2018-12-25 14:35:23 +08002000// Add compile time check for interface implementation
2001var _ android.IDEInfo = (*Import)(nil)
2002var _ android.IDECustomizedModuleName = (*Import)(nil)
2003
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002004// Collect information for opening IDE project files in java/jdeps.go.
2005const (
2006 removedPrefix = "prebuilt_"
2007)
2008
2009func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2010 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2011}
2012
2013func (j *Import) IDECustomizedModuleName() string {
2014 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2015 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2016 // solution to get the Import name.
2017 name := j.Name()
2018 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002019 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002020 }
2021 return name
2022}
2023
Colin Cross74d73e22017-08-02 11:05:49 -07002024var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002025
Colin Cross1b16b0e2019-02-12 14:41:32 -08002026// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2027//
2028// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2029// compiled against an Android classpath.
2030//
2031// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2032// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002033func ImportFactory() android.Module {
2034 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002035
Colin Cross74d73e22017-08-02 11:05:49 -07002036 module.AddProperties(&module.properties)
2037
2038 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002039 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002040 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002041 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002042}
2043
Colin Cross1b16b0e2019-02-12 14:41:32 -08002044// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2045// module.
2046//
2047// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2048// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002049func ImportFactoryHost() android.Module {
2050 module := &Import{}
2051
2052 module.AddProperties(&module.properties)
2053
2054 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002055 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002056 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002057 return module
2058}
2059
Colin Cross42be7612019-02-21 18:12:14 -08002060// dex_import module
2061
2062type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002063 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002064}
2065
2066type DexImport struct {
2067 android.ModuleBase
2068 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002069 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002070 prebuilt android.Prebuilt
2071
2072 properties DexImportProperties
2073
2074 dexJarFile android.Path
2075 maybeStrippedDexJarFile android.Path
2076
2077 dexpreopter
2078}
2079
2080func (j *DexImport) Prebuilt() *android.Prebuilt {
2081 return &j.prebuilt
2082}
2083
2084func (j *DexImport) PrebuiltSrcs() []string {
2085 return j.properties.Jars
2086}
2087
2088func (j *DexImport) Name() string {
2089 return j.prebuilt.Name(j.ModuleBase.Name())
2090}
2091
Colin Cross42be7612019-02-21 18:12:14 -08002092func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2093 if len(j.properties.Jars) != 1 {
2094 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2095 }
2096
2097 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2098 j.dexpreopter.isInstallable = true
2099 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2100
2101 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2102 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2103
2104 if j.dexpreopter.uncompressedDex {
2105 rule := android.NewRuleBuilder()
2106
2107 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2108 rule.Temporary(temporary)
2109
2110 // use zip2zip to uncompress classes*.dex files
2111 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002112 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002113 FlagWithInput("-i ", inputJar).
2114 FlagWithOutput("-o ", temporary).
2115 FlagWithArg("-0 ", "'classes*.dex'")
2116
2117 // use zipalign to align uncompressed classes*.dex files
2118 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002119 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002120 Flag("-f").
2121 Text("4").
2122 Input(temporary).
2123 Output(dexOutputFile)
2124
2125 rule.DeleteTemporaryFiles()
2126
2127 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2128 } else {
2129 ctx.Build(pctx, android.BuildParams{
2130 Rule: android.Cp,
2131 Input: inputJar,
2132 Output: dexOutputFile,
2133 })
2134 }
2135
2136 j.dexJarFile = dexOutputFile
2137
2138 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2139
2140 j.maybeStrippedDexJarFile = dexOutputFile
2141
2142 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2143 ctx.ModuleName()+".jar", dexOutputFile)
2144}
2145
2146func (j *DexImport) DexJar() android.Path {
2147 return j.dexJarFile
2148}
2149
2150// dex_import imports a `.jar` file containing classes.dex files.
2151//
2152// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2153// to the device.
2154func DexImportFactory() android.Module {
2155 module := &DexImport{}
2156
2157 module.AddProperties(&module.properties)
2158
2159 android.InitPrebuiltModule(module, &module.properties.Jars)
2160 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002161 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002162 return module
2163}
2164
Colin Cross89536d42017-07-07 14:35:50 -07002165//
2166// Defaults
2167//
2168type Defaults struct {
2169 android.ModuleBase
2170 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002171 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002172}
2173
Colin Cross1b16b0e2019-02-12 14:41:32 -08002174// java_defaults provides a set of properties that can be inherited by other java or android modules.
2175//
2176// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2177// property in the defaults module that exists in the depending module will be prepended to the depending module's
2178// value for that property.
2179//
2180// Example:
2181//
2182// java_defaults {
2183// name: "example_defaults",
2184// srcs: ["common/**/*.java"],
2185// javacflags: ["-Xlint:all"],
2186// aaptflags: ["--auto-add-overlay"],
2187// }
2188//
2189// java_library {
2190// name: "example",
2191// defaults: ["example_defaults"],
2192// srcs: ["example/**/*.java"],
2193// }
2194//
2195// is functionally identical to:
2196//
2197// java_library {
2198// name: "example",
2199// srcs: [
2200// "common/**/*.java",
2201// "example/**/*.java",
2202// ],
2203// javacflags: ["-Xlint:all"],
2204// }
Colin Cross89536d42017-07-07 14:35:50 -07002205func defaultsFactory() android.Module {
2206 return DefaultsFactory()
2207}
2208
2209func DefaultsFactory(props ...interface{}) android.Module {
2210 module := &Defaults{}
2211
2212 module.AddProperties(props...)
2213 module.AddProperties(
2214 &CompilerProperties{},
2215 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002216 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002217 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002218 &aaptProperties{},
2219 &androidLibraryProperties{},
2220 &appProperties{},
2221 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002222 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002223 &ImportProperties{},
2224 &AARImportProperties{},
2225 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002226 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002227 )
2228
2229 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002230 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002231 return module
2232}
Nan Zhangea568a42017-11-08 21:20:04 -08002233
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002234func kytheExtractJavaFactory() android.Singleton {
2235 return &kytheExtractJavaSingleton{}
2236}
2237
2238type kytheExtractJavaSingleton struct {
2239}
2240
2241func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2242 var xrefTargets android.Paths
2243 ctx.VisitAllModules(func(module android.Module) {
2244 if javaModule, ok := module.(xref); ok {
2245 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2246 }
2247 })
2248 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2249 if len(xrefTargets) > 0 {
2250 ctx.Build(pctx, android.BuildParams{
2251 Rule: blueprint.Phony,
2252 Output: android.PathForPhony(ctx, "xref_java"),
2253 Inputs: xrefTargets,
2254 })
2255 }
2256}
2257
Nan Zhangea568a42017-11-08 21:20:04 -08002258var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002259var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002260var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002261var inList = android.InList