blob: 70c6c438073c618633c87ef250436069340cfa53 [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 Cross43f08db2018-11-12 10:13:39 -0800316 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700317}
318
Colin Cross54250902017-12-05 09:28:08 -0800319func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700320 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800321}
322
Jiyong Park8fd61922018-11-08 02:50:25 +0900323func (j *Module) DexJarFile() android.Path {
324 return j.dexJarFile
325}
326
Colin Cross54250902017-12-05 09:28:08 -0800327var _ android.SourceFileProducer = (*Module)(nil)
328
Colin Crossf506d872017-07-19 15:53:04 -0700329type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700330 HeaderJars() android.Paths
331 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700332 ResourceJars() android.Paths
333 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700334 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900335 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700336}
337
Jiyong Parkc678ad32018-04-10 13:07:10 +0900338type SdkLibraryDependency interface {
Sundong Ahn054b19a2018-10-19 13:46:09 +0900339 HeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
340 ImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900341}
342
Nan Zhangb2b33de2018-02-23 11:18:47 -0800343type SrcDependency interface {
344 CompiledSrcs() android.Paths
345 CompiledSrcJars() android.Paths
346}
347
348func (j *Module) CompiledSrcs() android.Paths {
349 return j.compiledJavaSrcs
350}
351
352func (j *Module) CompiledSrcJars() android.Paths {
353 return j.compiledSrcJars
354}
355
356var _ SrcDependency = (*Module)(nil)
357
Colin Cross89536d42017-07-07 14:35:50 -0700358func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
359 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
360 android.InitDefaultableModule(module)
361}
362
Colin Crossbe1da472017-07-07 15:59:46 -0700363type dependencyTag struct {
364 blueprint.BaseDependencyTag
365 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700366}
367
Colin Crossa4f08812018-10-02 22:03:40 -0700368type jniDependencyTag struct {
369 blueprint.BaseDependencyTag
370 target android.Target
371}
372
Colin Crossbe1da472017-07-07 15:59:46 -0700373var (
Colin Cross4b964c02018-10-15 16:18:06 -0700374 staticLibTag = dependencyTag{name: "staticlib"}
375 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800376 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700377 bootClasspathTag = dependencyTag{name: "bootclasspath"}
378 systemModulesTag = dependencyTag{name: "system modules"}
379 frameworkResTag = dependencyTag{name: "framework-res"}
380 frameworkApkTag = dependencyTag{name: "framework-apk"}
381 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800382 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700383 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
384 certificateTag = dependencyTag{name: "certificate"}
385 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700386)
Colin Cross2fe66872015-03-30 17:20:39 -0700387
Colin Crossfc3674a2017-09-18 17:41:52 -0700388type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700389 useModule, useFiles, useDefaultLibs, invalidVersion bool
390
Colin Cross86a60ae2018-05-29 14:44:55 -0700391 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700392 systemModules string
393
Colin Crossa97c5d32018-03-28 14:58:31 -0700394 frameworkResModule string
395
Colin Cross86a60ae2018-05-29 14:44:55 -0700396 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700397 aidl android.Path
398}
399
Colin Crossa4f08812018-10-02 22:03:40 -0700400type jniLib struct {
401 name string
402 path android.Path
403 target android.Target
404}
405
Colin Cross3144dfc2018-01-03 15:06:47 -0800406func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
407 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
408}
409
410func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
411 return j.shouldInstrument(ctx) &&
412 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
413 ctx.Config().UnbundledBuild())
414}
415
Colin Cross83bb3162018-06-25 15:48:06 -0700416func (j *Module) sdkVersion() string {
417 return String(j.deviceProperties.Sdk_version)
418}
419
420func (j *Module) minSdkVersion() string {
421 if j.deviceProperties.Min_sdk_version != nil {
422 return *j.deviceProperties.Min_sdk_version
423 }
424 return j.sdkVersion()
425}
426
Dan Willemsen419290a2018-10-31 15:28:47 -0700427func (j *Module) targetSdkVersion() string {
428 if j.deviceProperties.Target_sdk_version != nil {
429 return *j.deviceProperties.Target_sdk_version
430 }
431 return j.sdkVersion()
432}
433
Colin Crossbe1da472017-07-07 15:59:46 -0700434func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700435 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700436 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700437 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700438 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700439 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100440 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700441 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700442 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700443 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700444 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100445 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700446 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800447 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700448 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
449 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800450 }
Colin Crossbe1da472017-07-07 15:59:46 -0700451 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700452 } else if j.deviceProperties.System_modules == nil {
453 ctx.PropertyErrorf("no_standard_libs",
454 "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 +0100455 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700456 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700457 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100458 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700459 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800460 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800461 if ctx.ModuleName() == "android_stubs_current" ||
462 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700463 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700464 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800465 }
Colin Cross2fe66872015-03-30 17:20:39 -0700466 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700467
Colin Cross42d48b72018-08-29 14:10:52 -0700468 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
469 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700470
Colin Crossbe9cdb82019-01-21 21:37:16 -0800471 ctx.AddFarVariationDependencies([]blueprint.Variation{
472 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
473 }, pluginTag, j.properties.Plugins...)
474
Colin Cross7f9036c2017-08-30 13:27:57 -0700475 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000476 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700477 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800478 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800479 android.ExtractSourceDeps(ctx, j.properties.Jarjar_rules)
Colin Cross6af17aa2017-09-20 12:59:05 -0700480
481 if j.hasSrcExt(".proto") {
482 protoDeps(ctx, &j.protoProperties)
483 }
Colin Cross93e85952017-08-15 13:34:18 -0700484
485 if j.hasSrcExt(".kt") {
486 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
487 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700488 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800489 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800490 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
491 }
Colin Cross93e85952017-08-15 13:34:18 -0700492 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800493
494 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700495 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800496 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700497}
498
499func hasSrcExt(srcs []string, ext string) bool {
500 for _, src := range srcs {
501 if filepath.Ext(src) == ext {
502 return true
503 }
504 }
505
506 return false
507}
508
Nan Zhang61eaedb2017-11-02 13:28:15 -0700509func shardPaths(paths android.Paths, shardSize int) []android.Paths {
510 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
511 for len(paths) > shardSize {
512 ret = append(ret, paths[0:shardSize])
513 paths = paths[shardSize:]
514 }
515 if len(paths) > 0 {
516 ret = append(ret, paths)
517 }
518 return ret
519}
520
Colin Cross6af17aa2017-09-20 12:59:05 -0700521func (j *Module) hasSrcExt(ext string) bool {
522 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700523}
524
Colin Cross46c9b8b2017-06-22 16:51:17 -0700525func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700526 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700527
Colin Crossebe1a512017-11-14 13:12:14 -0800528 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
529 aidlIncludes = append(aidlIncludes,
530 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
531 aidlIncludes = append(aidlIncludes,
532 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700533
Steven Moreland667f6882018-07-26 12:55:08 -0700534 flags := []string{"-b"}
535
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700536 if aidlPreprocess.Valid() {
537 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700538 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700539 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700540 }
541
Colin Cross635c3b02016-05-18 15:37:25 -0700542 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800543 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700544 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800545 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700546 flags = append(flags, "-I"+src.String())
547 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700548
Martijn Coeneneab15642018-03-09 09:29:59 +0100549 if Bool(j.deviceProperties.Aidl.Generate_traces) {
550 flags = append(flags, "-t")
551 }
552
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100553 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
554 flags = append(flags, "--transaction_names")
555 }
556
Colin Crossf03c82b2015-04-13 13:53:40 -0700557 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700558}
559
Colin Cross32f676a2017-09-06 13:41:06 -0700560type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800561 classpath classpath
562 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700563 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800564 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700565 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700566 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700567 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700568 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800569 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700570 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700571 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700572 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700573 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800574 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800575
576 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700577}
Colin Cross2fe66872015-03-30 17:20:39 -0700578
Colin Cross54250902017-12-05 09:28:08 -0800579func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
580 for _, f := range dep.Srcs() {
581 if f.Ext() != ".jar" {
582 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
583 ctx.OtherModuleName(dep.(blueprint.Module)))
584 }
585 }
586}
587
Jiyong Park2d492942018-03-05 17:44:10 +0900588type linkType int
589
590const (
591 javaCore linkType = iota
592 javaSdk
593 javaSystem
594 javaPlatform
595)
596
Jiyong Park46f78fb2018-10-20 16:33:17 +0900597func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700598 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700599 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900600 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
601 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
602 name == "core-lambda-stubs":
603 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100604 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900605 return javaCore, false
606 case name == "android_system_stubs_current":
607 return javaSystem, true
608 case strings.HasPrefix(ver, "system_"):
609 return javaSystem, false
610 case name == "android_test_stubs_current":
611 return javaSystem, true
612 case strings.HasPrefix(ver, "test_"):
613 return javaPlatform, false
614 case name == "android_stubs_current":
615 return javaSdk, true
616 case ver == "current":
617 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700618 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900619 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700620 default:
621 if _, err := strconv.Atoi(ver); err != nil {
622 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
623 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900624 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900625 }
626}
627
Jiyong Park750e5572018-01-31 00:20:13 +0900628func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700629 if ctx.Host() {
630 return
631 }
632
Jiyong Park46f78fb2018-10-20 16:33:17 +0900633 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
634 if stubs {
635 return
636 }
637 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900638 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."
639
640 switch myLinkType {
641 case javaCore:
642 if otherLinkType != javaCore {
643 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 +0900644 ctx.OtherModuleName(to))
645 }
Jiyong Park2d492942018-03-05 17:44:10 +0900646 break
647 case javaSdk:
648 if otherLinkType != javaCore && otherLinkType != javaSdk {
649 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
650 ctx.OtherModuleName(to))
651 }
652 break
653 case javaSystem:
654 if otherLinkType == javaPlatform {
655 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
656 ctx.OtherModuleName(to))
657 }
658 break
659 case javaPlatform:
660 // no restriction on link-type
661 break
Jiyong Park750e5572018-01-31 00:20:13 +0900662 }
663}
664
Colin Cross32f676a2017-09-06 13:41:06 -0700665func (j *Module) collectDeps(ctx android.ModuleContext) deps {
666 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700667
Colin Cross300f0382018-03-06 13:11:51 -0800668 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700669 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800670 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700671 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800672 } else if sdkDep.useFiles {
673 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700674 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800675 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
676 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700677 }
678
Colin Crossd11fcda2017-10-23 17:59:01 -0700679 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700680 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700681 tag := ctx.OtherModuleDependencyTag(module)
682
Colin Crossa4f08812018-10-02 22:03:40 -0700683 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700684 // Handled by AndroidApp.collectAppDeps
685 return
686 }
687 if tag == certificateTag {
688 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700689 return
690 }
691
Jiyong Park750e5572018-01-31 00:20:13 +0900692 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700693 switch tag {
694 case bootClasspathTag, libTag, staticLibTag:
695 checkLinkType(ctx, j, to, tag.(dependencyTag))
696 }
Jiyong Park750e5572018-01-31 00:20:13 +0900697 }
Colin Cross54250902017-12-05 09:28:08 -0800698 switch dep := module.(type) {
699 case Dependency:
700 switch tag {
701 case bootClasspathTag:
702 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700703 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800704 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900705 // sdk lib names from dependencies are re-exported
706 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800707 case staticLibTag:
708 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
709 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
710 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700711 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900712 // sdk lib names from dependencies are re-exported
713 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800714 case pluginTag:
715 if plugin, ok := dep.(*Plugin); ok {
716 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
717 if plugin.pluginProperties.Processor_class != nil {
718 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
719 }
720 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
721 } else {
722 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
723 }
Colin Cross54250902017-12-05 09:28:08 -0800724 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100725 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800726 // framework.jar has a one-off dependency on the R.java and Manifest.java files
727 // generated by framework-res.apk
728 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
729 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800730 case frameworkApkTag:
731 if ctx.ModuleName() == "android_stubs_current" ||
732 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700733 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800734 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
735 // resource files out of there for aapt.
736 //
737 // Normally the package rule runs aapt, which includes the resource,
738 // but we're not running that in our package rule so just copy in the
739 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700740 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800741 }
Colin Cross54250902017-12-05 09:28:08 -0800742 case kotlinStdlibTag:
743 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800744 case kotlinAnnotationsTag:
745 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800746 }
747
748 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900749 case SdkLibraryDependency:
750 switch tag {
751 case libTag:
Sundong Ahn054b19a2018-10-19 13:46:09 +0900752 deps.classpath = append(deps.classpath, dep.HeaderJars(ctx, j.sdkVersion())...)
Jiyong Park1be96912018-05-28 18:02:19 +0900753 // names of sdk libs that are directly depended are exported
754 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900755 default:
756 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
757 }
Colin Cross54250902017-12-05 09:28:08 -0800758 case android.SourceFileProducer:
759 switch tag {
760 case libTag:
761 checkProducesJars(ctx, dep)
762 deps.classpath = append(deps.classpath, dep.Srcs()...)
763 case staticLibTag:
764 checkProducesJars(ctx, dep)
765 deps.classpath = append(deps.classpath, dep.Srcs()...)
766 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
767 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
768 case android.DefaultsDepTag, android.SourceDepTag:
769 // Nothing to do
Sundong Ahn054b19a2018-10-19 13:46:09 +0900770 case publicApiFileTag, systemApiFileTag, testApiFileTag:
771 // Nothing to do
Colin Cross54250902017-12-05 09:28:08 -0800772 default:
773 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
774 }
775 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700776 switch tag {
777 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700778 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700779 case systemModulesTag:
780 if deps.systemModules != nil {
781 panic("Found two system module dependencies")
782 }
783 sm := module.(*SystemModules)
784 if sm.outputFile == nil {
785 panic("Missing directory for system module dependency")
786 }
787 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700788 default:
789 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700790 }
Colin Crossec7a0422017-07-07 14:47:12 -0700791 }
Colin Cross2fe66872015-03-30 17:20:39 -0700792 })
793
Jiyong Park1be96912018-05-28 18:02:19 +0900794 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
795
Colin Cross32f676a2017-09-06 13:41:06 -0700796 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700797}
798
Colin Cross83bb3162018-06-25 15:48:06 -0700799func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700800 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800801 v := sdkContext.sdkVersion()
802 // For PDK builds, use the latest SDK version instead of "current"
803 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
804 sdkVersions := ctx.Config().Get(sdkSingletonKey).([]int)
805 latestSdkVersion := 0
806 if len(sdkVersions) > 0 {
807 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
808 }
809 v = strconv.Itoa(latestSdkVersion)
810 }
811
812 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700813 if err != nil {
814 ctx.PropertyErrorf("sdk_version", "%s", err)
815 }
Nan Zhang357466b2018-04-17 17:38:36 -0700816 if javaVersion != "" {
817 ret = javaVersion
818 } else if ctx.Device() && sdk <= 23 {
819 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900820 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700821 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700822 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700823 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
824 ret = "1.8"
825 } else {
826 ret = "1.9"
827 }
828
829 return ret
830}
831
Nan Zhanged19fc32017-10-19 13:06:22 -0700832func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700833
Colin Crossf03c82b2015-04-13 13:53:40 -0700834 var flags javaBuilderFlags
835
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100836 // javaVersion flag.
837 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
838
Nan Zhanged19fc32017-10-19 13:06:22 -0700839 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700840 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100841 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700842 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700843 }
Colin Cross6510f912017-11-29 00:27:14 -0800844 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700845 // Override the -g flag passed globally to remove local variable debug info to reduce
846 // disk and memory usage.
847 javacFlags = append(javacFlags, "-g:source,lines")
848 }
Colin Cross64162712017-08-08 13:17:59 -0700849
Colin Cross66548102018-06-19 22:47:35 -0700850 if ctx.Config().RunErrorProne() {
851 if config.ErrorProneClasspath == nil {
852 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
853 }
854
855 errorProneFlags := []string{
856 "-Xplugin:ErrorProne",
857 "${config.ErrorProneChecks}",
858 }
859 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
860
861 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
862 "'" + strings.Join(errorProneFlags, " ") + "'"
863 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800864 }
865
Nan Zhanged19fc32017-10-19 13:06:22 -0700866 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800867 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
868 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700869 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800870
Colin Crossbe9cdb82019-01-21 21:37:16 -0800871 flags.processor = strings.Join(deps.processorClasses, ",")
872
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100873 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800874 !Bool(j.properties.No_standard_libs) &&
875 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
876 // Give host-side tools a version of OpenJDK's standard libraries
877 // close to what they're targeting. As of Dec 2017, AOSP is only
878 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
879 //
880 // When building with OpenJDK 8, the following should have no
881 // effect since those jars would be available by default.
882 //
883 // When building with OpenJDK 9 but targeting a version < 1.8,
884 // putting them on the bootclasspath means that:
885 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
886 // b) references to existing APIs are not reinterpreted in an
887 // OpenJDK 9-specific way, eg. calls to subclasses of
888 // java.nio.Buffer as in http://b/70862583
889 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
890 flags.bootClasspath = append(flags.bootClasspath,
891 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
892 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800893 if Bool(j.properties.Use_tools_jar) {
894 flags.bootClasspath = append(flags.bootClasspath,
895 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
896 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800897 }
898
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100899 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800900 // Manually specify build directory in case it is not under the repo root.
901 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
902 // just adding a symlink under the root doesn't help.)
903 patchPaths := ".:" + ctx.Config().BuildDir()
904 classPath := flags.classpath.FormJavaClassPath("")
905 if classPath != "" {
906 patchPaths += ":" + classPath
907 }
908 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700909 }
910
Nan Zhanged19fc32017-10-19 13:06:22 -0700911 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700912 if deps.systemModules != nil {
913 flags.systemModules = append(flags.systemModules, deps.systemModules)
914 }
915
Nan Zhanged19fc32017-10-19 13:06:22 -0700916 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -0700917 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -0700918 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -0700919 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -0700920 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
921 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -0700922 }
923
Colin Cross81440082018-08-15 20:21:55 -0700924 if len(javacFlags) > 0 {
925 // optimization.
926 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
927 flags.javacFlags = "$javacFlags"
928 }
929
Nan Zhanged19fc32017-10-19 13:06:22 -0700930 return flags
931}
Colin Crossc0b06f12015-04-08 13:03:43 -0700932
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800933func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700934
Colin Crossebe1a512017-11-14 13:12:14 -0800935 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700936
937 deps := j.collectDeps(ctx)
938 flags := j.collectBuilderFlags(ctx, deps)
939
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100940 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700941 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
942 }
943 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700944 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800945 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700946 }
947
Colin Crossaf050172017-11-15 23:01:59 -0800948 srcFiles = j.genSources(ctx, srcFiles, flags)
949
950 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700951 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800952 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700953
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700954 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
955 // that IDEInfo struct will use
956 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
957
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800958 if j.properties.Jarjar_rules != nil {
959 j.expandJarjarRules = ctx.ExpandSource(*j.properties.Jarjar_rules, "jarjar_rules")
960 }
961
Colin Cross1ee23172017-10-18 14:44:18 -0700962 jarName := ctx.ModuleName() + ".jar"
963
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000964 javaSrcFiles := srcFiles.FilterByExt(".java")
965 var uniqueSrcFiles android.Paths
966 set := make(map[string]bool)
967 for _, v := range javaSrcFiles {
968 if _, found := set[v.String()]; !found {
969 set[v.String()] = true
970 uniqueSrcFiles = append(uniqueSrcFiles, v)
971 }
972 }
973
Colin Cross55f63ea2018-08-27 12:37:09 -0700974 var kotlinJars android.Paths
975
Colin Cross93e85952017-08-15 13:34:18 -0700976 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200977 // user defined kotlin flags.
978 kotlincFlags := j.properties.Kotlincflags
979 CheckKotlincFlags(ctx, kotlincFlags)
980
Colin Cross93e85952017-08-15 13:34:18 -0700981 // If there are kotlin files, compile them first but pass all the kotlin and java files
982 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
983 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200984 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700985 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200986 kotlincFlags = append(kotlincFlags, "-no-jdk")
987 }
988 if len(kotlincFlags) > 0 {
989 // optimization.
990 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
991 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -0700992 }
993
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000994 var kotlinSrcFiles android.Paths
995 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
996 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
997
Colin Crossafbb1732019-01-17 15:42:52 -0800998 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
999 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1000
1001 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1002 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1003
1004 if len(flags.processorPath) > 0 {
1005 // Use kapt for annotation processing
1006 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1007 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1008 srcJars = append(srcJars, kaptSrcJar)
1009 // Disable annotation processing in javac, it's already been handled by kapt
1010 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001011 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001012 }
Colin Cross93e85952017-08-15 13:34:18 -07001013
Colin Cross1ee23172017-10-18 14:44:18 -07001014 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001015 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001016 if ctx.Failed() {
1017 return
1018 }
1019
1020 // Make javac rule depend on the kotlinc rule
1021 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001022
Colin Cross93e85952017-08-15 13:34:18 -07001023 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001024 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001025 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001026 }
1027
Colin Cross55f63ea2018-08-27 12:37:09 -07001028 jars := append(android.Paths(nil), kotlinJars...)
1029
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001030 // Store the list of .java files that was passed to javac
1031 j.compiledJavaSrcs = uniqueSrcFiles
1032 j.compiledSrcJars = srcJars
1033
Nan Zhang61eaedb2017-11-02 13:28:15 -07001034 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001035 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001036 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1037 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001038 // Formerly, there was a check here that prevented annotation processors
1039 // from being used when sharding was enabled, as some annotation processors
1040 // do not function correctly in sharded environments. It was removed to
1041 // allow for the use of annotation processors that do function correctly
1042 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001043 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001044 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001045 if ctx.Failed() {
1046 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001047 }
1048 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001049 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001050 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001051 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001052 // If error-prone is enabled, add an additional rule to compile the java files into
1053 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001054 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001055 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1056 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001057 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001058 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001059 extraJarDeps = append(extraJarDeps, errorprone)
1060 }
1061
Nan Zhang61eaedb2017-11-02 13:28:15 -07001062 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001063 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001064 shardSize := int(*(j.properties.Javac_shard_size))
1065 var shardSrcs []android.Paths
1066 if len(uniqueSrcFiles) > 0 {
1067 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1068 for idx, shardSrc := range shardSrcs {
1069 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1070 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1071 jars = append(jars, classes)
1072 }
1073 }
1074 if len(srcJars) > 0 {
1075 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1076 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1077 jars = append(jars, classes)
1078 }
1079 } else {
1080 classes := android.PathForModuleOut(ctx, "javac", jarName)
1081 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1082 jars = append(jars, classes)
1083 }
Colin Crossd6891432017-09-27 17:39:56 -07001084 if ctx.Failed() {
1085 return
1086 }
Colin Cross2fe66872015-03-30 17:20:39 -07001087 }
1088
Colin Crosscedd4762018-09-13 11:26:19 -07001089 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1090 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001091 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1092
1093 var resArgs []string
1094 var resDeps android.Paths
1095
1096 resArgs = append(resArgs, dirArgs...)
1097 resDeps = append(resDeps, dirDeps...)
1098
1099 resArgs = append(resArgs, fileArgs...)
1100 resDeps = append(resDeps, fileDeps...)
1101
Colin Crossff3ae9d2018-04-10 16:15:18 -07001102 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001103 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001104 resArgs = append(resArgs, srcArgs...)
1105 resDeps = append(resDeps, srcDeps...)
1106 }
Colin Cross40a36712017-09-27 17:41:35 -07001107
1108 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001109 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001110 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001111 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001112 if ctx.Failed() {
1113 return
1114 }
1115 }
1116
Colin Cross331a1212018-08-15 20:40:52 -07001117 if len(deps.staticResourceJars) > 0 {
1118 var jars android.Paths
1119 if j.resourceJar != nil {
1120 jars = append(jars, j.resourceJar)
1121 }
1122 jars = append(jars, deps.staticResourceJars...)
1123
1124 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1125 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1126 false, nil, nil)
1127 j.resourceJar = combinedJar
1128 }
1129
Colin Cross32f676a2017-09-06 13:41:06 -07001130 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001131 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001132
Colin Cross094054a2018-10-17 15:10:48 -07001133 manifest := j.overrideManifest
1134 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001135 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1136 }
Colin Cross635acc92017-09-12 22:50:46 -07001137
Colin Cross0a6e0072017-08-30 14:24:55 -07001138 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001139 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001140 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001141
1142 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001143 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1144 // Optimization: skip the combine step if there is nothing to do
1145 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1146 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1147 // any if len(jars) == 1.
1148 outputFile = moduleOutPath
1149 } else {
1150 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1151 ctx.Build(pctx, android.BuildParams{
1152 Rule: android.Cp,
1153 Input: jars[0],
1154 Output: combinedJar,
1155 })
1156 outputFile = combinedJar
1157 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001158 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001159 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001160 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001161 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001162 outputFile = combinedJar
1163 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001164
Colin Cross331a1212018-08-15 20:40:52 -07001165 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001166 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001167 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001168 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001169 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001170 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001171
1172 // jarjar resource jar if necessary
1173 if j.resourceJar != nil {
1174 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001175 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001176 j.resourceJar = resourceJarJarFile
1177 }
1178
Colin Cross0a6e0072017-08-30 14:24:55 -07001179 if ctx.Failed() {
1180 return
1181 }
1182 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001183 j.implementationJarFile = outputFile
1184 if j.headerJarFile == nil {
1185 j.headerJarFile = j.implementationJarFile
1186 }
Colin Cross2fe66872015-03-30 17:20:39 -07001187
Colin Cross6510f912017-11-29 00:27:14 -08001188 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001189 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1190 j.properties.Instrument = true
1191 }
1192 }
1193
Colin Cross3144dfc2018-01-03 15:06:47 -08001194 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001195 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1196 }
1197
Colin Cross331a1212018-08-15 20:40:52 -07001198 // merge implementation jar with resources if necessary
1199 implementationAndResourcesJar := outputFile
1200 if j.resourceJar != nil {
1201 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1202 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1203 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1204 false, nil, nil)
1205 implementationAndResourcesJar = combinedJar
1206 }
1207
1208 j.implementationAndResourcesJar = implementationAndResourcesJar
1209
Colin Cross9ae1b922018-06-26 17:59:05 -07001210 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001211 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001212 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001213 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001214 if ctx.Failed() {
1215 return
1216 }
Colin Cross331a1212018-08-15 20:40:52 -07001217
Colin Cross8faf8fc2019-01-16 15:15:52 -08001218 // Hidden API CSV generation and dex encoding
David Brazdil9fc36a62019-01-18 11:12:05 +00001219 if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
1220 isBootJar := inList(ctx.ModuleName(), ctx.Config().BootJars())
1221 if isBootJar || inList(ctx.ModuleName(), ctx.Config().HiddenAPIExtraAppUsageJars()) {
1222 // Derive the greylist from classes jar.
1223 hiddenAPIGenerateCSV(ctx, j.implementationJarFile)
1224 }
1225 if isBootJar {
1226 hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", jarName)
Colin Crosscd964b32019-01-18 22:03:02 -08001227 hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexOutputFile, j.deviceProperties.UncompressDex)
David Brazdil9fc36a62019-01-18 11:12:05 +00001228 dexOutputFile = hiddenAPIJar
1229 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001230 }
1231
Colin Cross331a1212018-08-15 20:40:52 -07001232 // merge dex jar with resources if necessary
1233 if j.resourceJar != nil {
1234 jars := android.Paths{dexOutputFile, j.resourceJar}
1235 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1236 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1237 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001238 if j.deviceProperties.UncompressDex {
1239 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1240 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1241 dexOutputFile = combinedAlignedJar
1242 } else {
1243 dexOutputFile = combinedJar
1244 }
Colin Cross331a1212018-08-15 20:40:52 -07001245 }
1246
1247 j.dexJarFile = dexOutputFile
1248
Colin Cross8faf8fc2019-01-16 15:15:52 -08001249 // Dexpreopting
Colin Crossdc2da912019-01-05 22:13:05 -08001250 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross2fc72f62018-12-21 12:59:54 -08001251 j.dexpreopter.uncompressedDex = j.deviceProperties.UncompressDex
Colin Cross43f08db2018-11-12 10:13:39 -08001252 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1253
1254 j.maybeStrippedDexJarFile = dexOutputFile
1255
Colin Cross3063b782018-08-15 11:19:12 -07001256 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001257
1258 if ctx.Failed() {
1259 return
1260 }
Colin Cross331a1212018-08-15 20:40:52 -07001261 } else {
1262 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001263 }
Colin Cross331a1212018-08-15 20:40:52 -07001264
Colin Crossb7a63242015-04-16 14:09:14 -07001265 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001266
1267 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1268 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001269}
1270
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001271// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1272// since some of these flags may be used internally.
1273func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1274 for _, flag := range flags {
1275 flag = strings.TrimSpace(flag)
1276
1277 if !strings.HasPrefix(flag, "-") {
1278 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1279 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1280 ctx.PropertyErrorf("kotlincflags",
1281 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1282 } else if inList(flag, config.KotlincIllegalFlags) {
1283 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1284 } else if flag == "-include-runtime" {
1285 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1286 } else {
1287 args := strings.Split(flag, " ")
1288 if args[0] == "-kotlin-home" {
1289 ctx.PropertyErrorf("kotlincflags",
1290 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1291 }
1292 }
1293 }
1294}
1295
Colin Cross8eadbf02017-10-24 17:46:00 -07001296func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001297 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001298
1299 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001300 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001301 // Compile java sources into turbine.jar.
1302 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1303 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1304 if ctx.Failed() {
1305 return nil
1306 }
1307 jars = append(jars, turbineJar)
1308 }
1309
Colin Cross55f63ea2018-08-27 12:37:09 -07001310 jars = append(jars, extraJars...)
1311
Nan Zhanged19fc32017-10-19 13:06:22 -07001312 // Combine any static header libraries into classes-header.jar. If there is only
1313 // one input jar this step will be skipped.
1314 var headerJar android.Path
1315 jars = append(jars, deps.staticHeaderJars...)
1316
Colin Cross5c6ecc12017-10-23 18:12:27 -07001317 // we cannot skip the combine step for now if there is only one jar
1318 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1319 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001320 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1321 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001322 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001323
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001324 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001325 // Transform classes.jar into classes-jarjar.jar
1326 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001327 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001328 headerJar = jarjarFile
1329 if ctx.Failed() {
1330 return nil
1331 }
1332 }
1333
1334 return headerJar
1335}
1336
Colin Crosscb933592017-11-22 13:49:43 -08001337func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001338 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001339
Colin Cross7a3139e2017-12-19 13:57:50 -08001340 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001341
Colin Cross84c38822018-01-03 15:59:46 -08001342 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001343 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1344
1345 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1346
1347 j.jacocoReportClassesFile = jacocoReportClassesFile
1348
1349 return instrumentedJar
1350}
1351
albaltai36ff7dc2018-12-25 14:35:23 +08001352var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001353
Nan Zhanged19fc32017-10-19 13:06:22 -07001354func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001355 if j.headerJarFile == nil {
1356 return nil
1357 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001358 return android.Paths{j.headerJarFile}
1359}
1360
1361func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001362 if j.implementationJarFile == nil {
1363 return nil
1364 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001365 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001366}
1367
Colin Cross331a1212018-08-15 20:40:52 -07001368func (j *Module) ResourceJars() android.Paths {
1369 if j.resourceJar == nil {
1370 return nil
1371 }
1372 return android.Paths{j.resourceJar}
1373}
1374
1375func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001376 if j.implementationAndResourcesJar == nil {
1377 return nil
1378 }
Colin Cross331a1212018-08-15 20:40:52 -07001379 return android.Paths{j.implementationAndResourcesJar}
1380}
1381
Colin Cross46c9b8b2017-06-22 16:51:17 -07001382func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001383 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001384 return j.exportAidlIncludeDirs
1385}
1386
Jiyong Park1be96912018-05-28 18:02:19 +09001387func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001388 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001389 return j.exportedSdkLibs
1390}
1391
Colin Cross46c9b8b2017-06-22 16:51:17 -07001392var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001393
Colin Cross46c9b8b2017-06-22 16:51:17 -07001394func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001395 return j.logtagsSrcs
1396}
1397
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001398// Collect information for opening IDE project files in java/jdeps.go.
1399func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1400 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1401 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1402 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001403 if j.expandJarjarRules != nil {
1404 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001405 }
1406}
1407
1408func (j *Module) CompilerDeps() []string {
1409 jdeps := []string{}
1410 jdeps = append(jdeps, j.properties.Libs...)
1411 jdeps = append(jdeps, j.properties.Static_libs...)
1412 return jdeps
1413}
1414
Colin Cross2fe66872015-03-30 17:20:39 -07001415//
1416// Java libraries (.jar file)
1417//
1418
Colin Crossf506d872017-07-19 15:53:04 -07001419type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001420 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001421}
1422
Colin Cross2fc72f62018-12-21 12:59:54 -08001423func (j *Library) shouldUncompressDex(ctx android.ModuleContext) bool {
Nicolas Geoffray5921f752019-01-24 17:52:04 +00001424 // Store uncompressed (and do not strip) dex files from boot class path jars that are
1425 // in an apex.
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001426 if inList(ctx.ModuleName(), ctx.Config().BootJars()) &&
Nicolas Geoffray5921f752019-01-24 17:52:04 +00001427 android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001428 return true
1429 }
Colin Cross2fc72f62018-12-21 12:59:54 -08001430 return false
1431}
1432
Colin Crossf506d872017-07-19 15:53:04 -07001433func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001434 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1435 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Colin Cross2fc72f62018-12-21 12:59:54 -08001436 j.deviceProperties.UncompressDex = j.shouldUncompressDex(ctx)
Colin Cross46c9b8b2017-06-22 16:51:17 -07001437 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001438
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001439 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001440 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1441 ctx.ModuleName()+".jar", j.outputFile)
1442 }
Colin Crossb7a63242015-04-16 14:09:14 -07001443}
1444
Colin Crossf506d872017-07-19 15:53:04 -07001445func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001446 j.deps(ctx)
1447}
1448
Colin Cross9ae1b922018-06-26 17:59:05 -07001449func LibraryFactory() android.Module {
1450 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001451
Colin Cross9ae1b922018-06-26 17:59:05 -07001452 module.AddProperties(
1453 &module.Module.properties,
1454 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001455 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001456 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001457
Colin Cross9ae1b922018-06-26 17:59:05 -07001458 InitJavaModule(module, android.HostAndDeviceSupported)
1459 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001460}
1461
Colin Crossf506d872017-07-19 15:53:04 -07001462func LibraryHostFactory() android.Module {
1463 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001464
Colin Cross6af17aa2017-09-20 12:59:05 -07001465 module.AddProperties(
1466 &module.Module.properties,
1467 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001468
Colin Cross9ae1b922018-06-26 17:59:05 -07001469 module.Module.properties.Installable = proptools.BoolPtr(true)
1470
Colin Cross89536d42017-07-07 14:35:50 -07001471 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001472 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001473}
1474
1475//
Colin Crossb628ea52018-08-14 16:42:33 -07001476// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001477//
1478
1479type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001480 // list of compatibility suites (for example "cts", "vts") that the module should be
1481 // installed into.
1482 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001483
1484 // the name of the test configuration (for example "AndroidTest.xml") that should be
1485 // installed with the module.
1486 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001487
Jack He33338892018-09-19 02:21:28 -07001488 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1489 // should be installed with the module.
1490 Test_config_template *string `android:"arch_variant"`
1491
Colin Crossd96ca352018-08-10 16:06:24 -07001492 // list of files or filegroup modules that provide data that should be installed alongside
1493 // the test
1494 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001495}
1496
1497type Test struct {
1498 Library
1499
1500 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001501
1502 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001503 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001504}
1505
1506func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001507 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001508 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001509
1510 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001511}
1512
1513func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1514 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001515 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001516 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001517 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001518}
1519
1520func TestFactory() android.Module {
1521 module := &Test{}
1522
1523 module.AddProperties(
1524 &module.Module.properties,
1525 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001526 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001527 &module.Module.protoProperties,
1528 &module.testProperties)
1529
Colin Cross9ae1b922018-06-26 17:59:05 -07001530 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001531 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001532
Colin Cross05638fc2018-04-09 18:40:24 -07001533 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001534 return module
1535}
1536
1537func TestHostFactory() android.Module {
1538 module := &Test{}
1539
1540 module.AddProperties(
1541 &module.Module.properties,
1542 &module.Module.protoProperties,
1543 &module.testProperties)
1544
Colin Cross9ae1b922018-06-26 17:59:05 -07001545 module.Module.properties.Installable = proptools.BoolPtr(true)
1546
Colin Cross05638fc2018-04-09 18:40:24 -07001547 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001548 return module
1549}
1550
1551//
Colin Cross2fe66872015-03-30 17:20:39 -07001552// Java Binaries (.jar file plus wrapper script)
1553//
1554
Colin Crossf506d872017-07-19 15:53:04 -07001555type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001556 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001557 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001558
1559 // Name of the class containing main to be inserted into the manifest as Main-Class.
1560 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001561}
1562
Colin Crossf506d872017-07-19 15:53:04 -07001563type Binary struct {
1564 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001565
Colin Crossf506d872017-07-19 15:53:04 -07001566 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001567
Colin Cross6b4a32d2017-12-05 13:42:45 -08001568 isWrapperVariant bool
1569
Colin Crossc3315992017-12-08 19:12:36 -08001570 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001571 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001572}
1573
Alex Light24237172017-10-26 09:46:21 -07001574func (j *Binary) HostToolPath() android.OptionalPath {
1575 return android.OptionalPathForPath(j.binaryFile)
1576}
1577
Colin Crossf506d872017-07-19 15:53:04 -07001578func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001579 if ctx.Arch().ArchType == android.Common {
1580 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001581 if j.binaryProperties.Main_class != nil {
1582 if j.properties.Manifest != nil {
1583 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1584 }
1585 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1586 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1587 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1588 }
1589
Colin Cross6b4a32d2017-12-05 13:42:45 -08001590 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001591 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001592 // Handle the binary wrapper
1593 j.isWrapperVariant = true
1594
Colin Cross366938f2017-12-11 16:29:02 -08001595 if j.binaryProperties.Wrapper != nil {
1596 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001597 } else {
1598 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1599 }
1600
1601 // Depend on the installed jar so that the wrapper doesn't get executed by
1602 // another build rule before the jar has been installed.
1603 jarFile := ctx.PrimaryModule().(*Binary).installFile
1604
1605 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1606 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001607 }
Colin Cross2fe66872015-03-30 17:20:39 -07001608}
1609
Colin Crossf506d872017-07-19 15:53:04 -07001610func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001611 if ctx.Arch().ArchType == android.Common {
1612 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001613 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001614 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001615 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001616}
1617
Colin Crossf506d872017-07-19 15:53:04 -07001618func BinaryFactory() android.Module {
1619 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001620
Colin Cross36242852017-06-23 15:06:31 -07001621 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001622 &module.Module.properties,
1623 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001624 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001625 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001626 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001627
Colin Cross9ae1b922018-06-26 17:59:05 -07001628 module.Module.properties.Installable = proptools.BoolPtr(true)
1629
Colin Cross6b4a32d2017-12-05 13:42:45 -08001630 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1631 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001632 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001633}
1634
Colin Crossf506d872017-07-19 15:53:04 -07001635func BinaryHostFactory() android.Module {
1636 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001637
Colin Cross36242852017-06-23 15:06:31 -07001638 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001639 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001640 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001641 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001642
Colin Cross9ae1b922018-06-26 17:59:05 -07001643 module.Module.properties.Installable = proptools.BoolPtr(true)
1644
Colin Cross6b4a32d2017-12-05 13:42:45 -08001645 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1646 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001647 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001648}
1649
1650//
1651// Java prebuilts
1652//
1653
Colin Cross74d73e22017-08-02 11:05:49 -07001654type ImportProperties struct {
1655 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001656
Nan Zhangea568a42017-11-08 21:20:04 -08001657 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001658
1659 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001660
1661 // List of shared java libs that this module has dependencies to
1662 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001663
1664 // List of files to remove from the jar file(s)
1665 Exclude_files []string
1666
1667 // List of directories to remove from the jar file(s)
1668 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001669
1670 // if set to true, run Jetifier against .jar file. Defaults to false.
1671 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001672}
1673
1674type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001675 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001676 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001677 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001678
Colin Cross74d73e22017-08-02 11:05:49 -07001679 properties ImportProperties
1680
Colin Cross0a6e0072017-08-30 14:24:55 -07001681 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001682 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001683}
1684
Colin Cross83bb3162018-06-25 15:48:06 -07001685func (j *Import) sdkVersion() string {
1686 return String(j.properties.Sdk_version)
1687}
1688
1689func (j *Import) minSdkVersion() string {
1690 return j.sdkVersion()
1691}
1692
Colin Cross74d73e22017-08-02 11:05:49 -07001693func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001694 return &j.prebuilt
1695}
1696
Colin Cross74d73e22017-08-02 11:05:49 -07001697func (j *Import) PrebuiltSrcs() []string {
1698 return j.properties.Jars
1699}
1700
1701func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001702 return j.prebuilt.Name(j.ModuleBase.Name())
1703}
1704
Colin Cross74d73e22017-08-02 11:05:49 -07001705func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001706 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001707 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001708}
1709
Colin Cross74d73e22017-08-02 11:05:49 -07001710func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001711 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001712
Nan Zhang4c819fb2018-08-27 18:31:46 -07001713 jarName := ctx.ModuleName() + ".jar"
1714 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001715 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1716 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001717 if Bool(j.properties.Jetifier_enabled) {
1718 inputFile := outputFile
1719 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1720 TransformJetifier(ctx, outputFile, inputFile)
1721 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001722 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001723
1724 ctx.VisitDirectDeps(func(module android.Module) {
1725 otherName := ctx.OtherModuleName(module)
1726 tag := ctx.OtherModuleDependencyTag(module)
1727
1728 switch dep := module.(type) {
1729 case Dependency:
1730 switch tag {
1731 case libTag, staticLibTag:
1732 // sdk lib names from dependencies are re-exported
1733 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1734 }
1735 case SdkLibraryDependency:
1736 switch tag {
1737 case libTag:
1738 // names of sdk libs that are directly depended are exported
1739 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1740 }
1741 }
1742 })
1743
1744 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001745 if Bool(j.properties.Installable) {
1746 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1747 ctx.ModuleName()+".jar", outputFile)
1748 }
Colin Cross2fe66872015-03-30 17:20:39 -07001749}
1750
Colin Cross74d73e22017-08-02 11:05:49 -07001751var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001752
Nan Zhanged19fc32017-10-19 13:06:22 -07001753func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001754 if j.combinedClasspathFile == nil {
1755 return nil
1756 }
Colin Cross37f6d792018-07-12 12:28:41 -07001757 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001758}
1759
1760func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001761 if j.combinedClasspathFile == nil {
1762 return nil
1763 }
Colin Cross37f6d792018-07-12 12:28:41 -07001764 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001765}
1766
Colin Cross331a1212018-08-15 20:40:52 -07001767func (j *Import) ResourceJars() android.Paths {
1768 return nil
1769}
1770
1771func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001772 if j.combinedClasspathFile == nil {
1773 return nil
1774 }
Colin Cross331a1212018-08-15 20:40:52 -07001775 return android.Paths{j.combinedClasspathFile}
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