blob: c5414f4fab367b92925b6065a91556699765ba4c [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 Crossbd01e2a2018-10-04 15:21:03 -0700380 certificateTag = dependencyTag{name: "certificate"}
Colin Crossbe1da472017-07-07 15:59:46 -0700381)
Colin Cross2fe66872015-03-30 17:20:39 -0700382
Colin Crossfc3674a2017-09-18 17:41:52 -0700383type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700384 useModule, useFiles, useDefaultLibs, invalidVersion bool
385
Colin Cross86a60ae2018-05-29 14:44:55 -0700386 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700387 systemModules string
388
Colin Crossa97c5d32018-03-28 14:58:31 -0700389 frameworkResModule string
390
Colin Cross86a60ae2018-05-29 14:44:55 -0700391 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700392 aidl android.Path
393}
394
Colin Crossa4f08812018-10-02 22:03:40 -0700395type jniLib struct {
396 name string
397 path android.Path
398 target android.Target
399}
400
Colin Cross3144dfc2018-01-03 15:06:47 -0800401func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
402 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
403}
404
405func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
406 return j.shouldInstrument(ctx) &&
407 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
408 ctx.Config().UnbundledBuild())
409}
410
Colin Cross83bb3162018-06-25 15:48:06 -0700411func (j *Module) sdkVersion() string {
412 return String(j.deviceProperties.Sdk_version)
413}
414
415func (j *Module) minSdkVersion() string {
416 if j.deviceProperties.Min_sdk_version != nil {
417 return *j.deviceProperties.Min_sdk_version
418 }
419 return j.sdkVersion()
420}
421
422type sdkContext interface {
423 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
424 sdkVersion() string
425 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
426 minSdkVersion() string
427}
428
429func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
430 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100431 case "", "current", "system_current", "test_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700432 return ctx.Config().DefaultAppTargetSdk()
433 default:
434 return v
435 }
436}
437
438// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
439// it returns android.FutureApiLevel (10000).
440func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
441 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100442 case "", "current", "test_current", "system_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700443 return ctx.Config().DefaultAppTargetSdkInt(), nil
444 default:
445 n := android.GetNumericSdkVersion(v)
446 if i, err := strconv.Atoi(n); err != nil {
447 return -1, fmt.Errorf("invalid sdk version %q", n)
448 } else {
449 return i, nil
450 }
451 }
452}
453
454func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
455 n, err := sdkVersionToNumber(ctx, v)
456 if err != nil {
457 return "", err
458 }
459 return strconv.Itoa(n), nil
460}
461
462func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
463 v := sdkContext.sdkVersion()
464 i, err := sdkVersionToNumber(ctx, v)
465 if err != nil {
466 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700467 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700468 }
469
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900470 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
471 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
472 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
473 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
474 if ctx.DeviceSpecific() || ctx.SocSpecific() {
475 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
476 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
477 }
478 }
479 version := strings.TrimPrefix(v, "system_")
480 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
481 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
482 v, allowed_versions)
483 }
484 }
485
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100486 toPrebuilt := func(sdk string) sdkDep {
487 var api, v string
488 if strings.Contains(sdk, "_") {
489 t := strings.Split(sdk, "_")
490 api = t[0]
491 v = t[1]
492 } else {
493 api = "public"
494 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900495 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100496 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700497 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100498 // There's no aidl for other SDKs yet.
499 // TODO(77525052): Add aidl files for other SDKs too.
500 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
501 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800502 jarPath := android.ExistentPathForSource(ctx, jar)
503 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700504 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700505
Colin Cross6510f912017-11-29 00:27:14 -0800506 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700507 return sdkDep{
508 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700509 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700510 }
511 }
512
Colin Crossfc3674a2017-09-18 17:41:52 -0700513 if !jarPath.Valid() {
514 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
515 return sdkDep{}
516 }
Colin Cross47ff2522017-10-02 14:22:08 -0700517
Colin Crossfc3674a2017-09-18 17:41:52 -0700518 if !aidlPath.Valid() {
519 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
520 return sdkDep{}
521 }
Colin Cross47ff2522017-10-02 14:22:08 -0700522
Colin Crossfc3674a2017-09-18 17:41:52 -0700523 return sdkDep{
524 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700525 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700526 aidl: aidlPath.Path(),
527 }
528 }
529
Colin Crossa97c5d32018-03-28 14:58:31 -0700530 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700531 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700532 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700533 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700534 systemModules: m + "_system_modules",
535 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700536 }
537 if m == "core.current.stubs" {
538 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100539 } else if m == "core.platform.api.stubs" {
540 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700541 }
542 return ret
543 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700544
Colin Cross6510f912017-11-29 00:27:14 -0800545 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100546 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700547 }
548
549 switch v {
550 case "":
551 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700552 useDefaultLibs: true,
553 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700554 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700555 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700556 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700557 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700558 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700559 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700560 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700561 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700562 return toModule("core.current.stubs", "")
Neil Fuller3c979c32018-09-11 09:29:31 +0100563 case "core_platform_current":
564 return toModule("core.platform.api.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700565 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100566 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700567 }
568}
569
Colin Crossbe1da472017-07-07 15:59:46 -0700570func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700571 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700572 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700573 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700574 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700575 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100576 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700577 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700578 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700579 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700580 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100581 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700582 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800583 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700584 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
585 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800586 }
Colin Crossbe1da472017-07-07 15:59:46 -0700587 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700588 } else if j.deviceProperties.System_modules == nil {
589 ctx.PropertyErrorf("no_standard_libs",
590 "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 +0100591 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700592 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700593 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100594 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700595 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800596 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800597 if ctx.ModuleName() == "android_stubs_current" ||
598 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700599 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700600 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800601 }
Colin Cross2fe66872015-03-30 17:20:39 -0700602 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700603
Colin Cross42d48b72018-08-29 14:10:52 -0700604 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
605 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100606 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700607 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100608 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700609
Colin Cross7f9036c2017-08-30 13:27:57 -0700610 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000611 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700612 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800613 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700614
615 if j.hasSrcExt(".proto") {
616 protoDeps(ctx, &j.protoProperties)
617 }
Colin Cross93e85952017-08-15 13:34:18 -0700618
619 if j.hasSrcExt(".kt") {
620 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
621 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700622 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700623 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800624
625 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700626 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800627 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700628}
629
630func hasSrcExt(srcs []string, ext string) bool {
631 for _, src := range srcs {
632 if filepath.Ext(src) == ext {
633 return true
634 }
635 }
636
637 return false
638}
639
Nan Zhang61eaedb2017-11-02 13:28:15 -0700640func shardPaths(paths android.Paths, shardSize int) []android.Paths {
641 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
642 for len(paths) > shardSize {
643 ret = append(ret, paths[0:shardSize])
644 paths = paths[shardSize:]
645 }
646 if len(paths) > 0 {
647 ret = append(ret, paths)
648 }
649 return ret
650}
651
Colin Cross6af17aa2017-09-20 12:59:05 -0700652func (j *Module) hasSrcExt(ext string) bool {
653 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700654}
655
Colin Cross46c9b8b2017-06-22 16:51:17 -0700656func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700657 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700658
Colin Crossebe1a512017-11-14 13:12:14 -0800659 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
660 aidlIncludes = append(aidlIncludes,
661 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
662 aidlIncludes = append(aidlIncludes,
663 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700664
Steven Moreland667f6882018-07-26 12:55:08 -0700665 flags := []string{"-b"}
666
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700667 if aidlPreprocess.Valid() {
668 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700669 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700670 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700671 }
672
Colin Cross635c3b02016-05-18 15:37:25 -0700673 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800674 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700675 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800676 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700677 flags = append(flags, "-I"+src.String())
678 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700679
Martijn Coeneneab15642018-03-09 09:29:59 +0100680 if Bool(j.deviceProperties.Aidl.Generate_traces) {
681 flags = append(flags, "-t")
682 }
683
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100684 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
685 flags = append(flags, "--transaction_names")
686 }
687
Colin Crossf03c82b2015-04-13 13:53:40 -0700688 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700689}
690
Colin Cross32f676a2017-09-06 13:41:06 -0700691type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800692 classpath classpath
693 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700694 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700695 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700696 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700697 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700698 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800699 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700700 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700701 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700702 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700703 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700704}
Colin Cross2fe66872015-03-30 17:20:39 -0700705
Colin Cross54250902017-12-05 09:28:08 -0800706func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
707 for _, f := range dep.Srcs() {
708 if f.Ext() != ".jar" {
709 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
710 ctx.OtherModuleName(dep.(blueprint.Module)))
711 }
712 }
713}
714
Jiyong Park2d492942018-03-05 17:44:10 +0900715type linkType int
716
717const (
718 javaCore linkType = iota
719 javaSdk
720 javaSystem
721 javaPlatform
722)
723
Colin Crossf19b9bb2018-03-26 14:42:44 -0700724func getLinkType(m *Module, name string) linkType {
Colin Cross83bb3162018-06-25 15:48:06 -0700725 ver := m.sdkVersion()
Colin Cross86a60ae2018-05-29 14:44:55 -0700726 noStdLibs := Bool(m.properties.No_standard_libs)
Colin Crossf19b9bb2018-03-26 14:42:44 -0700727 switch {
Neil Fuller3c979c32018-09-11 09:29:31 +0100728 case name == "core.current.stubs" || ver == "core_current" ||
729 name == "core.platform.api.stubs" || ver == "core_platform_current" ||
730 noStdLibs || name == "stub-annotations" || name == "private-stub-annotations-jar":
Jiyong Park2d492942018-03-05 17:44:10 +0900731 return javaCore
Nan Zhang863f05b2018-08-07 13:41:10 -0700732 case name == "android_system_stubs_current" || strings.HasPrefix(ver, "system_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900733 return javaSystem
Nan Zhang863f05b2018-08-07 13:41:10 -0700734 case name == "android_test_stubs_current" || strings.HasPrefix(ver, "test_"):
Jiyong Park2d492942018-03-05 17:44:10 +0900735 return javaPlatform
Nan Zhang863f05b2018-08-07 13:41:10 -0700736 case name == "android_stubs_current" || ver == "current":
Colin Crossf19b9bb2018-03-26 14:42:44 -0700737 return javaSdk
738 case ver == "":
739 return javaPlatform
740 default:
741 if _, err := strconv.Atoi(ver); err != nil {
742 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
743 }
744 return javaSdk
Jiyong Park2d492942018-03-05 17:44:10 +0900745 }
746}
747
Jiyong Park750e5572018-01-31 00:20:13 +0900748func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700749 if ctx.Host() {
750 return
751 }
752
753 myLinkType := getLinkType(from, ctx.ModuleName())
754 otherLinkType := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900755 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."
756
757 switch myLinkType {
758 case javaCore:
759 if otherLinkType != javaCore {
760 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 +0900761 ctx.OtherModuleName(to))
762 }
Jiyong Park2d492942018-03-05 17:44:10 +0900763 break
764 case javaSdk:
765 if otherLinkType != javaCore && otherLinkType != javaSdk {
766 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
767 ctx.OtherModuleName(to))
768 }
769 break
770 case javaSystem:
771 if otherLinkType == javaPlatform {
772 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
773 ctx.OtherModuleName(to))
774 }
775 break
776 case javaPlatform:
777 // no restriction on link-type
778 break
Jiyong Park750e5572018-01-31 00:20:13 +0900779 }
780}
781
Colin Cross32f676a2017-09-06 13:41:06 -0700782func (j *Module) collectDeps(ctx android.ModuleContext) deps {
783 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700784
Colin Cross300f0382018-03-06 13:11:51 -0800785 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700786 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800787 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700788 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800789 } else if sdkDep.useFiles {
790 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700791 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800792 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
793 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700794 }
795
Colin Crossd11fcda2017-10-23 17:59:01 -0700796 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700797 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700798 tag := ctx.OtherModuleDependencyTag(module)
799
Colin Crossa4f08812018-10-02 22:03:40 -0700800 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700801 // Handled by AndroidApp.collectAppDeps
802 return
803 }
804 if tag == certificateTag {
805 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700806 return
807 }
808
Jiyong Park750e5572018-01-31 00:20:13 +0900809 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700810 switch tag {
811 case bootClasspathTag, libTag, staticLibTag:
812 checkLinkType(ctx, j, to, tag.(dependencyTag))
813 }
Jiyong Park750e5572018-01-31 00:20:13 +0900814 }
Colin Cross54250902017-12-05 09:28:08 -0800815 switch dep := module.(type) {
816 case Dependency:
817 switch tag {
818 case bootClasspathTag:
819 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
820 case libTag:
821 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900822 // sdk lib names from dependencies are re-exported
823 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800824 case staticLibTag:
825 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
826 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
827 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700828 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900829 // sdk lib names from dependencies are re-exported
830 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700831 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700832 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800833 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100834 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800835 // framework.jar has a one-off dependency on the R.java and Manifest.java files
836 // generated by framework-res.apk
837 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
838 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800839 case frameworkApkTag:
840 if ctx.ModuleName() == "android_stubs_current" ||
841 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700842 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800843 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
844 // resource files out of there for aapt.
845 //
846 // Normally the package rule runs aapt, which includes the resource,
847 // but we're not running that in our package rule so just copy in the
848 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700849 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800850 }
Colin Cross54250902017-12-05 09:28:08 -0800851 case kotlinStdlibTag:
852 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800853 }
854
855 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900856 case SdkLibraryDependency:
857 switch tag {
858 case libTag:
859 deps.classpath = append(deps.classpath, dep.HeaderJars(getLinkType(j, ctx.ModuleName()))...)
Jiyong Park1be96912018-05-28 18:02:19 +0900860 // names of sdk libs that are directly depended are exported
861 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900862 default:
863 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
864 }
Colin Cross54250902017-12-05 09:28:08 -0800865 case android.SourceFileProducer:
866 switch tag {
867 case libTag:
868 checkProducesJars(ctx, dep)
869 deps.classpath = append(deps.classpath, dep.Srcs()...)
870 case staticLibTag:
871 checkProducesJars(ctx, dep)
872 deps.classpath = append(deps.classpath, dep.Srcs()...)
873 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
874 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
875 case android.DefaultsDepTag, android.SourceDepTag:
876 // Nothing to do
877 default:
878 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
879 }
880 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700881 switch tag {
882 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700883 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700884 case systemModulesTag:
885 if deps.systemModules != nil {
886 panic("Found two system module dependencies")
887 }
888 sm := module.(*SystemModules)
889 if sm.outputFile == nil {
890 panic("Missing directory for system module dependency")
891 }
892 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700893 default:
894 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700895 }
Colin Crossec7a0422017-07-07 14:47:12 -0700896 }
Colin Cross2fe66872015-03-30 17:20:39 -0700897 })
898
Jiyong Park1be96912018-05-28 18:02:19 +0900899 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
900
Colin Cross32f676a2017-09-06 13:41:06 -0700901 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700902}
903
Colin Cross83bb3162018-06-25 15:48:06 -0700904func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700905 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700906 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
907 if err != nil {
908 ctx.PropertyErrorf("sdk_version", "%s", err)
909 }
Nan Zhang357466b2018-04-17 17:38:36 -0700910 if javaVersion != "" {
911 ret = javaVersion
912 } else if ctx.Device() && sdk <= 23 {
913 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900914 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700915 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700916 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700917 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
918 ret = "1.8"
919 } else {
920 ret = "1.9"
921 }
922
923 return ret
924}
925
Nan Zhanged19fc32017-10-19 13:06:22 -0700926func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700927
Colin Crossf03c82b2015-04-13 13:53:40 -0700928 var flags javaBuilderFlags
929
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100930 // javaVersion flag.
931 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
932
Nan Zhanged19fc32017-10-19 13:06:22 -0700933 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700934 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100935 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700936 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700937 }
Colin Cross6510f912017-11-29 00:27:14 -0800938 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700939 // Override the -g flag passed globally to remove local variable debug info to reduce
940 // disk and memory usage.
941 javacFlags = append(javacFlags, "-g:source,lines")
942 }
Colin Cross64162712017-08-08 13:17:59 -0700943
Colin Cross66548102018-06-19 22:47:35 -0700944 if ctx.Config().RunErrorProne() {
945 if config.ErrorProneClasspath == nil {
946 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
947 }
948
949 errorProneFlags := []string{
950 "-Xplugin:ErrorProne",
951 "${config.ErrorProneChecks}",
952 }
953 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
954
955 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
956 "'" + strings.Join(errorProneFlags, " ") + "'"
957 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800958 }
959
Nan Zhanged19fc32017-10-19 13:06:22 -0700960 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800961 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
962 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700963 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800964
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100965 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800966 !Bool(j.properties.No_standard_libs) &&
967 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
968 // Give host-side tools a version of OpenJDK's standard libraries
969 // close to what they're targeting. As of Dec 2017, AOSP is only
970 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
971 //
972 // When building with OpenJDK 8, the following should have no
973 // effect since those jars would be available by default.
974 //
975 // When building with OpenJDK 9 but targeting a version < 1.8,
976 // putting them on the bootclasspath means that:
977 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
978 // b) references to existing APIs are not reinterpreted in an
979 // OpenJDK 9-specific way, eg. calls to subclasses of
980 // java.nio.Buffer as in http://b/70862583
981 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
982 flags.bootClasspath = append(flags.bootClasspath,
983 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
984 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800985 if Bool(j.properties.Use_tools_jar) {
986 flags.bootClasspath = append(flags.bootClasspath,
987 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
988 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800989 }
990
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100991 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -0700992 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
993 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
994 }
995
Nan Zhanged19fc32017-10-19 13:06:22 -0700996 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700997 if deps.systemModules != nil {
998 flags.systemModules = append(flags.systemModules, deps.systemModules)
999 }
1000
Nan Zhanged19fc32017-10-19 13:06:22 -07001001 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -07001002 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -07001003 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001004 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -07001005 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
1006 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -07001007 }
1008
Colin Cross81440082018-08-15 20:21:55 -07001009 if len(javacFlags) > 0 {
1010 // optimization.
1011 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1012 flags.javacFlags = "$javacFlags"
1013 }
1014
Nan Zhanged19fc32017-10-19 13:06:22 -07001015 return flags
1016}
Colin Crossc0b06f12015-04-08 13:03:43 -07001017
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001018func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001019
Colin Crossebe1a512017-11-14 13:12:14 -08001020 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001021
1022 deps := j.collectDeps(ctx)
1023 flags := j.collectBuilderFlags(ctx, deps)
1024
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001025 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001026 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1027 }
1028 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001029 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001030 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001031 }
1032
Colin Crossaf050172017-11-15 23:01:59 -08001033 srcFiles = j.genSources(ctx, srcFiles, flags)
1034
1035 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001036 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001037 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001038
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001039 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1040 // that IDEInfo struct will use
1041 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1042
Colin Cross1ee23172017-10-18 14:44:18 -07001043 jarName := ctx.ModuleName() + ".jar"
1044
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001045 javaSrcFiles := srcFiles.FilterByExt(".java")
1046 var uniqueSrcFiles android.Paths
1047 set := make(map[string]bool)
1048 for _, v := range javaSrcFiles {
1049 if _, found := set[v.String()]; !found {
1050 set[v.String()] = true
1051 uniqueSrcFiles = append(uniqueSrcFiles, v)
1052 }
1053 }
1054
Colin Cross55f63ea2018-08-27 12:37:09 -07001055 var kotlinJars android.Paths
1056
Colin Cross93e85952017-08-15 13:34:18 -07001057 if srcFiles.HasExt(".kt") {
1058 // If there are kotlin files, compile them first but pass all the kotlin and java files
1059 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1060 // won't emit any classes for them.
1061
1062 flags.kotlincFlags = "-no-stdlib"
1063 if ctx.Device() {
1064 flags.kotlincFlags += " -no-jdk"
1065 }
1066
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001067 var kotlinSrcFiles android.Paths
1068 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1069 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1070
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001071 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001072 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1073 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1074
Colin Cross1ee23172017-10-18 14:44:18 -07001075 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001076 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001077 if ctx.Failed() {
1078 return
1079 }
1080
1081 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001082 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001083 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001084
Colin Cross93e85952017-08-15 13:34:18 -07001085 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001086 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001087 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001088 }
1089
Colin Cross55f63ea2018-08-27 12:37:09 -07001090 jars := append(android.Paths(nil), kotlinJars...)
1091
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001092 // Store the list of .java files that was passed to javac
1093 j.compiledJavaSrcs = uniqueSrcFiles
1094 j.compiledSrcJars = srcJars
1095
Nan Zhang61eaedb2017-11-02 13:28:15 -07001096 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001097 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001098 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1099 enable_sharding = true
1100 if len(j.properties.Annotation_processors) != 0 ||
1101 len(j.properties.Annotation_processor_classes) != 0 {
1102 ctx.PropertyErrorf("javac_shard_size",
1103 "%q cannot be set when annotation processors are enabled.",
1104 j.properties.Javac_shard_size)
1105 }
1106 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001107 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001108 if ctx.Failed() {
1109 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001110 }
1111 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001112 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001113 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001114 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001115 // If error-prone is enabled, add an additional rule to compile the java files into
1116 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001117 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001118 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1119 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001120 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001121 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001122 extraJarDeps = append(extraJarDeps, errorprone)
1123 }
1124
Nan Zhang61eaedb2017-11-02 13:28:15 -07001125 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001126 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001127 shardSize := int(*(j.properties.Javac_shard_size))
1128 var shardSrcs []android.Paths
1129 if len(uniqueSrcFiles) > 0 {
1130 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1131 for idx, shardSrc := range shardSrcs {
1132 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1133 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1134 jars = append(jars, classes)
1135 }
1136 }
1137 if len(srcJars) > 0 {
1138 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1139 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1140 jars = append(jars, classes)
1141 }
1142 } else {
1143 classes := android.PathForModuleOut(ctx, "javac", jarName)
1144 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1145 jars = append(jars, classes)
1146 }
Colin Crossd6891432017-09-27 17:39:56 -07001147 if ctx.Failed() {
1148 return
1149 }
Colin Cross2fe66872015-03-30 17:20:39 -07001150 }
1151
Colin Crosscedd4762018-09-13 11:26:19 -07001152 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1153 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001154 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1155
1156 var resArgs []string
1157 var resDeps android.Paths
1158
1159 resArgs = append(resArgs, dirArgs...)
1160 resDeps = append(resDeps, dirDeps...)
1161
1162 resArgs = append(resArgs, fileArgs...)
1163 resDeps = append(resDeps, fileDeps...)
1164
Colin Crossff3ae9d2018-04-10 16:15:18 -07001165 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001166 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001167 resArgs = append(resArgs, srcArgs...)
1168 resDeps = append(resDeps, srcDeps...)
1169 }
Colin Cross40a36712017-09-27 17:41:35 -07001170
1171 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001172 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001173 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001174 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001175 if ctx.Failed() {
1176 return
1177 }
1178 }
1179
Colin Cross331a1212018-08-15 20:40:52 -07001180 if len(deps.staticResourceJars) > 0 {
1181 var jars android.Paths
1182 if j.resourceJar != nil {
1183 jars = append(jars, j.resourceJar)
1184 }
1185 jars = append(jars, deps.staticResourceJars...)
1186
1187 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1188 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1189 false, nil, nil)
1190 j.resourceJar = combinedJar
1191 }
1192
Colin Cross32f676a2017-09-06 13:41:06 -07001193 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001194 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001195
Colin Cross366938f2017-12-11 16:29:02 -08001196 var manifest android.OptionalPath
1197 if j.properties.Manifest != nil {
1198 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1199 }
Colin Cross635acc92017-09-12 22:50:46 -07001200
Colin Cross0a6e0072017-08-30 14:24:55 -07001201 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001202 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001203 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001204
1205 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001206 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1207 // Optimization: skip the combine step if there is nothing to do
1208 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1209 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1210 // any if len(jars) == 1.
1211 outputFile = moduleOutPath
1212 } else {
1213 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1214 ctx.Build(pctx, android.BuildParams{
1215 Rule: android.Cp,
1216 Input: jars[0],
1217 Output: combinedJar,
1218 })
1219 outputFile = combinedJar
1220 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001221 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001222 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001223 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001224 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001225 outputFile = combinedJar
1226 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001227
Colin Cross331a1212018-08-15 20:40:52 -07001228 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001229 if j.properties.Jarjar_rules != nil {
1230 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001231 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001232 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001233 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1234 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001235
1236 // jarjar resource jar if necessary
1237 if j.resourceJar != nil {
1238 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1239 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1240 j.resourceJar = resourceJarJarFile
1241 }
1242
Colin Cross0a6e0072017-08-30 14:24:55 -07001243 if ctx.Failed() {
1244 return
1245 }
1246 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001247 j.implementationJarFile = outputFile
1248 if j.headerJarFile == nil {
1249 j.headerJarFile = j.implementationJarFile
1250 }
Colin Cross2fe66872015-03-30 17:20:39 -07001251
Colin Cross6510f912017-11-29 00:27:14 -08001252 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001253 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1254 j.properties.Instrument = true
1255 }
1256 }
1257
Colin Cross3144dfc2018-01-03 15:06:47 -08001258 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001259 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1260 }
1261
Colin Cross331a1212018-08-15 20:40:52 -07001262 // merge implementation jar with resources if necessary
1263 implementationAndResourcesJar := outputFile
1264 if j.resourceJar != nil {
1265 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1266 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1267 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1268 false, nil, nil)
1269 implementationAndResourcesJar = combinedJar
1270 }
1271
1272 j.implementationAndResourcesJar = implementationAndResourcesJar
1273
Colin Cross9ae1b922018-06-26 17:59:05 -07001274 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001275 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001276 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001277 if ctx.Failed() {
1278 return
1279 }
Colin Cross331a1212018-08-15 20:40:52 -07001280
1281 // merge dex jar with resources if necessary
1282 if j.resourceJar != nil {
1283 jars := android.Paths{dexOutputFile, j.resourceJar}
1284 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1285 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1286 false, nil, nil)
1287 dexOutputFile = combinedJar
1288 }
1289
1290 j.dexJarFile = dexOutputFile
1291
Colin Cross3063b782018-08-15 11:19:12 -07001292 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001293 } else {
1294 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001295 }
Colin Cross331a1212018-08-15 20:40:52 -07001296
Colin Crossb7a63242015-04-16 14:09:14 -07001297 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001298
1299 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1300 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001301}
1302
Colin Cross8eadbf02017-10-24 17:46:00 -07001303func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001304 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001305
1306 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001307 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001308 // Compile java sources into turbine.jar.
1309 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1310 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1311 if ctx.Failed() {
1312 return nil
1313 }
1314 jars = append(jars, turbineJar)
1315 }
1316
Colin Cross55f63ea2018-08-27 12:37:09 -07001317 jars = append(jars, extraJars...)
1318
Nan Zhanged19fc32017-10-19 13:06:22 -07001319 // Combine any static header libraries into classes-header.jar. If there is only
1320 // one input jar this step will be skipped.
1321 var headerJar android.Path
1322 jars = append(jars, deps.staticHeaderJars...)
1323
Colin Cross5c6ecc12017-10-23 18:12:27 -07001324 // we cannot skip the combine step for now if there is only one jar
1325 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1326 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001327 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1328 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001329 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001330
1331 if j.properties.Jarjar_rules != nil {
1332 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1333 // Transform classes.jar into classes-jarjar.jar
1334 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1335 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1336 headerJar = jarjarFile
1337 if ctx.Failed() {
1338 return nil
1339 }
1340 }
1341
1342 return headerJar
1343}
1344
Colin Crosscb933592017-11-22 13:49:43 -08001345func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001346 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001347
Colin Cross7a3139e2017-12-19 13:57:50 -08001348 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001349
Colin Cross84c38822018-01-03 15:59:46 -08001350 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001351 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1352
1353 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1354
1355 j.jacocoReportClassesFile = jacocoReportClassesFile
1356
1357 return instrumentedJar
1358}
1359
Colin Crossf506d872017-07-19 15:53:04 -07001360var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001361
Nan Zhanged19fc32017-10-19 13:06:22 -07001362func (j *Module) HeaderJars() android.Paths {
1363 return android.Paths{j.headerJarFile}
1364}
1365
1366func (j *Module) ImplementationJars() android.Paths {
1367 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001368}
1369
Colin Cross331a1212018-08-15 20:40:52 -07001370func (j *Module) ResourceJars() android.Paths {
1371 if j.resourceJar == nil {
1372 return nil
1373 }
1374 return android.Paths{j.resourceJar}
1375}
1376
1377func (j *Module) ImplementationAndResourcesJars() android.Paths {
1378 return android.Paths{j.implementationAndResourcesJar}
1379}
1380
Colin Cross46c9b8b2017-06-22 16:51:17 -07001381func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001382 return j.exportAidlIncludeDirs
1383}
1384
Jiyong Park1be96912018-05-28 18:02:19 +09001385func (j *Module) ExportedSdkLibs() []string {
1386 return j.exportedSdkLibs
1387}
1388
Colin Cross46c9b8b2017-06-22 16:51:17 -07001389var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001390
Colin Cross46c9b8b2017-06-22 16:51:17 -07001391func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001392 return j.logtagsSrcs
1393}
1394
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001395// Collect information for opening IDE project files in java/jdeps.go.
1396func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1397 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1398 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1399 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1400 if j.properties.Jarjar_rules != nil {
1401 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1402 }
1403}
1404
1405func (j *Module) CompilerDeps() []string {
1406 jdeps := []string{}
1407 jdeps = append(jdeps, j.properties.Libs...)
1408 jdeps = append(jdeps, j.properties.Static_libs...)
1409 return jdeps
1410}
1411
Colin Cross2fe66872015-03-30 17:20:39 -07001412//
1413// Java libraries (.jar file)
1414//
1415
Colin Crossf506d872017-07-19 15:53:04 -07001416type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001417 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001418}
1419
Colin Crossf506d872017-07-19 15:53:04 -07001420func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001421 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001422
Colin Cross9ae1b922018-06-26 17:59:05 -07001423 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001424 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1425 ctx.ModuleName()+".jar", j.outputFile)
1426 }
Colin Crossb7a63242015-04-16 14:09:14 -07001427}
1428
Colin Crossf506d872017-07-19 15:53:04 -07001429func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001430 j.deps(ctx)
1431}
1432
Colin Cross9ae1b922018-06-26 17:59:05 -07001433func LibraryFactory() android.Module {
1434 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001435
Colin Cross9ae1b922018-06-26 17:59:05 -07001436 module.AddProperties(
1437 &module.Module.properties,
1438 &module.Module.deviceProperties,
1439 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001440
Colin Cross9ae1b922018-06-26 17:59:05 -07001441 InitJavaModule(module, android.HostAndDeviceSupported)
1442 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001443}
1444
Colin Crossf506d872017-07-19 15:53:04 -07001445func LibraryHostFactory() android.Module {
1446 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001447
Colin Cross6af17aa2017-09-20 12:59:05 -07001448 module.AddProperties(
1449 &module.Module.properties,
1450 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001451
Colin Cross9ae1b922018-06-26 17:59:05 -07001452 module.Module.properties.Installable = proptools.BoolPtr(true)
1453
Colin Cross89536d42017-07-07 14:35:50 -07001454 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001455 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001456}
1457
1458//
Colin Crossb628ea52018-08-14 16:42:33 -07001459// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001460//
1461
1462type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001463 // list of compatibility suites (for example "cts", "vts") that the module should be
1464 // installed into.
1465 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001466
1467 // the name of the test configuration (for example "AndroidTest.xml") that should be
1468 // installed with the module.
1469 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001470
Jack He33338892018-09-19 02:21:28 -07001471 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1472 // should be installed with the module.
1473 Test_config_template *string `android:"arch_variant"`
1474
Colin Crossd96ca352018-08-10 16:06:24 -07001475 // list of files or filegroup modules that provide data that should be installed alongside
1476 // the test
1477 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001478}
1479
1480type Test struct {
1481 Library
1482
1483 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001484
1485 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001486 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001487}
1488
1489func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001490 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001491 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001492
1493 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001494}
1495
1496func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1497 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001498 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001499 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001500 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001501}
1502
1503func TestFactory() android.Module {
1504 module := &Test{}
1505
1506 module.AddProperties(
1507 &module.Module.properties,
1508 &module.Module.deviceProperties,
1509 &module.Module.protoProperties,
1510 &module.testProperties)
1511
Colin Cross9ae1b922018-06-26 17:59:05 -07001512 module.Module.properties.Installable = proptools.BoolPtr(true)
1513
Colin Cross05638fc2018-04-09 18:40:24 -07001514 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001515 return module
1516}
1517
1518func TestHostFactory() android.Module {
1519 module := &Test{}
1520
1521 module.AddProperties(
1522 &module.Module.properties,
1523 &module.Module.protoProperties,
1524 &module.testProperties)
1525
Colin Cross9ae1b922018-06-26 17:59:05 -07001526 module.Module.properties.Installable = proptools.BoolPtr(true)
1527
Colin Cross05638fc2018-04-09 18:40:24 -07001528 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001529 return module
1530}
1531
1532//
Colin Cross2fe66872015-03-30 17:20:39 -07001533// Java Binaries (.jar file plus wrapper script)
1534//
1535
Colin Crossf506d872017-07-19 15:53:04 -07001536type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001537 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001538 Wrapper *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001539}
1540
Colin Crossf506d872017-07-19 15:53:04 -07001541type Binary struct {
1542 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001543
Colin Crossf506d872017-07-19 15:53:04 -07001544 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001545
Colin Cross6b4a32d2017-12-05 13:42:45 -08001546 isWrapperVariant bool
1547
Colin Crossc3315992017-12-08 19:12:36 -08001548 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001549 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001550}
1551
Alex Light24237172017-10-26 09:46:21 -07001552func (j *Binary) HostToolPath() android.OptionalPath {
1553 return android.OptionalPathForPath(j.binaryFile)
1554}
1555
Colin Crossf506d872017-07-19 15:53:04 -07001556func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001557 if ctx.Arch().ArchType == android.Common {
1558 // Compile the jar
1559 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001560 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001561 // Handle the binary wrapper
1562 j.isWrapperVariant = true
1563
Colin Cross366938f2017-12-11 16:29:02 -08001564 if j.binaryProperties.Wrapper != nil {
1565 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001566 } else {
1567 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1568 }
1569
1570 // Depend on the installed jar so that the wrapper doesn't get executed by
1571 // another build rule before the jar has been installed.
1572 jarFile := ctx.PrimaryModule().(*Binary).installFile
1573
1574 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1575 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001576 }
Colin Cross2fe66872015-03-30 17:20:39 -07001577}
1578
Colin Crossf506d872017-07-19 15:53:04 -07001579func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001580 if ctx.Arch().ArchType == android.Common {
1581 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001582 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001583 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001584 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001585}
1586
Colin Crossf506d872017-07-19 15:53:04 -07001587func BinaryFactory() android.Module {
1588 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001589
Colin Cross36242852017-06-23 15:06:31 -07001590 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001591 &module.Module.properties,
1592 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001593 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001594 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001595
Colin Cross9ae1b922018-06-26 17:59:05 -07001596 module.Module.properties.Installable = proptools.BoolPtr(true)
1597
Colin Cross6b4a32d2017-12-05 13:42:45 -08001598 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1599 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001600 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001601}
1602
Colin Crossf506d872017-07-19 15:53:04 -07001603func BinaryHostFactory() android.Module {
1604 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001605
Colin Cross36242852017-06-23 15:06:31 -07001606 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001607 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001608 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001609 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001610
Colin Cross9ae1b922018-06-26 17:59:05 -07001611 module.Module.properties.Installable = proptools.BoolPtr(true)
1612
Colin Cross6b4a32d2017-12-05 13:42:45 -08001613 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1614 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001615 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001616}
1617
1618//
1619// Java prebuilts
1620//
1621
Colin Cross74d73e22017-08-02 11:05:49 -07001622type ImportProperties struct {
1623 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001624
Nan Zhangea568a42017-11-08 21:20:04 -08001625 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001626
1627 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001628
1629 // List of shared java libs that this module has dependencies to
1630 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001631
1632 // List of files to remove from the jar file(s)
1633 Exclude_files []string
1634
1635 // List of directories to remove from the jar file(s)
1636 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001637
1638 // if set to true, run Jetifier against .jar file. Defaults to false.
1639 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001640}
1641
1642type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001643 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001644 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001645 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001646
Colin Cross74d73e22017-08-02 11:05:49 -07001647 properties ImportProperties
1648
Colin Cross0a6e0072017-08-30 14:24:55 -07001649 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001650 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001651}
1652
Colin Cross83bb3162018-06-25 15:48:06 -07001653func (j *Import) sdkVersion() string {
1654 return String(j.properties.Sdk_version)
1655}
1656
1657func (j *Import) minSdkVersion() string {
1658 return j.sdkVersion()
1659}
1660
Colin Cross74d73e22017-08-02 11:05:49 -07001661func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001662 return &j.prebuilt
1663}
1664
Colin Cross74d73e22017-08-02 11:05:49 -07001665func (j *Import) PrebuiltSrcs() []string {
1666 return j.properties.Jars
1667}
1668
1669func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001670 return j.prebuilt.Name(j.ModuleBase.Name())
1671}
1672
Colin Cross74d73e22017-08-02 11:05:49 -07001673func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001674 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001675 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001676}
1677
Colin Cross74d73e22017-08-02 11:05:49 -07001678func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001679 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001680
Nan Zhang4c819fb2018-08-27 18:31:46 -07001681 jarName := ctx.ModuleName() + ".jar"
1682 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001683 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1684 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001685 if Bool(j.properties.Jetifier_enabled) {
1686 inputFile := outputFile
1687 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1688 TransformJetifier(ctx, outputFile, inputFile)
1689 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001690 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001691
1692 ctx.VisitDirectDeps(func(module android.Module) {
1693 otherName := ctx.OtherModuleName(module)
1694 tag := ctx.OtherModuleDependencyTag(module)
1695
1696 switch dep := module.(type) {
1697 case Dependency:
1698 switch tag {
1699 case libTag, staticLibTag:
1700 // sdk lib names from dependencies are re-exported
1701 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1702 }
1703 case SdkLibraryDependency:
1704 switch tag {
1705 case libTag:
1706 // names of sdk libs that are directly depended are exported
1707 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1708 }
1709 }
1710 })
1711
1712 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001713 if Bool(j.properties.Installable) {
1714 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1715 ctx.ModuleName()+".jar", outputFile)
1716 }
Colin Cross2fe66872015-03-30 17:20:39 -07001717}
1718
Colin Cross74d73e22017-08-02 11:05:49 -07001719var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001720
Nan Zhanged19fc32017-10-19 13:06:22 -07001721func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001722 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001723}
1724
1725func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001726 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001727}
1728
Colin Cross331a1212018-08-15 20:40:52 -07001729func (j *Import) ResourceJars() android.Paths {
1730 return nil
1731}
1732
1733func (j *Import) ImplementationAndResourcesJars() android.Paths {
1734 return android.Paths{j.combinedClasspathFile}
1735}
1736
Colin Cross74d73e22017-08-02 11:05:49 -07001737func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001738 return nil
1739}
1740
Jiyong Park1be96912018-05-28 18:02:19 +09001741func (j *Import) ExportedSdkLibs() []string {
1742 return j.exportedSdkLibs
1743}
1744
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001745// Collect information for opening IDE project files in java/jdeps.go.
1746const (
1747 removedPrefix = "prebuilt_"
1748)
1749
1750func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1751 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1752}
1753
1754func (j *Import) IDECustomizedModuleName() string {
1755 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1756 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1757 // solution to get the Import name.
1758 name := j.Name()
1759 if strings.HasPrefix(name, removedPrefix) {
1760 name = strings.Trim(name, removedPrefix)
1761 }
1762 return name
1763}
1764
Colin Cross74d73e22017-08-02 11:05:49 -07001765var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001766
Colin Cross74d73e22017-08-02 11:05:49 -07001767func ImportFactory() android.Module {
1768 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001769
Colin Cross74d73e22017-08-02 11:05:49 -07001770 module.AddProperties(&module.properties)
1771
1772 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001773 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001774 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001775}
1776
Colin Cross74d73e22017-08-02 11:05:49 -07001777func ImportFactoryHost() android.Module {
1778 module := &Import{}
1779
1780 module.AddProperties(&module.properties)
1781
1782 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001783 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001784 return module
1785}
1786
Colin Cross89536d42017-07-07 14:35:50 -07001787//
1788// Defaults
1789//
1790type Defaults struct {
1791 android.ModuleBase
1792 android.DefaultsModuleBase
1793}
1794
1795func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1796}
1797
1798func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1799}
1800
1801func defaultsFactory() android.Module {
1802 return DefaultsFactory()
1803}
1804
1805func DefaultsFactory(props ...interface{}) android.Module {
1806 module := &Defaults{}
1807
1808 module.AddProperties(props...)
1809 module.AddProperties(
1810 &CompilerProperties{},
1811 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001812 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001813 &aaptProperties{},
1814 &androidLibraryProperties{},
1815 &appProperties{},
1816 &appTestProperties{},
1817 &ImportProperties{},
1818 &AARImportProperties{},
1819 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001820 )
1821
1822 android.InitDefaultsModule(module)
1823
1824 return module
1825}
Nan Zhangea568a42017-11-08 21:20:04 -08001826
1827var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001828var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001829var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001830var inList = android.InList