blob: 2193a2bd7bf2b4851c1b229ebec36571a3554d04 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Sasha Smundak2a4549e2018-11-05 16:49:08 -080053 android.RegisterSingletonType("kythe_java_extract", kytheExtractJavaFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Jeongik Cha538c0d02019-07-11 15:54:27 +090056func (j *Module) checkPlatformAPI(ctx android.ModuleContext) {
57 if sc, ok := ctx.Module().(sdkContext); ok {
58 usePlatformAPI := proptools.Bool(j.deviceProperties.Platform_apis)
59 if usePlatformAPI != (sc.sdkVersion() == "") {
60 if usePlatformAPI {
61 ctx.PropertyErrorf("platform_apis", "platform_apis must be false when sdk_version is not empty.")
62 } else {
63 ctx.PropertyErrorf("platform_apis", "platform_apis must be true when sdk_version is empty.")
64 }
65 }
66
67 }
68}
69
Colin Cross2fe66872015-03-30 17:20:39 -070070// TODO:
71// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070072// Renderscript
73// Post-jar passes:
74// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070075// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070076// DroidDoc
77// Findbugs
78
Colin Cross89536d42017-07-07 14:35:50 -070079type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070080 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
81 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080082 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070083
84 // list of source files that should not be used to build the Java module.
85 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080086 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070087
88 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070089 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070090
Colin Cross86a63ff2017-09-27 17:33:10 -070091 // list of directories that should be excluded from java_resource_dirs
92 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070093
Colin Cross0f37af02017-09-27 17:42:05 -070094 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080095 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070096
Colin Crosscedd4762018-09-13 11:26:19 -070097 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080098 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070099
Colin Cross7d5136f2015-05-11 13:39:40 -0700100 // list of module-specific flags that will be used for javac compiles
101 Javacflags []string `android:"arch_variant"`
102
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200103 // list of module-specific flags that will be used for kotlinc compiles
104 Kotlincflags []string `android:"arch_variant"`
105
Colin Cross7d5136f2015-05-11 13:39:40 -0700106 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -0700107 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700108
109 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700110 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700111
112 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800113 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700114
Colin Cross540eff82017-06-22 17:01:52 -0700115 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800116 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700117
118 // If not blank, set the java version passed to javac as -source and -target
119 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700120
Colin Cross9ae1b922018-06-26 17:59:05 -0700121 // If set to true, allow this module to be dexed and installed on devices. Has no
122 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700123 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700124
Colin Cross0f37af02017-09-27 17:42:05 -0700125 // If set to true, include sources used to compile the module in to the final jar
126 Include_srcs *bool
127
Vladimir Marko0975ee02019-04-02 10:29:55 +0100128 // If not empty, classes are restricted to the specified packages and their sub-packages.
129 // This restriction is checked after applying jarjar rules and including static libs.
130 Permitted_packages []string
131
Colin Crossbe9cdb82019-01-21 21:37:16 -0800132 // List of modules to use as annotation processors
133 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700134
Nan Zhang61eaedb2017-11-02 13:28:15 -0700135 // The number of Java source entries each Javac instance can process
136 Javac_shard_size *int64
137
Nan Zhang5f8cb422018-02-06 10:34:32 -0800138 // Add host jdk tools.jar to bootclasspath
139 Use_tools_jar *bool
140
Colin Cross1369cdb2017-09-29 17:58:17 -0700141 Openjdk9 struct {
142 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800143 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700144
145 // List of javac flags that should only be used when passing -source 1.9
146 Javacflags []string
147 }
Colin Crosscb933592017-11-22 13:49:43 -0800148
Colin Cross81440082018-08-15 20:21:55 -0700149 // When compiling language level 9+ .java code in packages that are part of
150 // a system module, patch_module names the module that your sources and
151 // dependencies should be patched into. The Android runtime currently
152 // doesn't implement the JEP 261 module system so this option is only
153 // supported at compile time. It should only be needed to compile tests in
154 // packages that exist in libcore and which are inconvenient to move
155 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100156 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700157
Colin Crosscb933592017-11-22 13:49:43 -0800158 Jacoco struct {
159 // List of classes to include for instrumentation with jacoco to collect coverage
160 // information at runtime when building with coverage enabled. If unset defaults to all
161 // classes.
162 // Supports '*' as the last character of an entry in the list as a wildcard match.
163 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
164 // it matches classes in the package that have the class name as a prefix.
165 Include_filter []string
166
167 // List of classes to exclude from instrumentation with jacoco to collect coverage
168 // information at runtime when building with coverage enabled. Overrides classes selected
169 // by the include_filter property.
170 // Supports '*' as the last character of an entry in the list as a wildcard match.
171 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
172 // it matches classes in the package that have the class name as a prefix.
173 Exclude_filter []string
174 }
175
Andreas Gampef3e5b552018-01-22 21:27:21 -0800176 Errorprone struct {
177 // List of javac flags that should only be used when running errorprone.
178 Javacflags []string
179 }
180
Colin Cross0f2ee152017-12-14 15:22:43 -0800181 Proto struct {
182 // List of extra options that will be passed to the proto generator.
183 Output_params []string
184 }
185
Inseob Kim42882742019-07-30 17:55:33 +0900186 Sysprop struct {
187 Platform *bool
188 } `blueprint:"mutated"`
189
Colin Crosscb933592017-11-22 13:49:43 -0800190 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800191
192 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800193 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700194}
195
Colin Cross89536d42017-07-07 14:35:50 -0700196type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700197 // list of module-specific flags that will be used for dex compiles
198 Dxflags []string `android:"arch_variant"`
199
Jeongik Cha538c0d02019-07-11 15:54:27 +0900200 // if not blank, set to the version of the sdk to compile against.
201 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800202 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700203
Colin Cross83bb3162018-06-25 15:48:06 -0700204 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
205 // Defaults to sdk_version if not set.
206 Min_sdk_version *string
207
Dan Willemsen419290a2018-10-31 15:28:47 -0700208 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
209 // Defaults to sdk_version if not set.
210 Target_sdk_version *string
211
Jeongik Cha356dac42019-08-19 14:09:52 +0900212 // Whether to compile against the platform APIs instead of an SDK.
213 // If true, then sdk_version must be empty. The value of this field
214 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700215 Platform_apis *bool
216
Colin Crossebe1a512017-11-14 13:12:14 -0800217 Aidl struct {
218 // Top level directories to pass to aidl tool
219 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700220
Colin Crossebe1a512017-11-14 13:12:14 -0800221 // Directories rooted at the Android.bp file to pass to aidl tool
222 Local_include_dirs []string
223
224 // directories that should be added as include directories for any aidl sources of modules
225 // that depend on this module, as well as to aidl for this module.
226 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100227
228 // whether to generate traces (for systrace) for this interface
229 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100230
231 // whether to generate Binder#GetTransaction name method.
232 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800233 }
Colin Cross92430102017-10-09 14:59:32 -0700234
235 // If true, export a copy of the module as a -hostdex module for host testing.
236 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700237
Colin Cross7f87f4f2019-04-24 13:41:45 -0700238 Target struct {
239 Hostdex struct {
240 // Additional required dependencies to add to -hostdex modules.
241 Required []string
242 }
243 }
244
David Brazdil17ef5632018-06-27 10:27:45 +0100245 // If set to true, compile dex regardless of installable. Defaults to false.
246 Compile_dex *bool
247
Colin Cross66dbc0b2017-12-28 12:23:20 -0800248 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700249 // If false, disable all optimization. Defaults to true for android_app and android_test
250 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800251 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700252 // True if the module containing this has it set by default.
253 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800254
255 // If true, optimize for size by removing unused code. Defaults to true for apps,
256 // false for libraries and tests.
257 Shrink *bool
258
259 // If true, optimize bytecode. Defaults to false.
260 Optimize *bool
261
262 // If true, obfuscate bytecode. Defaults to false.
263 Obfuscate *bool
264
265 // If true, do not use the flag files generated by aapt that automatically keep
266 // classes referenced by the app manifest. Defaults to false.
267 No_aapt_flags *bool
268
269 // Flags to pass to proguard.
270 Proguard_flags []string
271
272 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800273 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800274 }
275
Colin Cross1369cdb2017-09-29 17:58:17 -0700276 // When targeting 1.9, override the modules to use with --system
277 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700278
279 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800280 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700281}
282
Sasha Smundak2057f822019-04-16 17:16:58 -0700283func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
284 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
285}
286
Colin Cross46c9b8b2017-06-22 16:51:17 -0700287// Module contains the properties and members used by all java module types
288type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700289 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700290 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900291 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700292
Colin Cross89536d42017-07-07 14:35:50 -0700293 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700294 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700295 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700296
Colin Cross331a1212018-08-15 20:40:52 -0700297 // jar file containing header classes including static library dependencies, suitable for
298 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 headerJarFile android.Path
300
Colin Cross331a1212018-08-15 20:40:52 -0700301 // jar file containing implementation classes including static library dependencies but no
302 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700303 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700304
Colin Cross331a1212018-08-15 20:40:52 -0700305 // jar file containing only resources including from static library dependencies
306 resourceJar android.Path
307
Colin Cross0c4ce212019-05-03 15:28:19 -0700308 // args and dependencies to package source files into a srcjar
309 srcJarArgs []string
310 srcJarDeps android.Paths
311
Colin Cross331a1212018-08-15 20:40:52 -0700312 // jar file containing implementation classes and resources including static library
313 // dependencies
314 implementationAndResourcesJar android.Path
315
316 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700317 dexJarFile android.Path
318
Colin Cross43f08db2018-11-12 10:13:39 -0800319 // output file that contains classes.dex if it should be in the output file
320 maybeStrippedDexJarFile android.Path
321
Colin Crosscb933592017-11-22 13:49:43 -0800322 // output file containing uninstrumented classes that will be instrumented by jacoco
323 jacocoReportClassesFile android.Path
324
Colin Cross66dbc0b2017-12-28 12:23:20 -0800325 // output file containing mapping of obfuscated names
326 proguardDictionary android.Path
327
Colin Cross331a1212018-08-15 20:40:52 -0700328 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700329 outputFile android.Path
330 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700331
Colin Cross635c3b02016-05-18 15:37:25 -0700332 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700333
Colin Cross635c3b02016-05-18 15:37:25 -0700334 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700335
Colin Cross2fe66872015-03-30 17:20:39 -0700336 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700337 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800338
339 // list of .java files and srcjars that was passed to javac
340 compiledJavaSrcs android.Paths
341 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800342
343 // list of extra progurad flag files
344 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900345
Colin Cross094054a2018-10-17 15:10:48 -0700346 // manifest file to use instead of properties.Manifest
347 overrideManifest android.OptionalPath
348
Jiyong Park1be96912018-05-28 18:02:19 +0900349 // list of SDK lib names that this java moudule is exporting
350 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700351
352 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
353 // filter out Exclude_srcs, will be used by android.IDEInfo struct
354 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800355
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800356 // expanded Jarjar_rules
357 expandJarjarRules android.Path
358
Vladimir Marko0975ee02019-04-02 10:29:55 +0100359 // list of additional targets for checkbuild
360 additionalCheckedModules android.Paths
361
Colin Cross988708c2019-05-06 14:04:11 -0700362 // Extra files generated by the module type to be added as java resources.
363 extraResources android.Paths
364
Colin Crossf24a22a2019-01-31 14:12:44 -0800365 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800366 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800367
368 // list of the xref extraction files
369 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700370}
371
Colin Cross41955e82019-05-29 14:40:35 -0700372func (j *Module) OutputFiles(tag string) (android.Paths, error) {
373 switch tag {
374 case "":
375 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700376 case ".jar":
377 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700378 case ".proguard_map":
379 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700380 default:
381 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
382 }
Colin Cross54250902017-12-05 09:28:08 -0800383}
384
Jiyong Park8fd61922018-11-08 02:50:25 +0900385func (j *Module) DexJarFile() android.Path {
386 return j.dexJarFile
387}
388
Colin Cross41955e82019-05-29 14:40:35 -0700389var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800390
Colin Crossf506d872017-07-19 15:53:04 -0700391type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700392 HeaderJars() android.Paths
393 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700394 ResourceJars() android.Paths
395 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800396 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700397 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900398 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700399 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700400}
401
Jiyong Parkc678ad32018-04-10 13:07:10 +0900402type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700403 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
404 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900405}
406
Nan Zhangb2b33de2018-02-23 11:18:47 -0800407type SrcDependency interface {
408 CompiledSrcs() android.Paths
409 CompiledSrcJars() android.Paths
410}
411
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800412type xref interface {
413 XrefJavaFiles() android.Paths
414}
415
Nan Zhangb2b33de2018-02-23 11:18:47 -0800416func (j *Module) CompiledSrcs() android.Paths {
417 return j.compiledJavaSrcs
418}
419
420func (j *Module) CompiledSrcJars() android.Paths {
421 return j.compiledSrcJars
422}
423
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800424func (j *Module) XrefJavaFiles() android.Paths {
425 return j.kytheFiles
426}
427
Nan Zhangb2b33de2018-02-23 11:18:47 -0800428var _ SrcDependency = (*Module)(nil)
429
Colin Cross89536d42017-07-07 14:35:50 -0700430func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
431 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
432 android.InitDefaultableModule(module)
433}
434
Colin Crossbe1da472017-07-07 15:59:46 -0700435type dependencyTag struct {
436 blueprint.BaseDependencyTag
437 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700438}
439
Colin Crossa4f08812018-10-02 22:03:40 -0700440type jniDependencyTag struct {
441 blueprint.BaseDependencyTag
442 target android.Target
443}
444
Colin Crossbe1da472017-07-07 15:59:46 -0700445var (
Colin Cross4b964c02018-10-15 16:18:06 -0700446 staticLibTag = dependencyTag{name: "staticlib"}
447 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800448 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700449 bootClasspathTag = dependencyTag{name: "bootclasspath"}
450 systemModulesTag = dependencyTag{name: "system modules"}
451 frameworkResTag = dependencyTag{name: "framework-res"}
452 frameworkApkTag = dependencyTag{name: "framework-apk"}
453 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800454 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700455 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
456 certificateTag = dependencyTag{name: "certificate"}
457 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700458 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700459)
Colin Cross2fe66872015-03-30 17:20:39 -0700460
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900461func defaultSdkVersion(ctx checkVendorModuleContext) string {
462 if ctx.SocSpecific() || ctx.DeviceSpecific() {
463 return "system_current"
464 }
465 return ""
466}
467
468type checkVendorModuleContext interface {
469 SocSpecific() bool
470 DeviceSpecific() bool
471}
472
Colin Crossfc3674a2017-09-18 17:41:52 -0700473type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700474 useModule, useFiles, useDefaultLibs, invalidVersion bool
475
Colin Cross86a60ae2018-05-29 14:44:55 -0700476 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700477 systemModules string
478
Colin Crossa97c5d32018-03-28 14:58:31 -0700479 frameworkResModule string
480
Colin Cross86a60ae2018-05-29 14:44:55 -0700481 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700482 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100483
484 noStandardLibs, noFrameworksLibs bool
485}
486
487func (s sdkDep) hasStandardLibs() bool {
488 return !s.noStandardLibs
489}
490
491func (s sdkDep) hasFrameworkLibs() bool {
492 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700493}
494
Colin Crossa4f08812018-10-02 22:03:40 -0700495type jniLib struct {
496 name string
497 path android.Path
498 target android.Target
499}
500
Colin Cross0ea8ba82019-06-06 14:33:29 -0700501func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800502 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
503}
504
Colin Cross0ea8ba82019-06-06 14:33:29 -0700505func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800506 return j.shouldInstrument(ctx) &&
507 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
508 ctx.Config().UnbundledBuild())
509}
510
Colin Cross83bb3162018-06-25 15:48:06 -0700511func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900512 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700513}
514
515func (j *Module) minSdkVersion() string {
516 if j.deviceProperties.Min_sdk_version != nil {
517 return *j.deviceProperties.Min_sdk_version
518 }
519 return j.sdkVersion()
520}
521
Dan Willemsen419290a2018-10-31 15:28:47 -0700522func (j *Module) targetSdkVersion() string {
523 if j.deviceProperties.Target_sdk_version != nil {
524 return *j.deviceProperties.Target_sdk_version
525 }
526 return j.sdkVersion()
527}
528
Colin Crossbe1da472017-07-07 15:59:46 -0700529func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700530 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100531 sdkDep := decodeSdkDep(ctx, sdkContext(j))
532 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700533 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700534 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100535 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100536 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700537 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700538 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700539 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100540 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700541 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700542 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700543 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
544 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800545 }
Colin Crossbe1da472017-07-07 15:59:46 -0700546 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700547 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100548 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100549 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100550 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700551 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700552 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100553 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700554 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800555 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800556 if ctx.ModuleName() == "android_stubs_current" ||
557 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700558 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700559 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800560 }
Colin Cross2fe66872015-03-30 17:20:39 -0700561 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700562
Colin Cross42d48b72018-08-29 14:10:52 -0700563 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
564 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700565
Colin Crossbe9cdb82019-01-21 21:37:16 -0800566 ctx.AddFarVariationDependencies([]blueprint.Variation{
567 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
568 }, pluginTag, j.properties.Plugins...)
569
Colin Crossfe17f6f2019-03-28 19:30:56 -0700570 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700571 if j.hasSrcExt(".proto") {
572 protoDeps(ctx, &j.protoProperties)
573 }
Colin Cross93e85952017-08-15 13:34:18 -0700574
575 if j.hasSrcExt(".kt") {
576 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
577 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700578 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
579 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800580 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800581 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
582 }
Colin Cross93e85952017-08-15 13:34:18 -0700583 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800584
585 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700586 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800587 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700588}
589
590func hasSrcExt(srcs []string, ext string) bool {
591 for _, src := range srcs {
592 if filepath.Ext(src) == ext {
593 return true
594 }
595 }
596
597 return false
598}
599
Nan Zhang61eaedb2017-11-02 13:28:15 -0700600func shardPaths(paths android.Paths, shardSize int) []android.Paths {
601 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
602 for len(paths) > shardSize {
603 ret = append(ret, paths[0:shardSize])
604 paths = paths[shardSize:]
605 }
606 if len(paths) > 0 {
607 ret = append(ret, paths)
608 }
609 return ret
610}
611
Colin Cross6af17aa2017-09-20 12:59:05 -0700612func (j *Module) hasSrcExt(ext string) bool {
613 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700614}
615
Colin Cross46c9b8b2017-06-22 16:51:17 -0700616func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700617 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700618
Colin Crossebe1a512017-11-14 13:12:14 -0800619 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
620 aidlIncludes = append(aidlIncludes,
621 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
622 aidlIncludes = append(aidlIncludes,
623 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700624
Colin Cross3047fa22019-04-18 10:56:44 -0700625 var flags []string
626 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700627
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700628 if aidlPreprocess.Valid() {
629 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700630 deps = append(deps, aidlPreprocess.Path())
631 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700632 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700633 }
634
Colin Cross3047fa22019-04-18 10:56:44 -0700635 if len(j.exportAidlIncludeDirs) > 0 {
636 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
637 }
638
639 if len(aidlIncludes) > 0 {
640 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
641 }
642
Colin Cross635c3b02016-05-18 15:37:25 -0700643 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800644 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700645 flags = append(flags, "-I"+src.String())
646 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700647
Martijn Coeneneab15642018-03-09 09:29:59 +0100648 if Bool(j.deviceProperties.Aidl.Generate_traces) {
649 flags = append(flags, "-t")
650 }
651
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100652 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
653 flags = append(flags, "--transaction_names")
654 }
655
Colin Cross3047fa22019-04-18 10:56:44 -0700656 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700657}
658
Colin Cross32f676a2017-09-06 13:41:06 -0700659type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800660 classpath classpath
661 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700662 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800663 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700664 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700665 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700666 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700667 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800668 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700669 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700670 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700671 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700672 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800673 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800674
675 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700676}
Colin Cross2fe66872015-03-30 17:20:39 -0700677
Colin Cross54250902017-12-05 09:28:08 -0800678func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
679 for _, f := range dep.Srcs() {
680 if f.Ext() != ".jar" {
681 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
682 ctx.OtherModuleName(dep.(blueprint.Module)))
683 }
684 }
685}
686
Jiyong Park2d492942018-03-05 17:44:10 +0900687type linkType int
688
689const (
690 javaCore linkType = iota
691 javaSdk
692 javaSystem
693 javaPlatform
694)
695
Jiyong Park46f78fb2018-10-20 16:33:17 +0900696func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700697 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700698 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900699 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
700 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100701 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900702 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100703 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900704 return javaCore, false
705 case name == "android_system_stubs_current":
706 return javaSystem, true
707 case strings.HasPrefix(ver, "system_"):
708 return javaSystem, false
709 case name == "android_test_stubs_current":
710 return javaSystem, true
711 case strings.HasPrefix(ver, "test_"):
712 return javaPlatform, false
713 case name == "android_stubs_current":
714 return javaSdk, true
715 case ver == "current":
716 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100717 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900718 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700719 default:
720 if _, err := strconv.Atoi(ver); err != nil {
721 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
722 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900723 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900724 }
725}
726
Jiyong Park750e5572018-01-31 00:20:13 +0900727func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700728 if ctx.Host() {
729 return
730 }
731
Jiyong Park46f78fb2018-10-20 16:33:17 +0900732 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
733 if stubs {
734 return
735 }
736 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900737 commonMessage := "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source."
738
739 switch myLinkType {
740 case javaCore:
741 if otherLinkType != javaCore {
742 ctx.ModuleErrorf("compiles against core Java API, but dependency %q is compiling against non-core Java APIs."+commonMessage,
Jiyong Park750e5572018-01-31 00:20:13 +0900743 ctx.OtherModuleName(to))
744 }
Jiyong Park2d492942018-03-05 17:44:10 +0900745 break
746 case javaSdk:
747 if otherLinkType != javaCore && otherLinkType != javaSdk {
748 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
749 ctx.OtherModuleName(to))
750 }
751 break
752 case javaSystem:
753 if otherLinkType == javaPlatform {
754 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
755 ctx.OtherModuleName(to))
756 }
757 break
758 case javaPlatform:
759 // no restriction on link-type
760 break
Jiyong Park750e5572018-01-31 00:20:13 +0900761 }
762}
763
Colin Cross32f676a2017-09-06 13:41:06 -0700764func (j *Module) collectDeps(ctx android.ModuleContext) deps {
765 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700766
Colin Cross300f0382018-03-06 13:11:51 -0800767 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700768 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800769 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700770 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800771 } else if sdkDep.useFiles {
772 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700773 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700774 deps.aidlPreprocess = sdkDep.aidl
775 } else {
776 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800777 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700778 }
779
Colin Crossd11fcda2017-10-23 17:59:01 -0700780 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700781 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700782 tag := ctx.OtherModuleDependencyTag(module)
783
Colin Crossa4f08812018-10-02 22:03:40 -0700784 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700785 // Handled by AndroidApp.collectAppDeps
786 return
787 }
788 if tag == certificateTag {
789 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700790 return
791 }
792
Jiyong Park750e5572018-01-31 00:20:13 +0900793 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700794 switch tag {
795 case bootClasspathTag, libTag, staticLibTag:
796 checkLinkType(ctx, j, to, tag.(dependencyTag))
797 }
Jiyong Park750e5572018-01-31 00:20:13 +0900798 }
Colin Cross54250902017-12-05 09:28:08 -0800799 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800800 case SdkLibraryDependency:
801 switch tag {
802 case libTag:
803 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
804 // names of sdk libs that are directly depended are exported
805 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700806 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800807 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
808 }
Colin Cross54250902017-12-05 09:28:08 -0800809 case Dependency:
810 switch tag {
811 case bootClasspathTag:
812 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700813 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800814 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900815 // sdk lib names from dependencies are re-exported
816 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700817 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800818 case staticLibTag:
819 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
820 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
821 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700822 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900823 // sdk lib names from dependencies are re-exported
824 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700825 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800826 case pluginTag:
827 if plugin, ok := dep.(*Plugin); ok {
828 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
829 if plugin.pluginProperties.Processor_class != nil {
830 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
831 }
832 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
833 } else {
834 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
835 }
Colin Cross54250902017-12-05 09:28:08 -0800836 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100837 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800838 // framework.jar has a one-off dependency on the R.java and Manifest.java files
839 // generated by framework-res.apk
840 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
841 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800842 case frameworkApkTag:
843 if ctx.ModuleName() == "android_stubs_current" ||
844 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700845 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800846 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
847 // resource files out of there for aapt.
848 //
849 // Normally the package rule runs aapt, which includes the resource,
850 // but we're not running that in our package rule so just copy in the
851 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700852 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800853 }
Colin Cross54250902017-12-05 09:28:08 -0800854 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700855 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800856 case kotlinAnnotationsTag:
857 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800858 }
859
Colin Cross54250902017-12-05 09:28:08 -0800860 case android.SourceFileProducer:
861 switch tag {
862 case libTag:
863 checkProducesJars(ctx, dep)
864 deps.classpath = append(deps.classpath, dep.Srcs()...)
865 case staticLibTag:
866 checkProducesJars(ctx, dep)
867 deps.classpath = append(deps.classpath, dep.Srcs()...)
868 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
869 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800870 }
871 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700872 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700873 case systemModulesTag:
874 if deps.systemModules != nil {
875 panic("Found two system module dependencies")
876 }
877 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000878 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700879 panic("Missing directory for system module dependency")
880 }
Colin Crossb77043e2019-07-16 13:57:13 -0700881 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700882 }
Colin Crossec7a0422017-07-07 14:47:12 -0700883 }
Colin Cross2fe66872015-03-30 17:20:39 -0700884 })
885
Jiyong Park1be96912018-05-28 18:02:19 +0900886 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
887
Colin Cross32f676a2017-09-06 13:41:06 -0700888 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700889}
890
Colin Cross83bb3162018-06-25 15:48:06 -0700891func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700892 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800893 v := sdkContext.sdkVersion()
894 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100895 if ctx.Config().IsPdkBuild() &&
896 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700897 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800898 latestSdkVersion := 0
899 if len(sdkVersions) > 0 {
900 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
901 }
902 v = strconv.Itoa(latestSdkVersion)
903 }
904
905 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700906 if err != nil {
907 ctx.PropertyErrorf("sdk_version", "%s", err)
908 }
Nan Zhang357466b2018-04-17 17:38:36 -0700909 if javaVersion != "" {
910 ret = javaVersion
911 } else if ctx.Device() && sdk <= 23 {
912 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100913 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700914 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100915 } else if ctx.Device() &&
916 sdkContext.sdkVersion() != "" &&
917 sdkContext.sdkVersion() != "none" &&
918 sdkContext.sdkVersion() != "core_platform" &&
919 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700920 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
921 ret = "1.8"
922 } else {
923 ret = "1.9"
924 }
925
926 return ret
927}
928
Nan Zhanged19fc32017-10-19 13:06:22 -0700929func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700930
Colin Crossf03c82b2015-04-13 13:53:40 -0700931 var flags javaBuilderFlags
932
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100933 // javaVersion flag.
934 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
935
Nan Zhanged19fc32017-10-19 13:06:22 -0700936 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700937 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100938 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700939 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700940 }
Colin Cross6510f912017-11-29 00:27:14 -0800941 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700942 // Override the -g flag passed globally to remove local variable debug info to reduce
943 // disk and memory usage.
944 javacFlags = append(javacFlags, "-g:source,lines")
945 }
Colin Cross64162712017-08-08 13:17:59 -0700946
Colin Cross66548102018-06-19 22:47:35 -0700947 if ctx.Config().RunErrorProne() {
948 if config.ErrorProneClasspath == nil {
949 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
950 }
951
952 errorProneFlags := []string{
953 "-Xplugin:ErrorProne",
954 "${config.ErrorProneChecks}",
955 }
956 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
957
958 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
959 "'" + strings.Join(errorProneFlags, " ") + "'"
960 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800961 }
962
Nan Zhanged19fc32017-10-19 13:06:22 -0700963 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800964 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
965 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700966 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800967
Colin Crossbe9cdb82019-01-21 21:37:16 -0800968 flags.processor = strings.Join(deps.processorClasses, ",")
969
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100970 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100971 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800972 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
973 // Give host-side tools a version of OpenJDK's standard libraries
974 // close to what they're targeting. As of Dec 2017, AOSP is only
975 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
976 //
977 // When building with OpenJDK 8, the following should have no
978 // effect since those jars would be available by default.
979 //
980 // When building with OpenJDK 9 but targeting a version < 1.8,
981 // putting them on the bootclasspath means that:
982 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
983 // b) references to existing APIs are not reinterpreted in an
984 // OpenJDK 9-specific way, eg. calls to subclasses of
985 // java.nio.Buffer as in http://b/70862583
986 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
987 flags.bootClasspath = append(flags.bootClasspath,
988 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
989 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800990 if Bool(j.properties.Use_tools_jar) {
991 flags.bootClasspath = append(flags.bootClasspath,
992 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
993 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800994 }
995
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100996 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800997 // Manually specify build directory in case it is not under the repo root.
998 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
999 // just adding a symlink under the root doesn't help.)
1000 patchPaths := ".:" + ctx.Config().BuildDir()
1001 classPath := flags.classpath.FormJavaClassPath("")
1002 if classPath != "" {
1003 patchPaths += ":" + classPath
1004 }
1005 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001006 }
1007
Nan Zhanged19fc32017-10-19 13:06:22 -07001008 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001009 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001010
Nan Zhanged19fc32017-10-19 13:06:22 -07001011 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001012 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001013
Colin Cross81440082018-08-15 20:21:55 -07001014 if len(javacFlags) > 0 {
1015 // optimization.
1016 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1017 flags.javacFlags = "$javacFlags"
1018 }
1019
Nan Zhanged19fc32017-10-19 13:06:22 -07001020 return flags
1021}
Colin Crossc0b06f12015-04-08 13:03:43 -07001022
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001023func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
Colin Crossebe1a512017-11-14 13:12:14 -08001024 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001025
1026 deps := j.collectDeps(ctx)
1027 flags := j.collectBuilderFlags(ctx, deps)
1028
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001029 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001030 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1031 }
Colin Cross8a497952019-03-05 22:25:09 -08001032 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001033 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001034 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001035 }
1036
Colin Crossaf050172017-11-15 23:01:59 -08001037 srcFiles = j.genSources(ctx, srcFiles, flags)
1038
1039 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001040 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001041 if aaptSrcJar != nil {
1042 srcJars = append(srcJars, aaptSrcJar)
1043 }
Colin Crossb7a63242015-04-16 14:09:14 -07001044
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001045 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1046 // that IDEInfo struct will use
1047 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1048
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001049 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001050 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001051 }
1052
Colin Cross1ee23172017-10-18 14:44:18 -07001053 jarName := ctx.ModuleName() + ".jar"
1054
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001055 javaSrcFiles := srcFiles.FilterByExt(".java")
1056 var uniqueSrcFiles android.Paths
1057 set := make(map[string]bool)
1058 for _, v := range javaSrcFiles {
1059 if _, found := set[v.String()]; !found {
1060 set[v.String()] = true
1061 uniqueSrcFiles = append(uniqueSrcFiles, v)
1062 }
1063 }
1064
Colin Cross55f63ea2018-08-27 12:37:09 -07001065 var kotlinJars android.Paths
1066
Colin Cross93e85952017-08-15 13:34:18 -07001067 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001068 // user defined kotlin flags.
1069 kotlincFlags := j.properties.Kotlincflags
1070 CheckKotlincFlags(ctx, kotlincFlags)
1071
Colin Cross93e85952017-08-15 13:34:18 -07001072 // If there are kotlin files, compile them first but pass all the kotlin and java files
1073 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1074 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001075 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001076 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001077 kotlincFlags = append(kotlincFlags, "-no-jdk")
1078 }
1079 if len(kotlincFlags) > 0 {
1080 // optimization.
1081 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1082 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001083 }
1084
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001085 var kotlinSrcFiles android.Paths
1086 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1087 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1088
Colin Crossafbb1732019-01-17 15:42:52 -08001089 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1090 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1091
1092 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1093 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1094
1095 if len(flags.processorPath) > 0 {
1096 // Use kapt for annotation processing
1097 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1098 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1099 srcJars = append(srcJars, kaptSrcJar)
1100 // Disable annotation processing in javac, it's already been handled by kapt
1101 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001102 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001103 }
Colin Cross93e85952017-08-15 13:34:18 -07001104
Colin Cross1ee23172017-10-18 14:44:18 -07001105 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001106 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001107 if ctx.Failed() {
1108 return
1109 }
1110
1111 // Make javac rule depend on the kotlinc rule
1112 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001113
Colin Cross93e85952017-08-15 13:34:18 -07001114 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001115 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001116 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001117 }
1118
Colin Cross55f63ea2018-08-27 12:37:09 -07001119 jars := append(android.Paths(nil), kotlinJars...)
1120
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001121 // Store the list of .java files that was passed to javac
1122 j.compiledJavaSrcs = uniqueSrcFiles
1123 j.compiledSrcJars = srcJars
1124
Nan Zhang61eaedb2017-11-02 13:28:15 -07001125 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001126 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001127 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1128 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001129 // Formerly, there was a check here that prevented annotation processors
1130 // from being used when sharding was enabled, as some annotation processors
1131 // do not function correctly in sharded environments. It was removed to
1132 // allow for the use of annotation processors that do function correctly
1133 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001134 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001135 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001136 if ctx.Failed() {
1137 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001138 }
1139 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001140 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001141 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001142 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001143 // If error-prone is enabled, add an additional rule to compile the java files into
1144 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001145 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001146 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1147 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001148 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001149 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001150 extraJarDeps = append(extraJarDeps, errorprone)
1151 }
1152
Nan Zhang61eaedb2017-11-02 13:28:15 -07001153 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001154 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001155 shardSize := int(*(j.properties.Javac_shard_size))
1156 var shardSrcs []android.Paths
1157 if len(uniqueSrcFiles) > 0 {
1158 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1159 for idx, shardSrc := range shardSrcs {
1160 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1161 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1162 jars = append(jars, classes)
1163 }
1164 }
1165 if len(srcJars) > 0 {
1166 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1167 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1168 jars = append(jars, classes)
1169 }
1170 } else {
1171 classes := android.PathForModuleOut(ctx, "javac", jarName)
1172 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1173 jars = append(jars, classes)
1174 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001175 if ctx.Config().EmitXrefRules() {
1176 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1177 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1178 j.kytheFiles = append(j.kytheFiles, extractionFile)
1179
1180 }
Colin Crossd6891432017-09-27 17:39:56 -07001181 if ctx.Failed() {
1182 return
1183 }
Colin Cross2fe66872015-03-30 17:20:39 -07001184 }
1185
Colin Cross0c4ce212019-05-03 15:28:19 -07001186 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1187
1188 var includeSrcJar android.WritablePath
1189 if Bool(j.properties.Include_srcs) {
1190 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1191 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1192 }
1193
Colin Crosscedd4762018-09-13 11:26:19 -07001194 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1195 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001196 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001197 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001198
1199 var resArgs []string
1200 var resDeps android.Paths
1201
1202 resArgs = append(resArgs, dirArgs...)
1203 resDeps = append(resDeps, dirDeps...)
1204
1205 resArgs = append(resArgs, fileArgs...)
1206 resDeps = append(resDeps, fileDeps...)
1207
Colin Cross988708c2019-05-06 14:04:11 -07001208 resArgs = append(resArgs, extraArgs...)
1209 resDeps = append(resDeps, extraDeps...)
1210
Colin Cross40a36712017-09-27 17:41:35 -07001211 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001212 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001213 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001214 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001215 if ctx.Failed() {
1216 return
1217 }
1218 }
1219
Colin Cross0c4ce212019-05-03 15:28:19 -07001220 var resourceJars android.Paths
1221 if j.resourceJar != nil {
1222 resourceJars = append(resourceJars, j.resourceJar)
1223 }
1224 if Bool(j.properties.Include_srcs) {
1225 resourceJars = append(resourceJars, includeSrcJar)
1226 }
1227 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001228
Colin Cross0c4ce212019-05-03 15:28:19 -07001229 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001230 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001231 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001232 false, nil, nil)
1233 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001234 } else if len(resourceJars) == 1 {
1235 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001236 }
1237
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001238 if len(deps.staticJars) > 0 {
1239 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001240 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001241
Colin Cross094054a2018-10-17 15:10:48 -07001242 manifest := j.overrideManifest
1243 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001244 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001245 }
Colin Cross635acc92017-09-12 22:50:46 -07001246
Colin Cross8a497952019-03-05 22:25:09 -08001247 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001248 if len(services) > 0 {
1249 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1250 var zipargs []string
1251 for _, file := range services {
1252 serviceFile := file.String()
1253 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1254 }
1255 ctx.Build(pctx, android.BuildParams{
1256 Rule: zip,
1257 Output: servicesJar,
1258 Implicits: services,
1259 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001260 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001261 },
1262 })
1263 jars = append(jars, servicesJar)
1264 }
1265
Colin Cross0a6e0072017-08-30 14:24:55 -07001266 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001267 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001268 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001269
1270 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001271 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1272 // Optimization: skip the combine step if there is nothing to do
1273 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1274 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1275 // any if len(jars) == 1.
1276 outputFile = moduleOutPath
1277 } else {
1278 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1279 ctx.Build(pctx, android.BuildParams{
1280 Rule: android.Cp,
1281 Input: jars[0],
1282 Output: combinedJar,
1283 })
1284 outputFile = combinedJar
1285 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001286 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001287 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001288 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001289 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001290 outputFile = combinedJar
1291 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001292
Colin Cross331a1212018-08-15 20:40:52 -07001293 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001294 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001295 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001296 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001297 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001298 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001299
1300 // jarjar resource jar if necessary
1301 if j.resourceJar != nil {
1302 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001303 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001304 j.resourceJar = resourceJarJarFile
1305 }
1306
Colin Cross0a6e0072017-08-30 14:24:55 -07001307 if ctx.Failed() {
1308 return
1309 }
1310 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001311
1312 // Check package restrictions if necessary.
1313 if len(j.properties.Permitted_packages) > 0 {
1314 // Check packages and copy to package-checked file.
1315 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1316 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1317 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1318
1319 if ctx.Failed() {
1320 return
1321 }
1322 }
1323
Nan Zhanged19fc32017-10-19 13:06:22 -07001324 j.implementationJarFile = outputFile
1325 if j.headerJarFile == nil {
1326 j.headerJarFile = j.implementationJarFile
1327 }
Colin Cross2fe66872015-03-30 17:20:39 -07001328
Colin Cross6510f912017-11-29 00:27:14 -08001329 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001330 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1331 j.properties.Instrument = true
1332 }
1333 }
1334
Colin Cross3144dfc2018-01-03 15:06:47 -08001335 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001336 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1337 }
1338
Colin Cross331a1212018-08-15 20:40:52 -07001339 // merge implementation jar with resources if necessary
1340 implementationAndResourcesJar := outputFile
1341 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001342 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001343 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001344 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001345 false, nil, nil)
1346 implementationAndResourcesJar = combinedJar
1347 }
1348
1349 j.implementationAndResourcesJar = implementationAndResourcesJar
1350
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001351 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001352 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001353 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001354 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001355 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001356 if ctx.Failed() {
1357 return
1358 }
Colin Cross331a1212018-08-15 20:40:52 -07001359
Jiyong Park09cb6292019-07-15 15:29:23 +09001360 // Hidden API CSV generation and dex encoding
1361 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1362 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001363
Colin Cross331a1212018-08-15 20:40:52 -07001364 // merge dex jar with resources if necessary
1365 if j.resourceJar != nil {
1366 jars := android.Paths{dexOutputFile, j.resourceJar}
1367 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1368 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1369 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001370 if j.deviceProperties.UncompressDex {
1371 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1372 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1373 dexOutputFile = combinedAlignedJar
1374 } else {
1375 dexOutputFile = combinedJar
1376 }
Colin Cross331a1212018-08-15 20:40:52 -07001377 }
1378
1379 j.dexJarFile = dexOutputFile
1380
Colin Cross8faf8fc2019-01-16 15:15:52 -08001381 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001382 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1383
1384 j.maybeStrippedDexJarFile = dexOutputFile
1385
Colin Cross3063b782018-08-15 11:19:12 -07001386 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001387
1388 if ctx.Failed() {
1389 return
1390 }
Colin Cross331a1212018-08-15 20:40:52 -07001391 } else {
1392 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001393 }
Colin Cross331a1212018-08-15 20:40:52 -07001394
Colin Crossb7a63242015-04-16 14:09:14 -07001395 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001396
1397 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1398 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001399}
1400
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001401// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1402// since some of these flags may be used internally.
1403func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1404 for _, flag := range flags {
1405 flag = strings.TrimSpace(flag)
1406
1407 if !strings.HasPrefix(flag, "-") {
1408 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1409 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1410 ctx.PropertyErrorf("kotlincflags",
1411 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1412 } else if inList(flag, config.KotlincIllegalFlags) {
1413 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1414 } else if flag == "-include-runtime" {
1415 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1416 } else {
1417 args := strings.Split(flag, " ")
1418 if args[0] == "-kotlin-home" {
1419 ctx.PropertyErrorf("kotlincflags",
1420 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1421 }
1422 }
1423 }
1424}
1425
Colin Cross8eadbf02017-10-24 17:46:00 -07001426func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001427 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001428
1429 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001430 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001431 // Compile java sources into turbine.jar.
1432 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1433 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1434 if ctx.Failed() {
1435 return nil
1436 }
1437 jars = append(jars, turbineJar)
1438 }
1439
Colin Cross55f63ea2018-08-27 12:37:09 -07001440 jars = append(jars, extraJars...)
1441
Nan Zhanged19fc32017-10-19 13:06:22 -07001442 // Combine any static header libraries into classes-header.jar. If there is only
1443 // one input jar this step will be skipped.
1444 var headerJar android.Path
1445 jars = append(jars, deps.staticHeaderJars...)
1446
Colin Cross5c6ecc12017-10-23 18:12:27 -07001447 // we cannot skip the combine step for now if there is only one jar
1448 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1449 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001450 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001451 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001452 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001453
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001454 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001455 // Transform classes.jar into classes-jarjar.jar
1456 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001457 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001458 headerJar = jarjarFile
1459 if ctx.Failed() {
1460 return nil
1461 }
1462 }
1463
1464 return headerJar
1465}
1466
Colin Crosscb933592017-11-22 13:49:43 -08001467func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001468 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001469
Colin Cross7a3139e2017-12-19 13:57:50 -08001470 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001471
Colin Cross84c38822018-01-03 15:59:46 -08001472 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001473 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1474
1475 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1476
1477 j.jacocoReportClassesFile = jacocoReportClassesFile
1478
1479 return instrumentedJar
1480}
1481
albaltai36ff7dc2018-12-25 14:35:23 +08001482var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001483
Nan Zhanged19fc32017-10-19 13:06:22 -07001484func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001485 if j.headerJarFile == nil {
1486 return nil
1487 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001488 return android.Paths{j.headerJarFile}
1489}
1490
1491func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001492 if j.implementationJarFile == nil {
1493 return nil
1494 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001495 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001496}
1497
Colin Crossf24a22a2019-01-31 14:12:44 -08001498func (j *Module) DexJar() android.Path {
1499 return j.dexJarFile
1500}
1501
Colin Cross331a1212018-08-15 20:40:52 -07001502func (j *Module) ResourceJars() android.Paths {
1503 if j.resourceJar == nil {
1504 return nil
1505 }
1506 return android.Paths{j.resourceJar}
1507}
1508
1509func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001510 if j.implementationAndResourcesJar == nil {
1511 return nil
1512 }
Colin Cross331a1212018-08-15 20:40:52 -07001513 return android.Paths{j.implementationAndResourcesJar}
1514}
1515
Colin Cross46c9b8b2017-06-22 16:51:17 -07001516func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001517 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001518 return j.exportAidlIncludeDirs
1519}
1520
Jiyong Park1be96912018-05-28 18:02:19 +09001521func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001522 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001523 return j.exportedSdkLibs
1524}
1525
Colin Cross0c4ce212019-05-03 15:28:19 -07001526func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1527 return j.srcJarArgs, j.srcJarDeps
1528}
1529
Colin Cross46c9b8b2017-06-22 16:51:17 -07001530var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001531
Colin Cross46c9b8b2017-06-22 16:51:17 -07001532func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001533 return j.logtagsSrcs
1534}
1535
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001536// Collect information for opening IDE project files in java/jdeps.go.
1537func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1538 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1539 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001540 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001541 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001542 if j.expandJarjarRules != nil {
1543 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001544 }
1545}
1546
1547func (j *Module) CompilerDeps() []string {
1548 jdeps := []string{}
1549 jdeps = append(jdeps, j.properties.Libs...)
1550 jdeps = append(jdeps, j.properties.Static_libs...)
1551 return jdeps
1552}
1553
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001554func (j *Module) hasCode(ctx android.ModuleContext) bool {
1555 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1556 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1557}
1558
Colin Cross2fe66872015-03-30 17:20:39 -07001559//
1560// Java libraries (.jar file)
1561//
1562
Colin Crossf506d872017-07-19 15:53:04 -07001563type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001564 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001565}
1566
Colin Cross42be7612019-02-21 18:12:14 -08001567func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001568 // Store uncompressed (and do not strip) dex files from boot class path jars.
1569 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1570 return true
1571 }
1572
1573 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001574 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001575 return true
1576 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001577 if ctx.Config().UncompressPrivAppDex() &&
1578 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1579 return true
1580 }
1581
Colin Cross2fc72f62018-12-21 12:59:54 -08001582 return false
1583}
1584
Colin Crossf506d872017-07-19 15:53:04 -07001585func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001586 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1587 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001588 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001589 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001590 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001591 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001592
Jiyong Park7f7766d2019-07-25 22:02:35 +09001593 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1594 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001595 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1596 ctx.ModuleName()+".jar", j.outputFile)
1597 }
Colin Crossb7a63242015-04-16 14:09:14 -07001598}
1599
Colin Crossf506d872017-07-19 15:53:04 -07001600func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001601 j.deps(ctx)
1602}
1603
Colin Cross1b16b0e2019-02-12 14:41:32 -08001604// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1605//
1606// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1607// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1608// as a `static_libs` dependency of another module.
1609//
1610// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1611// a device.
1612//
1613// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1614// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001615func LibraryFactory() android.Module {
1616 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001617
Colin Cross9ae1b922018-06-26 17:59:05 -07001618 module.AddProperties(
1619 &module.Module.properties,
1620 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001621 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001622 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001623
Colin Cross9ae1b922018-06-26 17:59:05 -07001624 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001625 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001626 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001627}
1628
Colin Cross1b16b0e2019-02-12 14:41:32 -08001629// java_library_static is an obsolete alias for java_library.
1630func LibraryStaticFactory() android.Module {
1631 return LibraryFactory()
1632}
1633
1634// java_library_host builds and links sources into a `.jar` file for the host.
1635//
1636// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1637// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001638func LibraryHostFactory() android.Module {
1639 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001640
Colin Cross6af17aa2017-09-20 12:59:05 -07001641 module.AddProperties(
1642 &module.Module.properties,
1643 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001644
Colin Cross9ae1b922018-06-26 17:59:05 -07001645 module.Module.properties.Installable = proptools.BoolPtr(true)
1646
Colin Cross89536d42017-07-07 14:35:50 -07001647 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001648 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001649 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001650}
1651
1652//
Colin Crossb628ea52018-08-14 16:42:33 -07001653// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001654//
1655
1656type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001657 // list of compatibility suites (for example "cts", "vts") that the module should be
1658 // installed into.
1659 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001660
1661 // the name of the test configuration (for example "AndroidTest.xml") that should be
1662 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001663 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001664
Jack He33338892018-09-19 02:21:28 -07001665 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1666 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001667 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001668
Colin Crossd96ca352018-08-10 16:06:24 -07001669 // list of files or filegroup modules that provide data that should be installed alongside
1670 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001671 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001672}
1673
Paul Duffin42df1442019-03-20 12:45:53 +00001674type testHelperLibraryProperties struct {
1675 // list of compatibility suites (for example "cts", "vts") that the module should be
1676 // installed into.
1677 Test_suites []string `android:"arch_variant"`
1678}
1679
Colin Cross05638fc2018-04-09 18:40:24 -07001680type Test struct {
1681 Library
1682
1683 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001684
1685 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001686 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001687}
1688
Paul Duffin42df1442019-03-20 12:45:53 +00001689type TestHelperLibrary struct {
1690 Library
1691
1692 testHelperLibraryProperties testHelperLibraryProperties
1693}
1694
Colin Cross303e21f2018-08-07 16:49:25 -07001695func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001696 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 -08001697 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001698
1699 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001700}
1701
Paul Duffin42df1442019-03-20 12:45:53 +00001702func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1703 j.Library.GenerateAndroidBuildActions(ctx)
1704}
1705
Colin Cross1b16b0e2019-02-12 14:41:32 -08001706// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1707// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1708//
1709// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1710// compiled against the device bootclasspath.
1711//
1712// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1713// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001714func TestFactory() android.Module {
1715 module := &Test{}
1716
1717 module.AddProperties(
1718 &module.Module.properties,
1719 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001720 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001721 &module.Module.protoProperties,
1722 &module.testProperties)
1723
Colin Cross9ae1b922018-06-26 17:59:05 -07001724 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001725 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001726
Colin Cross05638fc2018-04-09 18:40:24 -07001727 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001728 return module
1729}
1730
Paul Duffin42df1442019-03-20 12:45:53 +00001731// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1732func TestHelperLibraryFactory() android.Module {
1733 module := &TestHelperLibrary{}
1734
1735 module.AddProperties(
1736 &module.Module.properties,
1737 &module.Module.deviceProperties,
1738 &module.Module.dexpreoptProperties,
1739 &module.Module.protoProperties,
1740 &module.testHelperLibraryProperties)
1741
Colin Cross9a4abed2019-04-24 13:19:28 -07001742 module.Module.properties.Installable = proptools.BoolPtr(true)
1743 module.Module.dexpreopter.isTest = true
1744
Paul Duffin42df1442019-03-20 12:45:53 +00001745 InitJavaModule(module, android.HostAndDeviceSupported)
1746 return module
1747}
1748
Colin Cross1b16b0e2019-02-12 14:41:32 -08001749// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1750// allow running the test with `atest` or a `TEST_MAPPING` file.
1751//
1752// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1753// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001754func TestHostFactory() android.Module {
1755 module := &Test{}
1756
1757 module.AddProperties(
1758 &module.Module.properties,
1759 &module.Module.protoProperties,
1760 &module.testProperties)
1761
Colin Cross9ae1b922018-06-26 17:59:05 -07001762 module.Module.properties.Installable = proptools.BoolPtr(true)
1763
Colin Cross05638fc2018-04-09 18:40:24 -07001764 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001765 return module
1766}
1767
1768//
Colin Cross2fe66872015-03-30 17:20:39 -07001769// Java Binaries (.jar file plus wrapper script)
1770//
1771
Colin Crossf506d872017-07-19 15:53:04 -07001772type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001773 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001774 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001775
1776 // Name of the class containing main to be inserted into the manifest as Main-Class.
1777 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001778}
1779
Colin Crossf506d872017-07-19 15:53:04 -07001780type Binary struct {
1781 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001782
Colin Crossf506d872017-07-19 15:53:04 -07001783 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001784
Colin Cross6b4a32d2017-12-05 13:42:45 -08001785 isWrapperVariant bool
1786
Colin Crossc3315992017-12-08 19:12:36 -08001787 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001788 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001789}
1790
Alex Light24237172017-10-26 09:46:21 -07001791func (j *Binary) HostToolPath() android.OptionalPath {
1792 return android.OptionalPathForPath(j.binaryFile)
1793}
1794
Colin Crossf506d872017-07-19 15:53:04 -07001795func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001796 if ctx.Arch().ArchType == android.Common {
1797 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001798 if j.binaryProperties.Main_class != nil {
1799 if j.properties.Manifest != nil {
1800 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1801 }
1802 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1803 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1804 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1805 }
1806
Colin Cross6b4a32d2017-12-05 13:42:45 -08001807 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001808 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001809 // Handle the binary wrapper
1810 j.isWrapperVariant = true
1811
Colin Cross366938f2017-12-11 16:29:02 -08001812 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001813 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001814 } else {
1815 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1816 }
1817
1818 // Depend on the installed jar so that the wrapper doesn't get executed by
1819 // another build rule before the jar has been installed.
1820 jarFile := ctx.PrimaryModule().(*Binary).installFile
1821
1822 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1823 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001824 }
Colin Cross2fe66872015-03-30 17:20:39 -07001825}
1826
Colin Crossf506d872017-07-19 15:53:04 -07001827func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001828 if ctx.Arch().ArchType == android.Common {
1829 j.deps(ctx)
1830 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001831}
1832
Colin Cross1b16b0e2019-02-12 14:41:32 -08001833// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1834// as well.
1835//
1836// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1837// compiled against the device bootclasspath.
1838//
1839// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1840// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001841func BinaryFactory() android.Module {
1842 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001843
Colin Cross36242852017-06-23 15:06:31 -07001844 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001845 &module.Module.properties,
1846 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001847 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001848 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001849 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001850
Colin Cross9ae1b922018-06-26 17:59:05 -07001851 module.Module.properties.Installable = proptools.BoolPtr(true)
1852
Colin Cross6b4a32d2017-12-05 13:42:45 -08001853 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1854 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001855 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001856}
1857
Colin Cross1b16b0e2019-02-12 14:41:32 -08001858// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1859//
1860// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1861// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001862func BinaryHostFactory() android.Module {
1863 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001864
Colin Cross36242852017-06-23 15:06:31 -07001865 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001866 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001867 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001868 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001869
Colin Cross9ae1b922018-06-26 17:59:05 -07001870 module.Module.properties.Installable = proptools.BoolPtr(true)
1871
Colin Cross6b4a32d2017-12-05 13:42:45 -08001872 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1873 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001874 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001875}
1876
1877//
1878// Java prebuilts
1879//
1880
Colin Cross74d73e22017-08-02 11:05:49 -07001881type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001882 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001883
Nan Zhangea568a42017-11-08 21:20:04 -08001884 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001885
1886 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001887
1888 // List of shared java libs that this module has dependencies to
1889 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001890
1891 // List of files to remove from the jar file(s)
1892 Exclude_files []string
1893
1894 // List of directories to remove from the jar file(s)
1895 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001896
1897 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001898 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001899}
1900
1901type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001902 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001903 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001904 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001905 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001906
Colin Cross74d73e22017-08-02 11:05:49 -07001907 properties ImportProperties
1908
Colin Cross0a6e0072017-08-30 14:24:55 -07001909 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001910 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001911}
1912
Colin Cross83bb3162018-06-25 15:48:06 -07001913func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001914 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001915}
1916
1917func (j *Import) minSdkVersion() string {
1918 return j.sdkVersion()
1919}
1920
Colin Cross74d73e22017-08-02 11:05:49 -07001921func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001922 return &j.prebuilt
1923}
1924
Colin Cross74d73e22017-08-02 11:05:49 -07001925func (j *Import) PrebuiltSrcs() []string {
1926 return j.properties.Jars
1927}
1928
1929func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001930 return j.prebuilt.Name(j.ModuleBase.Name())
1931}
1932
Colin Cross74d73e22017-08-02 11:05:49 -07001933func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001934 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001935}
1936
Colin Cross74d73e22017-08-02 11:05:49 -07001937func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001938 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001939
Nan Zhang4c819fb2018-08-27 18:31:46 -07001940 jarName := ctx.ModuleName() + ".jar"
1941 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001942 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1943 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001944 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001945 inputFile := outputFile
1946 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1947 TransformJetifier(ctx, outputFile, inputFile)
1948 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001949 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001950
1951 ctx.VisitDirectDeps(func(module android.Module) {
1952 otherName := ctx.OtherModuleName(module)
1953 tag := ctx.OtherModuleDependencyTag(module)
1954
1955 switch dep := module.(type) {
1956 case Dependency:
1957 switch tag {
1958 case libTag, staticLibTag:
1959 // sdk lib names from dependencies are re-exported
1960 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1961 }
1962 case SdkLibraryDependency:
1963 switch tag {
1964 case libTag:
1965 // names of sdk libs that are directly depended are exported
1966 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1967 }
1968 }
1969 })
1970
1971 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001972 if Bool(j.properties.Installable) {
1973 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1974 ctx.ModuleName()+".jar", outputFile)
1975 }
Colin Cross2fe66872015-03-30 17:20:39 -07001976}
1977
Colin Cross74d73e22017-08-02 11:05:49 -07001978var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001979
Nan Zhanged19fc32017-10-19 13:06:22 -07001980func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001981 if j.combinedClasspathFile == nil {
1982 return nil
1983 }
Colin Cross37f6d792018-07-12 12:28:41 -07001984 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001985}
1986
1987func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001988 if j.combinedClasspathFile == nil {
1989 return nil
1990 }
Colin Cross37f6d792018-07-12 12:28:41 -07001991 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001992}
1993
Colin Cross331a1212018-08-15 20:40:52 -07001994func (j *Import) ResourceJars() android.Paths {
1995 return nil
1996}
1997
1998func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001999 if j.combinedClasspathFile == nil {
2000 return nil
2001 }
Colin Cross331a1212018-08-15 20:40:52 -07002002 return android.Paths{j.combinedClasspathFile}
2003}
2004
Colin Crossf24a22a2019-01-31 14:12:44 -08002005func (j *Import) DexJar() android.Path {
2006 return nil
2007}
2008
Colin Cross74d73e22017-08-02 11:05:49 -07002009func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002010 return nil
2011}
2012
Jiyong Park1be96912018-05-28 18:02:19 +09002013func (j *Import) ExportedSdkLibs() []string {
2014 return j.exportedSdkLibs
2015}
2016
Colin Cross0c4ce212019-05-03 15:28:19 -07002017func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2018 return nil, nil
2019}
2020
albaltai36ff7dc2018-12-25 14:35:23 +08002021// Add compile time check for interface implementation
2022var _ android.IDEInfo = (*Import)(nil)
2023var _ android.IDECustomizedModuleName = (*Import)(nil)
2024
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002025// Collect information for opening IDE project files in java/jdeps.go.
2026const (
2027 removedPrefix = "prebuilt_"
2028)
2029
2030func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2031 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2032}
2033
2034func (j *Import) IDECustomizedModuleName() string {
2035 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2036 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2037 // solution to get the Import name.
2038 name := j.Name()
2039 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002040 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002041 }
2042 return name
2043}
2044
Colin Cross74d73e22017-08-02 11:05:49 -07002045var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002046
Colin Cross1b16b0e2019-02-12 14:41:32 -08002047// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2048//
2049// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2050// compiled against an Android classpath.
2051//
2052// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2053// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002054func ImportFactory() android.Module {
2055 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002056
Colin Cross74d73e22017-08-02 11:05:49 -07002057 module.AddProperties(&module.properties)
2058
2059 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002060 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002061 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002062 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002063}
2064
Colin Cross1b16b0e2019-02-12 14:41:32 -08002065// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2066// module.
2067//
2068// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2069// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002070func ImportFactoryHost() android.Module {
2071 module := &Import{}
2072
2073 module.AddProperties(&module.properties)
2074
2075 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002076 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002077 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002078 return module
2079}
2080
Colin Cross42be7612019-02-21 18:12:14 -08002081// dex_import module
2082
2083type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002084 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002085}
2086
2087type DexImport struct {
2088 android.ModuleBase
2089 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002090 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002091 prebuilt android.Prebuilt
2092
2093 properties DexImportProperties
2094
2095 dexJarFile android.Path
2096 maybeStrippedDexJarFile android.Path
2097
2098 dexpreopter
2099}
2100
2101func (j *DexImport) Prebuilt() *android.Prebuilt {
2102 return &j.prebuilt
2103}
2104
2105func (j *DexImport) PrebuiltSrcs() []string {
2106 return j.properties.Jars
2107}
2108
2109func (j *DexImport) Name() string {
2110 return j.prebuilt.Name(j.ModuleBase.Name())
2111}
2112
Colin Cross42be7612019-02-21 18:12:14 -08002113func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2114 if len(j.properties.Jars) != 1 {
2115 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2116 }
2117
2118 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2119 j.dexpreopter.isInstallable = true
2120 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2121
2122 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2123 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2124
2125 if j.dexpreopter.uncompressedDex {
2126 rule := android.NewRuleBuilder()
2127
2128 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2129 rule.Temporary(temporary)
2130
2131 // use zip2zip to uncompress classes*.dex files
2132 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002133 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002134 FlagWithInput("-i ", inputJar).
2135 FlagWithOutput("-o ", temporary).
2136 FlagWithArg("-0 ", "'classes*.dex'")
2137
2138 // use zipalign to align uncompressed classes*.dex files
2139 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002140 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002141 Flag("-f").
2142 Text("4").
2143 Input(temporary).
2144 Output(dexOutputFile)
2145
2146 rule.DeleteTemporaryFiles()
2147
2148 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2149 } else {
2150 ctx.Build(pctx, android.BuildParams{
2151 Rule: android.Cp,
2152 Input: inputJar,
2153 Output: dexOutputFile,
2154 })
2155 }
2156
2157 j.dexJarFile = dexOutputFile
2158
2159 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2160
2161 j.maybeStrippedDexJarFile = dexOutputFile
2162
2163 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2164 ctx.ModuleName()+".jar", dexOutputFile)
2165}
2166
2167func (j *DexImport) DexJar() android.Path {
2168 return j.dexJarFile
2169}
2170
2171// dex_import imports a `.jar` file containing classes.dex files.
2172//
2173// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2174// to the device.
2175func DexImportFactory() android.Module {
2176 module := &DexImport{}
2177
2178 module.AddProperties(&module.properties)
2179
2180 android.InitPrebuiltModule(module, &module.properties.Jars)
2181 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002182 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002183 return module
2184}
2185
Colin Cross89536d42017-07-07 14:35:50 -07002186//
2187// Defaults
2188//
2189type Defaults struct {
2190 android.ModuleBase
2191 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002192 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002193}
2194
Colin Cross1b16b0e2019-02-12 14:41:32 -08002195// java_defaults provides a set of properties that can be inherited by other java or android modules.
2196//
2197// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2198// property in the defaults module that exists in the depending module will be prepended to the depending module's
2199// value for that property.
2200//
2201// Example:
2202//
2203// java_defaults {
2204// name: "example_defaults",
2205// srcs: ["common/**/*.java"],
2206// javacflags: ["-Xlint:all"],
2207// aaptflags: ["--auto-add-overlay"],
2208// }
2209//
2210// java_library {
2211// name: "example",
2212// defaults: ["example_defaults"],
2213// srcs: ["example/**/*.java"],
2214// }
2215//
2216// is functionally identical to:
2217//
2218// java_library {
2219// name: "example",
2220// srcs: [
2221// "common/**/*.java",
2222// "example/**/*.java",
2223// ],
2224// javacflags: ["-Xlint:all"],
2225// }
Colin Cross89536d42017-07-07 14:35:50 -07002226func defaultsFactory() android.Module {
2227 return DefaultsFactory()
2228}
2229
2230func DefaultsFactory(props ...interface{}) android.Module {
2231 module := &Defaults{}
2232
2233 module.AddProperties(props...)
2234 module.AddProperties(
2235 &CompilerProperties{},
2236 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002237 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002238 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002239 &aaptProperties{},
2240 &androidLibraryProperties{},
2241 &appProperties{},
2242 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002243 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002244 &ImportProperties{},
2245 &AARImportProperties{},
2246 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002247 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002248 )
2249
2250 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002251 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002252 return module
2253}
Nan Zhangea568a42017-11-08 21:20:04 -08002254
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002255func kytheExtractJavaFactory() android.Singleton {
2256 return &kytheExtractJavaSingleton{}
2257}
2258
2259type kytheExtractJavaSingleton struct {
2260}
2261
2262func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2263 var xrefTargets android.Paths
2264 ctx.VisitAllModules(func(module android.Module) {
2265 if javaModule, ok := module.(xref); ok {
2266 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2267 }
2268 })
2269 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2270 if len(xrefTargets) > 0 {
2271 ctx.Build(pctx, android.BuildParams{
2272 Rule: blueprint.Phony,
2273 Output: android.PathForPhony(ctx, "xref_java"),
2274 Inputs: xrefTargets,
2275 })
2276 }
2277}
2278
Nan Zhangea568a42017-11-08 21:20:04 -08002279var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002280var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002281var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002282var inList = android.InList