blob: c15a62b2417ded08b52f79aa0e0a95b68afa5bb1 [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)
39 android.RegisterModuleType("java_library_static", LibraryFactory)
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)
44 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070045 android.RegisterModuleType("java_import", ImportFactory)
46 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070047
Colin Cross798bfce2016-10-12 14:28:16 -070048 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070049}
50
Colin Cross2fe66872015-03-30 17:20:39 -070051// TODO:
52// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070053// Renderscript
54// Post-jar passes:
55// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070056// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070057// DroidDoc
58// Findbugs
59
Colin Cross89536d42017-07-07 14:35:50 -070060type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070061 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
62 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070063 Srcs []string `android:"arch_variant"`
64
65 // list of source files that should not be used to build the Java module.
66 // This is most useful in the arch/multilib variants to remove non-common files
67 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070068
69 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070070 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
Colin Cross86a63ff2017-09-27 17:33:10 -070072 // list of directories that should be excluded from java_resource_dirs
73 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070074
Colin Cross0f37af02017-09-27 17:42:05 -070075 // list of files to use as Java resources
76 Java_resources []string `android:"arch_variant"`
77
Colin Crosscedd4762018-09-13 11:26:19 -070078 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross0f37af02017-09-27 17:42:05 -070079 Exclude_java_resources []string `android:"arch_variant"`
80
Colin Crossfa5eb232017-10-01 20:33:03 -070081 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070082 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070083 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070084
Colin Crossfa5eb232017-10-01 20:33:03 -070085 // don't build against the framework libraries (legacy-test, core-junit,
86 // ext, and framework for device targets)
87 No_framework_libs *bool
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of module-specific flags that will be used for javac compiles
90 Javacflags []string `android:"arch_variant"`
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
Colin Cross540eff82017-06-22 17:01:52 -0700101 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700102 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700103
104 // If not blank, set the java version passed to javac as -source and -target
105 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700106
Colin Cross9ae1b922018-06-26 17:59:05 -0700107 // If set to true, allow this module to be dexed and installed on devices. Has no
108 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700109 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700110
Colin Cross0f37af02017-09-27 17:42:05 -0700111 // If set to true, include sources used to compile the module in to the final jar
112 Include_srcs *bool
113
Colin Cross32f676a2017-09-06 13:41:06 -0700114 // List of modules to use as annotation processors
115 Annotation_processors []string
116
117 // List of classes to pass to javac to use as annotation processors
118 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700119
Nan Zhang61eaedb2017-11-02 13:28:15 -0700120 // The number of Java source entries each Javac instance can process
121 Javac_shard_size *int64
122
Nan Zhang5f8cb422018-02-06 10:34:32 -0800123 // Add host jdk tools.jar to bootclasspath
124 Use_tools_jar *bool
125
Colin Cross1369cdb2017-09-29 17:58:17 -0700126 Openjdk9 struct {
127 // List of source files that should only be used when passing -source 1.9
128 Srcs []string
129
130 // List of javac flags that should only be used when passing -source 1.9
131 Javacflags []string
132 }
Colin Crosscb933592017-11-22 13:49:43 -0800133
Colin Cross81440082018-08-15 20:21:55 -0700134 // When compiling language level 9+ .java code in packages that are part of
135 // a system module, patch_module names the module that your sources and
136 // dependencies should be patched into. The Android runtime currently
137 // doesn't implement the JEP 261 module system so this option is only
138 // supported at compile time. It should only be needed to compile tests in
139 // packages that exist in libcore and which are inconvenient to move
140 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100141 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700142
Colin Crosscb933592017-11-22 13:49:43 -0800143 Jacoco struct {
144 // List of classes to include for instrumentation with jacoco to collect coverage
145 // information at runtime when building with coverage enabled. If unset defaults to all
146 // classes.
147 // Supports '*' as the last character of an entry in the list as a wildcard match.
148 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
149 // it matches classes in the package that have the class name as a prefix.
150 Include_filter []string
151
152 // List of classes to exclude from instrumentation with jacoco to collect coverage
153 // information at runtime when building with coverage enabled. Overrides classes selected
154 // by the include_filter property.
155 // Supports '*' as the last character of an entry in the list as a wildcard match.
156 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
157 // it matches classes in the package that have the class name as a prefix.
158 Exclude_filter []string
159 }
160
Andreas Gampef3e5b552018-01-22 21:27:21 -0800161 Errorprone struct {
162 // List of javac flags that should only be used when running errorprone.
163 Javacflags []string
164 }
165
Colin Cross0f2ee152017-12-14 15:22:43 -0800166 Proto struct {
167 // List of extra options that will be passed to the proto generator.
168 Output_params []string
169 }
170
Colin Crosscb933592017-11-22 13:49:43 -0800171 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700172}
173
Colin Cross89536d42017-07-07 14:35:50 -0700174type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700175 // list of module-specific flags that will be used for dex compiles
176 Dxflags []string `android:"arch_variant"`
177
Colin Cross83bb3162018-06-25 15:48:06 -0700178 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
179 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800180 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700181
Colin Cross83bb3162018-06-25 15:48:06 -0700182 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
183 // Defaults to sdk_version if not set.
184 Min_sdk_version *string
185
Colin Cross6af2e492018-05-22 11:12:33 -0700186 // if true, compile against the platform APIs instead of an SDK.
187 Platform_apis *bool
188
Colin Crossebe1a512017-11-14 13:12:14 -0800189 Aidl struct {
190 // Top level directories to pass to aidl tool
191 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700192
Colin Crossebe1a512017-11-14 13:12:14 -0800193 // Directories rooted at the Android.bp file to pass to aidl tool
194 Local_include_dirs []string
195
196 // directories that should be added as include directories for any aidl sources of modules
197 // that depend on this module, as well as to aidl for this module.
198 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100199
200 // whether to generate traces (for systrace) for this interface
201 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100202
203 // whether to generate Binder#GetTransaction name method.
204 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800205 }
Colin Cross92430102017-10-09 14:59:32 -0700206
207 // If true, export a copy of the module as a -hostdex module for host testing.
208 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700209
David Brazdil17ef5632018-06-27 10:27:45 +0100210 // If set to true, compile dex regardless of installable. Defaults to false.
211 Compile_dex *bool
212
Colin Cross1bd87802017-12-05 15:31:19 -0800213 Dex_preopt struct {
214 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
215 // true.
216 Enabled *bool
217
218 // If true, generate an app image (.art file) for this module.
219 App_image *bool
220
221 // If true, use a checked-in profile to guide optimization. Defaults to false unless
222 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
223 // that matches the name of this module, in which case it is defaulted to true.
224 Profile_guided *bool
225
226 // If set, provides the path to profile relative to the Android.bp file. If not set,
227 // defaults to searching for a file that matches the name of this module in the default
228 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
229 Profile *string
230 }
Colin Crossa22116e2017-10-19 14:18:58 -0700231
Colin Cross66dbc0b2017-12-28 12:23:20 -0800232 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700233 // If false, disable all optimization. Defaults to true for android_app and android_test
234 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800235 Enabled *bool
236
237 // If true, optimize for size by removing unused code. Defaults to true for apps,
238 // false for libraries and tests.
239 Shrink *bool
240
241 // If true, optimize bytecode. Defaults to false.
242 Optimize *bool
243
244 // If true, obfuscate bytecode. Defaults to false.
245 Obfuscate *bool
246
247 // If true, do not use the flag files generated by aapt that automatically keep
248 // classes referenced by the app manifest. Defaults to false.
249 No_aapt_flags *bool
250
251 // Flags to pass to proguard.
252 Proguard_flags []string
253
254 // Specifies the locations of files containing proguard flags.
255 Proguard_flags_files []string
256 }
257
Colin Cross1369cdb2017-09-29 17:58:17 -0700258 // When targeting 1.9, override the modules to use with --system
259 System_modules *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700260}
261
Colin Cross46c9b8b2017-06-22 16:51:17 -0700262// Module contains the properties and members used by all java module types
263type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700264 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700265 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700266
Colin Cross89536d42017-07-07 14:35:50 -0700267 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700268 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700269 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700270
Colin Cross331a1212018-08-15 20:40:52 -0700271 // jar file containing header classes including static library dependencies, suitable for
272 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700273 headerJarFile android.Path
274
Colin Cross331a1212018-08-15 20:40:52 -0700275 // jar file containing implementation classes including static library dependencies but no
276 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700278
Colin Cross331a1212018-08-15 20:40:52 -0700279 // jar file containing only resources including from static library dependencies
280 resourceJar android.Path
281
282 // jar file containing implementation classes and resources including static library
283 // dependencies
284 implementationAndResourcesJar android.Path
285
286 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700287 dexJarFile android.Path
288
Colin Crosscb933592017-11-22 13:49:43 -0800289 // output file containing uninstrumented classes that will be instrumented by jacoco
290 jacocoReportClassesFile android.Path
291
Colin Cross66dbc0b2017-12-28 12:23:20 -0800292 // output file containing mapping of obfuscated names
293 proguardDictionary android.Path
294
Colin Cross331a1212018-08-15 20:40:52 -0700295 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700296 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700297
Colin Cross635c3b02016-05-18 15:37:25 -0700298 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700299
Colin Cross635c3b02016-05-18 15:37:25 -0700300 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700301
Colin Cross2fe66872015-03-30 17:20:39 -0700302 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700303 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800304
305 // list of .java files and srcjars that was passed to javac
306 compiledJavaSrcs android.Paths
307 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800308
309 // list of extra progurad flag files
310 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900311
Colin Cross094054a2018-10-17 15:10:48 -0700312 // manifest file to use instead of properties.Manifest
313 overrideManifest android.OptionalPath
314
Jiyong Park1be96912018-05-28 18:02:19 +0900315 // list of SDK lib names that this java moudule is exporting
316 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700317
318 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
319 // filter out Exclude_srcs, will be used by android.IDEInfo struct
320 expandIDEInfoCompiledSrcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700321}
322
Colin Cross54250902017-12-05 09:28:08 -0800323func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700324 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800325}
326
327var _ android.SourceFileProducer = (*Module)(nil)
328
Colin Crossf506d872017-07-19 15:53:04 -0700329type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700330 HeaderJars() android.Paths
331 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700332 ResourceJars() android.Paths
333 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700334 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900335 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700336}
337
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338type SdkLibraryDependency interface {
339 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900340 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900341}
342
Nan Zhangb2b33de2018-02-23 11:18:47 -0800343type SrcDependency interface {
344 CompiledSrcs() android.Paths
345 CompiledSrcJars() android.Paths
346}
347
348func (j *Module) CompiledSrcs() android.Paths {
349 return j.compiledJavaSrcs
350}
351
352func (j *Module) CompiledSrcJars() android.Paths {
353 return j.compiledSrcJars
354}
355
356var _ SrcDependency = (*Module)(nil)
357
Colin Cross89536d42017-07-07 14:35:50 -0700358func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
359 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
360 android.InitDefaultableModule(module)
361}
362
Colin Crossbe1da472017-07-07 15:59:46 -0700363type dependencyTag struct {
364 blueprint.BaseDependencyTag
365 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700366}
367
Colin Crossa4f08812018-10-02 22:03:40 -0700368type jniDependencyTag struct {
369 blueprint.BaseDependencyTag
370 target android.Target
371}
372
Colin Crossbe1da472017-07-07 15:59:46 -0700373var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700374 staticLibTag = dependencyTag{name: "staticlib"}
375 libTag = dependencyTag{name: "javalib"}
Colin Cross6a77c982018-06-19 22:43:34 -0700376 annoTag = dependencyTag{name: "annotation processor"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700377 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700378 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700379 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800380 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700381 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800382 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbd01e2a2018-10-04 15:21:03 -0700383 certificateTag = dependencyTag{name: "certificate"}
Colin Crossbe1da472017-07-07 15:59:46 -0700384)
Colin Cross2fe66872015-03-30 17:20:39 -0700385
Colin Crossfc3674a2017-09-18 17:41:52 -0700386type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700387 useModule, useFiles, useDefaultLibs, invalidVersion bool
388
Colin Cross86a60ae2018-05-29 14:44:55 -0700389 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700390 systemModules string
391
Colin Crossa97c5d32018-03-28 14:58:31 -0700392 frameworkResModule string
393
Colin Cross86a60ae2018-05-29 14:44:55 -0700394 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700395 aidl android.Path
396}
397
Colin Crossa4f08812018-10-02 22:03:40 -0700398type jniLib struct {
399 name string
400 path android.Path
401 target android.Target
402}
403
Colin Cross3144dfc2018-01-03 15:06:47 -0800404func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
405 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
406}
407
408func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
409 return j.shouldInstrument(ctx) &&
410 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
411 ctx.Config().UnbundledBuild())
412}
413
Colin Cross83bb3162018-06-25 15:48:06 -0700414func (j *Module) sdkVersion() string {
415 return String(j.deviceProperties.Sdk_version)
416}
417
418func (j *Module) minSdkVersion() string {
419 if j.deviceProperties.Min_sdk_version != nil {
420 return *j.deviceProperties.Min_sdk_version
421 }
422 return j.sdkVersion()
423}
424
425type sdkContext interface {
426 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
427 sdkVersion() string
428 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
429 minSdkVersion() string
430}
431
432func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
433 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100434 case "", "current", "system_current", "test_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700435 return ctx.Config().DefaultAppTargetSdk()
436 default:
437 return v
438 }
439}
440
441// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
442// it returns android.FutureApiLevel (10000).
443func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
444 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100445 case "", "current", "test_current", "system_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700446 return ctx.Config().DefaultAppTargetSdkInt(), nil
447 default:
448 n := android.GetNumericSdkVersion(v)
449 if i, err := strconv.Atoi(n); err != nil {
450 return -1, fmt.Errorf("invalid sdk version %q", n)
451 } else {
452 return i, nil
453 }
454 }
455}
456
457func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
458 n, err := sdkVersionToNumber(ctx, v)
459 if err != nil {
460 return "", err
461 }
462 return strconv.Itoa(n), nil
463}
464
465func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
466 v := sdkContext.sdkVersion()
467 i, err := sdkVersionToNumber(ctx, v)
468 if err != nil {
469 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700470 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700471 }
472
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900473 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
474 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
475 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
476 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
477 if ctx.DeviceSpecific() || ctx.SocSpecific() {
478 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
479 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
480 }
481 }
482 version := strings.TrimPrefix(v, "system_")
483 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
484 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
485 v, allowed_versions)
486 }
487 }
488
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100489 toPrebuilt := func(sdk string) sdkDep {
490 var api, v string
491 if strings.Contains(sdk, "_") {
492 t := strings.Split(sdk, "_")
493 api = t[0]
494 v = t[1]
495 } else {
496 api = "public"
497 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900498 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100499 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700500 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100501 // There's no aidl for other SDKs yet.
502 // TODO(77525052): Add aidl files for other SDKs too.
503 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
504 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800505 jarPath := android.ExistentPathForSource(ctx, jar)
506 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700507 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700508
Colin Cross6510f912017-11-29 00:27:14 -0800509 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700510 return sdkDep{
511 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700512 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700513 }
514 }
515
Colin Crossfc3674a2017-09-18 17:41:52 -0700516 if !jarPath.Valid() {
517 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
518 return sdkDep{}
519 }
Colin Cross47ff2522017-10-02 14:22:08 -0700520
Colin Crossfc3674a2017-09-18 17:41:52 -0700521 if !aidlPath.Valid() {
522 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
523 return sdkDep{}
524 }
Colin Cross47ff2522017-10-02 14:22:08 -0700525
Colin Crossfc3674a2017-09-18 17:41:52 -0700526 return sdkDep{
527 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700528 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700529 aidl: aidlPath.Path(),
530 }
531 }
532
Colin Crossa97c5d32018-03-28 14:58:31 -0700533 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700534 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700535 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700536 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700537 systemModules: m + "_system_modules",
538 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700539 }
540 if m == "core.current.stubs" {
541 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100542 } else if m == "core.platform.api.stubs" {
543 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700544 }
545 return ret
546 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700547
Colin Cross6510f912017-11-29 00:27:14 -0800548 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100549 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700550 }
551
552 switch v {
553 case "":
554 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700555 useDefaultLibs: true,
556 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700557 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700558 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700559 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700560 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700561 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700562 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700563 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700564 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700565 return toModule("core.current.stubs", "")
Neil Fuller3c979c32018-09-11 09:29:31 +0100566 case "core_platform_current":
567 return toModule("core.platform.api.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700568 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100569 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700570 }
571}
572
Colin Crossbe1da472017-07-07 15:59:46 -0700573func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700574 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700575 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700576 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700577 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700578 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100579 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700580 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700581 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700582 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700583 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100584 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700585 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800586 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700587 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
588 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800589 }
Colin Crossbe1da472017-07-07 15:59:46 -0700590 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700591 } else if j.deviceProperties.System_modules == nil {
592 ctx.PropertyErrorf("no_standard_libs",
593 "system_modules is required to be set when no_standard_libs is true, did you mean no_framework_libs?")
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100594 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700595 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700596 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100597 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700598 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800599 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800600 if ctx.ModuleName() == "android_stubs_current" ||
601 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700602 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700603 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800604 }
Colin Cross2fe66872015-03-30 17:20:39 -0700605 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700606
Colin Cross42d48b72018-08-29 14:10:52 -0700607 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
608 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100609 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700610 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100611 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700612
Colin Cross7f9036c2017-08-30 13:27:57 -0700613 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000614 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700615 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800616 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700617
618 if j.hasSrcExt(".proto") {
619 protoDeps(ctx, &j.protoProperties)
620 }
Colin Cross93e85952017-08-15 13:34:18 -0700621
622 if j.hasSrcExt(".kt") {
623 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
624 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700625 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700626 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800627
628 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700629 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800630 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700631}
632
633func hasSrcExt(srcs []string, ext string) bool {
634 for _, src := range srcs {
635 if filepath.Ext(src) == ext {
636 return true
637 }
638 }
639
640 return false
641}
642
Nan Zhang61eaedb2017-11-02 13:28:15 -0700643func shardPaths(paths android.Paths, shardSize int) []android.Paths {
644 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
645 for len(paths) > shardSize {
646 ret = append(ret, paths[0:shardSize])
647 paths = paths[shardSize:]
648 }
649 if len(paths) > 0 {
650 ret = append(ret, paths)
651 }
652 return ret
653}
654
Colin Cross6af17aa2017-09-20 12:59:05 -0700655func (j *Module) hasSrcExt(ext string) bool {
656 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700657}
658
Colin Cross46c9b8b2017-06-22 16:51:17 -0700659func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700660 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700661
Colin Crossebe1a512017-11-14 13:12:14 -0800662 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
663 aidlIncludes = append(aidlIncludes,
664 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
665 aidlIncludes = append(aidlIncludes,
666 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700667
Steven Moreland667f6882018-07-26 12:55:08 -0700668 flags := []string{"-b"}
669
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700670 if aidlPreprocess.Valid() {
671 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700672 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700673 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700674 }
675
Colin Cross635c3b02016-05-18 15:37:25 -0700676 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800677 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700678 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800679 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700680 flags = append(flags, "-I"+src.String())
681 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700682
Martijn Coeneneab15642018-03-09 09:29:59 +0100683 if Bool(j.deviceProperties.Aidl.Generate_traces) {
684 flags = append(flags, "-t")
685 }
686
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100687 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
688 flags = append(flags, "--transaction_names")
689 }
690
Colin Crossf03c82b2015-04-13 13:53:40 -0700691 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700692}
693
Colin Cross32f676a2017-09-06 13:41:06 -0700694type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800695 classpath classpath
696 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700697 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700698 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700699 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700700 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700701 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800702 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700703 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700704 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700705 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700706 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700707}
Colin Cross2fe66872015-03-30 17:20:39 -0700708
Colin Cross54250902017-12-05 09:28:08 -0800709func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
710 for _, f := range dep.Srcs() {
711 if f.Ext() != ".jar" {
712 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
713 ctx.OtherModuleName(dep.(blueprint.Module)))
714 }
715 }
716}
717
Jiyong Park2d492942018-03-05 17:44:10 +0900718type linkType int
719
720const (
721 javaCore linkType = iota
722 javaSdk
723 javaSystem
724 javaPlatform
725)
726
Colin Crossf19b9bb2018-03-26 14:42:44 -0700727func getLinkType(m *Module, name string) linkType {
Colin Cross83bb3162018-06-25 15:48:06 -0700728 ver := m.sdkVersion()
Colin Cross86a60ae2018-05-29 14:44:55 -0700729 noStdLibs := Bool(m.properties.No_standard_libs)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700730 switch {
Neil Fuller3c979c32018-09-11 09:29:31 +0100731 case name == "core.current.stubs" || ver == "core_current" ||
732 name == "core.platform.api.stubs" || ver == "core_platform_current" ||
733 noStdLibs || name == "stub-annotations" || name == "private-stub-annotations-jar":
Jiyong Park2d492942018-03-05 17:44:10 +0900734 return javaCore
Nan Zhang863f05b2018-08-07 13:41:10 -0700735 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900736 return javaSystem
Nan Zhang863f05b2018-08-07 13:41:10 -0700737 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900738 return javaPlatform
Nan Zhang863f05b2018-08-07 13:41:10 -0700739 case name == "android_stubs_current" || ver == "current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700740 return javaSdk
741 case ver == "":
742 return javaPlatform
743 default:
744 if _, err := strconv.Atoi(ver); err != nil {
745 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
746 }
747 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900748 }
749}
750
Jiyong Park750e5572018-01-31 00:20:13 +0900751func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700752 if ctx.Host() {
753 return
754 }
755
756 myLinkType := getLinkType(from, ctx.ModuleName())
757 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900758 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."
759
760 switch myLinkType {
761 case javaCore:
762 if otherLinkType != javaCore {
763 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 +0900764 ctx.OtherModuleName(to))
765 }
Jiyong Park2d492942018-03-05 17:44:10 +0900766 break
767 case javaSdk:
768 if otherLinkType != javaCore && otherLinkType != javaSdk {
769 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
770 ctx.OtherModuleName(to))
771 }
772 break
773 case javaSystem:
774 if otherLinkType == javaPlatform {
775 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
776 ctx.OtherModuleName(to))
777 }
778 break
779 case javaPlatform:
780 // no restriction on link-type
781 break
Jiyong Park750e5572018-01-31 00:20:13 +0900782 }
783}
784
Colin Cross32f676a2017-09-06 13:41:06 -0700785func (j *Module) collectDeps(ctx android.ModuleContext) deps {
786 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700787
Colin Cross300f0382018-03-06 13:11:51 -0800788 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700789 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800790 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700791 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800792 } else if sdkDep.useFiles {
793 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700794 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800795 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
796 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700797 }
798
Colin Crossd11fcda2017-10-23 17:59:01 -0700799 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700800 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700801 tag := ctx.OtherModuleDependencyTag(module)
802
Colin Crossa4f08812018-10-02 22:03:40 -0700803 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700804 // Handled by AndroidApp.collectAppDeps
805 return
806 }
807 if tag == certificateTag {
808 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700809 return
810 }
811
Jiyong Park750e5572018-01-31 00:20:13 +0900812 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700813 switch tag {
814 case bootClasspathTag, libTag, staticLibTag:
815 checkLinkType(ctx, j, to, tag.(dependencyTag))
816 }
Jiyong Park750e5572018-01-31 00:20:13 +0900817 }
Colin Cross54250902017-12-05 09:28:08 -0800818 switch dep := module.(type) {
819 case Dependency:
820 switch tag {
821 case bootClasspathTag:
822 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
823 case libTag:
824 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900825 // sdk lib names from dependencies are re-exported
826 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800827 case staticLibTag:
828 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
829 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
830 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700831 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900832 // sdk lib names from dependencies are re-exported
833 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700834 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700835 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800836 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100837 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800838 // framework.jar has a one-off dependency on the R.java and Manifest.java files
839 // generated by framework-res.apk
840 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
841 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800842 case frameworkApkTag:
843 if ctx.ModuleName() == "android_stubs_current" ||
844 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700845 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800846 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
847 // resource files out of there for aapt.
848 //
849 // Normally the package rule runs aapt, which includes the resource,
850 // but we're not running that in our package rule so just copy in the
851 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700852 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800853 }
Colin Cross54250902017-12-05 09:28:08 -0800854 case kotlinStdlibTag:
855 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800856 }
857
858 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900859 case SdkLibraryDependency:
860 switch tag {
861 case libTag:
862 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
Jiyong Park1be96912018-05-28 18:02:19 +0900863 // names of sdk libs that are directly depended are exported
864 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900865 default:
866 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
867 }
Colin Cross54250902017-12-05 09:28:08 -0800868 case android.SourceFileProducer:
869 switch tag {
870 case libTag:
871 checkProducesJars(ctx, dep)
872 deps.classpath = append(deps.classpath, dep.Srcs()...)
873 case staticLibTag:
874 checkProducesJars(ctx, dep)
875 deps.classpath = append(deps.classpath, dep.Srcs()...)
876 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
877 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
878 case android.DefaultsDepTag, android.SourceDepTag:
879 // Nothing to do
880 default:
881 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
882 }
883 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700884 switch tag {
885 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700886 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700887 case systemModulesTag:
888 if deps.systemModules != nil {
889 panic("Found two system module dependencies")
890 }
891 sm := module.(*SystemModules)
892 if sm.outputFile == nil {
893 panic("Missing directory for system module dependency")
894 }
895 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700896 default:
897 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700898 }
Colin Crossec7a0422017-07-07 14:47:12 -0700899 }
Colin Cross2fe66872015-03-30 17:20:39 -0700900 })
901
Jiyong Park1be96912018-05-28 18:02:19 +0900902 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
903
Colin Cross32f676a2017-09-06 13:41:06 -0700904 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700905}
906
Colin Cross83bb3162018-06-25 15:48:06 -0700907func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700908 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700909 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
910 if err != nil {
911 ctx.PropertyErrorf("sdk_version", "%s", err)
912 }
Nan Zhang357466b2018-04-17 17:38:36 -0700913 if javaVersion != "" {
914 ret = javaVersion
915 } else if ctx.Device() && sdk <= 23 {
916 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900917 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700918 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700919 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700920 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
921 ret = "1.8"
922 } else {
923 ret = "1.9"
924 }
925
926 return ret
927}
928
Nan Zhanged19fc32017-10-19 13:06:22 -0700929func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700930
Colin Crossf03c82b2015-04-13 13:53:40 -0700931 var flags javaBuilderFlags
932
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100933 // javaVersion flag.
934 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
935
Nan Zhanged19fc32017-10-19 13:06:22 -0700936 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700937 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100938 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700939 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700940 }
Colin Cross6510f912017-11-29 00:27:14 -0800941 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700942 // Override the -g flag passed globally to remove local variable debug info to reduce
943 // disk and memory usage.
944 javacFlags = append(javacFlags, "-g:source,lines")
945 }
Colin Cross64162712017-08-08 13:17:59 -0700946
Colin Cross66548102018-06-19 22:47:35 -0700947 if ctx.Config().RunErrorProne() {
948 if config.ErrorProneClasspath == nil {
949 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
950 }
951
952 errorProneFlags := []string{
953 "-Xplugin:ErrorProne",
954 "${config.ErrorProneChecks}",
955 }
956 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
957
958 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
959 "'" + strings.Join(errorProneFlags, " ") + "'"
960 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800961 }
962
Nan Zhanged19fc32017-10-19 13:06:22 -0700963 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800964 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
965 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700966 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800967
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100968 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800969 !Bool(j.properties.No_standard_libs) &&
970 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
971 // Give host-side tools a version of OpenJDK's standard libraries
972 // close to what they're targeting. As of Dec 2017, AOSP is only
973 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
974 //
975 // When building with OpenJDK 8, the following should have no
976 // effect since those jars would be available by default.
977 //
978 // When building with OpenJDK 9 but targeting a version < 1.8,
979 // putting them on the bootclasspath means that:
980 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
981 // b) references to existing APIs are not reinterpreted in an
982 // OpenJDK 9-specific way, eg. calls to subclasses of
983 // java.nio.Buffer as in http://b/70862583
984 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
985 flags.bootClasspath = append(flags.bootClasspath,
986 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
987 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800988 if Bool(j.properties.Use_tools_jar) {
989 flags.bootClasspath = append(flags.bootClasspath,
990 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
991 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800992 }
993
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100994 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -0700995 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
996 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
997 }
998
Nan Zhanged19fc32017-10-19 13:06:22 -0700999 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001000 if deps.systemModules != nil {
1001 flags.systemModules = append(flags.systemModules, deps.systemModules)
1002 }
1003
Nan Zhanged19fc32017-10-19 13:06:22 -07001004 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -07001005 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -07001006 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001007 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -07001008 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
1009 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -07001010 }
1011
Colin Cross81440082018-08-15 20:21:55 -07001012 if len(javacFlags) > 0 {
1013 // optimization.
1014 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1015 flags.javacFlags = "$javacFlags"
1016 }
1017
Nan Zhanged19fc32017-10-19 13:06:22 -07001018 return flags
1019}
Colin Crossc0b06f12015-04-08 13:03:43 -07001020
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001021func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001022
Colin Crossebe1a512017-11-14 13:12:14 -08001023 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001024
1025 deps := j.collectDeps(ctx)
1026 flags := j.collectBuilderFlags(ctx, deps)
1027
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001028 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001029 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1030 }
1031 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001032 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001033 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001034 }
1035
Colin Crossaf050172017-11-15 23:01:59 -08001036 srcFiles = j.genSources(ctx, srcFiles, flags)
1037
1038 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001039 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001040 srcJars = append(srcJars, extraSrcJars...)
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
Colin Cross1ee23172017-10-18 14:44:18 -07001046 jarName := ctx.ModuleName() + ".jar"
1047
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001048 javaSrcFiles := srcFiles.FilterByExt(".java")
1049 var uniqueSrcFiles android.Paths
1050 set := make(map[string]bool)
1051 for _, v := range javaSrcFiles {
1052 if _, found := set[v.String()]; !found {
1053 set[v.String()] = true
1054 uniqueSrcFiles = append(uniqueSrcFiles, v)
1055 }
1056 }
1057
Colin Cross55f63ea2018-08-27 12:37:09 -07001058 var kotlinJars android.Paths
1059
Colin Cross93e85952017-08-15 13:34:18 -07001060 if srcFiles.HasExt(".kt") {
1061 // If there are kotlin files, compile them first but pass all the kotlin and java files
1062 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1063 // won't emit any classes for them.
1064
1065 flags.kotlincFlags = "-no-stdlib"
1066 if ctx.Device() {
1067 flags.kotlincFlags += " -no-jdk"
1068 }
1069
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001070 var kotlinSrcFiles android.Paths
1071 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1072 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1073
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001074 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001075 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1076 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1077
Colin Cross1ee23172017-10-18 14:44:18 -07001078 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001079 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001080 if ctx.Failed() {
1081 return
1082 }
1083
1084 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001085 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001086 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001087
Colin Cross93e85952017-08-15 13:34:18 -07001088 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001089 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001090 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001091 }
1092
Colin Cross55f63ea2018-08-27 12:37:09 -07001093 jars := append(android.Paths(nil), kotlinJars...)
1094
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001095 // Store the list of .java files that was passed to javac
1096 j.compiledJavaSrcs = uniqueSrcFiles
1097 j.compiledSrcJars = srcJars
1098
Nan Zhang61eaedb2017-11-02 13:28:15 -07001099 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001100 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001101 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1102 enable_sharding = true
1103 if len(j.properties.Annotation_processors) != 0 ||
1104 len(j.properties.Annotation_processor_classes) != 0 {
1105 ctx.PropertyErrorf("javac_shard_size",
1106 "%q cannot be set when annotation processors are enabled.",
1107 j.properties.Javac_shard_size)
1108 }
1109 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001110 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001111 if ctx.Failed() {
1112 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001113 }
1114 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001115 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001116 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001117 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001118 // If error-prone is enabled, add an additional rule to compile the java files into
1119 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001120 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001121 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1122 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001123 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001124 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001125 extraJarDeps = append(extraJarDeps, errorprone)
1126 }
1127
Nan Zhang61eaedb2017-11-02 13:28:15 -07001128 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001129 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001130 shardSize := int(*(j.properties.Javac_shard_size))
1131 var shardSrcs []android.Paths
1132 if len(uniqueSrcFiles) > 0 {
1133 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1134 for idx, shardSrc := range shardSrcs {
1135 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1136 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1137 jars = append(jars, classes)
1138 }
1139 }
1140 if len(srcJars) > 0 {
1141 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1142 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1143 jars = append(jars, classes)
1144 }
1145 } else {
1146 classes := android.PathForModuleOut(ctx, "javac", jarName)
1147 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1148 jars = append(jars, classes)
1149 }
Colin Crossd6891432017-09-27 17:39:56 -07001150 if ctx.Failed() {
1151 return
1152 }
Colin Cross2fe66872015-03-30 17:20:39 -07001153 }
1154
Colin Crosscedd4762018-09-13 11:26:19 -07001155 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1156 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001157 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1158
1159 var resArgs []string
1160 var resDeps android.Paths
1161
1162 resArgs = append(resArgs, dirArgs...)
1163 resDeps = append(resDeps, dirDeps...)
1164
1165 resArgs = append(resArgs, fileArgs...)
1166 resDeps = append(resDeps, fileDeps...)
1167
Colin Crossff3ae9d2018-04-10 16:15:18 -07001168 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001169 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001170 resArgs = append(resArgs, srcArgs...)
1171 resDeps = append(resDeps, srcDeps...)
1172 }
Colin Cross40a36712017-09-27 17:41:35 -07001173
1174 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001175 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001176 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001177 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001178 if ctx.Failed() {
1179 return
1180 }
1181 }
1182
Colin Cross331a1212018-08-15 20:40:52 -07001183 if len(deps.staticResourceJars) > 0 {
1184 var jars android.Paths
1185 if j.resourceJar != nil {
1186 jars = append(jars, j.resourceJar)
1187 }
1188 jars = append(jars, deps.staticResourceJars...)
1189
1190 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1191 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1192 false, nil, nil)
1193 j.resourceJar = combinedJar
1194 }
1195
Colin Cross32f676a2017-09-06 13:41:06 -07001196 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001197 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001198
Colin Cross094054a2018-10-17 15:10:48 -07001199 manifest := j.overrideManifest
1200 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001201 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1202 }
Colin Cross635acc92017-09-12 22:50:46 -07001203
Colin Cross0a6e0072017-08-30 14:24:55 -07001204 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001205 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001206 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001207
1208 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001209 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1210 // Optimization: skip the combine step if there is nothing to do
1211 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1212 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1213 // any if len(jars) == 1.
1214 outputFile = moduleOutPath
1215 } else {
1216 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1217 ctx.Build(pctx, android.BuildParams{
1218 Rule: android.Cp,
1219 Input: jars[0],
1220 Output: combinedJar,
1221 })
1222 outputFile = combinedJar
1223 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001224 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001225 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001226 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001227 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001228 outputFile = combinedJar
1229 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001230
Colin Cross331a1212018-08-15 20:40:52 -07001231 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001232 if j.properties.Jarjar_rules != nil {
1233 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001234 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001235 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001236 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1237 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001238
1239 // jarjar resource jar if necessary
1240 if j.resourceJar != nil {
1241 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1242 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1243 j.resourceJar = resourceJarJarFile
1244 }
1245
Colin Cross0a6e0072017-08-30 14:24:55 -07001246 if ctx.Failed() {
1247 return
1248 }
1249 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001250 j.implementationJarFile = outputFile
1251 if j.headerJarFile == nil {
1252 j.headerJarFile = j.implementationJarFile
1253 }
Colin Cross2fe66872015-03-30 17:20:39 -07001254
Colin Cross6510f912017-11-29 00:27:14 -08001255 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001256 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1257 j.properties.Instrument = true
1258 }
1259 }
1260
Colin Cross3144dfc2018-01-03 15:06:47 -08001261 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001262 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1263 }
1264
Colin Cross331a1212018-08-15 20:40:52 -07001265 // merge implementation jar with resources if necessary
1266 implementationAndResourcesJar := outputFile
1267 if j.resourceJar != nil {
1268 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1269 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1270 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1271 false, nil, nil)
1272 implementationAndResourcesJar = combinedJar
1273 }
1274
1275 j.implementationAndResourcesJar = implementationAndResourcesJar
1276
Colin Cross9ae1b922018-06-26 17:59:05 -07001277 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001278 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001279 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001280 if ctx.Failed() {
1281 return
1282 }
Colin Cross331a1212018-08-15 20:40:52 -07001283
1284 // merge dex jar with resources if necessary
1285 if j.resourceJar != nil {
1286 jars := android.Paths{dexOutputFile, j.resourceJar}
1287 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1288 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1289 false, nil, nil)
1290 dexOutputFile = combinedJar
1291 }
1292
1293 j.dexJarFile = dexOutputFile
1294
Colin Cross3063b782018-08-15 11:19:12 -07001295 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001296 } else {
1297 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001298 }
Colin Cross331a1212018-08-15 20:40:52 -07001299
Colin Crossb7a63242015-04-16 14:09:14 -07001300 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001301
1302 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1303 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001304}
1305
Colin Cross8eadbf02017-10-24 17:46:00 -07001306func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001307 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001308
1309 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001310 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001311 // Compile java sources into turbine.jar.
1312 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1313 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1314 if ctx.Failed() {
1315 return nil
1316 }
1317 jars = append(jars, turbineJar)
1318 }
1319
Colin Cross55f63ea2018-08-27 12:37:09 -07001320 jars = append(jars, extraJars...)
1321
Nan Zhanged19fc32017-10-19 13:06:22 -07001322 // Combine any static header libraries into classes-header.jar. If there is only
1323 // one input jar this step will be skipped.
1324 var headerJar android.Path
1325 jars = append(jars, deps.staticHeaderJars...)
1326
Colin Cross5c6ecc12017-10-23 18:12:27 -07001327 // we cannot skip the combine step for now if there is only one jar
1328 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1329 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001330 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1331 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001332 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001333
1334 if j.properties.Jarjar_rules != nil {
1335 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1336 // Transform classes.jar into classes-jarjar.jar
1337 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1338 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1339 headerJar = jarjarFile
1340 if ctx.Failed() {
1341 return nil
1342 }
1343 }
1344
1345 return headerJar
1346}
1347
Colin Crosscb933592017-11-22 13:49:43 -08001348func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001349 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001350
Colin Cross7a3139e2017-12-19 13:57:50 -08001351 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001352
Colin Cross84c38822018-01-03 15:59:46 -08001353 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001354 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1355
1356 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1357
1358 j.jacocoReportClassesFile = jacocoReportClassesFile
1359
1360 return instrumentedJar
1361}
1362
Colin Crossf506d872017-07-19 15:53:04 -07001363var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001364
Nan Zhanged19fc32017-10-19 13:06:22 -07001365func (j *Module) HeaderJars() android.Paths {
1366 return android.Paths{j.headerJarFile}
1367}
1368
1369func (j *Module) ImplementationJars() android.Paths {
1370 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001371}
1372
Colin Cross331a1212018-08-15 20:40:52 -07001373func (j *Module) ResourceJars() android.Paths {
1374 if j.resourceJar == nil {
1375 return nil
1376 }
1377 return android.Paths{j.resourceJar}
1378}
1379
1380func (j *Module) ImplementationAndResourcesJars() android.Paths {
1381 return android.Paths{j.implementationAndResourcesJar}
1382}
1383
Colin Cross46c9b8b2017-06-22 16:51:17 -07001384func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001385 return j.exportAidlIncludeDirs
1386}
1387
Jiyong Park1be96912018-05-28 18:02:19 +09001388func (j *Module) ExportedSdkLibs() []string {
1389 return j.exportedSdkLibs
1390}
1391
Colin Cross46c9b8b2017-06-22 16:51:17 -07001392var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001393
Colin Cross46c9b8b2017-06-22 16:51:17 -07001394func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001395 return j.logtagsSrcs
1396}
1397
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001398// Collect information for opening IDE project files in java/jdeps.go.
1399func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1400 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1401 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1402 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1403 if j.properties.Jarjar_rules != nil {
1404 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1405 }
1406}
1407
1408func (j *Module) CompilerDeps() []string {
1409 jdeps := []string{}
1410 jdeps = append(jdeps, j.properties.Libs...)
1411 jdeps = append(jdeps, j.properties.Static_libs...)
1412 return jdeps
1413}
1414
Colin Cross2fe66872015-03-30 17:20:39 -07001415//
1416// Java libraries (.jar file)
1417//
1418
Colin Crossf506d872017-07-19 15:53:04 -07001419type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001420 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001421}
1422
Colin Crossf506d872017-07-19 15:53:04 -07001423func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001424 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001425
Colin Cross9ae1b922018-06-26 17:59:05 -07001426 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001427 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1428 ctx.ModuleName()+".jar", j.outputFile)
1429 }
Colin Crossb7a63242015-04-16 14:09:14 -07001430}
1431
Colin Crossf506d872017-07-19 15:53:04 -07001432func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001433 j.deps(ctx)
1434}
1435
Colin Cross9ae1b922018-06-26 17:59:05 -07001436func LibraryFactory() android.Module {
1437 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001438
Colin Cross9ae1b922018-06-26 17:59:05 -07001439 module.AddProperties(
1440 &module.Module.properties,
1441 &module.Module.deviceProperties,
1442 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001443
Colin Cross9ae1b922018-06-26 17:59:05 -07001444 InitJavaModule(module, android.HostAndDeviceSupported)
1445 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001446}
1447
Colin Crossf506d872017-07-19 15:53:04 -07001448func LibraryHostFactory() android.Module {
1449 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001450
Colin Cross6af17aa2017-09-20 12:59:05 -07001451 module.AddProperties(
1452 &module.Module.properties,
1453 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001454
Colin Cross9ae1b922018-06-26 17:59:05 -07001455 module.Module.properties.Installable = proptools.BoolPtr(true)
1456
Colin Cross89536d42017-07-07 14:35:50 -07001457 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001458 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001459}
1460
1461//
Colin Crossb628ea52018-08-14 16:42:33 -07001462// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001463//
1464
1465type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001466 // list of compatibility suites (for example "cts", "vts") that the module should be
1467 // installed into.
1468 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001469
1470 // the name of the test configuration (for example "AndroidTest.xml") that should be
1471 // installed with the module.
1472 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001473
Jack He33338892018-09-19 02:21:28 -07001474 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1475 // should be installed with the module.
1476 Test_config_template *string `android:"arch_variant"`
1477
Colin Crossd96ca352018-08-10 16:06:24 -07001478 // list of files or filegroup modules that provide data that should be installed alongside
1479 // the test
1480 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001481}
1482
1483type Test struct {
1484 Library
1485
1486 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001487
1488 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001489 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001490}
1491
1492func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001493 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001494 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001495
1496 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001497}
1498
1499func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1500 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001501 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001502 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001503 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001504}
1505
1506func TestFactory() android.Module {
1507 module := &Test{}
1508
1509 module.AddProperties(
1510 &module.Module.properties,
1511 &module.Module.deviceProperties,
1512 &module.Module.protoProperties,
1513 &module.testProperties)
1514
Colin Cross9ae1b922018-06-26 17:59:05 -07001515 module.Module.properties.Installable = proptools.BoolPtr(true)
1516
Colin Cross05638fc2018-04-09 18:40:24 -07001517 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001518 return module
1519}
1520
1521func TestHostFactory() android.Module {
1522 module := &Test{}
1523
1524 module.AddProperties(
1525 &module.Module.properties,
1526 &module.Module.protoProperties,
1527 &module.testProperties)
1528
Colin Cross9ae1b922018-06-26 17:59:05 -07001529 module.Module.properties.Installable = proptools.BoolPtr(true)
1530
Colin Cross05638fc2018-04-09 18:40:24 -07001531 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001532 return module
1533}
1534
1535//
Colin Cross2fe66872015-03-30 17:20:39 -07001536// Java Binaries (.jar file plus wrapper script)
1537//
1538
Colin Crossf506d872017-07-19 15:53:04 -07001539type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001540 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001541 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001542
1543 // Name of the class containing main to be inserted into the manifest as Main-Class.
1544 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001545}
1546
Colin Crossf506d872017-07-19 15:53:04 -07001547type Binary struct {
1548 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001549
Colin Crossf506d872017-07-19 15:53:04 -07001550 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001551
Colin Cross6b4a32d2017-12-05 13:42:45 -08001552 isWrapperVariant bool
1553
Colin Crossc3315992017-12-08 19:12:36 -08001554 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001555 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001556}
1557
Alex Light24237172017-10-26 09:46:21 -07001558func (j *Binary) HostToolPath() android.OptionalPath {
1559 return android.OptionalPathForPath(j.binaryFile)
1560}
1561
Colin Crossf506d872017-07-19 15:53:04 -07001562func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001563 if ctx.Arch().ArchType == android.Common {
1564 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001565 if j.binaryProperties.Main_class != nil {
1566 if j.properties.Manifest != nil {
1567 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1568 }
1569 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1570 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1571 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1572 }
1573
Colin Cross6b4a32d2017-12-05 13:42:45 -08001574 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001575 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001576 // Handle the binary wrapper
1577 j.isWrapperVariant = true
1578
Colin Cross366938f2017-12-11 16:29:02 -08001579 if j.binaryProperties.Wrapper != nil {
1580 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001581 } else {
1582 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1583 }
1584
1585 // Depend on the installed jar so that the wrapper doesn't get executed by
1586 // another build rule before the jar has been installed.
1587 jarFile := ctx.PrimaryModule().(*Binary).installFile
1588
1589 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1590 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001591 }
Colin Cross2fe66872015-03-30 17:20:39 -07001592}
1593
Colin Crossf506d872017-07-19 15:53:04 -07001594func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001595 if ctx.Arch().ArchType == android.Common {
1596 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001597 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001598 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001599 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001600}
1601
Colin Crossf506d872017-07-19 15:53:04 -07001602func BinaryFactory() android.Module {
1603 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001604
Colin Cross36242852017-06-23 15:06:31 -07001605 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001606 &module.Module.properties,
1607 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001608 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001609 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001610
Colin Cross9ae1b922018-06-26 17:59:05 -07001611 module.Module.properties.Installable = proptools.BoolPtr(true)
1612
Colin Cross6b4a32d2017-12-05 13:42:45 -08001613 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1614 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001615 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001616}
1617
Colin Crossf506d872017-07-19 15:53:04 -07001618func BinaryHostFactory() android.Module {
1619 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001620
Colin Cross36242852017-06-23 15:06:31 -07001621 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001622 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001623 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001624 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001625
Colin Cross9ae1b922018-06-26 17:59:05 -07001626 module.Module.properties.Installable = proptools.BoolPtr(true)
1627
Colin Cross6b4a32d2017-12-05 13:42:45 -08001628 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1629 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001630 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001631}
1632
1633//
1634// Java prebuilts
1635//
1636
Colin Cross74d73e22017-08-02 11:05:49 -07001637type ImportProperties struct {
1638 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001639
Nan Zhangea568a42017-11-08 21:20:04 -08001640 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001641
1642 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001643
1644 // List of shared java libs that this module has dependencies to
1645 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001646
1647 // List of files to remove from the jar file(s)
1648 Exclude_files []string
1649
1650 // List of directories to remove from the jar file(s)
1651 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001652
1653 // if set to true, run Jetifier against .jar file. Defaults to false.
1654 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001655}
1656
1657type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001658 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001659 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001660 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001661
Colin Cross74d73e22017-08-02 11:05:49 -07001662 properties ImportProperties
1663
Colin Cross0a6e0072017-08-30 14:24:55 -07001664 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001665 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001666}
1667
Colin Cross83bb3162018-06-25 15:48:06 -07001668func (j *Import) sdkVersion() string {
1669 return String(j.properties.Sdk_version)
1670}
1671
1672func (j *Import) minSdkVersion() string {
1673 return j.sdkVersion()
1674}
1675
Colin Cross74d73e22017-08-02 11:05:49 -07001676func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001677 return &j.prebuilt
1678}
1679
Colin Cross74d73e22017-08-02 11:05:49 -07001680func (j *Import) PrebuiltSrcs() []string {
1681 return j.properties.Jars
1682}
1683
1684func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001685 return j.prebuilt.Name(j.ModuleBase.Name())
1686}
1687
Colin Cross74d73e22017-08-02 11:05:49 -07001688func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001689 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001690 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001691}
1692
Colin Cross74d73e22017-08-02 11:05:49 -07001693func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001694 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001695
Nan Zhang4c819fb2018-08-27 18:31:46 -07001696 jarName := ctx.ModuleName() + ".jar"
1697 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001698 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1699 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001700 if Bool(j.properties.Jetifier_enabled) {
1701 inputFile := outputFile
1702 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1703 TransformJetifier(ctx, outputFile, inputFile)
1704 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001705 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001706
1707 ctx.VisitDirectDeps(func(module android.Module) {
1708 otherName := ctx.OtherModuleName(module)
1709 tag := ctx.OtherModuleDependencyTag(module)
1710
1711 switch dep := module.(type) {
1712 case Dependency:
1713 switch tag {
1714 case libTag, staticLibTag:
1715 // sdk lib names from dependencies are re-exported
1716 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1717 }
1718 case SdkLibraryDependency:
1719 switch tag {
1720 case libTag:
1721 // names of sdk libs that are directly depended are exported
1722 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1723 }
1724 }
1725 })
1726
1727 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001728 if Bool(j.properties.Installable) {
1729 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1730 ctx.ModuleName()+".jar", outputFile)
1731 }
Colin Cross2fe66872015-03-30 17:20:39 -07001732}
1733
Colin Cross74d73e22017-08-02 11:05:49 -07001734var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001735
Nan Zhanged19fc32017-10-19 13:06:22 -07001736func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001737 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001738}
1739
1740func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001741 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001742}
1743
Colin Cross331a1212018-08-15 20:40:52 -07001744func (j *Import) ResourceJars() android.Paths {
1745 return nil
1746}
1747
1748func (j *Import) ImplementationAndResourcesJars() android.Paths {
1749 return android.Paths{j.combinedClasspathFile}
1750}
1751
Colin Cross74d73e22017-08-02 11:05:49 -07001752func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001753 return nil
1754}
1755
Jiyong Park1be96912018-05-28 18:02:19 +09001756func (j *Import) ExportedSdkLibs() []string {
1757 return j.exportedSdkLibs
1758}
1759
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001760// Collect information for opening IDE project files in java/jdeps.go.
1761const (
1762 removedPrefix = "prebuilt_"
1763)
1764
1765func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1766 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1767}
1768
1769func (j *Import) IDECustomizedModuleName() string {
1770 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1771 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1772 // solution to get the Import name.
1773 name := j.Name()
1774 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001775 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001776 }
1777 return name
1778}
1779
Colin Cross74d73e22017-08-02 11:05:49 -07001780var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001781
Colin Cross74d73e22017-08-02 11:05:49 -07001782func ImportFactory() android.Module {
1783 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001784
Colin Cross74d73e22017-08-02 11:05:49 -07001785 module.AddProperties(&module.properties)
1786
1787 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001788 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001789 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001790}
1791
Colin Cross74d73e22017-08-02 11:05:49 -07001792func ImportFactoryHost() android.Module {
1793 module := &Import{}
1794
1795 module.AddProperties(&module.properties)
1796
1797 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001798 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001799 return module
1800}
1801
Colin Cross89536d42017-07-07 14:35:50 -07001802//
1803// Defaults
1804//
1805type Defaults struct {
1806 android.ModuleBase
1807 android.DefaultsModuleBase
1808}
1809
1810func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1811}
1812
1813func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1814}
1815
1816func defaultsFactory() android.Module {
1817 return DefaultsFactory()
1818}
1819
1820func DefaultsFactory(props ...interface{}) android.Module {
1821 module := &Defaults{}
1822
1823 module.AddProperties(props...)
1824 module.AddProperties(
1825 &CompilerProperties{},
1826 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001827 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001828 &aaptProperties{},
1829 &androidLibraryProperties{},
1830 &appProperties{},
1831 &appTestProperties{},
1832 &ImportProperties{},
1833 &AARImportProperties{},
1834 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001835 )
1836
1837 android.InitDefaultsModule(module)
1838
1839 return module
1840}
Nan Zhangea568a42017-11-08 21:20:04 -08001841
1842var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001843var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001844var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001845var inList = android.InList