blob: 1a73133e4062a36004317561c70e37b89cf99074 [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 Cross5a0dcd52018-10-05 14:20:06 -0700260
261 UncompressDex bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700262}
263
Colin Cross46c9b8b2017-06-22 16:51:17 -0700264// Module contains the properties and members used by all java module types
265type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700266 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700267 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700268
Colin Cross89536d42017-07-07 14:35:50 -0700269 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700270 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700271 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700272
Colin Cross331a1212018-08-15 20:40:52 -0700273 // jar file containing header classes including static library dependencies, suitable for
274 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700275 headerJarFile android.Path
276
Colin Cross331a1212018-08-15 20:40:52 -0700277 // jar file containing implementation classes including static library dependencies but no
278 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700279 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700280
Colin Cross331a1212018-08-15 20:40:52 -0700281 // jar file containing only resources including from static library dependencies
282 resourceJar android.Path
283
284 // jar file containing implementation classes and resources including static library
285 // dependencies
286 implementationAndResourcesJar android.Path
287
288 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700289 dexJarFile android.Path
290
Colin Crosscb933592017-11-22 13:49:43 -0800291 // output file containing uninstrumented classes that will be instrumented by jacoco
292 jacocoReportClassesFile android.Path
293
Colin Cross66dbc0b2017-12-28 12:23:20 -0800294 // output file containing mapping of obfuscated names
295 proguardDictionary android.Path
296
Colin Cross331a1212018-08-15 20:40:52 -0700297 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700298 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700299
Colin Cross635c3b02016-05-18 15:37:25 -0700300 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700301
Colin Cross635c3b02016-05-18 15:37:25 -0700302 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700303
Colin Cross2fe66872015-03-30 17:20:39 -0700304 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700305 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800306
307 // list of .java files and srcjars that was passed to javac
308 compiledJavaSrcs android.Paths
309 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800310
311 // list of extra progurad flag files
312 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900313
Colin Cross094054a2018-10-17 15:10:48 -0700314 // manifest file to use instead of properties.Manifest
315 overrideManifest android.OptionalPath
316
Jiyong Park1be96912018-05-28 18:02:19 +0900317 // list of SDK lib names that this java moudule is exporting
318 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700319
320 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
321 // filter out Exclude_srcs, will be used by android.IDEInfo struct
322 expandIDEInfoCompiledSrcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700323}
324
Colin Cross54250902017-12-05 09:28:08 -0800325func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700326 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800327}
328
329var _ android.SourceFileProducer = (*Module)(nil)
330
Colin Crossf506d872017-07-19 15:53:04 -0700331type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700332 HeaderJars() android.Paths
333 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700334 ResourceJars() android.Paths
335 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700336 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900337 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700338}
339
Jiyong Parkc678ad32018-04-10 13:07:10 +0900340type SdkLibraryDependency interface {
341 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900342 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900343}
344
Nan Zhangb2b33de2018-02-23 11:18:47 -0800345type SrcDependency interface {
346 CompiledSrcs() android.Paths
347 CompiledSrcJars() android.Paths
348}
349
350func (j *Module) CompiledSrcs() android.Paths {
351 return j.compiledJavaSrcs
352}
353
354func (j *Module) CompiledSrcJars() android.Paths {
355 return j.compiledSrcJars
356}
357
358var _ SrcDependency = (*Module)(nil)
359
Colin Cross89536d42017-07-07 14:35:50 -0700360func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
361 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
362 android.InitDefaultableModule(module)
363}
364
Colin Crossbe1da472017-07-07 15:59:46 -0700365type dependencyTag struct {
366 blueprint.BaseDependencyTag
367 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700368}
369
Colin Crossa4f08812018-10-02 22:03:40 -0700370type jniDependencyTag struct {
371 blueprint.BaseDependencyTag
372 target android.Target
373}
374
Colin Crossbe1da472017-07-07 15:59:46 -0700375var (
Colin Cross4b964c02018-10-15 16:18:06 -0700376 staticLibTag = dependencyTag{name: "staticlib"}
377 libTag = dependencyTag{name: "javalib"}
378 annoTag = dependencyTag{name: "annotation processor"}
379 bootClasspathTag = dependencyTag{name: "bootclasspath"}
380 systemModulesTag = dependencyTag{name: "system modules"}
381 frameworkResTag = dependencyTag{name: "framework-res"}
382 frameworkApkTag = dependencyTag{name: "framework-apk"}
383 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
384 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
385 certificateTag = dependencyTag{name: "certificate"}
386 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700387)
Colin Cross2fe66872015-03-30 17:20:39 -0700388
Colin Crossfc3674a2017-09-18 17:41:52 -0700389type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700390 useModule, useFiles, useDefaultLibs, invalidVersion bool
391
Colin Cross86a60ae2018-05-29 14:44:55 -0700392 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700393 systemModules string
394
Colin Crossa97c5d32018-03-28 14:58:31 -0700395 frameworkResModule string
396
Colin Cross86a60ae2018-05-29 14:44:55 -0700397 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700398 aidl android.Path
399}
400
Colin Crossa4f08812018-10-02 22:03:40 -0700401type jniLib struct {
402 name string
403 path android.Path
404 target android.Target
405}
406
Colin Cross3144dfc2018-01-03 15:06:47 -0800407func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
408 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
409}
410
411func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
412 return j.shouldInstrument(ctx) &&
413 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
414 ctx.Config().UnbundledBuild())
415}
416
Colin Cross83bb3162018-06-25 15:48:06 -0700417func (j *Module) sdkVersion() string {
418 return String(j.deviceProperties.Sdk_version)
419}
420
421func (j *Module) minSdkVersion() string {
422 if j.deviceProperties.Min_sdk_version != nil {
423 return *j.deviceProperties.Min_sdk_version
424 }
425 return j.sdkVersion()
426}
427
428type sdkContext interface {
429 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
430 sdkVersion() string
431 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
432 minSdkVersion() string
433}
434
435func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
436 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100437 case "", "current", "system_current", "test_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700438 return ctx.Config().DefaultAppTargetSdk()
439 default:
440 return v
441 }
442}
443
444// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
445// it returns android.FutureApiLevel (10000).
446func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
447 switch v {
Neil Fuller3c979c32018-09-11 09:29:31 +0100448 case "", "current", "test_current", "system_current", "core_current", "core_platform_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700449 return ctx.Config().DefaultAppTargetSdkInt(), nil
450 default:
451 n := android.GetNumericSdkVersion(v)
452 if i, err := strconv.Atoi(n); err != nil {
453 return -1, fmt.Errorf("invalid sdk version %q", n)
454 } else {
455 return i, nil
456 }
457 }
458}
459
460func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
461 n, err := sdkVersionToNumber(ctx, v)
462 if err != nil {
463 return "", err
464 }
465 return strconv.Itoa(n), nil
466}
467
468func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
469 v := sdkContext.sdkVersion()
470 i, err := sdkVersionToNumber(ctx, v)
471 if err != nil {
472 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700473 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700474 }
475
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900476 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
477 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
478 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
479 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
480 if ctx.DeviceSpecific() || ctx.SocSpecific() {
481 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
482 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
483 }
484 }
485 version := strings.TrimPrefix(v, "system_")
486 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
487 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
488 v, allowed_versions)
489 }
490 }
491
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100492 toPrebuilt := func(sdk string) sdkDep {
493 var api, v string
494 if strings.Contains(sdk, "_") {
495 t := strings.Split(sdk, "_")
496 api = t[0]
497 v = t[1]
498 } else {
499 api = "public"
500 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900501 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100502 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700503 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100504 // There's no aidl for other SDKs yet.
505 // TODO(77525052): Add aidl files for other SDKs too.
506 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
507 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800508 jarPath := android.ExistentPathForSource(ctx, jar)
509 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700510 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700511
Colin Cross6510f912017-11-29 00:27:14 -0800512 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700513 return sdkDep{
514 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700515 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700516 }
517 }
518
Colin Crossfc3674a2017-09-18 17:41:52 -0700519 if !jarPath.Valid() {
520 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
521 return sdkDep{}
522 }
Colin Cross47ff2522017-10-02 14:22:08 -0700523
Colin Crossfc3674a2017-09-18 17:41:52 -0700524 if !aidlPath.Valid() {
525 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
526 return sdkDep{}
527 }
Colin Cross47ff2522017-10-02 14:22:08 -0700528
Colin Crossfc3674a2017-09-18 17:41:52 -0700529 return sdkDep{
530 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700531 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700532 aidl: aidlPath.Path(),
533 }
534 }
535
Colin Crossa97c5d32018-03-28 14:58:31 -0700536 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700537 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700538 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700539 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700540 systemModules: m + "_system_modules",
541 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700542 }
543 if m == "core.current.stubs" {
544 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100545 } else if m == "core.platform.api.stubs" {
546 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700547 }
548 return ret
549 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700550
Colin Cross6510f912017-11-29 00:27:14 -0800551 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100552 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700553 }
554
555 switch v {
556 case "":
557 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700558 useDefaultLibs: true,
559 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700560 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700561 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700562 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700563 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700564 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700565 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700566 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700567 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700568 return toModule("core.current.stubs", "")
Neil Fuller3c979c32018-09-11 09:29:31 +0100569 case "core_platform_current":
570 return toModule("core.platform.api.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700571 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100572 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700573 }
574}
575
Colin Crossbe1da472017-07-07 15:59:46 -0700576func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700577 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700578 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700579 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700580 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700581 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100582 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700583 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700584 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700585 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700586 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100587 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700588 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800589 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700590 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
591 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800592 }
Colin Crossbe1da472017-07-07 15:59:46 -0700593 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700594 } else if j.deviceProperties.System_modules == nil {
595 ctx.PropertyErrorf("no_standard_libs",
596 "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 +0100597 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700598 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700599 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100600 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700601 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800602 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800603 if ctx.ModuleName() == "android_stubs_current" ||
604 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700605 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700606 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800607 }
Colin Cross2fe66872015-03-30 17:20:39 -0700608 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700609
Colin Cross42d48b72018-08-29 14:10:52 -0700610 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
611 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100612 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700613 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100614 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700615
Colin Cross7f9036c2017-08-30 13:27:57 -0700616 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000617 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700618 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800619 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700620
621 if j.hasSrcExt(".proto") {
622 protoDeps(ctx, &j.protoProperties)
623 }
Colin Cross93e85952017-08-15 13:34:18 -0700624
625 if j.hasSrcExt(".kt") {
626 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
627 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700628 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700629 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800630
631 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700632 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800633 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700634}
635
636func hasSrcExt(srcs []string, ext string) bool {
637 for _, src := range srcs {
638 if filepath.Ext(src) == ext {
639 return true
640 }
641 }
642
643 return false
644}
645
Nan Zhang61eaedb2017-11-02 13:28:15 -0700646func shardPaths(paths android.Paths, shardSize int) []android.Paths {
647 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
648 for len(paths) > shardSize {
649 ret = append(ret, paths[0:shardSize])
650 paths = paths[shardSize:]
651 }
652 if len(paths) > 0 {
653 ret = append(ret, paths)
654 }
655 return ret
656}
657
Colin Cross6af17aa2017-09-20 12:59:05 -0700658func (j *Module) hasSrcExt(ext string) bool {
659 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700660}
661
Colin Cross46c9b8b2017-06-22 16:51:17 -0700662func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700663 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700664
Colin Crossebe1a512017-11-14 13:12:14 -0800665 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
666 aidlIncludes = append(aidlIncludes,
667 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
668 aidlIncludes = append(aidlIncludes,
669 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700670
Steven Moreland667f6882018-07-26 12:55:08 -0700671 flags := []string{"-b"}
672
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700673 if aidlPreprocess.Valid() {
674 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700675 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700676 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700677 }
678
Colin Cross635c3b02016-05-18 15:37:25 -0700679 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800680 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700681 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800682 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700683 flags = append(flags, "-I"+src.String())
684 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700685
Martijn Coeneneab15642018-03-09 09:29:59 +0100686 if Bool(j.deviceProperties.Aidl.Generate_traces) {
687 flags = append(flags, "-t")
688 }
689
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100690 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
691 flags = append(flags, "--transaction_names")
692 }
693
Colin Crossf03c82b2015-04-13 13:53:40 -0700694 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700695}
696
Colin Cross32f676a2017-09-06 13:41:06 -0700697type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800698 classpath classpath
699 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700700 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700701 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700702 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700703 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700704 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800705 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700706 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700707 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700708 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700709 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700710}
Colin Cross2fe66872015-03-30 17:20:39 -0700711
Colin Cross54250902017-12-05 09:28:08 -0800712func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
713 for _, f := range dep.Srcs() {
714 if f.Ext() != ".jar" {
715 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
716 ctx.OtherModuleName(dep.(blueprint.Module)))
717 }
718 }
719}
720
Jiyong Park2d492942018-03-05 17:44:10 +0900721type linkType int
722
723const (
724 javaCore linkType = iota
725 javaSdk
726 javaSystem
727 javaPlatform
728)
729
Jiyong Park46f78fb2018-10-20 16:33:17 +0900730func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700731 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700732 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900733 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
734 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
735 name == "core-lambda-stubs":
736 return javaCore, true
737 case ver == "core_current" || ver == "core_platform_current":
738 return javaCore, false
739 case name == "android_system_stubs_current":
740 return javaSystem, true
741 case strings.HasPrefix(ver, "system_"):
742 return javaSystem, false
743 case name == "android_test_stubs_current":
744 return javaSystem, true
745 case strings.HasPrefix(ver, "test_"):
746 return javaPlatform, false
747 case name == "android_stubs_current":
748 return javaSdk, true
749 case ver == "current":
750 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700751 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900752 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700753 default:
754 if _, err := strconv.Atoi(ver); err != nil {
755 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
756 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900757 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900758 }
759}
760
Jiyong Park750e5572018-01-31 00:20:13 +0900761func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700762 if ctx.Host() {
763 return
764 }
765
Jiyong Park46f78fb2018-10-20 16:33:17 +0900766 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
767 if stubs {
768 return
769 }
770 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900771 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."
772
773 switch myLinkType {
774 case javaCore:
775 if otherLinkType != javaCore {
776 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 +0900777 ctx.OtherModuleName(to))
778 }
Jiyong Park2d492942018-03-05 17:44:10 +0900779 break
780 case javaSdk:
781 if otherLinkType != javaCore && otherLinkType != javaSdk {
782 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
783 ctx.OtherModuleName(to))
784 }
785 break
786 case javaSystem:
787 if otherLinkType == javaPlatform {
788 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
789 ctx.OtherModuleName(to))
790 }
791 break
792 case javaPlatform:
793 // no restriction on link-type
794 break
Jiyong Park750e5572018-01-31 00:20:13 +0900795 }
796}
797
Colin Cross32f676a2017-09-06 13:41:06 -0700798func (j *Module) collectDeps(ctx android.ModuleContext) deps {
799 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700800
Colin Cross300f0382018-03-06 13:11:51 -0800801 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700802 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800803 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700804 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800805 } else if sdkDep.useFiles {
806 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700807 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800808 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
809 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700810 }
811
Colin Crossd11fcda2017-10-23 17:59:01 -0700812 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700813 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700814 tag := ctx.OtherModuleDependencyTag(module)
815
Colin Crossa4f08812018-10-02 22:03:40 -0700816 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700817 // Handled by AndroidApp.collectAppDeps
818 return
819 }
820 if tag == certificateTag {
821 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700822 return
823 }
824
Jiyong Park750e5572018-01-31 00:20:13 +0900825 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700826 switch tag {
827 case bootClasspathTag, libTag, staticLibTag:
828 checkLinkType(ctx, j, to, tag.(dependencyTag))
829 }
Jiyong Park750e5572018-01-31 00:20:13 +0900830 }
Colin Cross54250902017-12-05 09:28:08 -0800831 switch dep := module.(type) {
832 case Dependency:
833 switch tag {
834 case bootClasspathTag:
835 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700836 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800837 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900838 // sdk lib names from dependencies are re-exported
839 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800840 case staticLibTag:
841 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
842 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
843 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700844 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900845 // sdk lib names from dependencies are re-exported
846 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700847 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700848 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800849 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100850 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800851 // framework.jar has a one-off dependency on the R.java and Manifest.java files
852 // generated by framework-res.apk
853 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
854 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800855 case frameworkApkTag:
856 if ctx.ModuleName() == "android_stubs_current" ||
857 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700858 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800859 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
860 // resource files out of there for aapt.
861 //
862 // Normally the package rule runs aapt, which includes the resource,
863 // but we're not running that in our package rule so just copy in the
864 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700865 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800866 }
Colin Cross54250902017-12-05 09:28:08 -0800867 case kotlinStdlibTag:
868 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800869 }
870
871 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900872 case SdkLibraryDependency:
873 switch tag {
874 case libTag:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900875 linkType, _ := getLinkType(j, ctx.ModuleName())
876 deps.classpath = append(deps.classpath, dep.HeaderJars(linkType)...)
Jiyong Park1be96912018-05-28 18:02:19 +0900877 // names of sdk libs that are directly depended are exported
878 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900879 default:
880 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
881 }
Colin Cross54250902017-12-05 09:28:08 -0800882 case android.SourceFileProducer:
883 switch tag {
884 case libTag:
885 checkProducesJars(ctx, dep)
886 deps.classpath = append(deps.classpath, dep.Srcs()...)
887 case staticLibTag:
888 checkProducesJars(ctx, dep)
889 deps.classpath = append(deps.classpath, dep.Srcs()...)
890 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
891 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
892 case android.DefaultsDepTag, android.SourceDepTag:
893 // Nothing to do
894 default:
895 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
896 }
897 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700898 switch tag {
899 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700900 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700901 case systemModulesTag:
902 if deps.systemModules != nil {
903 panic("Found two system module dependencies")
904 }
905 sm := module.(*SystemModules)
906 if sm.outputFile == nil {
907 panic("Missing directory for system module dependency")
908 }
909 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700910 default:
911 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700912 }
Colin Crossec7a0422017-07-07 14:47:12 -0700913 }
Colin Cross2fe66872015-03-30 17:20:39 -0700914 })
915
Jiyong Park1be96912018-05-28 18:02:19 +0900916 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
917
Colin Cross32f676a2017-09-06 13:41:06 -0700918 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700919}
920
Colin Cross83bb3162018-06-25 15:48:06 -0700921func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700922 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700923 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
924 if err != nil {
925 ctx.PropertyErrorf("sdk_version", "%s", err)
926 }
Nan Zhang357466b2018-04-17 17:38:36 -0700927 if javaVersion != "" {
928 ret = javaVersion
929 } else if ctx.Device() && sdk <= 23 {
930 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900931 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700932 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700933 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700934 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
935 ret = "1.8"
936 } else {
937 ret = "1.9"
938 }
939
940 return ret
941}
942
Nan Zhanged19fc32017-10-19 13:06:22 -0700943func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700944
Colin Crossf03c82b2015-04-13 13:53:40 -0700945 var flags javaBuilderFlags
946
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100947 // javaVersion flag.
948 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
949
Nan Zhanged19fc32017-10-19 13:06:22 -0700950 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700951 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100952 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700953 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700954 }
Colin Cross6510f912017-11-29 00:27:14 -0800955 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700956 // Override the -g flag passed globally to remove local variable debug info to reduce
957 // disk and memory usage.
958 javacFlags = append(javacFlags, "-g:source,lines")
959 }
Colin Cross64162712017-08-08 13:17:59 -0700960
Colin Cross66548102018-06-19 22:47:35 -0700961 if ctx.Config().RunErrorProne() {
962 if config.ErrorProneClasspath == nil {
963 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
964 }
965
966 errorProneFlags := []string{
967 "-Xplugin:ErrorProne",
968 "${config.ErrorProneChecks}",
969 }
970 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
971
972 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
973 "'" + strings.Join(errorProneFlags, " ") + "'"
974 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800975 }
976
Nan Zhanged19fc32017-10-19 13:06:22 -0700977 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800978 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
979 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700980 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800981
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100982 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800983 !Bool(j.properties.No_standard_libs) &&
984 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
985 // Give host-side tools a version of OpenJDK's standard libraries
986 // close to what they're targeting. As of Dec 2017, AOSP is only
987 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
988 //
989 // When building with OpenJDK 8, the following should have no
990 // effect since those jars would be available by default.
991 //
992 // When building with OpenJDK 9 but targeting a version < 1.8,
993 // putting them on the bootclasspath means that:
994 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
995 // b) references to existing APIs are not reinterpreted in an
996 // OpenJDK 9-specific way, eg. calls to subclasses of
997 // java.nio.Buffer as in http://b/70862583
998 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
999 flags.bootClasspath = append(flags.bootClasspath,
1000 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1001 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001002 if Bool(j.properties.Use_tools_jar) {
1003 flags.bootClasspath = append(flags.bootClasspath,
1004 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1005 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001006 }
1007
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001008 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -07001009 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
1010 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
1011 }
1012
Nan Zhanged19fc32017-10-19 13:06:22 -07001013 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001014 if deps.systemModules != nil {
1015 flags.systemModules = append(flags.systemModules, deps.systemModules)
1016 }
1017
Nan Zhanged19fc32017-10-19 13:06:22 -07001018 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -07001019 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -07001020 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001021 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -07001022 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
1023 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -07001024 }
1025
Colin Cross81440082018-08-15 20:21:55 -07001026 if len(javacFlags) > 0 {
1027 // optimization.
1028 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1029 flags.javacFlags = "$javacFlags"
1030 }
1031
Nan Zhanged19fc32017-10-19 13:06:22 -07001032 return flags
1033}
Colin Crossc0b06f12015-04-08 13:03:43 -07001034
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001035func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001036
Colin Crossebe1a512017-11-14 13:12:14 -08001037 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001038
1039 deps := j.collectDeps(ctx)
1040 flags := j.collectBuilderFlags(ctx, deps)
1041
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001042 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001043 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1044 }
1045 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001046 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001047 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001048 }
1049
Colin Crossaf050172017-11-15 23:01:59 -08001050 srcFiles = j.genSources(ctx, srcFiles, flags)
1051
1052 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001053 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001054 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001055
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001056 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1057 // that IDEInfo struct will use
1058 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1059
Colin Cross1ee23172017-10-18 14:44:18 -07001060 jarName := ctx.ModuleName() + ".jar"
1061
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001062 javaSrcFiles := srcFiles.FilterByExt(".java")
1063 var uniqueSrcFiles android.Paths
1064 set := make(map[string]bool)
1065 for _, v := range javaSrcFiles {
1066 if _, found := set[v.String()]; !found {
1067 set[v.String()] = true
1068 uniqueSrcFiles = append(uniqueSrcFiles, v)
1069 }
1070 }
1071
Colin Cross55f63ea2018-08-27 12:37:09 -07001072 var kotlinJars android.Paths
1073
Colin Cross93e85952017-08-15 13:34:18 -07001074 if srcFiles.HasExt(".kt") {
1075 // If there are kotlin files, compile them first but pass all the kotlin and java files
1076 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1077 // won't emit any classes for them.
1078
1079 flags.kotlincFlags = "-no-stdlib"
1080 if ctx.Device() {
1081 flags.kotlincFlags += " -no-jdk"
1082 }
1083
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001084 var kotlinSrcFiles android.Paths
1085 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1086 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1087
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001088 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001089 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1090 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1091
Colin Cross1ee23172017-10-18 14:44:18 -07001092 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001093 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001094 if ctx.Failed() {
1095 return
1096 }
1097
1098 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001099 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001100 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001101
Colin Cross93e85952017-08-15 13:34:18 -07001102 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001103 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001104 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001105 }
1106
Colin Cross55f63ea2018-08-27 12:37:09 -07001107 jars := append(android.Paths(nil), kotlinJars...)
1108
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001109 // Store the list of .java files that was passed to javac
1110 j.compiledJavaSrcs = uniqueSrcFiles
1111 j.compiledSrcJars = srcJars
1112
Nan Zhang61eaedb2017-11-02 13:28:15 -07001113 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001114 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001115 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1116 enable_sharding = true
1117 if len(j.properties.Annotation_processors) != 0 ||
1118 len(j.properties.Annotation_processor_classes) != 0 {
1119 ctx.PropertyErrorf("javac_shard_size",
1120 "%q cannot be set when annotation processors are enabled.",
1121 j.properties.Javac_shard_size)
1122 }
1123 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001124 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001125 if ctx.Failed() {
1126 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001127 }
1128 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001129 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001130 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001131 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001132 // If error-prone is enabled, add an additional rule to compile the java files into
1133 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001134 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001135 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1136 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001137 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001138 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001139 extraJarDeps = append(extraJarDeps, errorprone)
1140 }
1141
Nan Zhang61eaedb2017-11-02 13:28:15 -07001142 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001143 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001144 shardSize := int(*(j.properties.Javac_shard_size))
1145 var shardSrcs []android.Paths
1146 if len(uniqueSrcFiles) > 0 {
1147 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1148 for idx, shardSrc := range shardSrcs {
1149 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1150 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1151 jars = append(jars, classes)
1152 }
1153 }
1154 if len(srcJars) > 0 {
1155 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1156 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1157 jars = append(jars, classes)
1158 }
1159 } else {
1160 classes := android.PathForModuleOut(ctx, "javac", jarName)
1161 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1162 jars = append(jars, classes)
1163 }
Colin Crossd6891432017-09-27 17:39:56 -07001164 if ctx.Failed() {
1165 return
1166 }
Colin Cross2fe66872015-03-30 17:20:39 -07001167 }
1168
Colin Crosscedd4762018-09-13 11:26:19 -07001169 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1170 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001171 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1172
1173 var resArgs []string
1174 var resDeps android.Paths
1175
1176 resArgs = append(resArgs, dirArgs...)
1177 resDeps = append(resDeps, dirDeps...)
1178
1179 resArgs = append(resArgs, fileArgs...)
1180 resDeps = append(resDeps, fileDeps...)
1181
Colin Crossff3ae9d2018-04-10 16:15:18 -07001182 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001183 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001184 resArgs = append(resArgs, srcArgs...)
1185 resDeps = append(resDeps, srcDeps...)
1186 }
Colin Cross40a36712017-09-27 17:41:35 -07001187
1188 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001189 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001190 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001191 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001192 if ctx.Failed() {
1193 return
1194 }
1195 }
1196
Colin Cross331a1212018-08-15 20:40:52 -07001197 if len(deps.staticResourceJars) > 0 {
1198 var jars android.Paths
1199 if j.resourceJar != nil {
1200 jars = append(jars, j.resourceJar)
1201 }
1202 jars = append(jars, deps.staticResourceJars...)
1203
1204 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1205 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1206 false, nil, nil)
1207 j.resourceJar = combinedJar
1208 }
1209
Colin Cross32f676a2017-09-06 13:41:06 -07001210 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001211 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001212
Colin Cross094054a2018-10-17 15:10:48 -07001213 manifest := j.overrideManifest
1214 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001215 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1216 }
Colin Cross635acc92017-09-12 22:50:46 -07001217
Colin Cross0a6e0072017-08-30 14:24:55 -07001218 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001219 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001220 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001221
1222 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001223 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1224 // Optimization: skip the combine step if there is nothing to do
1225 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1226 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1227 // any if len(jars) == 1.
1228 outputFile = moduleOutPath
1229 } else {
1230 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1231 ctx.Build(pctx, android.BuildParams{
1232 Rule: android.Cp,
1233 Input: jars[0],
1234 Output: combinedJar,
1235 })
1236 outputFile = combinedJar
1237 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001238 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001239 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001240 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001241 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001242 outputFile = combinedJar
1243 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001244
Colin Cross331a1212018-08-15 20:40:52 -07001245 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001246 if j.properties.Jarjar_rules != nil {
1247 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001248 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001249 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001250 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1251 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001252
1253 // jarjar resource jar if necessary
1254 if j.resourceJar != nil {
1255 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1256 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1257 j.resourceJar = resourceJarJarFile
1258 }
1259
Colin Cross0a6e0072017-08-30 14:24:55 -07001260 if ctx.Failed() {
1261 return
1262 }
1263 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001264 j.implementationJarFile = outputFile
1265 if j.headerJarFile == nil {
1266 j.headerJarFile = j.implementationJarFile
1267 }
Colin Cross2fe66872015-03-30 17:20:39 -07001268
Colin Cross6510f912017-11-29 00:27:14 -08001269 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001270 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1271 j.properties.Instrument = true
1272 }
1273 }
1274
Colin Cross3144dfc2018-01-03 15:06:47 -08001275 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001276 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1277 }
1278
Colin Cross331a1212018-08-15 20:40:52 -07001279 // merge implementation jar with resources if necessary
1280 implementationAndResourcesJar := outputFile
1281 if j.resourceJar != nil {
1282 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1283 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1284 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1285 false, nil, nil)
1286 implementationAndResourcesJar = combinedJar
1287 }
1288
1289 j.implementationAndResourcesJar = implementationAndResourcesJar
1290
Colin Cross9ae1b922018-06-26 17:59:05 -07001291 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001292 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001293 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001294 if ctx.Failed() {
1295 return
1296 }
Colin Cross331a1212018-08-15 20:40:52 -07001297
1298 // merge dex jar with resources if necessary
1299 if j.resourceJar != nil {
1300 jars := android.Paths{dexOutputFile, j.resourceJar}
1301 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1302 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1303 false, nil, nil)
1304 dexOutputFile = combinedJar
1305 }
1306
1307 j.dexJarFile = dexOutputFile
1308
Colin Cross3063b782018-08-15 11:19:12 -07001309 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001310 } else {
1311 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001312 }
Colin Cross331a1212018-08-15 20:40:52 -07001313
Colin Crossb7a63242015-04-16 14:09:14 -07001314 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001315
1316 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1317 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001318}
1319
Colin Cross8eadbf02017-10-24 17:46:00 -07001320func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001321 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001322
1323 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001324 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001325 // Compile java sources into turbine.jar.
1326 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1327 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1328 if ctx.Failed() {
1329 return nil
1330 }
1331 jars = append(jars, turbineJar)
1332 }
1333
Colin Cross55f63ea2018-08-27 12:37:09 -07001334 jars = append(jars, extraJars...)
1335
Nan Zhanged19fc32017-10-19 13:06:22 -07001336 // Combine any static header libraries into classes-header.jar. If there is only
1337 // one input jar this step will be skipped.
1338 var headerJar android.Path
1339 jars = append(jars, deps.staticHeaderJars...)
1340
Colin Cross5c6ecc12017-10-23 18:12:27 -07001341 // we cannot skip the combine step for now if there is only one jar
1342 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1343 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001344 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1345 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001346 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001347
1348 if j.properties.Jarjar_rules != nil {
1349 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1350 // Transform classes.jar into classes-jarjar.jar
1351 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1352 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1353 headerJar = jarjarFile
1354 if ctx.Failed() {
1355 return nil
1356 }
1357 }
1358
1359 return headerJar
1360}
1361
Colin Crosscb933592017-11-22 13:49:43 -08001362func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001363 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001364
Colin Cross7a3139e2017-12-19 13:57:50 -08001365 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001366
Colin Cross84c38822018-01-03 15:59:46 -08001367 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001368 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1369
1370 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1371
1372 j.jacocoReportClassesFile = jacocoReportClassesFile
1373
1374 return instrumentedJar
1375}
1376
Colin Crossf506d872017-07-19 15:53:04 -07001377var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001378
Nan Zhanged19fc32017-10-19 13:06:22 -07001379func (j *Module) HeaderJars() android.Paths {
1380 return android.Paths{j.headerJarFile}
1381}
1382
1383func (j *Module) ImplementationJars() android.Paths {
1384 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001385}
1386
Colin Cross331a1212018-08-15 20:40:52 -07001387func (j *Module) ResourceJars() android.Paths {
1388 if j.resourceJar == nil {
1389 return nil
1390 }
1391 return android.Paths{j.resourceJar}
1392}
1393
1394func (j *Module) ImplementationAndResourcesJars() android.Paths {
1395 return android.Paths{j.implementationAndResourcesJar}
1396}
1397
Colin Cross46c9b8b2017-06-22 16:51:17 -07001398func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001399 return j.exportAidlIncludeDirs
1400}
1401
Jiyong Park1be96912018-05-28 18:02:19 +09001402func (j *Module) ExportedSdkLibs() []string {
1403 return j.exportedSdkLibs
1404}
1405
Colin Cross46c9b8b2017-06-22 16:51:17 -07001406var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001407
Colin Cross46c9b8b2017-06-22 16:51:17 -07001408func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001409 return j.logtagsSrcs
1410}
1411
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001412// Collect information for opening IDE project files in java/jdeps.go.
1413func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1414 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1415 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1416 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1417 if j.properties.Jarjar_rules != nil {
1418 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1419 }
1420}
1421
1422func (j *Module) CompilerDeps() []string {
1423 jdeps := []string{}
1424 jdeps = append(jdeps, j.properties.Libs...)
1425 jdeps = append(jdeps, j.properties.Static_libs...)
1426 return jdeps
1427}
1428
Colin Cross2fe66872015-03-30 17:20:39 -07001429//
1430// Java libraries (.jar file)
1431//
1432
Colin Crossf506d872017-07-19 15:53:04 -07001433type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001434 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001435}
1436
Colin Crossf506d872017-07-19 15:53:04 -07001437func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001438 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001439
Colin Cross9ae1b922018-06-26 17:59:05 -07001440 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001441 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1442 ctx.ModuleName()+".jar", j.outputFile)
1443 }
Colin Crossb7a63242015-04-16 14:09:14 -07001444}
1445
Colin Crossf506d872017-07-19 15:53:04 -07001446func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001447 j.deps(ctx)
1448}
1449
Colin Cross9ae1b922018-06-26 17:59:05 -07001450func LibraryFactory() android.Module {
1451 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001452
Colin Cross9ae1b922018-06-26 17:59:05 -07001453 module.AddProperties(
1454 &module.Module.properties,
1455 &module.Module.deviceProperties,
1456 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001457
Colin Cross9ae1b922018-06-26 17:59:05 -07001458 InitJavaModule(module, android.HostAndDeviceSupported)
1459 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001460}
1461
Colin Crossf506d872017-07-19 15:53:04 -07001462func LibraryHostFactory() android.Module {
1463 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001464
Colin Cross6af17aa2017-09-20 12:59:05 -07001465 module.AddProperties(
1466 &module.Module.properties,
1467 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001468
Colin Cross9ae1b922018-06-26 17:59:05 -07001469 module.Module.properties.Installable = proptools.BoolPtr(true)
1470
Colin Cross89536d42017-07-07 14:35:50 -07001471 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001472 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001473}
1474
1475//
Colin Crossb628ea52018-08-14 16:42:33 -07001476// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001477//
1478
1479type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001480 // list of compatibility suites (for example "cts", "vts") that the module should be
1481 // installed into.
1482 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001483
1484 // the name of the test configuration (for example "AndroidTest.xml") that should be
1485 // installed with the module.
1486 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001487
Jack He33338892018-09-19 02:21:28 -07001488 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1489 // should be installed with the module.
1490 Test_config_template *string `android:"arch_variant"`
1491
Colin Crossd96ca352018-08-10 16:06:24 -07001492 // list of files or filegroup modules that provide data that should be installed alongside
1493 // the test
1494 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001495}
1496
1497type Test struct {
1498 Library
1499
1500 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001501
1502 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001503 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001504}
1505
1506func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001507 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001508 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001509
1510 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001511}
1512
1513func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1514 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001515 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001516 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001517 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001518}
1519
1520func TestFactory() android.Module {
1521 module := &Test{}
1522
1523 module.AddProperties(
1524 &module.Module.properties,
1525 &module.Module.deviceProperties,
1526 &module.Module.protoProperties,
1527 &module.testProperties)
1528
Colin Cross9ae1b922018-06-26 17:59:05 -07001529 module.Module.properties.Installable = proptools.BoolPtr(true)
1530
Colin Cross05638fc2018-04-09 18:40:24 -07001531 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001532 return module
1533}
1534
1535func TestHostFactory() android.Module {
1536 module := &Test{}
1537
1538 module.AddProperties(
1539 &module.Module.properties,
1540 &module.Module.protoProperties,
1541 &module.testProperties)
1542
Colin Cross9ae1b922018-06-26 17:59:05 -07001543 module.Module.properties.Installable = proptools.BoolPtr(true)
1544
Colin Cross05638fc2018-04-09 18:40:24 -07001545 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001546 return module
1547}
1548
1549//
Colin Cross2fe66872015-03-30 17:20:39 -07001550// Java Binaries (.jar file plus wrapper script)
1551//
1552
Colin Crossf506d872017-07-19 15:53:04 -07001553type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001554 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001555 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001556
1557 // Name of the class containing main to be inserted into the manifest as Main-Class.
1558 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001559}
1560
Colin Crossf506d872017-07-19 15:53:04 -07001561type Binary struct {
1562 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001563
Colin Crossf506d872017-07-19 15:53:04 -07001564 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001565
Colin Cross6b4a32d2017-12-05 13:42:45 -08001566 isWrapperVariant bool
1567
Colin Crossc3315992017-12-08 19:12:36 -08001568 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001569 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001570}
1571
Alex Light24237172017-10-26 09:46:21 -07001572func (j *Binary) HostToolPath() android.OptionalPath {
1573 return android.OptionalPathForPath(j.binaryFile)
1574}
1575
Colin Crossf506d872017-07-19 15:53:04 -07001576func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001577 if ctx.Arch().ArchType == android.Common {
1578 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001579 if j.binaryProperties.Main_class != nil {
1580 if j.properties.Manifest != nil {
1581 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1582 }
1583 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1584 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1585 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1586 }
1587
Colin Cross6b4a32d2017-12-05 13:42:45 -08001588 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001589 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001590 // Handle the binary wrapper
1591 j.isWrapperVariant = true
1592
Colin Cross366938f2017-12-11 16:29:02 -08001593 if j.binaryProperties.Wrapper != nil {
1594 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001595 } else {
1596 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1597 }
1598
1599 // Depend on the installed jar so that the wrapper doesn't get executed by
1600 // another build rule before the jar has been installed.
1601 jarFile := ctx.PrimaryModule().(*Binary).installFile
1602
1603 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1604 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001605 }
Colin Cross2fe66872015-03-30 17:20:39 -07001606}
1607
Colin Crossf506d872017-07-19 15:53:04 -07001608func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001609 if ctx.Arch().ArchType == android.Common {
1610 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001611 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001612 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001613 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001614}
1615
Colin Crossf506d872017-07-19 15:53:04 -07001616func BinaryFactory() android.Module {
1617 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001618
Colin Cross36242852017-06-23 15:06:31 -07001619 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001620 &module.Module.properties,
1621 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001622 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001623 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001624
Colin Cross9ae1b922018-06-26 17:59:05 -07001625 module.Module.properties.Installable = proptools.BoolPtr(true)
1626
Colin Cross6b4a32d2017-12-05 13:42:45 -08001627 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1628 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001629 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001630}
1631
Colin Crossf506d872017-07-19 15:53:04 -07001632func BinaryHostFactory() android.Module {
1633 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001634
Colin Cross36242852017-06-23 15:06:31 -07001635 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001636 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001637 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001638 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001639
Colin Cross9ae1b922018-06-26 17:59:05 -07001640 module.Module.properties.Installable = proptools.BoolPtr(true)
1641
Colin Cross6b4a32d2017-12-05 13:42:45 -08001642 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1643 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001644 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001645}
1646
1647//
1648// Java prebuilts
1649//
1650
Colin Cross74d73e22017-08-02 11:05:49 -07001651type ImportProperties struct {
1652 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001653
Nan Zhangea568a42017-11-08 21:20:04 -08001654 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001655
1656 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001657
1658 // List of shared java libs that this module has dependencies to
1659 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001660
1661 // List of files to remove from the jar file(s)
1662 Exclude_files []string
1663
1664 // List of directories to remove from the jar file(s)
1665 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001666
1667 // if set to true, run Jetifier against .jar file. Defaults to false.
1668 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001669}
1670
1671type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001672 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001673 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001674 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001675
Colin Cross74d73e22017-08-02 11:05:49 -07001676 properties ImportProperties
1677
Colin Cross0a6e0072017-08-30 14:24:55 -07001678 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001679 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001680}
1681
Colin Cross83bb3162018-06-25 15:48:06 -07001682func (j *Import) sdkVersion() string {
1683 return String(j.properties.Sdk_version)
1684}
1685
1686func (j *Import) minSdkVersion() string {
1687 return j.sdkVersion()
1688}
1689
Colin Cross74d73e22017-08-02 11:05:49 -07001690func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001691 return &j.prebuilt
1692}
1693
Colin Cross74d73e22017-08-02 11:05:49 -07001694func (j *Import) PrebuiltSrcs() []string {
1695 return j.properties.Jars
1696}
1697
1698func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001699 return j.prebuilt.Name(j.ModuleBase.Name())
1700}
1701
Colin Cross74d73e22017-08-02 11:05:49 -07001702func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001703 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001704 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001705}
1706
Colin Cross74d73e22017-08-02 11:05:49 -07001707func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001708 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001709
Nan Zhang4c819fb2018-08-27 18:31:46 -07001710 jarName := ctx.ModuleName() + ".jar"
1711 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001712 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1713 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001714 if Bool(j.properties.Jetifier_enabled) {
1715 inputFile := outputFile
1716 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1717 TransformJetifier(ctx, outputFile, inputFile)
1718 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001719 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001720
1721 ctx.VisitDirectDeps(func(module android.Module) {
1722 otherName := ctx.OtherModuleName(module)
1723 tag := ctx.OtherModuleDependencyTag(module)
1724
1725 switch dep := module.(type) {
1726 case Dependency:
1727 switch tag {
1728 case libTag, staticLibTag:
1729 // sdk lib names from dependencies are re-exported
1730 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1731 }
1732 case SdkLibraryDependency:
1733 switch tag {
1734 case libTag:
1735 // names of sdk libs that are directly depended are exported
1736 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1737 }
1738 }
1739 })
1740
1741 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001742 if Bool(j.properties.Installable) {
1743 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1744 ctx.ModuleName()+".jar", outputFile)
1745 }
Colin Cross2fe66872015-03-30 17:20:39 -07001746}
1747
Colin Cross74d73e22017-08-02 11:05:49 -07001748var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001749
Nan Zhanged19fc32017-10-19 13:06:22 -07001750func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001751 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001752}
1753
1754func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001755 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001756}
1757
Colin Cross331a1212018-08-15 20:40:52 -07001758func (j *Import) ResourceJars() android.Paths {
1759 return nil
1760}
1761
1762func (j *Import) ImplementationAndResourcesJars() android.Paths {
1763 return android.Paths{j.combinedClasspathFile}
1764}
1765
Colin Cross74d73e22017-08-02 11:05:49 -07001766func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001767 return nil
1768}
1769
Jiyong Park1be96912018-05-28 18:02:19 +09001770func (j *Import) ExportedSdkLibs() []string {
1771 return j.exportedSdkLibs
1772}
1773
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001774// Collect information for opening IDE project files in java/jdeps.go.
1775const (
1776 removedPrefix = "prebuilt_"
1777)
1778
1779func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1780 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1781}
1782
1783func (j *Import) IDECustomizedModuleName() string {
1784 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1785 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1786 // solution to get the Import name.
1787 name := j.Name()
1788 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001789 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001790 }
1791 return name
1792}
1793
Colin Cross74d73e22017-08-02 11:05:49 -07001794var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001795
Colin Cross74d73e22017-08-02 11:05:49 -07001796func ImportFactory() android.Module {
1797 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001798
Colin Cross74d73e22017-08-02 11:05:49 -07001799 module.AddProperties(&module.properties)
1800
1801 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001802 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001803 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001804}
1805
Colin Cross74d73e22017-08-02 11:05:49 -07001806func ImportFactoryHost() android.Module {
1807 module := &Import{}
1808
1809 module.AddProperties(&module.properties)
1810
1811 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001812 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001813 return module
1814}
1815
Colin Cross89536d42017-07-07 14:35:50 -07001816//
1817// Defaults
1818//
1819type Defaults struct {
1820 android.ModuleBase
1821 android.DefaultsModuleBase
1822}
1823
1824func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1825}
1826
1827func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1828}
1829
1830func defaultsFactory() android.Module {
1831 return DefaultsFactory()
1832}
1833
1834func DefaultsFactory(props ...interface{}) android.Module {
1835 module := &Defaults{}
1836
1837 module.AddProperties(props...)
1838 module.AddProperties(
1839 &CompilerProperties{},
1840 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001841 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001842 &aaptProperties{},
1843 &androidLibraryProperties{},
1844 &appProperties{},
1845 &appTestProperties{},
1846 &ImportProperties{},
1847 &AARImportProperties{},
1848 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001849 )
1850
1851 android.InitDefaultsModule(module)
1852
1853 return module
1854}
Nan Zhangea568a42017-11-08 21:20:04 -08001855
1856var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001857var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001858var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001859var inList = android.InList