blob: 5ed99f7e8a7a4e34fcf4f308f85e373ce298393d [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 Cross32f676a2017-09-06 13:41:06 -0700117 // List of modules to use as annotation processors
118 Annotation_processors []string
119
120 // List of classes to pass to javac to use as annotation processors
121 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700122
Nan Zhang61eaedb2017-11-02 13:28:15 -0700123 // The number of Java source entries each Javac instance can process
124 Javac_shard_size *int64
125
Nan Zhang5f8cb422018-02-06 10:34:32 -0800126 // Add host jdk tools.jar to bootclasspath
127 Use_tools_jar *bool
128
Colin Cross1369cdb2017-09-29 17:58:17 -0700129 Openjdk9 struct {
130 // List of source files that should only be used when passing -source 1.9
131 Srcs []string
132
133 // List of javac flags that should only be used when passing -source 1.9
134 Javacflags []string
135 }
Colin Crosscb933592017-11-22 13:49:43 -0800136
Colin Cross81440082018-08-15 20:21:55 -0700137 // When compiling language level 9+ .java code in packages that are part of
138 // a system module, patch_module names the module that your sources and
139 // dependencies should be patched into. The Android runtime currently
140 // doesn't implement the JEP 261 module system so this option is only
141 // supported at compile time. It should only be needed to compile tests in
142 // packages that exist in libcore and which are inconvenient to move
143 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100144 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700145
Colin Crosscb933592017-11-22 13:49:43 -0800146 Jacoco struct {
147 // List of classes to include for instrumentation with jacoco to collect coverage
148 // information at runtime when building with coverage enabled. If unset defaults to all
149 // classes.
150 // Supports '*' as the last character of an entry in the list as a wildcard match.
151 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
152 // it matches classes in the package that have the class name as a prefix.
153 Include_filter []string
154
155 // List of classes to exclude from instrumentation with jacoco to collect coverage
156 // information at runtime when building with coverage enabled. Overrides classes selected
157 // by the include_filter property.
158 // Supports '*' as the last character of an entry in the list as a wildcard match.
159 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
160 // it matches classes in the package that have the class name as a prefix.
161 Exclude_filter []string
162 }
163
Andreas Gampef3e5b552018-01-22 21:27:21 -0800164 Errorprone struct {
165 // List of javac flags that should only be used when running errorprone.
166 Javacflags []string
167 }
168
Colin Cross0f2ee152017-12-14 15:22:43 -0800169 Proto struct {
170 // List of extra options that will be passed to the proto generator.
171 Output_params []string
172 }
173
Colin Crosscb933592017-11-22 13:49:43 -0800174 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700175}
176
Colin Cross89536d42017-07-07 14:35:50 -0700177type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700178 // list of module-specific flags that will be used for dex compiles
179 Dxflags []string `android:"arch_variant"`
180
Colin Cross83bb3162018-06-25 15:48:06 -0700181 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
182 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800183 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700184
Colin Cross83bb3162018-06-25 15:48:06 -0700185 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
186 // Defaults to sdk_version if not set.
187 Min_sdk_version *string
188
Dan Willemsen419290a2018-10-31 15:28:47 -0700189 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
190 // Defaults to sdk_version if not set.
191 Target_sdk_version *string
192
Colin Cross6af2e492018-05-22 11:12:33 -0700193 // if true, compile against the platform APIs instead of an SDK.
194 Platform_apis *bool
195
Colin Crossebe1a512017-11-14 13:12:14 -0800196 Aidl struct {
197 // Top level directories to pass to aidl tool
198 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700199
Colin Crossebe1a512017-11-14 13:12:14 -0800200 // Directories rooted at the Android.bp file to pass to aidl tool
201 Local_include_dirs []string
202
203 // directories that should be added as include directories for any aidl sources of modules
204 // that depend on this module, as well as to aidl for this module.
205 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100206
207 // whether to generate traces (for systrace) for this interface
208 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100209
210 // whether to generate Binder#GetTransaction name method.
211 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800212 }
Colin Cross92430102017-10-09 14:59:32 -0700213
214 // If true, export a copy of the module as a -hostdex module for host testing.
215 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700216
David Brazdil17ef5632018-06-27 10:27:45 +0100217 // If set to true, compile dex regardless of installable. Defaults to false.
218 Compile_dex *bool
219
Colin Cross4ff85eb2018-12-14 11:47:19 -0800220 Dex_preopt struct {
221 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
222 // true.
223 Enabled *bool
224
225 // If true, generate an app image (.art file) for this module.
226 App_image *bool
227
228 // If true, use a checked-in profile to guide optimization. Defaults to false unless
229 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
230 // that matches the name of this module, in which case it is defaulted to true.
231 Profile_guided *bool
232
233 // If set, provides the path to profile relative to the Android.bp file. If not set,
234 // defaults to searching for a file that matches the name of this module in the default
235 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
236 Profile *string
237 }
238
Colin Cross66dbc0b2017-12-28 12:23:20 -0800239 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700240 // If false, disable all optimization. Defaults to true for android_app and android_test
241 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800242 Enabled *bool
243
244 // If true, optimize for size by removing unused code. Defaults to true for apps,
245 // false for libraries and tests.
246 Shrink *bool
247
248 // If true, optimize bytecode. Defaults to false.
249 Optimize *bool
250
251 // If true, obfuscate bytecode. Defaults to false.
252 Obfuscate *bool
253
254 // If true, do not use the flag files generated by aapt that automatically keep
255 // classes referenced by the app manifest. Defaults to false.
256 No_aapt_flags *bool
257
258 // Flags to pass to proguard.
259 Proguard_flags []string
260
261 // Specifies the locations of files containing proguard flags.
262 Proguard_flags_files []string
263 }
264
Colin Cross1369cdb2017-09-29 17:58:17 -0700265 // When targeting 1.9, override the modules to use with --system
266 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700267
268 UncompressDex bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700269}
270
Colin Cross46c9b8b2017-06-22 16:51:17 -0700271// Module contains the properties and members used by all java module types
272type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700273 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700274 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700275
Colin Cross89536d42017-07-07 14:35:50 -0700276 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700277 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700278 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700279
Colin Cross331a1212018-08-15 20:40:52 -0700280 // jar file containing header classes including static library dependencies, suitable for
281 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700282 headerJarFile android.Path
283
Colin Cross331a1212018-08-15 20:40:52 -0700284 // jar file containing implementation classes including static library dependencies but no
285 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700286 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700287
Colin Cross331a1212018-08-15 20:40:52 -0700288 // jar file containing only resources including from static library dependencies
289 resourceJar android.Path
290
291 // jar file containing implementation classes and resources including static library
292 // dependencies
293 implementationAndResourcesJar android.Path
294
295 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700296 dexJarFile android.Path
297
Colin Crosscb933592017-11-22 13:49:43 -0800298 // output file containing uninstrumented classes that will be instrumented by jacoco
299 jacocoReportClassesFile android.Path
300
Colin Cross66dbc0b2017-12-28 12:23:20 -0800301 // output file containing mapping of obfuscated names
302 proguardDictionary android.Path
303
Colin Cross331a1212018-08-15 20:40:52 -0700304 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700305 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700306
Colin Cross635c3b02016-05-18 15:37:25 -0700307 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700308
Colin Cross635c3b02016-05-18 15:37:25 -0700309 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700310
Colin Cross2fe66872015-03-30 17:20:39 -0700311 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700312 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800313
314 // list of .java files and srcjars that was passed to javac
315 compiledJavaSrcs android.Paths
316 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800317
318 // list of extra progurad flag files
319 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900320
Colin Cross094054a2018-10-17 15:10:48 -0700321 // manifest file to use instead of properties.Manifest
322 overrideManifest android.OptionalPath
323
Jiyong Park1be96912018-05-28 18:02:19 +0900324 // list of SDK lib names that this java moudule is exporting
325 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700326
327 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
328 // filter out Exclude_srcs, will be used by android.IDEInfo struct
329 expandIDEInfoCompiledSrcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700330}
331
Colin Cross54250902017-12-05 09:28:08 -0800332func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700333 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800334}
335
Jiyong Park8fd61922018-11-08 02:50:25 +0900336func (j *Module) DexJarFile() android.Path {
337 return j.dexJarFile
338}
339
Colin Cross54250902017-12-05 09:28:08 -0800340var _ android.SourceFileProducer = (*Module)(nil)
341
Colin Crossf506d872017-07-19 15:53:04 -0700342type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700343 HeaderJars() android.Paths
344 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700345 ResourceJars() android.Paths
346 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700347 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900348 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700349}
350
Jiyong Parkc678ad32018-04-10 13:07:10 +0900351type SdkLibraryDependency interface {
352 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900353 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900354}
355
Nan Zhangb2b33de2018-02-23 11:18:47 -0800356type SrcDependency interface {
357 CompiledSrcs() android.Paths
358 CompiledSrcJars() android.Paths
359}
360
361func (j *Module) CompiledSrcs() android.Paths {
362 return j.compiledJavaSrcs
363}
364
365func (j *Module) CompiledSrcJars() android.Paths {
366 return j.compiledSrcJars
367}
368
369var _ SrcDependency = (*Module)(nil)
370
Colin Cross89536d42017-07-07 14:35:50 -0700371func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
372 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
373 android.InitDefaultableModule(module)
374}
375
Colin Crossbe1da472017-07-07 15:59:46 -0700376type dependencyTag struct {
377 blueprint.BaseDependencyTag
378 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700379}
380
Colin Crossa4f08812018-10-02 22:03:40 -0700381type jniDependencyTag struct {
382 blueprint.BaseDependencyTag
383 target android.Target
384}
385
Colin Crossbe1da472017-07-07 15:59:46 -0700386var (
Colin Cross4b964c02018-10-15 16:18:06 -0700387 staticLibTag = dependencyTag{name: "staticlib"}
388 libTag = dependencyTag{name: "javalib"}
389 annoTag = dependencyTag{name: "annotation processor"}
390 bootClasspathTag = dependencyTag{name: "bootclasspath"}
391 systemModulesTag = dependencyTag{name: "system modules"}
392 frameworkResTag = dependencyTag{name: "framework-res"}
393 frameworkApkTag = dependencyTag{name: "framework-apk"}
394 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
395 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
396 certificateTag = dependencyTag{name: "certificate"}
397 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700398)
Colin Cross2fe66872015-03-30 17:20:39 -0700399
Colin Crossfc3674a2017-09-18 17:41:52 -0700400type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700401 useModule, useFiles, useDefaultLibs, invalidVersion bool
402
Colin Cross86a60ae2018-05-29 14:44:55 -0700403 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700404 systemModules string
405
Colin Crossa97c5d32018-03-28 14:58:31 -0700406 frameworkResModule string
407
Colin Cross86a60ae2018-05-29 14:44:55 -0700408 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700409 aidl android.Path
410}
411
Colin Crossa4f08812018-10-02 22:03:40 -0700412type jniLib struct {
413 name string
414 path android.Path
415 target android.Target
416}
417
Colin Cross3144dfc2018-01-03 15:06:47 -0800418func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
419 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
420}
421
422func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
423 return j.shouldInstrument(ctx) &&
424 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
425 ctx.Config().UnbundledBuild())
426}
427
Colin Cross83bb3162018-06-25 15:48:06 -0700428func (j *Module) sdkVersion() string {
429 return String(j.deviceProperties.Sdk_version)
430}
431
432func (j *Module) minSdkVersion() string {
433 if j.deviceProperties.Min_sdk_version != nil {
434 return *j.deviceProperties.Min_sdk_version
435 }
436 return j.sdkVersion()
437}
438
Dan Willemsen419290a2018-10-31 15:28:47 -0700439func (j *Module) targetSdkVersion() string {
440 if j.deviceProperties.Target_sdk_version != nil {
441 return *j.deviceProperties.Target_sdk_version
442 }
443 return j.sdkVersion()
444}
445
Colin Cross83bb3162018-06-25 15:48:06 -0700446type sdkContext interface {
447 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
448 sdkVersion() string
449 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
450 minSdkVersion() string
Dan Willemsen419290a2018-10-31 15:28:47 -0700451 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
452 targetSdkVersion() string
Colin Cross83bb3162018-06-25 15:48:06 -0700453}
454
455func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
456 switch v {
Neil Fuller401eeba2018-10-18 19:48:58 +0100457 case "", "current", "system_current", "test_current", "core_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700458 return ctx.Config().DefaultAppTargetSdk()
459 default:
460 return v
461 }
462}
463
464// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
465// it returns android.FutureApiLevel (10000).
466func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
467 switch v {
Neil Fuller401eeba2018-10-18 19:48:58 +0100468 case "", "current", "test_current", "system_current", "core_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700469 return ctx.Config().DefaultAppTargetSdkInt(), nil
470 default:
471 n := android.GetNumericSdkVersion(v)
472 if i, err := strconv.Atoi(n); err != nil {
473 return -1, fmt.Errorf("invalid sdk version %q", n)
474 } else {
475 return i, nil
476 }
477 }
478}
479
480func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
481 n, err := sdkVersionToNumber(ctx, v)
482 if err != nil {
483 return "", err
484 }
485 return strconv.Itoa(n), nil
486}
487
488func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
489 v := sdkContext.sdkVersion()
490 i, err := sdkVersionToNumber(ctx, v)
491 if err != nil {
492 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700493 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700494 }
495
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900496 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
497 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
498 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
499 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
500 if ctx.DeviceSpecific() || ctx.SocSpecific() {
501 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
502 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
503 }
504 }
505 version := strings.TrimPrefix(v, "system_")
506 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
507 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
508 v, allowed_versions)
509 }
510 }
511
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100512 toPrebuilt := func(sdk string) sdkDep {
513 var api, v string
514 if strings.Contains(sdk, "_") {
515 t := strings.Split(sdk, "_")
516 api = t[0]
517 v = t[1]
518 } else {
519 api = "public"
520 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900521 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100522 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700523 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100524 // There's no aidl for other SDKs yet.
525 // TODO(77525052): Add aidl files for other SDKs too.
526 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
527 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800528 jarPath := android.ExistentPathForSource(ctx, jar)
529 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700530 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700531
Colin Cross6510f912017-11-29 00:27:14 -0800532 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700533 return sdkDep{
534 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700535 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700536 }
537 }
538
Colin Crossfc3674a2017-09-18 17:41:52 -0700539 if !jarPath.Valid() {
540 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
541 return sdkDep{}
542 }
Colin Cross47ff2522017-10-02 14:22:08 -0700543
Colin Crossfc3674a2017-09-18 17:41:52 -0700544 if !aidlPath.Valid() {
545 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
546 return sdkDep{}
547 }
Colin Cross47ff2522017-10-02 14:22:08 -0700548
Colin Crossfc3674a2017-09-18 17:41:52 -0700549 return sdkDep{
550 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700551 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700552 aidl: aidlPath.Path(),
553 }
554 }
555
Colin Crossa97c5d32018-03-28 14:58:31 -0700556 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700557 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700558 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700559 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700560 systemModules: m + "_system_modules",
561 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700562 }
563 if m == "core.current.stubs" {
564 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100565 } else if m == "core.platform.api.stubs" {
566 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700567 }
568 return ret
569 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700570
Colin Cross6510f912017-11-29 00:27:14 -0800571 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100572 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700573 }
574
575 switch v {
576 case "":
577 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700578 useDefaultLibs: true,
579 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700580 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700581 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700582 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700583 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700584 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700585 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700586 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700587 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700588 return toModule("core.current.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700589 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100590 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700591 }
592}
593
Colin Crossbe1da472017-07-07 15:59:46 -0700594func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700595 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700596 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700597 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700598 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700599 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100600 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700601 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700602 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700603 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700604 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100605 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700606 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800607 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700608 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
609 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800610 }
Colin Crossbe1da472017-07-07 15:59:46 -0700611 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700612 } else if j.deviceProperties.System_modules == nil {
613 ctx.PropertyErrorf("no_standard_libs",
614 "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 +0100615 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700616 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700617 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100618 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700619 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800620 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800621 if ctx.ModuleName() == "android_stubs_current" ||
622 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700623 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700624 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800625 }
Colin Cross2fe66872015-03-30 17:20:39 -0700626 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700627
Colin Cross42d48b72018-08-29 14:10:52 -0700628 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
629 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100630 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700631 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100632 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700633
Colin Cross7f9036c2017-08-30 13:27:57 -0700634 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000635 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700636 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800637 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700638
639 if j.hasSrcExt(".proto") {
640 protoDeps(ctx, &j.protoProperties)
641 }
Colin Cross93e85952017-08-15 13:34:18 -0700642
643 if j.hasSrcExt(".kt") {
644 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
645 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700646 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700647 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800648
649 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700650 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800651 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700652}
653
654func hasSrcExt(srcs []string, ext string) bool {
655 for _, src := range srcs {
656 if filepath.Ext(src) == ext {
657 return true
658 }
659 }
660
661 return false
662}
663
Nan Zhang61eaedb2017-11-02 13:28:15 -0700664func shardPaths(paths android.Paths, shardSize int) []android.Paths {
665 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
666 for len(paths) > shardSize {
667 ret = append(ret, paths[0:shardSize])
668 paths = paths[shardSize:]
669 }
670 if len(paths) > 0 {
671 ret = append(ret, paths)
672 }
673 return ret
674}
675
Colin Cross6af17aa2017-09-20 12:59:05 -0700676func (j *Module) hasSrcExt(ext string) bool {
677 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700678}
679
Colin Cross46c9b8b2017-06-22 16:51:17 -0700680func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700681 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700682
Colin Crossebe1a512017-11-14 13:12:14 -0800683 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
684 aidlIncludes = append(aidlIncludes,
685 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
686 aidlIncludes = append(aidlIncludes,
687 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700688
Steven Moreland667f6882018-07-26 12:55:08 -0700689 flags := []string{"-b"}
690
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700691 if aidlPreprocess.Valid() {
692 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700693 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700694 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700695 }
696
Colin Cross635c3b02016-05-18 15:37:25 -0700697 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800698 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700699 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800700 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700701 flags = append(flags, "-I"+src.String())
702 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700703
Martijn Coeneneab15642018-03-09 09:29:59 +0100704 if Bool(j.deviceProperties.Aidl.Generate_traces) {
705 flags = append(flags, "-t")
706 }
707
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100708 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
709 flags = append(flags, "--transaction_names")
710 }
711
Colin Crossf03c82b2015-04-13 13:53:40 -0700712 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700713}
714
Colin Cross32f676a2017-09-06 13:41:06 -0700715type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800716 classpath classpath
717 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700718 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700719 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700720 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700721 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700722 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800723 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700724 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700725 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700726 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700727 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700728}
Colin Cross2fe66872015-03-30 17:20:39 -0700729
Colin Cross54250902017-12-05 09:28:08 -0800730func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
731 for _, f := range dep.Srcs() {
732 if f.Ext() != ".jar" {
733 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
734 ctx.OtherModuleName(dep.(blueprint.Module)))
735 }
736 }
737}
738
Jiyong Park2d492942018-03-05 17:44:10 +0900739type linkType int
740
741const (
742 javaCore linkType = iota
743 javaSdk
744 javaSystem
745 javaPlatform
746)
747
Jiyong Park46f78fb2018-10-20 16:33:17 +0900748func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700749 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700750 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900751 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
752 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
753 name == "core-lambda-stubs":
754 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100755 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900756 return javaCore, false
757 case name == "android_system_stubs_current":
758 return javaSystem, true
759 case strings.HasPrefix(ver, "system_"):
760 return javaSystem, false
761 case name == "android_test_stubs_current":
762 return javaSystem, true
763 case strings.HasPrefix(ver, "test_"):
764 return javaPlatform, false
765 case name == "android_stubs_current":
766 return javaSdk, true
767 case ver == "current":
768 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700769 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900770 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700771 default:
772 if _, err := strconv.Atoi(ver); err != nil {
773 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
774 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900775 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900776 }
777}
778
Jiyong Park750e5572018-01-31 00:20:13 +0900779func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700780 if ctx.Host() {
781 return
782 }
783
Jiyong Park46f78fb2018-10-20 16:33:17 +0900784 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
785 if stubs {
786 return
787 }
788 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900789 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."
790
791 switch myLinkType {
792 case javaCore:
793 if otherLinkType != javaCore {
794 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 +0900795 ctx.OtherModuleName(to))
796 }
Jiyong Park2d492942018-03-05 17:44:10 +0900797 break
798 case javaSdk:
799 if otherLinkType != javaCore && otherLinkType != javaSdk {
800 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
801 ctx.OtherModuleName(to))
802 }
803 break
804 case javaSystem:
805 if otherLinkType == javaPlatform {
806 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
807 ctx.OtherModuleName(to))
808 }
809 break
810 case javaPlatform:
811 // no restriction on link-type
812 break
Jiyong Park750e5572018-01-31 00:20:13 +0900813 }
814}
815
Colin Cross32f676a2017-09-06 13:41:06 -0700816func (j *Module) collectDeps(ctx android.ModuleContext) deps {
817 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700818
Colin Cross300f0382018-03-06 13:11:51 -0800819 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700820 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800821 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700822 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800823 } else if sdkDep.useFiles {
824 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700825 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800826 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
827 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700828 }
829
Colin Crossd11fcda2017-10-23 17:59:01 -0700830 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700831 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700832 tag := ctx.OtherModuleDependencyTag(module)
833
Colin Crossa4f08812018-10-02 22:03:40 -0700834 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700835 // Handled by AndroidApp.collectAppDeps
836 return
837 }
838 if tag == certificateTag {
839 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700840 return
841 }
842
Jiyong Park750e5572018-01-31 00:20:13 +0900843 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700844 switch tag {
845 case bootClasspathTag, libTag, staticLibTag:
846 checkLinkType(ctx, j, to, tag.(dependencyTag))
847 }
Jiyong Park750e5572018-01-31 00:20:13 +0900848 }
Colin Cross54250902017-12-05 09:28:08 -0800849 switch dep := module.(type) {
850 case Dependency:
851 switch tag {
852 case bootClasspathTag:
853 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700854 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800855 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900856 // sdk lib names from dependencies are re-exported
857 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800858 case staticLibTag:
859 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
860 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
861 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700862 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900863 // sdk lib names from dependencies are re-exported
864 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross6a77c982018-06-19 22:43:34 -0700865 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700866 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800867 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100868 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800869 // framework.jar has a one-off dependency on the R.java and Manifest.java files
870 // generated by framework-res.apk
871 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
872 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800873 case frameworkApkTag:
874 if ctx.ModuleName() == "android_stubs_current" ||
875 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700876 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800877 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
878 // resource files out of there for aapt.
879 //
880 // Normally the package rule runs aapt, which includes the resource,
881 // but we're not running that in our package rule so just copy in the
882 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700883 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800884 }
Colin Cross54250902017-12-05 09:28:08 -0800885 case kotlinStdlibTag:
886 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800887 }
888
889 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900890 case SdkLibraryDependency:
891 switch tag {
892 case libTag:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900893 linkType, _ := getLinkType(j, ctx.ModuleName())
894 deps.classpath = append(deps.classpath, dep.HeaderJars(linkType)...)
Jiyong Park1be96912018-05-28 18:02:19 +0900895 // names of sdk libs that are directly depended are exported
896 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900897 default:
898 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
899 }
Colin Cross54250902017-12-05 09:28:08 -0800900 case android.SourceFileProducer:
901 switch tag {
902 case libTag:
903 checkProducesJars(ctx, dep)
904 deps.classpath = append(deps.classpath, dep.Srcs()...)
905 case staticLibTag:
906 checkProducesJars(ctx, dep)
907 deps.classpath = append(deps.classpath, dep.Srcs()...)
908 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
909 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
910 case android.DefaultsDepTag, android.SourceDepTag:
911 // Nothing to do
912 default:
913 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
914 }
915 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700916 switch tag {
917 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700918 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700919 case systemModulesTag:
920 if deps.systemModules != nil {
921 panic("Found two system module dependencies")
922 }
923 sm := module.(*SystemModules)
924 if sm.outputFile == nil {
925 panic("Missing directory for system module dependency")
926 }
927 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700928 default:
929 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700930 }
Colin Crossec7a0422017-07-07 14:47:12 -0700931 }
Colin Cross2fe66872015-03-30 17:20:39 -0700932 })
933
Jiyong Park1be96912018-05-28 18:02:19 +0900934 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
935
Colin Cross32f676a2017-09-06 13:41:06 -0700936 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700937}
938
Colin Cross83bb3162018-06-25 15:48:06 -0700939func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700940 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700941 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
942 if err != nil {
943 ctx.PropertyErrorf("sdk_version", "%s", err)
944 }
Nan Zhang357466b2018-04-17 17:38:36 -0700945 if javaVersion != "" {
946 ret = javaVersion
947 } else if ctx.Device() && sdk <= 23 {
948 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900949 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700950 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700951 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700952 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
953 ret = "1.8"
954 } else {
955 ret = "1.9"
956 }
957
958 return ret
959}
960
Nan Zhanged19fc32017-10-19 13:06:22 -0700961func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700962
Colin Crossf03c82b2015-04-13 13:53:40 -0700963 var flags javaBuilderFlags
964
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100965 // javaVersion flag.
966 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
967
Nan Zhanged19fc32017-10-19 13:06:22 -0700968 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700969 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100970 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700971 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700972 }
Colin Cross6510f912017-11-29 00:27:14 -0800973 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700974 // Override the -g flag passed globally to remove local variable debug info to reduce
975 // disk and memory usage.
976 javacFlags = append(javacFlags, "-g:source,lines")
977 }
Colin Cross64162712017-08-08 13:17:59 -0700978
Colin Cross66548102018-06-19 22:47:35 -0700979 if ctx.Config().RunErrorProne() {
980 if config.ErrorProneClasspath == nil {
981 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
982 }
983
984 errorProneFlags := []string{
985 "-Xplugin:ErrorProne",
986 "${config.ErrorProneChecks}",
987 }
988 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
989
990 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
991 "'" + strings.Join(errorProneFlags, " ") + "'"
992 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800993 }
994
Nan Zhanged19fc32017-10-19 13:06:22 -0700995 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800996 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
997 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700998 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800999
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001000 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -08001001 !Bool(j.properties.No_standard_libs) &&
1002 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
1003 // Give host-side tools a version of OpenJDK's standard libraries
1004 // close to what they're targeting. As of Dec 2017, AOSP is only
1005 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
1006 //
1007 // When building with OpenJDK 8, the following should have no
1008 // effect since those jars would be available by default.
1009 //
1010 // When building with OpenJDK 9 but targeting a version < 1.8,
1011 // putting them on the bootclasspath means that:
1012 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1013 // b) references to existing APIs are not reinterpreted in an
1014 // OpenJDK 9-specific way, eg. calls to subclasses of
1015 // java.nio.Buffer as in http://b/70862583
1016 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1017 flags.bootClasspath = append(flags.bootClasspath,
1018 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1019 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001020 if Bool(j.properties.Use_tools_jar) {
1021 flags.bootClasspath = append(flags.bootClasspath,
1022 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1023 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001024 }
1025
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001026 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -07001027 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
1028 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
1029 }
1030
Nan Zhanged19fc32017-10-19 13:06:22 -07001031 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001032 if deps.systemModules != nil {
1033 flags.systemModules = append(flags.systemModules, deps.systemModules)
1034 }
1035
Nan Zhanged19fc32017-10-19 13:06:22 -07001036 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -07001037 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -07001038 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001039 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -07001040 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
1041 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -07001042 }
1043
Colin Cross81440082018-08-15 20:21:55 -07001044 if len(javacFlags) > 0 {
1045 // optimization.
1046 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1047 flags.javacFlags = "$javacFlags"
1048 }
1049
Nan Zhanged19fc32017-10-19 13:06:22 -07001050 return flags
1051}
Colin Crossc0b06f12015-04-08 13:03:43 -07001052
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001053func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001054
Colin Crossebe1a512017-11-14 13:12:14 -08001055 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001056
1057 deps := j.collectDeps(ctx)
1058 flags := j.collectBuilderFlags(ctx, deps)
1059
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001060 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001061 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1062 }
1063 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001064 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001065 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001066 }
1067
Colin Crossaf050172017-11-15 23:01:59 -08001068 srcFiles = j.genSources(ctx, srcFiles, flags)
1069
1070 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001071 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001072 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001073
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001074 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1075 // that IDEInfo struct will use
1076 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1077
Colin Cross1ee23172017-10-18 14:44:18 -07001078 jarName := ctx.ModuleName() + ".jar"
1079
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001080 javaSrcFiles := srcFiles.FilterByExt(".java")
1081 var uniqueSrcFiles android.Paths
1082 set := make(map[string]bool)
1083 for _, v := range javaSrcFiles {
1084 if _, found := set[v.String()]; !found {
1085 set[v.String()] = true
1086 uniqueSrcFiles = append(uniqueSrcFiles, v)
1087 }
1088 }
1089
Colin Cross55f63ea2018-08-27 12:37:09 -07001090 var kotlinJars android.Paths
1091
Colin Cross93e85952017-08-15 13:34:18 -07001092 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001093 // user defined kotlin flags.
1094 kotlincFlags := j.properties.Kotlincflags
1095 CheckKotlincFlags(ctx, kotlincFlags)
1096
Colin Cross93e85952017-08-15 13:34:18 -07001097 // If there are kotlin files, compile them first but pass all the kotlin and java files
1098 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1099 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001100 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001101 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001102 kotlincFlags = append(kotlincFlags, "-no-jdk")
1103 }
1104 if len(kotlincFlags) > 0 {
1105 // optimization.
1106 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1107 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001108 }
1109
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001110 var kotlinSrcFiles android.Paths
1111 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1112 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1113
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001114 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001115 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1116 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1117
Colin Cross1ee23172017-10-18 14:44:18 -07001118 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001119 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001120 if ctx.Failed() {
1121 return
1122 }
1123
1124 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001125 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001126 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001127
Colin Cross93e85952017-08-15 13:34:18 -07001128 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001129 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001130 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001131 }
1132
Colin Cross55f63ea2018-08-27 12:37:09 -07001133 jars := append(android.Paths(nil), kotlinJars...)
1134
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001135 // Store the list of .java files that was passed to javac
1136 j.compiledJavaSrcs = uniqueSrcFiles
1137 j.compiledSrcJars = srcJars
1138
Nan Zhang61eaedb2017-11-02 13:28:15 -07001139 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001140 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001141 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1142 enable_sharding = true
1143 if len(j.properties.Annotation_processors) != 0 ||
1144 len(j.properties.Annotation_processor_classes) != 0 {
1145 ctx.PropertyErrorf("javac_shard_size",
1146 "%q cannot be set when annotation processors are enabled.",
1147 j.properties.Javac_shard_size)
1148 }
1149 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001150 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001151 if ctx.Failed() {
1152 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001153 }
1154 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001155 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001156 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001157 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001158 // If error-prone is enabled, add an additional rule to compile the java files into
1159 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001160 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001161 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1162 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001163 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001164 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001165 extraJarDeps = append(extraJarDeps, errorprone)
1166 }
1167
Nan Zhang61eaedb2017-11-02 13:28:15 -07001168 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001169 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001170 shardSize := int(*(j.properties.Javac_shard_size))
1171 var shardSrcs []android.Paths
1172 if len(uniqueSrcFiles) > 0 {
1173 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1174 for idx, shardSrc := range shardSrcs {
1175 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1176 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1177 jars = append(jars, classes)
1178 }
1179 }
1180 if len(srcJars) > 0 {
1181 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1182 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1183 jars = append(jars, classes)
1184 }
1185 } else {
1186 classes := android.PathForModuleOut(ctx, "javac", jarName)
1187 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1188 jars = append(jars, classes)
1189 }
Colin Crossd6891432017-09-27 17:39:56 -07001190 if ctx.Failed() {
1191 return
1192 }
Colin Cross2fe66872015-03-30 17:20:39 -07001193 }
1194
Colin Crosscedd4762018-09-13 11:26:19 -07001195 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1196 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001197 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1198
1199 var resArgs []string
1200 var resDeps android.Paths
1201
1202 resArgs = append(resArgs, dirArgs...)
1203 resDeps = append(resDeps, dirDeps...)
1204
1205 resArgs = append(resArgs, fileArgs...)
1206 resDeps = append(resDeps, fileDeps...)
1207
Colin Crossff3ae9d2018-04-10 16:15:18 -07001208 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001209 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001210 resArgs = append(resArgs, srcArgs...)
1211 resDeps = append(resDeps, srcDeps...)
1212 }
Colin Cross40a36712017-09-27 17:41:35 -07001213
1214 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001215 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001216 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001217 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001218 if ctx.Failed() {
1219 return
1220 }
1221 }
1222
Colin Cross331a1212018-08-15 20:40:52 -07001223 if len(deps.staticResourceJars) > 0 {
1224 var jars android.Paths
1225 if j.resourceJar != nil {
1226 jars = append(jars, j.resourceJar)
1227 }
1228 jars = append(jars, deps.staticResourceJars...)
1229
1230 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1231 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1232 false, nil, nil)
1233 j.resourceJar = combinedJar
1234 }
1235
Colin Cross32f676a2017-09-06 13:41:06 -07001236 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001237 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001238
Colin Cross094054a2018-10-17 15:10:48 -07001239 manifest := j.overrideManifest
1240 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001241 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1242 }
Colin Cross635acc92017-09-12 22:50:46 -07001243
Colin Cross0a6e0072017-08-30 14:24:55 -07001244 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001245 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001246 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001247
1248 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001249 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1250 // Optimization: skip the combine step if there is nothing to do
1251 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1252 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1253 // any if len(jars) == 1.
1254 outputFile = moduleOutPath
1255 } else {
1256 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1257 ctx.Build(pctx, android.BuildParams{
1258 Rule: android.Cp,
1259 Input: jars[0],
1260 Output: combinedJar,
1261 })
1262 outputFile = combinedJar
1263 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001264 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001265 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001266 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001267 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001268 outputFile = combinedJar
1269 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001270
Colin Cross331a1212018-08-15 20:40:52 -07001271 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001272 if j.properties.Jarjar_rules != nil {
1273 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001274 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001275 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001276 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1277 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001278
1279 // jarjar resource jar if necessary
1280 if j.resourceJar != nil {
1281 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1282 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1283 j.resourceJar = resourceJarJarFile
1284 }
1285
Colin Cross0a6e0072017-08-30 14:24:55 -07001286 if ctx.Failed() {
1287 return
1288 }
1289 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001290 j.implementationJarFile = outputFile
1291 if j.headerJarFile == nil {
1292 j.headerJarFile = j.implementationJarFile
1293 }
Colin Cross2fe66872015-03-30 17:20:39 -07001294
Colin Cross6510f912017-11-29 00:27:14 -08001295 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001296 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1297 j.properties.Instrument = true
1298 }
1299 }
1300
Colin Cross3144dfc2018-01-03 15:06:47 -08001301 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001302 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1303 }
1304
Colin Cross331a1212018-08-15 20:40:52 -07001305 // merge implementation jar with resources if necessary
1306 implementationAndResourcesJar := outputFile
1307 if j.resourceJar != nil {
1308 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1309 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1310 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1311 false, nil, nil)
1312 implementationAndResourcesJar = combinedJar
1313 }
1314
1315 j.implementationAndResourcesJar = implementationAndResourcesJar
1316
Colin Cross9ae1b922018-06-26 17:59:05 -07001317 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001318 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001319 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001320 if ctx.Failed() {
1321 return
1322 }
Colin Cross331a1212018-08-15 20:40:52 -07001323
1324 // merge dex jar with resources if necessary
1325 if j.resourceJar != nil {
1326 jars := android.Paths{dexOutputFile, j.resourceJar}
1327 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1328 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1329 false, nil, nil)
1330 dexOutputFile = combinedJar
1331 }
1332
1333 j.dexJarFile = dexOutputFile
1334
Colin Cross3063b782018-08-15 11:19:12 -07001335 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001336 } else {
1337 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001338 }
Colin Cross331a1212018-08-15 20:40:52 -07001339
Colin Crossb7a63242015-04-16 14:09:14 -07001340 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001341
1342 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1343 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001344}
1345
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001346// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1347// since some of these flags may be used internally.
1348func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1349 for _, flag := range flags {
1350 flag = strings.TrimSpace(flag)
1351
1352 if !strings.HasPrefix(flag, "-") {
1353 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1354 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1355 ctx.PropertyErrorf("kotlincflags",
1356 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1357 } else if inList(flag, config.KotlincIllegalFlags) {
1358 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1359 } else if flag == "-include-runtime" {
1360 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1361 } else {
1362 args := strings.Split(flag, " ")
1363 if args[0] == "-kotlin-home" {
1364 ctx.PropertyErrorf("kotlincflags",
1365 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1366 }
1367 }
1368 }
1369}
1370
Colin Cross8eadbf02017-10-24 17:46:00 -07001371func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001372 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001373
1374 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001375 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001376 // Compile java sources into turbine.jar.
1377 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1378 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1379 if ctx.Failed() {
1380 return nil
1381 }
1382 jars = append(jars, turbineJar)
1383 }
1384
Colin Cross55f63ea2018-08-27 12:37:09 -07001385 jars = append(jars, extraJars...)
1386
Nan Zhanged19fc32017-10-19 13:06:22 -07001387 // Combine any static header libraries into classes-header.jar. If there is only
1388 // one input jar this step will be skipped.
1389 var headerJar android.Path
1390 jars = append(jars, deps.staticHeaderJars...)
1391
Colin Cross5c6ecc12017-10-23 18:12:27 -07001392 // we cannot skip the combine step for now if there is only one jar
1393 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1394 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001395 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1396 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001397 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001398
1399 if j.properties.Jarjar_rules != nil {
1400 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1401 // Transform classes.jar into classes-jarjar.jar
1402 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1403 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1404 headerJar = jarjarFile
1405 if ctx.Failed() {
1406 return nil
1407 }
1408 }
1409
1410 return headerJar
1411}
1412
Colin Crosscb933592017-11-22 13:49:43 -08001413func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001414 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001415
Colin Cross7a3139e2017-12-19 13:57:50 -08001416 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001417
Colin Cross84c38822018-01-03 15:59:46 -08001418 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001419 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1420
1421 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1422
1423 j.jacocoReportClassesFile = jacocoReportClassesFile
1424
1425 return instrumentedJar
1426}
1427
Colin Crossf506d872017-07-19 15:53:04 -07001428var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001429
Nan Zhanged19fc32017-10-19 13:06:22 -07001430func (j *Module) HeaderJars() android.Paths {
1431 return android.Paths{j.headerJarFile}
1432}
1433
1434func (j *Module) ImplementationJars() android.Paths {
1435 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001436}
1437
Colin Cross331a1212018-08-15 20:40:52 -07001438func (j *Module) ResourceJars() android.Paths {
1439 if j.resourceJar == nil {
1440 return nil
1441 }
1442 return android.Paths{j.resourceJar}
1443}
1444
1445func (j *Module) ImplementationAndResourcesJars() android.Paths {
1446 return android.Paths{j.implementationAndResourcesJar}
1447}
1448
Colin Cross46c9b8b2017-06-22 16:51:17 -07001449func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001450 return j.exportAidlIncludeDirs
1451}
1452
Jiyong Park1be96912018-05-28 18:02:19 +09001453func (j *Module) ExportedSdkLibs() []string {
1454 return j.exportedSdkLibs
1455}
1456
Colin Cross46c9b8b2017-06-22 16:51:17 -07001457var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001458
Colin Cross46c9b8b2017-06-22 16:51:17 -07001459func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001460 return j.logtagsSrcs
1461}
1462
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001463// Collect information for opening IDE project files in java/jdeps.go.
1464func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1465 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1466 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1467 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1468 if j.properties.Jarjar_rules != nil {
1469 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1470 }
1471}
1472
1473func (j *Module) CompilerDeps() []string {
1474 jdeps := []string{}
1475 jdeps = append(jdeps, j.properties.Libs...)
1476 jdeps = append(jdeps, j.properties.Static_libs...)
1477 return jdeps
1478}
1479
Colin Cross2fe66872015-03-30 17:20:39 -07001480//
1481// Java libraries (.jar file)
1482//
1483
Colin Crossf506d872017-07-19 15:53:04 -07001484type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001485 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001486}
1487
Colin Crossf506d872017-07-19 15:53:04 -07001488func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001489 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001490
Colin Cross9ae1b922018-06-26 17:59:05 -07001491 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001492 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1493 ctx.ModuleName()+".jar", j.outputFile)
1494 }
Colin Crossb7a63242015-04-16 14:09:14 -07001495}
1496
Colin Crossf506d872017-07-19 15:53:04 -07001497func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001498 j.deps(ctx)
1499}
1500
Colin Cross9ae1b922018-06-26 17:59:05 -07001501func LibraryFactory() android.Module {
1502 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001503
Colin Cross9ae1b922018-06-26 17:59:05 -07001504 module.AddProperties(
1505 &module.Module.properties,
1506 &module.Module.deviceProperties,
1507 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001508
Colin Cross9ae1b922018-06-26 17:59:05 -07001509 InitJavaModule(module, android.HostAndDeviceSupported)
1510 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001511}
1512
Colin Crossf506d872017-07-19 15:53:04 -07001513func LibraryHostFactory() android.Module {
1514 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001515
Colin Cross6af17aa2017-09-20 12:59:05 -07001516 module.AddProperties(
1517 &module.Module.properties,
1518 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001519
Colin Cross9ae1b922018-06-26 17:59:05 -07001520 module.Module.properties.Installable = proptools.BoolPtr(true)
1521
Colin Cross89536d42017-07-07 14:35:50 -07001522 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001523 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001524}
1525
1526//
Colin Crossb628ea52018-08-14 16:42:33 -07001527// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001528//
1529
1530type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001531 // list of compatibility suites (for example "cts", "vts") that the module should be
1532 // installed into.
1533 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001534
1535 // the name of the test configuration (for example "AndroidTest.xml") that should be
1536 // installed with the module.
1537 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001538
Jack He33338892018-09-19 02:21:28 -07001539 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1540 // should be installed with the module.
1541 Test_config_template *string `android:"arch_variant"`
1542
Colin Crossd96ca352018-08-10 16:06:24 -07001543 // list of files or filegroup modules that provide data that should be installed alongside
1544 // the test
1545 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001546}
1547
1548type Test struct {
1549 Library
1550
1551 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001552
1553 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001554 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001555}
1556
1557func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001558 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001559 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001560
1561 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001562}
1563
1564func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1565 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001566 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001567 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001568 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001569}
1570
1571func TestFactory() android.Module {
1572 module := &Test{}
1573
1574 module.AddProperties(
1575 &module.Module.properties,
1576 &module.Module.deviceProperties,
1577 &module.Module.protoProperties,
1578 &module.testProperties)
1579
Colin Cross9ae1b922018-06-26 17:59:05 -07001580 module.Module.properties.Installable = proptools.BoolPtr(true)
1581
Colin Cross05638fc2018-04-09 18:40:24 -07001582 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001583 return module
1584}
1585
1586func TestHostFactory() android.Module {
1587 module := &Test{}
1588
1589 module.AddProperties(
1590 &module.Module.properties,
1591 &module.Module.protoProperties,
1592 &module.testProperties)
1593
Colin Cross9ae1b922018-06-26 17:59:05 -07001594 module.Module.properties.Installable = proptools.BoolPtr(true)
1595
Colin Cross05638fc2018-04-09 18:40:24 -07001596 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001597 return module
1598}
1599
1600//
Colin Cross2fe66872015-03-30 17:20:39 -07001601// Java Binaries (.jar file plus wrapper script)
1602//
1603
Colin Crossf506d872017-07-19 15:53:04 -07001604type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001605 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001606 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001607
1608 // Name of the class containing main to be inserted into the manifest as Main-Class.
1609 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001610}
1611
Colin Crossf506d872017-07-19 15:53:04 -07001612type Binary struct {
1613 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001614
Colin Crossf506d872017-07-19 15:53:04 -07001615 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001616
Colin Cross6b4a32d2017-12-05 13:42:45 -08001617 isWrapperVariant bool
1618
Colin Crossc3315992017-12-08 19:12:36 -08001619 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001620 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001621}
1622
Alex Light24237172017-10-26 09:46:21 -07001623func (j *Binary) HostToolPath() android.OptionalPath {
1624 return android.OptionalPathForPath(j.binaryFile)
1625}
1626
Colin Crossf506d872017-07-19 15:53:04 -07001627func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001628 if ctx.Arch().ArchType == android.Common {
1629 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001630 if j.binaryProperties.Main_class != nil {
1631 if j.properties.Manifest != nil {
1632 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1633 }
1634 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1635 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1636 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1637 }
1638
Colin Cross6b4a32d2017-12-05 13:42:45 -08001639 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001640 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001641 // Handle the binary wrapper
1642 j.isWrapperVariant = true
1643
Colin Cross366938f2017-12-11 16:29:02 -08001644 if j.binaryProperties.Wrapper != nil {
1645 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001646 } else {
1647 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1648 }
1649
1650 // Depend on the installed jar so that the wrapper doesn't get executed by
1651 // another build rule before the jar has been installed.
1652 jarFile := ctx.PrimaryModule().(*Binary).installFile
1653
1654 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1655 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001656 }
Colin Cross2fe66872015-03-30 17:20:39 -07001657}
1658
Colin Crossf506d872017-07-19 15:53:04 -07001659func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001660 if ctx.Arch().ArchType == android.Common {
1661 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001662 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001663 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001664 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001665}
1666
Colin Crossf506d872017-07-19 15:53:04 -07001667func BinaryFactory() android.Module {
1668 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001669
Colin Cross36242852017-06-23 15:06:31 -07001670 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001671 &module.Module.properties,
1672 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001673 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001674 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001675
Colin Cross9ae1b922018-06-26 17:59:05 -07001676 module.Module.properties.Installable = proptools.BoolPtr(true)
1677
Colin Cross6b4a32d2017-12-05 13:42:45 -08001678 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1679 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001680 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001681}
1682
Colin Crossf506d872017-07-19 15:53:04 -07001683func BinaryHostFactory() android.Module {
1684 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001685
Colin Cross36242852017-06-23 15:06:31 -07001686 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001687 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001688 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001689 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001690
Colin Cross9ae1b922018-06-26 17:59:05 -07001691 module.Module.properties.Installable = proptools.BoolPtr(true)
1692
Colin Cross6b4a32d2017-12-05 13:42:45 -08001693 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1694 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001695 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001696}
1697
1698//
1699// Java prebuilts
1700//
1701
Colin Cross74d73e22017-08-02 11:05:49 -07001702type ImportProperties struct {
1703 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001704
Nan Zhangea568a42017-11-08 21:20:04 -08001705 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001706
1707 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001708
1709 // List of shared java libs that this module has dependencies to
1710 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001711
1712 // List of files to remove from the jar file(s)
1713 Exclude_files []string
1714
1715 // List of directories to remove from the jar file(s)
1716 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001717
1718 // if set to true, run Jetifier against .jar file. Defaults to false.
1719 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001720}
1721
1722type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001723 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001724 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001725 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001726
Colin Cross74d73e22017-08-02 11:05:49 -07001727 properties ImportProperties
1728
Colin Cross0a6e0072017-08-30 14:24:55 -07001729 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001730 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001731}
1732
Colin Cross83bb3162018-06-25 15:48:06 -07001733func (j *Import) sdkVersion() string {
1734 return String(j.properties.Sdk_version)
1735}
1736
1737func (j *Import) minSdkVersion() string {
1738 return j.sdkVersion()
1739}
1740
Colin Cross74d73e22017-08-02 11:05:49 -07001741func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001742 return &j.prebuilt
1743}
1744
Colin Cross74d73e22017-08-02 11:05:49 -07001745func (j *Import) PrebuiltSrcs() []string {
1746 return j.properties.Jars
1747}
1748
1749func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001750 return j.prebuilt.Name(j.ModuleBase.Name())
1751}
1752
Colin Cross74d73e22017-08-02 11:05:49 -07001753func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001754 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001755 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001756}
1757
Colin Cross74d73e22017-08-02 11:05:49 -07001758func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001759 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001760
Nan Zhang4c819fb2018-08-27 18:31:46 -07001761 jarName := ctx.ModuleName() + ".jar"
1762 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001763 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1764 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001765 if Bool(j.properties.Jetifier_enabled) {
1766 inputFile := outputFile
1767 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1768 TransformJetifier(ctx, outputFile, inputFile)
1769 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001770 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001771
1772 ctx.VisitDirectDeps(func(module android.Module) {
1773 otherName := ctx.OtherModuleName(module)
1774 tag := ctx.OtherModuleDependencyTag(module)
1775
1776 switch dep := module.(type) {
1777 case Dependency:
1778 switch tag {
1779 case libTag, staticLibTag:
1780 // sdk lib names from dependencies are re-exported
1781 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1782 }
1783 case SdkLibraryDependency:
1784 switch tag {
1785 case libTag:
1786 // names of sdk libs that are directly depended are exported
1787 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1788 }
1789 }
1790 })
1791
1792 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001793 if Bool(j.properties.Installable) {
1794 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1795 ctx.ModuleName()+".jar", outputFile)
1796 }
Colin Cross2fe66872015-03-30 17:20:39 -07001797}
1798
Colin Cross74d73e22017-08-02 11:05:49 -07001799var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001800
Nan Zhanged19fc32017-10-19 13:06:22 -07001801func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001802 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001803}
1804
1805func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001806 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001807}
1808
Colin Cross331a1212018-08-15 20:40:52 -07001809func (j *Import) ResourceJars() android.Paths {
1810 return nil
1811}
1812
1813func (j *Import) ImplementationAndResourcesJars() android.Paths {
1814 return android.Paths{j.combinedClasspathFile}
1815}
1816
Colin Cross74d73e22017-08-02 11:05:49 -07001817func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001818 return nil
1819}
1820
Jiyong Park1be96912018-05-28 18:02:19 +09001821func (j *Import) ExportedSdkLibs() []string {
1822 return j.exportedSdkLibs
1823}
1824
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001825// Collect information for opening IDE project files in java/jdeps.go.
1826const (
1827 removedPrefix = "prebuilt_"
1828)
1829
1830func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1831 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1832}
1833
1834func (j *Import) IDECustomizedModuleName() string {
1835 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1836 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1837 // solution to get the Import name.
1838 name := j.Name()
1839 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001840 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001841 }
1842 return name
1843}
1844
Colin Cross74d73e22017-08-02 11:05:49 -07001845var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001846
Colin Cross74d73e22017-08-02 11:05:49 -07001847func ImportFactory() android.Module {
1848 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001849
Colin Cross74d73e22017-08-02 11:05:49 -07001850 module.AddProperties(&module.properties)
1851
1852 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001853 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001854 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001855}
1856
Colin Cross74d73e22017-08-02 11:05:49 -07001857func ImportFactoryHost() android.Module {
1858 module := &Import{}
1859
1860 module.AddProperties(&module.properties)
1861
1862 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001863 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001864 return module
1865}
1866
Colin Cross89536d42017-07-07 14:35:50 -07001867//
1868// Defaults
1869//
1870type Defaults struct {
1871 android.ModuleBase
1872 android.DefaultsModuleBase
1873}
1874
1875func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1876}
1877
1878func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1879}
1880
1881func defaultsFactory() android.Module {
1882 return DefaultsFactory()
1883}
1884
1885func DefaultsFactory(props ...interface{}) android.Module {
1886 module := &Defaults{}
1887
1888 module.AddProperties(props...)
1889 module.AddProperties(
1890 &CompilerProperties{},
1891 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001892 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001893 &aaptProperties{},
1894 &androidLibraryProperties{},
1895 &appProperties{},
1896 &appTestProperties{},
1897 &ImportProperties{},
1898 &AARImportProperties{},
1899 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001900 )
1901
1902 android.InitDefaultsModule(module)
1903
1904 return module
1905}
Nan Zhangea568a42017-11-08 21:20:04 -08001906
1907var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001908var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001909var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001910var inList = android.InList