blob: c6f6a3455557776182a32a37a2af06de6ebf8780 [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
Colin Cross2fe66872015-03-30 17:20:39 -0700271
Colin Cross89536d42017-07-07 14:35:50 -0700272 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700273 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700274 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700275
Colin Cross331a1212018-08-15 20:40:52 -0700276 // jar file containing header classes including static library dependencies, suitable for
277 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700278 headerJarFile android.Path
279
Colin Cross331a1212018-08-15 20:40:52 -0700280 // jar file containing implementation classes including static library dependencies but no
281 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700282 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700283
Colin Cross331a1212018-08-15 20:40:52 -0700284 // jar file containing only resources including from static library dependencies
285 resourceJar android.Path
286
Colin Cross0c4ce212019-05-03 15:28:19 -0700287 // args and dependencies to package source files into a srcjar
288 srcJarArgs []string
289 srcJarDeps android.Paths
290
Colin Cross331a1212018-08-15 20:40:52 -0700291 // jar file containing implementation classes and resources including static library
292 // dependencies
293 implementationAndResourcesJar android.Path
294
295 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700296 dexJarFile android.Path
297
Colin Cross43f08db2018-11-12 10:13:39 -0800298 // output file that contains classes.dex if it should be in the output file
299 maybeStrippedDexJarFile android.Path
300
Colin Crosscb933592017-11-22 13:49:43 -0800301 // output file containing uninstrumented classes that will be instrumented by jacoco
302 jacocoReportClassesFile android.Path
303
Colin Cross66dbc0b2017-12-28 12:23:20 -0800304 // output file containing mapping of obfuscated names
305 proguardDictionary android.Path
306
Colin Cross331a1212018-08-15 20:40:52 -0700307 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700308 outputFile android.Path
309 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700310
Colin Cross635c3b02016-05-18 15:37:25 -0700311 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700312
Colin Cross635c3b02016-05-18 15:37:25 -0700313 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700314
Colin Cross2fe66872015-03-30 17:20:39 -0700315 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700316 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800317
318 // list of .java files and srcjars that was passed to javac
319 compiledJavaSrcs android.Paths
320 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800321
322 // list of extra progurad flag files
323 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900324
Colin Cross094054a2018-10-17 15:10:48 -0700325 // manifest file to use instead of properties.Manifest
326 overrideManifest android.OptionalPath
327
Jiyong Park1be96912018-05-28 18:02:19 +0900328 // list of SDK lib names that this java moudule is exporting
329 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700330
331 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
332 // filter out Exclude_srcs, will be used by android.IDEInfo struct
333 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800334
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800335 // expanded Jarjar_rules
336 expandJarjarRules android.Path
337
Vladimir Marko0975ee02019-04-02 10:29:55 +0100338 // list of additional targets for checkbuild
339 additionalCheckedModules android.Paths
340
Colin Cross988708c2019-05-06 14:04:11 -0700341 // Extra files generated by the module type to be added as java resources.
342 extraResources android.Paths
343
Colin Crossf24a22a2019-01-31 14:12:44 -0800344 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800345 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800346
347 // list of the xref extraction files
348 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700349}
350
Colin Cross41955e82019-05-29 14:40:35 -0700351func (j *Module) OutputFiles(tag string) (android.Paths, error) {
352 switch tag {
353 case "":
354 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700355 case ".jar":
356 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700357 default:
358 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
359 }
Colin Cross54250902017-12-05 09:28:08 -0800360}
361
Jiyong Park8fd61922018-11-08 02:50:25 +0900362func (j *Module) DexJarFile() android.Path {
363 return j.dexJarFile
364}
365
Colin Cross41955e82019-05-29 14:40:35 -0700366var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800367
Colin Crossf506d872017-07-19 15:53:04 -0700368type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700369 HeaderJars() android.Paths
370 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700371 ResourceJars() android.Paths
372 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800373 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700374 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900375 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700376 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700377}
378
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700380 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
381 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900382}
383
Nan Zhangb2b33de2018-02-23 11:18:47 -0800384type SrcDependency interface {
385 CompiledSrcs() android.Paths
386 CompiledSrcJars() android.Paths
387}
388
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800389type xref interface {
390 XrefJavaFiles() android.Paths
391}
392
Nan Zhangb2b33de2018-02-23 11:18:47 -0800393func (j *Module) CompiledSrcs() android.Paths {
394 return j.compiledJavaSrcs
395}
396
397func (j *Module) CompiledSrcJars() android.Paths {
398 return j.compiledSrcJars
399}
400
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800401func (j *Module) XrefJavaFiles() android.Paths {
402 return j.kytheFiles
403}
404
Nan Zhangb2b33de2018-02-23 11:18:47 -0800405var _ SrcDependency = (*Module)(nil)
406
Colin Cross89536d42017-07-07 14:35:50 -0700407func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
408 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
409 android.InitDefaultableModule(module)
410}
411
Colin Crossbe1da472017-07-07 15:59:46 -0700412type dependencyTag struct {
413 blueprint.BaseDependencyTag
414 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700415}
416
Colin Crossa4f08812018-10-02 22:03:40 -0700417type jniDependencyTag struct {
418 blueprint.BaseDependencyTag
419 target android.Target
420}
421
Colin Crossbe1da472017-07-07 15:59:46 -0700422var (
Colin Cross4b964c02018-10-15 16:18:06 -0700423 staticLibTag = dependencyTag{name: "staticlib"}
424 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800425 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700426 bootClasspathTag = dependencyTag{name: "bootclasspath"}
427 systemModulesTag = dependencyTag{name: "system modules"}
428 frameworkResTag = dependencyTag{name: "framework-res"}
429 frameworkApkTag = dependencyTag{name: "framework-apk"}
430 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800431 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700432 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
433 certificateTag = dependencyTag{name: "certificate"}
434 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700435 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700436)
Colin Cross2fe66872015-03-30 17:20:39 -0700437
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900438func defaultSdkVersion(ctx checkVendorModuleContext) string {
439 if ctx.SocSpecific() || ctx.DeviceSpecific() {
440 return "system_current"
441 }
442 return ""
443}
444
445type checkVendorModuleContext interface {
446 SocSpecific() bool
447 DeviceSpecific() bool
448}
449
Colin Crossfc3674a2017-09-18 17:41:52 -0700450type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700451 useModule, useFiles, useDefaultLibs, invalidVersion bool
452
Colin Cross86a60ae2018-05-29 14:44:55 -0700453 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700454 systemModules string
455
Colin Crossa97c5d32018-03-28 14:58:31 -0700456 frameworkResModule string
457
Colin Cross86a60ae2018-05-29 14:44:55 -0700458 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700459 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100460
461 noStandardLibs, noFrameworksLibs bool
462}
463
464func (s sdkDep) hasStandardLibs() bool {
465 return !s.noStandardLibs
466}
467
468func (s sdkDep) hasFrameworkLibs() bool {
469 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700470}
471
Colin Crossa4f08812018-10-02 22:03:40 -0700472type jniLib struct {
473 name string
474 path android.Path
475 target android.Target
476}
477
Colin Cross0ea8ba82019-06-06 14:33:29 -0700478func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800479 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
480}
481
Colin Cross0ea8ba82019-06-06 14:33:29 -0700482func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800483 return j.shouldInstrument(ctx) &&
484 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
485 ctx.Config().UnbundledBuild())
486}
487
Colin Cross83bb3162018-06-25 15:48:06 -0700488func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900489 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700490}
491
492func (j *Module) minSdkVersion() string {
493 if j.deviceProperties.Min_sdk_version != nil {
494 return *j.deviceProperties.Min_sdk_version
495 }
496 return j.sdkVersion()
497}
498
Dan Willemsen419290a2018-10-31 15:28:47 -0700499func (j *Module) targetSdkVersion() string {
500 if j.deviceProperties.Target_sdk_version != nil {
501 return *j.deviceProperties.Target_sdk_version
502 }
503 return j.sdkVersion()
504}
505
Colin Crossbe1da472017-07-07 15:59:46 -0700506func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700507 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100508 sdkDep := decodeSdkDep(ctx, sdkContext(j))
509 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700510 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700511 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100512 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100513 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700514 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700515 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700516 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100517 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700518 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700519 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700520 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
521 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800522 }
Colin Crossbe1da472017-07-07 15:59:46 -0700523 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700524 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100525 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100526 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100527 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700528 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700529 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100530 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700531 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800532 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800533 if ctx.ModuleName() == "android_stubs_current" ||
534 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700535 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700536 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800537 }
Colin Cross2fe66872015-03-30 17:20:39 -0700538 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700539
Colin Cross42d48b72018-08-29 14:10:52 -0700540 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
541 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700542
Colin Crossbe9cdb82019-01-21 21:37:16 -0800543 ctx.AddFarVariationDependencies([]blueprint.Variation{
544 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
545 }, pluginTag, j.properties.Plugins...)
546
Colin Crossfe17f6f2019-03-28 19:30:56 -0700547 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700548 if j.hasSrcExt(".proto") {
549 protoDeps(ctx, &j.protoProperties)
550 }
Colin Cross93e85952017-08-15 13:34:18 -0700551
552 if j.hasSrcExt(".kt") {
553 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
554 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700555 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
556 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800557 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800558 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
559 }
Colin Cross93e85952017-08-15 13:34:18 -0700560 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800561
562 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700563 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800564 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700565}
566
567func hasSrcExt(srcs []string, ext string) bool {
568 for _, src := range srcs {
569 if filepath.Ext(src) == ext {
570 return true
571 }
572 }
573
574 return false
575}
576
Nan Zhang61eaedb2017-11-02 13:28:15 -0700577func shardPaths(paths android.Paths, shardSize int) []android.Paths {
578 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
579 for len(paths) > shardSize {
580 ret = append(ret, paths[0:shardSize])
581 paths = paths[shardSize:]
582 }
583 if len(paths) > 0 {
584 ret = append(ret, paths)
585 }
586 return ret
587}
588
Colin Cross6af17aa2017-09-20 12:59:05 -0700589func (j *Module) hasSrcExt(ext string) bool {
590 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700591}
592
Colin Cross46c9b8b2017-06-22 16:51:17 -0700593func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700594 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700595
Colin Crossebe1a512017-11-14 13:12:14 -0800596 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
597 aidlIncludes = append(aidlIncludes,
598 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
599 aidlIncludes = append(aidlIncludes,
600 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700601
Colin Cross3047fa22019-04-18 10:56:44 -0700602 var flags []string
603 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700604
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700605 if aidlPreprocess.Valid() {
606 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700607 deps = append(deps, aidlPreprocess.Path())
608 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700609 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700610 }
611
Colin Cross3047fa22019-04-18 10:56:44 -0700612 if len(j.exportAidlIncludeDirs) > 0 {
613 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
614 }
615
616 if len(aidlIncludes) > 0 {
617 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
618 }
619
Colin Cross635c3b02016-05-18 15:37:25 -0700620 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800621 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700622 flags = append(flags, "-I"+src.String())
623 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700624
Martijn Coeneneab15642018-03-09 09:29:59 +0100625 if Bool(j.deviceProperties.Aidl.Generate_traces) {
626 flags = append(flags, "-t")
627 }
628
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100629 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
630 flags = append(flags, "--transaction_names")
631 }
632
Colin Cross3047fa22019-04-18 10:56:44 -0700633 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700634}
635
Colin Cross32f676a2017-09-06 13:41:06 -0700636type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800637 classpath classpath
638 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700639 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800640 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700641 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700642 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700643 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700644 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800645 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700646 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700647 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700648 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700649 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800650 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800651
652 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700653}
Colin Cross2fe66872015-03-30 17:20:39 -0700654
Colin Cross54250902017-12-05 09:28:08 -0800655func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
656 for _, f := range dep.Srcs() {
657 if f.Ext() != ".jar" {
658 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
659 ctx.OtherModuleName(dep.(blueprint.Module)))
660 }
661 }
662}
663
Jiyong Park2d492942018-03-05 17:44:10 +0900664type linkType int
665
666const (
667 javaCore linkType = iota
668 javaSdk
669 javaSystem
670 javaPlatform
671)
672
Jiyong Park46f78fb2018-10-20 16:33:17 +0900673func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700674 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700675 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900676 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
677 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100678 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900679 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100680 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900681 return javaCore, false
682 case name == "android_system_stubs_current":
683 return javaSystem, true
684 case strings.HasPrefix(ver, "system_"):
685 return javaSystem, false
686 case name == "android_test_stubs_current":
687 return javaSystem, true
688 case strings.HasPrefix(ver, "test_"):
689 return javaPlatform, false
690 case name == "android_stubs_current":
691 return javaSdk, true
692 case ver == "current":
693 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100694 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900695 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700696 default:
697 if _, err := strconv.Atoi(ver); err != nil {
698 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
699 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900700 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900701 }
702}
703
Jiyong Park750e5572018-01-31 00:20:13 +0900704func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700705 if ctx.Host() {
706 return
707 }
708
Jiyong Park46f78fb2018-10-20 16:33:17 +0900709 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
710 if stubs {
711 return
712 }
713 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900714 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."
715
716 switch myLinkType {
717 case javaCore:
718 if otherLinkType != javaCore {
719 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 +0900720 ctx.OtherModuleName(to))
721 }
Jiyong Park2d492942018-03-05 17:44:10 +0900722 break
723 case javaSdk:
724 if otherLinkType != javaCore && otherLinkType != javaSdk {
725 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
726 ctx.OtherModuleName(to))
727 }
728 break
729 case javaSystem:
730 if otherLinkType == javaPlatform {
731 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
732 ctx.OtherModuleName(to))
733 }
734 break
735 case javaPlatform:
736 // no restriction on link-type
737 break
Jiyong Park750e5572018-01-31 00:20:13 +0900738 }
739}
740
Colin Cross32f676a2017-09-06 13:41:06 -0700741func (j *Module) collectDeps(ctx android.ModuleContext) deps {
742 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700743
Colin Cross300f0382018-03-06 13:11:51 -0800744 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700745 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800746 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700747 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800748 } else if sdkDep.useFiles {
749 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700750 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700751 deps.aidlPreprocess = sdkDep.aidl
752 } else {
753 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800754 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700755 }
756
Colin Crossd11fcda2017-10-23 17:59:01 -0700757 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700758 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700759 tag := ctx.OtherModuleDependencyTag(module)
760
Colin Crossa4f08812018-10-02 22:03:40 -0700761 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700762 // Handled by AndroidApp.collectAppDeps
763 return
764 }
765 if tag == certificateTag {
766 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700767 return
768 }
769
Jiyong Park750e5572018-01-31 00:20:13 +0900770 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700771 switch tag {
772 case bootClasspathTag, libTag, staticLibTag:
773 checkLinkType(ctx, j, to, tag.(dependencyTag))
774 }
Jiyong Park750e5572018-01-31 00:20:13 +0900775 }
Colin Cross54250902017-12-05 09:28:08 -0800776 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800777 case SdkLibraryDependency:
778 switch tag {
779 case libTag:
780 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
781 // names of sdk libs that are directly depended are exported
782 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700783 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800784 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
785 }
Colin Cross54250902017-12-05 09:28:08 -0800786 case Dependency:
787 switch tag {
788 case bootClasspathTag:
789 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700790 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800791 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900792 // sdk lib names from dependencies are re-exported
793 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700794 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800795 case staticLibTag:
796 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
797 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
798 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700799 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900800 // sdk lib names from dependencies are re-exported
801 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700802 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800803 case pluginTag:
804 if plugin, ok := dep.(*Plugin); ok {
805 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
806 if plugin.pluginProperties.Processor_class != nil {
807 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
808 }
809 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
810 } else {
811 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
812 }
Colin Cross54250902017-12-05 09:28:08 -0800813 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100814 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800815 // framework.jar has a one-off dependency on the R.java and Manifest.java files
816 // generated by framework-res.apk
817 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
818 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800819 case frameworkApkTag:
820 if ctx.ModuleName() == "android_stubs_current" ||
821 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700822 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800823 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
824 // resource files out of there for aapt.
825 //
826 // Normally the package rule runs aapt, which includes the resource,
827 // but we're not running that in our package rule so just copy in the
828 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700829 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800830 }
Colin Cross54250902017-12-05 09:28:08 -0800831 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700832 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800833 case kotlinAnnotationsTag:
834 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800835 }
836
Colin Cross54250902017-12-05 09:28:08 -0800837 case android.SourceFileProducer:
838 switch tag {
839 case libTag:
840 checkProducesJars(ctx, dep)
841 deps.classpath = append(deps.classpath, dep.Srcs()...)
842 case staticLibTag:
843 checkProducesJars(ctx, dep)
844 deps.classpath = append(deps.classpath, dep.Srcs()...)
845 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
846 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800847 }
848 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700849 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700850 case systemModulesTag:
851 if deps.systemModules != nil {
852 panic("Found two system module dependencies")
853 }
854 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000855 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700856 panic("Missing directory for system module dependency")
857 }
Colin Crossb77043e2019-07-16 13:57:13 -0700858 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700859 }
Colin Crossec7a0422017-07-07 14:47:12 -0700860 }
Colin Cross2fe66872015-03-30 17:20:39 -0700861 })
862
Jiyong Park1be96912018-05-28 18:02:19 +0900863 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
864
Colin Cross32f676a2017-09-06 13:41:06 -0700865 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700866}
867
Colin Cross83bb3162018-06-25 15:48:06 -0700868func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700869 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800870 v := sdkContext.sdkVersion()
871 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100872 if ctx.Config().IsPdkBuild() &&
873 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700874 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800875 latestSdkVersion := 0
876 if len(sdkVersions) > 0 {
877 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
878 }
879 v = strconv.Itoa(latestSdkVersion)
880 }
881
882 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700883 if err != nil {
884 ctx.PropertyErrorf("sdk_version", "%s", err)
885 }
Nan Zhang357466b2018-04-17 17:38:36 -0700886 if javaVersion != "" {
887 ret = javaVersion
888 } else if ctx.Device() && sdk <= 23 {
889 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100890 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700891 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100892 } else if ctx.Device() &&
893 sdkContext.sdkVersion() != "" &&
894 sdkContext.sdkVersion() != "none" &&
895 sdkContext.sdkVersion() != "core_platform" &&
896 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700897 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
898 ret = "1.8"
899 } else {
900 ret = "1.9"
901 }
902
903 return ret
904}
905
Nan Zhanged19fc32017-10-19 13:06:22 -0700906func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700907
Colin Crossf03c82b2015-04-13 13:53:40 -0700908 var flags javaBuilderFlags
909
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100910 // javaVersion flag.
911 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
912
Nan Zhanged19fc32017-10-19 13:06:22 -0700913 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700914 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100915 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700916 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700917 }
Colin Cross6510f912017-11-29 00:27:14 -0800918 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700919 // Override the -g flag passed globally to remove local variable debug info to reduce
920 // disk and memory usage.
921 javacFlags = append(javacFlags, "-g:source,lines")
922 }
Colin Cross64162712017-08-08 13:17:59 -0700923
Colin Cross66548102018-06-19 22:47:35 -0700924 if ctx.Config().RunErrorProne() {
925 if config.ErrorProneClasspath == nil {
926 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
927 }
928
929 errorProneFlags := []string{
930 "-Xplugin:ErrorProne",
931 "${config.ErrorProneChecks}",
932 }
933 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
934
935 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
936 "'" + strings.Join(errorProneFlags, " ") + "'"
937 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800938 }
939
Nan Zhanged19fc32017-10-19 13:06:22 -0700940 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800941 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
942 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700943 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800944
Colin Crossbe9cdb82019-01-21 21:37:16 -0800945 flags.processor = strings.Join(deps.processorClasses, ",")
946
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100947 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100948 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800949 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
950 // Give host-side tools a version of OpenJDK's standard libraries
951 // close to what they're targeting. As of Dec 2017, AOSP is only
952 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
953 //
954 // When building with OpenJDK 8, the following should have no
955 // effect since those jars would be available by default.
956 //
957 // When building with OpenJDK 9 but targeting a version < 1.8,
958 // putting them on the bootclasspath means that:
959 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
960 // b) references to existing APIs are not reinterpreted in an
961 // OpenJDK 9-specific way, eg. calls to subclasses of
962 // java.nio.Buffer as in http://b/70862583
963 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
964 flags.bootClasspath = append(flags.bootClasspath,
965 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
966 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800967 if Bool(j.properties.Use_tools_jar) {
968 flags.bootClasspath = append(flags.bootClasspath,
969 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
970 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800971 }
972
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100973 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800974 // Manually specify build directory in case it is not under the repo root.
975 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
976 // just adding a symlink under the root doesn't help.)
977 patchPaths := ".:" + ctx.Config().BuildDir()
978 classPath := flags.classpath.FormJavaClassPath("")
979 if classPath != "" {
980 patchPaths += ":" + classPath
981 }
982 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700983 }
984
Nan Zhanged19fc32017-10-19 13:06:22 -0700985 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -0700986 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700987
Nan Zhanged19fc32017-10-19 13:06:22 -0700988 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700989 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700990
Colin Cross81440082018-08-15 20:21:55 -0700991 if len(javacFlags) > 0 {
992 // optimization.
993 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
994 flags.javacFlags = "$javacFlags"
995 }
996
Nan Zhanged19fc32017-10-19 13:06:22 -0700997 return flags
998}
Colin Crossc0b06f12015-04-08 13:03:43 -0700999
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001000func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
1001
Colin Crossebe1a512017-11-14 13:12:14 -08001002 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001003
1004 deps := j.collectDeps(ctx)
1005 flags := j.collectBuilderFlags(ctx, deps)
1006
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001007 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001008 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1009 }
Colin Cross8a497952019-03-05 22:25:09 -08001010 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001011 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001012 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001013 }
1014
Colin Crossaf050172017-11-15 23:01:59 -08001015 srcFiles = j.genSources(ctx, srcFiles, flags)
1016
1017 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001018 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001019 if aaptSrcJar != nil {
1020 srcJars = append(srcJars, aaptSrcJar)
1021 }
Colin Crossb7a63242015-04-16 14:09:14 -07001022
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001023 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1024 // that IDEInfo struct will use
1025 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1026
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001027 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001028 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001029 }
1030
Colin Cross1ee23172017-10-18 14:44:18 -07001031 jarName := ctx.ModuleName() + ".jar"
1032
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001033 javaSrcFiles := srcFiles.FilterByExt(".java")
1034 var uniqueSrcFiles android.Paths
1035 set := make(map[string]bool)
1036 for _, v := range javaSrcFiles {
1037 if _, found := set[v.String()]; !found {
1038 set[v.String()] = true
1039 uniqueSrcFiles = append(uniqueSrcFiles, v)
1040 }
1041 }
1042
Colin Cross55f63ea2018-08-27 12:37:09 -07001043 var kotlinJars android.Paths
1044
Colin Cross93e85952017-08-15 13:34:18 -07001045 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001046 // user defined kotlin flags.
1047 kotlincFlags := j.properties.Kotlincflags
1048 CheckKotlincFlags(ctx, kotlincFlags)
1049
Colin Cross93e85952017-08-15 13:34:18 -07001050 // If there are kotlin files, compile them first but pass all the kotlin and java files
1051 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1052 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001053 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001054 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001055 kotlincFlags = append(kotlincFlags, "-no-jdk")
1056 }
1057 if len(kotlincFlags) > 0 {
1058 // optimization.
1059 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1060 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001061 }
1062
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001063 var kotlinSrcFiles android.Paths
1064 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1065 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1066
Colin Crossafbb1732019-01-17 15:42:52 -08001067 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1068 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1069
1070 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1071 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1072
1073 if len(flags.processorPath) > 0 {
1074 // Use kapt for annotation processing
1075 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1076 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1077 srcJars = append(srcJars, kaptSrcJar)
1078 // Disable annotation processing in javac, it's already been handled by kapt
1079 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001080 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001081 }
Colin Cross93e85952017-08-15 13:34:18 -07001082
Colin Cross1ee23172017-10-18 14:44:18 -07001083 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001084 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001085 if ctx.Failed() {
1086 return
1087 }
1088
1089 // Make javac rule depend on the kotlinc rule
1090 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001091
Colin Cross93e85952017-08-15 13:34:18 -07001092 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001093 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001094 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001095 }
1096
Colin Cross55f63ea2018-08-27 12:37:09 -07001097 jars := append(android.Paths(nil), kotlinJars...)
1098
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001099 // Store the list of .java files that was passed to javac
1100 j.compiledJavaSrcs = uniqueSrcFiles
1101 j.compiledSrcJars = srcJars
1102
Nan Zhang61eaedb2017-11-02 13:28:15 -07001103 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001104 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001105 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1106 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001107 // Formerly, there was a check here that prevented annotation processors
1108 // from being used when sharding was enabled, as some annotation processors
1109 // do not function correctly in sharded environments. It was removed to
1110 // allow for the use of annotation processors that do function correctly
1111 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001112 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001113 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001114 if ctx.Failed() {
1115 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001116 }
1117 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001118 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001119 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001120 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001121 // If error-prone is enabled, add an additional rule to compile the java files into
1122 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001123 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001124 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1125 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001126 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001127 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001128 extraJarDeps = append(extraJarDeps, errorprone)
1129 }
1130
Nan Zhang61eaedb2017-11-02 13:28:15 -07001131 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001132 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001133 shardSize := int(*(j.properties.Javac_shard_size))
1134 var shardSrcs []android.Paths
1135 if len(uniqueSrcFiles) > 0 {
1136 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1137 for idx, shardSrc := range shardSrcs {
1138 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1139 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1140 jars = append(jars, classes)
1141 }
1142 }
1143 if len(srcJars) > 0 {
1144 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1145 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1146 jars = append(jars, classes)
1147 }
1148 } else {
1149 classes := android.PathForModuleOut(ctx, "javac", jarName)
1150 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1151 jars = append(jars, classes)
1152 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001153 if ctx.Config().EmitXrefRules() {
1154 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1155 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1156 j.kytheFiles = append(j.kytheFiles, extractionFile)
1157
1158 }
Colin Crossd6891432017-09-27 17:39:56 -07001159 if ctx.Failed() {
1160 return
1161 }
Colin Cross2fe66872015-03-30 17:20:39 -07001162 }
1163
Colin Cross0c4ce212019-05-03 15:28:19 -07001164 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1165
1166 var includeSrcJar android.WritablePath
1167 if Bool(j.properties.Include_srcs) {
1168 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1169 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1170 }
1171
Colin Crosscedd4762018-09-13 11:26:19 -07001172 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1173 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001174 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001175 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001176
1177 var resArgs []string
1178 var resDeps android.Paths
1179
1180 resArgs = append(resArgs, dirArgs...)
1181 resDeps = append(resDeps, dirDeps...)
1182
1183 resArgs = append(resArgs, fileArgs...)
1184 resDeps = append(resDeps, fileDeps...)
1185
Colin Cross988708c2019-05-06 14:04:11 -07001186 resArgs = append(resArgs, extraArgs...)
1187 resDeps = append(resDeps, extraDeps...)
1188
Colin Cross40a36712017-09-27 17:41:35 -07001189 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001190 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001191 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001192 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001193 if ctx.Failed() {
1194 return
1195 }
1196 }
1197
Colin Cross0c4ce212019-05-03 15:28:19 -07001198 var resourceJars android.Paths
1199 if j.resourceJar != nil {
1200 resourceJars = append(resourceJars, j.resourceJar)
1201 }
1202 if Bool(j.properties.Include_srcs) {
1203 resourceJars = append(resourceJars, includeSrcJar)
1204 }
1205 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001206
Colin Cross0c4ce212019-05-03 15:28:19 -07001207 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001208 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001209 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001210 false, nil, nil)
1211 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001212 } else if len(resourceJars) == 1 {
1213 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001214 }
1215
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001216 if len(deps.staticJars) > 0 {
1217 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001218 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001219
Colin Cross094054a2018-10-17 15:10:48 -07001220 manifest := j.overrideManifest
1221 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001222 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001223 }
Colin Cross635acc92017-09-12 22:50:46 -07001224
Colin Cross8a497952019-03-05 22:25:09 -08001225 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001226 if len(services) > 0 {
1227 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1228 var zipargs []string
1229 for _, file := range services {
1230 serviceFile := file.String()
1231 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1232 }
1233 ctx.Build(pctx, android.BuildParams{
1234 Rule: zip,
1235 Output: servicesJar,
1236 Implicits: services,
1237 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001238 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001239 },
1240 })
1241 jars = append(jars, servicesJar)
1242 }
1243
Colin Cross0a6e0072017-08-30 14:24:55 -07001244 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001245 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001246 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001247
1248 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001249 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1250 // Optimization: skip the combine step if there is nothing to do
1251 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1252 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1253 // any if len(jars) == 1.
1254 outputFile = moduleOutPath
1255 } else {
1256 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1257 ctx.Build(pctx, android.BuildParams{
1258 Rule: android.Cp,
1259 Input: jars[0],
1260 Output: combinedJar,
1261 })
1262 outputFile = combinedJar
1263 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001264 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001265 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001266 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001267 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001268 outputFile = combinedJar
1269 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001270
Colin Cross331a1212018-08-15 20:40:52 -07001271 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001272 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001273 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001274 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001275 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001276 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001277
1278 // jarjar resource jar if necessary
1279 if j.resourceJar != nil {
1280 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001281 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001282 j.resourceJar = resourceJarJarFile
1283 }
1284
Colin Cross0a6e0072017-08-30 14:24:55 -07001285 if ctx.Failed() {
1286 return
1287 }
1288 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001289
1290 // Check package restrictions if necessary.
1291 if len(j.properties.Permitted_packages) > 0 {
1292 // Check packages and copy to package-checked file.
1293 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1294 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1295 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1296
1297 if ctx.Failed() {
1298 return
1299 }
1300 }
1301
Nan Zhanged19fc32017-10-19 13:06:22 -07001302 j.implementationJarFile = outputFile
1303 if j.headerJarFile == nil {
1304 j.headerJarFile = j.implementationJarFile
1305 }
Colin Cross2fe66872015-03-30 17:20:39 -07001306
Colin Cross6510f912017-11-29 00:27:14 -08001307 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001308 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1309 j.properties.Instrument = true
1310 }
1311 }
1312
Colin Cross3144dfc2018-01-03 15:06:47 -08001313 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001314 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1315 }
1316
Colin Cross331a1212018-08-15 20:40:52 -07001317 // merge implementation jar with resources if necessary
1318 implementationAndResourcesJar := outputFile
1319 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001320 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001321 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001322 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001323 false, nil, nil)
1324 implementationAndResourcesJar = combinedJar
1325 }
1326
1327 j.implementationAndResourcesJar = implementationAndResourcesJar
1328
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001329 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001330 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001331 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001332 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001333 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001334 if ctx.Failed() {
1335 return
1336 }
Colin Cross331a1212018-08-15 20:40:52 -07001337
Jiyong Park09cb6292019-07-15 15:29:23 +09001338 // Hidden API CSV generation and dex encoding
1339 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1340 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001341
Colin Cross331a1212018-08-15 20:40:52 -07001342 // merge dex jar with resources if necessary
1343 if j.resourceJar != nil {
1344 jars := android.Paths{dexOutputFile, j.resourceJar}
1345 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1346 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1347 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001348 if j.deviceProperties.UncompressDex {
1349 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1350 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1351 dexOutputFile = combinedAlignedJar
1352 } else {
1353 dexOutputFile = combinedJar
1354 }
Colin Cross331a1212018-08-15 20:40:52 -07001355 }
1356
1357 j.dexJarFile = dexOutputFile
1358
Colin Cross8faf8fc2019-01-16 15:15:52 -08001359 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001360 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1361
1362 j.maybeStrippedDexJarFile = dexOutputFile
1363
Colin Cross3063b782018-08-15 11:19:12 -07001364 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001365
1366 if ctx.Failed() {
1367 return
1368 }
Colin Cross331a1212018-08-15 20:40:52 -07001369 } else {
1370 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001371 }
Colin Cross331a1212018-08-15 20:40:52 -07001372
Colin Crossb7a63242015-04-16 14:09:14 -07001373 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001374
1375 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1376 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001377}
1378
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001379// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1380// since some of these flags may be used internally.
1381func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1382 for _, flag := range flags {
1383 flag = strings.TrimSpace(flag)
1384
1385 if !strings.HasPrefix(flag, "-") {
1386 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1387 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1388 ctx.PropertyErrorf("kotlincflags",
1389 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1390 } else if inList(flag, config.KotlincIllegalFlags) {
1391 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1392 } else if flag == "-include-runtime" {
1393 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1394 } else {
1395 args := strings.Split(flag, " ")
1396 if args[0] == "-kotlin-home" {
1397 ctx.PropertyErrorf("kotlincflags",
1398 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1399 }
1400 }
1401 }
1402}
1403
Colin Cross8eadbf02017-10-24 17:46:00 -07001404func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001405 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001406
1407 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001408 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001409 // Compile java sources into turbine.jar.
1410 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1411 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1412 if ctx.Failed() {
1413 return nil
1414 }
1415 jars = append(jars, turbineJar)
1416 }
1417
Colin Cross55f63ea2018-08-27 12:37:09 -07001418 jars = append(jars, extraJars...)
1419
Nan Zhanged19fc32017-10-19 13:06:22 -07001420 // Combine any static header libraries into classes-header.jar. If there is only
1421 // one input jar this step will be skipped.
1422 var headerJar android.Path
1423 jars = append(jars, deps.staticHeaderJars...)
1424
Colin Cross5c6ecc12017-10-23 18:12:27 -07001425 // we cannot skip the combine step for now if there is only one jar
1426 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1427 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001428 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001429 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001430 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001431
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001432 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001433 // Transform classes.jar into classes-jarjar.jar
1434 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001435 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001436 headerJar = jarjarFile
1437 if ctx.Failed() {
1438 return nil
1439 }
1440 }
1441
1442 return headerJar
1443}
1444
Colin Crosscb933592017-11-22 13:49:43 -08001445func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001446 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001447
Colin Cross7a3139e2017-12-19 13:57:50 -08001448 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001449
Colin Cross84c38822018-01-03 15:59:46 -08001450 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001451 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1452
1453 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1454
1455 j.jacocoReportClassesFile = jacocoReportClassesFile
1456
1457 return instrumentedJar
1458}
1459
albaltai36ff7dc2018-12-25 14:35:23 +08001460var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001461
Nan Zhanged19fc32017-10-19 13:06:22 -07001462func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001463 if j.headerJarFile == nil {
1464 return nil
1465 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001466 return android.Paths{j.headerJarFile}
1467}
1468
1469func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001470 if j.implementationJarFile == nil {
1471 return nil
1472 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001473 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001474}
1475
Colin Crossf24a22a2019-01-31 14:12:44 -08001476func (j *Module) DexJar() android.Path {
1477 return j.dexJarFile
1478}
1479
Colin Cross331a1212018-08-15 20:40:52 -07001480func (j *Module) ResourceJars() android.Paths {
1481 if j.resourceJar == nil {
1482 return nil
1483 }
1484 return android.Paths{j.resourceJar}
1485}
1486
1487func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001488 if j.implementationAndResourcesJar == nil {
1489 return nil
1490 }
Colin Cross331a1212018-08-15 20:40:52 -07001491 return android.Paths{j.implementationAndResourcesJar}
1492}
1493
Colin Cross46c9b8b2017-06-22 16:51:17 -07001494func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001495 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001496 return j.exportAidlIncludeDirs
1497}
1498
Jiyong Park1be96912018-05-28 18:02:19 +09001499func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001500 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001501 return j.exportedSdkLibs
1502}
1503
Colin Cross0c4ce212019-05-03 15:28:19 -07001504func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1505 return j.srcJarArgs, j.srcJarDeps
1506}
1507
Colin Cross46c9b8b2017-06-22 16:51:17 -07001508var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001509
Colin Cross46c9b8b2017-06-22 16:51:17 -07001510func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001511 return j.logtagsSrcs
1512}
1513
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001514// Collect information for opening IDE project files in java/jdeps.go.
1515func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1516 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1517 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001518 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001519 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001520 if j.expandJarjarRules != nil {
1521 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001522 }
1523}
1524
1525func (j *Module) CompilerDeps() []string {
1526 jdeps := []string{}
1527 jdeps = append(jdeps, j.properties.Libs...)
1528 jdeps = append(jdeps, j.properties.Static_libs...)
1529 return jdeps
1530}
1531
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001532func (j *Module) hasCode(ctx android.ModuleContext) bool {
1533 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1534 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1535}
1536
Colin Cross2fe66872015-03-30 17:20:39 -07001537//
1538// Java libraries (.jar file)
1539//
1540
Colin Crossf506d872017-07-19 15:53:04 -07001541type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001542 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001543}
1544
Colin Cross42be7612019-02-21 18:12:14 -08001545func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001546 // Store uncompressed (and do not strip) dex files from boot class path jars.
1547 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1548 return true
1549 }
1550
1551 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001552 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001553 return true
1554 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001555 if ctx.Config().UncompressPrivAppDex() &&
1556 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1557 return true
1558 }
1559
Colin Cross2fc72f62018-12-21 12:59:54 -08001560 return false
1561}
1562
Colin Crossf506d872017-07-19 15:53:04 -07001563func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001564 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1565 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001566 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001567 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001568 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001569 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001570
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001571 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001572 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1573 ctx.ModuleName()+".jar", j.outputFile)
1574 }
Colin Crossb7a63242015-04-16 14:09:14 -07001575}
1576
Colin Crossf506d872017-07-19 15:53:04 -07001577func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001578 j.deps(ctx)
1579}
1580
Colin Cross1b16b0e2019-02-12 14:41:32 -08001581// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1582//
1583// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1584// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1585// as a `static_libs` dependency of another module.
1586//
1587// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1588// a device.
1589//
1590// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1591// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001592func LibraryFactory() android.Module {
1593 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001594
Colin Cross9ae1b922018-06-26 17:59:05 -07001595 module.AddProperties(
1596 &module.Module.properties,
1597 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001598 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001599 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001600
Colin Cross9ae1b922018-06-26 17:59:05 -07001601 InitJavaModule(module, android.HostAndDeviceSupported)
1602 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001603}
1604
Colin Cross1b16b0e2019-02-12 14:41:32 -08001605// java_library_static is an obsolete alias for java_library.
1606func LibraryStaticFactory() android.Module {
1607 return LibraryFactory()
1608}
1609
1610// java_library_host builds and links sources into a `.jar` file for the host.
1611//
1612// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1613// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001614func LibraryHostFactory() android.Module {
1615 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001616
Colin Cross6af17aa2017-09-20 12:59:05 -07001617 module.AddProperties(
1618 &module.Module.properties,
1619 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001620
Colin Cross9ae1b922018-06-26 17:59:05 -07001621 module.Module.properties.Installable = proptools.BoolPtr(true)
1622
Colin Cross89536d42017-07-07 14:35:50 -07001623 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001624 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001625}
1626
1627//
Colin Crossb628ea52018-08-14 16:42:33 -07001628// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001629//
1630
1631type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001632 // list of compatibility suites (for example "cts", "vts") that the module should be
1633 // installed into.
1634 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001635
1636 // the name of the test configuration (for example "AndroidTest.xml") that should be
1637 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001638 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001639
Jack He33338892018-09-19 02:21:28 -07001640 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1641 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001642 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001643
Colin Crossd96ca352018-08-10 16:06:24 -07001644 // list of files or filegroup modules that provide data that should be installed alongside
1645 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001646 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001647}
1648
Paul Duffin42df1442019-03-20 12:45:53 +00001649type testHelperLibraryProperties struct {
1650 // list of compatibility suites (for example "cts", "vts") that the module should be
1651 // installed into.
1652 Test_suites []string `android:"arch_variant"`
1653}
1654
Colin Cross05638fc2018-04-09 18:40:24 -07001655type Test struct {
1656 Library
1657
1658 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001659
1660 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001661 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001662}
1663
Paul Duffin42df1442019-03-20 12:45:53 +00001664type TestHelperLibrary struct {
1665 Library
1666
1667 testHelperLibraryProperties testHelperLibraryProperties
1668}
1669
Colin Cross303e21f2018-08-07 16:49:25 -07001670func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001671 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 -08001672 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001673
1674 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001675}
1676
Paul Duffin42df1442019-03-20 12:45:53 +00001677func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1678 j.Library.GenerateAndroidBuildActions(ctx)
1679}
1680
Colin Cross1b16b0e2019-02-12 14:41:32 -08001681// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1682// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1683//
1684// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1685// compiled against the device bootclasspath.
1686//
1687// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1688// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001689func TestFactory() android.Module {
1690 module := &Test{}
1691
1692 module.AddProperties(
1693 &module.Module.properties,
1694 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001695 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001696 &module.Module.protoProperties,
1697 &module.testProperties)
1698
Colin Cross9ae1b922018-06-26 17:59:05 -07001699 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001700 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001701
Colin Cross05638fc2018-04-09 18:40:24 -07001702 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001703 return module
1704}
1705
Paul Duffin42df1442019-03-20 12:45:53 +00001706// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1707func TestHelperLibraryFactory() android.Module {
1708 module := &TestHelperLibrary{}
1709
1710 module.AddProperties(
1711 &module.Module.properties,
1712 &module.Module.deviceProperties,
1713 &module.Module.dexpreoptProperties,
1714 &module.Module.protoProperties,
1715 &module.testHelperLibraryProperties)
1716
Colin Cross9a4abed2019-04-24 13:19:28 -07001717 module.Module.properties.Installable = proptools.BoolPtr(true)
1718 module.Module.dexpreopter.isTest = true
1719
Paul Duffin42df1442019-03-20 12:45:53 +00001720 InitJavaModule(module, android.HostAndDeviceSupported)
1721 return module
1722}
1723
Colin Cross1b16b0e2019-02-12 14:41:32 -08001724// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1725// allow running the test with `atest` or a `TEST_MAPPING` file.
1726//
1727// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1728// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001729func TestHostFactory() android.Module {
1730 module := &Test{}
1731
1732 module.AddProperties(
1733 &module.Module.properties,
1734 &module.Module.protoProperties,
1735 &module.testProperties)
1736
Colin Cross9ae1b922018-06-26 17:59:05 -07001737 module.Module.properties.Installable = proptools.BoolPtr(true)
1738
Colin Cross05638fc2018-04-09 18:40:24 -07001739 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001740 return module
1741}
1742
1743//
Colin Cross2fe66872015-03-30 17:20:39 -07001744// Java Binaries (.jar file plus wrapper script)
1745//
1746
Colin Crossf506d872017-07-19 15:53:04 -07001747type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001748 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001749 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001750
1751 // Name of the class containing main to be inserted into the manifest as Main-Class.
1752 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001753}
1754
Colin Crossf506d872017-07-19 15:53:04 -07001755type Binary struct {
1756 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001757
Colin Crossf506d872017-07-19 15:53:04 -07001758 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001759
Colin Cross6b4a32d2017-12-05 13:42:45 -08001760 isWrapperVariant bool
1761
Colin Crossc3315992017-12-08 19:12:36 -08001762 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001763 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001764}
1765
Alex Light24237172017-10-26 09:46:21 -07001766func (j *Binary) HostToolPath() android.OptionalPath {
1767 return android.OptionalPathForPath(j.binaryFile)
1768}
1769
Colin Crossf506d872017-07-19 15:53:04 -07001770func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001771 if ctx.Arch().ArchType == android.Common {
1772 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001773 if j.binaryProperties.Main_class != nil {
1774 if j.properties.Manifest != nil {
1775 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1776 }
1777 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1778 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1779 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1780 }
1781
Colin Cross6b4a32d2017-12-05 13:42:45 -08001782 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001783 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001784 // Handle the binary wrapper
1785 j.isWrapperVariant = true
1786
Colin Cross366938f2017-12-11 16:29:02 -08001787 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001788 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001789 } else {
1790 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1791 }
1792
1793 // Depend on the installed jar so that the wrapper doesn't get executed by
1794 // another build rule before the jar has been installed.
1795 jarFile := ctx.PrimaryModule().(*Binary).installFile
1796
1797 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1798 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001799 }
Colin Cross2fe66872015-03-30 17:20:39 -07001800}
1801
Colin Crossf506d872017-07-19 15:53:04 -07001802func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001803 if ctx.Arch().ArchType == android.Common {
1804 j.deps(ctx)
1805 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001806}
1807
Colin Cross1b16b0e2019-02-12 14:41:32 -08001808// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1809// as well.
1810//
1811// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1812// compiled against the device bootclasspath.
1813//
1814// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1815// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001816func BinaryFactory() android.Module {
1817 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001818
Colin Cross36242852017-06-23 15:06:31 -07001819 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001820 &module.Module.properties,
1821 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001822 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001823 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001824 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001825
Colin Cross9ae1b922018-06-26 17:59:05 -07001826 module.Module.properties.Installable = proptools.BoolPtr(true)
1827
Colin Cross6b4a32d2017-12-05 13:42:45 -08001828 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1829 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001830 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001831}
1832
Colin Cross1b16b0e2019-02-12 14:41:32 -08001833// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1834//
1835// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1836// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001837func BinaryHostFactory() android.Module {
1838 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001839
Colin Cross36242852017-06-23 15:06:31 -07001840 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001841 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001842 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001843 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001844
Colin Cross9ae1b922018-06-26 17:59:05 -07001845 module.Module.properties.Installable = proptools.BoolPtr(true)
1846
Colin Cross6b4a32d2017-12-05 13:42:45 -08001847 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1848 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001849 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001850}
1851
1852//
1853// Java prebuilts
1854//
1855
Colin Cross74d73e22017-08-02 11:05:49 -07001856type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001857 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001858
Nan Zhangea568a42017-11-08 21:20:04 -08001859 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001860
1861 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001862
1863 // List of shared java libs that this module has dependencies to
1864 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001865
1866 // List of files to remove from the jar file(s)
1867 Exclude_files []string
1868
1869 // List of directories to remove from the jar file(s)
1870 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001871
1872 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001873 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001874}
1875
1876type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001877 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001878 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001879 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001880
Colin Cross74d73e22017-08-02 11:05:49 -07001881 properties ImportProperties
1882
Colin Cross0a6e0072017-08-30 14:24:55 -07001883 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001884 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001885}
1886
Colin Cross83bb3162018-06-25 15:48:06 -07001887func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001888 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001889}
1890
1891func (j *Import) minSdkVersion() string {
1892 return j.sdkVersion()
1893}
1894
Colin Cross74d73e22017-08-02 11:05:49 -07001895func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001896 return &j.prebuilt
1897}
1898
Colin Cross74d73e22017-08-02 11:05:49 -07001899func (j *Import) PrebuiltSrcs() []string {
1900 return j.properties.Jars
1901}
1902
1903func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001904 return j.prebuilt.Name(j.ModuleBase.Name())
1905}
1906
Colin Cross74d73e22017-08-02 11:05:49 -07001907func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001908 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001909}
1910
Colin Cross74d73e22017-08-02 11:05:49 -07001911func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001912 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001913
Nan Zhang4c819fb2018-08-27 18:31:46 -07001914 jarName := ctx.ModuleName() + ".jar"
1915 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001916 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1917 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001918 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001919 inputFile := outputFile
1920 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1921 TransformJetifier(ctx, outputFile, inputFile)
1922 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001923 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001924
1925 ctx.VisitDirectDeps(func(module android.Module) {
1926 otherName := ctx.OtherModuleName(module)
1927 tag := ctx.OtherModuleDependencyTag(module)
1928
1929 switch dep := module.(type) {
1930 case Dependency:
1931 switch tag {
1932 case libTag, staticLibTag:
1933 // sdk lib names from dependencies are re-exported
1934 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1935 }
1936 case SdkLibraryDependency:
1937 switch tag {
1938 case libTag:
1939 // names of sdk libs that are directly depended are exported
1940 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1941 }
1942 }
1943 })
1944
1945 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001946 if Bool(j.properties.Installable) {
1947 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1948 ctx.ModuleName()+".jar", outputFile)
1949 }
Colin Cross2fe66872015-03-30 17:20:39 -07001950}
1951
Colin Cross74d73e22017-08-02 11:05:49 -07001952var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001953
Nan Zhanged19fc32017-10-19 13:06:22 -07001954func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001955 if j.combinedClasspathFile == nil {
1956 return nil
1957 }
Colin Cross37f6d792018-07-12 12:28:41 -07001958 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001959}
1960
1961func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001962 if j.combinedClasspathFile == nil {
1963 return nil
1964 }
Colin Cross37f6d792018-07-12 12:28:41 -07001965 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001966}
1967
Colin Cross331a1212018-08-15 20:40:52 -07001968func (j *Import) ResourceJars() android.Paths {
1969 return nil
1970}
1971
1972func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001973 if j.combinedClasspathFile == nil {
1974 return nil
1975 }
Colin Cross331a1212018-08-15 20:40:52 -07001976 return android.Paths{j.combinedClasspathFile}
1977}
1978
Colin Crossf24a22a2019-01-31 14:12:44 -08001979func (j *Import) DexJar() android.Path {
1980 return nil
1981}
1982
Colin Cross74d73e22017-08-02 11:05:49 -07001983func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001984 return nil
1985}
1986
Jiyong Park1be96912018-05-28 18:02:19 +09001987func (j *Import) ExportedSdkLibs() []string {
1988 return j.exportedSdkLibs
1989}
1990
Colin Cross0c4ce212019-05-03 15:28:19 -07001991func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1992 return nil, nil
1993}
1994
albaltai36ff7dc2018-12-25 14:35:23 +08001995// Add compile time check for interface implementation
1996var _ android.IDEInfo = (*Import)(nil)
1997var _ android.IDECustomizedModuleName = (*Import)(nil)
1998
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001999// Collect information for opening IDE project files in java/jdeps.go.
2000const (
2001 removedPrefix = "prebuilt_"
2002)
2003
2004func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2005 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2006}
2007
2008func (j *Import) IDECustomizedModuleName() string {
2009 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2010 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2011 // solution to get the Import name.
2012 name := j.Name()
2013 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002014 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002015 }
2016 return name
2017}
2018
Colin Cross74d73e22017-08-02 11:05:49 -07002019var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002020
Colin Cross1b16b0e2019-02-12 14:41:32 -08002021// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2022//
2023// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2024// compiled against an Android classpath.
2025//
2026// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2027// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002028func ImportFactory() android.Module {
2029 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002030
Colin Cross74d73e22017-08-02 11:05:49 -07002031 module.AddProperties(&module.properties)
2032
2033 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002034 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002035 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002036}
2037
Colin Cross1b16b0e2019-02-12 14:41:32 -08002038// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2039// module.
2040//
2041// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2042// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002043func ImportFactoryHost() android.Module {
2044 module := &Import{}
2045
2046 module.AddProperties(&module.properties)
2047
2048 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002049 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002050 return module
2051}
2052
Colin Cross42be7612019-02-21 18:12:14 -08002053// dex_import module
2054
2055type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002056 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002057}
2058
2059type DexImport struct {
2060 android.ModuleBase
2061 android.DefaultableModuleBase
2062 prebuilt android.Prebuilt
2063
2064 properties DexImportProperties
2065
2066 dexJarFile android.Path
2067 maybeStrippedDexJarFile android.Path
2068
2069 dexpreopter
2070}
2071
2072func (j *DexImport) Prebuilt() *android.Prebuilt {
2073 return &j.prebuilt
2074}
2075
2076func (j *DexImport) PrebuiltSrcs() []string {
2077 return j.properties.Jars
2078}
2079
2080func (j *DexImport) Name() string {
2081 return j.prebuilt.Name(j.ModuleBase.Name())
2082}
2083
Colin Cross42be7612019-02-21 18:12:14 -08002084func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2085 if len(j.properties.Jars) != 1 {
2086 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2087 }
2088
2089 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2090 j.dexpreopter.isInstallable = true
2091 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2092
2093 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2094 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2095
2096 if j.dexpreopter.uncompressedDex {
2097 rule := android.NewRuleBuilder()
2098
2099 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2100 rule.Temporary(temporary)
2101
2102 // use zip2zip to uncompress classes*.dex files
2103 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002104 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002105 FlagWithInput("-i ", inputJar).
2106 FlagWithOutput("-o ", temporary).
2107 FlagWithArg("-0 ", "'classes*.dex'")
2108
2109 // use zipalign to align uncompressed classes*.dex files
2110 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002111 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002112 Flag("-f").
2113 Text("4").
2114 Input(temporary).
2115 Output(dexOutputFile)
2116
2117 rule.DeleteTemporaryFiles()
2118
2119 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2120 } else {
2121 ctx.Build(pctx, android.BuildParams{
2122 Rule: android.Cp,
2123 Input: inputJar,
2124 Output: dexOutputFile,
2125 })
2126 }
2127
2128 j.dexJarFile = dexOutputFile
2129
2130 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2131
2132 j.maybeStrippedDexJarFile = dexOutputFile
2133
2134 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2135 ctx.ModuleName()+".jar", dexOutputFile)
2136}
2137
2138func (j *DexImport) DexJar() android.Path {
2139 return j.dexJarFile
2140}
2141
2142// dex_import imports a `.jar` file containing classes.dex files.
2143//
2144// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2145// to the device.
2146func DexImportFactory() android.Module {
2147 module := &DexImport{}
2148
2149 module.AddProperties(&module.properties)
2150
2151 android.InitPrebuiltModule(module, &module.properties.Jars)
2152 InitJavaModule(module, android.DeviceSupported)
2153 return module
2154}
2155
Colin Cross89536d42017-07-07 14:35:50 -07002156//
2157// Defaults
2158//
2159type Defaults struct {
2160 android.ModuleBase
2161 android.DefaultsModuleBase
2162}
2163
Colin Cross1b16b0e2019-02-12 14:41:32 -08002164// java_defaults provides a set of properties that can be inherited by other java or android modules.
2165//
2166// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2167// property in the defaults module that exists in the depending module will be prepended to the depending module's
2168// value for that property.
2169//
2170// Example:
2171//
2172// java_defaults {
2173// name: "example_defaults",
2174// srcs: ["common/**/*.java"],
2175// javacflags: ["-Xlint:all"],
2176// aaptflags: ["--auto-add-overlay"],
2177// }
2178//
2179// java_library {
2180// name: "example",
2181// defaults: ["example_defaults"],
2182// srcs: ["example/**/*.java"],
2183// }
2184//
2185// is functionally identical to:
2186//
2187// java_library {
2188// name: "example",
2189// srcs: [
2190// "common/**/*.java",
2191// "example/**/*.java",
2192// ],
2193// javacflags: ["-Xlint:all"],
2194// }
Colin Cross89536d42017-07-07 14:35:50 -07002195func defaultsFactory() android.Module {
2196 return DefaultsFactory()
2197}
2198
2199func DefaultsFactory(props ...interface{}) android.Module {
2200 module := &Defaults{}
2201
2202 module.AddProperties(props...)
2203 module.AddProperties(
2204 &CompilerProperties{},
2205 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002206 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002207 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002208 &aaptProperties{},
2209 &androidLibraryProperties{},
2210 &appProperties{},
2211 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002212 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002213 &ImportProperties{},
2214 &AARImportProperties{},
2215 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002216 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002217 )
2218
2219 android.InitDefaultsModule(module)
Jiyong Parkf0f7ca82019-07-25 04:38:02 +00002220
Colin Cross89536d42017-07-07 14:35:50 -07002221 return module
2222}
Nan Zhangea568a42017-11-08 21:20:04 -08002223
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002224func kytheExtractJavaFactory() android.Singleton {
2225 return &kytheExtractJavaSingleton{}
2226}
2227
2228type kytheExtractJavaSingleton struct {
2229}
2230
2231func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2232 var xrefTargets android.Paths
2233 ctx.VisitAllModules(func(module android.Module) {
2234 if javaModule, ok := module.(xref); ok {
2235 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2236 }
2237 })
2238 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2239 if len(xrefTargets) > 0 {
2240 ctx.Build(pctx, android.BuildParams{
2241 Rule: blueprint.Phony,
2242 Output: android.PathForPhony(ctx, "xref_java"),
2243 Inputs: xrefTargets,
2244 })
2245 }
2246}
2247
Nan Zhangea568a42017-11-08 21:20:04 -08002248var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002249var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002250var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002251var inList = android.InList