blob: 9c7f845ac0e487b102565f38f6f4fb5a09d3ff4a [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 Cross3b706fd2019-09-05 16:44:18 -070028 "github.com/google/blueprint/pathtools"
Colin Cross76b5f0c2017-08-29 16:02:06 -070029 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070030
Colin Cross635c3b02016-05-18 15:37:25 -070031 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070032 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070033 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
Colin Cross89536d42017-07-07 14:35:50 -070037 android.RegisterModuleType("java_defaults", defaultsFactory)
38
Colin Cross9ae1b922018-06-26 17:59:05 -070039 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080040 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070041 android.RegisterModuleType("java_library_host", LibraryHostFactory)
42 android.RegisterModuleType("java_binary", BinaryFactory)
43 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070044 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000045 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070046 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070047 android.RegisterModuleType("java_import", ImportFactory)
48 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080049 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
50 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080051 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070052
Colin Cross798bfce2016-10-12 14:28:16 -070053 android.RegisterSingletonType("logtags", LogtagsSingleton)
Sasha Smundak2a4549e2018-11-05 16:49:08 -080054 android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070055}
56
Jeongik Cha538c0d02019-07-11 15:54:27 +090057func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
58 if sc, ok := ctx.Module().(sdkContext); ok {
59 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
60 if usePlatformAPI != (sc.sdkVersion() == "") {
61 if usePlatformAPI {
62 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
63 } else {
64 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
65 }
66 }
67
68 }
69}
70
Colin Cross2fe66872015-03-30 17:20:39 -070071// TODO:
72// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070073// Renderscript
74// Post-jar passes:
75// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070076// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070077// DroidDoc
78// Findbugs
79
Colin Cross89536d42017-07-07 14:35:50 -070080type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070081 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
82 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080083 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070084
85 // list of source files that should not be used to build the Java module.
86 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080087 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070088
89 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070090 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070091
Colin Cross86a63ff2017-09-27 17:33:10 -070092 // list of directories that should be excluded from java_resource_dirs
93 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070094
Colin Cross0f37af02017-09-27 17:42:05 -070095 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080096 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070097
Colin Crosscedd4762018-09-13 11:26:19 -070098 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080099 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -0700100
Colin Cross7d5136f2015-05-11 13:39:40 -0700101 // list of module-specific flags that will be used for javac compiles
102 Javacflags []string `android:"arch_variant"`
103
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200104 // list of module-specific flags that will be used for kotlinc compiles
105 Kotlincflags []string `android:"arch_variant"`
106
Colin Cross7d5136f2015-05-11 13:39:40 -0700107 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700108 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700109
110 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700111 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700112
113 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800114 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700115
Colin Cross540eff82017-06-22 17:01:52 -0700116 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800117 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700118
119 // If not blank, set the java version passed to javac as -source and -target
120 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700121
Colin Cross9ae1b922018-06-26 17:59:05 -0700122 // If set to true, allow this module to be dexed and installed on devices. Has no
123 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700124 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700125
Colin Cross0f37af02017-09-27 17:42:05 -0700126 // If set to true, include sources used to compile the module in to the final jar
127 Include_srcs *bool
128
Vladimir Marko0975ee02019-04-02 10:29:55 +0100129 // If not empty, classes are restricted to the specified packages and their sub-packages.
130 // This restriction is checked after applying jarjar rules and including static libs.
131 Permitted_packages []string
132
Colin Crossbe9cdb82019-01-21 21:37:16 -0800133 // List of modules to use as annotation processors
134 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700135
Nan Zhang61eaedb2017-11-02 13:28:15 -0700136 // The number of Java source entries each Javac instance can process
137 Javac_shard_size *int64
138
Nan Zhang5f8cb422018-02-06 10:34:32 -0800139 // Add host jdk tools.jar to bootclasspath
140 Use_tools_jar *bool
141
Colin Cross1369cdb2017-09-29 17:58:17 -0700142 Openjdk9 struct {
143 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800144 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700145
146 // List of javac flags that should only be used when passing -source 1.9
147 Javacflags []string
148 }
Colin Crosscb933592017-11-22 13:49:43 -0800149
Colin Cross81440082018-08-15 20:21:55 -0700150 // When compiling language level 9+ .java code in packages that are part of
151 // a system module, patch_module names the module that your sources and
152 // dependencies should be patched into. The Android runtime currently
153 // doesn't implement the JEP 261 module system so this option is only
154 // supported at compile time. It should only be needed to compile tests in
155 // packages that exist in libcore and which are inconvenient to move
156 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100157 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700158
Colin Crosscb933592017-11-22 13:49:43 -0800159 Jacoco struct {
160 // List of classes to include for instrumentation with jacoco to collect coverage
161 // information at runtime when building with coverage enabled. If unset defaults to all
162 // classes.
163 // Supports '*' as the last character of an entry in the list as a wildcard match.
164 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
165 // it matches classes in the package that have the class name as a prefix.
166 Include_filter []string
167
168 // List of classes to exclude from instrumentation with jacoco to collect coverage
169 // information at runtime when building with coverage enabled. Overrides classes selected
170 // by the include_filter property.
171 // Supports '*' as the last character of an entry in the list as a wildcard match.
172 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
173 // it matches classes in the package that have the class name as a prefix.
174 Exclude_filter []string
175 }
176
Andreas Gampef3e5b552018-01-22 21:27:21 -0800177 Errorprone struct {
178 // List of javac flags that should only be used when running errorprone.
179 Javacflags []string
180 }
181
Colin Cross0f2ee152017-12-14 15:22:43 -0800182 Proto struct {
183 // List of extra options that will be passed to the proto generator.
184 Output_params []string
185 }
186
Inseob Kim42882742019-07-30 17:55:33 +0900187 Sysprop struct {
188 Platform *bool
189 } `blueprint:"mutated"`
190
Colin Crosscb933592017-11-22 13:49:43 -0800191 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800192
193 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800194 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700195}
196
Colin Cross89536d42017-07-07 14:35:50 -0700197type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700198 // list of module-specific flags that will be used for dex compiles
199 Dxflags []string `android:"arch_variant"`
200
Jeongik Cha538c0d02019-07-11 15:54:27 +0900201 // if not blank, set to the version of the sdk to compile against.
202 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800203 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700204
Colin Cross83bb3162018-06-25 15:48:06 -0700205 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
206 // Defaults to sdk_version if not set.
207 Min_sdk_version *string
208
Dan Willemsen419290a2018-10-31 15:28:47 -0700209 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
210 // Defaults to sdk_version if not set.
211 Target_sdk_version *string
212
Jeongik Cha356dac42019-08-19 14:09:52 +0900213 // Whether to compile against the platform APIs instead of an SDK.
214 // If true, then sdk_version must be empty. The value of this field
215 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700216 Platform_apis *bool
217
Colin Crossebe1a512017-11-14 13:12:14 -0800218 Aidl struct {
219 // Top level directories to pass to aidl tool
220 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700221
Colin Crossebe1a512017-11-14 13:12:14 -0800222 // Directories rooted at the Android.bp file to pass to aidl tool
223 Local_include_dirs []string
224
225 // directories that should be added as include directories for any aidl sources of modules
226 // that depend on this module, as well as to aidl for this module.
227 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100228
229 // whether to generate traces (for systrace) for this interface
230 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100231
232 // whether to generate Binder#GetTransaction name method.
233 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800234 }
Colin Cross92430102017-10-09 14:59:32 -0700235
236 // If true, export a copy of the module as a -hostdex module for host testing.
237 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700238
Colin Cross7f87f4f2019-04-24 13:41:45 -0700239 Target struct {
240 Hostdex struct {
241 // Additional required dependencies to add to -hostdex modules.
242 Required []string
243 }
244 }
245
David Brazdil17ef5632018-06-27 10:27:45 +0100246 // If set to true, compile dex regardless of installable. Defaults to false.
247 Compile_dex *bool
248
Colin Cross66dbc0b2017-12-28 12:23:20 -0800249 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700250 // If false, disable all optimization. Defaults to true for android_app and android_test
251 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800252 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700253 // True if the module containing this has it set by default.
254 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800255
256 // If true, optimize for size by removing unused code. Defaults to true for apps,
257 // false for libraries and tests.
258 Shrink *bool
259
260 // If true, optimize bytecode. Defaults to false.
261 Optimize *bool
262
263 // If true, obfuscate bytecode. Defaults to false.
264 Obfuscate *bool
265
266 // If true, do not use the flag files generated by aapt that automatically keep
267 // classes referenced by the app manifest. Defaults to false.
268 No_aapt_flags *bool
269
270 // Flags to pass to proguard.
271 Proguard_flags []string
272
273 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800274 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800275 }
276
Colin Cross1369cdb2017-09-29 17:58:17 -0700277 // When targeting 1.9, override the modules to use with --system
278 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700279
280 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800281 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700282}
283
Sasha Smundak2057f822019-04-16 17:16:58 -0700284func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
285 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
286}
287
Colin Cross46c9b8b2017-06-22 16:51:17 -0700288// Module contains the properties and members used by all java module types
289type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700290 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700291 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900292 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700293
Colin Cross89536d42017-07-07 14:35:50 -0700294 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700295 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700296 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700297
Colin Cross331a1212018-08-15 20:40:52 -0700298 // jar file containing header classes including static library dependencies, suitable for
299 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700300 headerJarFile android.Path
301
Colin Cross331a1212018-08-15 20:40:52 -0700302 // jar file containing implementation classes including static library dependencies but no
303 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700304 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700305
Colin Cross331a1212018-08-15 20:40:52 -0700306 // jar file containing only resources including from static library dependencies
307 resourceJar android.Path
308
Colin Cross0c4ce212019-05-03 15:28:19 -0700309 // args and dependencies to package source files into a srcjar
310 srcJarArgs []string
311 srcJarDeps android.Paths
312
Colin Cross331a1212018-08-15 20:40:52 -0700313 // jar file containing implementation classes and resources including static library
314 // dependencies
315 implementationAndResourcesJar android.Path
316
317 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700318 dexJarFile android.Path
319
Colin Cross43f08db2018-11-12 10:13:39 -0800320 // output file that contains classes.dex if it should be in the output file
321 maybeStrippedDexJarFile android.Path
322
Colin Crosscb933592017-11-22 13:49:43 -0800323 // output file containing uninstrumented classes that will be instrumented by jacoco
324 jacocoReportClassesFile android.Path
325
Colin Cross66dbc0b2017-12-28 12:23:20 -0800326 // output file containing mapping of obfuscated names
327 proguardDictionary android.Path
328
Colin Cross331a1212018-08-15 20:40:52 -0700329 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700330 outputFile android.Path
331 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700332
Colin Cross635c3b02016-05-18 15:37:25 -0700333 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700334
Colin Cross635c3b02016-05-18 15:37:25 -0700335 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700336
Colin Cross2fe66872015-03-30 17:20:39 -0700337 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700338 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800339
340 // list of .java files and srcjars that was passed to javac
341 compiledJavaSrcs android.Paths
342 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800343
344 // list of extra progurad flag files
345 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900346
Colin Cross094054a2018-10-17 15:10:48 -0700347 // manifest file to use instead of properties.Manifest
348 overrideManifest android.OptionalPath
349
Jiyong Park1be96912018-05-28 18:02:19 +0900350 // list of SDK lib names that this java moudule is exporting
351 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700352
patricktu242faad2019-09-24 15:41:30 +0800353 // list of source files, collected from srcFiles with uniqie java and all kt files,
354 // will be used by android.IDEInfo struct
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700355 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800356
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800357 // expanded Jarjar_rules
358 expandJarjarRules android.Path
359
Vladimir Marko0975ee02019-04-02 10:29:55 +0100360 // list of additional targets for checkbuild
361 additionalCheckedModules android.Paths
362
Colin Cross988708c2019-05-06 14:04:11 -0700363 // Extra files generated by the module type to be added as java resources.
364 extraResources android.Paths
365
Colin Crossf24a22a2019-01-31 14:12:44 -0800366 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800367 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800368
369 // list of the xref extraction files
370 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700371}
372
Colin Cross41955e82019-05-29 14:40:35 -0700373func (j *Module) OutputFiles(tag string) (android.Paths, error) {
374 switch tag {
375 case "":
376 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700377 case ".jar":
378 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700379 case ".proguard_map":
380 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700381 default:
382 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
383 }
Colin Cross54250902017-12-05 09:28:08 -0800384}
385
Jiyong Park8fd61922018-11-08 02:50:25 +0900386func (j *Module) DexJarFile() android.Path {
387 return j.dexJarFile
388}
389
Colin Cross41955e82019-05-29 14:40:35 -0700390var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800391
Colin Crossf506d872017-07-19 15:53:04 -0700392type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700393 HeaderJars() android.Paths
394 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700395 ResourceJars() android.Paths
396 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800397 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700398 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900399 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700400 SrcJarArgs() ([]string, android.Paths)
Colin Crosse323f3c2019-09-17 15:34:09 -0700401 BaseModuleName() string
Colin Cross2fe66872015-03-30 17:20:39 -0700402}
403
Jiyong Parkc678ad32018-04-10 13:07:10 +0900404type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700405 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
406 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900407}
408
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800409type xref interface {
410 XrefJavaFiles() android.Paths
411}
412
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800413func (j *Module) XrefJavaFiles() android.Paths {
414 return j.kytheFiles
415}
416
Colin Cross89536d42017-07-07 14:35:50 -0700417func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
418 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
419 android.InitDefaultableModule(module)
420}
421
Colin Crossbe1da472017-07-07 15:59:46 -0700422type dependencyTag struct {
423 blueprint.BaseDependencyTag
424 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700425}
426
Colin Crossa4f08812018-10-02 22:03:40 -0700427type jniDependencyTag struct {
428 blueprint.BaseDependencyTag
429 target android.Target
430}
431
Colin Crossbe1da472017-07-07 15:59:46 -0700432var (
Colin Cross4b964c02018-10-15 16:18:06 -0700433 staticLibTag = dependencyTag{name: "staticlib"}
434 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800435 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700436 bootClasspathTag = dependencyTag{name: "bootclasspath"}
437 systemModulesTag = dependencyTag{name: "system modules"}
438 frameworkResTag = dependencyTag{name: "framework-res"}
439 frameworkApkTag = dependencyTag{name: "framework-apk"}
440 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800441 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700442 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
443 certificateTag = dependencyTag{name: "certificate"}
444 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700445 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700446)
Colin Cross2fe66872015-03-30 17:20:39 -0700447
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900448func defaultSdkVersion(ctx checkVendorModuleContext) string {
449 if ctx.SocSpecific() || ctx.DeviceSpecific() {
450 return "system_current"
451 }
452 return ""
453}
454
455type checkVendorModuleContext interface {
456 SocSpecific() bool
457 DeviceSpecific() bool
458}
459
Colin Crossfc3674a2017-09-18 17:41:52 -0700460type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700461 useModule, useFiles, useDefaultLibs, invalidVersion bool
462
Colin Cross86a60ae2018-05-29 14:44:55 -0700463 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700464 systemModules string
465
Colin Crossa97c5d32018-03-28 14:58:31 -0700466 frameworkResModule string
467
Colin Cross86a60ae2018-05-29 14:44:55 -0700468 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700469 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100470
471 noStandardLibs, noFrameworksLibs bool
472}
473
474func (s sdkDep) hasStandardLibs() bool {
475 return !s.noStandardLibs
476}
477
478func (s sdkDep) hasFrameworkLibs() bool {
479 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700480}
481
Colin Crossa4f08812018-10-02 22:03:40 -0700482type jniLib struct {
483 name string
484 path android.Path
485 target android.Target
486}
487
Colin Cross0ea8ba82019-06-06 14:33:29 -0700488func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800489 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
490}
491
Colin Cross0ea8ba82019-06-06 14:33:29 -0700492func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800493 return j.shouldInstrument(ctx) &&
494 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
495 ctx.Config().UnbundledBuild())
496}
497
Colin Cross83bb3162018-06-25 15:48:06 -0700498func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900499 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700500}
501
502func (j *Module) minSdkVersion() string {
503 if j.deviceProperties.Min_sdk_version != nil {
504 return *j.deviceProperties.Min_sdk_version
505 }
506 return j.sdkVersion()
507}
508
Dan Willemsen419290a2018-10-31 15:28:47 -0700509func (j *Module) targetSdkVersion() string {
510 if j.deviceProperties.Target_sdk_version != nil {
511 return *j.deviceProperties.Target_sdk_version
512 }
513 return j.sdkVersion()
514}
515
Colin Crossbe1da472017-07-07 15:59:46 -0700516func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700517 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100518 sdkDep := decodeSdkDep(ctx, sdkContext(j))
519 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700520 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700521 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100522 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100523 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700524 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700525 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700526 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100527 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700528 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700529 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700530 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
531 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800532 }
Colin Crossbe1da472017-07-07 15:59:46 -0700533 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700534 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100535 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100536 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100537 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700538 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700539 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800540 if ctx.ModuleName() == "android_stubs_current" ||
541 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700542 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700543 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800544 }
Colin Cross2fe66872015-03-30 17:20:39 -0700545 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700546
Colin Cross42d48b72018-08-29 14:10:52 -0700547 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
548 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700549
Colin Crossbe9cdb82019-01-21 21:37:16 -0800550 ctx.AddFarVariationDependencies([]blueprint.Variation{
551 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
552 }, pluginTag, j.properties.Plugins...)
553
Colin Crossfe17f6f2019-03-28 19:30:56 -0700554 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700555 if j.hasSrcExt(".proto") {
556 protoDeps(ctx, &j.protoProperties)
557 }
Colin Cross93e85952017-08-15 13:34:18 -0700558
559 if j.hasSrcExt(".kt") {
560 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
561 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700562 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
563 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800564 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800565 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
566 }
Colin Cross93e85952017-08-15 13:34:18 -0700567 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800568
569 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700570 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800571 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700572}
573
574func hasSrcExt(srcs []string, ext string) bool {
575 for _, src := range srcs {
576 if filepath.Ext(src) == ext {
577 return true
578 }
579 }
580
581 return false
582}
583
Nan Zhang61eaedb2017-11-02 13:28:15 -0700584func shardPaths(paths android.Paths, shardSize int) []android.Paths {
585 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
586 for len(paths) > shardSize {
587 ret = append(ret, paths[0:shardSize])
588 paths = paths[shardSize:]
589 }
590 if len(paths) > 0 {
591 ret = append(ret, paths)
592 }
593 return ret
594}
595
Colin Cross6af17aa2017-09-20 12:59:05 -0700596func (j *Module) hasSrcExt(ext string) bool {
597 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700598}
599
Colin Cross46c9b8b2017-06-22 16:51:17 -0700600func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700601 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700602
Colin Crossebe1a512017-11-14 13:12:14 -0800603 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
604 aidlIncludes = append(aidlIncludes,
605 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
606 aidlIncludes = append(aidlIncludes,
607 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700608
Colin Cross3047fa22019-04-18 10:56:44 -0700609 var flags []string
610 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700611
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700612 if aidlPreprocess.Valid() {
613 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700614 deps = append(deps, aidlPreprocess.Path())
615 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700616 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700617 }
618
Colin Cross3047fa22019-04-18 10:56:44 -0700619 if len(j.exportAidlIncludeDirs) > 0 {
620 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
621 }
622
623 if len(aidlIncludes) > 0 {
624 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
625 }
626
Colin Cross635c3b02016-05-18 15:37:25 -0700627 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800628 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700629 flags = append(flags, "-I"+src.String())
630 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700631
Martijn Coeneneab15642018-03-09 09:29:59 +0100632 if Bool(j.deviceProperties.Aidl.Generate_traces) {
633 flags = append(flags, "-t")
634 }
635
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100636 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
637 flags = append(flags, "--transaction_names")
638 }
639
Colin Cross3047fa22019-04-18 10:56:44 -0700640 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700641}
642
Colin Cross32f676a2017-09-06 13:41:06 -0700643type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800644 classpath classpath
645 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700646 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800647 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700648 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700649 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700650 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700651 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800652 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700653 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700654 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700655 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700656 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800657 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800658
659 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700660}
Colin Cross2fe66872015-03-30 17:20:39 -0700661
Colin Cross54250902017-12-05 09:28:08 -0800662func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
663 for _, f := range dep.Srcs() {
664 if f.Ext() != ".jar" {
665 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
666 ctx.OtherModuleName(dep.(blueprint.Module)))
667 }
668 }
669}
670
Jiyong Park2d492942018-03-05 17:44:10 +0900671type linkType int
672
673const (
674 javaCore linkType = iota
675 javaSdk
676 javaSystem
677 javaPlatform
678)
679
Jiyong Park46f78fb2018-10-20 16:33:17 +0900680func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700681 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700682 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900683 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
684 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100685 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900686 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100687 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900688 return javaCore, false
689 case name == "android_system_stubs_current":
690 return javaSystem, true
691 case strings.HasPrefix(ver, "system_"):
692 return javaSystem, false
693 case name == "android_test_stubs_current":
694 return javaSystem, true
695 case strings.HasPrefix(ver, "test_"):
696 return javaPlatform, false
697 case name == "android_stubs_current":
698 return javaSdk, true
699 case ver == "current":
700 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100701 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900702 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700703 default:
704 if _, err := strconv.Atoi(ver); err != nil {
705 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
706 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900707 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900708 }
709}
710
Jiyong Park750e5572018-01-31 00:20:13 +0900711func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700712 if ctx.Host() {
713 return
714 }
715
Jiyong Park46f78fb2018-10-20 16:33:17 +0900716 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
717 if stubs {
718 return
719 }
720 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900721 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."
722
723 switch myLinkType {
724 case javaCore:
725 if otherLinkType != javaCore {
726 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 +0900727 ctx.OtherModuleName(to))
728 }
Jiyong Park2d492942018-03-05 17:44:10 +0900729 break
730 case javaSdk:
731 if otherLinkType != javaCore && otherLinkType != javaSdk {
732 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
733 ctx.OtherModuleName(to))
734 }
735 break
736 case javaSystem:
737 if otherLinkType == javaPlatform {
738 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
739 ctx.OtherModuleName(to))
740 }
741 break
742 case javaPlatform:
743 // no restriction on link-type
744 break
Jiyong Park750e5572018-01-31 00:20:13 +0900745 }
746}
747
Colin Cross32f676a2017-09-06 13:41:06 -0700748func (j *Module) collectDeps(ctx android.ModuleContext) deps {
749 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700750
Colin Cross300f0382018-03-06 13:11:51 -0800751 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700752 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800753 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700754 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800755 } else if sdkDep.useFiles {
756 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700757 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700758 deps.aidlPreprocess = sdkDep.aidl
759 } else {
760 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800761 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700762 }
763
Colin Crossd11fcda2017-10-23 17:59:01 -0700764 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700765 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700766 tag := ctx.OtherModuleDependencyTag(module)
767
Colin Crossa4f08812018-10-02 22:03:40 -0700768 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700769 // Handled by AndroidApp.collectAppDeps
770 return
771 }
772 if tag == certificateTag {
773 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700774 return
775 }
776
Jiyong Park750e5572018-01-31 00:20:13 +0900777 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700778 switch tag {
779 case bootClasspathTag, libTag, staticLibTag:
780 checkLinkType(ctx, j, to, tag.(dependencyTag))
781 }
Jiyong Park750e5572018-01-31 00:20:13 +0900782 }
Colin Cross54250902017-12-05 09:28:08 -0800783 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800784 case SdkLibraryDependency:
785 switch tag {
786 case libTag:
787 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
788 // names of sdk libs that are directly depended are exported
789 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700790 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800791 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
792 }
Colin Cross54250902017-12-05 09:28:08 -0800793 case Dependency:
794 switch tag {
795 case bootClasspathTag:
796 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700797 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800798 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900799 // sdk lib names from dependencies are re-exported
800 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700801 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800802 case staticLibTag:
803 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
804 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
805 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700806 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900807 // sdk lib names from dependencies are re-exported
808 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700809 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800810 case pluginTag:
811 if plugin, ok := dep.(*Plugin); ok {
812 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
813 if plugin.pluginProperties.Processor_class != nil {
814 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
815 }
816 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
817 } else {
818 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
819 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800820 case frameworkApkTag:
821 if ctx.ModuleName() == "android_stubs_current" ||
822 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700823 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800824 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
825 // resource files out of there for aapt.
826 //
827 // Normally the package rule runs aapt, which includes the resource,
828 // but we're not running that in our package rule so just copy in the
829 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700830 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800831 }
Colin Cross54250902017-12-05 09:28:08 -0800832 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700833 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800834 case kotlinAnnotationsTag:
835 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800836 }
837
Colin Cross54250902017-12-05 09:28:08 -0800838 case android.SourceFileProducer:
839 switch tag {
840 case libTag:
841 checkProducesJars(ctx, dep)
842 deps.classpath = append(deps.classpath, dep.Srcs()...)
843 case staticLibTag:
844 checkProducesJars(ctx, dep)
845 deps.classpath = append(deps.classpath, dep.Srcs()...)
846 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
847 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800848 }
849 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700850 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700851 case systemModulesTag:
852 if deps.systemModules != nil {
853 panic("Found two system module dependencies")
854 }
855 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000856 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700857 panic("Missing directory for system module dependency")
858 }
Colin Crossb77043e2019-07-16 13:57:13 -0700859 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700860 }
Colin Crossec7a0422017-07-07 14:47:12 -0700861 }
Colin Cross2fe66872015-03-30 17:20:39 -0700862 })
863
Jiyong Park1be96912018-05-28 18:02:19 +0900864 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
865
Colin Cross32f676a2017-09-06 13:41:06 -0700866 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700867}
868
Colin Cross83bb3162018-06-25 15:48:06 -0700869func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700870 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800871 v := sdkContext.sdkVersion()
872 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100873 if ctx.Config().IsPdkBuild() &&
874 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700875 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800876 latestSdkVersion := 0
877 if len(sdkVersions) > 0 {
878 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
879 }
880 v = strconv.Itoa(latestSdkVersion)
881 }
882
883 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700884 if err != nil {
885 ctx.PropertyErrorf("sdk_version", "%s", err)
886 }
Nan Zhang357466b2018-04-17 17:38:36 -0700887 if javaVersion != "" {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100888 ret = normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700889 } else if ctx.Device() && sdk <= 23 {
890 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100891 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700892 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100893 } else if ctx.Device() &&
894 sdkContext.sdkVersion() != "" &&
895 sdkContext.sdkVersion() != "none" &&
896 sdkContext.sdkVersion() != "core_platform" &&
897 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700898 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
899 ret = "1.8"
900 } else {
901 ret = "1.9"
902 }
903
904 return ret
905}
906
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100907func normalizeJavaVersion(ctx android.ModuleContext, javaVersion string) string {
908 switch javaVersion {
909 case "1.6", "6":
910 return "1.6"
911 case "1.7", "7":
912 return "1.7"
913 case "1.8", "8":
914 return "1.8"
915 case "1.9", "9":
916 return "1.9"
917 case "10", "11":
918 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
919 return "unsupported"
920 default:
921 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
922 return "unrecognized"
923 }
924}
925
Nan Zhanged19fc32017-10-19 13:06:22 -0700926func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700927
Colin Crossf03c82b2015-04-13 13:53:40 -0700928 var flags javaBuilderFlags
929
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100930 // javaVersion flag.
931 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
932
Nan Zhanged19fc32017-10-19 13:06:22 -0700933 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700934 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100935 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700936 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700937 }
Colin Cross6510f912017-11-29 00:27:14 -0800938 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700939 // Override the -g flag passed globally to remove local variable debug info to reduce
940 // disk and memory usage.
941 javacFlags = append(javacFlags, "-g:source,lines")
942 }
Colin Cross64162712017-08-08 13:17:59 -0700943
Colin Cross66548102018-06-19 22:47:35 -0700944 if ctx.Config().RunErrorProne() {
945 if config.ErrorProneClasspath == nil {
946 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
947 }
948
949 errorProneFlags := []string{
950 "-Xplugin:ErrorProne",
951 "${config.ErrorProneChecks}",
952 }
953 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
954
955 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
956 "'" + strings.Join(errorProneFlags, " ") + "'"
957 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800958 }
959
Nan Zhanged19fc32017-10-19 13:06:22 -0700960 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800961 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
962 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700963 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800964
Colin Crossbe9cdb82019-01-21 21:37:16 -0800965 flags.processor = strings.Join(deps.processorClasses, ",")
966
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100967 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100968 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800969 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
970 // Give host-side tools a version of OpenJDK's standard libraries
971 // close to what they're targeting. As of Dec 2017, AOSP is only
972 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
973 //
974 // When building with OpenJDK 8, the following should have no
975 // effect since those jars would be available by default.
976 //
977 // When building with OpenJDK 9 but targeting a version < 1.8,
978 // putting them on the bootclasspath means that:
979 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
980 // b) references to existing APIs are not reinterpreted in an
981 // OpenJDK 9-specific way, eg. calls to subclasses of
982 // java.nio.Buffer as in http://b/70862583
983 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
984 flags.bootClasspath = append(flags.bootClasspath,
985 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
986 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800987 if Bool(j.properties.Use_tools_jar) {
988 flags.bootClasspath = append(flags.bootClasspath,
989 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
990 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800991 }
992
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100993 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800994 // Manually specify build directory in case it is not under the repo root.
995 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
996 // just adding a symlink under the root doesn't help.)
997 patchPaths := ".:" + ctx.Config().BuildDir()
998 classPath := flags.classpath.FormJavaClassPath("")
999 if classPath != "" {
1000 patchPaths += ":" + classPath
1001 }
1002 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001003 }
1004
Nan Zhanged19fc32017-10-19 13:06:22 -07001005 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001006 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001007
Nan Zhanged19fc32017-10-19 13:06:22 -07001008 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001009 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001010
Colin Cross81440082018-08-15 20:21:55 -07001011 if len(javacFlags) > 0 {
1012 // optimization.
1013 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1014 flags.javacFlags = "$javacFlags"
1015 }
1016
Nan Zhanged19fc32017-10-19 13:06:22 -07001017 return flags
1018}
Colin Crossc0b06f12015-04-08 13:03:43 -07001019
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001020func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001021 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001022
1023 deps := j.collectDeps(ctx)
1024 flags := j.collectBuilderFlags(ctx, deps)
1025
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001026 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001027 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1028 }
Colin Cross8a497952019-03-05 22:25:09 -08001029 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001030 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001031 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001032 }
1033
Colin Crossaf050172017-11-15 23:01:59 -08001034 srcFiles = j.genSources(ctx, srcFiles, flags)
1035
1036 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001037 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001038 if aaptSrcJar != nil {
1039 srcJars = append(srcJars, aaptSrcJar)
1040 }
Colin Crossb7a63242015-04-16 14:09:14 -07001041
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001042 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001043 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001044 }
1045
Colin Cross1ee23172017-10-18 14:44:18 -07001046 jarName := ctx.ModuleName() + ".jar"
1047
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001048 javaSrcFiles := srcFiles.FilterByExt(".java")
1049 var uniqueSrcFiles android.Paths
1050 set := make(map[string]bool)
1051 for _, v := range javaSrcFiles {
1052 if _, found := set[v.String()]; !found {
1053 set[v.String()] = true
1054 uniqueSrcFiles = append(uniqueSrcFiles, v)
1055 }
1056 }
1057
patricktu242faad2019-09-24 15:41:30 +08001058 // Collect .java files for AIDEGen
1059 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, uniqueSrcFiles.Strings()...)
1060
Colin Cross55f63ea2018-08-27 12:37:09 -07001061 var kotlinJars android.Paths
1062
Colin Cross93e85952017-08-15 13:34:18 -07001063 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001064 // user defined kotlin flags.
1065 kotlincFlags := j.properties.Kotlincflags
1066 CheckKotlincFlags(ctx, kotlincFlags)
1067
Colin Cross93e85952017-08-15 13:34:18 -07001068 // If there are kotlin files, compile them first but pass all the kotlin and java files
1069 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1070 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001071 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001072 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001073 kotlincFlags = append(kotlincFlags, "-no-jdk")
1074 }
1075 if len(kotlincFlags) > 0 {
1076 // optimization.
1077 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1078 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001079 }
1080
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001081 var kotlinSrcFiles android.Paths
1082 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1083 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1084
patricktu242faad2019-09-24 15:41:30 +08001085 // Collect .kt files for AIDEGen
1086 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.FilterByExt(".kt").Strings()...)
1087
Colin Crossafbb1732019-01-17 15:42:52 -08001088 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1089 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1090
1091 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1092 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1093
1094 if len(flags.processorPath) > 0 {
1095 // Use kapt for annotation processing
1096 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1097 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1098 srcJars = append(srcJars, kaptSrcJar)
1099 // Disable annotation processing in javac, it's already been handled by kapt
1100 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001101 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001102 }
Colin Cross93e85952017-08-15 13:34:18 -07001103
Colin Cross1ee23172017-10-18 14:44:18 -07001104 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001105 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001106 if ctx.Failed() {
1107 return
1108 }
1109
1110 // Make javac rule depend on the kotlinc rule
1111 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001112
Colin Cross93e85952017-08-15 13:34:18 -07001113 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001114 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001115 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001116 }
1117
Colin Cross55f63ea2018-08-27 12:37:09 -07001118 jars := append(android.Paths(nil), kotlinJars...)
1119
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001120 // Store the list of .java files that was passed to javac
1121 j.compiledJavaSrcs = uniqueSrcFiles
1122 j.compiledSrcJars = srcJars
1123
Nan Zhang61eaedb2017-11-02 13:28:15 -07001124 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001125 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001126 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1127 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001128 // Formerly, there was a check here that prevented annotation processors
1129 // from being used when sharding was enabled, as some annotation processors
1130 // do not function correctly in sharded environments. It was removed to
1131 // allow for the use of annotation processors that do function correctly
1132 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001133 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001134 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001135 if ctx.Failed() {
1136 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001137 }
1138 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001139 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001140 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001141 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001142 // If error-prone is enabled, add an additional rule to compile the java files into
1143 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001144 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001145 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1146 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001147 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001148 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001149 extraJarDeps = append(extraJarDeps, errorprone)
1150 }
1151
Nan Zhang61eaedb2017-11-02 13:28:15 -07001152 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001153 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001154 shardSize := int(*(j.properties.Javac_shard_size))
1155 var shardSrcs []android.Paths
1156 if len(uniqueSrcFiles) > 0 {
1157 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1158 for idx, shardSrc := range shardSrcs {
Colin Cross3b706fd2019-09-05 16:44:18 -07001159 classes := j.compileJavaClasses(ctx, jarName, idx, shardSrc,
1160 nil, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001161 jars = append(jars, classes)
1162 }
1163 }
1164 if len(srcJars) > 0 {
Colin Cross3b706fd2019-09-05 16:44:18 -07001165 classes := j.compileJavaClasses(ctx, jarName, len(shardSrcs),
1166 nil, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001167 jars = append(jars, classes)
1168 }
1169 } else {
Colin Cross3b706fd2019-09-05 16:44:18 -07001170 classes := j.compileJavaClasses(ctx, jarName, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001171 jars = append(jars, classes)
1172 }
Colin Crossd6891432017-09-27 17:39:56 -07001173 if ctx.Failed() {
1174 return
1175 }
Colin Cross2fe66872015-03-30 17:20:39 -07001176 }
1177
Colin Cross0c4ce212019-05-03 15:28:19 -07001178 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1179
1180 var includeSrcJar android.WritablePath
1181 if Bool(j.properties.Include_srcs) {
1182 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1183 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1184 }
1185
Colin Crosscedd4762018-09-13 11:26:19 -07001186 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1187 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001188 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001189 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001190
1191 var resArgs []string
1192 var resDeps android.Paths
1193
1194 resArgs = append(resArgs, dirArgs...)
1195 resDeps = append(resDeps, dirDeps...)
1196
1197 resArgs = append(resArgs, fileArgs...)
1198 resDeps = append(resDeps, fileDeps...)
1199
Colin Cross988708c2019-05-06 14:04:11 -07001200 resArgs = append(resArgs, extraArgs...)
1201 resDeps = append(resDeps, extraDeps...)
1202
Colin Cross40a36712017-09-27 17:41:35 -07001203 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001204 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001205 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001206 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001207 if ctx.Failed() {
1208 return
1209 }
1210 }
1211
Colin Cross0c4ce212019-05-03 15:28:19 -07001212 var resourceJars android.Paths
1213 if j.resourceJar != nil {
1214 resourceJars = append(resourceJars, j.resourceJar)
1215 }
1216 if Bool(j.properties.Include_srcs) {
1217 resourceJars = append(resourceJars, includeSrcJar)
1218 }
1219 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001220
Colin Cross0c4ce212019-05-03 15:28:19 -07001221 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001222 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001223 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001224 false, nil, nil)
1225 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001226 } else if len(resourceJars) == 1 {
1227 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001228 }
1229
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001230 if len(deps.staticJars) > 0 {
1231 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001232 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001233
Colin Cross094054a2018-10-17 15:10:48 -07001234 manifest := j.overrideManifest
1235 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001236 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001237 }
Colin Cross635acc92017-09-12 22:50:46 -07001238
Colin Cross8a497952019-03-05 22:25:09 -08001239 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001240 if len(services) > 0 {
1241 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1242 var zipargs []string
1243 for _, file := range services {
1244 serviceFile := file.String()
1245 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1246 }
1247 ctx.Build(pctx, android.BuildParams{
1248 Rule: zip,
1249 Output: servicesJar,
1250 Implicits: services,
1251 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001252 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001253 },
1254 })
1255 jars = append(jars, servicesJar)
1256 }
1257
Colin Cross0a6e0072017-08-30 14:24:55 -07001258 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001259 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001260 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001261
1262 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001263 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1264 // Optimization: skip the combine step if there is nothing to do
1265 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1266 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1267 // any if len(jars) == 1.
1268 outputFile = moduleOutPath
1269 } else {
1270 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1271 ctx.Build(pctx, android.BuildParams{
1272 Rule: android.Cp,
1273 Input: jars[0],
1274 Output: combinedJar,
1275 })
1276 outputFile = combinedJar
1277 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001278 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001279 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001280 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001281 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001282 outputFile = combinedJar
1283 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001284
Colin Cross331a1212018-08-15 20:40:52 -07001285 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001286 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001287 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001288 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001289 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001290 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001291
1292 // jarjar resource jar if necessary
1293 if j.resourceJar != nil {
1294 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001295 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001296 j.resourceJar = resourceJarJarFile
1297 }
1298
Colin Cross0a6e0072017-08-30 14:24:55 -07001299 if ctx.Failed() {
1300 return
1301 }
1302 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001303
1304 // Check package restrictions if necessary.
1305 if len(j.properties.Permitted_packages) > 0 {
1306 // Check packages and copy to package-checked file.
1307 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1308 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1309 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1310
1311 if ctx.Failed() {
1312 return
1313 }
1314 }
1315
Nan Zhanged19fc32017-10-19 13:06:22 -07001316 j.implementationJarFile = outputFile
1317 if j.headerJarFile == nil {
1318 j.headerJarFile = j.implementationJarFile
1319 }
Colin Cross2fe66872015-03-30 17:20:39 -07001320
Colin Cross6510f912017-11-29 00:27:14 -08001321 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001322 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1323 j.properties.Instrument = true
1324 }
1325 }
1326
Colin Cross3144dfc2018-01-03 15:06:47 -08001327 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001328 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1329 }
1330
Colin Cross331a1212018-08-15 20:40:52 -07001331 // merge implementation jar with resources if necessary
1332 implementationAndResourcesJar := outputFile
1333 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001334 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001335 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001336 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001337 false, nil, nil)
1338 implementationAndResourcesJar = combinedJar
1339 }
1340
1341 j.implementationAndResourcesJar = implementationAndResourcesJar
1342
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001343 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001344 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001345 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001346 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001347 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001348 if ctx.Failed() {
1349 return
1350 }
Colin Cross331a1212018-08-15 20:40:52 -07001351
Jiyong Park09cb6292019-07-15 15:29:23 +09001352 // Hidden API CSV generation and dex encoding
1353 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1354 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001355
Colin Cross331a1212018-08-15 20:40:52 -07001356 // merge dex jar with resources if necessary
1357 if j.resourceJar != nil {
1358 jars := android.Paths{dexOutputFile, j.resourceJar}
1359 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1360 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1361 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001362 if j.deviceProperties.UncompressDex {
1363 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1364 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1365 dexOutputFile = combinedAlignedJar
1366 } else {
1367 dexOutputFile = combinedJar
1368 }
Colin Cross331a1212018-08-15 20:40:52 -07001369 }
1370
1371 j.dexJarFile = dexOutputFile
1372
Colin Cross8faf8fc2019-01-16 15:15:52 -08001373 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001374 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1375
1376 j.maybeStrippedDexJarFile = dexOutputFile
1377
Colin Cross3063b782018-08-15 11:19:12 -07001378 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001379
1380 if ctx.Failed() {
1381 return
1382 }
Colin Cross331a1212018-08-15 20:40:52 -07001383 } else {
1384 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001385 }
Colin Cross331a1212018-08-15 20:40:52 -07001386
Colin Crossb7a63242015-04-16 14:09:14 -07001387 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001388
1389 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1390 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001391}
1392
Colin Cross3b706fd2019-09-05 16:44:18 -07001393func (j *Module) compileJavaClasses(ctx android.ModuleContext, jarName string, idx int,
1394 srcFiles, srcJars android.Paths, flags javaBuilderFlags, extraJarDeps android.Paths) android.WritablePath {
1395
1396 kzipName := pathtools.ReplaceExtension(jarName, "kzip")
1397 if idx >= 0 {
1398 kzipName = strings.TrimSuffix(jarName, filepath.Ext(jarName)) + strconv.Itoa(idx) + ".kzip"
1399 jarName += strconv.Itoa(idx)
1400 }
1401
1402 classes := android.PathForModuleOut(ctx, "javac", jarName)
1403 TransformJavaToClasses(ctx, classes, idx, srcFiles, srcJars, flags, extraJarDeps)
1404
1405 if ctx.Config().EmitXrefRules() {
1406 extractionFile := android.PathForModuleOut(ctx, kzipName)
1407 emitXrefRule(ctx, extractionFile, idx, srcFiles, srcJars, flags, extraJarDeps)
1408 j.kytheFiles = append(j.kytheFiles, extractionFile)
1409 }
1410
1411 return classes
1412}
1413
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001414// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1415// since some of these flags may be used internally.
1416func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1417 for _, flag := range flags {
1418 flag = strings.TrimSpace(flag)
1419
1420 if !strings.HasPrefix(flag, "-") {
1421 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1422 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1423 ctx.PropertyErrorf("kotlincflags",
1424 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1425 } else if inList(flag, config.KotlincIllegalFlags) {
1426 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1427 } else if flag == "-include-runtime" {
1428 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1429 } else {
1430 args := strings.Split(flag, " ")
1431 if args[0] == "-kotlin-home" {
1432 ctx.PropertyErrorf("kotlincflags",
1433 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1434 }
1435 }
1436 }
1437}
1438
Colin Cross8eadbf02017-10-24 17:46:00 -07001439func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001440 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001441
1442 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001443 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001444 // Compile java sources into turbine.jar.
1445 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1446 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1447 if ctx.Failed() {
1448 return nil
1449 }
1450 jars = append(jars, turbineJar)
1451 }
1452
Colin Cross55f63ea2018-08-27 12:37:09 -07001453 jars = append(jars, extraJars...)
1454
Nan Zhanged19fc32017-10-19 13:06:22 -07001455 // Combine any static header libraries into classes-header.jar. If there is only
1456 // one input jar this step will be skipped.
1457 var headerJar android.Path
1458 jars = append(jars, deps.staticHeaderJars...)
1459
Colin Cross5c6ecc12017-10-23 18:12:27 -07001460 // we cannot skip the combine step for now if there is only one jar
1461 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1462 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001463 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001464 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001465 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001466
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001467 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001468 // Transform classes.jar into classes-jarjar.jar
1469 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001470 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001471 headerJar = jarjarFile
1472 if ctx.Failed() {
1473 return nil
1474 }
1475 }
1476
1477 return headerJar
1478}
1479
Colin Crosscb933592017-11-22 13:49:43 -08001480func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001481 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001482
Colin Cross7a3139e2017-12-19 13:57:50 -08001483 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001484
Colin Cross84c38822018-01-03 15:59:46 -08001485 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001486 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1487
1488 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1489
1490 j.jacocoReportClassesFile = jacocoReportClassesFile
1491
1492 return instrumentedJar
1493}
1494
albaltai36ff7dc2018-12-25 14:35:23 +08001495var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001496
Nan Zhanged19fc32017-10-19 13:06:22 -07001497func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001498 if j.headerJarFile == nil {
1499 return nil
1500 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001501 return android.Paths{j.headerJarFile}
1502}
1503
1504func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001505 if j.implementationJarFile == nil {
1506 return nil
1507 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001508 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001509}
1510
Colin Crossf24a22a2019-01-31 14:12:44 -08001511func (j *Module) DexJar() android.Path {
1512 return j.dexJarFile
1513}
1514
Colin Cross331a1212018-08-15 20:40:52 -07001515func (j *Module) ResourceJars() android.Paths {
1516 if j.resourceJar == nil {
1517 return nil
1518 }
1519 return android.Paths{j.resourceJar}
1520}
1521
1522func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001523 if j.implementationAndResourcesJar == nil {
1524 return nil
1525 }
Colin Cross331a1212018-08-15 20:40:52 -07001526 return android.Paths{j.implementationAndResourcesJar}
1527}
1528
Colin Cross46c9b8b2017-06-22 16:51:17 -07001529func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001530 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001531 return j.exportAidlIncludeDirs
1532}
1533
Jiyong Park1be96912018-05-28 18:02:19 +09001534func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001535 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001536 return j.exportedSdkLibs
1537}
1538
Colin Cross0c4ce212019-05-03 15:28:19 -07001539func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1540 return j.srcJarArgs, j.srcJarDeps
1541}
1542
Colin Cross46c9b8b2017-06-22 16:51:17 -07001543var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001544
Colin Cross46c9b8b2017-06-22 16:51:17 -07001545func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001546 return j.logtagsSrcs
1547}
1548
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001549// Collect information for opening IDE project files in java/jdeps.go.
1550func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1551 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1552 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001553 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001554 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001555 if j.expandJarjarRules != nil {
1556 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001557 }
1558}
1559
1560func (j *Module) CompilerDeps() []string {
1561 jdeps := []string{}
1562 jdeps = append(jdeps, j.properties.Libs...)
1563 jdeps = append(jdeps, j.properties.Static_libs...)
1564 return jdeps
1565}
1566
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001567func (j *Module) hasCode(ctx android.ModuleContext) bool {
1568 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1569 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1570}
1571
Colin Cross2fe66872015-03-30 17:20:39 -07001572//
1573// Java libraries (.jar file)
1574//
1575
Colin Crossf506d872017-07-19 15:53:04 -07001576type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001577 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001578}
1579
Colin Cross42be7612019-02-21 18:12:14 -08001580func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001581 // Store uncompressed (and do not strip) dex files from boot class path jars.
1582 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1583 return true
1584 }
1585
1586 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001587 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001588 return true
1589 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001590 if ctx.Config().UncompressPrivAppDex() &&
1591 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1592 return true
1593 }
1594
Colin Cross2fc72f62018-12-21 12:59:54 -08001595 return false
1596}
1597
Colin Crossf506d872017-07-19 15:53:04 -07001598func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001599 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1600 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001601 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001602 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001603 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001604 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001605
Jiyong Park7f7766d2019-07-25 22:02:35 +09001606 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1607 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001608 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1609 ctx.ModuleName()+".jar", j.outputFile)
1610 }
Colin Crossb7a63242015-04-16 14:09:14 -07001611}
1612
Colin Crossf506d872017-07-19 15:53:04 -07001613func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001614 j.deps(ctx)
1615}
1616
Colin Cross1b16b0e2019-02-12 14:41:32 -08001617// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1618//
1619// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1620// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1621// as a `static_libs` dependency of another module.
1622//
1623// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1624// a device.
1625//
1626// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1627// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001628func LibraryFactory() android.Module {
1629 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001630
Colin Cross9ae1b922018-06-26 17:59:05 -07001631 module.AddProperties(
1632 &module.Module.properties,
1633 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001634 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001635 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001636
Colin Cross9ae1b922018-06-26 17:59:05 -07001637 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001638 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001639 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001640}
1641
Colin Cross1b16b0e2019-02-12 14:41:32 -08001642// java_library_static is an obsolete alias for java_library.
1643func LibraryStaticFactory() android.Module {
1644 return LibraryFactory()
1645}
1646
1647// java_library_host builds and links sources into a `.jar` file for the host.
1648//
1649// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1650// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001651func LibraryHostFactory() android.Module {
1652 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001653
Colin Cross6af17aa2017-09-20 12:59:05 -07001654 module.AddProperties(
1655 &module.Module.properties,
1656 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001657
Colin Cross9ae1b922018-06-26 17:59:05 -07001658 module.Module.properties.Installable = proptools.BoolPtr(true)
1659
Colin Cross89536d42017-07-07 14:35:50 -07001660 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001661 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001662 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001663}
1664
1665//
Colin Crossb628ea52018-08-14 16:42:33 -07001666// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001667//
1668
1669type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001670 // list of compatibility suites (for example "cts", "vts") that the module should be
1671 // installed into.
1672 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001673
1674 // the name of the test configuration (for example "AndroidTest.xml") that should be
1675 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001676 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001677
Jack He33338892018-09-19 02:21:28 -07001678 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1679 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001680 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001681
Colin Crossd96ca352018-08-10 16:06:24 -07001682 // list of files or filegroup modules that provide data that should be installed alongside
1683 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001684 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001685}
1686
Paul Duffin42df1442019-03-20 12:45:53 +00001687type testHelperLibraryProperties struct {
1688 // list of compatibility suites (for example "cts", "vts") that the module should be
1689 // installed into.
1690 Test_suites []string `android:"arch_variant"`
1691}
1692
Colin Cross05638fc2018-04-09 18:40:24 -07001693type Test struct {
1694 Library
1695
1696 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001697
1698 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001699 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001700}
1701
Paul Duffin42df1442019-03-20 12:45:53 +00001702type TestHelperLibrary struct {
1703 Library
1704
1705 testHelperLibraryProperties testHelperLibraryProperties
1706}
1707
Colin Cross303e21f2018-08-07 16:49:25 -07001708func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001709 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 -08001710 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001711
1712 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001713}
1714
Paul Duffin42df1442019-03-20 12:45:53 +00001715func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1716 j.Library.GenerateAndroidBuildActions(ctx)
1717}
1718
Colin Cross1b16b0e2019-02-12 14:41:32 -08001719// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1720// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1721//
1722// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1723// compiled against the device bootclasspath.
1724//
1725// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1726// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001727func TestFactory() android.Module {
1728 module := &Test{}
1729
1730 module.AddProperties(
1731 &module.Module.properties,
1732 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001733 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001734 &module.Module.protoProperties,
1735 &module.testProperties)
1736
Colin Cross9ae1b922018-06-26 17:59:05 -07001737 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001738 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001739
Colin Cross05638fc2018-04-09 18:40:24 -07001740 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001741 return module
1742}
1743
Paul Duffin42df1442019-03-20 12:45:53 +00001744// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1745func TestHelperLibraryFactory() android.Module {
1746 module := &TestHelperLibrary{}
1747
1748 module.AddProperties(
1749 &module.Module.properties,
1750 &module.Module.deviceProperties,
1751 &module.Module.dexpreoptProperties,
1752 &module.Module.protoProperties,
1753 &module.testHelperLibraryProperties)
1754
Colin Cross9a4abed2019-04-24 13:19:28 -07001755 module.Module.properties.Installable = proptools.BoolPtr(true)
1756 module.Module.dexpreopter.isTest = true
1757
Paul Duffin42df1442019-03-20 12:45:53 +00001758 InitJavaModule(module, android.HostAndDeviceSupported)
1759 return module
1760}
1761
Colin Cross1b16b0e2019-02-12 14:41:32 -08001762// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1763// allow running the test with `atest` or a `TEST_MAPPING` file.
1764//
1765// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1766// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001767func TestHostFactory() android.Module {
1768 module := &Test{}
1769
1770 module.AddProperties(
1771 &module.Module.properties,
1772 &module.Module.protoProperties,
1773 &module.testProperties)
1774
Colin Cross9ae1b922018-06-26 17:59:05 -07001775 module.Module.properties.Installable = proptools.BoolPtr(true)
1776
Colin Cross05638fc2018-04-09 18:40:24 -07001777 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001778 return module
1779}
1780
1781//
Colin Cross2fe66872015-03-30 17:20:39 -07001782// Java Binaries (.jar file plus wrapper script)
1783//
1784
Colin Crossf506d872017-07-19 15:53:04 -07001785type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001786 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001787 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001788
1789 // Name of the class containing main to be inserted into the manifest as Main-Class.
1790 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001791}
1792
Colin Crossf506d872017-07-19 15:53:04 -07001793type Binary struct {
1794 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001795
Colin Crossf506d872017-07-19 15:53:04 -07001796 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001797
Colin Cross6b4a32d2017-12-05 13:42:45 -08001798 isWrapperVariant bool
1799
Colin Crossc3315992017-12-08 19:12:36 -08001800 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001801 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001802}
1803
Alex Light24237172017-10-26 09:46:21 -07001804func (j *Binary) HostToolPath() android.OptionalPath {
1805 return android.OptionalPathForPath(j.binaryFile)
1806}
1807
Colin Crossf506d872017-07-19 15:53:04 -07001808func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001809 if ctx.Arch().ArchType == android.Common {
1810 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001811 if j.binaryProperties.Main_class != nil {
1812 if j.properties.Manifest != nil {
1813 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1814 }
1815 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1816 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1817 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1818 }
1819
Colin Cross6b4a32d2017-12-05 13:42:45 -08001820 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001821 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001822 // Handle the binary wrapper
1823 j.isWrapperVariant = true
1824
Colin Cross366938f2017-12-11 16:29:02 -08001825 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001826 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001827 } else {
1828 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1829 }
1830
1831 // Depend on the installed jar so that the wrapper doesn't get executed by
1832 // another build rule before the jar has been installed.
1833 jarFile := ctx.PrimaryModule().(*Binary).installFile
1834
1835 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1836 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001837 }
Colin Cross2fe66872015-03-30 17:20:39 -07001838}
1839
Colin Crossf506d872017-07-19 15:53:04 -07001840func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001841 if ctx.Arch().ArchType == android.Common {
1842 j.deps(ctx)
1843 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001844}
1845
Colin Cross1b16b0e2019-02-12 14:41:32 -08001846// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1847// as well.
1848//
1849// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1850// compiled against the device bootclasspath.
1851//
1852// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1853// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001854func BinaryFactory() android.Module {
1855 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001856
Colin Cross36242852017-06-23 15:06:31 -07001857 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001858 &module.Module.properties,
1859 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001860 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001861 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001862 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001863
Colin Cross9ae1b922018-06-26 17:59:05 -07001864 module.Module.properties.Installable = proptools.BoolPtr(true)
1865
Colin Cross6b4a32d2017-12-05 13:42:45 -08001866 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1867 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001868 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001869}
1870
Colin Cross1b16b0e2019-02-12 14:41:32 -08001871// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1872//
1873// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1874// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001875func BinaryHostFactory() android.Module {
1876 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001877
Colin Cross36242852017-06-23 15:06:31 -07001878 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001879 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001880 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001881 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001882
Colin Cross9ae1b922018-06-26 17:59:05 -07001883 module.Module.properties.Installable = proptools.BoolPtr(true)
1884
Colin Cross6b4a32d2017-12-05 13:42:45 -08001885 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1886 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001887 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001888}
1889
1890//
1891// Java prebuilts
1892//
1893
Colin Cross74d73e22017-08-02 11:05:49 -07001894type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001895 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001896
Nan Zhangea568a42017-11-08 21:20:04 -08001897 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001898
1899 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001900
1901 // List of shared java libs that this module has dependencies to
1902 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001903
1904 // List of files to remove from the jar file(s)
1905 Exclude_files []string
1906
1907 // List of directories to remove from the jar file(s)
1908 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001909
1910 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001911 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001912}
1913
1914type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001915 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001916 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001917 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001918 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001919
Colin Cross74d73e22017-08-02 11:05:49 -07001920 properties ImportProperties
1921
Colin Cross0a6e0072017-08-30 14:24:55 -07001922 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001923 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001924}
1925
Colin Cross83bb3162018-06-25 15:48:06 -07001926func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001927 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001928}
1929
1930func (j *Import) minSdkVersion() string {
1931 return j.sdkVersion()
1932}
1933
Colin Cross74d73e22017-08-02 11:05:49 -07001934func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001935 return &j.prebuilt
1936}
1937
Colin Cross74d73e22017-08-02 11:05:49 -07001938func (j *Import) PrebuiltSrcs() []string {
1939 return j.properties.Jars
1940}
1941
1942func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001943 return j.prebuilt.Name(j.ModuleBase.Name())
1944}
1945
Colin Cross74d73e22017-08-02 11:05:49 -07001946func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001947 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001948}
1949
Colin Cross74d73e22017-08-02 11:05:49 -07001950func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001951 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001952
Nan Zhang4c819fb2018-08-27 18:31:46 -07001953 jarName := ctx.ModuleName() + ".jar"
1954 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001955 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1956 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001957 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001958 inputFile := outputFile
1959 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1960 TransformJetifier(ctx, outputFile, inputFile)
1961 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001962 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001963
1964 ctx.VisitDirectDeps(func(module android.Module) {
1965 otherName := ctx.OtherModuleName(module)
1966 tag := ctx.OtherModuleDependencyTag(module)
1967
1968 switch dep := module.(type) {
1969 case Dependency:
1970 switch tag {
1971 case libTag, staticLibTag:
1972 // sdk lib names from dependencies are re-exported
1973 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1974 }
1975 case SdkLibraryDependency:
1976 switch tag {
1977 case libTag:
1978 // names of sdk libs that are directly depended are exported
1979 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1980 }
1981 }
1982 })
1983
1984 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001985 if Bool(j.properties.Installable) {
1986 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1987 ctx.ModuleName()+".jar", outputFile)
1988 }
Colin Cross2fe66872015-03-30 17:20:39 -07001989}
1990
Colin Cross74d73e22017-08-02 11:05:49 -07001991var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001992
Nan Zhanged19fc32017-10-19 13:06:22 -07001993func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001994 if j.combinedClasspathFile == nil {
1995 return nil
1996 }
Colin Cross37f6d792018-07-12 12:28:41 -07001997 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001998}
1999
2000func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002001 if j.combinedClasspathFile == nil {
2002 return nil
2003 }
Colin Cross37f6d792018-07-12 12:28:41 -07002004 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002005}
2006
Colin Cross331a1212018-08-15 20:40:52 -07002007func (j *Import) ResourceJars() android.Paths {
2008 return nil
2009}
2010
2011func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002012 if j.combinedClasspathFile == nil {
2013 return nil
2014 }
Colin Cross331a1212018-08-15 20:40:52 -07002015 return android.Paths{j.combinedClasspathFile}
2016}
2017
Colin Crossf24a22a2019-01-31 14:12:44 -08002018func (j *Import) DexJar() android.Path {
2019 return nil
2020}
2021
Colin Cross74d73e22017-08-02 11:05:49 -07002022func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002023 return nil
2024}
2025
Jiyong Park1be96912018-05-28 18:02:19 +09002026func (j *Import) ExportedSdkLibs() []string {
2027 return j.exportedSdkLibs
2028}
2029
Colin Cross0c4ce212019-05-03 15:28:19 -07002030func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2031 return nil, nil
2032}
2033
albaltai36ff7dc2018-12-25 14:35:23 +08002034// Add compile time check for interface implementation
2035var _ android.IDEInfo = (*Import)(nil)
2036var _ android.IDECustomizedModuleName = (*Import)(nil)
2037
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002038// Collect information for opening IDE project files in java/jdeps.go.
2039const (
2040 removedPrefix = "prebuilt_"
2041)
2042
2043func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2044 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2045}
2046
2047func (j *Import) IDECustomizedModuleName() string {
2048 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2049 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2050 // solution to get the Import name.
2051 name := j.Name()
2052 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002053 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002054 }
2055 return name
2056}
2057
Colin Cross74d73e22017-08-02 11:05:49 -07002058var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002059
Colin Cross1b16b0e2019-02-12 14:41:32 -08002060// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2061//
2062// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2063// compiled against an Android classpath.
2064//
2065// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2066// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002067func ImportFactory() android.Module {
2068 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002069
Colin Cross74d73e22017-08-02 11:05:49 -07002070 module.AddProperties(&module.properties)
2071
2072 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002073 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002074 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002075 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002076}
2077
Colin Cross1b16b0e2019-02-12 14:41:32 -08002078// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2079// module.
2080//
2081// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2082// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002083func ImportFactoryHost() android.Module {
2084 module := &Import{}
2085
2086 module.AddProperties(&module.properties)
2087
2088 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002089 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002090 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002091 return module
2092}
2093
Colin Cross42be7612019-02-21 18:12:14 -08002094// dex_import module
2095
2096type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002097 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002098}
2099
2100type DexImport struct {
2101 android.ModuleBase
2102 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002103 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002104 prebuilt android.Prebuilt
2105
2106 properties DexImportProperties
2107
2108 dexJarFile android.Path
2109 maybeStrippedDexJarFile android.Path
2110
2111 dexpreopter
2112}
2113
2114func (j *DexImport) Prebuilt() *android.Prebuilt {
2115 return &j.prebuilt
2116}
2117
2118func (j *DexImport) PrebuiltSrcs() []string {
2119 return j.properties.Jars
2120}
2121
2122func (j *DexImport) Name() string {
2123 return j.prebuilt.Name(j.ModuleBase.Name())
2124}
2125
Colin Cross42be7612019-02-21 18:12:14 -08002126func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2127 if len(j.properties.Jars) != 1 {
2128 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2129 }
2130
2131 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2132 j.dexpreopter.isInstallable = true
2133 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2134
2135 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2136 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2137
2138 if j.dexpreopter.uncompressedDex {
2139 rule := android.NewRuleBuilder()
2140
2141 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2142 rule.Temporary(temporary)
2143
2144 // use zip2zip to uncompress classes*.dex files
2145 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002146 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002147 FlagWithInput("-i ", inputJar).
2148 FlagWithOutput("-o ", temporary).
2149 FlagWithArg("-0 ", "'classes*.dex'")
2150
2151 // use zipalign to align uncompressed classes*.dex files
2152 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002153 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002154 Flag("-f").
2155 Text("4").
2156 Input(temporary).
2157 Output(dexOutputFile)
2158
2159 rule.DeleteTemporaryFiles()
2160
2161 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2162 } else {
2163 ctx.Build(pctx, android.BuildParams{
2164 Rule: android.Cp,
2165 Input: inputJar,
2166 Output: dexOutputFile,
2167 })
2168 }
2169
2170 j.dexJarFile = dexOutputFile
2171
2172 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2173
2174 j.maybeStrippedDexJarFile = dexOutputFile
2175
2176 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2177 ctx.ModuleName()+".jar", dexOutputFile)
2178}
2179
2180func (j *DexImport) DexJar() android.Path {
2181 return j.dexJarFile
2182}
2183
2184// dex_import imports a `.jar` file containing classes.dex files.
2185//
2186// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2187// to the device.
2188func DexImportFactory() android.Module {
2189 module := &DexImport{}
2190
2191 module.AddProperties(&module.properties)
2192
2193 android.InitPrebuiltModule(module, &module.properties.Jars)
2194 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002195 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002196 return module
2197}
2198
Colin Cross89536d42017-07-07 14:35:50 -07002199//
2200// Defaults
2201//
2202type Defaults struct {
2203 android.ModuleBase
2204 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002205 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002206}
2207
Colin Cross1b16b0e2019-02-12 14:41:32 -08002208// java_defaults provides a set of properties that can be inherited by other java or android modules.
2209//
2210// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2211// property in the defaults module that exists in the depending module will be prepended to the depending module's
2212// value for that property.
2213//
2214// Example:
2215//
2216// java_defaults {
2217// name: "example_defaults",
2218// srcs: ["common/**/*.java"],
2219// javacflags: ["-Xlint:all"],
2220// aaptflags: ["--auto-add-overlay"],
2221// }
2222//
2223// java_library {
2224// name: "example",
2225// defaults: ["example_defaults"],
2226// srcs: ["example/**/*.java"],
2227// }
2228//
2229// is functionally identical to:
2230//
2231// java_library {
2232// name: "example",
2233// srcs: [
2234// "common/**/*.java",
2235// "example/**/*.java",
2236// ],
2237// javacflags: ["-Xlint:all"],
2238// }
Colin Cross89536d42017-07-07 14:35:50 -07002239func defaultsFactory() android.Module {
2240 return DefaultsFactory()
2241}
2242
2243func DefaultsFactory(props ...interface{}) android.Module {
2244 module := &Defaults{}
2245
2246 module.AddProperties(props...)
2247 module.AddProperties(
2248 &CompilerProperties{},
2249 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002250 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002251 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002252 &aaptProperties{},
2253 &androidLibraryProperties{},
2254 &appProperties{},
2255 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002256 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002257 &ImportProperties{},
2258 &AARImportProperties{},
2259 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002260 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002261 )
2262
2263 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002264 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002265 return module
2266}
Nan Zhangea568a42017-11-08 21:20:04 -08002267
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002268func kytheExtractJavaFactory() android.Singleton {
2269 return &kytheExtractJavaSingleton{}
2270}
2271
2272type kytheExtractJavaSingleton struct {
2273}
2274
2275func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2276 var xrefTargets android.Paths
2277 ctx.VisitAllModules(func(module android.Module) {
2278 if javaModule, ok := module.(xref); ok {
2279 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2280 }
2281 })
2282 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2283 if len(xrefTargets) > 0 {
2284 ctx.Build(pctx, android.BuildParams{
2285 Rule: blueprint.Phony,
2286 Output: android.PathForPhony(ctx, "xref_java"),
2287 Inputs: xrefTargets,
2288 })
2289 }
2290}
2291
Nan Zhangea568a42017-11-08 21:20:04 -08002292var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002293var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002294var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002295var inList = android.InList