blob: b9c06a8e962ccadb3c6c1337274ff66d7aa206c3 [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)
Colin Cross1b16b0e2019-02-12 14:41:32 -080039 android.RegisterModuleType("java_library_static", LibraryStaticFactory)
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)
Paul Duffin42df1442019-03-20 12:45:53 +000044 android.RegisterModuleType("java_test_helper_library", TestHelperLibraryFactory)
Colin Cross05638fc2018-04-09 18:40:24 -070045 android.RegisterModuleType("java_test_host", TestHostFactory)
Colin Cross74d73e22017-08-02 11:05:49 -070046 android.RegisterModuleType("java_import", ImportFactory)
47 android.RegisterModuleType("java_import_host", ImportFactoryHost)
Colin Cross3d7c9822019-03-01 13:46:24 -080048 android.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
49 android.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
Colin Cross42be7612019-02-21 18:12:14 -080050 android.RegisterModuleType("dex_import", DexImportFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070051
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterSingletonType("logtags", LogtagsSingleton)
Colin Cross463a90e2015-06-17 14:20:06 -070053}
54
Colin Cross2fe66872015-03-30 17:20:39 -070055// TODO:
56// Autogenerated files:
Colin Cross2fe66872015-03-30 17:20:39 -070057// Renderscript
58// Post-jar passes:
59// Proguard
Colin Cross2fe66872015-03-30 17:20:39 -070060// Rmtypedefs
Colin Cross2fe66872015-03-30 17:20:39 -070061// DroidDoc
62// Findbugs
63
Colin Cross89536d42017-07-07 14:35:50 -070064type CompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -070065 // list of source files used to compile the Java module. May be .java, .logtags, .proto,
66 // or .aidl files.
Colin Cross27b922f2019-03-04 22:35:41 -080067 Srcs []string `android:"path,arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070068
69 // list of source files that should not be used to build the Java module.
70 // This is most useful in the arch/multilib variants to remove non-common files
Colin Cross27b922f2019-03-04 22:35:41 -080071 Exclude_srcs []string `android:"path,arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070072
73 // list of directories containing Java resources
Colin Cross86a63ff2017-09-27 17:33:10 -070074 Java_resource_dirs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070075
Colin Cross86a63ff2017-09-27 17:33:10 -070076 // list of directories that should be excluded from java_resource_dirs
77 Exclude_java_resource_dirs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -070078
Colin Cross0f37af02017-09-27 17:42:05 -070079 // list of files to use as Java resources
Colin Cross27b922f2019-03-04 22:35:41 -080080 Java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070081
Colin Crosscedd4762018-09-13 11:26:19 -070082 // list of files that should be excluded from java_resources and java_resource_dirs
Colin Cross27b922f2019-03-04 22:35:41 -080083 Exclude_java_resources []string `android:"path,arch_variant"`
Colin Cross0f37af02017-09-27 17:42:05 -070084
Paul Duffin2fbbfb82019-02-13 12:49:33 +000085 // don't build against the default libraries (bootclasspath, ext, and framework for device
86 // targets)
Colin Cross76b5f0c2017-08-29 16:02:06 -070087 No_standard_libs *bool
Colin Cross7d5136f2015-05-11 13:39:40 -070088
Paul Duffin2fbbfb82019-02-13 12:49:33 +000089 // don't build against the framework libraries (ext, and framework for device targets)
Colin Crossfa5eb232017-10-01 20:33:03 -070090 No_framework_libs *bool
91
Colin Cross7d5136f2015-05-11 13:39:40 -070092 // list of module-specific flags that will be used for javac compiles
93 Javacflags []string `android:"arch_variant"`
94
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020095 // list of module-specific flags that will be used for kotlinc compiles
96 Kotlincflags []string `android:"arch_variant"`
97
Colin Cross7d5136f2015-05-11 13:39:40 -070098 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070099 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700100
101 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -0700102 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700103
104 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700106
Colin Cross540eff82017-06-22 17:01:52 -0700107 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700109
110 // If not blank, set the java version passed to javac as -source and -target
111 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700112
Colin Cross9ae1b922018-06-26 17:59:05 -0700113 // If set to true, allow this module to be dexed and installed on devices. Has no
114 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700115 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700116
Colin Cross0f37af02017-09-27 17:42:05 -0700117 // If set to true, include sources used to compile the module in to the final jar
118 Include_srcs *bool
119
Vladimir Marko0975ee02019-04-02 10:29:55 +0100120 // If not empty, classes are restricted to the specified packages and their sub-packages.
121 // This restriction is checked after applying jarjar rules and including static libs.
122 Permitted_packages []string
123
Colin Crossbe9cdb82019-01-21 21:37:16 -0800124 // List of modules to use as annotation processors
125 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700126
Nan Zhang61eaedb2017-11-02 13:28:15 -0700127 // The number of Java source entries each Javac instance can process
128 Javac_shard_size *int64
129
Nan Zhang5f8cb422018-02-06 10:34:32 -0800130 // Add host jdk tools.jar to bootclasspath
131 Use_tools_jar *bool
132
Colin Cross1369cdb2017-09-29 17:58:17 -0700133 Openjdk9 struct {
134 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800135 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700136
137 // List of javac flags that should only be used when passing -source 1.9
138 Javacflags []string
139 }
Colin Crosscb933592017-11-22 13:49:43 -0800140
Colin Cross81440082018-08-15 20:21:55 -0700141 // When compiling language level 9+ .java code in packages that are part of
142 // a system module, patch_module names the module that your sources and
143 // dependencies should be patched into. The Android runtime currently
144 // doesn't implement the JEP 261 module system so this option is only
145 // supported at compile time. It should only be needed to compile tests in
146 // packages that exist in libcore and which are inconvenient to move
147 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100148 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700149
Colin Crosscb933592017-11-22 13:49:43 -0800150 Jacoco struct {
151 // List of classes to include for instrumentation with jacoco to collect coverage
152 // information at runtime when building with coverage enabled. If unset defaults to all
153 // classes.
154 // Supports '*' as the last character of an entry in the list as a wildcard match.
155 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
156 // it matches classes in the package that have the class name as a prefix.
157 Include_filter []string
158
159 // List of classes to exclude from instrumentation with jacoco to collect coverage
160 // information at runtime when building with coverage enabled. Overrides classes selected
161 // by the include_filter property.
162 // Supports '*' as the last character of an entry in the list as a wildcard match.
163 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
164 // it matches classes in the package that have the class name as a prefix.
165 Exclude_filter []string
166 }
167
Andreas Gampef3e5b552018-01-22 21:27:21 -0800168 Errorprone struct {
169 // List of javac flags that should only be used when running errorprone.
170 Javacflags []string
171 }
172
Colin Cross0f2ee152017-12-14 15:22:43 -0800173 Proto struct {
174 // List of extra options that will be passed to the proto generator.
175 Output_params []string
176 }
177
Colin Crosscb933592017-11-22 13:49:43 -0800178 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800179
180 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800181 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700182}
183
Colin Cross89536d42017-07-07 14:35:50 -0700184type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700185 // list of module-specific flags that will be used for dex compiles
186 Dxflags []string `android:"arch_variant"`
187
Colin Cross83bb3162018-06-25 15:48:06 -0700188 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
189 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800190 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700191
Colin Cross83bb3162018-06-25 15:48:06 -0700192 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
193 // Defaults to sdk_version if not set.
194 Min_sdk_version *string
195
Dan Willemsen419290a2018-10-31 15:28:47 -0700196 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
197 // Defaults to sdk_version if not set.
198 Target_sdk_version *string
199
Colin Cross6af2e492018-05-22 11:12:33 -0700200 // if true, compile against the platform APIs instead of an SDK.
201 Platform_apis *bool
202
Colin Crossebe1a512017-11-14 13:12:14 -0800203 Aidl struct {
204 // Top level directories to pass to aidl tool
205 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700206
Colin Crossebe1a512017-11-14 13:12:14 -0800207 // Directories rooted at the Android.bp file to pass to aidl tool
208 Local_include_dirs []string
209
210 // directories that should be added as include directories for any aidl sources of modules
211 // that depend on this module, as well as to aidl for this module.
212 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100213
214 // whether to generate traces (for systrace) for this interface
215 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100216
217 // whether to generate Binder#GetTransaction name method.
218 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800219 }
Colin Cross92430102017-10-09 14:59:32 -0700220
221 // If true, export a copy of the module as a -hostdex module for host testing.
222 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700223
Colin Cross7f87f4f2019-04-24 13:41:45 -0700224 Target struct {
225 Hostdex struct {
226 // Additional required dependencies to add to -hostdex modules.
227 Required []string
228 }
229 }
230
David Brazdil17ef5632018-06-27 10:27:45 +0100231 // If set to true, compile dex regardless of installable. Defaults to false.
232 Compile_dex *bool
233
Colin Cross66dbc0b2017-12-28 12:23:20 -0800234 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700235 // If false, disable all optimization. Defaults to true for android_app and android_test
236 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800237 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700238 // True if the module containing this has it set by default.
239 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800240
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.
Colin Cross27b922f2019-03-04 22:35:41 -0800259 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800260 }
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 Cross43f08db2018-11-12 10:13:39 -0800266 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700267}
268
Sasha Smundak2057f822019-04-16 17:16:58 -0700269func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
270 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
271}
272
Colin Cross46c9b8b2017-06-22 16:51:17 -0700273// Module contains the properties and members used by all java module types
274type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700275 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700276 android.DefaultableModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700277
Colin Cross89536d42017-07-07 14:35:50 -0700278 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700279 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700280 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700281
Colin Cross331a1212018-08-15 20:40:52 -0700282 // jar file containing header classes including static library dependencies, suitable for
283 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 headerJarFile android.Path
285
Colin Cross331a1212018-08-15 20:40:52 -0700286 // jar file containing implementation classes including static library dependencies but no
287 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700288 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700289
Colin Cross331a1212018-08-15 20:40:52 -0700290 // jar file containing only resources including from static library dependencies
291 resourceJar android.Path
292
293 // jar file containing implementation classes and resources including static library
294 // dependencies
295 implementationAndResourcesJar android.Path
296
297 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700298 dexJarFile android.Path
299
Colin Cross43f08db2018-11-12 10:13:39 -0800300 // output file that contains classes.dex if it should be in the output file
301 maybeStrippedDexJarFile android.Path
302
Colin Crosscb933592017-11-22 13:49:43 -0800303 // output file containing uninstrumented classes that will be instrumented by jacoco
304 jacocoReportClassesFile android.Path
305
Colin Cross66dbc0b2017-12-28 12:23:20 -0800306 // output file containing mapping of obfuscated names
307 proguardDictionary android.Path
308
Colin Cross331a1212018-08-15 20:40:52 -0700309 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700310 outputFile android.Path
311 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700312
Colin Cross635c3b02016-05-18 15:37:25 -0700313 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700314
Colin Cross635c3b02016-05-18 15:37:25 -0700315 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700316
Colin Cross2fe66872015-03-30 17:20:39 -0700317 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700318 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800319
320 // list of .java files and srcjars that was passed to javac
321 compiledJavaSrcs android.Paths
322 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800323
324 // list of extra progurad flag files
325 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900326
Colin Cross094054a2018-10-17 15:10:48 -0700327 // manifest file to use instead of properties.Manifest
328 overrideManifest android.OptionalPath
329
Jiyong Park1be96912018-05-28 18:02:19 +0900330 // list of SDK lib names that this java moudule is exporting
331 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700332
333 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
334 // filter out Exclude_srcs, will be used by android.IDEInfo struct
335 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800336
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800337 // expanded Jarjar_rules
338 expandJarjarRules android.Path
339
Vladimir Marko0975ee02019-04-02 10:29:55 +0100340 // list of additional targets for checkbuild
341 additionalCheckedModules android.Paths
342
Colin Cross988708c2019-05-06 14:04:11 -0700343 // Extra files generated by the module type to be added as java resources.
344 extraResources android.Paths
345
Colin Crossf24a22a2019-01-31 14:12:44 -0800346 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800347 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700348}
349
Colin Cross54250902017-12-05 09:28:08 -0800350func (j *Module) Srcs() android.Paths {
Colin Crosse560c4a2019-03-19 16:03:11 -0700351 return append(android.Paths{j.outputFile}, j.extraOutputFiles...)
Colin Cross54250902017-12-05 09:28:08 -0800352}
353
Jiyong Park8fd61922018-11-08 02:50:25 +0900354func (j *Module) DexJarFile() android.Path {
355 return j.dexJarFile
356}
357
Colin Cross54250902017-12-05 09:28:08 -0800358var _ android.SourceFileProducer = (*Module)(nil)
359
Colin Crossf506d872017-07-19 15:53:04 -0700360type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700361 HeaderJars() android.Paths
362 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700363 ResourceJars() android.Paths
364 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800365 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700366 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900367 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700368}
369
Jiyong Parkc678ad32018-04-10 13:07:10 +0900370type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800371 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
372 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900373}
374
Nan Zhangb2b33de2018-02-23 11:18:47 -0800375type SrcDependency interface {
376 CompiledSrcs() android.Paths
377 CompiledSrcJars() android.Paths
378}
379
380func (j *Module) CompiledSrcs() android.Paths {
381 return j.compiledJavaSrcs
382}
383
384func (j *Module) CompiledSrcJars() android.Paths {
385 return j.compiledSrcJars
386}
387
388var _ SrcDependency = (*Module)(nil)
389
Colin Cross89536d42017-07-07 14:35:50 -0700390func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
391 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
392 android.InitDefaultableModule(module)
393}
394
Colin Crossbe1da472017-07-07 15:59:46 -0700395type dependencyTag struct {
396 blueprint.BaseDependencyTag
397 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700398}
399
Colin Crossa4f08812018-10-02 22:03:40 -0700400type jniDependencyTag struct {
401 blueprint.BaseDependencyTag
402 target android.Target
403}
404
Colin Crossbe1da472017-07-07 15:59:46 -0700405var (
Colin Cross4b964c02018-10-15 16:18:06 -0700406 staticLibTag = dependencyTag{name: "staticlib"}
407 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800408 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700409 bootClasspathTag = dependencyTag{name: "bootclasspath"}
410 systemModulesTag = dependencyTag{name: "system modules"}
411 frameworkResTag = dependencyTag{name: "framework-res"}
412 frameworkApkTag = dependencyTag{name: "framework-apk"}
413 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800414 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700415 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
416 certificateTag = dependencyTag{name: "certificate"}
417 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700418)
Colin Cross2fe66872015-03-30 17:20:39 -0700419
Colin Crossfc3674a2017-09-18 17:41:52 -0700420type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700421 useModule, useFiles, useDefaultLibs, invalidVersion bool
422
Colin Cross86a60ae2018-05-29 14:44:55 -0700423 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700424 systemModules string
425
Colin Crossa97c5d32018-03-28 14:58:31 -0700426 frameworkResModule string
427
Colin Cross86a60ae2018-05-29 14:44:55 -0700428 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700429 aidl android.OptionalPath
Colin Cross1369cdb2017-09-29 17:58:17 -0700430}
431
Colin Crossa4f08812018-10-02 22:03:40 -0700432type jniLib struct {
433 name string
434 path android.Path
435 target android.Target
436}
437
Colin Cross3144dfc2018-01-03 15:06:47 -0800438func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
439 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
440}
441
442func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
443 return j.shouldInstrument(ctx) &&
444 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
445 ctx.Config().UnbundledBuild())
446}
447
Colin Cross83bb3162018-06-25 15:48:06 -0700448func (j *Module) sdkVersion() string {
449 return String(j.deviceProperties.Sdk_version)
450}
451
452func (j *Module) minSdkVersion() string {
453 if j.deviceProperties.Min_sdk_version != nil {
454 return *j.deviceProperties.Min_sdk_version
455 }
456 return j.sdkVersion()
457}
458
Dan Willemsen419290a2018-10-31 15:28:47 -0700459func (j *Module) targetSdkVersion() string {
460 if j.deviceProperties.Target_sdk_version != nil {
461 return *j.deviceProperties.Target_sdk_version
462 }
463 return j.sdkVersion()
464}
465
Colin Crossbe1da472017-07-07 15:59:46 -0700466func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700467 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700468 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700469 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700470 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700471 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100472 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700473 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700474 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700475 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700476 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100477 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700478 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700479 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700480 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
481 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800482 }
Colin Crossbe1da472017-07-07 15:59:46 -0700483 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700484 } else if j.deviceProperties.System_modules == nil {
485 ctx.PropertyErrorf("no_standard_libs",
486 "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 +0100487 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700488 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700489 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100490 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700491 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800492 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800493 if ctx.ModuleName() == "android_stubs_current" ||
494 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700495 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700496 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800497 }
Colin Cross2fe66872015-03-30 17:20:39 -0700498 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700499
Colin Cross42d48b72018-08-29 14:10:52 -0700500 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
501 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700502
Colin Crossbe9cdb82019-01-21 21:37:16 -0800503 ctx.AddFarVariationDependencies([]blueprint.Variation{
504 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
505 }, pluginTag, j.properties.Plugins...)
506
Colin Crossfe17f6f2019-03-28 19:30:56 -0700507 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700508 if j.hasSrcExt(".proto") {
509 protoDeps(ctx, &j.protoProperties)
510 }
Colin Cross93e85952017-08-15 13:34:18 -0700511
512 if j.hasSrcExt(".kt") {
513 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
514 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700515 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800516 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800517 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
518 }
Colin Cross93e85952017-08-15 13:34:18 -0700519 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800520
521 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700522 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800523 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700524}
525
526func hasSrcExt(srcs []string, ext string) bool {
527 for _, src := range srcs {
528 if filepath.Ext(src) == ext {
529 return true
530 }
531 }
532
533 return false
534}
535
Nan Zhang61eaedb2017-11-02 13:28:15 -0700536func shardPaths(paths android.Paths, shardSize int) []android.Paths {
537 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
538 for len(paths) > shardSize {
539 ret = append(ret, paths[0:shardSize])
540 paths = paths[shardSize:]
541 }
542 if len(paths) > 0 {
543 ret = append(ret, paths)
544 }
545 return ret
546}
547
Colin Cross6af17aa2017-09-20 12:59:05 -0700548func (j *Module) hasSrcExt(ext string) bool {
549 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700550}
551
Colin Cross46c9b8b2017-06-22 16:51:17 -0700552func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700553 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700554
Colin Crossebe1a512017-11-14 13:12:14 -0800555 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
556 aidlIncludes = append(aidlIncludes,
557 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
558 aidlIncludes = append(aidlIncludes,
559 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700560
Colin Cross3047fa22019-04-18 10:56:44 -0700561 var flags []string
562 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700563
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700564 if aidlPreprocess.Valid() {
565 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700566 deps = append(deps, aidlPreprocess.Path())
567 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700568 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700569 }
570
Colin Cross3047fa22019-04-18 10:56:44 -0700571 if len(j.exportAidlIncludeDirs) > 0 {
572 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
573 }
574
575 if len(aidlIncludes) > 0 {
576 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
577 }
578
Colin Cross635c3b02016-05-18 15:37:25 -0700579 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800580 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700581 flags = append(flags, "-I"+src.String())
582 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700583
Martijn Coeneneab15642018-03-09 09:29:59 +0100584 if Bool(j.deviceProperties.Aidl.Generate_traces) {
585 flags = append(flags, "-t")
586 }
587
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100588 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
589 flags = append(flags, "--transaction_names")
590 }
591
Colin Cross3047fa22019-04-18 10:56:44 -0700592 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700593}
594
Colin Cross32f676a2017-09-06 13:41:06 -0700595type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800596 classpath classpath
597 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700598 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800599 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700600 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700601 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700602 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700603 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800604 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700605 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700606 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700607 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700608 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800609 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800610
611 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700612}
Colin Cross2fe66872015-03-30 17:20:39 -0700613
Colin Cross54250902017-12-05 09:28:08 -0800614func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
615 for _, f := range dep.Srcs() {
616 if f.Ext() != ".jar" {
617 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
618 ctx.OtherModuleName(dep.(blueprint.Module)))
619 }
620 }
621}
622
Jiyong Park2d492942018-03-05 17:44:10 +0900623type linkType int
624
625const (
626 javaCore linkType = iota
627 javaSdk
628 javaSystem
629 javaPlatform
630)
631
Jiyong Park46f78fb2018-10-20 16:33:17 +0900632func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700633 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700634 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900635 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
636 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
637 name == "core-lambda-stubs":
638 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100639 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900640 return javaCore, false
641 case name == "android_system_stubs_current":
642 return javaSystem, true
643 case strings.HasPrefix(ver, "system_"):
644 return javaSystem, false
645 case name == "android_test_stubs_current":
646 return javaSystem, true
647 case strings.HasPrefix(ver, "test_"):
648 return javaPlatform, false
649 case name == "android_stubs_current":
650 return javaSdk, true
651 case ver == "current":
652 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700653 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900654 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700655 default:
656 if _, err := strconv.Atoi(ver); err != nil {
657 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
658 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900659 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900660 }
661}
662
Jiyong Park750e5572018-01-31 00:20:13 +0900663func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700664 if ctx.Host() {
665 return
666 }
667
Jiyong Park46f78fb2018-10-20 16:33:17 +0900668 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
669 if stubs {
670 return
671 }
672 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900673 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."
674
675 switch myLinkType {
676 case javaCore:
677 if otherLinkType != javaCore {
678 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 +0900679 ctx.OtherModuleName(to))
680 }
Jiyong Park2d492942018-03-05 17:44:10 +0900681 break
682 case javaSdk:
683 if otherLinkType != javaCore && otherLinkType != javaSdk {
684 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
685 ctx.OtherModuleName(to))
686 }
687 break
688 case javaSystem:
689 if otherLinkType == javaPlatform {
690 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
691 ctx.OtherModuleName(to))
692 }
693 break
694 case javaPlatform:
695 // no restriction on link-type
696 break
Jiyong Park750e5572018-01-31 00:20:13 +0900697 }
698}
699
Colin Cross32f676a2017-09-06 13:41:06 -0700700func (j *Module) collectDeps(ctx android.ModuleContext) deps {
701 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700702
Colin Cross300f0382018-03-06 13:11:51 -0800703 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700704 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800705 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700706 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800707 } else if sdkDep.useFiles {
708 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700709 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700710 deps.aidlPreprocess = sdkDep.aidl
711 } else {
712 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800713 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700714 }
715
Colin Crossd11fcda2017-10-23 17:59:01 -0700716 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700717 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700718 tag := ctx.OtherModuleDependencyTag(module)
719
Colin Crossa4f08812018-10-02 22:03:40 -0700720 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700721 // Handled by AndroidApp.collectAppDeps
722 return
723 }
724 if tag == certificateTag {
725 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700726 return
727 }
728
Jiyong Park750e5572018-01-31 00:20:13 +0900729 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700730 switch tag {
731 case bootClasspathTag, libTag, staticLibTag:
732 checkLinkType(ctx, j, to, tag.(dependencyTag))
733 }
Jiyong Park750e5572018-01-31 00:20:13 +0900734 }
Colin Cross54250902017-12-05 09:28:08 -0800735 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800736 case SdkLibraryDependency:
737 switch tag {
738 case libTag:
739 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
740 // names of sdk libs that are directly depended are exported
741 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700742 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800743 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
744 }
Colin Cross54250902017-12-05 09:28:08 -0800745 case Dependency:
746 switch tag {
747 case bootClasspathTag:
748 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700749 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800750 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900751 // sdk lib names from dependencies are re-exported
752 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700753 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800754 case staticLibTag:
755 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
756 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
757 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700758 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900759 // sdk lib names from dependencies are re-exported
760 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700761 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800762 case pluginTag:
763 if plugin, ok := dep.(*Plugin); ok {
764 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
765 if plugin.pluginProperties.Processor_class != nil {
766 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
767 }
768 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
769 } else {
770 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
771 }
Colin Cross54250902017-12-05 09:28:08 -0800772 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100773 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800774 // framework.jar has a one-off dependency on the R.java and Manifest.java files
775 // generated by framework-res.apk
776 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
777 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800778 case frameworkApkTag:
779 if ctx.ModuleName() == "android_stubs_current" ||
780 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700781 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800782 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
783 // resource files out of there for aapt.
784 //
785 // Normally the package rule runs aapt, which includes the resource,
786 // but we're not running that in our package rule so just copy in the
787 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700788 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800789 }
Colin Cross54250902017-12-05 09:28:08 -0800790 case kotlinStdlibTag:
791 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800792 case kotlinAnnotationsTag:
793 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800794 }
795
Colin Cross54250902017-12-05 09:28:08 -0800796 case android.SourceFileProducer:
797 switch tag {
798 case libTag:
799 checkProducesJars(ctx, dep)
800 deps.classpath = append(deps.classpath, dep.Srcs()...)
801 case staticLibTag:
802 checkProducesJars(ctx, dep)
803 deps.classpath = append(deps.classpath, dep.Srcs()...)
804 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
805 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800806 }
807 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700808 switch tag {
809 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700810 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700811 case systemModulesTag:
812 if deps.systemModules != nil {
813 panic("Found two system module dependencies")
814 }
815 sm := module.(*SystemModules)
816 if sm.outputFile == nil {
817 panic("Missing directory for system module dependency")
818 }
819 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700820 default:
821 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700822 }
Colin Crossec7a0422017-07-07 14:47:12 -0700823 }
Colin Cross2fe66872015-03-30 17:20:39 -0700824 })
825
Jiyong Park1be96912018-05-28 18:02:19 +0900826 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
827
Colin Cross32f676a2017-09-06 13:41:06 -0700828 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700829}
830
Colin Cross83bb3162018-06-25 15:48:06 -0700831func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700832 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800833 v := sdkContext.sdkVersion()
834 // For PDK builds, use the latest SDK version instead of "current"
835 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700836 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800837 latestSdkVersion := 0
838 if len(sdkVersions) > 0 {
839 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
840 }
841 v = strconv.Itoa(latestSdkVersion)
842 }
843
844 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700845 if err != nil {
846 ctx.PropertyErrorf("sdk_version", "%s", err)
847 }
Nan Zhang357466b2018-04-17 17:38:36 -0700848 if javaVersion != "" {
849 ret = javaVersion
850 } else if ctx.Device() && sdk <= 23 {
851 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900852 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700853 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700854 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700855 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
856 ret = "1.8"
857 } else {
858 ret = "1.9"
859 }
860
861 return ret
862}
863
Nan Zhanged19fc32017-10-19 13:06:22 -0700864func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700865
Colin Crossf03c82b2015-04-13 13:53:40 -0700866 var flags javaBuilderFlags
867
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100868 // javaVersion flag.
869 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
870
Nan Zhanged19fc32017-10-19 13:06:22 -0700871 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700872 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100873 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700874 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700875 }
Colin Cross6510f912017-11-29 00:27:14 -0800876 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700877 // Override the -g flag passed globally to remove local variable debug info to reduce
878 // disk and memory usage.
879 javacFlags = append(javacFlags, "-g:source,lines")
880 }
Colin Cross64162712017-08-08 13:17:59 -0700881
Colin Cross66548102018-06-19 22:47:35 -0700882 if ctx.Config().RunErrorProne() {
883 if config.ErrorProneClasspath == nil {
884 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
885 }
886
887 errorProneFlags := []string{
888 "-Xplugin:ErrorProne",
889 "${config.ErrorProneChecks}",
890 }
891 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
892
893 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
894 "'" + strings.Join(errorProneFlags, " ") + "'"
895 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800896 }
897
Nan Zhanged19fc32017-10-19 13:06:22 -0700898 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800899 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
900 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700901 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800902
Colin Crossbe9cdb82019-01-21 21:37:16 -0800903 flags.processor = strings.Join(deps.processorClasses, ",")
904
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100905 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800906 !Bool(j.properties.No_standard_libs) &&
907 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
908 // Give host-side tools a version of OpenJDK's standard libraries
909 // close to what they're targeting. As of Dec 2017, AOSP is only
910 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
911 //
912 // When building with OpenJDK 8, the following should have no
913 // effect since those jars would be available by default.
914 //
915 // When building with OpenJDK 9 but targeting a version < 1.8,
916 // putting them on the bootclasspath means that:
917 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
918 // b) references to existing APIs are not reinterpreted in an
919 // OpenJDK 9-specific way, eg. calls to subclasses of
920 // java.nio.Buffer as in http://b/70862583
921 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
922 flags.bootClasspath = append(flags.bootClasspath,
923 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
924 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800925 if Bool(j.properties.Use_tools_jar) {
926 flags.bootClasspath = append(flags.bootClasspath,
927 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
928 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800929 }
930
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100931 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800932 // Manually specify build directory in case it is not under the repo root.
933 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
934 // just adding a symlink under the root doesn't help.)
935 patchPaths := ".:" + ctx.Config().BuildDir()
936 classPath := flags.classpath.FormJavaClassPath("")
937 if classPath != "" {
938 patchPaths += ":" + classPath
939 }
940 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700941 }
942
Nan Zhanged19fc32017-10-19 13:06:22 -0700943 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700944 if deps.systemModules != nil {
945 flags.systemModules = append(flags.systemModules, deps.systemModules)
946 }
947
Nan Zhanged19fc32017-10-19 13:06:22 -0700948 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700949 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700950
Colin Cross81440082018-08-15 20:21:55 -0700951 if len(javacFlags) > 0 {
952 // optimization.
953 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
954 flags.javacFlags = "$javacFlags"
955 }
956
Nan Zhanged19fc32017-10-19 13:06:22 -0700957 return flags
958}
Colin Crossc0b06f12015-04-08 13:03:43 -0700959
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800960func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700961
Colin Crossebe1a512017-11-14 13:12:14 -0800962 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700963
964 deps := j.collectDeps(ctx)
965 flags := j.collectBuilderFlags(ctx, deps)
966
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100967 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700968 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
969 }
Colin Cross8a497952019-03-05 22:25:09 -0800970 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700971 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800972 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700973 }
974
Colin Crossaf050172017-11-15 23:01:59 -0800975 srcFiles = j.genSources(ctx, srcFiles, flags)
976
977 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700978 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800979 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700980
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700981 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
982 // that IDEInfo struct will use
983 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
984
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800985 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800986 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800987 }
988
Colin Cross1ee23172017-10-18 14:44:18 -0700989 jarName := ctx.ModuleName() + ".jar"
990
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +0000991 javaSrcFiles := srcFiles.FilterByExt(".java")
992 var uniqueSrcFiles android.Paths
993 set := make(map[string]bool)
994 for _, v := range javaSrcFiles {
995 if _, found := set[v.String()]; !found {
996 set[v.String()] = true
997 uniqueSrcFiles = append(uniqueSrcFiles, v)
998 }
999 }
1000
Colin Cross55f63ea2018-08-27 12:37:09 -07001001 var kotlinJars android.Paths
1002
Colin Cross93e85952017-08-15 13:34:18 -07001003 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001004 // user defined kotlin flags.
1005 kotlincFlags := j.properties.Kotlincflags
1006 CheckKotlincFlags(ctx, kotlincFlags)
1007
Colin Cross93e85952017-08-15 13:34:18 -07001008 // If there are kotlin files, compile them first but pass all the kotlin and java files
1009 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1010 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001011 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001012 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001013 kotlincFlags = append(kotlincFlags, "-no-jdk")
1014 }
1015 if len(kotlincFlags) > 0 {
1016 // optimization.
1017 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1018 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001019 }
1020
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001021 var kotlinSrcFiles android.Paths
1022 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1023 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1024
Colin Crossafbb1732019-01-17 15:42:52 -08001025 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1026 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1027
1028 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1029 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1030
1031 if len(flags.processorPath) > 0 {
1032 // Use kapt for annotation processing
1033 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1034 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1035 srcJars = append(srcJars, kaptSrcJar)
1036 // Disable annotation processing in javac, it's already been handled by kapt
1037 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001038 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001039 }
Colin Cross93e85952017-08-15 13:34:18 -07001040
Colin Cross1ee23172017-10-18 14:44:18 -07001041 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001042 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001043 if ctx.Failed() {
1044 return
1045 }
1046
1047 // Make javac rule depend on the kotlinc rule
1048 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001049
Colin Cross93e85952017-08-15 13:34:18 -07001050 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001051 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001052 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001053 }
1054
Colin Cross55f63ea2018-08-27 12:37:09 -07001055 jars := append(android.Paths(nil), kotlinJars...)
1056
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001057 // Store the list of .java files that was passed to javac
1058 j.compiledJavaSrcs = uniqueSrcFiles
1059 j.compiledSrcJars = srcJars
1060
Nan Zhang61eaedb2017-11-02 13:28:15 -07001061 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001062 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001063 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1064 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001065 // Formerly, there was a check here that prevented annotation processors
1066 // from being used when sharding was enabled, as some annotation processors
1067 // do not function correctly in sharded environments. It was removed to
1068 // allow for the use of annotation processors that do function correctly
1069 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001070 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001071 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001072 if ctx.Failed() {
1073 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001074 }
1075 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001076 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001077 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001078 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001079 // If error-prone is enabled, add an additional rule to compile the java files into
1080 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001081 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001082 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1083 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001084 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001085 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001086 extraJarDeps = append(extraJarDeps, errorprone)
1087 }
1088
Nan Zhang61eaedb2017-11-02 13:28:15 -07001089 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001090 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001091 shardSize := int(*(j.properties.Javac_shard_size))
1092 var shardSrcs []android.Paths
1093 if len(uniqueSrcFiles) > 0 {
1094 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1095 for idx, shardSrc := range shardSrcs {
1096 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1097 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1098 jars = append(jars, classes)
1099 }
1100 }
1101 if len(srcJars) > 0 {
1102 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1103 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1104 jars = append(jars, classes)
1105 }
1106 } else {
1107 classes := android.PathForModuleOut(ctx, "javac", jarName)
1108 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1109 jars = append(jars, classes)
1110 }
Colin Crossd6891432017-09-27 17:39:56 -07001111 if ctx.Failed() {
1112 return
1113 }
Colin Cross2fe66872015-03-30 17:20:39 -07001114 }
1115
Colin Crosscedd4762018-09-13 11:26:19 -07001116 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1117 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001118 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001119 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001120
1121 var resArgs []string
1122 var resDeps android.Paths
1123
1124 resArgs = append(resArgs, dirArgs...)
1125 resDeps = append(resDeps, dirDeps...)
1126
1127 resArgs = append(resArgs, fileArgs...)
1128 resDeps = append(resDeps, fileDeps...)
1129
Colin Cross988708c2019-05-06 14:04:11 -07001130 resArgs = append(resArgs, extraArgs...)
1131 resDeps = append(resDeps, extraDeps...)
1132
Colin Crossff3ae9d2018-04-10 16:15:18 -07001133 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001134 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001135 resArgs = append(resArgs, srcArgs...)
1136 resDeps = append(resDeps, srcDeps...)
1137 }
Colin Cross40a36712017-09-27 17:41:35 -07001138
1139 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001140 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001141 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001142 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001143 if ctx.Failed() {
1144 return
1145 }
1146 }
1147
Colin Cross331a1212018-08-15 20:40:52 -07001148 if len(deps.staticResourceJars) > 0 {
1149 var jars android.Paths
1150 if j.resourceJar != nil {
1151 jars = append(jars, j.resourceJar)
1152 }
1153 jars = append(jars, deps.staticResourceJars...)
1154
1155 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1156 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1157 false, nil, nil)
1158 j.resourceJar = combinedJar
1159 }
1160
Colin Cross32f676a2017-09-06 13:41:06 -07001161 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001162
Colin Cross094054a2018-10-17 15:10:48 -07001163 manifest := j.overrideManifest
1164 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001165 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001166 }
Colin Cross635acc92017-09-12 22:50:46 -07001167
Colin Cross8a497952019-03-05 22:25:09 -08001168 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001169 if len(services) > 0 {
1170 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1171 var zipargs []string
1172 for _, file := range services {
1173 serviceFile := file.String()
1174 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1175 }
1176 ctx.Build(pctx, android.BuildParams{
1177 Rule: zip,
1178 Output: servicesJar,
1179 Implicits: services,
1180 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001181 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001182 },
1183 })
1184 jars = append(jars, servicesJar)
1185 }
1186
Colin Cross0a6e0072017-08-30 14:24:55 -07001187 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001188 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001189 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001190
1191 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001192 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1193 // Optimization: skip the combine step if there is nothing to do
1194 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1195 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1196 // any if len(jars) == 1.
1197 outputFile = moduleOutPath
1198 } else {
1199 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1200 ctx.Build(pctx, android.BuildParams{
1201 Rule: android.Cp,
1202 Input: jars[0],
1203 Output: combinedJar,
1204 })
1205 outputFile = combinedJar
1206 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001207 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001208 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001209 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001210 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001211 outputFile = combinedJar
1212 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001213
Colin Cross331a1212018-08-15 20:40:52 -07001214 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001215 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001216 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001217 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001218 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001219 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001220
1221 // jarjar resource jar if necessary
1222 if j.resourceJar != nil {
1223 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001224 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001225 j.resourceJar = resourceJarJarFile
1226 }
1227
Colin Cross0a6e0072017-08-30 14:24:55 -07001228 if ctx.Failed() {
1229 return
1230 }
1231 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001232
1233 // Check package restrictions if necessary.
1234 if len(j.properties.Permitted_packages) > 0 {
1235 // Check packages and copy to package-checked file.
1236 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1237 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1238 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1239
1240 if ctx.Failed() {
1241 return
1242 }
1243 }
1244
Nan Zhanged19fc32017-10-19 13:06:22 -07001245 j.implementationJarFile = outputFile
1246 if j.headerJarFile == nil {
1247 j.headerJarFile = j.implementationJarFile
1248 }
Colin Cross2fe66872015-03-30 17:20:39 -07001249
Colin Cross6510f912017-11-29 00:27:14 -08001250 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001251 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1252 j.properties.Instrument = true
1253 }
1254 }
1255
Colin Cross3144dfc2018-01-03 15:06:47 -08001256 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001257 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1258 }
1259
Colin Cross331a1212018-08-15 20:40:52 -07001260 // merge implementation jar with resources if necessary
1261 implementationAndResourcesJar := outputFile
1262 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001263 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001264 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001265 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001266 false, nil, nil)
1267 implementationAndResourcesJar = combinedJar
1268 }
1269
1270 j.implementationAndResourcesJar = implementationAndResourcesJar
1271
Colin Cross9ae1b922018-06-26 17:59:05 -07001272 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001273 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001274 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001275 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001276 if ctx.Failed() {
1277 return
1278 }
Colin Cross331a1212018-08-15 20:40:52 -07001279
Colin Cross8faf8fc2019-01-16 15:15:52 -08001280 // Hidden API CSV generation and dex encoding
Colin Crossf24a22a2019-01-31 14:12:44 -08001281 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1282 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001283
Colin Cross331a1212018-08-15 20:40:52 -07001284 // merge dex jar with resources if necessary
1285 if j.resourceJar != nil {
1286 jars := android.Paths{dexOutputFile, j.resourceJar}
1287 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1288 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1289 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001290 if j.deviceProperties.UncompressDex {
1291 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1292 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1293 dexOutputFile = combinedAlignedJar
1294 } else {
1295 dexOutputFile = combinedJar
1296 }
Colin Cross331a1212018-08-15 20:40:52 -07001297 }
1298
1299 j.dexJarFile = dexOutputFile
1300
Colin Cross8faf8fc2019-01-16 15:15:52 -08001301 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001302 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1303
1304 j.maybeStrippedDexJarFile = dexOutputFile
1305
Colin Cross3063b782018-08-15 11:19:12 -07001306 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001307
1308 if ctx.Failed() {
1309 return
1310 }
Colin Cross331a1212018-08-15 20:40:52 -07001311 } else {
1312 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001313 }
Colin Cross331a1212018-08-15 20:40:52 -07001314
Colin Crossb7a63242015-04-16 14:09:14 -07001315 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001316
1317 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1318 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001319}
1320
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001321// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1322// since some of these flags may be used internally.
1323func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1324 for _, flag := range flags {
1325 flag = strings.TrimSpace(flag)
1326
1327 if !strings.HasPrefix(flag, "-") {
1328 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1329 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1330 ctx.PropertyErrorf("kotlincflags",
1331 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1332 } else if inList(flag, config.KotlincIllegalFlags) {
1333 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1334 } else if flag == "-include-runtime" {
1335 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1336 } else {
1337 args := strings.Split(flag, " ")
1338 if args[0] == "-kotlin-home" {
1339 ctx.PropertyErrorf("kotlincflags",
1340 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1341 }
1342 }
1343 }
1344}
1345
Colin Cross8eadbf02017-10-24 17:46:00 -07001346func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001347 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001348
1349 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001350 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001351 // Compile java sources into turbine.jar.
1352 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1353 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1354 if ctx.Failed() {
1355 return nil
1356 }
1357 jars = append(jars, turbineJar)
1358 }
1359
Colin Cross55f63ea2018-08-27 12:37:09 -07001360 jars = append(jars, extraJars...)
1361
Nan Zhanged19fc32017-10-19 13:06:22 -07001362 // Combine any static header libraries into classes-header.jar. If there is only
1363 // one input jar this step will be skipped.
1364 var headerJar android.Path
1365 jars = append(jars, deps.staticHeaderJars...)
1366
Colin Cross5c6ecc12017-10-23 18:12:27 -07001367 // we cannot skip the combine step for now if there is only one jar
1368 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1369 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001370 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1371 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001372 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001373
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001374 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001375 // Transform classes.jar into classes-jarjar.jar
1376 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001377 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001378 headerJar = jarjarFile
1379 if ctx.Failed() {
1380 return nil
1381 }
1382 }
1383
1384 return headerJar
1385}
1386
Colin Crosscb933592017-11-22 13:49:43 -08001387func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001388 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001389
Colin Cross7a3139e2017-12-19 13:57:50 -08001390 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001391
Colin Cross84c38822018-01-03 15:59:46 -08001392 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001393 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1394
1395 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1396
1397 j.jacocoReportClassesFile = jacocoReportClassesFile
1398
1399 return instrumentedJar
1400}
1401
albaltai36ff7dc2018-12-25 14:35:23 +08001402var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001403
Nan Zhanged19fc32017-10-19 13:06:22 -07001404func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001405 if j.headerJarFile == nil {
1406 return nil
1407 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001408 return android.Paths{j.headerJarFile}
1409}
1410
1411func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001412 if j.implementationJarFile == nil {
1413 return nil
1414 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001415 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001416}
1417
Colin Crossf24a22a2019-01-31 14:12:44 -08001418func (j *Module) DexJar() android.Path {
1419 return j.dexJarFile
1420}
1421
Colin Cross331a1212018-08-15 20:40:52 -07001422func (j *Module) ResourceJars() android.Paths {
1423 if j.resourceJar == nil {
1424 return nil
1425 }
1426 return android.Paths{j.resourceJar}
1427}
1428
1429func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001430 if j.implementationAndResourcesJar == nil {
1431 return nil
1432 }
Colin Cross331a1212018-08-15 20:40:52 -07001433 return android.Paths{j.implementationAndResourcesJar}
1434}
1435
Colin Cross46c9b8b2017-06-22 16:51:17 -07001436func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001437 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001438 return j.exportAidlIncludeDirs
1439}
1440
Jiyong Park1be96912018-05-28 18:02:19 +09001441func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001442 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001443 return j.exportedSdkLibs
1444}
1445
Colin Cross46c9b8b2017-06-22 16:51:17 -07001446var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001447
Colin Cross46c9b8b2017-06-22 16:51:17 -07001448func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001449 return j.logtagsSrcs
1450}
1451
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001452// Collect information for opening IDE project files in java/jdeps.go.
1453func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1454 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1455 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1456 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001457 if j.expandJarjarRules != nil {
1458 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001459 }
1460}
1461
1462func (j *Module) CompilerDeps() []string {
1463 jdeps := []string{}
1464 jdeps = append(jdeps, j.properties.Libs...)
1465 jdeps = append(jdeps, j.properties.Static_libs...)
1466 return jdeps
1467}
1468
Colin Cross2fe66872015-03-30 17:20:39 -07001469//
1470// Java libraries (.jar file)
1471//
1472
Colin Crossf506d872017-07-19 15:53:04 -07001473type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001474 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001475}
1476
Colin Cross42be7612019-02-21 18:12:14 -08001477func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001478 // Store uncompressed (and do not strip) dex files from boot class path jars.
1479 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1480 return true
1481 }
1482
1483 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001484 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001485 return true
1486 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001487 if ctx.Config().UncompressPrivAppDex() &&
1488 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1489 return true
1490 }
1491
Colin Cross2fc72f62018-12-21 12:59:54 -08001492 return false
1493}
1494
Colin Crossf506d872017-07-19 15:53:04 -07001495func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001496 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1497 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001498 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001499 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001500 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001501 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001502
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001503 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001504 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1505 ctx.ModuleName()+".jar", j.outputFile)
1506 }
Colin Crossb7a63242015-04-16 14:09:14 -07001507}
1508
Colin Crossf506d872017-07-19 15:53:04 -07001509func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001510 j.deps(ctx)
1511}
1512
Colin Cross1b16b0e2019-02-12 14:41:32 -08001513// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1514//
1515// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1516// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1517// as a `static_libs` dependency of another module.
1518//
1519// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1520// a device.
1521//
1522// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1523// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001524func LibraryFactory() android.Module {
1525 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001526
Colin Cross9ae1b922018-06-26 17:59:05 -07001527 module.AddProperties(
1528 &module.Module.properties,
1529 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001530 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001531 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001532
Colin Cross9ae1b922018-06-26 17:59:05 -07001533 InitJavaModule(module, android.HostAndDeviceSupported)
1534 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001535}
1536
Colin Cross1b16b0e2019-02-12 14:41:32 -08001537// java_library_static is an obsolete alias for java_library.
1538func LibraryStaticFactory() android.Module {
1539 return LibraryFactory()
1540}
1541
1542// java_library_host builds and links sources into a `.jar` file for the host.
1543//
1544// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1545// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001546func LibraryHostFactory() android.Module {
1547 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001548
Colin Cross6af17aa2017-09-20 12:59:05 -07001549 module.AddProperties(
1550 &module.Module.properties,
1551 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001552
Colin Cross9ae1b922018-06-26 17:59:05 -07001553 module.Module.properties.Installable = proptools.BoolPtr(true)
1554
Colin Cross89536d42017-07-07 14:35:50 -07001555 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001556 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001557}
1558
1559//
Colin Crossb628ea52018-08-14 16:42:33 -07001560// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001561//
1562
1563type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001564 // list of compatibility suites (for example "cts", "vts") that the module should be
1565 // installed into.
1566 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001567
1568 // the name of the test configuration (for example "AndroidTest.xml") that should be
1569 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001570 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001571
Jack He33338892018-09-19 02:21:28 -07001572 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1573 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001574 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001575
Colin Crossd96ca352018-08-10 16:06:24 -07001576 // list of files or filegroup modules that provide data that should be installed alongside
1577 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001578 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001579}
1580
Paul Duffin42df1442019-03-20 12:45:53 +00001581type testHelperLibraryProperties struct {
1582 // list of compatibility suites (for example "cts", "vts") that the module should be
1583 // installed into.
1584 Test_suites []string `android:"arch_variant"`
1585}
1586
Colin Cross05638fc2018-04-09 18:40:24 -07001587type Test struct {
1588 Library
1589
1590 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001591
1592 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001593 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001594}
1595
Paul Duffin42df1442019-03-20 12:45:53 +00001596type TestHelperLibrary struct {
1597 Library
1598
1599 testHelperLibraryProperties testHelperLibraryProperties
1600}
1601
Colin Cross303e21f2018-08-07 16:49:25 -07001602func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001603 j.testConfig = tradefed.AutoGenJavaTestConfig(ctx, j.testProperties.Test_config, j.testProperties.Test_config_template, j.testProperties.Test_suites)
Colin Cross8a497952019-03-05 22:25:09 -08001604 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001605
1606 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001607}
1608
Paul Duffin42df1442019-03-20 12:45:53 +00001609func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1610 j.Library.GenerateAndroidBuildActions(ctx)
1611}
1612
Colin Cross1b16b0e2019-02-12 14:41:32 -08001613// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1614// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1615//
1616// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1617// compiled against the device bootclasspath.
1618//
1619// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1620// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001621func TestFactory() android.Module {
1622 module := &Test{}
1623
1624 module.AddProperties(
1625 &module.Module.properties,
1626 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001627 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001628 &module.Module.protoProperties,
1629 &module.testProperties)
1630
Colin Cross9ae1b922018-06-26 17:59:05 -07001631 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001632 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001633
Colin Cross05638fc2018-04-09 18:40:24 -07001634 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001635 return module
1636}
1637
Paul Duffin42df1442019-03-20 12:45:53 +00001638// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1639func TestHelperLibraryFactory() android.Module {
1640 module := &TestHelperLibrary{}
1641
1642 module.AddProperties(
1643 &module.Module.properties,
1644 &module.Module.deviceProperties,
1645 &module.Module.dexpreoptProperties,
1646 &module.Module.protoProperties,
1647 &module.testHelperLibraryProperties)
1648
Colin Cross9a4abed2019-04-24 13:19:28 -07001649 module.Module.properties.Installable = proptools.BoolPtr(true)
1650 module.Module.dexpreopter.isTest = true
1651
Paul Duffin42df1442019-03-20 12:45:53 +00001652 InitJavaModule(module, android.HostAndDeviceSupported)
1653 return module
1654}
1655
Colin Cross1b16b0e2019-02-12 14:41:32 -08001656// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1657// allow running the test with `atest` or a `TEST_MAPPING` file.
1658//
1659// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1660// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001661func TestHostFactory() android.Module {
1662 module := &Test{}
1663
1664 module.AddProperties(
1665 &module.Module.properties,
1666 &module.Module.protoProperties,
1667 &module.testProperties)
1668
Colin Cross9ae1b922018-06-26 17:59:05 -07001669 module.Module.properties.Installable = proptools.BoolPtr(true)
1670
Colin Cross05638fc2018-04-09 18:40:24 -07001671 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001672 return module
1673}
1674
1675//
Colin Cross2fe66872015-03-30 17:20:39 -07001676// Java Binaries (.jar file plus wrapper script)
1677//
1678
Colin Crossf506d872017-07-19 15:53:04 -07001679type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001680 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001681 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001682
1683 // Name of the class containing main to be inserted into the manifest as Main-Class.
1684 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001685}
1686
Colin Crossf506d872017-07-19 15:53:04 -07001687type Binary struct {
1688 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001689
Colin Crossf506d872017-07-19 15:53:04 -07001690 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001691
Colin Cross6b4a32d2017-12-05 13:42:45 -08001692 isWrapperVariant bool
1693
Colin Crossc3315992017-12-08 19:12:36 -08001694 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001695 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001696}
1697
Alex Light24237172017-10-26 09:46:21 -07001698func (j *Binary) HostToolPath() android.OptionalPath {
1699 return android.OptionalPathForPath(j.binaryFile)
1700}
1701
Colin Crossf506d872017-07-19 15:53:04 -07001702func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001703 if ctx.Arch().ArchType == android.Common {
1704 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001705 if j.binaryProperties.Main_class != nil {
1706 if j.properties.Manifest != nil {
1707 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1708 }
1709 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1710 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1711 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1712 }
1713
Colin Cross6b4a32d2017-12-05 13:42:45 -08001714 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001715 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001716 // Handle the binary wrapper
1717 j.isWrapperVariant = true
1718
Colin Cross366938f2017-12-11 16:29:02 -08001719 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001720 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001721 } else {
1722 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1723 }
1724
1725 // Depend on the installed jar so that the wrapper doesn't get executed by
1726 // another build rule before the jar has been installed.
1727 jarFile := ctx.PrimaryModule().(*Binary).installFile
1728
1729 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1730 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001731 }
Colin Cross2fe66872015-03-30 17:20:39 -07001732}
1733
Colin Crossf506d872017-07-19 15:53:04 -07001734func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001735 if ctx.Arch().ArchType == android.Common {
1736 j.deps(ctx)
1737 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001738}
1739
Colin Cross1b16b0e2019-02-12 14:41:32 -08001740// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1741// as well.
1742//
1743// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1744// compiled against the device bootclasspath.
1745//
1746// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1747// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001748func BinaryFactory() android.Module {
1749 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001750
Colin Cross36242852017-06-23 15:06:31 -07001751 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001752 &module.Module.properties,
1753 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001754 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001755 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001756 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001757
Colin Cross9ae1b922018-06-26 17:59:05 -07001758 module.Module.properties.Installable = proptools.BoolPtr(true)
1759
Colin Cross6b4a32d2017-12-05 13:42:45 -08001760 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1761 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001762 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001763}
1764
Colin Cross1b16b0e2019-02-12 14:41:32 -08001765// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1766//
1767// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1768// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001769func BinaryHostFactory() android.Module {
1770 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001771
Colin Cross36242852017-06-23 15:06:31 -07001772 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001773 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001774 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001775 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001776
Colin Cross9ae1b922018-06-26 17:59:05 -07001777 module.Module.properties.Installable = proptools.BoolPtr(true)
1778
Colin Cross6b4a32d2017-12-05 13:42:45 -08001779 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1780 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001781 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001782}
1783
1784//
1785// Java prebuilts
1786//
1787
Colin Cross74d73e22017-08-02 11:05:49 -07001788type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001789 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001790
Nan Zhangea568a42017-11-08 21:20:04 -08001791 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001792
1793 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001794
1795 // List of shared java libs that this module has dependencies to
1796 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001797
1798 // List of files to remove from the jar file(s)
1799 Exclude_files []string
1800
1801 // List of directories to remove from the jar file(s)
1802 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001803
1804 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001805 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001806}
1807
1808type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001809 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001810 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001811 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001812
Colin Cross74d73e22017-08-02 11:05:49 -07001813 properties ImportProperties
1814
Colin Cross0a6e0072017-08-30 14:24:55 -07001815 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001816 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001817}
1818
Colin Cross83bb3162018-06-25 15:48:06 -07001819func (j *Import) sdkVersion() string {
1820 return String(j.properties.Sdk_version)
1821}
1822
1823func (j *Import) minSdkVersion() string {
1824 return j.sdkVersion()
1825}
1826
Colin Cross74d73e22017-08-02 11:05:49 -07001827func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001828 return &j.prebuilt
1829}
1830
Colin Cross74d73e22017-08-02 11:05:49 -07001831func (j *Import) PrebuiltSrcs() []string {
1832 return j.properties.Jars
1833}
1834
1835func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001836 return j.prebuilt.Name(j.ModuleBase.Name())
1837}
1838
Colin Cross74d73e22017-08-02 11:05:49 -07001839func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001840 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001841}
1842
Colin Cross74d73e22017-08-02 11:05:49 -07001843func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001844 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001845
Nan Zhang4c819fb2018-08-27 18:31:46 -07001846 jarName := ctx.ModuleName() + ".jar"
1847 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001848 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1849 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001850 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001851 inputFile := outputFile
1852 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1853 TransformJetifier(ctx, outputFile, inputFile)
1854 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001855 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001856
1857 ctx.VisitDirectDeps(func(module android.Module) {
1858 otherName := ctx.OtherModuleName(module)
1859 tag := ctx.OtherModuleDependencyTag(module)
1860
1861 switch dep := module.(type) {
1862 case Dependency:
1863 switch tag {
1864 case libTag, staticLibTag:
1865 // sdk lib names from dependencies are re-exported
1866 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1867 }
1868 case SdkLibraryDependency:
1869 switch tag {
1870 case libTag:
1871 // names of sdk libs that are directly depended are exported
1872 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1873 }
1874 }
1875 })
1876
1877 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001878 if Bool(j.properties.Installable) {
1879 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1880 ctx.ModuleName()+".jar", outputFile)
1881 }
Colin Cross2fe66872015-03-30 17:20:39 -07001882}
1883
Colin Cross74d73e22017-08-02 11:05:49 -07001884var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001885
Nan Zhanged19fc32017-10-19 13:06:22 -07001886func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001887 if j.combinedClasspathFile == nil {
1888 return nil
1889 }
Colin Cross37f6d792018-07-12 12:28:41 -07001890 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001891}
1892
1893func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001894 if j.combinedClasspathFile == nil {
1895 return nil
1896 }
Colin Cross37f6d792018-07-12 12:28:41 -07001897 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001898}
1899
Colin Cross331a1212018-08-15 20:40:52 -07001900func (j *Import) ResourceJars() android.Paths {
1901 return nil
1902}
1903
1904func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001905 if j.combinedClasspathFile == nil {
1906 return nil
1907 }
Colin Cross331a1212018-08-15 20:40:52 -07001908 return android.Paths{j.combinedClasspathFile}
1909}
1910
Colin Crossf24a22a2019-01-31 14:12:44 -08001911func (j *Import) DexJar() android.Path {
1912 return nil
1913}
1914
Colin Cross74d73e22017-08-02 11:05:49 -07001915func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001916 return nil
1917}
1918
Jiyong Park1be96912018-05-28 18:02:19 +09001919func (j *Import) ExportedSdkLibs() []string {
1920 return j.exportedSdkLibs
1921}
1922
albaltai36ff7dc2018-12-25 14:35:23 +08001923// Add compile time check for interface implementation
1924var _ android.IDEInfo = (*Import)(nil)
1925var _ android.IDECustomizedModuleName = (*Import)(nil)
1926
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001927// Collect information for opening IDE project files in java/jdeps.go.
1928const (
1929 removedPrefix = "prebuilt_"
1930)
1931
1932func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1933 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1934}
1935
1936func (j *Import) IDECustomizedModuleName() string {
1937 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1938 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1939 // solution to get the Import name.
1940 name := j.Name()
1941 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001942 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001943 }
1944 return name
1945}
1946
Colin Cross74d73e22017-08-02 11:05:49 -07001947var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001948
Colin Cross1b16b0e2019-02-12 14:41:32 -08001949// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1950//
1951// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1952// compiled against an Android classpath.
1953//
1954// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1955// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001956func ImportFactory() android.Module {
1957 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001958
Colin Cross74d73e22017-08-02 11:05:49 -07001959 module.AddProperties(&module.properties)
1960
1961 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001962 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001963 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001964}
1965
Colin Cross1b16b0e2019-02-12 14:41:32 -08001966// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1967// module.
1968//
1969// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1970// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001971func ImportFactoryHost() android.Module {
1972 module := &Import{}
1973
1974 module.AddProperties(&module.properties)
1975
1976 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001977 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001978 return module
1979}
1980
Colin Cross42be7612019-02-21 18:12:14 -08001981// dex_import module
1982
1983type DexImportProperties struct {
1984 Jars []string
1985}
1986
1987type DexImport struct {
1988 android.ModuleBase
1989 android.DefaultableModuleBase
1990 prebuilt android.Prebuilt
1991
1992 properties DexImportProperties
1993
1994 dexJarFile android.Path
1995 maybeStrippedDexJarFile android.Path
1996
1997 dexpreopter
1998}
1999
2000func (j *DexImport) Prebuilt() *android.Prebuilt {
2001 return &j.prebuilt
2002}
2003
2004func (j *DexImport) PrebuiltSrcs() []string {
2005 return j.properties.Jars
2006}
2007
2008func (j *DexImport) Name() string {
2009 return j.prebuilt.Name(j.ModuleBase.Name())
2010}
2011
2012func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
2013 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2014}
2015
2016func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2017 if len(j.properties.Jars) != 1 {
2018 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2019 }
2020
2021 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2022 j.dexpreopter.isInstallable = true
2023 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2024
2025 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2026 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2027
2028 if j.dexpreopter.uncompressedDex {
2029 rule := android.NewRuleBuilder()
2030
2031 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2032 rule.Temporary(temporary)
2033
2034 // use zip2zip to uncompress classes*.dex files
2035 rule.Command().
2036 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2037 FlagWithInput("-i ", inputJar).
2038 FlagWithOutput("-o ", temporary).
2039 FlagWithArg("-0 ", "'classes*.dex'")
2040
2041 // use zipalign to align uncompressed classes*.dex files
2042 rule.Command().
2043 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2044 Flag("-f").
2045 Text("4").
2046 Input(temporary).
2047 Output(dexOutputFile)
2048
2049 rule.DeleteTemporaryFiles()
2050
2051 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2052 } else {
2053 ctx.Build(pctx, android.BuildParams{
2054 Rule: android.Cp,
2055 Input: inputJar,
2056 Output: dexOutputFile,
2057 })
2058 }
2059
2060 j.dexJarFile = dexOutputFile
2061
2062 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2063
2064 j.maybeStrippedDexJarFile = dexOutputFile
2065
2066 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2067 ctx.ModuleName()+".jar", dexOutputFile)
2068}
2069
2070func (j *DexImport) DexJar() android.Path {
2071 return j.dexJarFile
2072}
2073
2074// dex_import imports a `.jar` file containing classes.dex files.
2075//
2076// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2077// to the device.
2078func DexImportFactory() android.Module {
2079 module := &DexImport{}
2080
2081 module.AddProperties(&module.properties)
2082
2083 android.InitPrebuiltModule(module, &module.properties.Jars)
2084 InitJavaModule(module, android.DeviceSupported)
2085 return module
2086}
2087
Colin Cross89536d42017-07-07 14:35:50 -07002088//
2089// Defaults
2090//
2091type Defaults struct {
2092 android.ModuleBase
2093 android.DefaultsModuleBase
2094}
2095
2096func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2097}
2098
Colin Cross1b16b0e2019-02-12 14:41:32 -08002099// java_defaults provides a set of properties that can be inherited by other java or android modules.
2100//
2101// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2102// property in the defaults module that exists in the depending module will be prepended to the depending module's
2103// value for that property.
2104//
2105// Example:
2106//
2107// java_defaults {
2108// name: "example_defaults",
2109// srcs: ["common/**/*.java"],
2110// javacflags: ["-Xlint:all"],
2111// aaptflags: ["--auto-add-overlay"],
2112// }
2113//
2114// java_library {
2115// name: "example",
2116// defaults: ["example_defaults"],
2117// srcs: ["example/**/*.java"],
2118// }
2119//
2120// is functionally identical to:
2121//
2122// java_library {
2123// name: "example",
2124// srcs: [
2125// "common/**/*.java",
2126// "example/**/*.java",
2127// ],
2128// javacflags: ["-Xlint:all"],
2129// }
Colin Cross89536d42017-07-07 14:35:50 -07002130func defaultsFactory() android.Module {
2131 return DefaultsFactory()
2132}
2133
2134func DefaultsFactory(props ...interface{}) android.Module {
2135 module := &Defaults{}
2136
2137 module.AddProperties(props...)
2138 module.AddProperties(
2139 &CompilerProperties{},
2140 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002141 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002142 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002143 &aaptProperties{},
2144 &androidLibraryProperties{},
2145 &appProperties{},
2146 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002147 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002148 &ImportProperties{},
2149 &AARImportProperties{},
2150 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002151 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002152 )
2153
2154 android.InitDefaultsModule(module)
2155
2156 return module
2157}
Nan Zhangea568a42017-11-08 21:20:04 -08002158
2159var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002160var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002161var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002162var inList = android.InList