blob: 494a4e7c0f6c76c1d32a6120712997266a134d79 [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 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800553 if ctx.ModuleName() == "android_stubs_current" ||
554 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700555 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700556 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800557 }
Colin Cross2fe66872015-03-30 17:20:39 -0700558 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700559
Colin Cross42d48b72018-08-29 14:10:52 -0700560 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
561 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700562
Colin Crossbe9cdb82019-01-21 21:37:16 -0800563 ctx.AddFarVariationDependencies([]blueprint.Variation{
564 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
565 }, pluginTag, j.properties.Plugins...)
566
Colin Crossfe17f6f2019-03-28 19:30:56 -0700567 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700568 if j.hasSrcExt(".proto") {
569 protoDeps(ctx, &j.protoProperties)
570 }
Colin Cross93e85952017-08-15 13:34:18 -0700571
572 if j.hasSrcExt(".kt") {
573 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
574 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700575 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
576 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800577 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800578 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
579 }
Colin Cross93e85952017-08-15 13:34:18 -0700580 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800581
582 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700583 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800584 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700585}
586
587func hasSrcExt(srcs []string, ext string) bool {
588 for _, src := range srcs {
589 if filepath.Ext(src) == ext {
590 return true
591 }
592 }
593
594 return false
595}
596
Nan Zhang61eaedb2017-11-02 13:28:15 -0700597func shardPaths(paths android.Paths, shardSize int) []android.Paths {
598 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
599 for len(paths) > shardSize {
600 ret = append(ret, paths[0:shardSize])
601 paths = paths[shardSize:]
602 }
603 if len(paths) > 0 {
604 ret = append(ret, paths)
605 }
606 return ret
607}
608
Colin Cross6af17aa2017-09-20 12:59:05 -0700609func (j *Module) hasSrcExt(ext string) bool {
610 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700611}
612
Colin Cross46c9b8b2017-06-22 16:51:17 -0700613func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700614 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700615
Colin Crossebe1a512017-11-14 13:12:14 -0800616 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
617 aidlIncludes = append(aidlIncludes,
618 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
619 aidlIncludes = append(aidlIncludes,
620 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700621
Colin Cross3047fa22019-04-18 10:56:44 -0700622 var flags []string
623 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700624
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700625 if aidlPreprocess.Valid() {
626 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700627 deps = append(deps, aidlPreprocess.Path())
628 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700629 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700630 }
631
Colin Cross3047fa22019-04-18 10:56:44 -0700632 if len(j.exportAidlIncludeDirs) > 0 {
633 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
634 }
635
636 if len(aidlIncludes) > 0 {
637 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
638 }
639
Colin Cross635c3b02016-05-18 15:37:25 -0700640 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800641 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700642 flags = append(flags, "-I"+src.String())
643 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700644
Martijn Coeneneab15642018-03-09 09:29:59 +0100645 if Bool(j.deviceProperties.Aidl.Generate_traces) {
646 flags = append(flags, "-t")
647 }
648
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100649 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
650 flags = append(flags, "--transaction_names")
651 }
652
Colin Cross3047fa22019-04-18 10:56:44 -0700653 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700654}
655
Colin Cross32f676a2017-09-06 13:41:06 -0700656type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800657 classpath classpath
658 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700659 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800660 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700661 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700662 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700663 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700664 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800665 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700666 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700667 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700668 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700669 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800670 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800671
672 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700673}
Colin Cross2fe66872015-03-30 17:20:39 -0700674
Colin Cross54250902017-12-05 09:28:08 -0800675func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
676 for _, f := range dep.Srcs() {
677 if f.Ext() != ".jar" {
678 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
679 ctx.OtherModuleName(dep.(blueprint.Module)))
680 }
681 }
682}
683
Jiyong Park2d492942018-03-05 17:44:10 +0900684type linkType int
685
686const (
687 javaCore linkType = iota
688 javaSdk
689 javaSystem
690 javaPlatform
691)
692
Jiyong Park46f78fb2018-10-20 16:33:17 +0900693func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700694 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700695 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900696 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
697 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100698 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900699 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100700 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900701 return javaCore, false
702 case name == "android_system_stubs_current":
703 return javaSystem, true
704 case strings.HasPrefix(ver, "system_"):
705 return javaSystem, false
706 case name == "android_test_stubs_current":
707 return javaSystem, true
708 case strings.HasPrefix(ver, "test_"):
709 return javaPlatform, false
710 case name == "android_stubs_current":
711 return javaSdk, true
712 case ver == "current":
713 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100714 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900715 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700716 default:
717 if _, err := strconv.Atoi(ver); err != nil {
718 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
719 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900720 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900721 }
722}
723
Jiyong Park750e5572018-01-31 00:20:13 +0900724func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700725 if ctx.Host() {
726 return
727 }
728
Jiyong Park46f78fb2018-10-20 16:33:17 +0900729 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
730 if stubs {
731 return
732 }
733 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900734 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."
735
736 switch myLinkType {
737 case javaCore:
738 if otherLinkType != javaCore {
739 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 +0900740 ctx.OtherModuleName(to))
741 }
Jiyong Park2d492942018-03-05 17:44:10 +0900742 break
743 case javaSdk:
744 if otherLinkType != javaCore && otherLinkType != javaSdk {
745 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
746 ctx.OtherModuleName(to))
747 }
748 break
749 case javaSystem:
750 if otherLinkType == javaPlatform {
751 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
752 ctx.OtherModuleName(to))
753 }
754 break
755 case javaPlatform:
756 // no restriction on link-type
757 break
Jiyong Park750e5572018-01-31 00:20:13 +0900758 }
759}
760
Colin Cross32f676a2017-09-06 13:41:06 -0700761func (j *Module) collectDeps(ctx android.ModuleContext) deps {
762 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700763
Colin Cross300f0382018-03-06 13:11:51 -0800764 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700765 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800766 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700767 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800768 } else if sdkDep.useFiles {
769 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700770 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700771 deps.aidlPreprocess = sdkDep.aidl
772 } else {
773 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800774 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700775 }
776
Colin Crossd11fcda2017-10-23 17:59:01 -0700777 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700778 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700779 tag := ctx.OtherModuleDependencyTag(module)
780
Colin Crossa4f08812018-10-02 22:03:40 -0700781 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700782 // Handled by AndroidApp.collectAppDeps
783 return
784 }
785 if tag == certificateTag {
786 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700787 return
788 }
789
Jiyong Park750e5572018-01-31 00:20:13 +0900790 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700791 switch tag {
792 case bootClasspathTag, libTag, staticLibTag:
793 checkLinkType(ctx, j, to, tag.(dependencyTag))
794 }
Jiyong Park750e5572018-01-31 00:20:13 +0900795 }
Colin Cross54250902017-12-05 09:28:08 -0800796 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800797 case SdkLibraryDependency:
798 switch tag {
799 case libTag:
800 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
801 // names of sdk libs that are directly depended are exported
802 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700803 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800804 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
805 }
Colin Cross54250902017-12-05 09:28:08 -0800806 case Dependency:
807 switch tag {
808 case bootClasspathTag:
809 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700810 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800811 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900812 // sdk lib names from dependencies are re-exported
813 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700814 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800815 case staticLibTag:
816 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
817 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
818 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700819 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900820 // sdk lib names from dependencies are re-exported
821 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700822 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800823 case pluginTag:
824 if plugin, ok := dep.(*Plugin); ok {
825 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
826 if plugin.pluginProperties.Processor_class != nil {
827 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
828 }
829 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
830 } else {
831 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
832 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800833 case frameworkApkTag:
834 if ctx.ModuleName() == "android_stubs_current" ||
835 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700836 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800837 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
838 // resource files out of there for aapt.
839 //
840 // Normally the package rule runs aapt, which includes the resource,
841 // but we're not running that in our package rule so just copy in the
842 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700843 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800844 }
Colin Cross54250902017-12-05 09:28:08 -0800845 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700846 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800847 case kotlinAnnotationsTag:
848 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800849 }
850
Colin Cross54250902017-12-05 09:28:08 -0800851 case android.SourceFileProducer:
852 switch tag {
853 case libTag:
854 checkProducesJars(ctx, dep)
855 deps.classpath = append(deps.classpath, dep.Srcs()...)
856 case staticLibTag:
857 checkProducesJars(ctx, dep)
858 deps.classpath = append(deps.classpath, dep.Srcs()...)
859 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
860 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800861 }
862 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700863 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700864 case systemModulesTag:
865 if deps.systemModules != nil {
866 panic("Found two system module dependencies")
867 }
868 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000869 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700870 panic("Missing directory for system module dependency")
871 }
Colin Crossb77043e2019-07-16 13:57:13 -0700872 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700873 }
Colin Crossec7a0422017-07-07 14:47:12 -0700874 }
Colin Cross2fe66872015-03-30 17:20:39 -0700875 })
876
Jiyong Park1be96912018-05-28 18:02:19 +0900877 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
878
Colin Cross32f676a2017-09-06 13:41:06 -0700879 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700880}
881
Colin Cross83bb3162018-06-25 15:48:06 -0700882func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700883 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800884 v := sdkContext.sdkVersion()
885 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100886 if ctx.Config().IsPdkBuild() &&
887 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700888 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800889 latestSdkVersion := 0
890 if len(sdkVersions) > 0 {
891 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
892 }
893 v = strconv.Itoa(latestSdkVersion)
894 }
895
896 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700897 if err != nil {
898 ctx.PropertyErrorf("sdk_version", "%s", err)
899 }
Nan Zhang357466b2018-04-17 17:38:36 -0700900 if javaVersion != "" {
901 ret = javaVersion
902 } else if ctx.Device() && sdk <= 23 {
903 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100904 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700905 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100906 } else if ctx.Device() &&
907 sdkContext.sdkVersion() != "" &&
908 sdkContext.sdkVersion() != "none" &&
909 sdkContext.sdkVersion() != "core_platform" &&
910 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700911 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
912 ret = "1.8"
913 } else {
914 ret = "1.9"
915 }
916
917 return ret
918}
919
Nan Zhanged19fc32017-10-19 13:06:22 -0700920func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700921
Colin Crossf03c82b2015-04-13 13:53:40 -0700922 var flags javaBuilderFlags
923
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100924 // javaVersion flag.
925 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
926
Nan Zhanged19fc32017-10-19 13:06:22 -0700927 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700928 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100929 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700930 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700931 }
Colin Cross6510f912017-11-29 00:27:14 -0800932 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700933 // Override the -g flag passed globally to remove local variable debug info to reduce
934 // disk and memory usage.
935 javacFlags = append(javacFlags, "-g:source,lines")
936 }
Colin Cross64162712017-08-08 13:17:59 -0700937
Colin Cross66548102018-06-19 22:47:35 -0700938 if ctx.Config().RunErrorProne() {
939 if config.ErrorProneClasspath == nil {
940 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
941 }
942
943 errorProneFlags := []string{
944 "-Xplugin:ErrorProne",
945 "${config.ErrorProneChecks}",
946 }
947 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
948
949 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
950 "'" + strings.Join(errorProneFlags, " ") + "'"
951 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800952 }
953
Nan Zhanged19fc32017-10-19 13:06:22 -0700954 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800955 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
956 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700957 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800958
Colin Crossbe9cdb82019-01-21 21:37:16 -0800959 flags.processor = strings.Join(deps.processorClasses, ",")
960
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100961 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100962 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800963 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
964 // Give host-side tools a version of OpenJDK's standard libraries
965 // close to what they're targeting. As of Dec 2017, AOSP is only
966 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
967 //
968 // When building with OpenJDK 8, the following should have no
969 // effect since those jars would be available by default.
970 //
971 // When building with OpenJDK 9 but targeting a version < 1.8,
972 // putting them on the bootclasspath means that:
973 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
974 // b) references to existing APIs are not reinterpreted in an
975 // OpenJDK 9-specific way, eg. calls to subclasses of
976 // java.nio.Buffer as in http://b/70862583
977 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
978 flags.bootClasspath = append(flags.bootClasspath,
979 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
980 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800981 if Bool(j.properties.Use_tools_jar) {
982 flags.bootClasspath = append(flags.bootClasspath,
983 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
984 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800985 }
986
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100987 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800988 // Manually specify build directory in case it is not under the repo root.
989 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
990 // just adding a symlink under the root doesn't help.)
991 patchPaths := ".:" + ctx.Config().BuildDir()
992 classPath := flags.classpath.FormJavaClassPath("")
993 if classPath != "" {
994 patchPaths += ":" + classPath
995 }
996 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700997 }
998
Nan Zhanged19fc32017-10-19 13:06:22 -0700999 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001000 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001001
Nan Zhanged19fc32017-10-19 13:06:22 -07001002 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001003 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001004
Colin Cross81440082018-08-15 20:21:55 -07001005 if len(javacFlags) > 0 {
1006 // optimization.
1007 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1008 flags.javacFlags = "$javacFlags"
1009 }
1010
Nan Zhanged19fc32017-10-19 13:06:22 -07001011 return flags
1012}
Colin Crossc0b06f12015-04-08 13:03:43 -07001013
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001014func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001015 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001016
1017 deps := j.collectDeps(ctx)
1018 flags := j.collectBuilderFlags(ctx, deps)
1019
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001020 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001021 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1022 }
Colin Cross8a497952019-03-05 22:25:09 -08001023 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001024 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001025 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001026 }
1027
Colin Crossaf050172017-11-15 23:01:59 -08001028 srcFiles = j.genSources(ctx, srcFiles, flags)
1029
1030 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001031 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001032 if aaptSrcJar != nil {
1033 srcJars = append(srcJars, aaptSrcJar)
1034 }
Colin Crossb7a63242015-04-16 14:09:14 -07001035
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001036 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1037 // that IDEInfo struct will use
1038 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1039
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001040 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001041 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001042 }
1043
Colin Cross1ee23172017-10-18 14:44:18 -07001044 jarName := ctx.ModuleName() + ".jar"
1045
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001046 javaSrcFiles := srcFiles.FilterByExt(".java")
1047 var uniqueSrcFiles android.Paths
1048 set := make(map[string]bool)
1049 for _, v := range javaSrcFiles {
1050 if _, found := set[v.String()]; !found {
1051 set[v.String()] = true
1052 uniqueSrcFiles = append(uniqueSrcFiles, v)
1053 }
1054 }
1055
Colin Cross55f63ea2018-08-27 12:37:09 -07001056 var kotlinJars android.Paths
1057
Colin Cross93e85952017-08-15 13:34:18 -07001058 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001059 // user defined kotlin flags.
1060 kotlincFlags := j.properties.Kotlincflags
1061 CheckKotlincFlags(ctx, kotlincFlags)
1062
Colin Cross93e85952017-08-15 13:34:18 -07001063 // If there are kotlin files, compile them first but pass all the kotlin and java files
1064 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1065 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001066 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001067 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001068 kotlincFlags = append(kotlincFlags, "-no-jdk")
1069 }
1070 if len(kotlincFlags) > 0 {
1071 // optimization.
1072 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1073 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001074 }
1075
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001076 var kotlinSrcFiles android.Paths
1077 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1078 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1079
Colin Crossafbb1732019-01-17 15:42:52 -08001080 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1081 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1082
1083 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1084 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1085
1086 if len(flags.processorPath) > 0 {
1087 // Use kapt for annotation processing
1088 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1089 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1090 srcJars = append(srcJars, kaptSrcJar)
1091 // Disable annotation processing in javac, it's already been handled by kapt
1092 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001093 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001094 }
Colin Cross93e85952017-08-15 13:34:18 -07001095
Colin Cross1ee23172017-10-18 14:44:18 -07001096 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001097 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001098 if ctx.Failed() {
1099 return
1100 }
1101
1102 // Make javac rule depend on the kotlinc rule
1103 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001104
Colin Cross93e85952017-08-15 13:34:18 -07001105 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001106 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001107 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001108 }
1109
Colin Cross55f63ea2018-08-27 12:37:09 -07001110 jars := append(android.Paths(nil), kotlinJars...)
1111
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001112 // Store the list of .java files that was passed to javac
1113 j.compiledJavaSrcs = uniqueSrcFiles
1114 j.compiledSrcJars = srcJars
1115
Nan Zhang61eaedb2017-11-02 13:28:15 -07001116 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001117 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001118 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1119 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001120 // Formerly, there was a check here that prevented annotation processors
1121 // from being used when sharding was enabled, as some annotation processors
1122 // do not function correctly in sharded environments. It was removed to
1123 // allow for the use of annotation processors that do function correctly
1124 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001125 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001126 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001127 if ctx.Failed() {
1128 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001129 }
1130 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001131 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001132 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001133 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001134 // If error-prone is enabled, add an additional rule to compile the java files into
1135 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001136 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001137 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1138 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001139 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001140 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001141 extraJarDeps = append(extraJarDeps, errorprone)
1142 }
1143
Nan Zhang61eaedb2017-11-02 13:28:15 -07001144 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001145 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001146 shardSize := int(*(j.properties.Javac_shard_size))
1147 var shardSrcs []android.Paths
1148 if len(uniqueSrcFiles) > 0 {
1149 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1150 for idx, shardSrc := range shardSrcs {
1151 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1152 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1153 jars = append(jars, classes)
1154 }
1155 }
1156 if len(srcJars) > 0 {
1157 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1158 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1159 jars = append(jars, classes)
1160 }
1161 } else {
1162 classes := android.PathForModuleOut(ctx, "javac", jarName)
1163 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1164 jars = append(jars, classes)
1165 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001166 if ctx.Config().EmitXrefRules() {
1167 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1168 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1169 j.kytheFiles = append(j.kytheFiles, extractionFile)
1170
1171 }
Colin Crossd6891432017-09-27 17:39:56 -07001172 if ctx.Failed() {
1173 return
1174 }
Colin Cross2fe66872015-03-30 17:20:39 -07001175 }
1176
Colin Cross0c4ce212019-05-03 15:28:19 -07001177 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1178
1179 var includeSrcJar android.WritablePath
1180 if Bool(j.properties.Include_srcs) {
1181 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1182 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1183 }
1184
Colin Crosscedd4762018-09-13 11:26:19 -07001185 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1186 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001187 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001188 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001189
1190 var resArgs []string
1191 var resDeps android.Paths
1192
1193 resArgs = append(resArgs, dirArgs...)
1194 resDeps = append(resDeps, dirDeps...)
1195
1196 resArgs = append(resArgs, fileArgs...)
1197 resDeps = append(resDeps, fileDeps...)
1198
Colin Cross988708c2019-05-06 14:04:11 -07001199 resArgs = append(resArgs, extraArgs...)
1200 resDeps = append(resDeps, extraDeps...)
1201
Colin Cross40a36712017-09-27 17:41:35 -07001202 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001203 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001204 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001205 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001206 if ctx.Failed() {
1207 return
1208 }
1209 }
1210
Colin Cross0c4ce212019-05-03 15:28:19 -07001211 var resourceJars android.Paths
1212 if j.resourceJar != nil {
1213 resourceJars = append(resourceJars, j.resourceJar)
1214 }
1215 if Bool(j.properties.Include_srcs) {
1216 resourceJars = append(resourceJars, includeSrcJar)
1217 }
1218 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001219
Colin Cross0c4ce212019-05-03 15:28:19 -07001220 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001221 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001222 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001223 false, nil, nil)
1224 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001225 } else if len(resourceJars) == 1 {
1226 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001227 }
1228
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001229 if len(deps.staticJars) > 0 {
1230 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001231 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001232
Colin Cross094054a2018-10-17 15:10:48 -07001233 manifest := j.overrideManifest
1234 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001235 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001236 }
Colin Cross635acc92017-09-12 22:50:46 -07001237
Colin Cross8a497952019-03-05 22:25:09 -08001238 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001239 if len(services) > 0 {
1240 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1241 var zipargs []string
1242 for _, file := range services {
1243 serviceFile := file.String()
1244 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1245 }
1246 ctx.Build(pctx, android.BuildParams{
1247 Rule: zip,
1248 Output: servicesJar,
1249 Implicits: services,
1250 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001251 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001252 },
1253 })
1254 jars = append(jars, servicesJar)
1255 }
1256
Colin Cross0a6e0072017-08-30 14:24:55 -07001257 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001258 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001259 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001260
1261 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001262 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1263 // Optimization: skip the combine step if there is nothing to do
1264 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1265 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1266 // any if len(jars) == 1.
1267 outputFile = moduleOutPath
1268 } else {
1269 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1270 ctx.Build(pctx, android.BuildParams{
1271 Rule: android.Cp,
1272 Input: jars[0],
1273 Output: combinedJar,
1274 })
1275 outputFile = combinedJar
1276 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001277 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001278 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001279 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001280 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001281 outputFile = combinedJar
1282 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001283
Colin Cross331a1212018-08-15 20:40:52 -07001284 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001285 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001286 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001287 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001288 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001289 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001290
1291 // jarjar resource jar if necessary
1292 if j.resourceJar != nil {
1293 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001294 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001295 j.resourceJar = resourceJarJarFile
1296 }
1297
Colin Cross0a6e0072017-08-30 14:24:55 -07001298 if ctx.Failed() {
1299 return
1300 }
1301 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001302
1303 // Check package restrictions if necessary.
1304 if len(j.properties.Permitted_packages) > 0 {
1305 // Check packages and copy to package-checked file.
1306 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1307 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1308 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1309
1310 if ctx.Failed() {
1311 return
1312 }
1313 }
1314
Nan Zhanged19fc32017-10-19 13:06:22 -07001315 j.implementationJarFile = outputFile
1316 if j.headerJarFile == nil {
1317 j.headerJarFile = j.implementationJarFile
1318 }
Colin Cross2fe66872015-03-30 17:20:39 -07001319
Colin Cross6510f912017-11-29 00:27:14 -08001320 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001321 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1322 j.properties.Instrument = true
1323 }
1324 }
1325
Colin Cross3144dfc2018-01-03 15:06:47 -08001326 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001327 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1328 }
1329
Colin Cross331a1212018-08-15 20:40:52 -07001330 // merge implementation jar with resources if necessary
1331 implementationAndResourcesJar := outputFile
1332 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001333 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001334 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001335 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001336 false, nil, nil)
1337 implementationAndResourcesJar = combinedJar
1338 }
1339
1340 j.implementationAndResourcesJar = implementationAndResourcesJar
1341
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001342 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001343 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001344 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001345 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001346 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001347 if ctx.Failed() {
1348 return
1349 }
Colin Cross331a1212018-08-15 20:40:52 -07001350
Jiyong Park09cb6292019-07-15 15:29:23 +09001351 // Hidden API CSV generation and dex encoding
1352 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1353 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001354
Colin Cross331a1212018-08-15 20:40:52 -07001355 // merge dex jar with resources if necessary
1356 if j.resourceJar != nil {
1357 jars := android.Paths{dexOutputFile, j.resourceJar}
1358 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1359 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1360 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001361 if j.deviceProperties.UncompressDex {
1362 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1363 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1364 dexOutputFile = combinedAlignedJar
1365 } else {
1366 dexOutputFile = combinedJar
1367 }
Colin Cross331a1212018-08-15 20:40:52 -07001368 }
1369
1370 j.dexJarFile = dexOutputFile
1371
Colin Cross8faf8fc2019-01-16 15:15:52 -08001372 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001373 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1374
1375 j.maybeStrippedDexJarFile = dexOutputFile
1376
Colin Cross3063b782018-08-15 11:19:12 -07001377 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001378
1379 if ctx.Failed() {
1380 return
1381 }
Colin Cross331a1212018-08-15 20:40:52 -07001382 } else {
1383 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001384 }
Colin Cross331a1212018-08-15 20:40:52 -07001385
Colin Crossb7a63242015-04-16 14:09:14 -07001386 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001387
1388 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1389 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001390}
1391
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001392// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1393// since some of these flags may be used internally.
1394func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1395 for _, flag := range flags {
1396 flag = strings.TrimSpace(flag)
1397
1398 if !strings.HasPrefix(flag, "-") {
1399 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1400 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1401 ctx.PropertyErrorf("kotlincflags",
1402 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1403 } else if inList(flag, config.KotlincIllegalFlags) {
1404 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1405 } else if flag == "-include-runtime" {
1406 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1407 } else {
1408 args := strings.Split(flag, " ")
1409 if args[0] == "-kotlin-home" {
1410 ctx.PropertyErrorf("kotlincflags",
1411 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1412 }
1413 }
1414 }
1415}
1416
Colin Cross8eadbf02017-10-24 17:46:00 -07001417func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001418 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001419
1420 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001421 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001422 // Compile java sources into turbine.jar.
1423 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1424 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1425 if ctx.Failed() {
1426 return nil
1427 }
1428 jars = append(jars, turbineJar)
1429 }
1430
Colin Cross55f63ea2018-08-27 12:37:09 -07001431 jars = append(jars, extraJars...)
1432
Nan Zhanged19fc32017-10-19 13:06:22 -07001433 // Combine any static header libraries into classes-header.jar. If there is only
1434 // one input jar this step will be skipped.
1435 var headerJar android.Path
1436 jars = append(jars, deps.staticHeaderJars...)
1437
Colin Cross5c6ecc12017-10-23 18:12:27 -07001438 // we cannot skip the combine step for now if there is only one jar
1439 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1440 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001441 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001442 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001443 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001444
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001445 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001446 // Transform classes.jar into classes-jarjar.jar
1447 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001448 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001449 headerJar = jarjarFile
1450 if ctx.Failed() {
1451 return nil
1452 }
1453 }
1454
1455 return headerJar
1456}
1457
Colin Crosscb933592017-11-22 13:49:43 -08001458func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001459 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001460
Colin Cross7a3139e2017-12-19 13:57:50 -08001461 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001462
Colin Cross84c38822018-01-03 15:59:46 -08001463 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001464 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1465
1466 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1467
1468 j.jacocoReportClassesFile = jacocoReportClassesFile
1469
1470 return instrumentedJar
1471}
1472
albaltai36ff7dc2018-12-25 14:35:23 +08001473var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001474
Nan Zhanged19fc32017-10-19 13:06:22 -07001475func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001476 if j.headerJarFile == nil {
1477 return nil
1478 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001479 return android.Paths{j.headerJarFile}
1480}
1481
1482func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001483 if j.implementationJarFile == nil {
1484 return nil
1485 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001486 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001487}
1488
Colin Crossf24a22a2019-01-31 14:12:44 -08001489func (j *Module) DexJar() android.Path {
1490 return j.dexJarFile
1491}
1492
Colin Cross331a1212018-08-15 20:40:52 -07001493func (j *Module) ResourceJars() android.Paths {
1494 if j.resourceJar == nil {
1495 return nil
1496 }
1497 return android.Paths{j.resourceJar}
1498}
1499
1500func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001501 if j.implementationAndResourcesJar == nil {
1502 return nil
1503 }
Colin Cross331a1212018-08-15 20:40:52 -07001504 return android.Paths{j.implementationAndResourcesJar}
1505}
1506
Colin Cross46c9b8b2017-06-22 16:51:17 -07001507func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001508 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001509 return j.exportAidlIncludeDirs
1510}
1511
Jiyong Park1be96912018-05-28 18:02:19 +09001512func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001513 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001514 return j.exportedSdkLibs
1515}
1516
Colin Cross0c4ce212019-05-03 15:28:19 -07001517func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1518 return j.srcJarArgs, j.srcJarDeps
1519}
1520
Colin Cross46c9b8b2017-06-22 16:51:17 -07001521var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001522
Colin Cross46c9b8b2017-06-22 16:51:17 -07001523func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001524 return j.logtagsSrcs
1525}
1526
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001527// Collect information for opening IDE project files in java/jdeps.go.
1528func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1529 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1530 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001531 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001532 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001533 if j.expandJarjarRules != nil {
1534 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001535 }
1536}
1537
1538func (j *Module) CompilerDeps() []string {
1539 jdeps := []string{}
1540 jdeps = append(jdeps, j.properties.Libs...)
1541 jdeps = append(jdeps, j.properties.Static_libs...)
1542 return jdeps
1543}
1544
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001545func (j *Module) hasCode(ctx android.ModuleContext) bool {
1546 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1547 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1548}
1549
Colin Cross2fe66872015-03-30 17:20:39 -07001550//
1551// Java libraries (.jar file)
1552//
1553
Colin Crossf506d872017-07-19 15:53:04 -07001554type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001555 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001556}
1557
Colin Cross42be7612019-02-21 18:12:14 -08001558func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001559 // Store uncompressed (and do not strip) dex files from boot class path jars.
1560 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1561 return true
1562 }
1563
1564 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001565 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001566 return true
1567 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001568 if ctx.Config().UncompressPrivAppDex() &&
1569 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1570 return true
1571 }
1572
Colin Cross2fc72f62018-12-21 12:59:54 -08001573 return false
1574}
1575
Colin Crossf506d872017-07-19 15:53:04 -07001576func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001577 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1578 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001579 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001580 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001581 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001582 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001583
Jiyong Park7f7766d2019-07-25 22:02:35 +09001584 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1585 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001586 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1587 ctx.ModuleName()+".jar", j.outputFile)
1588 }
Colin Crossb7a63242015-04-16 14:09:14 -07001589}
1590
Colin Crossf506d872017-07-19 15:53:04 -07001591func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001592 j.deps(ctx)
1593}
1594
Colin Cross1b16b0e2019-02-12 14:41:32 -08001595// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1596//
1597// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1598// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1599// as a `static_libs` dependency of another module.
1600//
1601// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1602// a device.
1603//
1604// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1605// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001606func LibraryFactory() android.Module {
1607 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001608
Colin Cross9ae1b922018-06-26 17:59:05 -07001609 module.AddProperties(
1610 &module.Module.properties,
1611 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001612 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001613 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001614
Colin Cross9ae1b922018-06-26 17:59:05 -07001615 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001616 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001617 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001618}
1619
Colin Cross1b16b0e2019-02-12 14:41:32 -08001620// java_library_static is an obsolete alias for java_library.
1621func LibraryStaticFactory() android.Module {
1622 return LibraryFactory()
1623}
1624
1625// java_library_host builds and links sources into a `.jar` file for the host.
1626//
1627// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1628// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001629func LibraryHostFactory() android.Module {
1630 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001631
Colin Cross6af17aa2017-09-20 12:59:05 -07001632 module.AddProperties(
1633 &module.Module.properties,
1634 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001635
Colin Cross9ae1b922018-06-26 17:59:05 -07001636 module.Module.properties.Installable = proptools.BoolPtr(true)
1637
Colin Cross89536d42017-07-07 14:35:50 -07001638 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001639 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001640 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001641}
1642
1643//
Colin Crossb628ea52018-08-14 16:42:33 -07001644// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001645//
1646
1647type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001648 // list of compatibility suites (for example "cts", "vts") that the module should be
1649 // installed into.
1650 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001651
1652 // the name of the test configuration (for example "AndroidTest.xml") that should be
1653 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001654 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001655
Jack He33338892018-09-19 02:21:28 -07001656 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1657 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001658 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001659
Colin Crossd96ca352018-08-10 16:06:24 -07001660 // list of files or filegroup modules that provide data that should be installed alongside
1661 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001662 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001663}
1664
Paul Duffin42df1442019-03-20 12:45:53 +00001665type testHelperLibraryProperties struct {
1666 // list of compatibility suites (for example "cts", "vts") that the module should be
1667 // installed into.
1668 Test_suites []string `android:"arch_variant"`
1669}
1670
Colin Cross05638fc2018-04-09 18:40:24 -07001671type Test struct {
1672 Library
1673
1674 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001675
1676 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001677 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001678}
1679
Paul Duffin42df1442019-03-20 12:45:53 +00001680type TestHelperLibrary struct {
1681 Library
1682
1683 testHelperLibraryProperties testHelperLibraryProperties
1684}
1685
Colin Cross303e21f2018-08-07 16:49:25 -07001686func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001687 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 -08001688 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001689
1690 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001691}
1692
Paul Duffin42df1442019-03-20 12:45:53 +00001693func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1694 j.Library.GenerateAndroidBuildActions(ctx)
1695}
1696
Colin Cross1b16b0e2019-02-12 14:41:32 -08001697// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1698// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1699//
1700// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1701// compiled against the device bootclasspath.
1702//
1703// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1704// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001705func TestFactory() android.Module {
1706 module := &Test{}
1707
1708 module.AddProperties(
1709 &module.Module.properties,
1710 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001711 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001712 &module.Module.protoProperties,
1713 &module.testProperties)
1714
Colin Cross9ae1b922018-06-26 17:59:05 -07001715 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001716 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001717
Colin Cross05638fc2018-04-09 18:40:24 -07001718 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001719 return module
1720}
1721
Paul Duffin42df1442019-03-20 12:45:53 +00001722// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1723func TestHelperLibraryFactory() android.Module {
1724 module := &TestHelperLibrary{}
1725
1726 module.AddProperties(
1727 &module.Module.properties,
1728 &module.Module.deviceProperties,
1729 &module.Module.dexpreoptProperties,
1730 &module.Module.protoProperties,
1731 &module.testHelperLibraryProperties)
1732
Colin Cross9a4abed2019-04-24 13:19:28 -07001733 module.Module.properties.Installable = proptools.BoolPtr(true)
1734 module.Module.dexpreopter.isTest = true
1735
Paul Duffin42df1442019-03-20 12:45:53 +00001736 InitJavaModule(module, android.HostAndDeviceSupported)
1737 return module
1738}
1739
Colin Cross1b16b0e2019-02-12 14:41:32 -08001740// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1741// allow running the test with `atest` or a `TEST_MAPPING` file.
1742//
1743// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1744// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001745func TestHostFactory() android.Module {
1746 module := &Test{}
1747
1748 module.AddProperties(
1749 &module.Module.properties,
1750 &module.Module.protoProperties,
1751 &module.testProperties)
1752
Colin Cross9ae1b922018-06-26 17:59:05 -07001753 module.Module.properties.Installable = proptools.BoolPtr(true)
1754
Colin Cross05638fc2018-04-09 18:40:24 -07001755 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001756 return module
1757}
1758
1759//
Colin Cross2fe66872015-03-30 17:20:39 -07001760// Java Binaries (.jar file plus wrapper script)
1761//
1762
Colin Crossf506d872017-07-19 15:53:04 -07001763type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001764 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001765 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001766
1767 // Name of the class containing main to be inserted into the manifest as Main-Class.
1768 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001769}
1770
Colin Crossf506d872017-07-19 15:53:04 -07001771type Binary struct {
1772 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001773
Colin Crossf506d872017-07-19 15:53:04 -07001774 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001775
Colin Cross6b4a32d2017-12-05 13:42:45 -08001776 isWrapperVariant bool
1777
Colin Crossc3315992017-12-08 19:12:36 -08001778 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001779 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001780}
1781
Alex Light24237172017-10-26 09:46:21 -07001782func (j *Binary) HostToolPath() android.OptionalPath {
1783 return android.OptionalPathForPath(j.binaryFile)
1784}
1785
Colin Crossf506d872017-07-19 15:53:04 -07001786func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001787 if ctx.Arch().ArchType == android.Common {
1788 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001789 if j.binaryProperties.Main_class != nil {
1790 if j.properties.Manifest != nil {
1791 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1792 }
1793 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1794 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1795 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1796 }
1797
Colin Cross6b4a32d2017-12-05 13:42:45 -08001798 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001799 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001800 // Handle the binary wrapper
1801 j.isWrapperVariant = true
1802
Colin Cross366938f2017-12-11 16:29:02 -08001803 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001804 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001805 } else {
1806 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1807 }
1808
1809 // Depend on the installed jar so that the wrapper doesn't get executed by
1810 // another build rule before the jar has been installed.
1811 jarFile := ctx.PrimaryModule().(*Binary).installFile
1812
1813 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1814 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001815 }
Colin Cross2fe66872015-03-30 17:20:39 -07001816}
1817
Colin Crossf506d872017-07-19 15:53:04 -07001818func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001819 if ctx.Arch().ArchType == android.Common {
1820 j.deps(ctx)
1821 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001822}
1823
Colin Cross1b16b0e2019-02-12 14:41:32 -08001824// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1825// as well.
1826//
1827// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1828// compiled against the device bootclasspath.
1829//
1830// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1831// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001832func BinaryFactory() android.Module {
1833 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001834
Colin Cross36242852017-06-23 15:06:31 -07001835 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001836 &module.Module.properties,
1837 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001838 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001839 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001840 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001841
Colin Cross9ae1b922018-06-26 17:59:05 -07001842 module.Module.properties.Installable = proptools.BoolPtr(true)
1843
Colin Cross6b4a32d2017-12-05 13:42:45 -08001844 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1845 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001846 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001847}
1848
Colin Cross1b16b0e2019-02-12 14:41:32 -08001849// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1850//
1851// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1852// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001853func BinaryHostFactory() android.Module {
1854 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001855
Colin Cross36242852017-06-23 15:06:31 -07001856 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001857 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001858 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001859 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001860
Colin Cross9ae1b922018-06-26 17:59:05 -07001861 module.Module.properties.Installable = proptools.BoolPtr(true)
1862
Colin Cross6b4a32d2017-12-05 13:42:45 -08001863 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1864 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001865 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001866}
1867
1868//
1869// Java prebuilts
1870//
1871
Colin Cross74d73e22017-08-02 11:05:49 -07001872type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001873 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001874
Nan Zhangea568a42017-11-08 21:20:04 -08001875 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001876
1877 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001878
1879 // List of shared java libs that this module has dependencies to
1880 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001881
1882 // List of files to remove from the jar file(s)
1883 Exclude_files []string
1884
1885 // List of directories to remove from the jar file(s)
1886 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001887
1888 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001889 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001890}
1891
1892type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001893 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001894 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001895 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001896 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001897
Colin Cross74d73e22017-08-02 11:05:49 -07001898 properties ImportProperties
1899
Colin Cross0a6e0072017-08-30 14:24:55 -07001900 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001901 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001902}
1903
Colin Cross83bb3162018-06-25 15:48:06 -07001904func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001905 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001906}
1907
1908func (j *Import) minSdkVersion() string {
1909 return j.sdkVersion()
1910}
1911
Colin Cross74d73e22017-08-02 11:05:49 -07001912func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001913 return &j.prebuilt
1914}
1915
Colin Cross74d73e22017-08-02 11:05:49 -07001916func (j *Import) PrebuiltSrcs() []string {
1917 return j.properties.Jars
1918}
1919
1920func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001921 return j.prebuilt.Name(j.ModuleBase.Name())
1922}
1923
Colin Cross74d73e22017-08-02 11:05:49 -07001924func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001925 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001926}
1927
Colin Cross74d73e22017-08-02 11:05:49 -07001928func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001929 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001930
Nan Zhang4c819fb2018-08-27 18:31:46 -07001931 jarName := ctx.ModuleName() + ".jar"
1932 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001933 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1934 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001935 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001936 inputFile := outputFile
1937 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1938 TransformJetifier(ctx, outputFile, inputFile)
1939 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001940 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001941
1942 ctx.VisitDirectDeps(func(module android.Module) {
1943 otherName := ctx.OtherModuleName(module)
1944 tag := ctx.OtherModuleDependencyTag(module)
1945
1946 switch dep := module.(type) {
1947 case Dependency:
1948 switch tag {
1949 case libTag, staticLibTag:
1950 // sdk lib names from dependencies are re-exported
1951 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1952 }
1953 case SdkLibraryDependency:
1954 switch tag {
1955 case libTag:
1956 // names of sdk libs that are directly depended are exported
1957 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1958 }
1959 }
1960 })
1961
1962 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001963 if Bool(j.properties.Installable) {
1964 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1965 ctx.ModuleName()+".jar", outputFile)
1966 }
Colin Cross2fe66872015-03-30 17:20:39 -07001967}
1968
Colin Cross74d73e22017-08-02 11:05:49 -07001969var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001970
Nan Zhanged19fc32017-10-19 13:06:22 -07001971func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001972 if j.combinedClasspathFile == nil {
1973 return nil
1974 }
Colin Cross37f6d792018-07-12 12:28:41 -07001975 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001976}
1977
1978func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001979 if j.combinedClasspathFile == nil {
1980 return nil
1981 }
Colin Cross37f6d792018-07-12 12:28:41 -07001982 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001983}
1984
Colin Cross331a1212018-08-15 20:40:52 -07001985func (j *Import) ResourceJars() android.Paths {
1986 return nil
1987}
1988
1989func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001990 if j.combinedClasspathFile == nil {
1991 return nil
1992 }
Colin Cross331a1212018-08-15 20:40:52 -07001993 return android.Paths{j.combinedClasspathFile}
1994}
1995
Colin Crossf24a22a2019-01-31 14:12:44 -08001996func (j *Import) DexJar() android.Path {
1997 return nil
1998}
1999
Colin Cross74d73e22017-08-02 11:05:49 -07002000func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002001 return nil
2002}
2003
Jiyong Park1be96912018-05-28 18:02:19 +09002004func (j *Import) ExportedSdkLibs() []string {
2005 return j.exportedSdkLibs
2006}
2007
Colin Cross0c4ce212019-05-03 15:28:19 -07002008func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2009 return nil, nil
2010}
2011
albaltai36ff7dc2018-12-25 14:35:23 +08002012// Add compile time check for interface implementation
2013var _ android.IDEInfo = (*Import)(nil)
2014var _ android.IDECustomizedModuleName = (*Import)(nil)
2015
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002016// Collect information for opening IDE project files in java/jdeps.go.
2017const (
2018 removedPrefix = "prebuilt_"
2019)
2020
2021func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2022 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2023}
2024
2025func (j *Import) IDECustomizedModuleName() string {
2026 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2027 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2028 // solution to get the Import name.
2029 name := j.Name()
2030 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002031 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002032 }
2033 return name
2034}
2035
Colin Cross74d73e22017-08-02 11:05:49 -07002036var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002037
Colin Cross1b16b0e2019-02-12 14:41:32 -08002038// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2039//
2040// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2041// compiled against an Android classpath.
2042//
2043// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2044// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002045func ImportFactory() android.Module {
2046 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002047
Colin Cross74d73e22017-08-02 11:05:49 -07002048 module.AddProperties(&module.properties)
2049
2050 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002051 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002052 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002053 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002054}
2055
Colin Cross1b16b0e2019-02-12 14:41:32 -08002056// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2057// module.
2058//
2059// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2060// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002061func ImportFactoryHost() android.Module {
2062 module := &Import{}
2063
2064 module.AddProperties(&module.properties)
2065
2066 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002067 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002068 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002069 return module
2070}
2071
Colin Cross42be7612019-02-21 18:12:14 -08002072// dex_import module
2073
2074type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002075 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002076}
2077
2078type DexImport struct {
2079 android.ModuleBase
2080 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002081 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002082 prebuilt android.Prebuilt
2083
2084 properties DexImportProperties
2085
2086 dexJarFile android.Path
2087 maybeStrippedDexJarFile android.Path
2088
2089 dexpreopter
2090}
2091
2092func (j *DexImport) Prebuilt() *android.Prebuilt {
2093 return &j.prebuilt
2094}
2095
2096func (j *DexImport) PrebuiltSrcs() []string {
2097 return j.properties.Jars
2098}
2099
2100func (j *DexImport) Name() string {
2101 return j.prebuilt.Name(j.ModuleBase.Name())
2102}
2103
Colin Cross42be7612019-02-21 18:12:14 -08002104func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2105 if len(j.properties.Jars) != 1 {
2106 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2107 }
2108
2109 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2110 j.dexpreopter.isInstallable = true
2111 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2112
2113 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2114 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2115
2116 if j.dexpreopter.uncompressedDex {
2117 rule := android.NewRuleBuilder()
2118
2119 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2120 rule.Temporary(temporary)
2121
2122 // use zip2zip to uncompress classes*.dex files
2123 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002124 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002125 FlagWithInput("-i ", inputJar).
2126 FlagWithOutput("-o ", temporary).
2127 FlagWithArg("-0 ", "'classes*.dex'")
2128
2129 // use zipalign to align uncompressed classes*.dex files
2130 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002131 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002132 Flag("-f").
2133 Text("4").
2134 Input(temporary).
2135 Output(dexOutputFile)
2136
2137 rule.DeleteTemporaryFiles()
2138
2139 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2140 } else {
2141 ctx.Build(pctx, android.BuildParams{
2142 Rule: android.Cp,
2143 Input: inputJar,
2144 Output: dexOutputFile,
2145 })
2146 }
2147
2148 j.dexJarFile = dexOutputFile
2149
2150 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2151
2152 j.maybeStrippedDexJarFile = dexOutputFile
2153
2154 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2155 ctx.ModuleName()+".jar", dexOutputFile)
2156}
2157
2158func (j *DexImport) DexJar() android.Path {
2159 return j.dexJarFile
2160}
2161
2162// dex_import imports a `.jar` file containing classes.dex files.
2163//
2164// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2165// to the device.
2166func DexImportFactory() android.Module {
2167 module := &DexImport{}
2168
2169 module.AddProperties(&module.properties)
2170
2171 android.InitPrebuiltModule(module, &module.properties.Jars)
2172 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002173 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002174 return module
2175}
2176
Colin Cross89536d42017-07-07 14:35:50 -07002177//
2178// Defaults
2179//
2180type Defaults struct {
2181 android.ModuleBase
2182 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002183 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002184}
2185
Colin Cross1b16b0e2019-02-12 14:41:32 -08002186// java_defaults provides a set of properties that can be inherited by other java or android modules.
2187//
2188// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2189// property in the defaults module that exists in the depending module will be prepended to the depending module's
2190// value for that property.
2191//
2192// Example:
2193//
2194// java_defaults {
2195// name: "example_defaults",
2196// srcs: ["common/**/*.java"],
2197// javacflags: ["-Xlint:all"],
2198// aaptflags: ["--auto-add-overlay"],
2199// }
2200//
2201// java_library {
2202// name: "example",
2203// defaults: ["example_defaults"],
2204// srcs: ["example/**/*.java"],
2205// }
2206//
2207// is functionally identical to:
2208//
2209// java_library {
2210// name: "example",
2211// srcs: [
2212// "common/**/*.java",
2213// "example/**/*.java",
2214// ],
2215// javacflags: ["-Xlint:all"],
2216// }
Colin Cross89536d42017-07-07 14:35:50 -07002217func defaultsFactory() android.Module {
2218 return DefaultsFactory()
2219}
2220
2221func DefaultsFactory(props ...interface{}) android.Module {
2222 module := &Defaults{}
2223
2224 module.AddProperties(props...)
2225 module.AddProperties(
2226 &CompilerProperties{},
2227 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002228 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002229 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002230 &aaptProperties{},
2231 &androidLibraryProperties{},
2232 &appProperties{},
2233 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002234 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002235 &ImportProperties{},
2236 &AARImportProperties{},
2237 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002238 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002239 )
2240
2241 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002242 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002243 return module
2244}
Nan Zhangea568a42017-11-08 21:20:04 -08002245
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002246func kytheExtractJavaFactory() android.Singleton {
2247 return &kytheExtractJavaSingleton{}
2248}
2249
2250type kytheExtractJavaSingleton struct {
2251}
2252
2253func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2254 var xrefTargets android.Paths
2255 ctx.VisitAllModules(func(module android.Module) {
2256 if javaModule, ok := module.(xref); ok {
2257 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2258 }
2259 })
2260 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2261 if len(xrefTargets) > 0 {
2262 ctx.Build(pctx, android.BuildParams{
2263 Rule: blueprint.Phony,
2264 Output: android.PathForPhony(ctx, "xref_java"),
2265 Inputs: xrefTargets,
2266 })
2267 }
2268}
2269
Nan Zhangea568a42017-11-08 21:20:04 -08002270var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002271var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002272var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002273var inList = android.InList