blob: 7ef3626d6f54d5409c51eafc651b5af80a5a20d9 [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
312 // list of SDK lib names that this java moudule is exporting
313 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700314
315 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
316 // filter out Exclude_srcs, will be used by android.IDEInfo struct
317 expandIDEInfoCompiledSrcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700318}
319
Colin Cross54250902017-12-05 09:28:08 -0800320func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700321 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800322}
323
324var _ android.SourceFileProducer = (*Module)(nil)
325
Colin Crossf506d872017-07-19 15:53:04 -0700326type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700327 HeaderJars() android.Paths
328 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700329 ResourceJars() android.Paths
330 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700331 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900332 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700333}
334
Jiyong Parkc678ad32018-04-10 13:07:10 +0900335type SdkLibraryDependency interface {
336 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900337 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338}
339
Nan Zhangb2b33de2018-02-23 11:18:47 -0800340type SrcDependency interface {
341 CompiledSrcs() android.Paths
342 CompiledSrcJars() android.Paths
343}
344
345func (j *Module) CompiledSrcs() android.Paths {
346 return j.compiledJavaSrcs
347}
348
349func (j *Module) CompiledSrcJars() android.Paths {
350 return j.compiledSrcJars
351}
352
353var _ SrcDependency = (*Module)(nil)
354
Colin Cross89536d42017-07-07 14:35:50 -0700355func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
356 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
357 android.InitDefaultableModule(module)
358}
359
Colin Crossbe1da472017-07-07 15:59:46 -0700360type dependencyTag struct {
361 blueprint.BaseDependencyTag
362 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700363}
364
Colin Crossa4f08812018-10-02 22:03:40 -0700365type jniDependencyTag struct {
366 blueprint.BaseDependencyTag
367 target android.Target
368}
369
Colin Crossbe1da472017-07-07 15:59:46 -0700370var (
Colin Cross6ade34f2017-09-15 13:00:47 -0700371 staticLibTag = dependencyTag{name: "staticlib"}
372 libTag = dependencyTag{name: "javalib"}
Colin Cross6a77c982018-06-19 22:43:34 -0700373 annoTag = dependencyTag{name: "annotation processor"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700374 bootClasspathTag = dependencyTag{name: "bootclasspath"}
Colin Cross1369cdb2017-09-29 17:58:17 -0700375 systemModulesTag = dependencyTag{name: "system modules"}
Colin Cross6ade34f2017-09-15 13:00:47 -0700376 frameworkResTag = dependencyTag{name: "framework-res"}
Nan Zhangb2b33de2018-02-23 11:18:47 -0800377 frameworkApkTag = dependencyTag{name: "framework-apk"}
Colin Cross93e85952017-08-15 13:34:18 -0700378 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Cross66dbc0b2017-12-28 12:23:20 -0800379 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
Colin Crossbe1da472017-07-07 15:59:46 -0700380)
Colin Cross2fe66872015-03-30 17:20:39 -0700381
Colin Crossfc3674a2017-09-18 17:41:52 -0700382type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700383 useModule, useFiles, useDefaultLibs, invalidVersion bool
384
Colin Cross86a60ae2018-05-29 14:44:55 -0700385 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700386 systemModules string
387
Colin Crossa97c5d32018-03-28 14:58:31 -0700388 frameworkResModule string
389
Colin Cross86a60ae2018-05-29 14:44:55 -0700390 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700391 aidl android.Path
392}
393
Colin Crossa4f08812018-10-02 22:03:40 -0700394type jniLib struct {
395 name string
396 path android.Path
397 target android.Target
398}
399
Colin Cross3144dfc2018-01-03 15:06:47 -0800400func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
401 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
402}
403
404func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
405 return j.shouldInstrument(ctx) &&
406 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
407 ctx.Config().UnbundledBuild())
408}
409
Colin Cross83bb3162018-06-25 15:48:06 -0700410func (j *Module) sdkVersion() string {
411 return String(j.deviceProperties.Sdk_version)
412}
413
414func (j *Module) minSdkVersion() string {
415 if j.deviceProperties.Min_sdk_version != nil {
416 return *j.deviceProperties.Min_sdk_version
417 }
418 return j.sdkVersion()
419}
420
421type sdkContext interface {
422 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
423 sdkVersion() string
424 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
425 minSdkVersion() string
426}
427
428func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
429 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100430 case "", "current", "system_current", "test_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700431 return ctx.Config().DefaultAppTargetSdk()
432 default:
433 return v
434 }
435}
436
437// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
438// it returns android.FutureApiLevel (10000).
439func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
440 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100441 case "", "current", "test_current", "system_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700442 return ctx.Config().DefaultAppTargetSdkInt(), nil
443 default:
444 n := android.GetNumericSdkVersion(v)
445 if i, err := strconv.Atoi(n); err != nil {
446 return -1, fmt.Errorf("invalid sdk version %q", n)
447 } else {
448 return i, nil
449 }
450 }
451}
452
453func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
454 n, err := sdkVersionToNumber(ctx, v)
455 if err != nil {
456 return "", err
457 }
458 return strconv.Itoa(n), nil
459}
460
461func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
462 v := sdkContext.sdkVersion()
463 i, err := sdkVersionToNumber(ctx, v)
464 if err != nil {
465 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700466 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700467 }
468
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900469 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
470 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
471 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
472 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
473 if ctx.DeviceSpecific() || ctx.SocSpecific() {
474 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
475 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
476 }
477 }
478 version := strings.TrimPrefix(v, "system_")
479 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
480 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
481 v, allowed_versions)
482 }
483 }
484
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100485 toPrebuilt := func(sdk string) sdkDep {
486 var api, v string
487 if strings.Contains(sdk, "_") {
488 t := strings.Split(sdk, "_")
489 api = t[0]
490 v = t[1]
491 } else {
492 api = "public"
493 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900494 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100495 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700496 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100497 // There's no aidl for other SDKs yet.
498 // TODO(77525052): Add aidl files for other SDKs too.
499 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
500 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800501 jarPath := android.ExistentPathForSource(ctx, jar)
502 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700503 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700504
Colin Cross6510f912017-11-29 00:27:14 -0800505 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700506 return sdkDep{
507 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700508 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700509 }
510 }
511
Colin Crossfc3674a2017-09-18 17:41:52 -0700512 if !jarPath.Valid() {
513 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
514 return sdkDep{}
515 }
Colin Cross47ff2522017-10-02 14:22:08 -0700516
Colin Crossfc3674a2017-09-18 17:41:52 -0700517 if !aidlPath.Valid() {
518 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
519 return sdkDep{}
520 }
Colin Cross47ff2522017-10-02 14:22:08 -0700521
Colin Crossfc3674a2017-09-18 17:41:52 -0700522 return sdkDep{
523 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700524 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700525 aidl: aidlPath.Path(),
526 }
527 }
528
Colin Crossa97c5d32018-03-28 14:58:31 -0700529 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700530 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700531 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700532 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700533 systemModules: m + "_system_modules",
534 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700535 }
536 if m == "core.current.stubs" {
537 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100538 } else if m == "core.platform.api.stubs" {
539 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700540 }
541 return ret
542 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700543
Colin Cross6510f912017-11-29 00:27:14 -0800544 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100545 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700546 }
547
548 switch v {
549 case "":
550 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700551 useDefaultLibs: true,
552 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700553 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700554 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700555 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700556 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700557 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700558 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700559 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700560 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700561 return toModule("core.current.stubs", "")
Neil Fuller3c979c32018-09-11 09:29:31 +0100562 case "core_platform_current":
563 return toModule("core.platform.api.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700564 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100565 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700566 }
567}
568
Colin Crossbe1da472017-07-07 15:59:46 -0700569func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700570 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700571 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700572 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700573 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700574 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100575 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700576 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700577 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700578 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700579 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100580 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700581 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800582 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700583 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
584 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800585 }
Colin Crossbe1da472017-07-07 15:59:46 -0700586 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700587 } else if j.deviceProperties.System_modules == nil {
588 ctx.PropertyErrorf("no_standard_libs",
589 "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 +0100590 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700591 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700592 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100593 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700594 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800595 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800596 if ctx.ModuleName() == "android_stubs_current" ||
597 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700598 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700599 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800600 }
Colin Cross2fe66872015-03-30 17:20:39 -0700601 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700602
Colin Cross42d48b72018-08-29 14:10:52 -0700603 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
604 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100605 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700606 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100607 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700608
Colin Cross7f9036c2017-08-30 13:27:57 -0700609 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000610 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700611 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800612 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700613
614 if j.hasSrcExt(".proto") {
615 protoDeps(ctx, &j.protoProperties)
616 }
Colin Cross93e85952017-08-15 13:34:18 -0700617
618 if j.hasSrcExt(".kt") {
619 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
620 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700621 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700622 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800623
624 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700625 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800626 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700627}
628
629func hasSrcExt(srcs []string, ext string) bool {
630 for _, src := range srcs {
631 if filepath.Ext(src) == ext {
632 return true
633 }
634 }
635
636 return false
637}
638
Nan Zhang61eaedb2017-11-02 13:28:15 -0700639func shardPaths(paths android.Paths, shardSize int) []android.Paths {
640 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
641 for len(paths) > shardSize {
642 ret = append(ret, paths[0:shardSize])
643 paths = paths[shardSize:]
644 }
645 if len(paths) > 0 {
646 ret = append(ret, paths)
647 }
648 return ret
649}
650
Colin Cross6af17aa2017-09-20 12:59:05 -0700651func (j *Module) hasSrcExt(ext string) bool {
652 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700653}
654
Colin Cross46c9b8b2017-06-22 16:51:17 -0700655func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700656 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700657
Colin Crossebe1a512017-11-14 13:12:14 -0800658 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
659 aidlIncludes = append(aidlIncludes,
660 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
661 aidlIncludes = append(aidlIncludes,
662 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700663
Steven Moreland667f6882018-07-26 12:55:08 -0700664 flags := []string{"-b"}
665
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700666 if aidlPreprocess.Valid() {
667 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700668 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700669 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700670 }
671
Colin Cross635c3b02016-05-18 15:37:25 -0700672 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800673 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700674 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800675 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700676 flags = append(flags, "-I"+src.String())
677 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700678
Martijn Coeneneab15642018-03-09 09:29:59 +0100679 if Bool(j.deviceProperties.Aidl.Generate_traces) {
680 flags = append(flags, "-t")
681 }
682
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100683 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
684 flags = append(flags, "--transaction_names")
685 }
686
Colin Crossf03c82b2015-04-13 13:53:40 -0700687 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700688}
689
Colin Cross32f676a2017-09-06 13:41:06 -0700690type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800691 classpath classpath
692 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700693 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700694 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700695 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700696 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700697 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800698 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700699 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700700 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700701 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700702 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700703}
Colin Cross2fe66872015-03-30 17:20:39 -0700704
Colin Cross54250902017-12-05 09:28:08 -0800705func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
706 for _, f := range dep.Srcs() {
707 if f.Ext() != ".jar" {
708 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
709 ctx.OtherModuleName(dep.(blueprint.Module)))
710 }
711 }
712}
713
Jiyong Park2d492942018-03-05 17:44:10 +0900714type linkType int
715
716const (
717 javaCore linkType = iota
718 javaSdk
719 javaSystem
720 javaPlatform
721)
722
Colin Crossf19b9bb2018-03-26 14:42:44 -0700723func getLinkType(m *Module, name string) linkType {
Colin Cross83bb3162018-06-25 15:48:06 -0700724 ver := m.sdkVersion()
Colin Cross86a60ae2018-05-29 14:44:55 -0700725 noStdLibs := Bool(m.properties.No_standard_libs)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700726 switch {
Neil Fuller3c979c32018-09-11 09:29:31 +0100727 case name == "core.current.stubs" || ver == "core_current" ||
728 name == "core.platform.api.stubs" || ver == "core_platform_current" ||
729 noStdLibs || name == "stub-annotations" || name == "private-stub-annotations-jar":
Jiyong Park2d492942018-03-05 17:44:10 +0900730 return javaCore
Nan Zhang863f05b2018-08-07 13:41:10 -0700731 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900732 return javaSystem
Nan Zhang863f05b2018-08-07 13:41:10 -0700733 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900734 return javaPlatform
Nan Zhang863f05b2018-08-07 13:41:10 -0700735 case name == "android_stubs_current" || ver == "current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700736 return javaSdk
737 case ver == "":
738 return javaPlatform
739 default:
740 if _, err := strconv.Atoi(ver); err != nil {
741 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
742 }
743 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900744 }
745}
746
Jiyong Park750e5572018-01-31 00:20:13 +0900747func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700748 if ctx.Host() {
749 return
750 }
751
752 myLinkType := getLinkType(from, ctx.ModuleName())
753 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900754 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."
755
756 switch myLinkType {
757 case javaCore:
758 if otherLinkType != javaCore {
759 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 +0900760 ctx.OtherModuleName(to))
761 }
Jiyong Park2d492942018-03-05 17:44:10 +0900762 break
763 case javaSdk:
764 if otherLinkType != javaCore && otherLinkType != javaSdk {
765 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
766 ctx.OtherModuleName(to))
767 }
768 break
769 case javaSystem:
770 if otherLinkType == javaPlatform {
771 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
772 ctx.OtherModuleName(to))
773 }
774 break
775 case javaPlatform:
776 // no restriction on link-type
777 break
Jiyong Park750e5572018-01-31 00:20:13 +0900778 }
779}
780
Colin Cross32f676a2017-09-06 13:41:06 -0700781func (j *Module) collectDeps(ctx android.ModuleContext) deps {
782 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700783
Colin Cross300f0382018-03-06 13:11:51 -0800784 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700785 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800786 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700787 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800788 } else if sdkDep.useFiles {
789 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700790 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800791 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
792 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700793 }
794
Colin Crossd11fcda2017-10-23 17:59:01 -0700795 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700796 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700797 tag := ctx.OtherModuleDependencyTag(module)
798
Colin Crossa4f08812018-10-02 22:03:40 -0700799 if _, ok := tag.(*jniDependencyTag); ok {
800 // Handled by AndroidApp.collectJniDeps
801 return
802 }
803
Jiyong Park750e5572018-01-31 00:20:13 +0900804 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700805 switch tag {
806 case bootClasspathTag, libTag, staticLibTag:
807 checkLinkType(ctx, j, to, tag.(dependencyTag))
808 }
Jiyong Park750e5572018-01-31 00:20:13 +0900809 }
Colin Cross54250902017-12-05 09:28:08 -0800810 switch dep := module.(type) {
811 case Dependency:
812 switch tag {
813 case bootClasspathTag:
814 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
815 case libTag:
816 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900817 // sdk lib names from dependencies are re-exported
818 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800819 case staticLibTag:
820 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
821 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
822 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700823 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900824 // sdk lib names from dependencies are re-exported
825 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700826 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700827 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800828 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100829 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800830 // framework.jar has a one-off dependency on the R.java and Manifest.java files
831 // generated by framework-res.apk
832 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
833 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800834 case frameworkApkTag:
835 if ctx.ModuleName() == "android_stubs_current" ||
836 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700837 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800838 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
839 // resource files out of there for aapt.
840 //
841 // Normally the package rule runs aapt, which includes the resource,
842 // but we're not running that in our package rule so just copy in the
843 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700844 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800845 }
Colin Cross54250902017-12-05 09:28:08 -0800846 case kotlinStdlibTag:
847 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800848 }
849
850 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900851 case SdkLibraryDependency:
852 switch tag {
853 case libTag:
854 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
Jiyong Park1be96912018-05-28 18:02:19 +0900855 // names of sdk libs that are directly depended are exported
856 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900857 default:
858 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
859 }
Colin Cross54250902017-12-05 09:28:08 -0800860 case android.SourceFileProducer:
861 switch tag {
862 case libTag:
863 checkProducesJars(ctx, dep)
864 deps.classpath = append(deps.classpath, dep.Srcs()...)
865 case staticLibTag:
866 checkProducesJars(ctx, dep)
867 deps.classpath = append(deps.classpath, dep.Srcs()...)
868 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
869 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
870 case android.DefaultsDepTag, android.SourceDepTag:
871 // Nothing to do
872 default:
873 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
874 }
875 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700876 switch tag {
877 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700878 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700879 case systemModulesTag:
880 if deps.systemModules != nil {
881 panic("Found two system module dependencies")
882 }
883 sm := module.(*SystemModules)
884 if sm.outputFile == nil {
885 panic("Missing directory for system module dependency")
886 }
887 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700888 default:
889 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700890 }
Colin Crossec7a0422017-07-07 14:47:12 -0700891 }
Colin Cross2fe66872015-03-30 17:20:39 -0700892 })
893
Jiyong Park1be96912018-05-28 18:02:19 +0900894 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
895
Colin Cross32f676a2017-09-06 13:41:06 -0700896 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700897}
898
Colin Cross83bb3162018-06-25 15:48:06 -0700899func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700900 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700901 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
902 if err != nil {
903 ctx.PropertyErrorf("sdk_version", "%s", err)
904 }
Nan Zhang357466b2018-04-17 17:38:36 -0700905 if javaVersion != "" {
906 ret = javaVersion
907 } else if ctx.Device() && sdk <= 23 {
908 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900909 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700910 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700911 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700912 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
913 ret = "1.8"
914 } else {
915 ret = "1.9"
916 }
917
918 return ret
919}
920
Nan Zhanged19fc32017-10-19 13:06:22 -0700921func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700922
Colin Crossf03c82b2015-04-13 13:53:40 -0700923 var flags javaBuilderFlags
924
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100925 // javaVersion flag.
926 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
927
Nan Zhanged19fc32017-10-19 13:06:22 -0700928 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700929 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100930 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700931 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700932 }
Colin Cross6510f912017-11-29 00:27:14 -0800933 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700934 // Override the -g flag passed globally to remove local variable debug info to reduce
935 // disk and memory usage.
936 javacFlags = append(javacFlags, "-g:source,lines")
937 }
Colin Cross64162712017-08-08 13:17:59 -0700938
Colin Cross66548102018-06-19 22:47:35 -0700939 if ctx.Config().RunErrorProne() {
940 if config.ErrorProneClasspath == nil {
941 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
942 }
943
944 errorProneFlags := []string{
945 "-Xplugin:ErrorProne",
946 "${config.ErrorProneChecks}",
947 }
948 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
949
950 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
951 "'" + strings.Join(errorProneFlags, " ") + "'"
952 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800953 }
954
Nan Zhanged19fc32017-10-19 13:06:22 -0700955 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800956 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
957 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700958 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800959
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100960 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800961 !Bool(j.properties.No_standard_libs) &&
962 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
963 // Give host-side tools a version of OpenJDK's standard libraries
964 // close to what they're targeting. As of Dec 2017, AOSP is only
965 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
966 //
967 // When building with OpenJDK 8, the following should have no
968 // effect since those jars would be available by default.
969 //
970 // When building with OpenJDK 9 but targeting a version < 1.8,
971 // putting them on the bootclasspath means that:
972 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
973 // b) references to existing APIs are not reinterpreted in an
974 // OpenJDK 9-specific way, eg. calls to subclasses of
975 // java.nio.Buffer as in http://b/70862583
976 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
977 flags.bootClasspath = append(flags.bootClasspath,
978 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
979 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800980 if Bool(j.properties.Use_tools_jar) {
981 flags.bootClasspath = append(flags.bootClasspath,
982 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
983 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800984 }
985
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100986 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -0700987 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
988 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
989 }
990
Nan Zhanged19fc32017-10-19 13:06:22 -0700991 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700992 if deps.systemModules != nil {
993 flags.systemModules = append(flags.systemModules, deps.systemModules)
994 }
995
Nan Zhanged19fc32017-10-19 13:06:22 -0700996 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700997 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700998 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700999 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -07001000 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
1001 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -07001002 }
1003
Colin Cross81440082018-08-15 20:21:55 -07001004 if len(javacFlags) > 0 {
1005 // optimization.
1006 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1007 flags.javacFlags = "$javacFlags"
1008 }
1009
Nan Zhanged19fc32017-10-19 13:06:22 -07001010 return flags
1011}
Colin Crossc0b06f12015-04-08 13:03:43 -07001012
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001013func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001014
Colin Crossebe1a512017-11-14 13:12:14 -08001015 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001016
1017 deps := j.collectDeps(ctx)
1018 flags := j.collectBuilderFlags(ctx, deps)
1019
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001020 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001021 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1022 }
1023 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001024 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001025 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001026 }
1027
Colin Crossaf050172017-11-15 23:01:59 -08001028 srcFiles = j.genSources(ctx, srcFiles, flags)
1029
1030 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001031 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001032 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001033
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001034 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1035 // that IDEInfo struct will use
1036 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1037
Colin Cross1ee23172017-10-18 14:44:18 -07001038 jarName := ctx.ModuleName() + ".jar"
1039
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001040 javaSrcFiles := srcFiles.FilterByExt(".java")
1041 var uniqueSrcFiles android.Paths
1042 set := make(map[string]bool)
1043 for _, v := range javaSrcFiles {
1044 if _, found := set[v.String()]; !found {
1045 set[v.String()] = true
1046 uniqueSrcFiles = append(uniqueSrcFiles, v)
1047 }
1048 }
1049
Colin Cross55f63ea2018-08-27 12:37:09 -07001050 var kotlinJars android.Paths
1051
Colin Cross93e85952017-08-15 13:34:18 -07001052 if srcFiles.HasExt(".kt") {
1053 // If there are kotlin files, compile them first but pass all the kotlin and java files
1054 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1055 // won't emit any classes for them.
1056
1057 flags.kotlincFlags = "-no-stdlib"
1058 if ctx.Device() {
1059 flags.kotlincFlags += " -no-jdk"
1060 }
1061
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001062 var kotlinSrcFiles android.Paths
1063 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1064 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1065
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001066 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001067 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1068 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1069
Colin Cross1ee23172017-10-18 14:44:18 -07001070 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001071 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001072 if ctx.Failed() {
1073 return
1074 }
1075
1076 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001077 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001078 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001079
Colin Cross93e85952017-08-15 13:34:18 -07001080 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001081 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001082 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001083 }
1084
Colin Cross55f63ea2018-08-27 12:37:09 -07001085 jars := append(android.Paths(nil), kotlinJars...)
1086
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001087 // Store the list of .java files that was passed to javac
1088 j.compiledJavaSrcs = uniqueSrcFiles
1089 j.compiledSrcJars = srcJars
1090
Nan Zhang61eaedb2017-11-02 13:28:15 -07001091 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001092 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001093 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1094 enable_sharding = true
1095 if len(j.properties.Annotation_processors) != 0 ||
1096 len(j.properties.Annotation_processor_classes) != 0 {
1097 ctx.PropertyErrorf("javac_shard_size",
1098 "%q cannot be set when annotation processors are enabled.",
1099 j.properties.Javac_shard_size)
1100 }
1101 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001102 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001103 if ctx.Failed() {
1104 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001105 }
1106 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001107 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001108 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001109 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001110 // If error-prone is enabled, add an additional rule to compile the java files into
1111 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001112 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001113 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1114 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001115 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001116 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001117 extraJarDeps = append(extraJarDeps, errorprone)
1118 }
1119
Nan Zhang61eaedb2017-11-02 13:28:15 -07001120 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001121 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001122 shardSize := int(*(j.properties.Javac_shard_size))
1123 var shardSrcs []android.Paths
1124 if len(uniqueSrcFiles) > 0 {
1125 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1126 for idx, shardSrc := range shardSrcs {
1127 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1128 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1129 jars = append(jars, classes)
1130 }
1131 }
1132 if len(srcJars) > 0 {
1133 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1134 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1135 jars = append(jars, classes)
1136 }
1137 } else {
1138 classes := android.PathForModuleOut(ctx, "javac", jarName)
1139 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1140 jars = append(jars, classes)
1141 }
Colin Crossd6891432017-09-27 17:39:56 -07001142 if ctx.Failed() {
1143 return
1144 }
Colin Cross2fe66872015-03-30 17:20:39 -07001145 }
1146
Colin Crosscedd4762018-09-13 11:26:19 -07001147 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1148 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001149 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1150
1151 var resArgs []string
1152 var resDeps android.Paths
1153
1154 resArgs = append(resArgs, dirArgs...)
1155 resDeps = append(resDeps, dirDeps...)
1156
1157 resArgs = append(resArgs, fileArgs...)
1158 resDeps = append(resDeps, fileDeps...)
1159
Colin Crossff3ae9d2018-04-10 16:15:18 -07001160 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001161 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001162 resArgs = append(resArgs, srcArgs...)
1163 resDeps = append(resDeps, srcDeps...)
1164 }
Colin Cross40a36712017-09-27 17:41:35 -07001165
1166 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001167 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001168 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001169 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001170 if ctx.Failed() {
1171 return
1172 }
1173 }
1174
Colin Cross331a1212018-08-15 20:40:52 -07001175 if len(deps.staticResourceJars) > 0 {
1176 var jars android.Paths
1177 if j.resourceJar != nil {
1178 jars = append(jars, j.resourceJar)
1179 }
1180 jars = append(jars, deps.staticResourceJars...)
1181
1182 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1183 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1184 false, nil, nil)
1185 j.resourceJar = combinedJar
1186 }
1187
Colin Cross32f676a2017-09-06 13:41:06 -07001188 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001189 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001190
Colin Cross366938f2017-12-11 16:29:02 -08001191 var manifest android.OptionalPath
1192 if j.properties.Manifest != nil {
1193 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1194 }
Colin Cross635acc92017-09-12 22:50:46 -07001195
Colin Cross0a6e0072017-08-30 14:24:55 -07001196 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001197 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001198 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001199
1200 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001201 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1202 // Optimization: skip the combine step if there is nothing to do
1203 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1204 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1205 // any if len(jars) == 1.
1206 outputFile = moduleOutPath
1207 } else {
1208 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1209 ctx.Build(pctx, android.BuildParams{
1210 Rule: android.Cp,
1211 Input: jars[0],
1212 Output: combinedJar,
1213 })
1214 outputFile = combinedJar
1215 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001216 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001217 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001218 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001219 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001220 outputFile = combinedJar
1221 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001222
Colin Cross331a1212018-08-15 20:40:52 -07001223 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001224 if j.properties.Jarjar_rules != nil {
1225 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001226 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001227 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001228 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1229 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001230
1231 // jarjar resource jar if necessary
1232 if j.resourceJar != nil {
1233 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1234 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1235 j.resourceJar = resourceJarJarFile
1236 }
1237
Colin Cross0a6e0072017-08-30 14:24:55 -07001238 if ctx.Failed() {
1239 return
1240 }
1241 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001242 j.implementationJarFile = outputFile
1243 if j.headerJarFile == nil {
1244 j.headerJarFile = j.implementationJarFile
1245 }
Colin Cross2fe66872015-03-30 17:20:39 -07001246
Colin Cross6510f912017-11-29 00:27:14 -08001247 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001248 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1249 j.properties.Instrument = true
1250 }
1251 }
1252
Colin Cross3144dfc2018-01-03 15:06:47 -08001253 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001254 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1255 }
1256
Colin Cross331a1212018-08-15 20:40:52 -07001257 // merge implementation jar with resources if necessary
1258 implementationAndResourcesJar := outputFile
1259 if j.resourceJar != nil {
1260 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1261 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1262 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1263 false, nil, nil)
1264 implementationAndResourcesJar = combinedJar
1265 }
1266
1267 j.implementationAndResourcesJar = implementationAndResourcesJar
1268
Colin Cross9ae1b922018-06-26 17:59:05 -07001269 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001270 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001271 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001272 if ctx.Failed() {
1273 return
1274 }
Colin Cross331a1212018-08-15 20:40:52 -07001275
1276 // merge dex jar with resources if necessary
1277 if j.resourceJar != nil {
1278 jars := android.Paths{dexOutputFile, j.resourceJar}
1279 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1280 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1281 false, nil, nil)
1282 dexOutputFile = combinedJar
1283 }
1284
1285 j.dexJarFile = dexOutputFile
1286
Colin Cross3063b782018-08-15 11:19:12 -07001287 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001288 } else {
1289 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001290 }
Colin Cross331a1212018-08-15 20:40:52 -07001291
Colin Crossb7a63242015-04-16 14:09:14 -07001292 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001293
1294 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1295 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001296}
1297
Colin Cross8eadbf02017-10-24 17:46:00 -07001298func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001299 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001300
1301 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001302 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001303 // Compile java sources into turbine.jar.
1304 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1305 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1306 if ctx.Failed() {
1307 return nil
1308 }
1309 jars = append(jars, turbineJar)
1310 }
1311
Colin Cross55f63ea2018-08-27 12:37:09 -07001312 jars = append(jars, extraJars...)
1313
Nan Zhanged19fc32017-10-19 13:06:22 -07001314 // Combine any static header libraries into classes-header.jar. If there is only
1315 // one input jar this step will be skipped.
1316 var headerJar android.Path
1317 jars = append(jars, deps.staticHeaderJars...)
1318
Colin Cross5c6ecc12017-10-23 18:12:27 -07001319 // we cannot skip the combine step for now if there is only one jar
1320 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1321 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001322 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1323 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001324 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001325
1326 if j.properties.Jarjar_rules != nil {
1327 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1328 // Transform classes.jar into classes-jarjar.jar
1329 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1330 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1331 headerJar = jarjarFile
1332 if ctx.Failed() {
1333 return nil
1334 }
1335 }
1336
1337 return headerJar
1338}
1339
Colin Crosscb933592017-11-22 13:49:43 -08001340func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001341 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001342
Colin Cross7a3139e2017-12-19 13:57:50 -08001343 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001344
Colin Cross84c38822018-01-03 15:59:46 -08001345 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001346 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1347
1348 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1349
1350 j.jacocoReportClassesFile = jacocoReportClassesFile
1351
1352 return instrumentedJar
1353}
1354
Colin Crossf506d872017-07-19 15:53:04 -07001355var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001356
Nan Zhanged19fc32017-10-19 13:06:22 -07001357func (j *Module) HeaderJars() android.Paths {
1358 return android.Paths{j.headerJarFile}
1359}
1360
1361func (j *Module) ImplementationJars() android.Paths {
1362 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001363}
1364
Colin Cross331a1212018-08-15 20:40:52 -07001365func (j *Module) ResourceJars() android.Paths {
1366 if j.resourceJar == nil {
1367 return nil
1368 }
1369 return android.Paths{j.resourceJar}
1370}
1371
1372func (j *Module) ImplementationAndResourcesJars() android.Paths {
1373 return android.Paths{j.implementationAndResourcesJar}
1374}
1375
Colin Cross46c9b8b2017-06-22 16:51:17 -07001376func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001377 return j.exportAidlIncludeDirs
1378}
1379
Jiyong Park1be96912018-05-28 18:02:19 +09001380func (j *Module) ExportedSdkLibs() []string {
1381 return j.exportedSdkLibs
1382}
1383
Colin Cross46c9b8b2017-06-22 16:51:17 -07001384var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001385
Colin Cross46c9b8b2017-06-22 16:51:17 -07001386func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001387 return j.logtagsSrcs
1388}
1389
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001390// Collect information for opening IDE project files in java/jdeps.go.
1391func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1392 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1393 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1394 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1395 if j.properties.Jarjar_rules != nil {
1396 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1397 }
1398}
1399
1400func (j *Module) CompilerDeps() []string {
1401 jdeps := []string{}
1402 jdeps = append(jdeps, j.properties.Libs...)
1403 jdeps = append(jdeps, j.properties.Static_libs...)
1404 return jdeps
1405}
1406
Colin Cross2fe66872015-03-30 17:20:39 -07001407//
1408// Java libraries (.jar file)
1409//
1410
Colin Crossf506d872017-07-19 15:53:04 -07001411type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001412 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001413}
1414
Colin Crossf506d872017-07-19 15:53:04 -07001415func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001416 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001417
Colin Cross9ae1b922018-06-26 17:59:05 -07001418 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001419 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1420 ctx.ModuleName()+".jar", j.outputFile)
1421 }
Colin Crossb7a63242015-04-16 14:09:14 -07001422}
1423
Colin Crossf506d872017-07-19 15:53:04 -07001424func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001425 j.deps(ctx)
1426}
1427
Colin Cross9ae1b922018-06-26 17:59:05 -07001428func LibraryFactory() android.Module {
1429 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001430
Colin Cross9ae1b922018-06-26 17:59:05 -07001431 module.AddProperties(
1432 &module.Module.properties,
1433 &module.Module.deviceProperties,
1434 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001435
Colin Cross9ae1b922018-06-26 17:59:05 -07001436 InitJavaModule(module, android.HostAndDeviceSupported)
1437 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001438}
1439
Colin Crossf506d872017-07-19 15:53:04 -07001440func LibraryHostFactory() android.Module {
1441 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001442
Colin Cross6af17aa2017-09-20 12:59:05 -07001443 module.AddProperties(
1444 &module.Module.properties,
1445 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001446
Colin Cross9ae1b922018-06-26 17:59:05 -07001447 module.Module.properties.Installable = proptools.BoolPtr(true)
1448
Colin Cross89536d42017-07-07 14:35:50 -07001449 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001450 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001451}
1452
1453//
Colin Crossb628ea52018-08-14 16:42:33 -07001454// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001455//
1456
1457type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001458 // list of compatibility suites (for example "cts", "vts") that the module should be
1459 // installed into.
1460 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001461
1462 // the name of the test configuration (for example "AndroidTest.xml") that should be
1463 // installed with the module.
1464 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001465
Jack He33338892018-09-19 02:21:28 -07001466 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1467 // should be installed with the module.
1468 Test_config_template *string `android:"arch_variant"`
1469
Colin Crossd96ca352018-08-10 16:06:24 -07001470 // list of files or filegroup modules that provide data that should be installed alongside
1471 // the test
1472 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001473}
1474
1475type Test struct {
1476 Library
1477
1478 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001479
1480 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001481 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001482}
1483
1484func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001485 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001486 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001487
1488 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001489}
1490
1491func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1492 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001493 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001494 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001495 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001496}
1497
1498func TestFactory() android.Module {
1499 module := &Test{}
1500
1501 module.AddProperties(
1502 &module.Module.properties,
1503 &module.Module.deviceProperties,
1504 &module.Module.protoProperties,
1505 &module.testProperties)
1506
Colin Cross9ae1b922018-06-26 17:59:05 -07001507 module.Module.properties.Installable = proptools.BoolPtr(true)
1508
Colin Cross05638fc2018-04-09 18:40:24 -07001509 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001510 return module
1511}
1512
1513func TestHostFactory() android.Module {
1514 module := &Test{}
1515
1516 module.AddProperties(
1517 &module.Module.properties,
1518 &module.Module.protoProperties,
1519 &module.testProperties)
1520
Colin Cross9ae1b922018-06-26 17:59:05 -07001521 module.Module.properties.Installable = proptools.BoolPtr(true)
1522
Colin Cross05638fc2018-04-09 18:40:24 -07001523 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001524 return module
1525}
1526
1527//
Colin Cross2fe66872015-03-30 17:20:39 -07001528// Java Binaries (.jar file plus wrapper script)
1529//
1530
Colin Crossf506d872017-07-19 15:53:04 -07001531type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001532 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001533 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001534}
1535
Colin Crossf506d872017-07-19 15:53:04 -07001536type Binary struct {
1537 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001538
Colin Crossf506d872017-07-19 15:53:04 -07001539 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001540
Colin Cross6b4a32d2017-12-05 13:42:45 -08001541 isWrapperVariant bool
1542
Colin Crossc3315992017-12-08 19:12:36 -08001543 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001544 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001545}
1546
Alex Light24237172017-10-26 09:46:21 -07001547func (j *Binary) HostToolPath() android.OptionalPath {
1548 return android.OptionalPathForPath(j.binaryFile)
1549}
1550
Colin Crossf506d872017-07-19 15:53:04 -07001551func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001552 if ctx.Arch().ArchType == android.Common {
1553 // Compile the jar
1554 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001555 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001556 // Handle the binary wrapper
1557 j.isWrapperVariant = true
1558
Colin Cross366938f2017-12-11 16:29:02 -08001559 if j.binaryProperties.Wrapper != nil {
1560 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001561 } else {
1562 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1563 }
1564
1565 // Depend on the installed jar so that the wrapper doesn't get executed by
1566 // another build rule before the jar has been installed.
1567 jarFile := ctx.PrimaryModule().(*Binary).installFile
1568
1569 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1570 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001571 }
Colin Cross2fe66872015-03-30 17:20:39 -07001572}
1573
Colin Crossf506d872017-07-19 15:53:04 -07001574func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001575 if ctx.Arch().ArchType == android.Common {
1576 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001577 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001578 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001579 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001580}
1581
Colin Crossf506d872017-07-19 15:53:04 -07001582func BinaryFactory() android.Module {
1583 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001584
Colin Cross36242852017-06-23 15:06:31 -07001585 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001586 &module.Module.properties,
1587 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001588 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001589 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001590
Colin Cross9ae1b922018-06-26 17:59:05 -07001591 module.Module.properties.Installable = proptools.BoolPtr(true)
1592
Colin Cross6b4a32d2017-12-05 13:42:45 -08001593 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1594 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001595 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001596}
1597
Colin Crossf506d872017-07-19 15:53:04 -07001598func BinaryHostFactory() android.Module {
1599 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001600
Colin Cross36242852017-06-23 15:06:31 -07001601 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001602 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001603 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001604 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001605
Colin Cross9ae1b922018-06-26 17:59:05 -07001606 module.Module.properties.Installable = proptools.BoolPtr(true)
1607
Colin Cross6b4a32d2017-12-05 13:42:45 -08001608 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1609 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001610 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001611}
1612
1613//
1614// Java prebuilts
1615//
1616
Colin Cross74d73e22017-08-02 11:05:49 -07001617type ImportProperties struct {
1618 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001619
Nan Zhangea568a42017-11-08 21:20:04 -08001620 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001621
1622 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001623
1624 // List of shared java libs that this module has dependencies to
1625 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001626
1627 // List of files to remove from the jar file(s)
1628 Exclude_files []string
1629
1630 // List of directories to remove from the jar file(s)
1631 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001632
1633 // if set to true, run Jetifier against .jar file. Defaults to false.
1634 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001635}
1636
1637type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001638 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001639 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001640 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001641
Colin Cross74d73e22017-08-02 11:05:49 -07001642 properties ImportProperties
1643
Colin Cross0a6e0072017-08-30 14:24:55 -07001644 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001645 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001646}
1647
Colin Cross83bb3162018-06-25 15:48:06 -07001648func (j *Import) sdkVersion() string {
1649 return String(j.properties.Sdk_version)
1650}
1651
1652func (j *Import) minSdkVersion() string {
1653 return j.sdkVersion()
1654}
1655
Colin Cross74d73e22017-08-02 11:05:49 -07001656func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001657 return &j.prebuilt
1658}
1659
Colin Cross74d73e22017-08-02 11:05:49 -07001660func (j *Import) PrebuiltSrcs() []string {
1661 return j.properties.Jars
1662}
1663
1664func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001665 return j.prebuilt.Name(j.ModuleBase.Name())
1666}
1667
Colin Cross74d73e22017-08-02 11:05:49 -07001668func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001669 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001670 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001671}
1672
Colin Cross74d73e22017-08-02 11:05:49 -07001673func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001674 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001675
Nan Zhang4c819fb2018-08-27 18:31:46 -07001676 jarName := ctx.ModuleName() + ".jar"
1677 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001678 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1679 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001680 if Bool(j.properties.Jetifier_enabled) {
1681 inputFile := outputFile
1682 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1683 TransformJetifier(ctx, outputFile, inputFile)
1684 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001685 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001686
1687 ctx.VisitDirectDeps(func(module android.Module) {
1688 otherName := ctx.OtherModuleName(module)
1689 tag := ctx.OtherModuleDependencyTag(module)
1690
1691 switch dep := module.(type) {
1692 case Dependency:
1693 switch tag {
1694 case libTag, staticLibTag:
1695 // sdk lib names from dependencies are re-exported
1696 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1697 }
1698 case SdkLibraryDependency:
1699 switch tag {
1700 case libTag:
1701 // names of sdk libs that are directly depended are exported
1702 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1703 }
1704 }
1705 })
1706
1707 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001708 if Bool(j.properties.Installable) {
1709 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1710 ctx.ModuleName()+".jar", outputFile)
1711 }
Colin Cross2fe66872015-03-30 17:20:39 -07001712}
1713
Colin Cross74d73e22017-08-02 11:05:49 -07001714var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001715
Nan Zhanged19fc32017-10-19 13:06:22 -07001716func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001717 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001718}
1719
1720func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001721 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001722}
1723
Colin Cross331a1212018-08-15 20:40:52 -07001724func (j *Import) ResourceJars() android.Paths {
1725 return nil
1726}
1727
1728func (j *Import) ImplementationAndResourcesJars() android.Paths {
1729 return android.Paths{j.combinedClasspathFile}
1730}
1731
Colin Cross74d73e22017-08-02 11:05:49 -07001732func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001733 return nil
1734}
1735
Jiyong Park1be96912018-05-28 18:02:19 +09001736func (j *Import) ExportedSdkLibs() []string {
1737 return j.exportedSdkLibs
1738}
1739
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001740// Collect information for opening IDE project files in java/jdeps.go.
1741const (
1742 removedPrefix = "prebuilt_"
1743)
1744
1745func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1746 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1747}
1748
1749func (j *Import) IDECustomizedModuleName() string {
1750 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1751 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1752 // solution to get the Import name.
1753 name := j.Name()
1754 if strings.HasPrefix(name, removedPrefix) {
1755 name = strings.Trim(name, removedPrefix)
1756 }
1757 return name
1758}
1759
Colin Cross74d73e22017-08-02 11:05:49 -07001760var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001761
Colin Cross74d73e22017-08-02 11:05:49 -07001762func ImportFactory() android.Module {
1763 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001764
Colin Cross74d73e22017-08-02 11:05:49 -07001765 module.AddProperties(&module.properties)
1766
1767 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001768 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001769 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001770}
1771
Colin Cross74d73e22017-08-02 11:05:49 -07001772func ImportFactoryHost() android.Module {
1773 module := &Import{}
1774
1775 module.AddProperties(&module.properties)
1776
1777 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001778 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001779 return module
1780}
1781
Colin Cross89536d42017-07-07 14:35:50 -07001782//
1783// Defaults
1784//
1785type Defaults struct {
1786 android.ModuleBase
1787 android.DefaultsModuleBase
1788}
1789
1790func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1791}
1792
1793func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1794}
1795
1796func defaultsFactory() android.Module {
1797 return DefaultsFactory()
1798}
1799
1800func DefaultsFactory(props ...interface{}) android.Module {
1801 module := &Defaults{}
1802
1803 module.AddProperties(props...)
1804 module.AddProperties(
1805 &CompilerProperties{},
1806 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001807 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001808 &aaptProperties{},
1809 &androidLibraryProperties{},
1810 &appProperties{},
1811 &appTestProperties{},
1812 &ImportProperties{},
1813 &AARImportProperties{},
1814 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001815 )
1816
1817 android.InitDefaultsModule(module)
1818
1819 return module
1820}
Nan Zhangea568a42017-11-08 21:20:04 -08001821
1822var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001823var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001824var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001825var inList = android.InList