blob: 50c284a94944a875677dab1950a2ac51dd66c222 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Java for Android, and converts the properties
Colin Cross46c9b8b2017-06-22 16:51:17 -070018// into the flags and filenames necessary to pass to the Module. The final creation of the rules
Colin Cross2fe66872015-03-30 17:20:39 -070019// is handled in builder.go
20
21import (
Colin Crossf19b9bb2018-03-26 14:42:44 -070022 "fmt"
Colin Crossfc3674a2017-09-18 17:41:52 -070023 "path/filepath"
Colin Cross74d73e22017-08-02 11:05:49 -070024 "strconv"
Colin Cross2fe66872015-03-30 17:20:39 -070025 "strings"
26
27 "github.com/google/blueprint"
Colin Cross76b5f0c2017-08-29 16:02:06 -070028 "github.com/google/blueprint/proptools"
Colin Cross2fe66872015-03-30 17:20:39 -070029
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross3e3e72d2017-06-22 17:20:19 -070031 "android/soong/java/config"
Colin Cross303e21f2018-08-07 16:49:25 -070032 "android/soong/tradefed"
Colin Cross2fe66872015-03-30 17:20:39 -070033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Cross89536d42017-07-07 14:35:50 -070036 android.RegisterModuleType("java_defaults", defaultsFactory)
37
Colin Cross9ae1b922018-06-26 17:59:05 -070038 android.RegisterModuleType("java_library", LibraryFactory)
39 android.RegisterModuleType("java_library_static", LibraryFactory)
Colin Crossf506d872017-07-19 15:53:04 -070040 android.RegisterModuleType("java_library_host", LibraryHostFactory)
41 android.RegisterModuleType("java_binary", BinaryFactory)
42 android.RegisterModuleType("java_binary_host", BinaryHostFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070043 android.RegisterModuleType("java_test", TestFactory)
44 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070045 android.RegisterModuleType("java_import", ImportFactory)
46 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross463a90e2015-06-17 14:20:06 -070047
Colin Cross798bfce2016-10-12 14:28:16 -070048 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070049}
50
Colin Cross2fe66872015-03-30 17:20:39 -070051// TODO:
52// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070053// Renderscript
54// Post-jar passes:
55// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070056// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070057// DroidDoc
58// Findbugs
59
Colin Cross89536d42017-07-07 14:35:50 -070060type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070061 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
62 // or .aidl files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -070063 Srcs []string `android:"arch_variant"`
64
65 // list of source files that should not be used to build the Java module.
66 // This is most useful in the arch/multilib variants to remove non-common files
67 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070068
69 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070070 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070071
Colin Cross86a63ff2017-09-27 17:33:10 -070072 // list of directories that should be excluded from java_resource_dirs
73 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070074
Colin Cross0f37af02017-09-27 17:42:05 -070075 // list of files to use as Java resources
76 Java_resources []string `android:"arch_variant"`
77
Colin Crosscedd4762018-09-13 11:26:19 -070078 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross0f37af02017-09-27 17:42:05 -070079 Exclude_java_resources []string `android:"arch_variant"`
80
Colin Crossfa5eb232017-10-01 20:33:03 -070081 // don't build against the default libraries (bootclasspath, legacy-test, core-junit,
Colin Cross7d5136f2015-05-11 13:39:40 -070082 // ext, and framework for device targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070083 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070084
Colin Crossfa5eb232017-10-01 20:33:03 -070085 // don't build against the framework libraries (legacy-test, core-junit,
86 // ext, and framework for device targets)
87 No_framework_libs *bool
88
Colin Cross7d5136f2015-05-11 13:39:40 -070089 // list of module-specific flags that will be used for javac compiles
90 Javacflags []string `android:"arch_variant"`
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070093 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070094
95 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070096 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070097
98 // manifest file to be included in resulting jar
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099 Manifest *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
Colin Cross540eff82017-06-22 17:01:52 -0700101 // if not blank, run jarjar using the specified rules file
Colin Cross975f9f72017-10-17 13:55:55 -0700102 Jarjar_rules *string `android:"arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700103
104 // If not blank, set the java version passed to javac as -source and -target
105 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700106
Colin Cross9ae1b922018-06-26 17:59:05 -0700107 // If set to true, allow this module to be dexed and installed on devices. Has no
108 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700109 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700110
Colin Cross0f37af02017-09-27 17:42:05 -0700111 // If set to true, include sources used to compile the module in to the final jar
112 Include_srcs *bool
113
Colin Cross32f676a2017-09-06 13:41:06 -0700114 // List of modules to use as annotation processors
115 Annotation_processors []string
116
117 // List of classes to pass to javac to use as annotation processors
118 Annotation_processor_classes []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700119
Nan Zhang61eaedb2017-11-02 13:28:15 -0700120 // The number of Java source entries each Javac instance can process
121 Javac_shard_size *int64
122
Nan Zhang5f8cb422018-02-06 10:34:32 -0800123 // Add host jdk tools.jar to bootclasspath
124 Use_tools_jar *bool
125
Colin Cross1369cdb2017-09-29 17:58:17 -0700126 Openjdk9 struct {
127 // List of source files that should only be used when passing -source 1.9
128 Srcs []string
129
130 // List of javac flags that should only be used when passing -source 1.9
131 Javacflags []string
132 }
Colin Crosscb933592017-11-22 13:49:43 -0800133
Colin Cross81440082018-08-15 20:21:55 -0700134 // When compiling language level 9+ .java code in packages that are part of
135 // a system module, patch_module names the module that your sources and
136 // dependencies should be patched into. The Android runtime currently
137 // doesn't implement the JEP 261 module system so this option is only
138 // supported at compile time. It should only be needed to compile tests in
139 // packages that exist in libcore and which are inconvenient to move
140 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100141 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700142
Colin Crosscb933592017-11-22 13:49:43 -0800143 Jacoco struct {
144 // List of classes to include for instrumentation with jacoco to collect coverage
145 // information at runtime when building with coverage enabled. If unset defaults to all
146 // classes.
147 // Supports '*' as the last character of an entry in the list as a wildcard match.
148 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
149 // it matches classes in the package that have the class name as a prefix.
150 Include_filter []string
151
152 // List of classes to exclude from instrumentation with jacoco to collect coverage
153 // information at runtime when building with coverage enabled. Overrides classes selected
154 // by the include_filter property.
155 // Supports '*' as the last character of an entry in the list as a wildcard match.
156 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
157 // it matches classes in the package that have the class name as a prefix.
158 Exclude_filter []string
159 }
160
Andreas Gampef3e5b552018-01-22 21:27:21 -0800161 Errorprone struct {
162 // List of javac flags that should only be used when running errorprone.
163 Javacflags []string
164 }
165
Colin Cross0f2ee152017-12-14 15:22:43 -0800166 Proto struct {
167 // List of extra options that will be passed to the proto generator.
168 Output_params []string
169 }
170
Colin Crosscb933592017-11-22 13:49:43 -0800171 Instrument bool `blueprint:"mutated"`
Colin Cross540eff82017-06-22 17:01:52 -0700172}
173
Colin Cross89536d42017-07-07 14:35:50 -0700174type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700175 // list of module-specific flags that will be used for dex compiles
176 Dxflags []string `android:"arch_variant"`
177
Colin Cross83bb3162018-06-25 15:48:06 -0700178 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
179 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800180 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700181
Colin Cross83bb3162018-06-25 15:48:06 -0700182 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
183 // Defaults to sdk_version if not set.
184 Min_sdk_version *string
185
Dan Willemsen419290a2018-10-31 15:28:47 -0700186 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
187 // Defaults to sdk_version if not set.
188 Target_sdk_version *string
189
Colin Cross6af2e492018-05-22 11:12:33 -0700190 // if true, compile against the platform APIs instead of an SDK.
191 Platform_apis *bool
192
Colin Crossebe1a512017-11-14 13:12:14 -0800193 Aidl struct {
194 // Top level directories to pass to aidl tool
195 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700196
Colin Crossebe1a512017-11-14 13:12:14 -0800197 // Directories rooted at the Android.bp file to pass to aidl tool
198 Local_include_dirs []string
199
200 // directories that should be added as include directories for any aidl sources of modules
201 // that depend on this module, as well as to aidl for this module.
202 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100203
204 // whether to generate traces (for systrace) for this interface
205 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100206
207 // whether to generate Binder#GetTransaction name method.
208 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800209 }
Colin Cross92430102017-10-09 14:59:32 -0700210
211 // If true, export a copy of the module as a -hostdex module for host testing.
212 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700213
David Brazdil17ef5632018-06-27 10:27:45 +0100214 // If set to true, compile dex regardless of installable. Defaults to false.
215 Compile_dex *bool
216
Colin Cross1bd87802017-12-05 15:31:19 -0800217 Dex_preopt struct {
218 // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to
219 // true.
220 Enabled *bool
221
222 // If true, generate an app image (.art file) for this module.
223 App_image *bool
224
225 // If true, use a checked-in profile to guide optimization. Defaults to false unless
226 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
227 // that matches the name of this module, in which case it is defaulted to true.
228 Profile_guided *bool
229
230 // If set, provides the path to profile relative to the Android.bp file. If not set,
231 // defaults to searching for a file that matches the name of this module in the default
232 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
233 Profile *string
234 }
Colin Crossa22116e2017-10-19 14:18:58 -0700235
Colin Cross66dbc0b2017-12-28 12:23:20 -0800236 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700237 // If false, disable all optimization. Defaults to true for android_app and android_test
238 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800239 Enabled *bool
240
241 // If true, optimize for size by removing unused code. Defaults to true for apps,
242 // false for libraries and tests.
243 Shrink *bool
244
245 // If true, optimize bytecode. Defaults to false.
246 Optimize *bool
247
248 // If true, obfuscate bytecode. Defaults to false.
249 Obfuscate *bool
250
251 // If true, do not use the flag files generated by aapt that automatically keep
252 // classes referenced by the app manifest. Defaults to false.
253 No_aapt_flags *bool
254
255 // Flags to pass to proguard.
256 Proguard_flags []string
257
258 // Specifies the locations of files containing proguard flags.
259 Proguard_flags_files []string
260 }
261
Colin Cross1369cdb2017-09-29 17:58:17 -0700262 // When targeting 1.9, override the modules to use with --system
263 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700264
265 UncompressDex bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700266}
267
Colin Cross46c9b8b2017-06-22 16:51:17 -0700268// Module contains the properties and members used by all java module types
269type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700270 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700271 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700272
Colin Cross89536d42017-07-07 14:35:50 -0700273 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700274 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700275 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700276
Colin Cross331a1212018-08-15 20:40:52 -0700277 // jar file containing header classes including static library dependencies, suitable for
278 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700279 headerJarFile android.Path
280
Colin Cross331a1212018-08-15 20:40:52 -0700281 // jar file containing implementation classes including static library dependencies but no
282 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700284
Colin Cross331a1212018-08-15 20:40:52 -0700285 // jar file containing only resources including from static library dependencies
286 resourceJar android.Path
287
288 // jar file containing implementation classes and resources including static library
289 // dependencies
290 implementationAndResourcesJar android.Path
291
292 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700293 dexJarFile android.Path
294
Colin Crosscb933592017-11-22 13:49:43 -0800295 // output file containing uninstrumented classes that will be instrumented by jacoco
296 jacocoReportClassesFile android.Path
297
Colin Cross66dbc0b2017-12-28 12:23:20 -0800298 // output file containing mapping of obfuscated names
299 proguardDictionary android.Path
300
Colin Cross331a1212018-08-15 20:40:52 -0700301 // output file of the module, which may be a classes jar or a dex jar
Colin Cross635c3b02016-05-18 15:37:25 -0700302 outputFile android.Path
Colin Crossb7a63242015-04-16 14:09:14 -0700303
Colin Cross635c3b02016-05-18 15:37:25 -0700304 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700305
Colin Cross635c3b02016-05-18 15:37:25 -0700306 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700307
Colin Cross2fe66872015-03-30 17:20:39 -0700308 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700309 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800310
311 // list of .java files and srcjars that was passed to javac
312 compiledJavaSrcs android.Paths
313 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800314
315 // list of extra progurad flag files
316 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900317
Colin Cross094054a2018-10-17 15:10:48 -0700318 // manifest file to use instead of properties.Manifest
319 overrideManifest android.OptionalPath
320
Jiyong Park1be96912018-05-28 18:02:19 +0900321 // list of SDK lib names that this java moudule is exporting
322 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700323
324 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
325 // filter out Exclude_srcs, will be used by android.IDEInfo struct
326 expandIDEInfoCompiledSrcs []string
Colin Cross2fe66872015-03-30 17:20:39 -0700327}
328
Colin Cross54250902017-12-05 09:28:08 -0800329func (j *Module) Srcs() android.Paths {
Colin Cross3063b782018-08-15 11:19:12 -0700330 return android.Paths{j.outputFile}
Colin Cross54250902017-12-05 09:28:08 -0800331}
332
333var _ android.SourceFileProducer = (*Module)(nil)
334
Colin Crossf506d872017-07-19 15:53:04 -0700335type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700336 HeaderJars() android.Paths
337 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700338 ResourceJars() android.Paths
339 ImplementationAndResourcesJars() android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700340 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900341 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700342}
343
Jiyong Parkc678ad32018-04-10 13:07:10 +0900344type SdkLibraryDependency interface {
345 HeaderJars(linkType linkType) android.Paths
Sundong Ahn241cd372018-07-13 16:16:44 +0900346 ImplementationJars(linkType linkType) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900347}
348
Nan Zhangb2b33de2018-02-23 11:18:47 -0800349type SrcDependency interface {
350 CompiledSrcs() android.Paths
351 CompiledSrcJars() android.Paths
352}
353
354func (j *Module) CompiledSrcs() android.Paths {
355 return j.compiledJavaSrcs
356}
357
358func (j *Module) CompiledSrcJars() android.Paths {
359 return j.compiledSrcJars
360}
361
362var _ SrcDependency = (*Module)(nil)
363
Colin Cross89536d42017-07-07 14:35:50 -0700364func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
365 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
366 android.InitDefaultableModule(module)
367}
368
Colin Crossbe1da472017-07-07 15:59:46 -0700369type dependencyTag struct {
370 blueprint.BaseDependencyTag
371 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700372}
373
Colin Crossa4f08812018-10-02 22:03:40 -0700374type jniDependencyTag struct {
375 blueprint.BaseDependencyTag
376 target android.Target
377}
378
Colin Crossbe1da472017-07-07 15:59:46 -0700379var (
Colin Cross4b964c02018-10-15 16:18:06 -0700380 staticLibTag = dependencyTag{name: "staticlib"}
381 libTag = dependencyTag{name: "javalib"}
382 annoTag = dependencyTag{name: "annotation processor"}
383 bootClasspathTag = dependencyTag{name: "bootclasspath"}
384 systemModulesTag = dependencyTag{name: "system modules"}
385 frameworkResTag = dependencyTag{name: "framework-res"}
386 frameworkApkTag = dependencyTag{name: "framework-apk"}
387 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
388 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
389 certificateTag = dependencyTag{name: "certificate"}
390 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700391)
Colin Cross2fe66872015-03-30 17:20:39 -0700392
Colin Crossfc3674a2017-09-18 17:41:52 -0700393type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700394 useModule, useFiles, useDefaultLibs, invalidVersion bool
395
Colin Cross86a60ae2018-05-29 14:44:55 -0700396 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700397 systemModules string
398
Colin Crossa97c5d32018-03-28 14:58:31 -0700399 frameworkResModule string
400
Colin Cross86a60ae2018-05-29 14:44:55 -0700401 jars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700402 aidl android.Path
403}
404
Colin Crossa4f08812018-10-02 22:03:40 -0700405type jniLib struct {
406 name string
407 path android.Path
408 target android.Target
409}
410
Colin Cross3144dfc2018-01-03 15:06:47 -0800411func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
412 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
413}
414
415func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
416 return j.shouldInstrument(ctx) &&
417 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
418 ctx.Config().UnbundledBuild())
419}
420
Colin Cross83bb3162018-06-25 15:48:06 -0700421func (j *Module) sdkVersion() string {
422 return String(j.deviceProperties.Sdk_version)
423}
424
425func (j *Module) minSdkVersion() string {
426 if j.deviceProperties.Min_sdk_version != nil {
427 return *j.deviceProperties.Min_sdk_version
428 }
429 return j.sdkVersion()
430}
431
Dan Willemsen419290a2018-10-31 15:28:47 -0700432func (j *Module) targetSdkVersion() string {
433 if j.deviceProperties.Target_sdk_version != nil {
434 return *j.deviceProperties.Target_sdk_version
435 }
436 return j.sdkVersion()
437}
438
Colin Cross83bb3162018-06-25 15:48:06 -0700439type sdkContext interface {
440 // sdkVersion eturns the sdk_version property of the current module, or an empty string if it is not set.
441 sdkVersion() string
442 // minSdkVersion returns the min_sdk_version property of the current module, or sdkVersion() if it is not set.
443 minSdkVersion() string
Dan Willemsen419290a2018-10-31 15:28:47 -0700444 // targetSdkVersion returns the target_sdk_version property of the current module, or sdkVersion() if it is not set.
445 targetSdkVersion() string
Colin Cross83bb3162018-06-25 15:48:06 -0700446}
447
448func sdkVersionOrDefault(ctx android.BaseContext, v string) string {
449 switch v {
Neil Fuller401eeba2018-10-18 19:48:58 +0100450 case "", "current", "system_current", "test_current", "core_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700451 return ctx.Config().DefaultAppTargetSdk()
452 default:
453 return v
454 }
455}
456
457// Returns a sdk version as a number. For modules targeting an unreleased SDK (meaning it does not yet have a number)
458// it returns android.FutureApiLevel (10000).
459func sdkVersionToNumber(ctx android.BaseContext, v string) (int, error) {
460 switch v {
Neil Fuller401eeba2018-10-18 19:48:58 +0100461 case "", "current", "test_current", "system_current", "core_current":
Colin Cross83bb3162018-06-25 15:48:06 -0700462 return ctx.Config().DefaultAppTargetSdkInt(), nil
463 default:
464 n := android.GetNumericSdkVersion(v)
465 if i, err := strconv.Atoi(n); err != nil {
466 return -1, fmt.Errorf("invalid sdk version %q", n)
467 } else {
468 return i, nil
469 }
470 }
471}
472
473func sdkVersionToNumberAsString(ctx android.BaseContext, v string) (string, error) {
474 n, err := sdkVersionToNumber(ctx, v)
475 if err != nil {
476 return "", err
477 }
478 return strconv.Itoa(n), nil
479}
480
481func decodeSdkDep(ctx android.BaseContext, sdkContext sdkContext) sdkDep {
482 v := sdkContext.sdkVersion()
483 i, err := sdkVersionToNumber(ctx, v)
484 if err != nil {
485 ctx.PropertyErrorf("sdk_version", "%s", err)
Colin Cross1369cdb2017-09-29 17:58:17 -0700486 return sdkDep{}
Colin Crossfc3674a2017-09-18 17:41:52 -0700487 }
488
Jiyong Park1a5d7b12018-01-15 15:05:10 +0900489 // Ensures that the specificed system SDK version is one of BOARD_SYSTEMSDK_VERSIONS (for vendor apks)
490 // or PRODUCT_SYSTEMSDK_VERSIONS (for other apks or when BOARD_SYSTEMSDK_VERSIONS is not set)
491 if strings.HasPrefix(v, "system_") && i != android.FutureApiLevel {
492 allowed_versions := ctx.DeviceConfig().PlatformSystemSdkVersions()
493 if ctx.DeviceSpecific() || ctx.SocSpecific() {
494 if len(ctx.DeviceConfig().SystemSdkVersions()) > 0 {
495 allowed_versions = ctx.DeviceConfig().SystemSdkVersions()
496 }
497 }
498 version := strings.TrimPrefix(v, "system_")
499 if len(allowed_versions) > 0 && !android.InList(version, allowed_versions) {
500 ctx.PropertyErrorf("sdk_version", "incompatible sdk version %q. System SDK version should be one of %q",
501 v, allowed_versions)
502 }
503 }
504
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100505 toPrebuilt := func(sdk string) sdkDep {
506 var api, v string
507 if strings.Contains(sdk, "_") {
508 t := strings.Split(sdk, "_")
509 api = t[0]
510 v = t[1]
511 } else {
512 api = "public"
513 v = sdk
Jiyong Park750e5572018-01-31 00:20:13 +0900514 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100515 dir := filepath.Join("prebuilts", "sdk", v, api)
Colin Crossfc3674a2017-09-18 17:41:52 -0700516 jar := filepath.Join(dir, "android.jar")
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100517 // There's no aidl for other SDKs yet.
518 // TODO(77525052): Add aidl files for other SDKs too.
519 public_dir := filepath.Join("prebuilts", "sdk", v, "public")
520 aidl := filepath.Join(public_dir, "framework.aidl")
Colin Cross32f38982018-02-22 11:47:25 -0800521 jarPath := android.ExistentPathForSource(ctx, jar)
522 aidlPath := android.ExistentPathForSource(ctx, aidl)
Colin Cross86a60ae2018-05-29 14:44:55 -0700523 lambdaStubsPath := android.PathForSource(ctx, config.SdkLambdaStubsPath)
Colin Cross47ff2522017-10-02 14:22:08 -0700524
Colin Cross6510f912017-11-29 00:27:14 -0800525 if (!jarPath.Valid() || !aidlPath.Valid()) && ctx.Config().AllowMissingDependencies() {
Colin Cross47ff2522017-10-02 14:22:08 -0700526 return sdkDep{
527 invalidVersion: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700528 modules: []string{fmt.Sprintf("sdk_%s_%s_android", api, v)},
Colin Cross47ff2522017-10-02 14:22:08 -0700529 }
530 }
531
Colin Crossfc3674a2017-09-18 17:41:52 -0700532 if !jarPath.Valid() {
533 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, jar)
534 return sdkDep{}
535 }
Colin Cross47ff2522017-10-02 14:22:08 -0700536
Colin Crossfc3674a2017-09-18 17:41:52 -0700537 if !aidlPath.Valid() {
538 ctx.PropertyErrorf("sdk_version", "invalid sdk version %q, %q does not exist", v, aidl)
539 return sdkDep{}
540 }
Colin Cross47ff2522017-10-02 14:22:08 -0700541
Colin Crossfc3674a2017-09-18 17:41:52 -0700542 return sdkDep{
543 useFiles: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700544 jars: android.Paths{jarPath.Path(), lambdaStubsPath},
Colin Crossfc3674a2017-09-18 17:41:52 -0700545 aidl: aidlPath.Path(),
546 }
547 }
548
Colin Crossa97c5d32018-03-28 14:58:31 -0700549 toModule := func(m, r string) sdkDep {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700550 ret := sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700551 useModule: true,
Colin Cross86a60ae2018-05-29 14:44:55 -0700552 modules: []string{m, config.DefaultLambdaStubsLibrary},
Colin Crossa97c5d32018-03-28 14:58:31 -0700553 systemModules: m + "_system_modules",
554 frameworkResModule: r,
Colin Crossf19b9bb2018-03-26 14:42:44 -0700555 }
556 if m == "core.current.stubs" {
557 ret.systemModules = "core-system-modules"
Neil Fuller3c979c32018-09-11 09:29:31 +0100558 } else if m == "core.platform.api.stubs" {
559 ret.systemModules = "core-platform-api-stubs-system-modules"
Colin Crossf19b9bb2018-03-26 14:42:44 -0700560 }
561 return ret
562 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700563
Colin Cross6510f912017-11-29 00:27:14 -0800564 if ctx.Config().UnbundledBuild() && v != "" {
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100565 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700566 }
567
568 switch v {
569 case "":
570 return sdkDep{
Colin Crossa97c5d32018-03-28 14:58:31 -0700571 useDefaultLibs: true,
572 frameworkResModule: "framework-res",
Colin Crossfc3674a2017-09-18 17:41:52 -0700573 }
Colin Crossf19b9bb2018-03-26 14:42:44 -0700574 case "current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700575 return toModule("android_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700576 case "system_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700577 return toModule("android_system_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700578 case "test_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700579 return toModule("android_test_stubs_current", "framework-res")
Colin Crossf19b9bb2018-03-26 14:42:44 -0700580 case "core_current":
Colin Crossa97c5d32018-03-28 14:58:31 -0700581 return toModule("core.current.stubs", "")
Colin Crossfc3674a2017-09-18 17:41:52 -0700582 default:
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100583 return toPrebuilt(v)
Colin Crossfc3674a2017-09-18 17:41:52 -0700584 }
585}
586
Colin Crossbe1da472017-07-07 15:59:46 -0700587func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700588 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700589 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700590 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700591 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700592 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100593 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700594 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700595 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700596 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700597 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100598 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700599 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800600 if Bool(j.deviceProperties.Optimize.Enabled) {
Colin Cross42d48b72018-08-29 14:10:52 -0700601 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
602 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800603 }
Colin Crossbe1da472017-07-07 15:59:46 -0700604 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700605 } else if j.deviceProperties.System_modules == nil {
606 ctx.PropertyErrorf("no_standard_libs",
607 "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 +0100608 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700609 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700610 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100611 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700612 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800613 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800614 if ctx.ModuleName() == "android_stubs_current" ||
615 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700616 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700617 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800618 }
Colin Cross2fe66872015-03-30 17:20:39 -0700619 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700620
Colin Cross42d48b72018-08-29 14:10:52 -0700621 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
622 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Mathew Inwood878c6622018-07-02 16:34:51 +0100623 ctx.AddFarVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -0700624 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
Mathew Inwood878c6622018-07-02 16:34:51 +0100625 }, annoTag, j.properties.Annotation_processors...)
Colin Crossa4f08812018-10-02 22:03:40 -0700626
Colin Cross7f9036c2017-08-30 13:27:57 -0700627 android.ExtractSourcesDeps(ctx, j.properties.Srcs)
Nan Zhang27e284d2018-02-09 21:03:53 +0000628 android.ExtractSourcesDeps(ctx, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -0700629 android.ExtractSourcesDeps(ctx, j.properties.Java_resources)
Colin Cross366938f2017-12-11 16:29:02 -0800630 android.ExtractSourceDeps(ctx, j.properties.Manifest)
Colin Cross6af17aa2017-09-20 12:59:05 -0700631
632 if j.hasSrcExt(".proto") {
633 protoDeps(ctx, &j.protoProperties)
634 }
Colin Cross93e85952017-08-15 13:34:18 -0700635
636 if j.hasSrcExt(".kt") {
637 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
638 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700639 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -0700640 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800641
642 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700643 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800644 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700645}
646
647func hasSrcExt(srcs []string, ext string) bool {
648 for _, src := range srcs {
649 if filepath.Ext(src) == ext {
650 return true
651 }
652 }
653
654 return false
655}
656
Nan Zhang61eaedb2017-11-02 13:28:15 -0700657func shardPaths(paths android.Paths, shardSize int) []android.Paths {
658 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
659 for len(paths) > shardSize {
660 ret = append(ret, paths[0:shardSize])
661 paths = paths[shardSize:]
662 }
663 if len(paths) > 0 {
664 ret = append(ret, paths)
665 }
666 return ret
667}
668
Colin Cross6af17aa2017-09-20 12:59:05 -0700669func (j *Module) hasSrcExt(ext string) bool {
670 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700671}
672
Colin Cross46c9b8b2017-06-22 16:51:17 -0700673func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross635c3b02016-05-18 15:37:25 -0700674 aidlIncludeDirs android.Paths) []string {
Colin Crossc0b06f12015-04-08 13:03:43 -0700675
Colin Crossebe1a512017-11-14 13:12:14 -0800676 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
677 aidlIncludes = append(aidlIncludes,
678 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
679 aidlIncludes = append(aidlIncludes,
680 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700681
Steven Moreland667f6882018-07-26 12:55:08 -0700682 flags := []string{"-b"}
683
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700684 if aidlPreprocess.Valid() {
685 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Crossc0b06f12015-04-08 13:03:43 -0700686 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700687 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700688 }
689
Colin Cross635c3b02016-05-18 15:37:25 -0700690 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
Colin Crossebe1a512017-11-14 13:12:14 -0800691 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
Colin Cross635c3b02016-05-18 15:37:25 -0700692 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800693 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700694 flags = append(flags, "-I"+src.String())
695 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700696
Martijn Coeneneab15642018-03-09 09:29:59 +0100697 if Bool(j.deviceProperties.Aidl.Generate_traces) {
698 flags = append(flags, "-t")
699 }
700
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100701 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
702 flags = append(flags, "--transaction_names")
703 }
704
Colin Crossf03c82b2015-04-13 13:53:40 -0700705 return flags
Colin Crossc0b06f12015-04-08 13:03:43 -0700706}
707
Colin Cross32f676a2017-09-06 13:41:06 -0700708type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800709 classpath classpath
710 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700711 processorPath classpath
Colin Cross6ade34f2017-09-15 13:00:47 -0700712 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700713 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700714 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700715 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800716 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700717 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700718 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700719 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700720 kotlinStdlib android.Paths
Colin Cross32f676a2017-09-06 13:41:06 -0700721}
Colin Cross2fe66872015-03-30 17:20:39 -0700722
Colin Cross54250902017-12-05 09:28:08 -0800723func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
724 for _, f := range dep.Srcs() {
725 if f.Ext() != ".jar" {
726 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
727 ctx.OtherModuleName(dep.(blueprint.Module)))
728 }
729 }
730}
731
Jiyong Park2d492942018-03-05 17:44:10 +0900732type linkType int
733
734const (
735 javaCore linkType = iota
736 javaSdk
737 javaSystem
738 javaPlatform
739)
740
Jiyong Park46f78fb2018-10-20 16:33:17 +0900741func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700742 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700743 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900744 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
745 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
746 name == "core-lambda-stubs":
747 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100748 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900749 return javaCore, false
750 case name == "android_system_stubs_current":
751 return javaSystem, true
752 case strings.HasPrefix(ver, "system_"):
753 return javaSystem, false
754 case name == "android_test_stubs_current":
755 return javaSystem, true
756 case strings.HasPrefix(ver, "test_"):
757 return javaPlatform, false
758 case name == "android_stubs_current":
759 return javaSdk, true
760 case ver == "current":
761 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700762 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900763 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700764 default:
765 if _, err := strconv.Atoi(ver); err != nil {
766 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
767 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900768 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900769 }
770}
771
Jiyong Park750e5572018-01-31 00:20:13 +0900772func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700773 if ctx.Host() {
774 return
775 }
776
Jiyong Park46f78fb2018-10-20 16:33:17 +0900777 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
778 if stubs {
779 return
780 }
781 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900782 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."
783
784 switch myLinkType {
785 case javaCore:
786 if otherLinkType != javaCore {
787 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 +0900788 ctx.OtherModuleName(to))
789 }
Jiyong Park2d492942018-03-05 17:44:10 +0900790 break
791 case javaSdk:
792 if otherLinkType != javaCore && otherLinkType != javaSdk {
793 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
794 ctx.OtherModuleName(to))
795 }
796 break
797 case javaSystem:
798 if otherLinkType == javaPlatform {
799 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
800 ctx.OtherModuleName(to))
801 }
802 break
803 case javaPlatform:
804 // no restriction on link-type
805 break
Jiyong Park750e5572018-01-31 00:20:13 +0900806 }
807}
808
Colin Cross32f676a2017-09-06 13:41:06 -0700809func (j *Module) collectDeps(ctx android.ModuleContext) deps {
810 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700811
Colin Cross300f0382018-03-06 13:11:51 -0800812 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700813 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800814 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700815 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800816 } else if sdkDep.useFiles {
817 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700818 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross300f0382018-03-06 13:11:51 -0800819 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, sdkDep.aidl)
820 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700821 }
822
Colin Crossd11fcda2017-10-23 17:59:01 -0700823 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700824 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700825 tag := ctx.OtherModuleDependencyTag(module)
826
Colin Crossa4f08812018-10-02 22:03:40 -0700827 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700828 // Handled by AndroidApp.collectAppDeps
829 return
830 }
831 if tag == certificateTag {
832 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700833 return
834 }
835
Jiyong Park750e5572018-01-31 00:20:13 +0900836 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700837 switch tag {
838 case bootClasspathTag, libTag, staticLibTag:
839 checkLinkType(ctx, j, to, tag.(dependencyTag))
840 }
Jiyong Park750e5572018-01-31 00:20:13 +0900841 }
Colin Cross54250902017-12-05 09:28:08 -0800842 switch dep := module.(type) {
843 case Dependency:
844 switch tag {
845 case bootClasspathTag:
846 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700847 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800848 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900849 // sdk lib names from dependencies are re-exported
850 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross54250902017-12-05 09:28:08 -0800851 case staticLibTag:
852 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
853 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
854 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700855 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
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 Cross6a77c982018-06-19 22:43:34 -0700858 case annoTag:
Colin Cross331a1212018-08-15 20:40:52 -0700859 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
Colin Cross54250902017-12-05 09:28:08 -0800860 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100861 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800862 // framework.jar has a one-off dependency on the R.java and Manifest.java files
863 // generated by framework-res.apk
864 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
865 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800866 case frameworkApkTag:
867 if ctx.ModuleName() == "android_stubs_current" ||
868 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700869 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800870 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
871 // resource files out of there for aapt.
872 //
873 // Normally the package rule runs aapt, which includes the resource,
874 // but we're not running that in our package rule so just copy in the
875 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700876 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800877 }
Colin Cross54250902017-12-05 09:28:08 -0800878 case kotlinStdlibTag:
879 deps.kotlinStdlib = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800880 }
881
882 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900883 case SdkLibraryDependency:
884 switch tag {
885 case libTag:
Jiyong Park46f78fb2018-10-20 16:33:17 +0900886 linkType, _ := getLinkType(j, ctx.ModuleName())
887 deps.classpath = append(deps.classpath, dep.HeaderJars(linkType)...)
Jiyong Park1be96912018-05-28 18:02:19 +0900888 // names of sdk libs that are directly depended are exported
889 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900890 default:
891 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
892 }
Colin Cross54250902017-12-05 09:28:08 -0800893 case android.SourceFileProducer:
894 switch tag {
895 case libTag:
896 checkProducesJars(ctx, dep)
897 deps.classpath = append(deps.classpath, dep.Srcs()...)
898 case staticLibTag:
899 checkProducesJars(ctx, dep)
900 deps.classpath = append(deps.classpath, dep.Srcs()...)
901 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
902 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
903 case android.DefaultsDepTag, android.SourceDepTag:
904 // Nothing to do
905 default:
906 ctx.ModuleErrorf("dependency on genrule %q may only be in srcs, libs, or static_libs", otherName)
907 }
908 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700909 switch tag {
910 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700911 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700912 case systemModulesTag:
913 if deps.systemModules != nil {
914 panic("Found two system module dependencies")
915 }
916 sm := module.(*SystemModules)
917 if sm.outputFile == nil {
918 panic("Missing directory for system module dependency")
919 }
920 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700921 default:
922 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700923 }
Colin Crossec7a0422017-07-07 14:47:12 -0700924 }
Colin Cross2fe66872015-03-30 17:20:39 -0700925 })
926
Jiyong Park1be96912018-05-28 18:02:19 +0900927 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
928
Colin Cross32f676a2017-09-06 13:41:06 -0700929 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700930}
931
Colin Cross83bb3162018-06-25 15:48:06 -0700932func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700933 var ret string
Colin Cross83bb3162018-06-25 15:48:06 -0700934 sdk, err := sdkVersionToNumber(ctx, sdkContext.sdkVersion())
935 if err != nil {
936 ctx.PropertyErrorf("sdk_version", "%s", err)
937 }
Nan Zhang357466b2018-04-17 17:38:36 -0700938 if javaVersion != "" {
939 ret = javaVersion
940 } else if ctx.Device() && sdk <= 23 {
941 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900942 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700943 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700944 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700945 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
946 ret = "1.8"
947 } else {
948 ret = "1.9"
949 }
950
951 return ret
952}
953
Nan Zhanged19fc32017-10-19 13:06:22 -0700954func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700955
Colin Crossf03c82b2015-04-13 13:53:40 -0700956 var flags javaBuilderFlags
957
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100958 // javaVersion flag.
959 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
960
Nan Zhanged19fc32017-10-19 13:06:22 -0700961 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700962 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100963 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700964 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700965 }
Colin Cross6510f912017-11-29 00:27:14 -0800966 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700967 // Override the -g flag passed globally to remove local variable debug info to reduce
968 // disk and memory usage.
969 javacFlags = append(javacFlags, "-g:source,lines")
970 }
Colin Cross64162712017-08-08 13:17:59 -0700971
Colin Cross66548102018-06-19 22:47:35 -0700972 if ctx.Config().RunErrorProne() {
973 if config.ErrorProneClasspath == nil {
974 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
975 }
976
977 errorProneFlags := []string{
978 "-Xplugin:ErrorProne",
979 "${config.ErrorProneChecks}",
980 }
981 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
982
983 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
984 "'" + strings.Join(errorProneFlags, " ") + "'"
985 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800986 }
987
Nan Zhanged19fc32017-10-19 13:06:22 -0700988 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800989 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
990 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700991 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800992
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100993 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800994 !Bool(j.properties.No_standard_libs) &&
995 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
996 // Give host-side tools a version of OpenJDK's standard libraries
997 // close to what they're targeting. As of Dec 2017, AOSP is only
998 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
999 //
1000 // When building with OpenJDK 8, the following should have no
1001 // effect since those jars would be available by default.
1002 //
1003 // When building with OpenJDK 9 but targeting a version < 1.8,
1004 // putting them on the bootclasspath means that:
1005 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
1006 // b) references to existing APIs are not reinterpreted in an
1007 // OpenJDK 9-specific way, eg. calls to subclasses of
1008 // java.nio.Buffer as in http://b/70862583
1009 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
1010 flags.bootClasspath = append(flags.bootClasspath,
1011 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
1012 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -08001013 if Bool(j.properties.Use_tools_jar) {
1014 flags.bootClasspath = append(flags.bootClasspath,
1015 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
1016 }
Colin Cross7fdd2b72018-01-02 18:14:25 -08001017 }
1018
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001019 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Colin Cross81440082018-08-15 20:21:55 -07001020 patchClasspath := ".:" + flags.classpath.FormJavaClassPath("")
1021 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchClasspath)
1022 }
1023
Nan Zhanged19fc32017-10-19 13:06:22 -07001024 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -07001025 if deps.systemModules != nil {
1026 flags.systemModules = append(flags.systemModules, deps.systemModules)
1027 }
1028
Nan Zhanged19fc32017-10-19 13:06:22 -07001029 // aidl flags.
Colin Cross32f676a2017-09-06 13:41:06 -07001030 aidlFlags := j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Crossf03c82b2015-04-13 13:53:40 -07001031 if len(aidlFlags) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001032 // optimization.
Colin Crossf03c82b2015-04-13 13:53:40 -07001033 ctx.Variable(pctx, "aidlFlags", strings.Join(aidlFlags, " "))
1034 flags.aidlFlags = "$aidlFlags"
Colin Cross2fe66872015-03-30 17:20:39 -07001035 }
1036
Colin Cross81440082018-08-15 20:21:55 -07001037 if len(javacFlags) > 0 {
1038 // optimization.
1039 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
1040 flags.javacFlags = "$javacFlags"
1041 }
1042
Nan Zhanged19fc32017-10-19 13:06:22 -07001043 return flags
1044}
Colin Crossc0b06f12015-04-08 13:03:43 -07001045
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001046func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -07001047
Colin Crossebe1a512017-11-14 13:12:14 -08001048 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -07001049
1050 deps := j.collectDeps(ctx)
1051 flags := j.collectBuilderFlags(ctx, deps)
1052
Tobias Thierer06dd04f2018-09-11 16:21:05 +01001053 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -07001054 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
1055 }
1056 srcFiles := ctx.ExpandSources(j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001057 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001058 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001059 }
1060
Colin Crossaf050172017-11-15 23:01:59 -08001061 srcFiles = j.genSources(ctx, srcFiles, flags)
1062
1063 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001064 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001065 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -07001066
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001067 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1068 // that IDEInfo struct will use
1069 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1070
Colin Cross1ee23172017-10-18 14:44:18 -07001071 jarName := ctx.ModuleName() + ".jar"
1072
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001073 javaSrcFiles := srcFiles.FilterByExt(".java")
1074 var uniqueSrcFiles android.Paths
1075 set := make(map[string]bool)
1076 for _, v := range javaSrcFiles {
1077 if _, found := set[v.String()]; !found {
1078 set[v.String()] = true
1079 uniqueSrcFiles = append(uniqueSrcFiles, v)
1080 }
1081 }
1082
Colin Cross55f63ea2018-08-27 12:37:09 -07001083 var kotlinJars android.Paths
1084
Colin Cross93e85952017-08-15 13:34:18 -07001085 if srcFiles.HasExt(".kt") {
1086 // If there are kotlin files, compile them first but pass all the kotlin and java files
1087 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1088 // won't emit any classes for them.
1089
1090 flags.kotlincFlags = "-no-stdlib"
1091 if ctx.Device() {
1092 flags.kotlincFlags += " -no-jdk"
1093 }
1094
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001095 var kotlinSrcFiles android.Paths
1096 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1097 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1098
Przemyslaw Szczepaniake3d26bf2018-03-05 16:06:42 +00001099 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.bootClasspath...)
Colin Cross93e85952017-08-15 13:34:18 -07001100 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.kotlinStdlib...)
1101 flags.kotlincClasspath = append(flags.kotlincClasspath, deps.classpath...)
1102
Colin Cross1ee23172017-10-18 14:44:18 -07001103 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001104 TransformKotlinToClasses(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001105 if ctx.Failed() {
1106 return
1107 }
1108
1109 // Make javac rule depend on the kotlinc rule
Colin Cross49da2752018-06-05 17:22:57 -07001110 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001111 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001112
Colin Cross93e85952017-08-15 13:34:18 -07001113 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001114 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001115 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001116 }
1117
Colin Cross55f63ea2018-08-27 12:37:09 -07001118 jars := append(android.Paths(nil), kotlinJars...)
1119
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001120 // Store the list of .java files that was passed to javac
1121 j.compiledJavaSrcs = uniqueSrcFiles
1122 j.compiledSrcJars = srcJars
1123
Nan Zhang61eaedb2017-11-02 13:28:15 -07001124 enable_sharding := false
Colin Cross6510f912017-11-29 00:27:14 -08001125 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001126 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1127 enable_sharding = true
1128 if len(j.properties.Annotation_processors) != 0 ||
1129 len(j.properties.Annotation_processor_classes) != 0 {
1130 ctx.PropertyErrorf("javac_shard_size",
1131 "%q cannot be set when annotation processors are enabled.",
1132 j.properties.Javac_shard_size)
1133 }
1134 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001135 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001136 if ctx.Failed() {
1137 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001138 }
1139 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001140 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001141 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001142 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001143 // If error-prone is enabled, add an additional rule to compile the java files into
1144 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001145 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001146 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1147 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001148 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001149 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001150 extraJarDeps = append(extraJarDeps, errorprone)
1151 }
1152
Nan Zhang61eaedb2017-11-02 13:28:15 -07001153 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001154 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001155 shardSize := int(*(j.properties.Javac_shard_size))
1156 var shardSrcs []android.Paths
1157 if len(uniqueSrcFiles) > 0 {
1158 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1159 for idx, shardSrc := range shardSrcs {
1160 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1161 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1162 jars = append(jars, classes)
1163 }
1164 }
1165 if len(srcJars) > 0 {
1166 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1167 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1168 jars = append(jars, classes)
1169 }
1170 } else {
1171 classes := android.PathForModuleOut(ctx, "javac", jarName)
1172 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1173 jars = append(jars, classes)
1174 }
Colin Crossd6891432017-09-27 17:39:56 -07001175 if ctx.Failed() {
1176 return
1177 }
Colin Cross2fe66872015-03-30 17:20:39 -07001178 }
1179
Colin Crosscedd4762018-09-13 11:26:19 -07001180 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1181 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001182 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1183
1184 var resArgs []string
1185 var resDeps android.Paths
1186
1187 resArgs = append(resArgs, dirArgs...)
1188 resDeps = append(resDeps, dirDeps...)
1189
1190 resArgs = append(resArgs, fileArgs...)
1191 resDeps = append(resDeps, fileDeps...)
1192
Colin Crossff3ae9d2018-04-10 16:15:18 -07001193 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001194 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001195 resArgs = append(resArgs, srcArgs...)
1196 resDeps = append(resDeps, srcDeps...)
1197 }
Colin Cross40a36712017-09-27 17:41:35 -07001198
1199 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001200 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001201 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001202 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001203 if ctx.Failed() {
1204 return
1205 }
1206 }
1207
Colin Cross331a1212018-08-15 20:40:52 -07001208 if len(deps.staticResourceJars) > 0 {
1209 var jars android.Paths
1210 if j.resourceJar != nil {
1211 jars = append(jars, j.resourceJar)
1212 }
1213 jars = append(jars, deps.staticResourceJars...)
1214
1215 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1216 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1217 false, nil, nil)
1218 j.resourceJar = combinedJar
1219 }
1220
Colin Cross32f676a2017-09-06 13:41:06 -07001221 jars = append(jars, deps.staticJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001222 jars = append(jars, deps.staticResourceJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001223
Colin Cross094054a2018-10-17 15:10:48 -07001224 manifest := j.overrideManifest
1225 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross366938f2017-12-11 16:29:02 -08001226 manifest = android.OptionalPathForPath(ctx.ExpandSource(*j.properties.Manifest, "manifest"))
1227 }
Colin Cross635acc92017-09-12 22:50:46 -07001228
Colin Cross0a6e0072017-08-30 14:24:55 -07001229 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001230 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001231 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001232
1233 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001234 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1235 // Optimization: skip the combine step if there is nothing to do
1236 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1237 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1238 // any if len(jars) == 1.
1239 outputFile = moduleOutPath
1240 } else {
1241 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1242 ctx.Build(pctx, android.BuildParams{
1243 Rule: android.Cp,
1244 Input: jars[0],
1245 Output: combinedJar,
1246 })
1247 outputFile = combinedJar
1248 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001249 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001250 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001251 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001252 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001253 outputFile = combinedJar
1254 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001255
Colin Cross331a1212018-08-15 20:40:52 -07001256 // jarjar implementation jar if necessary
Colin Cross0a6e0072017-08-30 14:24:55 -07001257 if j.properties.Jarjar_rules != nil {
1258 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Colin Cross8649b262017-09-27 18:03:17 -07001259 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001260 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001261 TransformJarJar(ctx, jarjarFile, outputFile, jarjar_rules)
1262 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001263
1264 // jarjar resource jar if necessary
1265 if j.resourceJar != nil {
1266 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
1267 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, jarjar_rules)
1268 j.resourceJar = resourceJarJarFile
1269 }
1270
Colin Cross0a6e0072017-08-30 14:24:55 -07001271 if ctx.Failed() {
1272 return
1273 }
1274 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001275 j.implementationJarFile = outputFile
1276 if j.headerJarFile == nil {
1277 j.headerJarFile = j.implementationJarFile
1278 }
Colin Cross2fe66872015-03-30 17:20:39 -07001279
Colin Cross6510f912017-11-29 00:27:14 -08001280 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001281 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1282 j.properties.Instrument = true
1283 }
1284 }
1285
Colin Cross3144dfc2018-01-03 15:06:47 -08001286 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001287 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1288 }
1289
Colin Cross331a1212018-08-15 20:40:52 -07001290 // merge implementation jar with resources if necessary
1291 implementationAndResourcesJar := outputFile
1292 if j.resourceJar != nil {
1293 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1294 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1295 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1296 false, nil, nil)
1297 implementationAndResourcesJar = combinedJar
1298 }
1299
1300 j.implementationAndResourcesJar = implementationAndResourcesJar
1301
Colin Cross9ae1b922018-06-26 17:59:05 -07001302 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross3063b782018-08-15 11:19:12 -07001303 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001304 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001305 if ctx.Failed() {
1306 return
1307 }
Colin Cross331a1212018-08-15 20:40:52 -07001308
1309 // merge dex jar with resources if necessary
1310 if j.resourceJar != nil {
1311 jars := android.Paths{dexOutputFile, j.resourceJar}
1312 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1313 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1314 false, nil, nil)
1315 dexOutputFile = combinedJar
1316 }
1317
1318 j.dexJarFile = dexOutputFile
1319
Colin Cross3063b782018-08-15 11:19:12 -07001320 outputFile = dexOutputFile
Colin Cross331a1212018-08-15 20:40:52 -07001321 } else {
1322 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001323 }
Colin Cross331a1212018-08-15 20:40:52 -07001324
Colin Crossb7a63242015-04-16 14:09:14 -07001325 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001326
1327 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1328 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001329}
1330
Colin Cross8eadbf02017-10-24 17:46:00 -07001331func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001332 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001333
1334 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001335 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001336 // Compile java sources into turbine.jar.
1337 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1338 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1339 if ctx.Failed() {
1340 return nil
1341 }
1342 jars = append(jars, turbineJar)
1343 }
1344
Colin Cross55f63ea2018-08-27 12:37:09 -07001345 jars = append(jars, extraJars...)
1346
Nan Zhanged19fc32017-10-19 13:06:22 -07001347 // Combine any static header libraries into classes-header.jar. If there is only
1348 // one input jar this step will be skipped.
1349 var headerJar android.Path
1350 jars = append(jars, deps.staticHeaderJars...)
1351
Colin Cross5c6ecc12017-10-23 18:12:27 -07001352 // we cannot skip the combine step for now if there is only one jar
1353 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1354 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001355 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1356 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001357 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001358
1359 if j.properties.Jarjar_rules != nil {
1360 jarjar_rules := android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
1361 // Transform classes.jar into classes-jarjar.jar
1362 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
1363 TransformJarJar(ctx, jarjarFile, headerJar, jarjar_rules)
1364 headerJar = jarjarFile
1365 if ctx.Failed() {
1366 return nil
1367 }
1368 }
1369
1370 return headerJar
1371}
1372
Colin Crosscb933592017-11-22 13:49:43 -08001373func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001374 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001375
Colin Cross7a3139e2017-12-19 13:57:50 -08001376 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001377
Colin Cross84c38822018-01-03 15:59:46 -08001378 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001379 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1380
1381 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1382
1383 j.jacocoReportClassesFile = jacocoReportClassesFile
1384
1385 return instrumentedJar
1386}
1387
Colin Crossf506d872017-07-19 15:53:04 -07001388var _ Dependency = (*Library)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001389
Nan Zhanged19fc32017-10-19 13:06:22 -07001390func (j *Module) HeaderJars() android.Paths {
1391 return android.Paths{j.headerJarFile}
1392}
1393
1394func (j *Module) ImplementationJars() android.Paths {
1395 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001396}
1397
Colin Cross331a1212018-08-15 20:40:52 -07001398func (j *Module) ResourceJars() android.Paths {
1399 if j.resourceJar == nil {
1400 return nil
1401 }
1402 return android.Paths{j.resourceJar}
1403}
1404
1405func (j *Module) ImplementationAndResourcesJars() android.Paths {
1406 return android.Paths{j.implementationAndResourcesJar}
1407}
1408
Colin Cross46c9b8b2017-06-22 16:51:17 -07001409func (j *Module) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001410 return j.exportAidlIncludeDirs
1411}
1412
Jiyong Park1be96912018-05-28 18:02:19 +09001413func (j *Module) ExportedSdkLibs() []string {
1414 return j.exportedSdkLibs
1415}
1416
Colin Cross46c9b8b2017-06-22 16:51:17 -07001417var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001418
Colin Cross46c9b8b2017-06-22 16:51:17 -07001419func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001420 return j.logtagsSrcs
1421}
1422
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001423// Collect information for opening IDE project files in java/jdeps.go.
1424func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1425 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1426 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1427 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
1428 if j.properties.Jarjar_rules != nil {
1429 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, *j.properties.Jarjar_rules)
1430 }
1431}
1432
1433func (j *Module) CompilerDeps() []string {
1434 jdeps := []string{}
1435 jdeps = append(jdeps, j.properties.Libs...)
1436 jdeps = append(jdeps, j.properties.Static_libs...)
1437 return jdeps
1438}
1439
Colin Cross2fe66872015-03-30 17:20:39 -07001440//
1441// Java libraries (.jar file)
1442//
1443
Colin Crossf506d872017-07-19 15:53:04 -07001444type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001445 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001446}
1447
Colin Crossf506d872017-07-19 15:53:04 -07001448func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001449 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001450
Colin Cross9ae1b922018-06-26 17:59:05 -07001451 if Bool(j.properties.Installable) || ctx.Host() {
Colin Cross2c429dc2017-08-31 16:45:16 -07001452 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1453 ctx.ModuleName()+".jar", j.outputFile)
1454 }
Colin Crossb7a63242015-04-16 14:09:14 -07001455}
1456
Colin Crossf506d872017-07-19 15:53:04 -07001457func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001458 j.deps(ctx)
1459}
1460
Colin Cross9ae1b922018-06-26 17:59:05 -07001461func LibraryFactory() android.Module {
1462 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001463
Colin Cross9ae1b922018-06-26 17:59:05 -07001464 module.AddProperties(
1465 &module.Module.properties,
1466 &module.Module.deviceProperties,
1467 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001468
Colin Cross9ae1b922018-06-26 17:59:05 -07001469 InitJavaModule(module, android.HostAndDeviceSupported)
1470 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001471}
1472
Colin Crossf506d872017-07-19 15:53:04 -07001473func LibraryHostFactory() android.Module {
1474 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001475
Colin Cross6af17aa2017-09-20 12:59:05 -07001476 module.AddProperties(
1477 &module.Module.properties,
1478 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001479
Colin Cross9ae1b922018-06-26 17:59:05 -07001480 module.Module.properties.Installable = proptools.BoolPtr(true)
1481
Colin Cross89536d42017-07-07 14:35:50 -07001482 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001483 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001484}
1485
1486//
Colin Crossb628ea52018-08-14 16:42:33 -07001487// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001488//
1489
1490type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001491 // list of compatibility suites (for example "cts", "vts") that the module should be
1492 // installed into.
1493 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001494
1495 // the name of the test configuration (for example "AndroidTest.xml") that should be
1496 // installed with the module.
1497 Test_config *string `android:"arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001498
Jack He33338892018-09-19 02:21:28 -07001499 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1500 // should be installed with the module.
1501 Test_config_template *string `android:"arch_variant"`
1502
Colin Crossd96ca352018-08-10 16:06:24 -07001503 // list of files or filegroup modules that provide data that should be installed alongside
1504 // the test
1505 Data []string
Colin Cross05638fc2018-04-09 18:40:24 -07001506}
1507
1508type Test struct {
1509 Library
1510
1511 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001512
1513 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001514 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001515}
1516
1517func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jack He33338892018-09-19 02:21:28 -07001518 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001519 j.data = ctx.ExpandSources(j.testProperties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -07001520
1521 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001522}
1523
1524func (j *Test) DepsMutator(ctx android.BottomUpMutatorContext) {
1525 j.deps(ctx)
Colin Cross303e21f2018-08-07 16:49:25 -07001526 android.ExtractSourceDeps(ctx, j.testProperties.Test_config)
Jack He33338892018-09-19 02:21:28 -07001527 android.ExtractSourceDeps(ctx, j.testProperties.Test_config_template)
Colin Crossd96ca352018-08-10 16:06:24 -07001528 android.ExtractSourcesDeps(ctx, j.testProperties.Data)
Colin Cross05638fc2018-04-09 18:40:24 -07001529}
1530
1531func TestFactory() android.Module {
1532 module := &Test{}
1533
1534 module.AddProperties(
1535 &module.Module.properties,
1536 &module.Module.deviceProperties,
1537 &module.Module.protoProperties,
1538 &module.testProperties)
1539
Colin Cross9ae1b922018-06-26 17:59:05 -07001540 module.Module.properties.Installable = proptools.BoolPtr(true)
1541
Colin Cross05638fc2018-04-09 18:40:24 -07001542 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001543 return module
1544}
1545
1546func TestHostFactory() android.Module {
1547 module := &Test{}
1548
1549 module.AddProperties(
1550 &module.Module.properties,
1551 &module.Module.protoProperties,
1552 &module.testProperties)
1553
Colin Cross9ae1b922018-06-26 17:59:05 -07001554 module.Module.properties.Installable = proptools.BoolPtr(true)
1555
Colin Cross05638fc2018-04-09 18:40:24 -07001556 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001557 return module
1558}
1559
1560//
Colin Cross2fe66872015-03-30 17:20:39 -07001561// Java Binaries (.jar file plus wrapper script)
1562//
1563
Colin Crossf506d872017-07-19 15:53:04 -07001564type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001565 // installable script to execute the resulting jar
Nan Zhangea568a42017-11-08 21:20:04 -08001566 Wrapper *string
Colin Cross094054a2018-10-17 15:10:48 -07001567
1568 // Name of the class containing main to be inserted into the manifest as Main-Class.
1569 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001570}
1571
Colin Crossf506d872017-07-19 15:53:04 -07001572type Binary struct {
1573 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001574
Colin Crossf506d872017-07-19 15:53:04 -07001575 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001576
Colin Cross6b4a32d2017-12-05 13:42:45 -08001577 isWrapperVariant bool
1578
Colin Crossc3315992017-12-08 19:12:36 -08001579 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001580 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001581}
1582
Alex Light24237172017-10-26 09:46:21 -07001583func (j *Binary) HostToolPath() android.OptionalPath {
1584 return android.OptionalPathForPath(j.binaryFile)
1585}
1586
Colin Crossf506d872017-07-19 15:53:04 -07001587func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001588 if ctx.Arch().ArchType == android.Common {
1589 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001590 if j.binaryProperties.Main_class != nil {
1591 if j.properties.Manifest != nil {
1592 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1593 }
1594 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1595 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1596 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1597 }
1598
Colin Cross6b4a32d2017-12-05 13:42:45 -08001599 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001600 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001601 // Handle the binary wrapper
1602 j.isWrapperVariant = true
1603
Colin Cross366938f2017-12-11 16:29:02 -08001604 if j.binaryProperties.Wrapper != nil {
1605 j.wrapperFile = ctx.ExpandSource(*j.binaryProperties.Wrapper, "wrapper")
Colin Cross6b4a32d2017-12-05 13:42:45 -08001606 } else {
1607 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1608 }
1609
1610 // Depend on the installed jar so that the wrapper doesn't get executed by
1611 // another build rule before the jar has been installed.
1612 jarFile := ctx.PrimaryModule().(*Binary).installFile
1613
1614 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1615 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001616 }
Colin Cross2fe66872015-03-30 17:20:39 -07001617}
1618
Colin Crossf506d872017-07-19 15:53:04 -07001619func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001620 if ctx.Arch().ArchType == android.Common {
1621 j.deps(ctx)
Colin Crossc3315992017-12-08 19:12:36 -08001622 } else {
Colin Cross366938f2017-12-11 16:29:02 -08001623 android.ExtractSourceDeps(ctx, j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001624 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001625}
1626
Colin Crossf506d872017-07-19 15:53:04 -07001627func BinaryFactory() android.Module {
1628 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001629
Colin Cross36242852017-06-23 15:06:31 -07001630 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001631 &module.Module.properties,
1632 &module.Module.deviceProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001633 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001634 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001635
Colin Cross9ae1b922018-06-26 17:59:05 -07001636 module.Module.properties.Installable = proptools.BoolPtr(true)
1637
Colin Cross6b4a32d2017-12-05 13:42:45 -08001638 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1639 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001640 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001641}
1642
Colin Crossf506d872017-07-19 15:53:04 -07001643func BinaryHostFactory() android.Module {
1644 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001645
Colin Cross36242852017-06-23 15:06:31 -07001646 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001647 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001648 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001649 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001650
Colin Cross9ae1b922018-06-26 17:59:05 -07001651 module.Module.properties.Installable = proptools.BoolPtr(true)
1652
Colin Cross6b4a32d2017-12-05 13:42:45 -08001653 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1654 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001655 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001656}
1657
1658//
1659// Java prebuilts
1660//
1661
Colin Cross74d73e22017-08-02 11:05:49 -07001662type ImportProperties struct {
1663 Jars []string
Colin Cross461bd1a2017-10-20 13:59:18 -07001664
Nan Zhangea568a42017-11-08 21:20:04 -08001665 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001666
1667 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001668
1669 // List of shared java libs that this module has dependencies to
1670 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001671
1672 // List of files to remove from the jar file(s)
1673 Exclude_files []string
1674
1675 // List of directories to remove from the jar file(s)
1676 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001677
1678 // if set to true, run Jetifier against .jar file. Defaults to false.
1679 Jetifier_enabled *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001680}
1681
1682type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001683 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001684 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001685 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001686
Colin Cross74d73e22017-08-02 11:05:49 -07001687 properties ImportProperties
1688
Colin Cross0a6e0072017-08-30 14:24:55 -07001689 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001690 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001691}
1692
Colin Cross83bb3162018-06-25 15:48:06 -07001693func (j *Import) sdkVersion() string {
1694 return String(j.properties.Sdk_version)
1695}
1696
1697func (j *Import) minSdkVersion() string {
1698 return j.sdkVersion()
1699}
1700
Colin Cross74d73e22017-08-02 11:05:49 -07001701func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001702 return &j.prebuilt
1703}
1704
Colin Cross74d73e22017-08-02 11:05:49 -07001705func (j *Import) PrebuiltSrcs() []string {
1706 return j.properties.Jars
1707}
1708
1709func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001710 return j.prebuilt.Name(j.ModuleBase.Name())
1711}
1712
Colin Cross74d73e22017-08-02 11:05:49 -07001713func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001714 android.ExtractSourcesDeps(ctx, j.properties.Jars)
Colin Cross42d48b72018-08-29 14:10:52 -07001715 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001716}
1717
Colin Cross74d73e22017-08-02 11:05:49 -07001718func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross37f6d792018-07-12 12:28:41 -07001719 jars := ctx.ExpandSources(j.properties.Jars, nil)
Colin Crosse1d62a82015-04-03 16:53:05 -07001720
Nan Zhang4c819fb2018-08-27 18:31:46 -07001721 jarName := ctx.ModuleName() + ".jar"
1722 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001723 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1724 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Nan Zhang4c819fb2018-08-27 18:31:46 -07001725 if Bool(j.properties.Jetifier_enabled) {
1726 inputFile := outputFile
1727 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1728 TransformJetifier(ctx, outputFile, inputFile)
1729 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001730 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001731
1732 ctx.VisitDirectDeps(func(module android.Module) {
1733 otherName := ctx.OtherModuleName(module)
1734 tag := ctx.OtherModuleDependencyTag(module)
1735
1736 switch dep := module.(type) {
1737 case Dependency:
1738 switch tag {
1739 case libTag, staticLibTag:
1740 // sdk lib names from dependencies are re-exported
1741 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1742 }
1743 case SdkLibraryDependency:
1744 switch tag {
1745 case libTag:
1746 // names of sdk libs that are directly depended are exported
1747 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1748 }
1749 }
1750 })
1751
1752 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001753 if Bool(j.properties.Installable) {
1754 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1755 ctx.ModuleName()+".jar", outputFile)
1756 }
Colin Cross2fe66872015-03-30 17:20:39 -07001757}
1758
Colin Cross74d73e22017-08-02 11:05:49 -07001759var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001760
Nan Zhanged19fc32017-10-19 13:06:22 -07001761func (j *Import) HeaderJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001762 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001763}
1764
1765func (j *Import) ImplementationJars() android.Paths {
Colin Cross37f6d792018-07-12 12:28:41 -07001766 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001767}
1768
Colin Cross331a1212018-08-15 20:40:52 -07001769func (j *Import) ResourceJars() android.Paths {
1770 return nil
1771}
1772
1773func (j *Import) ImplementationAndResourcesJars() android.Paths {
1774 return android.Paths{j.combinedClasspathFile}
1775}
1776
Colin Cross74d73e22017-08-02 11:05:49 -07001777func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001778 return nil
1779}
1780
Jiyong Park1be96912018-05-28 18:02:19 +09001781func (j *Import) ExportedSdkLibs() []string {
1782 return j.exportedSdkLibs
1783}
1784
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001785// Collect information for opening IDE project files in java/jdeps.go.
1786const (
1787 removedPrefix = "prebuilt_"
1788)
1789
1790func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1791 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1792}
1793
1794func (j *Import) IDECustomizedModuleName() string {
1795 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1796 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1797 // solution to get the Import name.
1798 name := j.Name()
1799 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001800 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001801 }
1802 return name
1803}
1804
Colin Cross74d73e22017-08-02 11:05:49 -07001805var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001806
Colin Cross74d73e22017-08-02 11:05:49 -07001807func ImportFactory() android.Module {
1808 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001809
Colin Cross74d73e22017-08-02 11:05:49 -07001810 module.AddProperties(&module.properties)
1811
1812 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001813 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001814 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001815}
1816
Colin Cross74d73e22017-08-02 11:05:49 -07001817func ImportFactoryHost() android.Module {
1818 module := &Import{}
1819
1820 module.AddProperties(&module.properties)
1821
1822 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001823 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001824 return module
1825}
1826
Colin Cross89536d42017-07-07 14:35:50 -07001827//
1828// Defaults
1829//
1830type Defaults struct {
1831 android.ModuleBase
1832 android.DefaultsModuleBase
1833}
1834
1835func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1836}
1837
1838func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1839}
1840
1841func defaultsFactory() android.Module {
1842 return DefaultsFactory()
1843}
1844
1845func DefaultsFactory(props ...interface{}) android.Module {
1846 module := &Defaults{}
1847
1848 module.AddProperties(props...)
1849 module.AddProperties(
1850 &CompilerProperties{},
1851 &CompilerDeviceProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001852 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07001853 &aaptProperties{},
1854 &androidLibraryProperties{},
1855 &appProperties{},
1856 &appTestProperties{},
1857 &ImportProperties{},
1858 &AARImportProperties{},
1859 &sdkLibraryProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07001860 )
1861
1862 android.InitDefaultsModule(module)
1863
1864 return module
1865}
Nan Zhangea568a42017-11-08 21:20:04 -08001866
1867var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001868var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08001869var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08001870var inList = android.InList