blob: 22829e4072b87a477eb47b9b34b5ab7da0eabd05 [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
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020092 // list of module-specific flags that will be used for kotlinc compiles
93 Kotlincflags []string `android:"arch_variant"`
94
Colin Cross7d5136f2015-05-11 13:39:40 -070095 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700102 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
Colin Cross540eff82017-06-22 17:01:52 -0700104 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700105 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700106
107 // If not blank, set the java version passed to javac as -source and -target
108 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700109
Colin Cross9ae1b922018-06-26 17:59:05 -0700110 // If set to true, allow this module to be dexed and installed on devices. Has no
111 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700112 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700113
Colin Cross0f37af02017-09-27 17:42:05 -0700114 // If set to true, include sources used to compile the module in to the final jar
115 Include_srcs *bool
116
Colin Crossbe9cdb82019-01-21 21:37:16 -0800117 // List of modules to use as annotation processors
118 Plugins []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
Dan Willemsen419290a2018-10-31 15:28:47 -0700186 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
187 // Defaults to sdk_version if not set.
188 Target_sdk_version *string
189
Colin Cross6af2e492018-05-22 11:12:33 -0700190 // if true, compile against the platform APIs instead of an SDK.
191 Platform_apis *bool
192
Colin Crossebe1a512017-11-14 13:12:14 -0800193 Aidl struct {
194 // Top level directories to pass to aidl tool
195 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700196
Colin Crossebe1a512017-11-14 13:12:14 -0800197 // Directories rooted at the Android.bp file to pass to aidl tool
198 Local_include_dirs []string
199
200 // directories that should be added as include directories for any aidl sources of modules
201 // that depend on this module, as well as to aidl for this module.
202 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100203
204 // whether to generate traces (for systrace) for this interface
205 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100206
207 // whether to generate Binder#GetTransaction name method.
208 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800209 }
Colin Cross92430102017-10-09 14:59:32 -0700210
211 // If true, export a copy of the module as a -hostdex module for host testing.
212 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700213
David Brazdil17ef5632018-06-27 10:27:45 +0100214 // If set to true, compile dex regardless of installable. Defaults to false.
215 Compile_dex *bool
216
Colin Cross66dbc0b2017-12-28 12:23:20 -0800217 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700218 // If false, disable all optimization. Defaults to true for android_app and android_test
219 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800220 Enabled *bool
221
222 // If true, optimize for size by removing unused code. Defaults to true for apps,
223 // false for libraries and tests.
224 Shrink *bool
225
226 // If true, optimize bytecode. Defaults to false.
227 Optimize *bool
228
229 // If true, obfuscate bytecode. Defaults to false.
230 Obfuscate *bool
231
232 // If true, do not use the flag files generated by aapt that automatically keep
233 // classes referenced by the app manifest. Defaults to false.
234 No_aapt_flags *bool
235
236 // Flags to pass to proguard.
237 Proguard_flags []string
238
239 // Specifies the locations of files containing proguard flags.
240 Proguard_flags_files []string
241 }
242
Colin Cross1369cdb2017-09-29 17:58:17 -0700243 // When targeting 1.9, override the modules to use with --system
244 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700245
246 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800247 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700248}
249
Colin Cross46c9b8b2017-06-22 16:51:17 -0700250// Module contains the properties and members used by all java module types
251type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700252 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700253 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700254
Colin Cross89536d42017-07-07 14:35:50 -0700255 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700256 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700257 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700258
Colin Cross331a1212018-08-15 20:40:52 -0700259 // jar file containing header classes including static library dependencies, suitable for
260 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700261 headerJarFile android.Path
262
Colin Cross331a1212018-08-15 20:40:52 -0700263 // jar file containing implementation classes including static library dependencies but no
264 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700265 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700266
Colin Cross331a1212018-08-15 20:40:52 -0700267 // jar file containing only resources including from static library dependencies
268 resourceJar android.Path
269
270 // jar file containing implementation classes and resources including static library
271 // dependencies
272 implementationAndResourcesJar android.Path
273
274 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700275 dexJarFile android.Path
276
Colin Cross43f08db2018-11-12 10:13:39 -0800277 // output file that contains classes.dex if it should be in the output file
278 maybeStrippedDexJarFile android.Path
279
Colin Crosscb933592017-11-22 13:49:43 -0800280 // output file containing uninstrumented classes that will be instrumented by jacoco
281 jacocoReportClassesFile android.Path
282
Colin Cross66dbc0b2017-12-28 12:23:20 -0800283 // output file containing mapping of obfuscated names
284 proguardDictionary android.Path
285
Colin Cross331a1212018-08-15 20:40:52 -0700286 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700287 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700288
Colin Cross635c3b02016-05-18 15:37:25 -0700289 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700290
Colin Cross635c3b02016-05-18 15:37:25 -0700291 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700292
Colin Cross2fe66872015-03-30 17:20:39 -0700293 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700294 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800295
296 // list of .java files and srcjars that was passed to javac
297 compiledJavaSrcs android.Paths
298 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800299
300 // list of extra progurad flag files
301 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900302
Colin Cross094054a2018-10-17 15:10:48 -0700303 // manifest file to use instead of properties.Manifest
304 overrideManifest android.OptionalPath
305
Jiyong Park1be96912018-05-28 18:02:19 +0900306 // list of SDK lib names that this java moudule is exporting
307 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700308
309 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
310 // filter out Exclude_srcs, will be used by android.IDEInfo struct
311 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800312
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800313 // expanded Jarjar_rules
314 expandJarjarRules android.Path
315
Colin Crossf24a22a2019-01-31 14:12:44 -0800316 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800317 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700318}
319
Colin Cross54250902017-12-05 09:28:08 -0800320func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700321 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800322}
323
Jiyong Park8fd61922018-11-08 02:50:25 +0900324func (j *Module) DexJarFile() android.Path {
325 return j.dexJarFile
326}
327
Colin Cross54250902017-12-05 09:28:08 -0800328var _ android.SourceFileProducer = (*Module)(nil)
329
Colin Crossf506d872017-07-19 15:53:04 -0700330type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700331 HeaderJars() android.Paths
332 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700333 ResourceJars() android.Paths
334 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800335 DexJar() android.Path
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 {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900341 HeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
342 ImplementationJars(ctx android.BaseContext, sdkVersion string) 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"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800378 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700379 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"}
Colin Crossafbb1732019-01-17 15:42:52 -0800384 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700385 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
386 certificateTag = dependencyTag{name: "certificate"}
387 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700388)
Colin Cross2fe66872015-03-30 17:20:39 -0700389
Colin Crossfc3674a2017-09-18 17:41:52 -0700390type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700391 useModule, useFiles, useDefaultLibs, invalidVersion bool
392
Colin Cross86a60ae2018-05-29 14:44:55 -0700393 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700394 systemModules string
395
Colin Crossa97c5d32018-03-28 14:58:31 -0700396 frameworkResModule string
397
Colin Cross86a60ae2018-05-29 14:44:55 -0700398 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700399 aidl android.Path
400}
401
Colin Crossa4f08812018-10-02 22:03:40 -0700402type jniLib struct {
403 name string
404 path android.Path
405 target android.Target
406}
407
Colin Cross3144dfc2018-01-03 15:06:47 -0800408func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
409 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
410}
411
412func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
413 return j.shouldInstrument(ctx) &&
414 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
415 ctx.Config().UnbundledBuild())
416}
417
Colin Cross83bb3162018-06-25 15:48:06 -0700418func (j *Module) sdkVersion() string {
419 return String(j.deviceProperties.Sdk_version)
420}
421
422func (j *Module) minSdkVersion() string {
423 if j.deviceProperties.Min_sdk_version != nil {
424 return *j.deviceProperties.Min_sdk_version
425 }
426 return j.sdkVersion()
427}
428
Dan Willemsen419290a2018-10-31 15:28:47 -0700429func (j *Module) targetSdkVersion() string {
430 if j.deviceProperties.Target_sdk_version != nil {
431 return *j.deviceProperties.Target_sdk_version
432 }
433 return j.sdkVersion()
434}
435
Colin Crossbe1da472017-07-07 15:59:46 -0700436func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700437 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700438 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700439 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700440 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700441 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100442 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700443 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700444 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700445 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700446 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100447 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700448 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800449 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700450 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
451 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800452 }
Colin Crossbe1da472017-07-07 15:59:46 -0700453 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700454 } else if j.deviceProperties.System_modules == nil {
455 ctx.PropertyErrorf("no_standard_libs",
456 "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 +0100457 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700458 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700459 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100460 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700461 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800462 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800463 if ctx.ModuleName() == "android_stubs_current" ||
464 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700465 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700466 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800467 }
Colin Cross2fe66872015-03-30 17:20:39 -0700468 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700469
Colin Cross42d48b72018-08-29 14:10:52 -0700470 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
471 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700472
Colin Crossbe9cdb82019-01-21 21:37:16 -0800473 ctx.AddFarVariationDependencies([]blueprint.Variation{
474 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
475 }, pluginTag, j.properties.Plugins...)
476
Colin Cross7f9036c2017-08-30 13:27:57 -0700477 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000478 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700479 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800480 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800481 android.ExtractSourceDeps(ctx, j.properties.Jarjar_rules)
Colin Cross6af17aa2017-09-20 12:59:05 -0700482
483 if j.hasSrcExt(".proto") {
484 protoDeps(ctx, &j.protoProperties)
485 }
Colin Cross93e85952017-08-15 13:34:18 -0700486
487 if j.hasSrcExt(".kt") {
488 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
489 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700490 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800491 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800492 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
493 }
Colin Cross93e85952017-08-15 13:34:18 -0700494 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800495
496 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700497 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800498 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700499}
500
501func hasSrcExt(srcs []string, ext string) bool {
502 for _, src := range srcs {
503 if filepath.Ext(src) == ext {
504 return true
505 }
506 }
507
508 return false
509}
510
Nan Zhang61eaedb2017-11-02 13:28:15 -0700511func shardPaths(paths android.Paths, shardSize int) []android.Paths {
512 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
513 for len(paths) > shardSize {
514 ret = append(ret, paths[0:shardSize])
515 paths = paths[shardSize:]
516 }
517 if len(paths) > 0 {
518 ret = append(ret, paths)
519 }
520 return ret
521}
522
Colin Cross6af17aa2017-09-20 12:59:05 -0700523func (j *Module) hasSrcExt(ext string) bool {
524 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700525}
526
Colin Cross46c9b8b2017-06-22 16:51:17 -0700527func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700528 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700529
Colin Crossebe1a512017-11-14 13:12:14 -0800530 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
531 aidlIncludes = append(aidlIncludes,
532 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
533 aidlIncludes = append(aidlIncludes,
534 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700535
Steven Moreland36b130f2019-02-05 17:02:55 -0800536 flags := []string{}
Steven Moreland667f6882018-07-26 12:55:08 -0700537
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700538 if aidlPreprocess.Valid() {
539 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700540 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700541 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700542 }
543
Colin Cross635c3b02016-05-18 15:37:25 -0700544 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800545 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700546 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800547 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700548 flags = append(flags, "-I"+src.String())
549 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700550
Martijn Coeneneab15642018-03-09 09:29:59 +0100551 if Bool(j.deviceProperties.Aidl.Generate_traces) {
552 flags = append(flags, "-t")
553 }
554
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100555 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
556 flags = append(flags, "--transaction_names")
557 }
558
Colin Crossf03c82b2015-04-13 13:53:40 -0700559 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700560}
561
Colin Cross32f676a2017-09-06 13:41:06 -0700562type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800563 classpath classpath
564 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700565 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800566 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700567 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700568 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700569 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700570 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800571 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700572 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700573 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700574 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700575 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800576 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800577
578 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700579}
Colin Cross2fe66872015-03-30 17:20:39 -0700580
Colin Cross54250902017-12-05 09:28:08 -0800581func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
582 for _, f := range dep.Srcs() {
583 if f.Ext() != ".jar" {
584 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
585 ctx.OtherModuleName(dep.(blueprint.Module)))
586 }
587 }
588}
589
Jiyong Park2d492942018-03-05 17:44:10 +0900590type linkType int
591
592const (
593 javaCore linkType = iota
594 javaSdk
595 javaSystem
596 javaPlatform
597)
598
Jiyong Park46f78fb2018-10-20 16:33:17 +0900599func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700600 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700601 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900602 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
603 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
604 name == "core-lambda-stubs":
605 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100606 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900607 return javaCore, false
608 case name == "android_system_stubs_current":
609 return javaSystem, true
610 case strings.HasPrefix(ver, "system_"):
611 return javaSystem, false
612 case name == "android_test_stubs_current":
613 return javaSystem, true
614 case strings.HasPrefix(ver, "test_"):
615 return javaPlatform, false
616 case name == "android_stubs_current":
617 return javaSdk, true
618 case ver == "current":
619 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700620 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900621 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700622 default:
623 if _, err := strconv.Atoi(ver); err != nil {
624 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
625 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900626 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900627 }
628}
629
Jiyong Park750e5572018-01-31 00:20:13 +0900630func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700631 if ctx.Host() {
632 return
633 }
634
Jiyong Park46f78fb2018-10-20 16:33:17 +0900635 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
636 if stubs {
637 return
638 }
639 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900640 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."
641
642 switch myLinkType {
643 case javaCore:
644 if otherLinkType != javaCore {
645 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 +0900646 ctx.OtherModuleName(to))
647 }
Jiyong Park2d492942018-03-05 17:44:10 +0900648 break
649 case javaSdk:
650 if otherLinkType != javaCore && otherLinkType != javaSdk {
651 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
652 ctx.OtherModuleName(to))
653 }
654 break
655 case javaSystem:
656 if otherLinkType == javaPlatform {
657 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
658 ctx.OtherModuleName(to))
659 }
660 break
661 case javaPlatform:
662 // no restriction on link-type
663 break
Jiyong Park750e5572018-01-31 00:20:13 +0900664 }
665}
666
Colin Cross32f676a2017-09-06 13:41:06 -0700667func (j *Module) collectDeps(ctx android.ModuleContext) deps {
668 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700669
Colin Cross300f0382018-03-06 13:11:51 -0800670 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700671 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800672 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700673 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800674 } else if sdkDep.useFiles {
675 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700676 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800677 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
678 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700679 }
680
Colin Crossd11fcda2017-10-23 17:59:01 -0700681 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700682 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700683 tag := ctx.OtherModuleDependencyTag(module)
684
Colin Crossa4f08812018-10-02 22:03:40 -0700685 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700686 // Handled by AndroidApp.collectAppDeps
687 return
688 }
689 if tag == certificateTag {
690 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700691 return
692 }
693
Jiyong Park750e5572018-01-31 00:20:13 +0900694 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700695 switch tag {
696 case bootClasspathTag, libTag, staticLibTag:
697 checkLinkType(ctx, j, to, tag.(dependencyTag))
698 }
Jiyong Park750e5572018-01-31 00:20:13 +0900699 }
Colin Cross54250902017-12-05 09:28:08 -0800700 switch dep := module.(type) {
701 case Dependency:
702 switch tag {
703 case bootClasspathTag:
704 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700705 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800706 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900707 // sdk lib names from dependencies are re-exported
708 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800709 case staticLibTag:
710 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
711 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
712 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700713 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900714 // sdk lib names from dependencies are re-exported
715 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800716 case pluginTag:
717 if plugin, ok := dep.(*Plugin); ok {
718 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
719 if plugin.pluginProperties.Processor_class != nil {
720 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
721 }
722 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
723 } else {
724 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
725 }
Colin Cross54250902017-12-05 09:28:08 -0800726 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100727 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800728 // framework.jar has a one-off dependency on the R.java and Manifest.java files
729 // generated by framework-res.apk
730 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
731 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800732 case frameworkApkTag:
733 if ctx.ModuleName() == "android_stubs_current" ||
734 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700735 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800736 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
737 // resource files out of there for aapt.
738 //
739 // Normally the package rule runs aapt, which includes the resource,
740 // but we're not running that in our package rule so just copy in the
741 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700742 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800743 }
Colin Cross54250902017-12-05 09:28:08 -0800744 case kotlinStdlibTag:
745 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800746 case kotlinAnnotationsTag:
747 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800748 }
749
750 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900751 case SdkLibraryDependency:
752 switch tag {
753 case libTag:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900754 deps.classpath = append(deps.classpath, dep.HeaderJars(ctx, j.sdkVersion())...)
Jiyong Park1be96912018-05-28 18:02:19 +0900755 // names of sdk libs that are directly depended are exported
756 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900757 default:
758 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
759 }
Colin Cross54250902017-12-05 09:28:08 -0800760 case android.SourceFileProducer:
761 switch tag {
762 case libTag:
763 checkProducesJars(ctx, dep)
764 deps.classpath = append(deps.classpath, dep.Srcs()...)
765 case staticLibTag:
766 checkProducesJars(ctx, dep)
767 deps.classpath = append(deps.classpath, dep.Srcs()...)
768 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
769 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
770 case android.DefaultsDepTag, android.SourceDepTag:
771 // Nothing to do
Sundong Ahn054b19a2018-10-19 13:46:09 +0900772 case publicApiFileTag, systemApiFileTag, testApiFileTag:
773 // Nothing to do
Colin Cross54250902017-12-05 09:28:08 -0800774 default:
775 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
776 }
777 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700778 switch tag {
779 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700780 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700781 case systemModulesTag:
782 if deps.systemModules != nil {
783 panic("Found two system module dependencies")
784 }
785 sm := module.(*SystemModules)
786 if sm.outputFile == nil {
787 panic("Missing directory for system module dependency")
788 }
789 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700790 default:
791 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700792 }
Colin Crossec7a0422017-07-07 14:47:12 -0700793 }
Colin Cross2fe66872015-03-30 17:20:39 -0700794 })
795
Jiyong Park1be96912018-05-28 18:02:19 +0900796 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
797
Colin Cross32f676a2017-09-06 13:41:06 -0700798 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700799}
800
Colin Cross83bb3162018-06-25 15:48:06 -0700801func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700802 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800803 v := sdkContext.sdkVersion()
804 // For PDK builds, use the latest SDK version instead of "current"
805 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
806 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
807 latestSdkVersion := 0
808 if len(sdkVersions) > 0 {
809 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
810 }
811 v = strconv.Itoa(latestSdkVersion)
812 }
813
814 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700815 if err != nil {
816 ctx.PropertyErrorf("sdk_version", "%s", err)
817 }
Nan Zhang357466b2018-04-17 17:38:36 -0700818 if javaVersion != "" {
819 ret = javaVersion
820 } else if ctx.Device() && sdk <= 23 {
821 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900822 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700823 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700824 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700825 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
826 ret = "1.8"
827 } else {
828 ret = "1.9"
829 }
830
831 return ret
832}
833
Nan Zhanged19fc32017-10-19 13:06:22 -0700834func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700835
Colin Crossf03c82b2015-04-13 13:53:40 -0700836 var flags javaBuilderFlags
837
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100838 // javaVersion flag.
839 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
840
Nan Zhanged19fc32017-10-19 13:06:22 -0700841 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700842 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100843 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700844 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700845 }
Colin Cross6510f912017-11-29 00:27:14 -0800846 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700847 // Override the -g flag passed globally to remove local variable debug info to reduce
848 // disk and memory usage.
849 javacFlags = append(javacFlags, "-g:source,lines")
850 }
Colin Cross64162712017-08-08 13:17:59 -0700851
Colin Cross66548102018-06-19 22:47:35 -0700852 if ctx.Config().RunErrorProne() {
853 if config.ErrorProneClasspath == nil {
854 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
855 }
856
857 errorProneFlags := []string{
858 "-Xplugin:ErrorProne",
859 "${config.ErrorProneChecks}",
860 }
861 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
862
863 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
864 "'" + strings.Join(errorProneFlags, " ") + "'"
865 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800866 }
867
Nan Zhanged19fc32017-10-19 13:06:22 -0700868 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800869 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
870 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700871 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800872
Colin Crossbe9cdb82019-01-21 21:37:16 -0800873 flags.processor = strings.Join(deps.processorClasses, ",")
874
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100875 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800876 !Bool(j.properties.No_standard_libs) &&
877 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
878 // Give host-side tools a version of OpenJDK's standard libraries
879 // close to what they're targeting. As of Dec 2017, AOSP is only
880 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
881 //
882 // When building with OpenJDK 8, the following should have no
883 // effect since those jars would be available by default.
884 //
885 // When building with OpenJDK 9 but targeting a version < 1.8,
886 // putting them on the bootclasspath means that:
887 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
888 // b) references to existing APIs are not reinterpreted in an
889 // OpenJDK 9-specific way, eg. calls to subclasses of
890 // java.nio.Buffer as in http://b/70862583
891 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
892 flags.bootClasspath = append(flags.bootClasspath,
893 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
894 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800895 if Bool(j.properties.Use_tools_jar) {
896 flags.bootClasspath = append(flags.bootClasspath,
897 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
898 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800899 }
900
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100901 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800902 // Manually specify build directory in case it is not under the repo root.
903 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
904 // just adding a symlink under the root doesn't help.)
905 patchPaths := ".:" + ctx.Config().BuildDir()
906 classPath := flags.classpath.FormJavaClassPath("")
907 if classPath != "" {
908 patchPaths += ":" + classPath
909 }
910 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700911 }
912
Nan Zhanged19fc32017-10-19 13:06:22 -0700913 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700914 if deps.systemModules != nil {
915 flags.systemModules = append(flags.systemModules, deps.systemModules)
916 }
917
Nan Zhanged19fc32017-10-19 13:06:22 -0700918 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700919 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700920 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700921 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700922 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
923 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700924 }
925
Colin Cross81440082018-08-15 20:21:55 -0700926 if len(javacFlags) > 0 {
927 // optimization.
928 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
929 flags.javacFlags = "$javacFlags"
930 }
931
Nan Zhanged19fc32017-10-19 13:06:22 -0700932 return flags
933}
Colin Crossc0b06f12015-04-08 13:03:43 -0700934
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800935func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700936
Colin Crossebe1a512017-11-14 13:12:14 -0800937 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700938
939 deps := j.collectDeps(ctx)
940 flags := j.collectBuilderFlags(ctx, deps)
941
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100942 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700943 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
944 }
945 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700946 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800947 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700948 }
949
Colin Crossaf050172017-11-15 23:01:59 -0800950 srcFiles = j.genSources(ctx, srcFiles, flags)
951
952 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700953 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800954 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700955
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700956 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
957 // that IDEInfo struct will use
958 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
959
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800960 if j.properties.Jarjar_rules != nil {
961 j.expandJarjarRules = ctx.ExpandSource(*j.properties.Jarjar_rules, "jarjar_rules")
962 }
963
Colin Cross1ee23172017-10-18 14:44:18 -0700964 jarName := ctx.ModuleName() + ".jar"
965
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000966 javaSrcFiles := srcFiles.FilterByExt(".java")
967 var uniqueSrcFiles android.Paths
968 set := make(map[string]bool)
969 for _, v := range javaSrcFiles {
970 if _, found := set[v.String()]; !found {
971 set[v.String()] = true
972 uniqueSrcFiles = append(uniqueSrcFiles, v)
973 }
974 }
975
Colin Cross55f63ea2018-08-27 12:37:09 -0700976 var kotlinJars android.Paths
977
Colin Cross93e85952017-08-15 13:34:18 -0700978 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200979 // user defined kotlin flags.
980 kotlincFlags := j.properties.Kotlincflags
981 CheckKotlincFlags(ctx, kotlincFlags)
982
Colin Cross93e85952017-08-15 13:34:18 -0700983 // If there are kotlin files, compile them first but pass all the kotlin and java files
984 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
985 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200986 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700987 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200988 kotlincFlags = append(kotlincFlags, "-no-jdk")
989 }
990 if len(kotlincFlags) > 0 {
991 // optimization.
992 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
993 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700994 }
995
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000996 var kotlinSrcFiles android.Paths
997 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
998 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
999
Colin Crossafbb1732019-01-17 15:42:52 -08001000 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1001 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1002
1003 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1004 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1005
1006 if len(flags.processorPath) > 0 {
1007 // Use kapt for annotation processing
1008 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1009 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1010 srcJars = append(srcJars, kaptSrcJar)
1011 // Disable annotation processing in javac, it's already been handled by kapt
1012 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001013 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001014 }
Colin Cross93e85952017-08-15 13:34:18 -07001015
Colin Cross1ee23172017-10-18 14:44:18 -07001016 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001017 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001018 if ctx.Failed() {
1019 return
1020 }
1021
1022 // Make javac rule depend on the kotlinc rule
1023 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001024
Colin Cross93e85952017-08-15 13:34:18 -07001025 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001026 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001027 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001028 }
1029
Colin Cross55f63ea2018-08-27 12:37:09 -07001030 jars := append(android.Paths(nil), kotlinJars...)
1031
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001032 // Store the list of .java files that was passed to javac
1033 j.compiledJavaSrcs = uniqueSrcFiles
1034 j.compiledSrcJars = srcJars
1035
Nan Zhang61eaedb2017-11-02 13:28:15 -07001036 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001037 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001038 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1039 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001040 // Formerly, there was a check here that prevented annotation processors
1041 // from being used when sharding was enabled, as some annotation processors
1042 // do not function correctly in sharded environments. It was removed to
1043 // allow for the use of annotation processors that do function correctly
1044 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001045 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001046 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001047 if ctx.Failed() {
1048 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001049 }
1050 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001051 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001052 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001053 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001054 // If error-prone is enabled, add an additional rule to compile the java files into
1055 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001056 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001057 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1058 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001059 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001060 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001061 extraJarDeps = append(extraJarDeps, errorprone)
1062 }
1063
Nan Zhang61eaedb2017-11-02 13:28:15 -07001064 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001065 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001066 shardSize := int(*(j.properties.Javac_shard_size))
1067 var shardSrcs []android.Paths
1068 if len(uniqueSrcFiles) > 0 {
1069 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1070 for idx, shardSrc := range shardSrcs {
1071 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1072 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1073 jars = append(jars, classes)
1074 }
1075 }
1076 if len(srcJars) > 0 {
1077 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1078 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1079 jars = append(jars, classes)
1080 }
1081 } else {
1082 classes := android.PathForModuleOut(ctx, "javac", jarName)
1083 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1084 jars = append(jars, classes)
1085 }
Colin Crossd6891432017-09-27 17:39:56 -07001086 if ctx.Failed() {
1087 return
1088 }
Colin Cross2fe66872015-03-30 17:20:39 -07001089 }
1090
Colin Crosscedd4762018-09-13 11:26:19 -07001091 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1092 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001093 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1094
1095 var resArgs []string
1096 var resDeps android.Paths
1097
1098 resArgs = append(resArgs, dirArgs...)
1099 resDeps = append(resDeps, dirDeps...)
1100
1101 resArgs = append(resArgs, fileArgs...)
1102 resDeps = append(resDeps, fileDeps...)
1103
Colin Crossff3ae9d2018-04-10 16:15:18 -07001104 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001105 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001106 resArgs = append(resArgs, srcArgs...)
1107 resDeps = append(resDeps, srcDeps...)
1108 }
Colin Cross40a36712017-09-27 17:41:35 -07001109
1110 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001111 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001112 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001113 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001114 if ctx.Failed() {
1115 return
1116 }
1117 }
1118
Colin Cross331a1212018-08-15 20:40:52 -07001119 if len(deps.staticResourceJars) > 0 {
1120 var jars android.Paths
1121 if j.resourceJar != nil {
1122 jars = append(jars, j.resourceJar)
1123 }
1124 jars = append(jars, deps.staticResourceJars...)
1125
1126 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1127 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1128 false, nil, nil)
1129 j.resourceJar = combinedJar
1130 }
1131
Colin Cross32f676a2017-09-06 13:41:06 -07001132 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001133 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001134
Colin Cross094054a2018-10-17 15:10:48 -07001135 manifest := j.overrideManifest
1136 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001137 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1138 }
Colin Cross635acc92017-09-12 22:50:46 -07001139
Colin Cross0a6e0072017-08-30 14:24:55 -07001140 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001141 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001142 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001143
1144 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001145 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1146 // Optimization: skip the combine step if there is nothing to do
1147 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1148 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1149 // any if len(jars) == 1.
1150 outputFile = moduleOutPath
1151 } else {
1152 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1153 ctx.Build(pctx, android.BuildParams{
1154 Rule: android.Cp,
1155 Input: jars[0],
1156 Output: combinedJar,
1157 })
1158 outputFile = combinedJar
1159 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001160 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001161 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001162 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001163 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001164 outputFile = combinedJar
1165 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001166
Colin Cross331a1212018-08-15 20:40:52 -07001167 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001168 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001169 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001170 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001171 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001172 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001173
1174 // jarjar resource jar if necessary
1175 if j.resourceJar != nil {
1176 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001177 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001178 j.resourceJar = resourceJarJarFile
1179 }
1180
Colin Cross0a6e0072017-08-30 14:24:55 -07001181 if ctx.Failed() {
1182 return
1183 }
1184 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001185 j.implementationJarFile = outputFile
1186 if j.headerJarFile == nil {
1187 j.headerJarFile = j.implementationJarFile
1188 }
Colin Cross2fe66872015-03-30 17:20:39 -07001189
Colin Cross6510f912017-11-29 00:27:14 -08001190 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001191 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1192 j.properties.Instrument = true
1193 }
1194 }
1195
Colin Cross3144dfc2018-01-03 15:06:47 -08001196 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001197 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1198 }
1199
Colin Cross331a1212018-08-15 20:40:52 -07001200 // merge implementation jar with resources if necessary
1201 implementationAndResourcesJar := outputFile
1202 if j.resourceJar != nil {
1203 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1204 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1205 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1206 false, nil, nil)
1207 implementationAndResourcesJar = combinedJar
1208 }
1209
1210 j.implementationAndResourcesJar = implementationAndResourcesJar
1211
Colin Cross9ae1b922018-06-26 17:59:05 -07001212 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001213 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001214 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001215 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001216 if ctx.Failed() {
1217 return
1218 }
Colin Cross331a1212018-08-15 20:40:52 -07001219
Colin Cross8faf8fc2019-01-16 15:15:52 -08001220 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001221 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1222 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001223
Colin Cross331a1212018-08-15 20:40:52 -07001224 // merge dex jar with resources if necessary
1225 if j.resourceJar != nil {
1226 jars := android.Paths{dexOutputFile, j.resourceJar}
1227 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1228 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1229 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001230 if j.deviceProperties.UncompressDex {
1231 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1232 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1233 dexOutputFile = combinedAlignedJar
1234 } else {
1235 dexOutputFile = combinedJar
1236 }
Colin Cross331a1212018-08-15 20:40:52 -07001237 }
1238
1239 j.dexJarFile = dexOutputFile
1240
Colin Cross8faf8fc2019-01-16 15:15:52 -08001241 // Dexpreopting
Colin Crossdc2da912019-01-05 22:13:05 -08001242 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross2fc72f62018-12-21 12:59:54 -08001243 j.dexpreopter.uncompressedDex = j.deviceProperties.UncompressDex
Colin Cross43f08db2018-11-12 10:13:39 -08001244 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1245
1246 j.maybeStrippedDexJarFile = dexOutputFile
1247
Colin Cross3063b782018-08-15 11:19:12 -07001248 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001249
1250 if ctx.Failed() {
1251 return
1252 }
Colin Cross331a1212018-08-15 20:40:52 -07001253 } else {
1254 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001255 }
Colin Cross331a1212018-08-15 20:40:52 -07001256
Colin Crossb7a63242015-04-16 14:09:14 -07001257 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001258
1259 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1260 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001261}
1262
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001263// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1264// since some of these flags may be used internally.
1265func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1266 for _, flag := range flags {
1267 flag = strings.TrimSpace(flag)
1268
1269 if !strings.HasPrefix(flag, "-") {
1270 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1271 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1272 ctx.PropertyErrorf("kotlincflags",
1273 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1274 } else if inList(flag, config.KotlincIllegalFlags) {
1275 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1276 } else if flag == "-include-runtime" {
1277 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1278 } else {
1279 args := strings.Split(flag, " ")
1280 if args[0] == "-kotlin-home" {
1281 ctx.PropertyErrorf("kotlincflags",
1282 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1283 }
1284 }
1285 }
1286}
1287
Colin Cross8eadbf02017-10-24 17:46:00 -07001288func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001289 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001290
1291 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001292 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001293 // Compile java sources into turbine.jar.
1294 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1295 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1296 if ctx.Failed() {
1297 return nil
1298 }
1299 jars = append(jars, turbineJar)
1300 }
1301
Colin Cross55f63ea2018-08-27 12:37:09 -07001302 jars = append(jars, extraJars...)
1303
Nan Zhanged19fc32017-10-19 13:06:22 -07001304 // Combine any static header libraries into classes-header.jar. If there is only
1305 // one input jar this step will be skipped.
1306 var headerJar android.Path
1307 jars = append(jars, deps.staticHeaderJars...)
1308
Colin Cross5c6ecc12017-10-23 18:12:27 -07001309 // we cannot skip the combine step for now if there is only one jar
1310 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1311 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001312 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1313 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001314 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001315
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001316 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001317 // Transform classes.jar into classes-jarjar.jar
1318 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001319 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001320 headerJar = jarjarFile
1321 if ctx.Failed() {
1322 return nil
1323 }
1324 }
1325
1326 return headerJar
1327}
1328
Colin Crosscb933592017-11-22 13:49:43 -08001329func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001330 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001331
Colin Cross7a3139e2017-12-19 13:57:50 -08001332 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001333
Colin Cross84c38822018-01-03 15:59:46 -08001334 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001335 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1336
1337 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1338
1339 j.jacocoReportClassesFile = jacocoReportClassesFile
1340
1341 return instrumentedJar
1342}
1343
albaltai36ff7dc2018-12-25 14:35:23 +08001344var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001345
Nan Zhanged19fc32017-10-19 13:06:22 -07001346func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001347 if j.headerJarFile == nil {
1348 return nil
1349 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001350 return android.Paths{j.headerJarFile}
1351}
1352
1353func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001354 if j.implementationJarFile == nil {
1355 return nil
1356 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001357 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001358}
1359
Colin Crossf24a22a2019-01-31 14:12:44 -08001360func (j *Module) DexJar() android.Path {
1361 return j.dexJarFile
1362}
1363
Colin Cross331a1212018-08-15 20:40:52 -07001364func (j *Module) ResourceJars() android.Paths {
1365 if j.resourceJar == nil {
1366 return nil
1367 }
1368 return android.Paths{j.resourceJar}
1369}
1370
1371func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001372 if j.implementationAndResourcesJar == nil {
1373 return nil
1374 }
Colin Cross331a1212018-08-15 20:40:52 -07001375 return android.Paths{j.implementationAndResourcesJar}
1376}
1377
Colin Cross46c9b8b2017-06-22 16:51:17 -07001378func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001379 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001380 return j.exportAidlIncludeDirs
1381}
1382
Jiyong Park1be96912018-05-28 18:02:19 +09001383func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001384 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001385 return j.exportedSdkLibs
1386}
1387
Colin Cross46c9b8b2017-06-22 16:51:17 -07001388var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001389
Colin Cross46c9b8b2017-06-22 16:51:17 -07001390func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001391 return j.logtagsSrcs
1392}
1393
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001394// Collect information for opening IDE project files in java/jdeps.go.
1395func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1396 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1397 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1398 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001399 if j.expandJarjarRules != nil {
1400 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001401 }
1402}
1403
1404func (j *Module) CompilerDeps() []string {
1405 jdeps := []string{}
1406 jdeps = append(jdeps, j.properties.Libs...)
1407 jdeps = append(jdeps, j.properties.Static_libs...)
1408 return jdeps
1409}
1410
Colin Cross2fe66872015-03-30 17:20:39 -07001411//
1412// Java libraries (.jar file)
1413//
1414
Colin Crossf506d872017-07-19 15:53:04 -07001415type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001416 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001417}
1418
Colin Cross2fc72f62018-12-21 12:59:54 -08001419func (j *Library) shouldUncompressDex(ctx android.ModuleContext) bool {
Nicolas Geoffray5921f752019-01-24 17:52:04 +00001420 // Store uncompressed (and do not strip) dex files from boot class path jars that are
1421 // in an apex.
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001422 if inList(ctx.ModuleName(), ctx.Config().BootJars()) &&
Nicolas Geoffray5921f752019-01-24 17:52:04 +00001423 android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001424 return true
1425 }
Colin Cross2fc72f62018-12-21 12:59:54 -08001426 return false
1427}
1428
Colin Crossf506d872017-07-19 15:53:04 -07001429func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001430 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1431 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross2fc72f62018-12-21 12:59:54 -08001432 j.deviceProperties.UncompressDex = j.shouldUncompressDex(ctx)
Colin Cross46c9b8b2017-06-22 16:51:17 -07001433 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001434
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001435 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001436 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1437 ctx.ModuleName()+".jar", j.outputFile)
1438 }
Colin Crossb7a63242015-04-16 14:09:14 -07001439}
1440
Colin Crossf506d872017-07-19 15:53:04 -07001441func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001442 j.deps(ctx)
1443}
1444
Colin Cross9ae1b922018-06-26 17:59:05 -07001445func LibraryFactory() android.Module {
1446 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001447
Colin Cross9ae1b922018-06-26 17:59:05 -07001448 module.AddProperties(
1449 &module.Module.properties,
1450 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001451 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001452 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001453
Colin Cross9ae1b922018-06-26 17:59:05 -07001454 InitJavaModule(module, android.HostAndDeviceSupported)
1455 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001456}
1457
Colin Crossf506d872017-07-19 15:53:04 -07001458func LibraryHostFactory() android.Module {
1459 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001460
Colin Cross6af17aa2017-09-20 12:59:05 -07001461 module.AddProperties(
1462 &module.Module.properties,
1463 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001464
Colin Cross9ae1b922018-06-26 17:59:05 -07001465 module.Module.properties.Installable = proptools.BoolPtr(true)
1466
Colin Cross89536d42017-07-07 14:35:50 -07001467 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001468 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001469}
1470
1471//
Colin Crossb628ea52018-08-14 16:42:33 -07001472// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001473//
1474
1475type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001476 // list of compatibility suites (for example "cts", "vts") that the module should be
1477 // installed into.
1478 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001479
1480 // the name of the test configuration (for example "AndroidTest.xml") that should be
1481 // installed with the module.
1482 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001483
Jack He33338892018-09-19 02:21:28 -07001484 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1485 // should be installed with the module.
1486 Test_config_template *string `android:"arch_variant"`
1487
Colin Crossd96ca352018-08-10 16:06:24 -07001488 // list of files or filegroup modules that provide data that should be installed alongside
1489 // the test
1490 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001491}
1492
1493type Test struct {
1494 Library
1495
1496 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001497
1498 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001499 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001500}
1501
1502func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001503 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001504 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001505
1506 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001507}
1508
1509func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1510 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001511 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001512 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001513 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001514}
1515
1516func TestFactory() android.Module {
1517 module := &Test{}
1518
1519 module.AddProperties(
1520 &module.Module.properties,
1521 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001522 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001523 &module.Module.protoProperties,
1524 &module.testProperties)
1525
Colin Cross9ae1b922018-06-26 17:59:05 -07001526 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001527 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001528
Colin Cross05638fc2018-04-09 18:40:24 -07001529 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001530 return module
1531}
1532
1533func TestHostFactory() android.Module {
1534 module := &Test{}
1535
1536 module.AddProperties(
1537 &module.Module.properties,
1538 &module.Module.protoProperties,
1539 &module.testProperties)
1540
Colin Cross9ae1b922018-06-26 17:59:05 -07001541 module.Module.properties.Installable = proptools.BoolPtr(true)
1542
Colin Cross05638fc2018-04-09 18:40:24 -07001543 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001544 return module
1545}
1546
1547//
Colin Cross2fe66872015-03-30 17:20:39 -07001548// Java Binaries (.jar file plus wrapper script)
1549//
1550
Colin Crossf506d872017-07-19 15:53:04 -07001551type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001552 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001553 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001554
1555 // Name of the class containing main to be inserted into the manifest as Main-Class.
1556 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001557}
1558
Colin Crossf506d872017-07-19 15:53:04 -07001559type Binary struct {
1560 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001561
Colin Crossf506d872017-07-19 15:53:04 -07001562 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001563
Colin Cross6b4a32d2017-12-05 13:42:45 -08001564 isWrapperVariant bool
1565
Colin Crossc3315992017-12-08 19:12:36 -08001566 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001567 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001568}
1569
Alex Light24237172017-10-26 09:46:21 -07001570func (j *Binary) HostToolPath() android.OptionalPath {
1571 return android.OptionalPathForPath(j.binaryFile)
1572}
1573
Colin Crossf506d872017-07-19 15:53:04 -07001574func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001575 if ctx.Arch().ArchType == android.Common {
1576 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001577 if j.binaryProperties.Main_class != nil {
1578 if j.properties.Manifest != nil {
1579 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1580 }
1581 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1582 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1583 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1584 }
1585
Colin Cross6b4a32d2017-12-05 13:42:45 -08001586 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001587 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001588 // Handle the binary wrapper
1589 j.isWrapperVariant = true
1590
Colin Cross366938f2017-12-11 16:29:02 -08001591 if j.binaryProperties.Wrapper != nil {
1592 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001593 } else {
1594 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1595 }
1596
1597 // Depend on the installed jar so that the wrapper doesn't get executed by
1598 // another build rule before the jar has been installed.
1599 jarFile := ctx.PrimaryModule().(*Binary).installFile
1600
1601 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1602 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001603 }
Colin Cross2fe66872015-03-30 17:20:39 -07001604}
1605
Colin Crossf506d872017-07-19 15:53:04 -07001606func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001607 if ctx.Arch().ArchType == android.Common {
1608 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001609 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001610 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001611 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001612}
1613
Colin Crossf506d872017-07-19 15:53:04 -07001614func BinaryFactory() android.Module {
1615 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001616
Colin Cross36242852017-06-23 15:06:31 -07001617 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001618 &module.Module.properties,
1619 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001620 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001621 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001622 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001623
Colin Cross9ae1b922018-06-26 17:59:05 -07001624 module.Module.properties.Installable = proptools.BoolPtr(true)
1625
Colin Cross6b4a32d2017-12-05 13:42:45 -08001626 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1627 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001628 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001629}
1630
Colin Crossf506d872017-07-19 15:53:04 -07001631func BinaryHostFactory() android.Module {
1632 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001633
Colin Cross36242852017-06-23 15:06:31 -07001634 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001635 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001636 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001637 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001638
Colin Cross9ae1b922018-06-26 17:59:05 -07001639 module.Module.properties.Installable = proptools.BoolPtr(true)
1640
Colin Cross6b4a32d2017-12-05 13:42:45 -08001641 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1642 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001643 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001644}
1645
1646//
1647// Java prebuilts
1648//
1649
Colin Cross74d73e22017-08-02 11:05:49 -07001650type ImportProperties struct {
1651 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001652
Nan Zhangea568a42017-11-08 21:20:04 -08001653 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001654
1655 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001656
1657 // List of shared java libs that this module has dependencies to
1658 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001659
1660 // List of files to remove from the jar file(s)
1661 Exclude_files []string
1662
1663 // List of directories to remove from the jar file(s)
1664 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001665
1666 // if set to true, run Jetifier against .jar file. Defaults to false.
1667 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001668}
1669
1670type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001671 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001672 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001673 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001674
Colin Cross74d73e22017-08-02 11:05:49 -07001675 properties ImportProperties
1676
Colin Cross0a6e0072017-08-30 14:24:55 -07001677 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001678 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001679}
1680
Colin Cross83bb3162018-06-25 15:48:06 -07001681func (j *Import) sdkVersion() string {
1682 return String(j.properties.Sdk_version)
1683}
1684
1685func (j *Import) minSdkVersion() string {
1686 return j.sdkVersion()
1687}
1688
Colin Cross74d73e22017-08-02 11:05:49 -07001689func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001690 return &j.prebuilt
1691}
1692
Colin Cross74d73e22017-08-02 11:05:49 -07001693func (j *Import) PrebuiltSrcs() []string {
1694 return j.properties.Jars
1695}
1696
1697func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001698 return j.prebuilt.Name(j.ModuleBase.Name())
1699}
1700
Colin Cross74d73e22017-08-02 11:05:49 -07001701func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001702 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001703 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001704}
1705
Colin Cross74d73e22017-08-02 11:05:49 -07001706func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001707 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001708
Nan Zhang4c819fb2018-08-27 18:31:46 -07001709 jarName := ctx.ModuleName() + ".jar"
1710 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001711 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1712 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001713 if Bool(j.properties.Jetifier_enabled) {
1714 inputFile := outputFile
1715 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1716 TransformJetifier(ctx, outputFile, inputFile)
1717 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001718 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001719
1720 ctx.VisitDirectDeps(func(module android.Module) {
1721 otherName := ctx.OtherModuleName(module)
1722 tag := ctx.OtherModuleDependencyTag(module)
1723
1724 switch dep := module.(type) {
1725 case Dependency:
1726 switch tag {
1727 case libTag, staticLibTag:
1728 // sdk lib names from dependencies are re-exported
1729 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1730 }
1731 case SdkLibraryDependency:
1732 switch tag {
1733 case libTag:
1734 // names of sdk libs that are directly depended are exported
1735 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1736 }
1737 }
1738 })
1739
1740 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001741 if Bool(j.properties.Installable) {
1742 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1743 ctx.ModuleName()+".jar", outputFile)
1744 }
Colin Cross2fe66872015-03-30 17:20:39 -07001745}
1746
Colin Cross74d73e22017-08-02 11:05:49 -07001747var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001748
Nan Zhanged19fc32017-10-19 13:06:22 -07001749func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001750 if j.combinedClasspathFile == nil {
1751 return nil
1752 }
Colin Cross37f6d792018-07-12 12:28:41 -07001753 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001754}
1755
1756func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001757 if j.combinedClasspathFile == nil {
1758 return nil
1759 }
Colin Cross37f6d792018-07-12 12:28:41 -07001760 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001761}
1762
Colin Cross331a1212018-08-15 20:40:52 -07001763func (j *Import) ResourceJars() android.Paths {
1764 return nil
1765}
1766
1767func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001768 if j.combinedClasspathFile == nil {
1769 return nil
1770 }
Colin Cross331a1212018-08-15 20:40:52 -07001771 return android.Paths{j.combinedClasspathFile}
1772}
1773
Colin Crossf24a22a2019-01-31 14:12:44 -08001774func (j *Import) DexJar() android.Path {
1775 return nil
1776}
1777
Colin Cross74d73e22017-08-02 11:05:49 -07001778func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001779 return nil
1780}
1781
Jiyong Park1be96912018-05-28 18:02:19 +09001782func (j *Import) ExportedSdkLibs() []string {
1783 return j.exportedSdkLibs
1784}
1785
albaltai36ff7dc2018-12-25 14:35:23 +08001786// Add compile time check for interface implementation
1787var _ android.IDEInfo = (*Import)(nil)
1788var _ android.IDECustomizedModuleName = (*Import)(nil)
1789
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001790// Collect information for opening IDE project files in java/jdeps.go.
1791const (
1792 removedPrefix = "prebuilt_"
1793)
1794
1795func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1796 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1797}
1798
1799func (j *Import) IDECustomizedModuleName() string {
1800 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1801 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1802 // solution to get the Import name.
1803 name := j.Name()
1804 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001805 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001806 }
1807 return name
1808}
1809
Colin Cross74d73e22017-08-02 11:05:49 -07001810var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001811
Colin Cross74d73e22017-08-02 11:05:49 -07001812func ImportFactory() android.Module {
1813 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001814
Colin Cross74d73e22017-08-02 11:05:49 -07001815 module.AddProperties(&module.properties)
1816
1817 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001818 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001819 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001820}
1821
Colin Cross74d73e22017-08-02 11:05:49 -07001822func ImportFactoryHost() android.Module {
1823 module := &Import{}
1824
1825 module.AddProperties(&module.properties)
1826
1827 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001828 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001829 return module
1830}
1831
Colin Cross89536d42017-07-07 14:35:50 -07001832//
1833// Defaults
1834//
1835type Defaults struct {
1836 android.ModuleBase
1837 android.DefaultsModuleBase
1838}
1839
1840func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1841}
1842
Colin Cross89536d42017-07-07 14:35:50 -07001843func defaultsFactory() android.Module {
1844 return DefaultsFactory()
1845}
1846
1847func DefaultsFactory(props ...interface{}) android.Module {
1848 module := &Defaults{}
1849
1850 module.AddProperties(props...)
1851 module.AddProperties(
1852 &CompilerProperties{},
1853 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08001854 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001855 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001856 &aaptProperties{},
1857 &androidLibraryProperties{},
1858 &appProperties{},
1859 &appTestProperties{},
1860 &ImportProperties{},
1861 &AARImportProperties{},
1862 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001863 )
1864
1865 android.InitDefaultsModule(module)
1866
1867 return module
1868}
Nan Zhangea568a42017-11-08 21:20:04 -08001869
1870var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001871var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001872var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001873var inList = android.InList