blob: f278d5ee0fd79b57549a116671ce06b482f9eeb7 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Sasha Smundak2a4549e2018-11-05 16:49:08 -080053 android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Jeongik Cha538c0d02019-07-11 15:54:27 +090056func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
57 if sc, ok := ctx.Module().(sdkContext); ok {
58 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
59 if usePlatformAPI != (sc.sdkVersion() == "") {
60 if usePlatformAPI {
61 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
62 } else {
63 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
64 }
65 }
66
67 }
68}
69
Colin Cross2fe66872015-03-30 17:20:39 -070070// TODO:
71// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070072// Renderscript
73// Post-jar passes:
74// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070075// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070076// DroidDoc
77// Findbugs
78
Colin Cross89536d42017-07-07 14:35:50 -070079type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070080 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
81 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080082 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070083
84 // list of source files that should not be used to build the Java module.
85 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080086 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070087
88 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070089 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070090
Colin Cross86a63ff2017-09-27 17:33:10 -070091 // list of directories that should be excluded from java_resource_dirs
92 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070093
Colin Cross0f37af02017-09-27 17:42:05 -070094 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080095 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070096
Colin Crosscedd4762018-09-13 11:26:19 -070097 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080098 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070099
Colin Cross7d5136f2015-05-11 13:39:40 -0700100 // list of module-specific flags that will be used for javac compiles
101 Javacflags []string `android:"arch_variant"`
102
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200103 // list of module-specific flags that will be used for kotlinc compiles
104 Kotlincflags []string `android:"arch_variant"`
105
Colin Cross7d5136f2015-05-11 13:39:40 -0700106 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700107 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700108
109 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700110 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700111
112 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800113 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700114
Colin Cross540eff82017-06-22 17:01:52 -0700115 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800116 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700117
118 // If not blank, set the java version passed to javac as -source and -target
119 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700120
Colin Cross9ae1b922018-06-26 17:59:05 -0700121 // If set to true, allow this module to be dexed and installed on devices. Has no
122 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700123 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700124
Colin Cross0f37af02017-09-27 17:42:05 -0700125 // If set to true, include sources used to compile the module in to the final jar
126 Include_srcs *bool
127
Vladimir Marko0975ee02019-04-02 10:29:55 +0100128 // If not empty, classes are restricted to the specified packages and their sub-packages.
129 // This restriction is checked after applying jarjar rules and including static libs.
130 Permitted_packages []string
131
Colin Crossbe9cdb82019-01-21 21:37:16 -0800132 // List of modules to use as annotation processors
133 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700134
Nan Zhang61eaedb2017-11-02 13:28:15 -0700135 // The number of Java source entries each Javac instance can process
136 Javac_shard_size *int64
137
Nan Zhang5f8cb422018-02-06 10:34:32 -0800138 // Add host jdk tools.jar to bootclasspath
139 Use_tools_jar *bool
140
Colin Cross1369cdb2017-09-29 17:58:17 -0700141 Openjdk9 struct {
142 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800143 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700144
145 // List of javac flags that should only be used when passing -source 1.9
146 Javacflags []string
147 }
Colin Crosscb933592017-11-22 13:49:43 -0800148
Colin Cross81440082018-08-15 20:21:55 -0700149 // When compiling language level 9+ .java code in packages that are part of
150 // a system module, patch_module names the module that your sources and
151 // dependencies should be patched into. The Android runtime currently
152 // doesn't implement the JEP 261 module system so this option is only
153 // supported at compile time. It should only be needed to compile tests in
154 // packages that exist in libcore and which are inconvenient to move
155 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100156 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700157
Colin Crosscb933592017-11-22 13:49:43 -0800158 Jacoco struct {
159 // List of classes to include for instrumentation with jacoco to collect coverage
160 // information at runtime when building with coverage enabled. If unset defaults to all
161 // classes.
162 // Supports '*' as the last character of an entry in the list as a wildcard match.
163 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
164 // it matches classes in the package that have the class name as a prefix.
165 Include_filter []string
166
167 // List of classes to exclude from instrumentation with jacoco to collect coverage
168 // information at runtime when building with coverage enabled. Overrides classes selected
169 // by the include_filter property.
170 // Supports '*' as the last character of an entry in the list as a wildcard match.
171 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
172 // it matches classes in the package that have the class name as a prefix.
173 Exclude_filter []string
174 }
175
Andreas Gampef3e5b552018-01-22 21:27:21 -0800176 Errorprone struct {
177 // List of javac flags that should only be used when running errorprone.
178 Javacflags []string
179 }
180
Colin Cross0f2ee152017-12-14 15:22:43 -0800181 Proto struct {
182 // List of extra options that will be passed to the proto generator.
183 Output_params []string
184 }
185
Inseob Kim42882742019-07-30 17:55:33 +0900186 Sysprop struct {
187 Platform *bool
188 } `blueprint:"mutated"`
189
Colin Crosscb933592017-11-22 13:49:43 -0800190 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800191
192 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800193 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700194}
195
Colin Cross89536d42017-07-07 14:35:50 -0700196type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700197 // list of module-specific flags that will be used for dex compiles
198 Dxflags []string `android:"arch_variant"`
199
Jeongik Cha538c0d02019-07-11 15:54:27 +0900200 // if not blank, set to the version of the sdk to compile against.
201 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800202 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700203
Colin Cross83bb3162018-06-25 15:48:06 -0700204 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
205 // Defaults to sdk_version if not set.
206 Min_sdk_version *string
207
Dan Willemsen419290a2018-10-31 15:28:47 -0700208 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
209 // Defaults to sdk_version if not set.
210 Target_sdk_version *string
211
Jeongik Cha356dac42019-08-19 14:09:52 +0900212 // Whether to compile against the platform APIs instead of an SDK.
213 // If true, then sdk_version must be empty. The value of this field
214 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700215 Platform_apis *bool
216
Colin Crossebe1a512017-11-14 13:12:14 -0800217 Aidl struct {
218 // Top level directories to pass to aidl tool
219 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700220
Colin Crossebe1a512017-11-14 13:12:14 -0800221 // Directories rooted at the Android.bp file to pass to aidl tool
222 Local_include_dirs []string
223
224 // directories that should be added as include directories for any aidl sources of modules
225 // that depend on this module, as well as to aidl for this module.
226 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100227
228 // whether to generate traces (for systrace) for this interface
229 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100230
231 // whether to generate Binder#GetTransaction name method.
232 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800233 }
Colin Cross92430102017-10-09 14:59:32 -0700234
235 // If true, export a copy of the module as a -hostdex module for host testing.
236 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700237
Colin Cross7f87f4f2019-04-24 13:41:45 -0700238 Target struct {
239 Hostdex struct {
240 // Additional required dependencies to add to -hostdex modules.
241 Required []string
242 }
243 }
244
David Brazdil17ef5632018-06-27 10:27:45 +0100245 // If set to true, compile dex regardless of installable. Defaults to false.
246 Compile_dex *bool
247
Colin Cross66dbc0b2017-12-28 12:23:20 -0800248 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700249 // If false, disable all optimization. Defaults to true for android_app and android_test
250 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800251 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700252 // True if the module containing this has it set by default.
253 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800254
255 // If true, optimize for size by removing unused code. Defaults to true for apps,
256 // false for libraries and tests.
257 Shrink *bool
258
259 // If true, optimize bytecode. Defaults to false.
260 Optimize *bool
261
262 // If true, obfuscate bytecode. Defaults to false.
263 Obfuscate *bool
264
265 // If true, do not use the flag files generated by aapt that automatically keep
266 // classes referenced by the app manifest. Defaults to false.
267 No_aapt_flags *bool
268
269 // Flags to pass to proguard.
270 Proguard_flags []string
271
272 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800273 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800274 }
275
Colin Cross1369cdb2017-09-29 17:58:17 -0700276 // When targeting 1.9, override the modules to use with --system
277 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700278
279 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800280 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700281}
282
Sasha Smundak2057f822019-04-16 17:16:58 -0700283func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
284 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
285}
286
Colin Cross46c9b8b2017-06-22 16:51:17 -0700287// Module contains the properties and members used by all java module types
288type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700289 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700290 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900291 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700292
Colin Cross89536d42017-07-07 14:35:50 -0700293 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700294 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700295 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700296
Colin Cross331a1212018-08-15 20:40:52 -0700297 // jar file containing header classes including static library dependencies, suitable for
298 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 headerJarFile android.Path
300
Colin Cross331a1212018-08-15 20:40:52 -0700301 // jar file containing implementation classes including static library dependencies but no
302 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700303 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700304
Colin Cross331a1212018-08-15 20:40:52 -0700305 // jar file containing only resources including from static library dependencies
306 resourceJar android.Path
307
Colin Cross0c4ce212019-05-03 15:28:19 -0700308 // args and dependencies to package source files into a srcjar
309 srcJarArgs []string
310 srcJarDeps android.Paths
311
Colin Cross331a1212018-08-15 20:40:52 -0700312 // jar file containing implementation classes and resources including static library
313 // dependencies
314 implementationAndResourcesJar android.Path
315
316 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700317 dexJarFile android.Path
318
Colin Cross43f08db2018-11-12 10:13:39 -0800319 // output file that contains classes.dex if it should be in the output file
320 maybeStrippedDexJarFile android.Path
321
Colin Crosscb933592017-11-22 13:49:43 -0800322 // output file containing uninstrumented classes that will be instrumented by jacoco
323 jacocoReportClassesFile android.Path
324
Colin Cross66dbc0b2017-12-28 12:23:20 -0800325 // output file containing mapping of obfuscated names
326 proguardDictionary android.Path
327
Colin Cross331a1212018-08-15 20:40:52 -0700328 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700329 outputFile android.Path
330 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700331
Colin Cross635c3b02016-05-18 15:37:25 -0700332 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700333
Colin Cross635c3b02016-05-18 15:37:25 -0700334 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700335
Colin Cross2fe66872015-03-30 17:20:39 -0700336 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700337 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800338
339 // list of .java files and srcjars that was passed to javac
340 compiledJavaSrcs android.Paths
341 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800342
343 // list of extra progurad flag files
344 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900345
Colin Cross094054a2018-10-17 15:10:48 -0700346 // manifest file to use instead of properties.Manifest
347 overrideManifest android.OptionalPath
348
Jiyong Park1be96912018-05-28 18:02:19 +0900349 // list of SDK lib names that this java moudule is exporting
350 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700351
352 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
353 // filter out Exclude_srcs, will be used by android.IDEInfo struct
354 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800355
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800356 // expanded Jarjar_rules
357 expandJarjarRules android.Path
358
Vladimir Marko0975ee02019-04-02 10:29:55 +0100359 // list of additional targets for checkbuild
360 additionalCheckedModules android.Paths
361
Colin Cross988708c2019-05-06 14:04:11 -0700362 // Extra files generated by the module type to be added as java resources.
363 extraResources android.Paths
364
Colin Crossf24a22a2019-01-31 14:12:44 -0800365 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800366 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800367
368 // list of the xref extraction files
369 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700370}
371
Colin Cross41955e82019-05-29 14:40:35 -0700372func (j *Module) OutputFiles(tag string) (android.Paths, error) {
373 switch tag {
374 case "":
375 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700376 case ".jar":
377 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700378 case ".proguard_map":
379 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700380 default:
381 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
382 }
Colin Cross54250902017-12-05 09:28:08 -0800383}
384
Jiyong Park8fd61922018-11-08 02:50:25 +0900385func (j *Module) DexJarFile() android.Path {
386 return j.dexJarFile
387}
388
Colin Cross41955e82019-05-29 14:40:35 -0700389var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800390
Colin Crossf506d872017-07-19 15:53:04 -0700391type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700392 HeaderJars() android.Paths
393 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700394 ResourceJars() android.Paths
395 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800396 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700397 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900398 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700399 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700400}
401
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700403 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
404 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405}
406
Nan Zhangb2b33de2018-02-23 11:18:47 -0800407type SrcDependency interface {
408 CompiledSrcs() android.Paths
409 CompiledSrcJars() android.Paths
410}
411
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800412type xref interface {
413 XrefJavaFiles() android.Paths
414}
415
Nan Zhangb2b33de2018-02-23 11:18:47 -0800416func (j *Module) CompiledSrcs() android.Paths {
417 return j.compiledJavaSrcs
418}
419
420func (j *Module) CompiledSrcJars() android.Paths {
421 return j.compiledSrcJars
422}
423
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800424func (j *Module) XrefJavaFiles() android.Paths {
425 return j.kytheFiles
426}
427
Nan Zhangb2b33de2018-02-23 11:18:47 -0800428var _ SrcDependency = (*Module)(nil)
429
Colin Cross89536d42017-07-07 14:35:50 -0700430func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
431 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
432 android.InitDefaultableModule(module)
433}
434
Colin Crossbe1da472017-07-07 15:59:46 -0700435type dependencyTag struct {
436 blueprint.BaseDependencyTag
437 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700438}
439
Colin Crossa4f08812018-10-02 22:03:40 -0700440type jniDependencyTag struct {
441 blueprint.BaseDependencyTag
442 target android.Target
443}
444
Colin Crossbe1da472017-07-07 15:59:46 -0700445var (
Colin Cross4b964c02018-10-15 16:18:06 -0700446 staticLibTag = dependencyTag{name: "staticlib"}
447 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800448 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700449 bootClasspathTag = dependencyTag{name: "bootclasspath"}
450 systemModulesTag = dependencyTag{name: "system modules"}
451 frameworkResTag = dependencyTag{name: "framework-res"}
452 frameworkApkTag = dependencyTag{name: "framework-apk"}
453 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800454 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700455 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
456 certificateTag = dependencyTag{name: "certificate"}
457 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700458 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700459)
Colin Cross2fe66872015-03-30 17:20:39 -0700460
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900461func defaultSdkVersion(ctx checkVendorModuleContext) string {
462 if ctx.SocSpecific() || ctx.DeviceSpecific() {
463 return "system_current"
464 }
465 return ""
466}
467
468type checkVendorModuleContext interface {
469 SocSpecific() bool
470 DeviceSpecific() bool
471}
472
Colin Crossfc3674a2017-09-18 17:41:52 -0700473type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700474 useModule, useFiles, useDefaultLibs, invalidVersion bool
475
Colin Cross86a60ae2018-05-29 14:44:55 -0700476 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700477 systemModules string
478
Colin Crossa97c5d32018-03-28 14:58:31 -0700479 frameworkResModule string
480
Colin Cross86a60ae2018-05-29 14:44:55 -0700481 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700482 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100483
484 noStandardLibs, noFrameworksLibs bool
485}
486
487func (s sdkDep) hasStandardLibs() bool {
488 return !s.noStandardLibs
489}
490
491func (s sdkDep) hasFrameworkLibs() bool {
492 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700493}
494
Colin Crossa4f08812018-10-02 22:03:40 -0700495type jniLib struct {
496 name string
497 path android.Path
498 target android.Target
499}
500
Colin Cross0ea8ba82019-06-06 14:33:29 -0700501func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800502 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
503}
504
Colin Cross0ea8ba82019-06-06 14:33:29 -0700505func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800506 return j.shouldInstrument(ctx) &&
507 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
508 ctx.Config().UnbundledBuild())
509}
510
Colin Cross83bb3162018-06-25 15:48:06 -0700511func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900512 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700513}
514
515func (j *Module) minSdkVersion() string {
516 if j.deviceProperties.Min_sdk_version != nil {
517 return *j.deviceProperties.Min_sdk_version
518 }
519 return j.sdkVersion()
520}
521
Dan Willemsen419290a2018-10-31 15:28:47 -0700522func (j *Module) targetSdkVersion() string {
523 if j.deviceProperties.Target_sdk_version != nil {
524 return *j.deviceProperties.Target_sdk_version
525 }
526 return j.sdkVersion()
527}
528
Colin Crossbe1da472017-07-07 15:59:46 -0700529func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700530 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100531 sdkDep := decodeSdkDep(ctx, sdkContext(j))
532 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700533 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700534 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100535 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100536 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700537 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700538 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700539 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100540 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700541 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700542 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700543 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
544 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800545 }
Colin Crossbe1da472017-07-07 15:59:46 -0700546 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700547 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100548 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100549 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100550 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700551 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700552 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100553 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700554 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800555 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800556 if ctx.ModuleName() == "android_stubs_current" ||
557 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700558 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700559 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800560 }
Colin Cross2fe66872015-03-30 17:20:39 -0700561 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700562
Colin Cross42d48b72018-08-29 14:10:52 -0700563 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
564 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700565
Colin Crossbe9cdb82019-01-21 21:37:16 -0800566 ctx.AddFarVariationDependencies([]blueprint.Variation{
567 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
568 }, pluginTag, j.properties.Plugins...)
569
Colin Crossfe17f6f2019-03-28 19:30:56 -0700570 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700571 if j.hasSrcExt(".proto") {
572 protoDeps(ctx, &j.protoProperties)
573 }
Colin Cross93e85952017-08-15 13:34:18 -0700574
575 if j.hasSrcExt(".kt") {
576 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
577 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700578 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
579 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800580 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800581 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
582 }
Colin Cross93e85952017-08-15 13:34:18 -0700583 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800584
585 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700586 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800587 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700588}
589
590func hasSrcExt(srcs []string, ext string) bool {
591 for _, src := range srcs {
592 if filepath.Ext(src) == ext {
593 return true
594 }
595 }
596
597 return false
598}
599
Nan Zhang61eaedb2017-11-02 13:28:15 -0700600func shardPaths(paths android.Paths, shardSize int) []android.Paths {
601 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
602 for len(paths) > shardSize {
603 ret = append(ret, paths[0:shardSize])
604 paths = paths[shardSize:]
605 }
606 if len(paths) > 0 {
607 ret = append(ret, paths)
608 }
609 return ret
610}
611
Colin Cross6af17aa2017-09-20 12:59:05 -0700612func (j *Module) hasSrcExt(ext string) bool {
613 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700614}
615
Colin Cross46c9b8b2017-06-22 16:51:17 -0700616func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700617 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700618
Colin Crossebe1a512017-11-14 13:12:14 -0800619 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
620 aidlIncludes = append(aidlIncludes,
621 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
622 aidlIncludes = append(aidlIncludes,
623 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700624
Colin Cross3047fa22019-04-18 10:56:44 -0700625 var flags []string
626 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700627
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700628 if aidlPreprocess.Valid() {
629 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700630 deps = append(deps, aidlPreprocess.Path())
631 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700632 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700633 }
634
Colin Cross3047fa22019-04-18 10:56:44 -0700635 if len(j.exportAidlIncludeDirs) > 0 {
636 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
637 }
638
639 if len(aidlIncludes) > 0 {
640 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
641 }
642
Colin Cross635c3b02016-05-18 15:37:25 -0700643 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800644 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700645 flags = append(flags, "-I"+src.String())
646 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700647
Martijn Coeneneab15642018-03-09 09:29:59 +0100648 if Bool(j.deviceProperties.Aidl.Generate_traces) {
649 flags = append(flags, "-t")
650 }
651
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100652 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
653 flags = append(flags, "--transaction_names")
654 }
655
Colin Cross3047fa22019-04-18 10:56:44 -0700656 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700657}
658
Colin Cross32f676a2017-09-06 13:41:06 -0700659type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800660 classpath classpath
661 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700662 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800663 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700664 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700665 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700666 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700667 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800668 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700669 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700670 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700671 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700672 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800673 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800674
675 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700676}
Colin Cross2fe66872015-03-30 17:20:39 -0700677
Colin Cross54250902017-12-05 09:28:08 -0800678func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
679 for _, f := range dep.Srcs() {
680 if f.Ext() != ".jar" {
681 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
682 ctx.OtherModuleName(dep.(blueprint.Module)))
683 }
684 }
685}
686
Jiyong Park2d492942018-03-05 17:44:10 +0900687type linkType int
688
689const (
690 javaCore linkType = iota
691 javaSdk
692 javaSystem
693 javaPlatform
694)
695
Jiyong Park46f78fb2018-10-20 16:33:17 +0900696func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700697 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700698 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900699 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
700 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100701 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900702 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100703 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900704 return javaCore, false
705 case name == "android_system_stubs_current":
706 return javaSystem, true
707 case strings.HasPrefix(ver, "system_"):
708 return javaSystem, false
709 case name == "android_test_stubs_current":
710 return javaSystem, true
711 case strings.HasPrefix(ver, "test_"):
712 return javaPlatform, false
713 case name == "android_stubs_current":
714 return javaSdk, true
715 case ver == "current":
716 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100717 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900718 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700719 default:
720 if _, err := strconv.Atoi(ver); err != nil {
721 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
722 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900723 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900724 }
725}
726
Jiyong Park750e5572018-01-31 00:20:13 +0900727func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700728 if ctx.Host() {
729 return
730 }
731
Jiyong Park46f78fb2018-10-20 16:33:17 +0900732 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
733 if stubs {
734 return
735 }
736 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900737 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."
738
739 switch myLinkType {
740 case javaCore:
741 if otherLinkType != javaCore {
742 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 +0900743 ctx.OtherModuleName(to))
744 }
Jiyong Park2d492942018-03-05 17:44:10 +0900745 break
746 case javaSdk:
747 if otherLinkType != javaCore && otherLinkType != javaSdk {
748 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
749 ctx.OtherModuleName(to))
750 }
751 break
752 case javaSystem:
753 if otherLinkType == javaPlatform {
754 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
755 ctx.OtherModuleName(to))
756 }
757 break
758 case javaPlatform:
759 // no restriction on link-type
760 break
Jiyong Park750e5572018-01-31 00:20:13 +0900761 }
762}
763
Colin Cross32f676a2017-09-06 13:41:06 -0700764func (j *Module) collectDeps(ctx android.ModuleContext) deps {
765 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700766
Colin Cross300f0382018-03-06 13:11:51 -0800767 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700768 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800769 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700770 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800771 } else if sdkDep.useFiles {
772 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700773 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700774 deps.aidlPreprocess = sdkDep.aidl
775 } else {
776 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800777 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700778 }
779
Colin Crossd11fcda2017-10-23 17:59:01 -0700780 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700781 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700782 tag := ctx.OtherModuleDependencyTag(module)
783
Colin Crossa4f08812018-10-02 22:03:40 -0700784 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700785 // Handled by AndroidApp.collectAppDeps
786 return
787 }
788 if tag == certificateTag {
789 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700790 return
791 }
792
Jiyong Park750e5572018-01-31 00:20:13 +0900793 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700794 switch tag {
795 case bootClasspathTag, libTag, staticLibTag:
796 checkLinkType(ctx, j, to, tag.(dependencyTag))
797 }
Jiyong Park750e5572018-01-31 00:20:13 +0900798 }
Colin Cross54250902017-12-05 09:28:08 -0800799 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800800 case SdkLibraryDependency:
801 switch tag {
802 case libTag:
803 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
804 // names of sdk libs that are directly depended are exported
805 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700806 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800807 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
808 }
Colin Cross54250902017-12-05 09:28:08 -0800809 case Dependency:
810 switch tag {
811 case bootClasspathTag:
812 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700813 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800814 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900815 // sdk lib names from dependencies are re-exported
816 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700817 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800818 case staticLibTag:
819 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
820 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
821 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700822 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900823 // sdk lib names from dependencies are re-exported
824 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700825 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800826 case pluginTag:
827 if plugin, ok := dep.(*Plugin); ok {
828 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
829 if plugin.pluginProperties.Processor_class != nil {
830 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
831 }
832 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
833 } else {
834 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
835 }
Colin Cross54250902017-12-05 09:28:08 -0800836 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100837 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800838 // framework.jar has a one-off dependency on the R.java and Manifest.java files
839 // generated by framework-res.apk
840 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
841 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800842 case frameworkApkTag:
843 if ctx.ModuleName() == "android_stubs_current" ||
844 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700845 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800846 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
847 // resource files out of there for aapt.
848 //
849 // Normally the package rule runs aapt, which includes the resource,
850 // but we're not running that in our package rule so just copy in the
851 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700852 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800853 }
Colin Cross54250902017-12-05 09:28:08 -0800854 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700855 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800856 case kotlinAnnotationsTag:
857 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800858 }
859
Colin Cross54250902017-12-05 09:28:08 -0800860 case android.SourceFileProducer:
861 switch tag {
862 case libTag:
863 checkProducesJars(ctx, dep)
864 deps.classpath = append(deps.classpath, dep.Srcs()...)
865 case staticLibTag:
866 checkProducesJars(ctx, dep)
867 deps.classpath = append(deps.classpath, dep.Srcs()...)
868 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
869 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800870 }
871 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700872 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700873 case systemModulesTag:
874 if deps.systemModules != nil {
875 panic("Found two system module dependencies")
876 }
877 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000878 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700879 panic("Missing directory for system module dependency")
880 }
Colin Crossb77043e2019-07-16 13:57:13 -0700881 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700882 }
Colin Crossec7a0422017-07-07 14:47:12 -0700883 }
Colin Cross2fe66872015-03-30 17:20:39 -0700884 })
885
Jiyong Park1be96912018-05-28 18:02:19 +0900886 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
887
Colin Cross32f676a2017-09-06 13:41:06 -0700888 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700889}
890
Colin Cross83bb3162018-06-25 15:48:06 -0700891func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700892 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800893 v := sdkContext.sdkVersion()
894 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100895 if ctx.Config().IsPdkBuild() &&
896 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700897 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800898 latestSdkVersion := 0
899 if len(sdkVersions) > 0 {
900 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
901 }
902 v = strconv.Itoa(latestSdkVersion)
903 }
904
905 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700906 if err != nil {
907 ctx.PropertyErrorf("sdk_version", "%s", err)
908 }
Nan Zhang357466b2018-04-17 17:38:36 -0700909 if javaVersion != "" {
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100910 ret = normalizeJavaVersion(ctx, javaVersion)
Nan Zhang357466b2018-04-17 17:38:36 -0700911 } else if ctx.Device() && sdk <= 23 {
912 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100913 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700914 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100915 } else if ctx.Device() &&
916 sdkContext.sdkVersion() != "" &&
917 sdkContext.sdkVersion() != "none" &&
918 sdkContext.sdkVersion() != "core_platform" &&
919 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700920 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
921 ret = "1.8"
922 } else {
923 ret = "1.9"
924 }
925
926 return ret
927}
928
Pete Gillin4e8b48a2019-07-12 13:16:17 +0100929func normalizeJavaVersion(ctx android.ModuleContext, javaVersion string) string {
930 switch javaVersion {
931 case "1.6", "6":
932 return "1.6"
933 case "1.7", "7":
934 return "1.7"
935 case "1.8", "8":
936 return "1.8"
937 case "1.9", "9":
938 return "1.9"
939 case "10", "11":
940 ctx.PropertyErrorf("java_version", "Java language levels above 9 are not supported")
941 return "unsupported"
942 default:
943 ctx.PropertyErrorf("java_version", "Unrecognized Java language level")
944 return "unrecognized"
945 }
946}
947
Nan Zhanged19fc32017-10-19 13:06:22 -0700948func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700949
Colin Crossf03c82b2015-04-13 13:53:40 -0700950 var flags javaBuilderFlags
951
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100952 // javaVersion flag.
953 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
954
Nan Zhanged19fc32017-10-19 13:06:22 -0700955 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700956 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100957 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700958 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700959 }
Colin Cross6510f912017-11-29 00:27:14 -0800960 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700961 // Override the -g flag passed globally to remove local variable debug info to reduce
962 // disk and memory usage.
963 javacFlags = append(javacFlags, "-g:source,lines")
964 }
Colin Cross64162712017-08-08 13:17:59 -0700965
Colin Cross66548102018-06-19 22:47:35 -0700966 if ctx.Config().RunErrorProne() {
967 if config.ErrorProneClasspath == nil {
968 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
969 }
970
971 errorProneFlags := []string{
972 "-Xplugin:ErrorProne",
973 "${config.ErrorProneChecks}",
974 }
975 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
976
977 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
978 "'" + strings.Join(errorProneFlags, " ") + "'"
979 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800980 }
981
Nan Zhanged19fc32017-10-19 13:06:22 -0700982 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800983 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
984 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700985 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800986
Colin Crossbe9cdb82019-01-21 21:37:16 -0800987 flags.processor = strings.Join(deps.processorClasses, ",")
988
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100989 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100990 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800991 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
992 // Give host-side tools a version of OpenJDK's standard libraries
993 // close to what they're targeting. As of Dec 2017, AOSP is only
994 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
995 //
996 // When building with OpenJDK 8, the following should have no
997 // effect since those jars would be available by default.
998 //
999 // When building with OpenJDK 9 but targeting a version < 1.8,
1000 // putting them on the bootclasspath means that:
1001 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1002 // b) references to existing APIs are not reinterpreted in an
1003 // OpenJDK 9-specific way, eg. calls to subclasses of
1004 // java.nio.Buffer as in http://b/70862583
1005 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1006 flags.bootClasspath = append(flags.bootClasspath,
1007 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1008 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001009 if Bool(j.properties.Use_tools_jar) {
1010 flags.bootClasspath = append(flags.bootClasspath,
1011 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1012 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001013 }
1014
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001015 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001016 // Manually specify build directory in case it is not under the repo root.
1017 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
1018 // just adding a symlink under the root doesn't help.)
1019 patchPaths := ".:" + ctx.Config().BuildDir()
1020 classPath := flags.classpath.FormJavaClassPath("")
1021 if classPath != "" {
1022 patchPaths += ":" + classPath
1023 }
1024 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001025 }
1026
Nan Zhanged19fc32017-10-19 13:06:22 -07001027 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001028 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001029
Nan Zhanged19fc32017-10-19 13:06:22 -07001030 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001031 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001032
Colin Cross81440082018-08-15 20:21:55 -07001033 if len(javacFlags) > 0 {
1034 // optimization.
1035 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1036 flags.javacFlags = "$javacFlags"
1037 }
1038
Nan Zhanged19fc32017-10-19 13:06:22 -07001039 return flags
1040}
Colin Crossc0b06f12015-04-08 13:03:43 -07001041
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001042func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001043 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001044
1045 deps := j.collectDeps(ctx)
1046 flags := j.collectBuilderFlags(ctx, deps)
1047
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001048 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001049 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1050 }
Colin Cross8a497952019-03-05 22:25:09 -08001051 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001052 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001053 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001054 }
1055
Colin Crossaf050172017-11-15 23:01:59 -08001056 srcFiles = j.genSources(ctx, srcFiles, flags)
1057
1058 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001059 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001060 if aaptSrcJar != nil {
1061 srcJars = append(srcJars, aaptSrcJar)
1062 }
Colin Crossb7a63242015-04-16 14:09:14 -07001063
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001064 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1065 // that IDEInfo struct will use
1066 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1067
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001068 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001069 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001070 }
1071
Colin Cross1ee23172017-10-18 14:44:18 -07001072 jarName := ctx.ModuleName() + ".jar"
1073
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001074 javaSrcFiles := srcFiles.FilterByExt(".java")
1075 var uniqueSrcFiles android.Paths
1076 set := make(map[string]bool)
1077 for _, v := range javaSrcFiles {
1078 if _, found := set[v.String()]; !found {
1079 set[v.String()] = true
1080 uniqueSrcFiles = append(uniqueSrcFiles, v)
1081 }
1082 }
1083
Colin Cross55f63ea2018-08-27 12:37:09 -07001084 var kotlinJars android.Paths
1085
Colin Cross93e85952017-08-15 13:34:18 -07001086 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001087 // user defined kotlin flags.
1088 kotlincFlags := j.properties.Kotlincflags
1089 CheckKotlincFlags(ctx, kotlincFlags)
1090
Colin Cross93e85952017-08-15 13:34:18 -07001091 // If there are kotlin files, compile them first but pass all the kotlin and java files
1092 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1093 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001094 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001095 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001096 kotlincFlags = append(kotlincFlags, "-no-jdk")
1097 }
1098 if len(kotlincFlags) > 0 {
1099 // optimization.
1100 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1101 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001102 }
1103
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001104 var kotlinSrcFiles android.Paths
1105 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1106 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1107
Colin Crossafbb1732019-01-17 15:42:52 -08001108 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1109 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1110
1111 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1112 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1113
1114 if len(flags.processorPath) > 0 {
1115 // Use kapt for annotation processing
1116 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1117 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1118 srcJars = append(srcJars, kaptSrcJar)
1119 // Disable annotation processing in javac, it's already been handled by kapt
1120 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001121 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001122 }
Colin Cross93e85952017-08-15 13:34:18 -07001123
Colin Cross1ee23172017-10-18 14:44:18 -07001124 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001125 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001126 if ctx.Failed() {
1127 return
1128 }
1129
1130 // Make javac rule depend on the kotlinc rule
1131 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001132
Colin Cross93e85952017-08-15 13:34:18 -07001133 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001134 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001135 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001136 }
1137
Colin Cross55f63ea2018-08-27 12:37:09 -07001138 jars := append(android.Paths(nil), kotlinJars...)
1139
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001140 // Store the list of .java files that was passed to javac
1141 j.compiledJavaSrcs = uniqueSrcFiles
1142 j.compiledSrcJars = srcJars
1143
Nan Zhang61eaedb2017-11-02 13:28:15 -07001144 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001145 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001146 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1147 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001148 // Formerly, there was a check here that prevented annotation processors
1149 // from being used when sharding was enabled, as some annotation processors
1150 // do not function correctly in sharded environments. It was removed to
1151 // allow for the use of annotation processors that do function correctly
1152 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001153 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001154 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001155 if ctx.Failed() {
1156 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001157 }
1158 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001159 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001160 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001161 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001162 // If error-prone is enabled, add an additional rule to compile the java files into
1163 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001164 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001165 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1166 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001167 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001168 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001169 extraJarDeps = append(extraJarDeps, errorprone)
1170 }
1171
Nan Zhang61eaedb2017-11-02 13:28:15 -07001172 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001173 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001174 shardSize := int(*(j.properties.Javac_shard_size))
1175 var shardSrcs []android.Paths
1176 if len(uniqueSrcFiles) > 0 {
1177 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1178 for idx, shardSrc := range shardSrcs {
1179 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1180 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1181 jars = append(jars, classes)
1182 }
1183 }
1184 if len(srcJars) > 0 {
1185 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1186 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1187 jars = append(jars, classes)
1188 }
1189 } else {
1190 classes := android.PathForModuleOut(ctx, "javac", jarName)
1191 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1192 jars = append(jars, classes)
1193 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001194 if ctx.Config().EmitXrefRules() {
1195 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1196 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1197 j.kytheFiles = append(j.kytheFiles, extractionFile)
1198
1199 }
Colin Crossd6891432017-09-27 17:39:56 -07001200 if ctx.Failed() {
1201 return
1202 }
Colin Cross2fe66872015-03-30 17:20:39 -07001203 }
1204
Colin Cross0c4ce212019-05-03 15:28:19 -07001205 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1206
1207 var includeSrcJar android.WritablePath
1208 if Bool(j.properties.Include_srcs) {
1209 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1210 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1211 }
1212
Colin Crosscedd4762018-09-13 11:26:19 -07001213 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1214 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001215 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001216 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001217
1218 var resArgs []string
1219 var resDeps android.Paths
1220
1221 resArgs = append(resArgs, dirArgs...)
1222 resDeps = append(resDeps, dirDeps...)
1223
1224 resArgs = append(resArgs, fileArgs...)
1225 resDeps = append(resDeps, fileDeps...)
1226
Colin Cross988708c2019-05-06 14:04:11 -07001227 resArgs = append(resArgs, extraArgs...)
1228 resDeps = append(resDeps, extraDeps...)
1229
Colin Cross40a36712017-09-27 17:41:35 -07001230 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001231 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001232 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001233 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001234 if ctx.Failed() {
1235 return
1236 }
1237 }
1238
Colin Cross0c4ce212019-05-03 15:28:19 -07001239 var resourceJars android.Paths
1240 if j.resourceJar != nil {
1241 resourceJars = append(resourceJars, j.resourceJar)
1242 }
1243 if Bool(j.properties.Include_srcs) {
1244 resourceJars = append(resourceJars, includeSrcJar)
1245 }
1246 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001247
Colin Cross0c4ce212019-05-03 15:28:19 -07001248 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001249 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001250 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001251 false, nil, nil)
1252 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001253 } else if len(resourceJars) == 1 {
1254 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001255 }
1256
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001257 if len(deps.staticJars) > 0 {
1258 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001259 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001260
Colin Cross094054a2018-10-17 15:10:48 -07001261 manifest := j.overrideManifest
1262 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001263 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001264 }
Colin Cross635acc92017-09-12 22:50:46 -07001265
Colin Cross8a497952019-03-05 22:25:09 -08001266 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001267 if len(services) > 0 {
1268 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1269 var zipargs []string
1270 for _, file := range services {
1271 serviceFile := file.String()
1272 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1273 }
1274 ctx.Build(pctx, android.BuildParams{
1275 Rule: zip,
1276 Output: servicesJar,
1277 Implicits: services,
1278 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001279 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001280 },
1281 })
1282 jars = append(jars, servicesJar)
1283 }
1284
Colin Cross0a6e0072017-08-30 14:24:55 -07001285 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001286 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001287 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001288
1289 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001290 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1291 // Optimization: skip the combine step if there is nothing to do
1292 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1293 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1294 // any if len(jars) == 1.
1295 outputFile = moduleOutPath
1296 } else {
1297 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1298 ctx.Build(pctx, android.BuildParams{
1299 Rule: android.Cp,
1300 Input: jars[0],
1301 Output: combinedJar,
1302 })
1303 outputFile = combinedJar
1304 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001305 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001306 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001307 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001308 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001309 outputFile = combinedJar
1310 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001311
Colin Cross331a1212018-08-15 20:40:52 -07001312 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001313 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001314 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001315 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001316 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001317 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001318
1319 // jarjar resource jar if necessary
1320 if j.resourceJar != nil {
1321 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001322 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001323 j.resourceJar = resourceJarJarFile
1324 }
1325
Colin Cross0a6e0072017-08-30 14:24:55 -07001326 if ctx.Failed() {
1327 return
1328 }
1329 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001330
1331 // Check package restrictions if necessary.
1332 if len(j.properties.Permitted_packages) > 0 {
1333 // Check packages and copy to package-checked file.
1334 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1335 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1336 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1337
1338 if ctx.Failed() {
1339 return
1340 }
1341 }
1342
Nan Zhanged19fc32017-10-19 13:06:22 -07001343 j.implementationJarFile = outputFile
1344 if j.headerJarFile == nil {
1345 j.headerJarFile = j.implementationJarFile
1346 }
Colin Cross2fe66872015-03-30 17:20:39 -07001347
Colin Cross6510f912017-11-29 00:27:14 -08001348 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001349 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1350 j.properties.Instrument = true
1351 }
1352 }
1353
Colin Cross3144dfc2018-01-03 15:06:47 -08001354 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001355 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1356 }
1357
Colin Cross331a1212018-08-15 20:40:52 -07001358 // merge implementation jar with resources if necessary
1359 implementationAndResourcesJar := outputFile
1360 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001361 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001362 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001363 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001364 false, nil, nil)
1365 implementationAndResourcesJar = combinedJar
1366 }
1367
1368 j.implementationAndResourcesJar = implementationAndResourcesJar
1369
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001370 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001371 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001372 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001373 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001374 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001375 if ctx.Failed() {
1376 return
1377 }
Colin Cross331a1212018-08-15 20:40:52 -07001378
Jiyong Park09cb6292019-07-15 15:29:23 +09001379 // Hidden API CSV generation and dex encoding
1380 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1381 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001382
Colin Cross331a1212018-08-15 20:40:52 -07001383 // merge dex jar with resources if necessary
1384 if j.resourceJar != nil {
1385 jars := android.Paths{dexOutputFile, j.resourceJar}
1386 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1387 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1388 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001389 if j.deviceProperties.UncompressDex {
1390 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1391 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1392 dexOutputFile = combinedAlignedJar
1393 } else {
1394 dexOutputFile = combinedJar
1395 }
Colin Cross331a1212018-08-15 20:40:52 -07001396 }
1397
1398 j.dexJarFile = dexOutputFile
1399
Colin Cross8faf8fc2019-01-16 15:15:52 -08001400 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001401 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1402
1403 j.maybeStrippedDexJarFile = dexOutputFile
1404
Colin Cross3063b782018-08-15 11:19:12 -07001405 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001406
1407 if ctx.Failed() {
1408 return
1409 }
Colin Cross331a1212018-08-15 20:40:52 -07001410 } else {
1411 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001412 }
Colin Cross331a1212018-08-15 20:40:52 -07001413
Colin Crossb7a63242015-04-16 14:09:14 -07001414 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001415
1416 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1417 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001418}
1419
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001420// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1421// since some of these flags may be used internally.
1422func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1423 for _, flag := range flags {
1424 flag = strings.TrimSpace(flag)
1425
1426 if !strings.HasPrefix(flag, "-") {
1427 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1428 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1429 ctx.PropertyErrorf("kotlincflags",
1430 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1431 } else if inList(flag, config.KotlincIllegalFlags) {
1432 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1433 } else if flag == "-include-runtime" {
1434 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1435 } else {
1436 args := strings.Split(flag, " ")
1437 if args[0] == "-kotlin-home" {
1438 ctx.PropertyErrorf("kotlincflags",
1439 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1440 }
1441 }
1442 }
1443}
1444
Colin Cross8eadbf02017-10-24 17:46:00 -07001445func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001446 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001447
1448 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001449 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001450 // Compile java sources into turbine.jar.
1451 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1452 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1453 if ctx.Failed() {
1454 return nil
1455 }
1456 jars = append(jars, turbineJar)
1457 }
1458
Colin Cross55f63ea2018-08-27 12:37:09 -07001459 jars = append(jars, extraJars...)
1460
Nan Zhanged19fc32017-10-19 13:06:22 -07001461 // Combine any static header libraries into classes-header.jar. If there is only
1462 // one input jar this step will be skipped.
1463 var headerJar android.Path
1464 jars = append(jars, deps.staticHeaderJars...)
1465
Colin Cross5c6ecc12017-10-23 18:12:27 -07001466 // we cannot skip the combine step for now if there is only one jar
1467 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1468 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001469 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001470 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001471 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001472
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001473 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001474 // Transform classes.jar into classes-jarjar.jar
1475 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001476 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001477 headerJar = jarjarFile
1478 if ctx.Failed() {
1479 return nil
1480 }
1481 }
1482
1483 return headerJar
1484}
1485
Colin Crosscb933592017-11-22 13:49:43 -08001486func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001487 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001488
Colin Cross7a3139e2017-12-19 13:57:50 -08001489 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001490
Colin Cross84c38822018-01-03 15:59:46 -08001491 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001492 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1493
1494 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1495
1496 j.jacocoReportClassesFile = jacocoReportClassesFile
1497
1498 return instrumentedJar
1499}
1500
albaltai36ff7dc2018-12-25 14:35:23 +08001501var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001502
Nan Zhanged19fc32017-10-19 13:06:22 -07001503func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001504 if j.headerJarFile == nil {
1505 return nil
1506 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001507 return android.Paths{j.headerJarFile}
1508}
1509
1510func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001511 if j.implementationJarFile == nil {
1512 return nil
1513 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001514 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001515}
1516
Colin Crossf24a22a2019-01-31 14:12:44 -08001517func (j *Module) DexJar() android.Path {
1518 return j.dexJarFile
1519}
1520
Colin Cross331a1212018-08-15 20:40:52 -07001521func (j *Module) ResourceJars() android.Paths {
1522 if j.resourceJar == nil {
1523 return nil
1524 }
1525 return android.Paths{j.resourceJar}
1526}
1527
1528func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001529 if j.implementationAndResourcesJar == nil {
1530 return nil
1531 }
Colin Cross331a1212018-08-15 20:40:52 -07001532 return android.Paths{j.implementationAndResourcesJar}
1533}
1534
Colin Cross46c9b8b2017-06-22 16:51:17 -07001535func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001536 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001537 return j.exportAidlIncludeDirs
1538}
1539
Jiyong Park1be96912018-05-28 18:02:19 +09001540func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001541 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001542 return j.exportedSdkLibs
1543}
1544
Colin Cross0c4ce212019-05-03 15:28:19 -07001545func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1546 return j.srcJarArgs, j.srcJarDeps
1547}
1548
Colin Cross46c9b8b2017-06-22 16:51:17 -07001549var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001550
Colin Cross46c9b8b2017-06-22 16:51:17 -07001551func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001552 return j.logtagsSrcs
1553}
1554
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001555// Collect information for opening IDE project files in java/jdeps.go.
1556func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1557 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1558 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001559 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001560 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001561 if j.expandJarjarRules != nil {
1562 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001563 }
1564}
1565
1566func (j *Module) CompilerDeps() []string {
1567 jdeps := []string{}
1568 jdeps = append(jdeps, j.properties.Libs...)
1569 jdeps = append(jdeps, j.properties.Static_libs...)
1570 return jdeps
1571}
1572
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001573func (j *Module) hasCode(ctx android.ModuleContext) bool {
1574 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1575 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1576}
1577
Colin Cross2fe66872015-03-30 17:20:39 -07001578//
1579// Java libraries (.jar file)
1580//
1581
Colin Crossf506d872017-07-19 15:53:04 -07001582type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001583 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001584}
1585
Colin Cross42be7612019-02-21 18:12:14 -08001586func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001587 // Store uncompressed (and do not strip) dex files from boot class path jars.
1588 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1589 return true
1590 }
1591
1592 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001593 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001594 return true
1595 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001596 if ctx.Config().UncompressPrivAppDex() &&
1597 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1598 return true
1599 }
1600
Colin Cross2fc72f62018-12-21 12:59:54 -08001601 return false
1602}
1603
Colin Crossf506d872017-07-19 15:53:04 -07001604func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001605 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1606 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001607 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001608 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001609 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001610 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001611
Jiyong Park7f7766d2019-07-25 22:02:35 +09001612 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1613 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001614 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1615 ctx.ModuleName()+".jar", j.outputFile)
1616 }
Colin Crossb7a63242015-04-16 14:09:14 -07001617}
1618
Colin Crossf506d872017-07-19 15:53:04 -07001619func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001620 j.deps(ctx)
1621}
1622
Colin Cross1b16b0e2019-02-12 14:41:32 -08001623// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1624//
1625// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1626// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1627// as a `static_libs` dependency of another module.
1628//
1629// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1630// a device.
1631//
1632// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1633// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001634func LibraryFactory() android.Module {
1635 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001636
Colin Cross9ae1b922018-06-26 17:59:05 -07001637 module.AddProperties(
1638 &module.Module.properties,
1639 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001640 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001641 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001642
Colin Cross9ae1b922018-06-26 17:59:05 -07001643 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001644 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001645 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001646}
1647
Colin Cross1b16b0e2019-02-12 14:41:32 -08001648// java_library_static is an obsolete alias for java_library.
1649func LibraryStaticFactory() android.Module {
1650 return LibraryFactory()
1651}
1652
1653// java_library_host builds and links sources into a `.jar` file for the host.
1654//
1655// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1656// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001657func LibraryHostFactory() android.Module {
1658 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001659
Colin Cross6af17aa2017-09-20 12:59:05 -07001660 module.AddProperties(
1661 &module.Module.properties,
1662 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001663
Colin Cross9ae1b922018-06-26 17:59:05 -07001664 module.Module.properties.Installable = proptools.BoolPtr(true)
1665
Colin Cross89536d42017-07-07 14:35:50 -07001666 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001667 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001668 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001669}
1670
1671//
Colin Crossb628ea52018-08-14 16:42:33 -07001672// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001673//
1674
1675type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001676 // list of compatibility suites (for example "cts", "vts") that the module should be
1677 // installed into.
1678 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001679
1680 // the name of the test configuration (for example "AndroidTest.xml") that should be
1681 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001682 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001683
Jack He33338892018-09-19 02:21:28 -07001684 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1685 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001686 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001687
Colin Crossd96ca352018-08-10 16:06:24 -07001688 // list of files or filegroup modules that provide data that should be installed alongside
1689 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001690 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001691}
1692
Paul Duffin42df1442019-03-20 12:45:53 +00001693type testHelperLibraryProperties struct {
1694 // list of compatibility suites (for example "cts", "vts") that the module should be
1695 // installed into.
1696 Test_suites []string `android:"arch_variant"`
1697}
1698
Colin Cross05638fc2018-04-09 18:40:24 -07001699type Test struct {
1700 Library
1701
1702 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001703
1704 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001705 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001706}
1707
Paul Duffin42df1442019-03-20 12:45:53 +00001708type TestHelperLibrary struct {
1709 Library
1710
1711 testHelperLibraryProperties testHelperLibraryProperties
1712}
1713
Colin Cross303e21f2018-08-07 16:49:25 -07001714func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001715 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 -08001716 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001717
1718 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001719}
1720
Paul Duffin42df1442019-03-20 12:45:53 +00001721func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1722 j.Library.GenerateAndroidBuildActions(ctx)
1723}
1724
Colin Cross1b16b0e2019-02-12 14:41:32 -08001725// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1726// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1727//
1728// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1729// compiled against the device bootclasspath.
1730//
1731// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1732// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001733func TestFactory() android.Module {
1734 module := &Test{}
1735
1736 module.AddProperties(
1737 &module.Module.properties,
1738 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001739 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001740 &module.Module.protoProperties,
1741 &module.testProperties)
1742
Colin Cross9ae1b922018-06-26 17:59:05 -07001743 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001744 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001745
Colin Cross05638fc2018-04-09 18:40:24 -07001746 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001747 return module
1748}
1749
Paul Duffin42df1442019-03-20 12:45:53 +00001750// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1751func TestHelperLibraryFactory() android.Module {
1752 module := &TestHelperLibrary{}
1753
1754 module.AddProperties(
1755 &module.Module.properties,
1756 &module.Module.deviceProperties,
1757 &module.Module.dexpreoptProperties,
1758 &module.Module.protoProperties,
1759 &module.testHelperLibraryProperties)
1760
Colin Cross9a4abed2019-04-24 13:19:28 -07001761 module.Module.properties.Installable = proptools.BoolPtr(true)
1762 module.Module.dexpreopter.isTest = true
1763
Paul Duffin42df1442019-03-20 12:45:53 +00001764 InitJavaModule(module, android.HostAndDeviceSupported)
1765 return module
1766}
1767
Colin Cross1b16b0e2019-02-12 14:41:32 -08001768// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1769// allow running the test with `atest` or a `TEST_MAPPING` file.
1770//
1771// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1772// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001773func TestHostFactory() android.Module {
1774 module := &Test{}
1775
1776 module.AddProperties(
1777 &module.Module.properties,
1778 &module.Module.protoProperties,
1779 &module.testProperties)
1780
Colin Cross9ae1b922018-06-26 17:59:05 -07001781 module.Module.properties.Installable = proptools.BoolPtr(true)
1782
Colin Cross05638fc2018-04-09 18:40:24 -07001783 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001784 return module
1785}
1786
1787//
Colin Cross2fe66872015-03-30 17:20:39 -07001788// Java Binaries (.jar file plus wrapper script)
1789//
1790
Colin Crossf506d872017-07-19 15:53:04 -07001791type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001792 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001793 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001794
1795 // Name of the class containing main to be inserted into the manifest as Main-Class.
1796 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001797}
1798
Colin Crossf506d872017-07-19 15:53:04 -07001799type Binary struct {
1800 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001801
Colin Crossf506d872017-07-19 15:53:04 -07001802 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001803
Colin Cross6b4a32d2017-12-05 13:42:45 -08001804 isWrapperVariant bool
1805
Colin Crossc3315992017-12-08 19:12:36 -08001806 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001807 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001808}
1809
Alex Light24237172017-10-26 09:46:21 -07001810func (j *Binary) HostToolPath() android.OptionalPath {
1811 return android.OptionalPathForPath(j.binaryFile)
1812}
1813
Colin Crossf506d872017-07-19 15:53:04 -07001814func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001815 if ctx.Arch().ArchType == android.Common {
1816 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001817 if j.binaryProperties.Main_class != nil {
1818 if j.properties.Manifest != nil {
1819 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1820 }
1821 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1822 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1823 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1824 }
1825
Colin Cross6b4a32d2017-12-05 13:42:45 -08001826 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001827 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001828 // Handle the binary wrapper
1829 j.isWrapperVariant = true
1830
Colin Cross366938f2017-12-11 16:29:02 -08001831 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001832 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001833 } else {
1834 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1835 }
1836
1837 // Depend on the installed jar so that the wrapper doesn't get executed by
1838 // another build rule before the jar has been installed.
1839 jarFile := ctx.PrimaryModule().(*Binary).installFile
1840
1841 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1842 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001843 }
Colin Cross2fe66872015-03-30 17:20:39 -07001844}
1845
Colin Crossf506d872017-07-19 15:53:04 -07001846func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001847 if ctx.Arch().ArchType == android.Common {
1848 j.deps(ctx)
1849 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001850}
1851
Colin Cross1b16b0e2019-02-12 14:41:32 -08001852// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1853// as well.
1854//
1855// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1856// compiled against the device bootclasspath.
1857//
1858// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1859// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001860func BinaryFactory() android.Module {
1861 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001862
Colin Cross36242852017-06-23 15:06:31 -07001863 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001864 &module.Module.properties,
1865 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001866 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001867 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001868 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001869
Colin Cross9ae1b922018-06-26 17:59:05 -07001870 module.Module.properties.Installable = proptools.BoolPtr(true)
1871
Colin Cross6b4a32d2017-12-05 13:42:45 -08001872 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1873 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001874 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001875}
1876
Colin Cross1b16b0e2019-02-12 14:41:32 -08001877// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1878//
1879// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1880// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001881func BinaryHostFactory() android.Module {
1882 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001883
Colin Cross36242852017-06-23 15:06:31 -07001884 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001885 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001886 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001887 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001888
Colin Cross9ae1b922018-06-26 17:59:05 -07001889 module.Module.properties.Installable = proptools.BoolPtr(true)
1890
Colin Cross6b4a32d2017-12-05 13:42:45 -08001891 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1892 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001893 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001894}
1895
1896//
1897// Java prebuilts
1898//
1899
Colin Cross74d73e22017-08-02 11:05:49 -07001900type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001901 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001902
Nan Zhangea568a42017-11-08 21:20:04 -08001903 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001904
1905 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001906
1907 // List of shared java libs that this module has dependencies to
1908 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001909
1910 // List of files to remove from the jar file(s)
1911 Exclude_files []string
1912
1913 // List of directories to remove from the jar file(s)
1914 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001915
1916 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001917 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001918}
1919
1920type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001921 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001922 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001923 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001924 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001925
Colin Cross74d73e22017-08-02 11:05:49 -07001926 properties ImportProperties
1927
Colin Cross0a6e0072017-08-30 14:24:55 -07001928 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001929 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001930}
1931
Colin Cross83bb3162018-06-25 15:48:06 -07001932func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001933 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001934}
1935
1936func (j *Import) minSdkVersion() string {
1937 return j.sdkVersion()
1938}
1939
Colin Cross74d73e22017-08-02 11:05:49 -07001940func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001941 return &j.prebuilt
1942}
1943
Colin Cross74d73e22017-08-02 11:05:49 -07001944func (j *Import) PrebuiltSrcs() []string {
1945 return j.properties.Jars
1946}
1947
1948func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001949 return j.prebuilt.Name(j.ModuleBase.Name())
1950}
1951
Colin Cross74d73e22017-08-02 11:05:49 -07001952func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001953 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001954}
1955
Colin Cross74d73e22017-08-02 11:05:49 -07001956func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001957 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001958
Nan Zhang4c819fb2018-08-27 18:31:46 -07001959 jarName := ctx.ModuleName() + ".jar"
1960 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001961 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1962 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001963 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001964 inputFile := outputFile
1965 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1966 TransformJetifier(ctx, outputFile, inputFile)
1967 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001968 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001969
1970 ctx.VisitDirectDeps(func(module android.Module) {
1971 otherName := ctx.OtherModuleName(module)
1972 tag := ctx.OtherModuleDependencyTag(module)
1973
1974 switch dep := module.(type) {
1975 case Dependency:
1976 switch tag {
1977 case libTag, staticLibTag:
1978 // sdk lib names from dependencies are re-exported
1979 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1980 }
1981 case SdkLibraryDependency:
1982 switch tag {
1983 case libTag:
1984 // names of sdk libs that are directly depended are exported
1985 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1986 }
1987 }
1988 })
1989
1990 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001991 if Bool(j.properties.Installable) {
1992 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1993 ctx.ModuleName()+".jar", outputFile)
1994 }
Colin Cross2fe66872015-03-30 17:20:39 -07001995}
1996
Colin Cross74d73e22017-08-02 11:05:49 -07001997var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001998
Nan Zhanged19fc32017-10-19 13:06:22 -07001999func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002000 if j.combinedClasspathFile == nil {
2001 return nil
2002 }
Colin Cross37f6d792018-07-12 12:28:41 -07002003 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07002004}
2005
2006func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08002007 if j.combinedClasspathFile == nil {
2008 return nil
2009 }
Colin Cross37f6d792018-07-12 12:28:41 -07002010 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07002011}
2012
Colin Cross331a1212018-08-15 20:40:52 -07002013func (j *Import) ResourceJars() android.Paths {
2014 return nil
2015}
2016
2017func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08002018 if j.combinedClasspathFile == nil {
2019 return nil
2020 }
Colin Cross331a1212018-08-15 20:40:52 -07002021 return android.Paths{j.combinedClasspathFile}
2022}
2023
Colin Crossf24a22a2019-01-31 14:12:44 -08002024func (j *Import) DexJar() android.Path {
2025 return nil
2026}
2027
Colin Cross74d73e22017-08-02 11:05:49 -07002028func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002029 return nil
2030}
2031
Jiyong Park1be96912018-05-28 18:02:19 +09002032func (j *Import) ExportedSdkLibs() []string {
2033 return j.exportedSdkLibs
2034}
2035
Colin Cross0c4ce212019-05-03 15:28:19 -07002036func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2037 return nil, nil
2038}
2039
albaltai36ff7dc2018-12-25 14:35:23 +08002040// Add compile time check for interface implementation
2041var _ android.IDEInfo = (*Import)(nil)
2042var _ android.IDECustomizedModuleName = (*Import)(nil)
2043
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002044// Collect information for opening IDE project files in java/jdeps.go.
2045const (
2046 removedPrefix = "prebuilt_"
2047)
2048
2049func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2050 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2051}
2052
2053func (j *Import) IDECustomizedModuleName() string {
2054 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2055 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2056 // solution to get the Import name.
2057 name := j.Name()
2058 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002059 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002060 }
2061 return name
2062}
2063
Colin Cross74d73e22017-08-02 11:05:49 -07002064var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002065
Colin Cross1b16b0e2019-02-12 14:41:32 -08002066// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2067//
2068// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2069// compiled against an Android classpath.
2070//
2071// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2072// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002073func ImportFactory() android.Module {
2074 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002075
Colin Cross74d73e22017-08-02 11:05:49 -07002076 module.AddProperties(&module.properties)
2077
2078 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002079 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002080 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002081 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002082}
2083
Colin Cross1b16b0e2019-02-12 14:41:32 -08002084// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2085// module.
2086//
2087// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2088// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002089func ImportFactoryHost() android.Module {
2090 module := &Import{}
2091
2092 module.AddProperties(&module.properties)
2093
2094 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002095 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002096 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002097 return module
2098}
2099
Colin Cross42be7612019-02-21 18:12:14 -08002100// dex_import module
2101
2102type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002103 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002104}
2105
2106type DexImport struct {
2107 android.ModuleBase
2108 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002109 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002110 prebuilt android.Prebuilt
2111
2112 properties DexImportProperties
2113
2114 dexJarFile android.Path
2115 maybeStrippedDexJarFile android.Path
2116
2117 dexpreopter
2118}
2119
2120func (j *DexImport) Prebuilt() *android.Prebuilt {
2121 return &j.prebuilt
2122}
2123
2124func (j *DexImport) PrebuiltSrcs() []string {
2125 return j.properties.Jars
2126}
2127
2128func (j *DexImport) Name() string {
2129 return j.prebuilt.Name(j.ModuleBase.Name())
2130}
2131
Colin Cross42be7612019-02-21 18:12:14 -08002132func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2133 if len(j.properties.Jars) != 1 {
2134 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2135 }
2136
2137 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2138 j.dexpreopter.isInstallable = true
2139 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2140
2141 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2142 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2143
2144 if j.dexpreopter.uncompressedDex {
2145 rule := android.NewRuleBuilder()
2146
2147 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2148 rule.Temporary(temporary)
2149
2150 // use zip2zip to uncompress classes*.dex files
2151 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002152 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002153 FlagWithInput("-i ", inputJar).
2154 FlagWithOutput("-o ", temporary).
2155 FlagWithArg("-0 ", "'classes*.dex'")
2156
2157 // use zipalign to align uncompressed classes*.dex files
2158 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002159 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002160 Flag("-f").
2161 Text("4").
2162 Input(temporary).
2163 Output(dexOutputFile)
2164
2165 rule.DeleteTemporaryFiles()
2166
2167 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2168 } else {
2169 ctx.Build(pctx, android.BuildParams{
2170 Rule: android.Cp,
2171 Input: inputJar,
2172 Output: dexOutputFile,
2173 })
2174 }
2175
2176 j.dexJarFile = dexOutputFile
2177
2178 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2179
2180 j.maybeStrippedDexJarFile = dexOutputFile
2181
2182 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2183 ctx.ModuleName()+".jar", dexOutputFile)
2184}
2185
2186func (j *DexImport) DexJar() android.Path {
2187 return j.dexJarFile
2188}
2189
2190// dex_import imports a `.jar` file containing classes.dex files.
2191//
2192// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2193// to the device.
2194func DexImportFactory() android.Module {
2195 module := &DexImport{}
2196
2197 module.AddProperties(&module.properties)
2198
2199 android.InitPrebuiltModule(module, &module.properties.Jars)
2200 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002201 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002202 return module
2203}
2204
Colin Cross89536d42017-07-07 14:35:50 -07002205//
2206// Defaults
2207//
2208type Defaults struct {
2209 android.ModuleBase
2210 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002211 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002212}
2213
Colin Cross1b16b0e2019-02-12 14:41:32 -08002214// java_defaults provides a set of properties that can be inherited by other java or android modules.
2215//
2216// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2217// property in the defaults module that exists in the depending module will be prepended to the depending module's
2218// value for that property.
2219//
2220// Example:
2221//
2222// java_defaults {
2223// name: "example_defaults",
2224// srcs: ["common/**/*.java"],
2225// javacflags: ["-Xlint:all"],
2226// aaptflags: ["--auto-add-overlay"],
2227// }
2228//
2229// java_library {
2230// name: "example",
2231// defaults: ["example_defaults"],
2232// srcs: ["example/**/*.java"],
2233// }
2234//
2235// is functionally identical to:
2236//
2237// java_library {
2238// name: "example",
2239// srcs: [
2240// "common/**/*.java",
2241// "example/**/*.java",
2242// ],
2243// javacflags: ["-Xlint:all"],
2244// }
Colin Cross89536d42017-07-07 14:35:50 -07002245func defaultsFactory() android.Module {
2246 return DefaultsFactory()
2247}
2248
2249func DefaultsFactory(props ...interface{}) android.Module {
2250 module := &Defaults{}
2251
2252 module.AddProperties(props...)
2253 module.AddProperties(
2254 &CompilerProperties{},
2255 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002256 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002257 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002258 &aaptProperties{},
2259 &androidLibraryProperties{},
2260 &appProperties{},
2261 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002262 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002263 &ImportProperties{},
2264 &AARImportProperties{},
2265 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002266 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002267 )
2268
2269 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002270 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002271 return module
2272}
Nan Zhangea568a42017-11-08 21:20:04 -08002273
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002274func kytheExtractJavaFactory() android.Singleton {
2275 return &kytheExtractJavaSingleton{}
2276}
2277
2278type kytheExtractJavaSingleton struct {
2279}
2280
2281func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2282 var xrefTargets android.Paths
2283 ctx.VisitAllModules(func(module android.Module) {
2284 if javaModule, ok := module.(xref); ok {
2285 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2286 }
2287 })
2288 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2289 if len(xrefTargets) > 0 {
2290 ctx.Build(pctx, android.BuildParams{
2291 Rule: blueprint.Phony,
2292 Output: android.PathForPhony(ctx, "xref_java"),
2293 Inputs: xrefTargets,
2294 })
2295 }
2296}
2297
Nan Zhangea568a42017-11-08 21:20:04 -08002298var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002299var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002300var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002301var inList = android.InList