blob: afb121871fe06641a988f545d2c46dd6badaae1b [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
Colin Crosscb933592017-11-22 13:49:43 -0800186 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800187
188 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800189 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700190}
191
Colin Cross89536d42017-07-07 14:35:50 -0700192type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700193 // list of module-specific flags that will be used for dex compiles
194 Dxflags []string `android:"arch_variant"`
195
Jeongik Cha538c0d02019-07-11 15:54:27 +0900196 // if not blank, set to the version of the sdk to compile against.
197 // Defaults to compiling against the current platform.
Nan Zhangea568a42017-11-08 21:20:04 -0800198 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700199
Colin Cross83bb3162018-06-25 15:48:06 -0700200 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
201 // Defaults to sdk_version if not set.
202 Min_sdk_version *string
203
Dan Willemsen419290a2018-10-31 15:28:47 -0700204 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
205 // Defaults to sdk_version if not set.
206 Target_sdk_version *string
207
Jeongik Cha356dac42019-08-19 14:09:52 +0900208 // Whether to compile against the platform APIs instead of an SDK.
209 // If true, then sdk_version must be empty. The value of this field
210 // is ignored when module's type isn't android_app.
Colin Cross6af2e492018-05-22 11:12:33 -0700211 Platform_apis *bool
212
Colin Crossebe1a512017-11-14 13:12:14 -0800213 Aidl struct {
214 // Top level directories to pass to aidl tool
215 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700216
Colin Crossebe1a512017-11-14 13:12:14 -0800217 // Directories rooted at the Android.bp file to pass to aidl tool
218 Local_include_dirs []string
219
220 // directories that should be added as include directories for any aidl sources of modules
221 // that depend on this module, as well as to aidl for this module.
222 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100223
224 // whether to generate traces (for systrace) for this interface
225 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100226
227 // whether to generate Binder#GetTransaction name method.
228 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800229 }
Colin Cross92430102017-10-09 14:59:32 -0700230
231 // If true, export a copy of the module as a -hostdex module for host testing.
232 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700233
Colin Cross7f87f4f2019-04-24 13:41:45 -0700234 Target struct {
235 Hostdex struct {
236 // Additional required dependencies to add to -hostdex modules.
237 Required []string
238 }
239 }
240
David Brazdil17ef5632018-06-27 10:27:45 +0100241 // If set to true, compile dex regardless of installable. Defaults to false.
242 Compile_dex *bool
243
Colin Cross66dbc0b2017-12-28 12:23:20 -0800244 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700245 // If false, disable all optimization. Defaults to true for android_app and android_test
246 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800247 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700248 // True if the module containing this has it set by default.
249 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800250
251 // If true, optimize for size by removing unused code. Defaults to true for apps,
252 // false for libraries and tests.
253 Shrink *bool
254
255 // If true, optimize bytecode. Defaults to false.
256 Optimize *bool
257
258 // If true, obfuscate bytecode. Defaults to false.
259 Obfuscate *bool
260
261 // If true, do not use the flag files generated by aapt that automatically keep
262 // classes referenced by the app manifest. Defaults to false.
263 No_aapt_flags *bool
264
265 // Flags to pass to proguard.
266 Proguard_flags []string
267
268 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800269 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800270 }
271
Colin Cross1369cdb2017-09-29 17:58:17 -0700272 // When targeting 1.9, override the modules to use with --system
273 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700274
275 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800276 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700277}
278
Sasha Smundak2057f822019-04-16 17:16:58 -0700279func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
280 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
281}
282
Colin Cross46c9b8b2017-06-22 16:51:17 -0700283// Module contains the properties and members used by all java module types
284type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700285 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700286 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900287 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700288
Colin Cross89536d42017-07-07 14:35:50 -0700289 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700290 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700291 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700292
Colin Cross331a1212018-08-15 20:40:52 -0700293 // jar file containing header classes including static library dependencies, suitable for
294 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700295 headerJarFile android.Path
296
Colin Cross331a1212018-08-15 20:40:52 -0700297 // jar file containing implementation classes including static library dependencies but no
298 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700300
Colin Cross331a1212018-08-15 20:40:52 -0700301 // jar file containing only resources including from static library dependencies
302 resourceJar android.Path
303
Colin Cross0c4ce212019-05-03 15:28:19 -0700304 // args and dependencies to package source files into a srcjar
305 srcJarArgs []string
306 srcJarDeps android.Paths
307
Colin Cross331a1212018-08-15 20:40:52 -0700308 // jar file containing implementation classes and resources including static library
309 // dependencies
310 implementationAndResourcesJar android.Path
311
312 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700313 dexJarFile android.Path
314
Colin Cross43f08db2018-11-12 10:13:39 -0800315 // output file that contains classes.dex if it should be in the output file
316 maybeStrippedDexJarFile android.Path
317
Colin Crosscb933592017-11-22 13:49:43 -0800318 // output file containing uninstrumented classes that will be instrumented by jacoco
319 jacocoReportClassesFile android.Path
320
Colin Cross66dbc0b2017-12-28 12:23:20 -0800321 // output file containing mapping of obfuscated names
322 proguardDictionary android.Path
323
Colin Cross331a1212018-08-15 20:40:52 -0700324 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700325 outputFile android.Path
326 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700327
Colin Cross635c3b02016-05-18 15:37:25 -0700328 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700329
Colin Cross635c3b02016-05-18 15:37:25 -0700330 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700331
Colin Cross2fe66872015-03-30 17:20:39 -0700332 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700333 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800334
335 // list of .java files and srcjars that was passed to javac
336 compiledJavaSrcs android.Paths
337 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800338
339 // list of extra progurad flag files
340 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900341
Colin Cross094054a2018-10-17 15:10:48 -0700342 // manifest file to use instead of properties.Manifest
343 overrideManifest android.OptionalPath
344
Jiyong Park1be96912018-05-28 18:02:19 +0900345 // list of SDK lib names that this java moudule is exporting
346 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700347
348 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
349 // filter out Exclude_srcs, will be used by android.IDEInfo struct
350 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800351
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800352 // expanded Jarjar_rules
353 expandJarjarRules android.Path
354
Vladimir Marko0975ee02019-04-02 10:29:55 +0100355 // list of additional targets for checkbuild
356 additionalCheckedModules android.Paths
357
Colin Cross988708c2019-05-06 14:04:11 -0700358 // Extra files generated by the module type to be added as java resources.
359 extraResources android.Paths
360
Colin Crossf24a22a2019-01-31 14:12:44 -0800361 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800362 dexpreopter
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800363
364 // list of the xref extraction files
365 kytheFiles android.Paths
Colin Cross2fe66872015-03-30 17:20:39 -0700366}
367
Colin Cross41955e82019-05-29 14:40:35 -0700368func (j *Module) OutputFiles(tag string) (android.Paths, error) {
369 switch tag {
370 case "":
371 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700372 case ".jar":
373 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross2d975b12019-07-29 16:47:42 -0700374 case ".proguard_map":
375 return android.Paths{j.proguardDictionary}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700376 default:
377 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
378 }
Colin Cross54250902017-12-05 09:28:08 -0800379}
380
Jiyong Park8fd61922018-11-08 02:50:25 +0900381func (j *Module) DexJarFile() android.Path {
382 return j.dexJarFile
383}
384
Colin Cross41955e82019-05-29 14:40:35 -0700385var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800386
Colin Crossf506d872017-07-19 15:53:04 -0700387type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700388 HeaderJars() android.Paths
389 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700390 ResourceJars() android.Paths
391 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800392 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700393 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900394 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700395 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700396}
397
Jiyong Parkc678ad32018-04-10 13:07:10 +0900398type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700399 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
400 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900401}
402
Nan Zhangb2b33de2018-02-23 11:18:47 -0800403type SrcDependency interface {
404 CompiledSrcs() android.Paths
405 CompiledSrcJars() android.Paths
406}
407
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800408type xref interface {
409 XrefJavaFiles() android.Paths
410}
411
Nan Zhangb2b33de2018-02-23 11:18:47 -0800412func (j *Module) CompiledSrcs() android.Paths {
413 return j.compiledJavaSrcs
414}
415
416func (j *Module) CompiledSrcJars() android.Paths {
417 return j.compiledSrcJars
418}
419
Sasha Smundak2a4549e2018-11-05 16:49:08 -0800420func (j *Module) XrefJavaFiles() android.Paths {
421 return j.kytheFiles
422}
423
Nan Zhangb2b33de2018-02-23 11:18:47 -0800424var _ SrcDependency = (*Module)(nil)
425
Colin Cross89536d42017-07-07 14:35:50 -0700426func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
427 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
428 android.InitDefaultableModule(module)
429}
430
Colin Crossbe1da472017-07-07 15:59:46 -0700431type dependencyTag struct {
432 blueprint.BaseDependencyTag
433 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700434}
435
Colin Crossa4f08812018-10-02 22:03:40 -0700436type jniDependencyTag struct {
437 blueprint.BaseDependencyTag
438 target android.Target
439}
440
Colin Crossbe1da472017-07-07 15:59:46 -0700441var (
Colin Cross4b964c02018-10-15 16:18:06 -0700442 staticLibTag = dependencyTag{name: "staticlib"}
443 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800444 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700445 bootClasspathTag = dependencyTag{name: "bootclasspath"}
446 systemModulesTag = dependencyTag{name: "system modules"}
447 frameworkResTag = dependencyTag{name: "framework-res"}
448 frameworkApkTag = dependencyTag{name: "framework-apk"}
449 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800450 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700451 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
452 certificateTag = dependencyTag{name: "certificate"}
453 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700454 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700455)
Colin Cross2fe66872015-03-30 17:20:39 -0700456
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900457func defaultSdkVersion(ctx checkVendorModuleContext) string {
458 if ctx.SocSpecific() || ctx.DeviceSpecific() {
459 return "system_current"
460 }
461 return ""
462}
463
464type checkVendorModuleContext interface {
465 SocSpecific() bool
466 DeviceSpecific() bool
467}
468
Colin Crossfc3674a2017-09-18 17:41:52 -0700469type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700470 useModule, useFiles, useDefaultLibs, invalidVersion bool
471
Colin Cross86a60ae2018-05-29 14:44:55 -0700472 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700473 systemModules string
474
Colin Crossa97c5d32018-03-28 14:58:31 -0700475 frameworkResModule string
476
Colin Cross86a60ae2018-05-29 14:44:55 -0700477 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700478 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100479
480 noStandardLibs, noFrameworksLibs bool
481}
482
483func (s sdkDep) hasStandardLibs() bool {
484 return !s.noStandardLibs
485}
486
487func (s sdkDep) hasFrameworkLibs() bool {
488 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700489}
490
Colin Crossa4f08812018-10-02 22:03:40 -0700491type jniLib struct {
492 name string
493 path android.Path
494 target android.Target
495}
496
Colin Cross0ea8ba82019-06-06 14:33:29 -0700497func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800498 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
499}
500
Colin Cross0ea8ba82019-06-06 14:33:29 -0700501func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800502 return j.shouldInstrument(ctx) &&
503 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
504 ctx.Config().UnbundledBuild())
505}
506
Colin Cross83bb3162018-06-25 15:48:06 -0700507func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900508 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700509}
510
511func (j *Module) minSdkVersion() string {
512 if j.deviceProperties.Min_sdk_version != nil {
513 return *j.deviceProperties.Min_sdk_version
514 }
515 return j.sdkVersion()
516}
517
Dan Willemsen419290a2018-10-31 15:28:47 -0700518func (j *Module) targetSdkVersion() string {
519 if j.deviceProperties.Target_sdk_version != nil {
520 return *j.deviceProperties.Target_sdk_version
521 }
522 return j.sdkVersion()
523}
524
Colin Crossbe1da472017-07-07 15:59:46 -0700525func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700526 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100527 sdkDep := decodeSdkDep(ctx, sdkContext(j))
528 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700529 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700530 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100531 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100532 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700533 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700534 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700535 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100536 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700537 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700538 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700539 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
540 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800541 }
Colin Crossbe1da472017-07-07 15:59:46 -0700542 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700543 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100544 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100545 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100546 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700547 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700548 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100549 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700550 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800551 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800552 if ctx.ModuleName() == "android_stubs_current" ||
553 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700554 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700555 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800556 }
Colin Cross2fe66872015-03-30 17:20:39 -0700557 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700558
Colin Cross42d48b72018-08-29 14:10:52 -0700559 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
560 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700561
Colin Crossbe9cdb82019-01-21 21:37:16 -0800562 ctx.AddFarVariationDependencies([]blueprint.Variation{
563 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
564 }, pluginTag, j.properties.Plugins...)
565
Colin Crossfe17f6f2019-03-28 19:30:56 -0700566 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700567 if j.hasSrcExt(".proto") {
568 protoDeps(ctx, &j.protoProperties)
569 }
Colin Cross93e85952017-08-15 13:34:18 -0700570
571 if j.hasSrcExt(".kt") {
572 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
573 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700574 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
575 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800576 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800577 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
578 }
Colin Cross93e85952017-08-15 13:34:18 -0700579 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800580
581 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700582 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800583 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700584}
585
586func hasSrcExt(srcs []string, ext string) bool {
587 for _, src := range srcs {
588 if filepath.Ext(src) == ext {
589 return true
590 }
591 }
592
593 return false
594}
595
Nan Zhang61eaedb2017-11-02 13:28:15 -0700596func shardPaths(paths android.Paths, shardSize int) []android.Paths {
597 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
598 for len(paths) > shardSize {
599 ret = append(ret, paths[0:shardSize])
600 paths = paths[shardSize:]
601 }
602 if len(paths) > 0 {
603 ret = append(ret, paths)
604 }
605 return ret
606}
607
Colin Cross6af17aa2017-09-20 12:59:05 -0700608func (j *Module) hasSrcExt(ext string) bool {
609 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700610}
611
Colin Cross46c9b8b2017-06-22 16:51:17 -0700612func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700613 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700614
Colin Crossebe1a512017-11-14 13:12:14 -0800615 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
616 aidlIncludes = append(aidlIncludes,
617 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
618 aidlIncludes = append(aidlIncludes,
619 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700620
Colin Cross3047fa22019-04-18 10:56:44 -0700621 var flags []string
622 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700623
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700624 if aidlPreprocess.Valid() {
625 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700626 deps = append(deps, aidlPreprocess.Path())
627 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700628 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700629 }
630
Colin Cross3047fa22019-04-18 10:56:44 -0700631 if len(j.exportAidlIncludeDirs) > 0 {
632 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
633 }
634
635 if len(aidlIncludes) > 0 {
636 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
637 }
638
Colin Cross635c3b02016-05-18 15:37:25 -0700639 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800640 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700641 flags = append(flags, "-I"+src.String())
642 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700643
Martijn Coeneneab15642018-03-09 09:29:59 +0100644 if Bool(j.deviceProperties.Aidl.Generate_traces) {
645 flags = append(flags, "-t")
646 }
647
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100648 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
649 flags = append(flags, "--transaction_names")
650 }
651
Colin Cross3047fa22019-04-18 10:56:44 -0700652 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700653}
654
Colin Cross32f676a2017-09-06 13:41:06 -0700655type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800656 classpath classpath
657 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700658 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800659 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700660 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700661 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700662 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700663 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800664 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700665 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700666 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700667 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700668 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800669 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800670
671 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700672}
Colin Cross2fe66872015-03-30 17:20:39 -0700673
Colin Cross54250902017-12-05 09:28:08 -0800674func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
675 for _, f := range dep.Srcs() {
676 if f.Ext() != ".jar" {
677 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
678 ctx.OtherModuleName(dep.(blueprint.Module)))
679 }
680 }
681}
682
Jiyong Park2d492942018-03-05 17:44:10 +0900683type linkType int
684
685const (
686 javaCore linkType = iota
687 javaSdk
688 javaSystem
689 javaPlatform
690)
691
Jiyong Park46f78fb2018-10-20 16:33:17 +0900692func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700693 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700694 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900695 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
696 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100697 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900698 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100699 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900700 return javaCore, false
701 case name == "android_system_stubs_current":
702 return javaSystem, true
703 case strings.HasPrefix(ver, "system_"):
704 return javaSystem, false
705 case name == "android_test_stubs_current":
706 return javaSystem, true
707 case strings.HasPrefix(ver, "test_"):
708 return javaPlatform, false
709 case name == "android_stubs_current":
710 return javaSdk, true
711 case ver == "current":
712 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100713 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900714 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700715 default:
716 if _, err := strconv.Atoi(ver); err != nil {
717 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
718 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900719 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900720 }
721}
722
Jiyong Park750e5572018-01-31 00:20:13 +0900723func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700724 if ctx.Host() {
725 return
726 }
727
Jiyong Park46f78fb2018-10-20 16:33:17 +0900728 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
729 if stubs {
730 return
731 }
732 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900733 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."
734
735 switch myLinkType {
736 case javaCore:
737 if otherLinkType != javaCore {
738 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 +0900739 ctx.OtherModuleName(to))
740 }
Jiyong Park2d492942018-03-05 17:44:10 +0900741 break
742 case javaSdk:
743 if otherLinkType != javaCore && otherLinkType != javaSdk {
744 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
745 ctx.OtherModuleName(to))
746 }
747 break
748 case javaSystem:
749 if otherLinkType == javaPlatform {
750 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
751 ctx.OtherModuleName(to))
752 }
753 break
754 case javaPlatform:
755 // no restriction on link-type
756 break
Jiyong Park750e5572018-01-31 00:20:13 +0900757 }
758}
759
Colin Cross32f676a2017-09-06 13:41:06 -0700760func (j *Module) collectDeps(ctx android.ModuleContext) deps {
761 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700762
Colin Cross300f0382018-03-06 13:11:51 -0800763 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700764 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800765 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700766 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800767 } else if sdkDep.useFiles {
768 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700769 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700770 deps.aidlPreprocess = sdkDep.aidl
771 } else {
772 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800773 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700774 }
775
Colin Crossd11fcda2017-10-23 17:59:01 -0700776 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700777 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700778 tag := ctx.OtherModuleDependencyTag(module)
779
Colin Crossa4f08812018-10-02 22:03:40 -0700780 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700781 // Handled by AndroidApp.collectAppDeps
782 return
783 }
784 if tag == certificateTag {
785 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700786 return
787 }
788
Jiyong Park750e5572018-01-31 00:20:13 +0900789 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700790 switch tag {
791 case bootClasspathTag, libTag, staticLibTag:
792 checkLinkType(ctx, j, to, tag.(dependencyTag))
793 }
Jiyong Park750e5572018-01-31 00:20:13 +0900794 }
Colin Cross54250902017-12-05 09:28:08 -0800795 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800796 case SdkLibraryDependency:
797 switch tag {
798 case libTag:
799 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
800 // names of sdk libs that are directly depended are exported
801 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700802 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800803 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
804 }
Colin Cross54250902017-12-05 09:28:08 -0800805 case Dependency:
806 switch tag {
807 case bootClasspathTag:
808 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700809 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800810 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900811 // sdk lib names from dependencies are re-exported
812 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700813 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800814 case staticLibTag:
815 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
816 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
817 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700818 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900819 // sdk lib names from dependencies are re-exported
820 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700821 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800822 case pluginTag:
823 if plugin, ok := dep.(*Plugin); ok {
824 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
825 if plugin.pluginProperties.Processor_class != nil {
826 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
827 }
828 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
829 } else {
830 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
831 }
Colin Cross54250902017-12-05 09:28:08 -0800832 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100833 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800834 // framework.jar has a one-off dependency on the R.java and Manifest.java files
835 // generated by framework-res.apk
836 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
837 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800838 case frameworkApkTag:
839 if ctx.ModuleName() == "android_stubs_current" ||
840 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700841 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800842 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
843 // resource files out of there for aapt.
844 //
845 // Normally the package rule runs aapt, which includes the resource,
846 // but we're not running that in our package rule so just copy in the
847 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700848 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800849 }
Colin Cross54250902017-12-05 09:28:08 -0800850 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700851 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800852 case kotlinAnnotationsTag:
853 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800854 }
855
Colin Cross54250902017-12-05 09:28:08 -0800856 case android.SourceFileProducer:
857 switch tag {
858 case libTag:
859 checkProducesJars(ctx, dep)
860 deps.classpath = append(deps.classpath, dep.Srcs()...)
861 case staticLibTag:
862 checkProducesJars(ctx, dep)
863 deps.classpath = append(deps.classpath, dep.Srcs()...)
864 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
865 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800866 }
867 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700868 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700869 case systemModulesTag:
870 if deps.systemModules != nil {
871 panic("Found two system module dependencies")
872 }
873 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000874 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700875 panic("Missing directory for system module dependency")
876 }
Colin Crossb77043e2019-07-16 13:57:13 -0700877 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700878 }
Colin Crossec7a0422017-07-07 14:47:12 -0700879 }
Colin Cross2fe66872015-03-30 17:20:39 -0700880 })
881
Jiyong Park1be96912018-05-28 18:02:19 +0900882 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
883
Colin Cross32f676a2017-09-06 13:41:06 -0700884 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700885}
886
Colin Cross83bb3162018-06-25 15:48:06 -0700887func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700888 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800889 v := sdkContext.sdkVersion()
890 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100891 if ctx.Config().IsPdkBuild() &&
892 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700893 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800894 latestSdkVersion := 0
895 if len(sdkVersions) > 0 {
896 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
897 }
898 v = strconv.Itoa(latestSdkVersion)
899 }
900
901 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700902 if err != nil {
903 ctx.PropertyErrorf("sdk_version", "%s", err)
904 }
Nan Zhang357466b2018-04-17 17:38:36 -0700905 if javaVersion != "" {
906 ret = javaVersion
907 } else if ctx.Device() && sdk <= 23 {
908 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100909 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700910 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100911 } else if ctx.Device() &&
912 sdkContext.sdkVersion() != "" &&
913 sdkContext.sdkVersion() != "none" &&
914 sdkContext.sdkVersion() != "core_platform" &&
915 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700916 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
917 ret = "1.8"
918 } else {
919 ret = "1.9"
920 }
921
922 return ret
923}
924
Nan Zhanged19fc32017-10-19 13:06:22 -0700925func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700926
Colin Crossf03c82b2015-04-13 13:53:40 -0700927 var flags javaBuilderFlags
928
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100929 // javaVersion flag.
930 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
931
Nan Zhanged19fc32017-10-19 13:06:22 -0700932 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700933 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100934 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700935 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700936 }
Colin Cross6510f912017-11-29 00:27:14 -0800937 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700938 // Override the -g flag passed globally to remove local variable debug info to reduce
939 // disk and memory usage.
940 javacFlags = append(javacFlags, "-g:source,lines")
941 }
Colin Cross64162712017-08-08 13:17:59 -0700942
Colin Cross66548102018-06-19 22:47:35 -0700943 if ctx.Config().RunErrorProne() {
944 if config.ErrorProneClasspath == nil {
945 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
946 }
947
948 errorProneFlags := []string{
949 "-Xplugin:ErrorProne",
950 "${config.ErrorProneChecks}",
951 }
952 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
953
954 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
955 "'" + strings.Join(errorProneFlags, " ") + "'"
956 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800957 }
958
Nan Zhanged19fc32017-10-19 13:06:22 -0700959 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800960 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
961 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700962 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800963
Colin Crossbe9cdb82019-01-21 21:37:16 -0800964 flags.processor = strings.Join(deps.processorClasses, ",")
965
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100966 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100967 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800968 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
969 // Give host-side tools a version of OpenJDK's standard libraries
970 // close to what they're targeting. As of Dec 2017, AOSP is only
971 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
972 //
973 // When building with OpenJDK 8, the following should have no
974 // effect since those jars would be available by default.
975 //
976 // When building with OpenJDK 9 but targeting a version < 1.8,
977 // putting them on the bootclasspath means that:
978 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
979 // b) references to existing APIs are not reinterpreted in an
980 // OpenJDK 9-specific way, eg. calls to subclasses of
981 // java.nio.Buffer as in http://b/70862583
982 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
983 flags.bootClasspath = append(flags.bootClasspath,
984 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
985 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800986 if Bool(j.properties.Use_tools_jar) {
987 flags.bootClasspath = append(flags.bootClasspath,
988 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
989 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800990 }
991
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100992 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800993 // Manually specify build directory in case it is not under the repo root.
994 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
995 // just adding a symlink under the root doesn't help.)
996 patchPaths := ".:" + ctx.Config().BuildDir()
997 classPath := flags.classpath.FormJavaClassPath("")
998 if classPath != "" {
999 patchPaths += ":" + classPath
1000 }
1001 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -07001002 }
1003
Nan Zhanged19fc32017-10-19 13:06:22 -07001004 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -07001005 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001006
Nan Zhanged19fc32017-10-19 13:06:22 -07001007 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -07001008 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -07001009
Colin Cross81440082018-08-15 20:21:55 -07001010 if len(javacFlags) > 0 {
1011 // optimization.
1012 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1013 flags.javacFlags = "$javacFlags"
1014 }
1015
Nan Zhanged19fc32017-10-19 13:06:22 -07001016 return flags
1017}
Colin Crossc0b06f12015-04-08 13:03:43 -07001018
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001019func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
1020
Colin Crossebe1a512017-11-14 13:12:14 -08001021 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001022
1023 deps := j.collectDeps(ctx)
1024 flags := j.collectBuilderFlags(ctx, deps)
1025
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001026 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001027 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1028 }
Colin Cross8a497952019-03-05 22:25:09 -08001029 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001030 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001031 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001032 }
1033
Colin Crossaf050172017-11-15 23:01:59 -08001034 srcFiles = j.genSources(ctx, srcFiles, flags)
1035
1036 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001037 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001038 if aaptSrcJar != nil {
1039 srcJars = append(srcJars, aaptSrcJar)
1040 }
Colin Crossb7a63242015-04-16 14:09:14 -07001041
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001042 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1043 // that IDEInfo struct will use
1044 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1045
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001046 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001047 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001048 }
1049
Colin Cross1ee23172017-10-18 14:44:18 -07001050 jarName := ctx.ModuleName() + ".jar"
1051
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001052 javaSrcFiles := srcFiles.FilterByExt(".java")
1053 var uniqueSrcFiles android.Paths
1054 set := make(map[string]bool)
1055 for _, v := range javaSrcFiles {
1056 if _, found := set[v.String()]; !found {
1057 set[v.String()] = true
1058 uniqueSrcFiles = append(uniqueSrcFiles, v)
1059 }
1060 }
1061
Colin Cross55f63ea2018-08-27 12:37:09 -07001062 var kotlinJars android.Paths
1063
Colin Cross93e85952017-08-15 13:34:18 -07001064 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001065 // user defined kotlin flags.
1066 kotlincFlags := j.properties.Kotlincflags
1067 CheckKotlincFlags(ctx, kotlincFlags)
1068
Colin Cross93e85952017-08-15 13:34:18 -07001069 // If there are kotlin files, compile them first but pass all the kotlin and java files
1070 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1071 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001072 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001073 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001074 kotlincFlags = append(kotlincFlags, "-no-jdk")
1075 }
1076 if len(kotlincFlags) > 0 {
1077 // optimization.
1078 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1079 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001080 }
1081
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001082 var kotlinSrcFiles android.Paths
1083 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1084 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1085
Colin Crossafbb1732019-01-17 15:42:52 -08001086 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1087 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1088
1089 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1090 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1091
1092 if len(flags.processorPath) > 0 {
1093 // Use kapt for annotation processing
1094 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1095 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1096 srcJars = append(srcJars, kaptSrcJar)
1097 // Disable annotation processing in javac, it's already been handled by kapt
1098 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001099 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001100 }
Colin Cross93e85952017-08-15 13:34:18 -07001101
Colin Cross1ee23172017-10-18 14:44:18 -07001102 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001103 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001104 if ctx.Failed() {
1105 return
1106 }
1107
1108 // Make javac rule depend on the kotlinc rule
1109 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001110
Colin Cross93e85952017-08-15 13:34:18 -07001111 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001112 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001113 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001114 }
1115
Colin Cross55f63ea2018-08-27 12:37:09 -07001116 jars := append(android.Paths(nil), kotlinJars...)
1117
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001118 // Store the list of .java files that was passed to javac
1119 j.compiledJavaSrcs = uniqueSrcFiles
1120 j.compiledSrcJars = srcJars
1121
Nan Zhang61eaedb2017-11-02 13:28:15 -07001122 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001123 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001124 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1125 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001126 // Formerly, there was a check here that prevented annotation processors
1127 // from being used when sharding was enabled, as some annotation processors
1128 // do not function correctly in sharded environments. It was removed to
1129 // allow for the use of annotation processors that do function correctly
1130 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001131 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001132 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001133 if ctx.Failed() {
1134 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001135 }
1136 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001137 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001138 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001139 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001140 // If error-prone is enabled, add an additional rule to compile the java files into
1141 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001142 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001143 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1144 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001145 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001146 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001147 extraJarDeps = append(extraJarDeps, errorprone)
1148 }
1149
Nan Zhang61eaedb2017-11-02 13:28:15 -07001150 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001151 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001152 shardSize := int(*(j.properties.Javac_shard_size))
1153 var shardSrcs []android.Paths
1154 if len(uniqueSrcFiles) > 0 {
1155 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1156 for idx, shardSrc := range shardSrcs {
1157 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1158 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1159 jars = append(jars, classes)
1160 }
1161 }
1162 if len(srcJars) > 0 {
1163 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1164 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1165 jars = append(jars, classes)
1166 }
1167 } else {
1168 classes := android.PathForModuleOut(ctx, "javac", jarName)
1169 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1170 jars = append(jars, classes)
1171 }
Sasha Smundak2a4549e2018-11-05 16:49:08 -08001172 if ctx.Config().EmitXrefRules() {
1173 extractionFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".kzip")
1174 emitXrefRule(ctx, extractionFile, uniqueSrcFiles, srcJars, flags, extraJarDeps, "xref")
1175 j.kytheFiles = append(j.kytheFiles, extractionFile)
1176
1177 }
Colin Crossd6891432017-09-27 17:39:56 -07001178 if ctx.Failed() {
1179 return
1180 }
Colin Cross2fe66872015-03-30 17:20:39 -07001181 }
1182
Colin Cross0c4ce212019-05-03 15:28:19 -07001183 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1184
1185 var includeSrcJar android.WritablePath
1186 if Bool(j.properties.Include_srcs) {
1187 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1188 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1189 }
1190
Colin Crosscedd4762018-09-13 11:26:19 -07001191 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1192 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001193 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001194 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001195
1196 var resArgs []string
1197 var resDeps android.Paths
1198
1199 resArgs = append(resArgs, dirArgs...)
1200 resDeps = append(resDeps, dirDeps...)
1201
1202 resArgs = append(resArgs, fileArgs...)
1203 resDeps = append(resDeps, fileDeps...)
1204
Colin Cross988708c2019-05-06 14:04:11 -07001205 resArgs = append(resArgs, extraArgs...)
1206 resDeps = append(resDeps, extraDeps...)
1207
Colin Cross40a36712017-09-27 17:41:35 -07001208 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001209 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001210 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001211 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001212 if ctx.Failed() {
1213 return
1214 }
1215 }
1216
Colin Cross0c4ce212019-05-03 15:28:19 -07001217 var resourceJars android.Paths
1218 if j.resourceJar != nil {
1219 resourceJars = append(resourceJars, j.resourceJar)
1220 }
1221 if Bool(j.properties.Include_srcs) {
1222 resourceJars = append(resourceJars, includeSrcJar)
1223 }
1224 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001225
Colin Cross0c4ce212019-05-03 15:28:19 -07001226 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001227 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001228 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001229 false, nil, nil)
1230 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001231 } else if len(resourceJars) == 1 {
1232 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001233 }
1234
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001235 if len(deps.staticJars) > 0 {
1236 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001237 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001238
Colin Cross094054a2018-10-17 15:10:48 -07001239 manifest := j.overrideManifest
1240 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001241 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001242 }
Colin Cross635acc92017-09-12 22:50:46 -07001243
Colin Cross8a497952019-03-05 22:25:09 -08001244 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001245 if len(services) > 0 {
1246 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1247 var zipargs []string
1248 for _, file := range services {
1249 serviceFile := file.String()
1250 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1251 }
1252 ctx.Build(pctx, android.BuildParams{
1253 Rule: zip,
1254 Output: servicesJar,
1255 Implicits: services,
1256 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001257 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001258 },
1259 })
1260 jars = append(jars, servicesJar)
1261 }
1262
Colin Cross0a6e0072017-08-30 14:24:55 -07001263 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001264 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001265 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001266
1267 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001268 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1269 // Optimization: skip the combine step if there is nothing to do
1270 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1271 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1272 // any if len(jars) == 1.
1273 outputFile = moduleOutPath
1274 } else {
1275 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1276 ctx.Build(pctx, android.BuildParams{
1277 Rule: android.Cp,
1278 Input: jars[0],
1279 Output: combinedJar,
1280 })
1281 outputFile = combinedJar
1282 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001283 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001284 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001285 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001286 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001287 outputFile = combinedJar
1288 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001289
Colin Cross331a1212018-08-15 20:40:52 -07001290 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001291 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001292 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001293 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001294 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001295 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001296
1297 // jarjar resource jar if necessary
1298 if j.resourceJar != nil {
1299 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001300 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001301 j.resourceJar = resourceJarJarFile
1302 }
1303
Colin Cross0a6e0072017-08-30 14:24:55 -07001304 if ctx.Failed() {
1305 return
1306 }
1307 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001308
1309 // Check package restrictions if necessary.
1310 if len(j.properties.Permitted_packages) > 0 {
1311 // Check packages and copy to package-checked file.
1312 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1313 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1314 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1315
1316 if ctx.Failed() {
1317 return
1318 }
1319 }
1320
Nan Zhanged19fc32017-10-19 13:06:22 -07001321 j.implementationJarFile = outputFile
1322 if j.headerJarFile == nil {
1323 j.headerJarFile = j.implementationJarFile
1324 }
Colin Cross2fe66872015-03-30 17:20:39 -07001325
Colin Cross6510f912017-11-29 00:27:14 -08001326 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001327 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1328 j.properties.Instrument = true
1329 }
1330 }
1331
Colin Cross3144dfc2018-01-03 15:06:47 -08001332 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001333 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1334 }
1335
Colin Cross331a1212018-08-15 20:40:52 -07001336 // merge implementation jar with resources if necessary
1337 implementationAndResourcesJar := outputFile
1338 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001339 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001340 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001341 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001342 false, nil, nil)
1343 implementationAndResourcesJar = combinedJar
1344 }
1345
1346 j.implementationAndResourcesJar = implementationAndResourcesJar
1347
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001348 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001349 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001350 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001351 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001352 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001353 if ctx.Failed() {
1354 return
1355 }
Colin Cross331a1212018-08-15 20:40:52 -07001356
Jiyong Park09cb6292019-07-15 15:29:23 +09001357 // Hidden API CSV generation and dex encoding
1358 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1359 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001360
Colin Cross331a1212018-08-15 20:40:52 -07001361 // merge dex jar with resources if necessary
1362 if j.resourceJar != nil {
1363 jars := android.Paths{dexOutputFile, j.resourceJar}
1364 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1365 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1366 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001367 if j.deviceProperties.UncompressDex {
1368 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1369 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1370 dexOutputFile = combinedAlignedJar
1371 } else {
1372 dexOutputFile = combinedJar
1373 }
Colin Cross331a1212018-08-15 20:40:52 -07001374 }
1375
1376 j.dexJarFile = dexOutputFile
1377
Colin Cross8faf8fc2019-01-16 15:15:52 -08001378 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001379 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1380
1381 j.maybeStrippedDexJarFile = dexOutputFile
1382
Colin Cross3063b782018-08-15 11:19:12 -07001383 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001384
1385 if ctx.Failed() {
1386 return
1387 }
Colin Cross331a1212018-08-15 20:40:52 -07001388 } else {
1389 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001390 }
Colin Cross331a1212018-08-15 20:40:52 -07001391
Colin Crossb7a63242015-04-16 14:09:14 -07001392 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001393
1394 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1395 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001396}
1397
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001398// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1399// since some of these flags may be used internally.
1400func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1401 for _, flag := range flags {
1402 flag = strings.TrimSpace(flag)
1403
1404 if !strings.HasPrefix(flag, "-") {
1405 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1406 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1407 ctx.PropertyErrorf("kotlincflags",
1408 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1409 } else if inList(flag, config.KotlincIllegalFlags) {
1410 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1411 } else if flag == "-include-runtime" {
1412 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1413 } else {
1414 args := strings.Split(flag, " ")
1415 if args[0] == "-kotlin-home" {
1416 ctx.PropertyErrorf("kotlincflags",
1417 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1418 }
1419 }
1420 }
1421}
1422
Colin Cross8eadbf02017-10-24 17:46:00 -07001423func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001424 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001425
1426 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001427 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001428 // Compile java sources into turbine.jar.
1429 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1430 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1431 if ctx.Failed() {
1432 return nil
1433 }
1434 jars = append(jars, turbineJar)
1435 }
1436
Colin Cross55f63ea2018-08-27 12:37:09 -07001437 jars = append(jars, extraJars...)
1438
Nan Zhanged19fc32017-10-19 13:06:22 -07001439 // Combine any static header libraries into classes-header.jar. If there is only
1440 // one input jar this step will be skipped.
1441 var headerJar android.Path
1442 jars = append(jars, deps.staticHeaderJars...)
1443
Colin Cross5c6ecc12017-10-23 18:12:27 -07001444 // we cannot skip the combine step for now if there is only one jar
1445 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1446 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001447 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001448 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001449 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001450
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001451 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001452 // Transform classes.jar into classes-jarjar.jar
1453 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001454 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001455 headerJar = jarjarFile
1456 if ctx.Failed() {
1457 return nil
1458 }
1459 }
1460
1461 return headerJar
1462}
1463
Colin Crosscb933592017-11-22 13:49:43 -08001464func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001465 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001466
Colin Cross7a3139e2017-12-19 13:57:50 -08001467 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001468
Colin Cross84c38822018-01-03 15:59:46 -08001469 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001470 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1471
1472 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1473
1474 j.jacocoReportClassesFile = jacocoReportClassesFile
1475
1476 return instrumentedJar
1477}
1478
albaltai36ff7dc2018-12-25 14:35:23 +08001479var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001480
Nan Zhanged19fc32017-10-19 13:06:22 -07001481func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001482 if j.headerJarFile == nil {
1483 return nil
1484 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001485 return android.Paths{j.headerJarFile}
1486}
1487
1488func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001489 if j.implementationJarFile == nil {
1490 return nil
1491 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001492 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001493}
1494
Colin Crossf24a22a2019-01-31 14:12:44 -08001495func (j *Module) DexJar() android.Path {
1496 return j.dexJarFile
1497}
1498
Colin Cross331a1212018-08-15 20:40:52 -07001499func (j *Module) ResourceJars() android.Paths {
1500 if j.resourceJar == nil {
1501 return nil
1502 }
1503 return android.Paths{j.resourceJar}
1504}
1505
1506func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001507 if j.implementationAndResourcesJar == nil {
1508 return nil
1509 }
Colin Cross331a1212018-08-15 20:40:52 -07001510 return android.Paths{j.implementationAndResourcesJar}
1511}
1512
Colin Cross46c9b8b2017-06-22 16:51:17 -07001513func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001514 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001515 return j.exportAidlIncludeDirs
1516}
1517
Jiyong Park1be96912018-05-28 18:02:19 +09001518func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001519 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001520 return j.exportedSdkLibs
1521}
1522
Colin Cross0c4ce212019-05-03 15:28:19 -07001523func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1524 return j.srcJarArgs, j.srcJarDeps
1525}
1526
Colin Cross46c9b8b2017-06-22 16:51:17 -07001527var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001528
Colin Cross46c9b8b2017-06-22 16:51:17 -07001529func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001530 return j.logtagsSrcs
1531}
1532
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001533// Collect information for opening IDE project files in java/jdeps.go.
1534func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1535 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1536 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001537 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001538 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001539 if j.expandJarjarRules != nil {
1540 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001541 }
1542}
1543
1544func (j *Module) CompilerDeps() []string {
1545 jdeps := []string{}
1546 jdeps = append(jdeps, j.properties.Libs...)
1547 jdeps = append(jdeps, j.properties.Static_libs...)
1548 return jdeps
1549}
1550
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001551func (j *Module) hasCode(ctx android.ModuleContext) bool {
1552 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1553 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1554}
1555
Colin Cross2fe66872015-03-30 17:20:39 -07001556//
1557// Java libraries (.jar file)
1558//
1559
Colin Crossf506d872017-07-19 15:53:04 -07001560type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001561 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001562}
1563
Colin Cross42be7612019-02-21 18:12:14 -08001564func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001565 // Store uncompressed (and do not strip) dex files from boot class path jars.
1566 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1567 return true
1568 }
1569
1570 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001571 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001572 return true
1573 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001574 if ctx.Config().UncompressPrivAppDex() &&
1575 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1576 return true
1577 }
1578
Colin Cross2fc72f62018-12-21 12:59:54 -08001579 return false
1580}
1581
Colin Crossf506d872017-07-19 15:53:04 -07001582func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001583 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1584 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001585 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001586 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001587 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001588 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001589
Jiyong Park7f7766d2019-07-25 22:02:35 +09001590 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1591 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001592 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1593 ctx.ModuleName()+".jar", j.outputFile)
1594 }
Colin Crossb7a63242015-04-16 14:09:14 -07001595}
1596
Colin Crossf506d872017-07-19 15:53:04 -07001597func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001598 j.deps(ctx)
1599}
1600
Colin Cross1b16b0e2019-02-12 14:41:32 -08001601// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1602//
1603// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1604// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1605// as a `static_libs` dependency of another module.
1606//
1607// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1608// a device.
1609//
1610// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1611// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001612func LibraryFactory() android.Module {
1613 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001614
Colin Cross9ae1b922018-06-26 17:59:05 -07001615 module.AddProperties(
1616 &module.Module.properties,
1617 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001618 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001619 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001620
Colin Cross9ae1b922018-06-26 17:59:05 -07001621 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001622 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001623 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001624}
1625
Colin Cross1b16b0e2019-02-12 14:41:32 -08001626// java_library_static is an obsolete alias for java_library.
1627func LibraryStaticFactory() android.Module {
1628 return LibraryFactory()
1629}
1630
1631// java_library_host builds and links sources into a `.jar` file for the host.
1632//
1633// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1634// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001635func LibraryHostFactory() android.Module {
1636 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001637
Colin Cross6af17aa2017-09-20 12:59:05 -07001638 module.AddProperties(
1639 &module.Module.properties,
1640 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001641
Colin Cross9ae1b922018-06-26 17:59:05 -07001642 module.Module.properties.Installable = proptools.BoolPtr(true)
1643
Colin Cross89536d42017-07-07 14:35:50 -07001644 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001645 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001646 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001647}
1648
1649//
Colin Crossb628ea52018-08-14 16:42:33 -07001650// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001651//
1652
1653type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001654 // list of compatibility suites (for example "cts", "vts") that the module should be
1655 // installed into.
1656 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001657
1658 // the name of the test configuration (for example "AndroidTest.xml") that should be
1659 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001660 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001661
Jack He33338892018-09-19 02:21:28 -07001662 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1663 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001664 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001665
Colin Crossd96ca352018-08-10 16:06:24 -07001666 // list of files or filegroup modules that provide data that should be installed alongside
1667 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001668 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001669}
1670
Paul Duffin42df1442019-03-20 12:45:53 +00001671type testHelperLibraryProperties struct {
1672 // list of compatibility suites (for example "cts", "vts") that the module should be
1673 // installed into.
1674 Test_suites []string `android:"arch_variant"`
1675}
1676
Colin Cross05638fc2018-04-09 18:40:24 -07001677type Test struct {
1678 Library
1679
1680 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001681
1682 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001683 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001684}
1685
Paul Duffin42df1442019-03-20 12:45:53 +00001686type TestHelperLibrary struct {
1687 Library
1688
1689 testHelperLibraryProperties testHelperLibraryProperties
1690}
1691
Colin Cross303e21f2018-08-07 16:49:25 -07001692func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001693 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 -08001694 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001695
1696 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001697}
1698
Paul Duffin42df1442019-03-20 12:45:53 +00001699func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1700 j.Library.GenerateAndroidBuildActions(ctx)
1701}
1702
Colin Cross1b16b0e2019-02-12 14:41:32 -08001703// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1704// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1705//
1706// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1707// compiled against the device bootclasspath.
1708//
1709// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1710// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001711func TestFactory() android.Module {
1712 module := &Test{}
1713
1714 module.AddProperties(
1715 &module.Module.properties,
1716 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001717 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001718 &module.Module.protoProperties,
1719 &module.testProperties)
1720
Colin Cross9ae1b922018-06-26 17:59:05 -07001721 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001722 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001723
Colin Cross05638fc2018-04-09 18:40:24 -07001724 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001725 return module
1726}
1727
Paul Duffin42df1442019-03-20 12:45:53 +00001728// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1729func TestHelperLibraryFactory() android.Module {
1730 module := &TestHelperLibrary{}
1731
1732 module.AddProperties(
1733 &module.Module.properties,
1734 &module.Module.deviceProperties,
1735 &module.Module.dexpreoptProperties,
1736 &module.Module.protoProperties,
1737 &module.testHelperLibraryProperties)
1738
Colin Cross9a4abed2019-04-24 13:19:28 -07001739 module.Module.properties.Installable = proptools.BoolPtr(true)
1740 module.Module.dexpreopter.isTest = true
1741
Paul Duffin42df1442019-03-20 12:45:53 +00001742 InitJavaModule(module, android.HostAndDeviceSupported)
1743 return module
1744}
1745
Colin Cross1b16b0e2019-02-12 14:41:32 -08001746// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1747// allow running the test with `atest` or a `TEST_MAPPING` file.
1748//
1749// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1750// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001751func TestHostFactory() android.Module {
1752 module := &Test{}
1753
1754 module.AddProperties(
1755 &module.Module.properties,
1756 &module.Module.protoProperties,
1757 &module.testProperties)
1758
Colin Cross9ae1b922018-06-26 17:59:05 -07001759 module.Module.properties.Installable = proptools.BoolPtr(true)
1760
Colin Cross05638fc2018-04-09 18:40:24 -07001761 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001762 return module
1763}
1764
1765//
Colin Cross2fe66872015-03-30 17:20:39 -07001766// Java Binaries (.jar file plus wrapper script)
1767//
1768
Colin Crossf506d872017-07-19 15:53:04 -07001769type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001770 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001771 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001772
1773 // Name of the class containing main to be inserted into the manifest as Main-Class.
1774 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001775}
1776
Colin Crossf506d872017-07-19 15:53:04 -07001777type Binary struct {
1778 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001779
Colin Crossf506d872017-07-19 15:53:04 -07001780 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001781
Colin Cross6b4a32d2017-12-05 13:42:45 -08001782 isWrapperVariant bool
1783
Colin Crossc3315992017-12-08 19:12:36 -08001784 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001785 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001786}
1787
Alex Light24237172017-10-26 09:46:21 -07001788func (j *Binary) HostToolPath() android.OptionalPath {
1789 return android.OptionalPathForPath(j.binaryFile)
1790}
1791
Colin Crossf506d872017-07-19 15:53:04 -07001792func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001793 if ctx.Arch().ArchType == android.Common {
1794 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001795 if j.binaryProperties.Main_class != nil {
1796 if j.properties.Manifest != nil {
1797 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1798 }
1799 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1800 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1801 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1802 }
1803
Colin Cross6b4a32d2017-12-05 13:42:45 -08001804 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001805 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001806 // Handle the binary wrapper
1807 j.isWrapperVariant = true
1808
Colin Cross366938f2017-12-11 16:29:02 -08001809 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001810 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001811 } else {
1812 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1813 }
1814
1815 // Depend on the installed jar so that the wrapper doesn't get executed by
1816 // another build rule before the jar has been installed.
1817 jarFile := ctx.PrimaryModule().(*Binary).installFile
1818
1819 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1820 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001821 }
Colin Cross2fe66872015-03-30 17:20:39 -07001822}
1823
Colin Crossf506d872017-07-19 15:53:04 -07001824func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001825 if ctx.Arch().ArchType == android.Common {
1826 j.deps(ctx)
1827 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001828}
1829
Colin Cross1b16b0e2019-02-12 14:41:32 -08001830// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1831// as well.
1832//
1833// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1834// compiled against the device bootclasspath.
1835//
1836// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1837// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001838func BinaryFactory() android.Module {
1839 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001840
Colin Cross36242852017-06-23 15:06:31 -07001841 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001842 &module.Module.properties,
1843 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001844 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001845 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001846 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001847
Colin Cross9ae1b922018-06-26 17:59:05 -07001848 module.Module.properties.Installable = proptools.BoolPtr(true)
1849
Colin Cross6b4a32d2017-12-05 13:42:45 -08001850 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1851 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001852 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001853}
1854
Colin Cross1b16b0e2019-02-12 14:41:32 -08001855// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1856//
1857// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1858// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001859func BinaryHostFactory() android.Module {
1860 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001861
Colin Cross36242852017-06-23 15:06:31 -07001862 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001863 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001864 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001865 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001866
Colin Cross9ae1b922018-06-26 17:59:05 -07001867 module.Module.properties.Installable = proptools.BoolPtr(true)
1868
Colin Cross6b4a32d2017-12-05 13:42:45 -08001869 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1870 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001871 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001872}
1873
1874//
1875// Java prebuilts
1876//
1877
Colin Cross74d73e22017-08-02 11:05:49 -07001878type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001879 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001880
Nan Zhangea568a42017-11-08 21:20:04 -08001881 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001882
1883 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001884
1885 // List of shared java libs that this module has dependencies to
1886 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001887
1888 // List of files to remove from the jar file(s)
1889 Exclude_files []string
1890
1891 // List of directories to remove from the jar file(s)
1892 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001893
1894 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001895 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001896}
1897
1898type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001899 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001900 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001901 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001902 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001903
Colin Cross74d73e22017-08-02 11:05:49 -07001904 properties ImportProperties
1905
Colin Cross0a6e0072017-08-30 14:24:55 -07001906 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001907 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001908}
1909
Colin Cross83bb3162018-06-25 15:48:06 -07001910func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001911 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001912}
1913
1914func (j *Import) minSdkVersion() string {
1915 return j.sdkVersion()
1916}
1917
Colin Cross74d73e22017-08-02 11:05:49 -07001918func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001919 return &j.prebuilt
1920}
1921
Colin Cross74d73e22017-08-02 11:05:49 -07001922func (j *Import) PrebuiltSrcs() []string {
1923 return j.properties.Jars
1924}
1925
1926func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001927 return j.prebuilt.Name(j.ModuleBase.Name())
1928}
1929
Colin Cross74d73e22017-08-02 11:05:49 -07001930func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001931 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001932}
1933
Colin Cross74d73e22017-08-02 11:05:49 -07001934func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001935 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001936
Nan Zhang4c819fb2018-08-27 18:31:46 -07001937 jarName := ctx.ModuleName() + ".jar"
1938 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001939 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1940 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001941 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001942 inputFile := outputFile
1943 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1944 TransformJetifier(ctx, outputFile, inputFile)
1945 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001946 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001947
1948 ctx.VisitDirectDeps(func(module android.Module) {
1949 otherName := ctx.OtherModuleName(module)
1950 tag := ctx.OtherModuleDependencyTag(module)
1951
1952 switch dep := module.(type) {
1953 case Dependency:
1954 switch tag {
1955 case libTag, staticLibTag:
1956 // sdk lib names from dependencies are re-exported
1957 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1958 }
1959 case SdkLibraryDependency:
1960 switch tag {
1961 case libTag:
1962 // names of sdk libs that are directly depended are exported
1963 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1964 }
1965 }
1966 })
1967
1968 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001969 if Bool(j.properties.Installable) {
1970 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1971 ctx.ModuleName()+".jar", outputFile)
1972 }
Colin Cross2fe66872015-03-30 17:20:39 -07001973}
1974
Colin Cross74d73e22017-08-02 11:05:49 -07001975var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001976
Nan Zhanged19fc32017-10-19 13:06:22 -07001977func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001978 if j.combinedClasspathFile == nil {
1979 return nil
1980 }
Colin Cross37f6d792018-07-12 12:28:41 -07001981 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001982}
1983
1984func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001985 if j.combinedClasspathFile == nil {
1986 return nil
1987 }
Colin Cross37f6d792018-07-12 12:28:41 -07001988 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001989}
1990
Colin Cross331a1212018-08-15 20:40:52 -07001991func (j *Import) ResourceJars() android.Paths {
1992 return nil
1993}
1994
1995func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001996 if j.combinedClasspathFile == nil {
1997 return nil
1998 }
Colin Cross331a1212018-08-15 20:40:52 -07001999 return android.Paths{j.combinedClasspathFile}
2000}
2001
Colin Crossf24a22a2019-01-31 14:12:44 -08002002func (j *Import) DexJar() android.Path {
2003 return nil
2004}
2005
Colin Cross74d73e22017-08-02 11:05:49 -07002006func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07002007 return nil
2008}
2009
Jiyong Park1be96912018-05-28 18:02:19 +09002010func (j *Import) ExportedSdkLibs() []string {
2011 return j.exportedSdkLibs
2012}
2013
Colin Cross0c4ce212019-05-03 15:28:19 -07002014func (j *Import) SrcJarArgs() ([]string, android.Paths) {
2015 return nil, nil
2016}
2017
albaltai36ff7dc2018-12-25 14:35:23 +08002018// Add compile time check for interface implementation
2019var _ android.IDEInfo = (*Import)(nil)
2020var _ android.IDECustomizedModuleName = (*Import)(nil)
2021
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002022// Collect information for opening IDE project files in java/jdeps.go.
2023const (
2024 removedPrefix = "prebuilt_"
2025)
2026
2027func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
2028 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
2029}
2030
2031func (j *Import) IDECustomizedModuleName() string {
2032 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
2033 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
2034 // solution to get the Import name.
2035 name := j.Name()
2036 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002037 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002038 }
2039 return name
2040}
2041
Colin Cross74d73e22017-08-02 11:05:49 -07002042var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002043
Colin Cross1b16b0e2019-02-12 14:41:32 -08002044// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2045//
2046// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2047// compiled against an Android classpath.
2048//
2049// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2050// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002051func ImportFactory() android.Module {
2052 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002053
Colin Cross74d73e22017-08-02 11:05:49 -07002054 module.AddProperties(&module.properties)
2055
2056 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002057 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002058 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002059 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002060}
2061
Colin Cross1b16b0e2019-02-12 14:41:32 -08002062// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2063// module.
2064//
2065// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2066// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002067func ImportFactoryHost() android.Module {
2068 module := &Import{}
2069
2070 module.AddProperties(&module.properties)
2071
2072 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002073 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002074 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002075 return module
2076}
2077
Colin Cross42be7612019-02-21 18:12:14 -08002078// dex_import module
2079
2080type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002081 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002082}
2083
2084type DexImport struct {
2085 android.ModuleBase
2086 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002087 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002088 prebuilt android.Prebuilt
2089
2090 properties DexImportProperties
2091
2092 dexJarFile android.Path
2093 maybeStrippedDexJarFile android.Path
2094
2095 dexpreopter
2096}
2097
2098func (j *DexImport) Prebuilt() *android.Prebuilt {
2099 return &j.prebuilt
2100}
2101
2102func (j *DexImport) PrebuiltSrcs() []string {
2103 return j.properties.Jars
2104}
2105
2106func (j *DexImport) Name() string {
2107 return j.prebuilt.Name(j.ModuleBase.Name())
2108}
2109
Colin Cross42be7612019-02-21 18:12:14 -08002110func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2111 if len(j.properties.Jars) != 1 {
2112 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2113 }
2114
2115 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2116 j.dexpreopter.isInstallable = true
2117 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2118
2119 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2120 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2121
2122 if j.dexpreopter.uncompressedDex {
2123 rule := android.NewRuleBuilder()
2124
2125 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2126 rule.Temporary(temporary)
2127
2128 // use zip2zip to uncompress classes*.dex files
2129 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002130 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002131 FlagWithInput("-i ", inputJar).
2132 FlagWithOutput("-o ", temporary).
2133 FlagWithArg("-0 ", "'classes*.dex'")
2134
2135 // use zipalign to align uncompressed classes*.dex files
2136 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002137 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002138 Flag("-f").
2139 Text("4").
2140 Input(temporary).
2141 Output(dexOutputFile)
2142
2143 rule.DeleteTemporaryFiles()
2144
2145 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2146 } else {
2147 ctx.Build(pctx, android.BuildParams{
2148 Rule: android.Cp,
2149 Input: inputJar,
2150 Output: dexOutputFile,
2151 })
2152 }
2153
2154 j.dexJarFile = dexOutputFile
2155
2156 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2157
2158 j.maybeStrippedDexJarFile = dexOutputFile
2159
2160 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2161 ctx.ModuleName()+".jar", dexOutputFile)
2162}
2163
2164func (j *DexImport) DexJar() android.Path {
2165 return j.dexJarFile
2166}
2167
2168// dex_import imports a `.jar` file containing classes.dex files.
2169//
2170// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2171// to the device.
2172func DexImportFactory() android.Module {
2173 module := &DexImport{}
2174
2175 module.AddProperties(&module.properties)
2176
2177 android.InitPrebuiltModule(module, &module.properties.Jars)
2178 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002179 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002180 return module
2181}
2182
Colin Cross89536d42017-07-07 14:35:50 -07002183//
2184// Defaults
2185//
2186type Defaults struct {
2187 android.ModuleBase
2188 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002189 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002190}
2191
Colin Cross1b16b0e2019-02-12 14:41:32 -08002192// java_defaults provides a set of properties that can be inherited by other java or android modules.
2193//
2194// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2195// property in the defaults module that exists in the depending module will be prepended to the depending module's
2196// value for that property.
2197//
2198// Example:
2199//
2200// java_defaults {
2201// name: "example_defaults",
2202// srcs: ["common/**/*.java"],
2203// javacflags: ["-Xlint:all"],
2204// aaptflags: ["--auto-add-overlay"],
2205// }
2206//
2207// java_library {
2208// name: "example",
2209// defaults: ["example_defaults"],
2210// srcs: ["example/**/*.java"],
2211// }
2212//
2213// is functionally identical to:
2214//
2215// java_library {
2216// name: "example",
2217// srcs: [
2218// "common/**/*.java",
2219// "example/**/*.java",
2220// ],
2221// javacflags: ["-Xlint:all"],
2222// }
Colin Cross89536d42017-07-07 14:35:50 -07002223func defaultsFactory() android.Module {
2224 return DefaultsFactory()
2225}
2226
2227func DefaultsFactory(props ...interface{}) android.Module {
2228 module := &Defaults{}
2229
2230 module.AddProperties(props...)
2231 module.AddProperties(
2232 &CompilerProperties{},
2233 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002234 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002235 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002236 &aaptProperties{},
2237 &androidLibraryProperties{},
2238 &appProperties{},
2239 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002240 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002241 &ImportProperties{},
2242 &AARImportProperties{},
2243 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002244 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002245 )
2246
2247 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002248 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002249 return module
2250}
Nan Zhangea568a42017-11-08 21:20:04 -08002251
Sasha Smundak2a4549e2018-11-05 16:49:08 -08002252func kytheExtractJavaFactory() android.Singleton {
2253 return &kytheExtractJavaSingleton{}
2254}
2255
2256type kytheExtractJavaSingleton struct {
2257}
2258
2259func (ks *kytheExtractJavaSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2260 var xrefTargets android.Paths
2261 ctx.VisitAllModules(func(module android.Module) {
2262 if javaModule, ok := module.(xref); ok {
2263 xrefTargets = append(xrefTargets, javaModule.XrefJavaFiles()...)
2264 }
2265 })
2266 // TODO(asmundak): perhaps emit a rule to output a warning if there were no xrefTargets
2267 if len(xrefTargets) > 0 {
2268 ctx.Build(pctx, android.BuildParams{
2269 Rule: blueprint.Phony,
2270 Output: android.PathForPhony(ctx, "xref_java"),
2271 Inputs: xrefTargets,
2272 })
2273 }
2274}
2275
Nan Zhangea568a42017-11-08 21:20:04 -08002276var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002277var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002278var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002279var inList = android.InList