blob: 7fedf6d6918e1087038e475b527746741826c8d1 [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 Markoe26f4a52019-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 Cross363360c2019-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 Smundak4eaeab42019-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 Smundak4eaeab42019-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 Markoe26f4a52019-04-02 10:29:55 +0100340 // list of additional targets for checkbuild
341 additionalCheckedModules android.Paths
342
Colin Crossf24a22a2019-01-31 14:12:44 -0800343 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800344 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700345}
346
Colin Cross54250902017-12-05 09:28:08 -0800347func (j *Module) Srcs() android.Paths {
Colin Crosse560c4a2019-03-19 16:03:11 -0700348 return append(android.Paths{j.outputFile}, j.extraOutputFiles...)
Colin Cross54250902017-12-05 09:28:08 -0800349}
350
Jiyong Park8fd61922018-11-08 02:50:25 +0900351func (j *Module) DexJarFile() android.Path {
352 return j.dexJarFile
353}
354
Colin Cross54250902017-12-05 09:28:08 -0800355var _ android.SourceFileProducer = (*Module)(nil)
356
Colin Crossf506d872017-07-19 15:53:04 -0700357type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700358 HeaderJars() android.Paths
359 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700360 ResourceJars() android.Paths
361 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800362 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700363 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900364 ExportedSdkLibs() []string
Colin Cross2fe66872015-03-30 17:20:39 -0700365}
366
Jiyong Parkc678ad32018-04-10 13:07:10 +0900367type SdkLibraryDependency interface {
Colin Cross897d2ed2019-02-11 14:03:51 -0800368 SdkHeaderJars(ctx android.BaseContext, sdkVersion string) android.Paths
369 SdkImplementationJars(ctx android.BaseContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900370}
371
Nan Zhangb2b33de2018-02-23 11:18:47 -0800372type SrcDependency interface {
373 CompiledSrcs() android.Paths
374 CompiledSrcJars() android.Paths
375}
376
377func (j *Module) CompiledSrcs() android.Paths {
378 return j.compiledJavaSrcs
379}
380
381func (j *Module) CompiledSrcJars() android.Paths {
382 return j.compiledSrcJars
383}
384
385var _ SrcDependency = (*Module)(nil)
386
Colin Cross89536d42017-07-07 14:35:50 -0700387func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
388 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
389 android.InitDefaultableModule(module)
390}
391
Colin Crossbe1da472017-07-07 15:59:46 -0700392type dependencyTag struct {
393 blueprint.BaseDependencyTag
394 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700395}
396
Colin Crossa4f08812018-10-02 22:03:40 -0700397type jniDependencyTag struct {
398 blueprint.BaseDependencyTag
399 target android.Target
400}
401
Colin Crossbe1da472017-07-07 15:59:46 -0700402var (
Colin Cross4b964c02018-10-15 16:18:06 -0700403 staticLibTag = dependencyTag{name: "staticlib"}
404 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800405 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700406 bootClasspathTag = dependencyTag{name: "bootclasspath"}
407 systemModulesTag = dependencyTag{name: "system modules"}
408 frameworkResTag = dependencyTag{name: "framework-res"}
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700409 lineageResTag = dependencyTag{name: "org.lineageos.platform-res"}
Colin Cross4b964c02018-10-15 16:18:06 -0700410 frameworkApkTag = dependencyTag{name: "framework-apk"}
411 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800412 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700413 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
414 certificateTag = dependencyTag{name: "certificate"}
415 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Crossbe1da472017-07-07 15:59:46 -0700416)
Colin Cross2fe66872015-03-30 17:20:39 -0700417
Colin Crossfc3674a2017-09-18 17:41:52 -0700418type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700419 useModule, useFiles, useDefaultLibs, invalidVersion bool
420
Colin Cross86a60ae2018-05-29 14:44:55 -0700421 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700422 systemModules string
423
Colin Crossa97c5d32018-03-28 14:58:31 -0700424 frameworkResModule string
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700425 lineageResModule string
Colin Crossa97c5d32018-03-28 14:58:31 -0700426
Colin Cross86a60ae2018-05-29 14:44:55 -0700427 jars android.Paths
Colin Cross9bdfaf02019-04-18 10:56:44 -0700428 aidl android.OptionalPath
Colin Cross1369cdb2017-09-29 17:58:17 -0700429}
430
Colin Crossa4f08812018-10-02 22:03:40 -0700431type jniLib struct {
432 name string
433 path android.Path
434 target android.Target
435}
436
Colin Cross3144dfc2018-01-03 15:06:47 -0800437func (j *Module) shouldInstrument(ctx android.BaseContext) bool {
438 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
439}
440
441func (j *Module) shouldInstrumentStatic(ctx android.BaseContext) bool {
442 return j.shouldInstrument(ctx) &&
443 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
444 ctx.Config().UnbundledBuild())
445}
446
Colin Cross83bb3162018-06-25 15:48:06 -0700447func (j *Module) sdkVersion() string {
448 return String(j.deviceProperties.Sdk_version)
449}
450
451func (j *Module) minSdkVersion() string {
452 if j.deviceProperties.Min_sdk_version != nil {
453 return *j.deviceProperties.Min_sdk_version
454 }
455 return j.sdkVersion()
456}
457
Dan Willemsen419290a2018-10-31 15:28:47 -0700458func (j *Module) targetSdkVersion() string {
459 if j.deviceProperties.Target_sdk_version != nil {
460 return *j.deviceProperties.Target_sdk_version
461 }
462 return j.sdkVersion()
463}
464
Colin Crossbe1da472017-07-07 15:59:46 -0700465func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700466 if ctx.Device() {
Colin Crossff3ae9d2018-04-10 16:15:18 -0700467 if !Bool(j.properties.No_standard_libs) {
Colin Cross83bb3162018-06-25 15:48:06 -0700468 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Crossfc3674a2017-09-18 17:41:52 -0700469 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700470 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100471 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Colin Crossff3ae9d2018-04-10 16:15:18 -0700472 if !Bool(j.properties.No_framework_libs) {
Colin Cross42d48b72018-08-29 14:10:52 -0700473 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700474 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700475 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100476 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700477 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak4eaeab42019-04-16 17:16:58 -0700478 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700479 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
480 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800481 }
Colin Crossbe1da472017-07-07 15:59:46 -0700482 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700483 } else if j.deviceProperties.System_modules == nil {
484 ctx.PropertyErrorf("no_standard_libs",
485 "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 +0100486 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700487 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700488 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100489 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700490 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700491 ctx.AddDependency(ctx.Module(), lineageResTag, "org.lineageos.platform-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 }
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700498 if ctx.ModuleName() == "org.lineageos.platform-res" {
499 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
500 }
501 if ctx.ModuleName() == "org.lineageos.platform" ||
502 ctx.ModuleName() == "org.lineageos.platform.internal" ||
503 ctx.ModuleName() == "org.lineageos.platform.sdk" {
504 ctx.AddDependency(ctx.Module(), lineageResTag, "org.lineageos.platform-res")
505 }
Colin Cross2fe66872015-03-30 17:20:39 -0700506 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700507
Colin Cross42d48b72018-08-29 14:10:52 -0700508 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
509 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700510
Colin Crossbe9cdb82019-01-21 21:37:16 -0800511 ctx.AddFarVariationDependencies([]blueprint.Variation{
512 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
513 }, pluginTag, j.properties.Plugins...)
514
Colin Crossfe17f6f2019-03-28 19:30:56 -0700515 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700516 if j.hasSrcExt(".proto") {
517 protoDeps(ctx, &j.protoProperties)
518 }
Colin Cross93e85952017-08-15 13:34:18 -0700519
520 if j.hasSrcExt(".kt") {
521 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
522 // Kotlin files
Colin Cross42d48b72018-08-29 14:10:52 -0700523 ctx.AddVariationDependencies(nil, kotlinStdlibTag, "kotlin-stdlib")
Colin Cross7788c122019-01-23 16:14:02 -0800524 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800525 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
526 }
Colin Cross93e85952017-08-15 13:34:18 -0700527 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800528
529 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700530 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800531 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700532}
533
534func hasSrcExt(srcs []string, ext string) bool {
535 for _, src := range srcs {
536 if filepath.Ext(src) == ext {
537 return true
538 }
539 }
540
541 return false
542}
543
544func (j *Module) hasSrcExt(ext string) bool {
545 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700546}
547
Colin Cross46c9b8b2017-06-22 16:51:17 -0700548func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross9bdfaf02019-04-18 10:56:44 -0700549 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700550
Colin Crossebe1a512017-11-14 13:12:14 -0800551 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
552 aidlIncludes = append(aidlIncludes,
553 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
554 aidlIncludes = append(aidlIncludes,
555 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700556
Colin Cross9bdfaf02019-04-18 10:56:44 -0700557 var flags []string
558 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700559
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700560 if aidlPreprocess.Valid() {
561 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross9bdfaf02019-04-18 10:56:44 -0700562 deps = append(deps, aidlPreprocess.Path())
563 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700564 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700565 }
566
Colin Cross9bdfaf02019-04-18 10:56:44 -0700567 if len(j.exportAidlIncludeDirs) > 0 {
568 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
569 }
570
571 if len(aidlIncludes) > 0 {
572 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
573 }
574
Colin Cross635c3b02016-05-18 15:37:25 -0700575 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800576 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700577 flags = append(flags, "-I"+src.String())
578 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700579
Martijn Coeneneab15642018-03-09 09:29:59 +0100580 if Bool(j.deviceProperties.Aidl.Generate_traces) {
581 flags = append(flags, "-t")
582 }
583
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100584 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
585 flags = append(flags, "--transaction_names")
586 }
587
Colin Cross9bdfaf02019-04-18 10:56:44 -0700588 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700589}
590
Colin Cross32f676a2017-09-06 13:41:06 -0700591type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800592 classpath classpath
593 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700594 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800595 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700596 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700597 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700598 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700599 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800600 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700601 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700602 systemModules android.Path
Colin Cross6ade34f2017-09-15 13:00:47 -0700603 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700604 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800605 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800606
607 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700608}
Colin Cross2fe66872015-03-30 17:20:39 -0700609
Colin Cross54250902017-12-05 09:28:08 -0800610func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
611 for _, f := range dep.Srcs() {
612 if f.Ext() != ".jar" {
613 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
614 ctx.OtherModuleName(dep.(blueprint.Module)))
615 }
616 }
617}
618
Jiyong Park2d492942018-03-05 17:44:10 +0900619type linkType int
620
621const (
622 javaCore linkType = iota
623 javaSdk
624 javaSystem
625 javaPlatform
626)
627
Jiyong Park46f78fb2018-10-20 16:33:17 +0900628func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700629 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700630 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900631 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
632 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
633 name == "core-lambda-stubs":
634 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100635 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900636 return javaCore, false
637 case name == "android_system_stubs_current":
638 return javaSystem, true
639 case strings.HasPrefix(ver, "system_"):
640 return javaSystem, false
641 case name == "android_test_stubs_current":
642 return javaSystem, true
643 case strings.HasPrefix(ver, "test_"):
644 return javaPlatform, false
645 case name == "android_stubs_current":
646 return javaSdk, true
647 case ver == "current":
648 return javaSdk, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700649 case ver == "":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900650 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700651 default:
652 if _, err := strconv.Atoi(ver); err != nil {
653 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
654 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900655 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900656 }
657}
658
Jiyong Park750e5572018-01-31 00:20:13 +0900659func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700660 if ctx.Host() {
661 return
662 }
663
Jiyong Park46f78fb2018-10-20 16:33:17 +0900664 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
665 if stubs {
666 return
667 }
668 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900669 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."
670
671 switch myLinkType {
672 case javaCore:
673 if otherLinkType != javaCore {
674 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 +0900675 ctx.OtherModuleName(to))
676 }
Jiyong Park2d492942018-03-05 17:44:10 +0900677 break
678 case javaSdk:
679 if otherLinkType != javaCore && otherLinkType != javaSdk {
680 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
681 ctx.OtherModuleName(to))
682 }
683 break
684 case javaSystem:
685 if otherLinkType == javaPlatform {
686 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
687 ctx.OtherModuleName(to))
688 }
689 break
690 case javaPlatform:
691 // no restriction on link-type
692 break
Jiyong Park750e5572018-01-31 00:20:13 +0900693 }
694}
695
Colin Cross32f676a2017-09-06 13:41:06 -0700696func (j *Module) collectDeps(ctx android.ModuleContext) deps {
697 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700698
Colin Cross300f0382018-03-06 13:11:51 -0800699 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700700 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800701 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700702 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800703 } else if sdkDep.useFiles {
704 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700705 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross9bdfaf02019-04-18 10:56:44 -0700706 deps.aidlPreprocess = sdkDep.aidl
707 } else {
708 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800709 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700710 }
711
Colin Crossd11fcda2017-10-23 17:59:01 -0700712 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700713 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700714 tag := ctx.OtherModuleDependencyTag(module)
715
Colin Crossa4f08812018-10-02 22:03:40 -0700716 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700717 // Handled by AndroidApp.collectAppDeps
718 return
719 }
720 if tag == certificateTag {
721 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700722 return
723 }
724
Jiyong Park750e5572018-01-31 00:20:13 +0900725 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700726 switch tag {
727 case bootClasspathTag, libTag, staticLibTag:
728 checkLinkType(ctx, j, to, tag.(dependencyTag))
729 }
Jiyong Park750e5572018-01-31 00:20:13 +0900730 }
Colin Cross54250902017-12-05 09:28:08 -0800731 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800732 case SdkLibraryDependency:
733 switch tag {
734 case libTag:
735 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
736 // names of sdk libs that are directly depended are exported
737 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
738 default:
739 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
740 }
Colin Cross54250902017-12-05 09:28:08 -0800741 case Dependency:
742 switch tag {
743 case bootClasspathTag:
744 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700745 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800746 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900747 // sdk lib names from dependencies are re-exported
748 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross9bdfaf02019-04-18 10:56:44 -0700749 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800750 case staticLibTag:
751 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
752 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
753 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700754 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900755 // sdk lib names from dependencies are re-exported
756 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross9bdfaf02019-04-18 10:56:44 -0700757 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800758 case pluginTag:
759 if plugin, ok := dep.(*Plugin); ok {
760 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
761 if plugin.pluginProperties.Processor_class != nil {
762 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
763 }
764 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
765 } else {
766 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
767 }
Colin Cross54250902017-12-05 09:28:08 -0800768 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100769 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800770 // framework.jar has a one-off dependency on the R.java and Manifest.java files
771 // generated by framework-res.apk
772 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
773 }
Rashed Abdel-Tawab47d35aa2018-08-09 14:08:53 -0700774 case lineageResTag:
775 if ctx.ModuleName() == "org.lineageos.platform" ||
776 ctx.ModuleName() == "org.lineageos.platform.internal" ||
777 ctx.ModuleName() == "org.lineageos.platform.sdk" {
778 // org.lineageos.platform.jar has a one-off dependency on the R.java and Manifest.java files
779 // generated by org.lineageos.platform-res.apk
780 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
781 }
782 if ctx.ModuleName() == "framework" {
783 // framework.jar has a one-off dependency on the R.java and Manifest.java files
784 // generated by org.lineageos.platform-res.apk
785 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
786 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800787 case frameworkApkTag:
788 if ctx.ModuleName() == "android_stubs_current" ||
789 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700790 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800791 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
792 // resource files out of there for aapt.
793 //
794 // Normally the package rule runs aapt, which includes the resource,
795 // but we're not running that in our package rule so just copy in the
796 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700797 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800798 }
Colin Cross54250902017-12-05 09:28:08 -0800799 case kotlinStdlibTag:
800 deps.kotlinStdlib = dep.HeaderJars()
Colin Crossafbb1732019-01-17 15:42:52 -0800801 case kotlinAnnotationsTag:
802 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800803 }
804
Colin Cross54250902017-12-05 09:28:08 -0800805 case android.SourceFileProducer:
806 switch tag {
807 case libTag:
808 checkProducesJars(ctx, dep)
809 deps.classpath = append(deps.classpath, dep.Srcs()...)
810 case staticLibTag:
811 checkProducesJars(ctx, dep)
812 deps.classpath = append(deps.classpath, dep.Srcs()...)
813 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
814 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800815 }
816 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700817 switch tag {
818 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700819 // Nothing to do
Colin Cross1369cdb2017-09-29 17:58:17 -0700820 case systemModulesTag:
821 if deps.systemModules != nil {
822 panic("Found two system module dependencies")
823 }
824 sm := module.(*SystemModules)
825 if sm.outputFile == nil {
826 panic("Missing directory for system module dependency")
827 }
828 deps.systemModules = sm.outputFile
Colin Crossec7a0422017-07-07 14:47:12 -0700829 default:
830 ctx.ModuleErrorf("depends on non-java module %q", otherName)
Colin Cross2fe66872015-03-30 17:20:39 -0700831 }
Colin Crossec7a0422017-07-07 14:47:12 -0700832 }
Colin Cross2fe66872015-03-30 17:20:39 -0700833 })
834
Jiyong Park1be96912018-05-28 18:02:19 +0900835 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
836
Colin Cross32f676a2017-09-06 13:41:06 -0700837 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700838}
839
Colin Cross83bb3162018-06-25 15:48:06 -0700840func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700841 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800842 v := sdkContext.sdkVersion()
843 // For PDK builds, use the latest SDK version instead of "current"
844 if ctx.Config().IsPdkBuild() && (v == "" || v == "current") {
Colin Cross9bdfaf02019-04-18 10:56:44 -0700845 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800846 latestSdkVersion := 0
847 if len(sdkVersions) > 0 {
848 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
849 }
850 v = strconv.Itoa(latestSdkVersion)
851 }
852
853 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700854 if err != nil {
855 ctx.PropertyErrorf("sdk_version", "%s", err)
856 }
Nan Zhang357466b2018-04-17 17:38:36 -0700857 if javaVersion != "" {
858 ret = javaVersion
859 } else if ctx.Device() && sdk <= 23 {
860 ret = "1.7"
Jiyong Park4584a8a2018-10-03 18:05:04 +0900861 } else if ctx.Device() && sdk <= 28 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700862 ret = "1.8"
Colin Cross83bb3162018-06-25 15:48:06 -0700863 } else if ctx.Device() && sdkContext.sdkVersion() != "" && sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700864 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
865 ret = "1.8"
866 } else {
867 ret = "1.9"
868 }
869
870 return ret
871}
872
Nan Zhanged19fc32017-10-19 13:06:22 -0700873func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700874
Colin Crossf03c82b2015-04-13 13:53:40 -0700875 var flags javaBuilderFlags
876
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100877 // javaVersion flag.
878 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
879
Nan Zhanged19fc32017-10-19 13:06:22 -0700880 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700881 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100882 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700883 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700884 }
Colin Cross6510f912017-11-29 00:27:14 -0800885 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700886 // Override the -g flag passed globally to remove local variable debug info to reduce
887 // disk and memory usage.
888 javacFlags = append(javacFlags, "-g:source,lines")
889 }
Colin Cross64162712017-08-08 13:17:59 -0700890
Colin Cross66548102018-06-19 22:47:35 -0700891 if ctx.Config().RunErrorProne() {
892 if config.ErrorProneClasspath == nil {
893 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
894 }
895
896 errorProneFlags := []string{
897 "-Xplugin:ErrorProne",
898 "${config.ErrorProneChecks}",
899 }
900 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
901
902 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
903 "'" + strings.Join(errorProneFlags, " ") + "'"
904 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800905 }
906
Nan Zhanged19fc32017-10-19 13:06:22 -0700907 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800908 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
909 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700910 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800911
Colin Crossbe9cdb82019-01-21 21:37:16 -0800912 flags.processor = strings.Join(deps.processorClasses, ",")
913
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100914 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800915 !Bool(j.properties.No_standard_libs) &&
916 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
917 // Give host-side tools a version of OpenJDK's standard libraries
918 // close to what they're targeting. As of Dec 2017, AOSP is only
919 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
920 //
921 // When building with OpenJDK 8, the following should have no
922 // effect since those jars would be available by default.
923 //
924 // When building with OpenJDK 9 but targeting a version < 1.8,
925 // putting them on the bootclasspath means that:
926 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
927 // b) references to existing APIs are not reinterpreted in an
928 // OpenJDK 9-specific way, eg. calls to subclasses of
929 // java.nio.Buffer as in http://b/70862583
930 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
931 flags.bootClasspath = append(flags.bootClasspath,
932 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
933 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800934 if Bool(j.properties.Use_tools_jar) {
935 flags.bootClasspath = append(flags.bootClasspath,
936 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
937 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800938 }
939
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100940 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800941 // Manually specify build directory in case it is not under the repo root.
942 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
943 // just adding a symlink under the root doesn't help.)
944 patchPaths := ".:" + ctx.Config().BuildDir()
945 classPath := flags.classpath.FormJavaClassPath("")
946 if classPath != "" {
947 patchPaths += ":" + classPath
948 }
949 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700950 }
951
Nan Zhanged19fc32017-10-19 13:06:22 -0700952 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700953 if deps.systemModules != nil {
954 flags.systemModules = append(flags.systemModules, deps.systemModules)
955 }
956
Nan Zhanged19fc32017-10-19 13:06:22 -0700957 // aidl flags.
Colin Cross9bdfaf02019-04-18 10:56:44 -0700958 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700959
Colin Cross81440082018-08-15 20:21:55 -0700960 if len(javacFlags) > 0 {
961 // optimization.
962 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
963 flags.javacFlags = "$javacFlags"
964 }
965
Nan Zhanged19fc32017-10-19 13:06:22 -0700966 return flags
967}
Colin Crossc0b06f12015-04-08 13:03:43 -0700968
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800969func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path) {
Nan Zhanged19fc32017-10-19 13:06:22 -0700970
Colin Crossebe1a512017-11-14 13:12:14 -0800971 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700972
973 deps := j.collectDeps(ctx)
974 flags := j.collectBuilderFlags(ctx, deps)
975
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100976 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700977 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
978 }
Colin Cross8a497952019-03-05 22:25:09 -0800979 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700980 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800981 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700982 }
983
Colin Crossaf050172017-11-15 23:01:59 -0800984 srcFiles = j.genSources(ctx, srcFiles, flags)
985
986 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700987 srcJars = append(srcJars, deps.srcJars...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800988 srcJars = append(srcJars, extraSrcJars...)
Colin Crossb7a63242015-04-16 14:09:14 -0700989
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700990 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
991 // that IDEInfo struct will use
992 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
993
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800994 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -0800995 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800996 }
997
Colin Cross1ee23172017-10-18 14:44:18 -0700998 jarName := ctx.ModuleName() + ".jar"
999
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001000 javaSrcFiles := srcFiles.FilterByExt(".java")
1001 var uniqueSrcFiles android.Paths
1002 set := make(map[string]bool)
1003 for _, v := range javaSrcFiles {
1004 if _, found := set[v.String()]; !found {
1005 set[v.String()] = true
1006 uniqueSrcFiles = append(uniqueSrcFiles, v)
1007 }
1008 }
1009
Colin Cross55f63ea2018-08-27 12:37:09 -07001010 var kotlinJars android.Paths
1011
Colin Cross93e85952017-08-15 13:34:18 -07001012 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001013 // user defined kotlin flags.
1014 kotlincFlags := j.properties.Kotlincflags
1015 CheckKotlincFlags(ctx, kotlincFlags)
1016
Colin Cross93e85952017-08-15 13:34:18 -07001017 // If there are kotlin files, compile them first but pass all the kotlin and java files
1018 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1019 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001020 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001021 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001022 kotlincFlags = append(kotlincFlags, "-no-jdk")
1023 }
1024 if len(kotlincFlags) > 0 {
1025 // optimization.
1026 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1027 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001028 }
1029
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001030 var kotlinSrcFiles android.Paths
1031 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1032 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1033
Colin Crossafbb1732019-01-17 15:42:52 -08001034 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1035 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1036
1037 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1038 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1039
1040 if len(flags.processorPath) > 0 {
1041 // Use kapt for annotation processing
1042 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1043 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1044 srcJars = append(srcJars, kaptSrcJar)
1045 // Disable annotation processing in javac, it's already been handled by kapt
1046 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001047 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001048 }
Colin Cross93e85952017-08-15 13:34:18 -07001049
Colin Cross1ee23172017-10-18 14:44:18 -07001050 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001051 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001052 if ctx.Failed() {
1053 return
1054 }
1055
1056 // Make javac rule depend on the kotlinc rule
1057 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001058
Colin Cross93e85952017-08-15 13:34:18 -07001059 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001060 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001061 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001062 }
1063
Colin Cross55f63ea2018-08-27 12:37:09 -07001064 jars := append(android.Paths(nil), kotlinJars...)
1065
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001066 // Store the list of .java files that was passed to javac
1067 j.compiledJavaSrcs = uniqueSrcFiles
1068 j.compiledSrcJars = srcJars
1069
Nan Zhang61eaedb2017-11-02 13:28:15 -07001070 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001071 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001072 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1073 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001074 // Formerly, there was a check here that prevented annotation processors
1075 // from being used when sharding was enabled, as some annotation processors
1076 // do not function correctly in sharded environments. It was removed to
1077 // allow for the use of annotation processors that do function correctly
1078 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001079 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001080 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001081 if ctx.Failed() {
1082 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001083 }
1084 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001085 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001086 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001087 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001088 // If error-prone is enabled, add an additional rule to compile the java files into
1089 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001090 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001091 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1092 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001093 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001094 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001095 extraJarDeps = append(extraJarDeps, errorprone)
1096 }
1097
Nan Zhang61eaedb2017-11-02 13:28:15 -07001098 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001099 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001100 shardSize := int(*(j.properties.Javac_shard_size))
1101 var shardSrcs []android.Paths
1102 if len(uniqueSrcFiles) > 0 {
Colin Crossa95304e2019-09-23 14:33:09 -07001103 shardSrcs = android.ShardPaths(uniqueSrcFiles, shardSize)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001104 for idx, shardSrc := range shardSrcs {
1105 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1106 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1107 jars = append(jars, classes)
1108 }
1109 }
1110 if len(srcJars) > 0 {
1111 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1112 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1113 jars = append(jars, classes)
1114 }
1115 } else {
1116 classes := android.PathForModuleOut(ctx, "javac", jarName)
1117 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1118 jars = append(jars, classes)
1119 }
Colin Crossd6891432017-09-27 17:39:56 -07001120 if ctx.Failed() {
1121 return
1122 }
Colin Cross2fe66872015-03-30 17:20:39 -07001123 }
1124
Colin Crosscedd4762018-09-13 11:26:19 -07001125 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1126 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001127 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
1128
1129 var resArgs []string
1130 var resDeps android.Paths
1131
1132 resArgs = append(resArgs, dirArgs...)
1133 resDeps = append(resDeps, dirDeps...)
1134
1135 resArgs = append(resArgs, fileArgs...)
1136 resDeps = append(resDeps, fileDeps...)
1137
Colin Crossff3ae9d2018-04-10 16:15:18 -07001138 if Bool(j.properties.Include_srcs) {
Colin Cross23729232017-10-03 13:14:07 -07001139 srcArgs, srcDeps := SourceFilesToJarArgs(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross0f37af02017-09-27 17:42:05 -07001140 resArgs = append(resArgs, srcArgs...)
1141 resDeps = append(resDeps, srcDeps...)
1142 }
Colin Cross40a36712017-09-27 17:41:35 -07001143
1144 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001145 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001146 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001147 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001148 if ctx.Failed() {
1149 return
1150 }
1151 }
1152
Colin Cross331a1212018-08-15 20:40:52 -07001153 if len(deps.staticResourceJars) > 0 {
1154 var jars android.Paths
1155 if j.resourceJar != nil {
1156 jars = append(jars, j.resourceJar)
1157 }
1158 jars = append(jars, deps.staticResourceJars...)
1159
1160 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
1161 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1162 false, nil, nil)
1163 j.resourceJar = combinedJar
1164 }
1165
Colin Cross32f676a2017-09-06 13:41:06 -07001166 jars = append(jars, deps.staticJars...)
Colin Cross0a6e0072017-08-30 14:24:55 -07001167
Colin Cross094054a2018-10-17 15:10:48 -07001168 manifest := j.overrideManifest
1169 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001170 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001171 }
Colin Cross635acc92017-09-12 22:50:46 -07001172
Colin Cross8a497952019-03-05 22:25:09 -08001173 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001174 if len(services) > 0 {
1175 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1176 var zipargs []string
1177 for _, file := range services {
1178 serviceFile := file.String()
1179 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1180 }
1181 ctx.Build(pctx, android.BuildParams{
1182 Rule: zip,
1183 Output: servicesJar,
1184 Implicits: services,
1185 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001186 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001187 },
1188 })
1189 jars = append(jars, servicesJar)
1190 }
1191
Colin Cross0a6e0072017-08-30 14:24:55 -07001192 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001193 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001194 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001195
1196 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001197 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1198 // Optimization: skip the combine step if there is nothing to do
1199 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1200 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1201 // any if len(jars) == 1.
1202 outputFile = moduleOutPath
1203 } else {
1204 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1205 ctx.Build(pctx, android.BuildParams{
1206 Rule: android.Cp,
1207 Input: jars[0],
1208 Output: combinedJar,
1209 })
1210 outputFile = combinedJar
1211 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001212 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001213 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001214 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001215 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001216 outputFile = combinedJar
1217 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001218
Colin Cross331a1212018-08-15 20:40:52 -07001219 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001220 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001221 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001222 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001223 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001224 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001225
1226 // jarjar resource jar if necessary
1227 if j.resourceJar != nil {
1228 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001229 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001230 j.resourceJar = resourceJarJarFile
1231 }
1232
Colin Cross0a6e0072017-08-30 14:24:55 -07001233 if ctx.Failed() {
1234 return
1235 }
1236 }
Vladimir Markoe26f4a52019-04-02 10:29:55 +01001237
1238 // Check package restrictions if necessary.
1239 if len(j.properties.Permitted_packages) > 0 {
1240 // Check packages and copy to package-checked file.
1241 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1242 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1243 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1244
1245 if ctx.Failed() {
1246 return
1247 }
1248 }
1249
Nan Zhanged19fc32017-10-19 13:06:22 -07001250 j.implementationJarFile = outputFile
1251 if j.headerJarFile == nil {
1252 j.headerJarFile = j.implementationJarFile
1253 }
Colin Cross2fe66872015-03-30 17:20:39 -07001254
Colin Cross6510f912017-11-29 00:27:14 -08001255 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001256 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1257 j.properties.Instrument = true
1258 }
1259 }
1260
Colin Cross3144dfc2018-01-03 15:06:47 -08001261 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001262 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1263 }
1264
Colin Cross331a1212018-08-15 20:40:52 -07001265 // merge implementation jar with resources if necessary
1266 implementationAndResourcesJar := outputFile
1267 if j.resourceJar != nil {
1268 jars := android.Paths{implementationAndResourcesJar, j.resourceJar}
1269 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
1270 TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
1271 false, nil, nil)
1272 implementationAndResourcesJar = combinedJar
1273 }
1274
1275 j.implementationAndResourcesJar = implementationAndResourcesJar
1276
Colin Cross9ae1b922018-06-26 17:59:05 -07001277 if ctx.Device() && (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001278 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001279 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001280 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001281 if ctx.Failed() {
1282 return
1283 }
Colin Cross331a1212018-08-15 20:40:52 -07001284
Jiyong Park2c7b1c42019-07-15 15:29:23 +09001285 // Hidden API CSV generation and dex encoding
1286 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1287 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001288
Colin Cross331a1212018-08-15 20:40:52 -07001289 // merge dex jar with resources if necessary
1290 if j.resourceJar != nil {
1291 jars := android.Paths{dexOutputFile, j.resourceJar}
1292 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1293 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1294 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001295 if j.deviceProperties.UncompressDex {
1296 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1297 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1298 dexOutputFile = combinedAlignedJar
1299 } else {
1300 dexOutputFile = combinedJar
1301 }
Colin Cross331a1212018-08-15 20:40:52 -07001302 }
1303
1304 j.dexJarFile = dexOutputFile
1305
Colin Cross8faf8fc2019-01-16 15:15:52 -08001306 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001307 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1308
1309 j.maybeStrippedDexJarFile = dexOutputFile
1310
Colin Cross3063b782018-08-15 11:19:12 -07001311 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001312
1313 if ctx.Failed() {
1314 return
1315 }
Colin Cross331a1212018-08-15 20:40:52 -07001316 } else {
1317 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001318 }
Colin Cross331a1212018-08-15 20:40:52 -07001319
Colin Crossb7a63242015-04-16 14:09:14 -07001320 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001321
1322 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1323 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001324}
1325
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001326// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1327// since some of these flags may be used internally.
1328func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1329 for _, flag := range flags {
1330 flag = strings.TrimSpace(flag)
1331
1332 if !strings.HasPrefix(flag, "-") {
1333 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1334 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1335 ctx.PropertyErrorf("kotlincflags",
1336 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1337 } else if inList(flag, config.KotlincIllegalFlags) {
1338 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1339 } else if flag == "-include-runtime" {
1340 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1341 } else {
1342 args := strings.Split(flag, " ")
1343 if args[0] == "-kotlin-home" {
1344 ctx.PropertyErrorf("kotlincflags",
1345 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1346 }
1347 }
1348 }
1349}
1350
Colin Cross8eadbf02017-10-24 17:46:00 -07001351func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001352 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001353
1354 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001355 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001356 // Compile java sources into turbine.jar.
1357 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1358 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1359 if ctx.Failed() {
1360 return nil
1361 }
1362 jars = append(jars, turbineJar)
1363 }
1364
Colin Cross55f63ea2018-08-27 12:37:09 -07001365 jars = append(jars, extraJars...)
1366
Nan Zhanged19fc32017-10-19 13:06:22 -07001367 // Combine any static header libraries into classes-header.jar. If there is only
1368 // one input jar this step will be skipped.
1369 var headerJar android.Path
1370 jars = append(jars, deps.staticHeaderJars...)
1371
Colin Cross5c6ecc12017-10-23 18:12:27 -07001372 // we cannot skip the combine step for now if there is only one jar
1373 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1374 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001375 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
1376 false, nil, []string{"META-INF"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001377 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001378
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001379 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001380 // Transform classes.jar into classes-jarjar.jar
1381 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001382 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001383 headerJar = jarjarFile
1384 if ctx.Failed() {
1385 return nil
1386 }
1387 }
1388
1389 return headerJar
1390}
1391
Colin Crosscb933592017-11-22 13:49:43 -08001392func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001393 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001394
Colin Cross7a3139e2017-12-19 13:57:50 -08001395 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001396
Colin Cross84c38822018-01-03 15:59:46 -08001397 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001398 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1399
1400 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1401
1402 j.jacocoReportClassesFile = jacocoReportClassesFile
1403
1404 return instrumentedJar
1405}
1406
albaltai36ff7dc2018-12-25 14:35:23 +08001407var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001408
Nan Zhanged19fc32017-10-19 13:06:22 -07001409func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001410 if j.headerJarFile == nil {
1411 return nil
1412 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001413 return android.Paths{j.headerJarFile}
1414}
1415
1416func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001417 if j.implementationJarFile == nil {
1418 return nil
1419 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001420 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001421}
1422
Colin Crossf24a22a2019-01-31 14:12:44 -08001423func (j *Module) DexJar() android.Path {
1424 return j.dexJarFile
1425}
1426
Colin Cross331a1212018-08-15 20:40:52 -07001427func (j *Module) ResourceJars() android.Paths {
1428 if j.resourceJar == nil {
1429 return nil
1430 }
1431 return android.Paths{j.resourceJar}
1432}
1433
1434func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001435 if j.implementationAndResourcesJar == nil {
1436 return nil
1437 }
Colin Cross331a1212018-08-15 20:40:52 -07001438 return android.Paths{j.implementationAndResourcesJar}
1439}
1440
Colin Cross46c9b8b2017-06-22 16:51:17 -07001441func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001442 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001443 return j.exportAidlIncludeDirs
1444}
1445
Jiyong Park1be96912018-05-28 18:02:19 +09001446func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001447 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001448 return j.exportedSdkLibs
1449}
1450
Colin Cross46c9b8b2017-06-22 16:51:17 -07001451var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001452
Colin Cross46c9b8b2017-06-22 16:51:17 -07001453func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001454 return j.logtagsSrcs
1455}
1456
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001457// Collect information for opening IDE project files in java/jdeps.go.
1458func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1459 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1460 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
1461 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001462 if j.expandJarjarRules != nil {
1463 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001464 }
1465}
1466
1467func (j *Module) CompilerDeps() []string {
1468 jdeps := []string{}
1469 jdeps = append(jdeps, j.properties.Libs...)
1470 jdeps = append(jdeps, j.properties.Static_libs...)
1471 return jdeps
1472}
1473
Colin Cross2fe66872015-03-30 17:20:39 -07001474//
1475// Java libraries (.jar file)
1476//
1477
Colin Crossf506d872017-07-19 15:53:04 -07001478type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001479 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001480}
1481
Colin Cross42be7612019-02-21 18:12:14 -08001482func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001483 // Store uncompressed (and do not strip) dex files from boot class path jars.
1484 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1485 return true
1486 }
1487
1488 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001489 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001490 return true
1491 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001492 if ctx.Config().UncompressPrivAppDex() &&
1493 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1494 return true
1495 }
1496
Colin Cross2fc72f62018-12-21 12:59:54 -08001497 return false
1498}
1499
Colin Crossf506d872017-07-19 15:53:04 -07001500func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001501 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1502 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001503 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001504 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001505 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Colin Cross46c9b8b2017-06-22 16:51:17 -07001506 j.compile(ctx)
Colin Crossb7a63242015-04-16 14:09:14 -07001507
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001508 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001509 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1510 ctx.ModuleName()+".jar", j.outputFile)
1511 }
Colin Crossb7a63242015-04-16 14:09:14 -07001512}
1513
Colin Crossf506d872017-07-19 15:53:04 -07001514func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001515 j.deps(ctx)
1516}
1517
Colin Cross1b16b0e2019-02-12 14:41:32 -08001518// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1519//
1520// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1521// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1522// as a `static_libs` dependency of another module.
1523//
1524// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1525// a device.
1526//
1527// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1528// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001529func LibraryFactory() android.Module {
1530 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001531
Colin Cross9ae1b922018-06-26 17:59:05 -07001532 module.AddProperties(
1533 &module.Module.properties,
1534 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001535 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001536 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001537
Colin Cross9ae1b922018-06-26 17:59:05 -07001538 InitJavaModule(module, android.HostAndDeviceSupported)
1539 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001540}
1541
Colin Cross1b16b0e2019-02-12 14:41:32 -08001542// java_library_static is an obsolete alias for java_library.
1543func LibraryStaticFactory() android.Module {
1544 return LibraryFactory()
1545}
1546
1547// java_library_host builds and links sources into a `.jar` file for the host.
1548//
1549// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1550// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001551func LibraryHostFactory() android.Module {
1552 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001553
Colin Cross6af17aa2017-09-20 12:59:05 -07001554 module.AddProperties(
1555 &module.Module.properties,
1556 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001557
Colin Cross9ae1b922018-06-26 17:59:05 -07001558 module.Module.properties.Installable = proptools.BoolPtr(true)
1559
Colin Cross89536d42017-07-07 14:35:50 -07001560 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001561 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001562}
1563
1564//
Colin Crossb628ea52018-08-14 16:42:33 -07001565// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001566//
1567
1568type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001569 // list of compatibility suites (for example "cts", "vts") that the module should be
1570 // installed into.
1571 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001572
1573 // the name of the test configuration (for example "AndroidTest.xml") that should be
1574 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001575 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001576
Jack He33338892018-09-19 02:21:28 -07001577 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1578 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001579 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001580
Colin Crossd96ca352018-08-10 16:06:24 -07001581 // list of files or filegroup modules that provide data that should be installed alongside
1582 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001583 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001584}
1585
Paul Duffin42df1442019-03-20 12:45:53 +00001586type testHelperLibraryProperties struct {
1587 // list of compatibility suites (for example "cts", "vts") that the module should be
1588 // installed into.
1589 Test_suites []string `android:"arch_variant"`
1590}
1591
Colin Cross05638fc2018-04-09 18:40:24 -07001592type Test struct {
1593 Library
1594
1595 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001596
1597 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001598 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001599}
1600
Paul Duffin42df1442019-03-20 12:45:53 +00001601type TestHelperLibrary struct {
1602 Library
1603
1604 testHelperLibraryProperties testHelperLibraryProperties
1605}
1606
Colin Cross303e21f2018-08-07 16:49:25 -07001607func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001608 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 -08001609 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001610
1611 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001612}
1613
Paul Duffin42df1442019-03-20 12:45:53 +00001614func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1615 j.Library.GenerateAndroidBuildActions(ctx)
1616}
1617
Colin Cross1b16b0e2019-02-12 14:41:32 -08001618// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1619// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1620//
1621// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1622// compiled against the device bootclasspath.
1623//
1624// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1625// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001626func TestFactory() android.Module {
1627 module := &Test{}
1628
1629 module.AddProperties(
1630 &module.Module.properties,
1631 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001632 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001633 &module.Module.protoProperties,
1634 &module.testProperties)
1635
Colin Cross9ae1b922018-06-26 17:59:05 -07001636 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001637 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001638
Colin Cross05638fc2018-04-09 18:40:24 -07001639 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001640 return module
1641}
1642
Paul Duffin42df1442019-03-20 12:45:53 +00001643// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1644func TestHelperLibraryFactory() android.Module {
1645 module := &TestHelperLibrary{}
1646
1647 module.AddProperties(
1648 &module.Module.properties,
1649 &module.Module.deviceProperties,
1650 &module.Module.dexpreoptProperties,
1651 &module.Module.protoProperties,
1652 &module.testHelperLibraryProperties)
1653
1654 InitJavaModule(module, android.HostAndDeviceSupported)
1655 return module
1656}
1657
Colin Cross1b16b0e2019-02-12 14:41:32 -08001658// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1659// allow running the test with `atest` or a `TEST_MAPPING` file.
1660//
1661// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1662// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001663func TestHostFactory() android.Module {
1664 module := &Test{}
1665
1666 module.AddProperties(
1667 &module.Module.properties,
1668 &module.Module.protoProperties,
1669 &module.testProperties)
1670
Colin Cross9ae1b922018-06-26 17:59:05 -07001671 module.Module.properties.Installable = proptools.BoolPtr(true)
1672
Colin Cross05638fc2018-04-09 18:40:24 -07001673 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001674 return module
1675}
1676
1677//
Colin Cross2fe66872015-03-30 17:20:39 -07001678// Java Binaries (.jar file plus wrapper script)
1679//
1680
Colin Crossf506d872017-07-19 15:53:04 -07001681type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001682 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001683 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001684
1685 // Name of the class containing main to be inserted into the manifest as Main-Class.
1686 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001687}
1688
Colin Crossf506d872017-07-19 15:53:04 -07001689type Binary struct {
1690 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001691
Colin Crossf506d872017-07-19 15:53:04 -07001692 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001693
Colin Cross6b4a32d2017-12-05 13:42:45 -08001694 isWrapperVariant bool
1695
Colin Crossc3315992017-12-08 19:12:36 -08001696 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001697 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001698}
1699
Alex Light24237172017-10-26 09:46:21 -07001700func (j *Binary) HostToolPath() android.OptionalPath {
1701 return android.OptionalPathForPath(j.binaryFile)
1702}
1703
Colin Crossf506d872017-07-19 15:53:04 -07001704func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001705 if ctx.Arch().ArchType == android.Common {
1706 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001707 if j.binaryProperties.Main_class != nil {
1708 if j.properties.Manifest != nil {
1709 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1710 }
1711 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1712 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1713 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1714 }
1715
Colin Cross6b4a32d2017-12-05 13:42:45 -08001716 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001717 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001718 // Handle the binary wrapper
1719 j.isWrapperVariant = true
1720
Colin Cross366938f2017-12-11 16:29:02 -08001721 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001722 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001723 } else {
1724 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1725 }
1726
1727 // Depend on the installed jar so that the wrapper doesn't get executed by
1728 // another build rule before the jar has been installed.
1729 jarFile := ctx.PrimaryModule().(*Binary).installFile
1730
1731 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1732 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001733 }
Colin Cross2fe66872015-03-30 17:20:39 -07001734}
1735
Colin Crossf506d872017-07-19 15:53:04 -07001736func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001737 if ctx.Arch().ArchType == android.Common {
1738 j.deps(ctx)
1739 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001740}
1741
Colin Cross1b16b0e2019-02-12 14:41:32 -08001742// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1743// as well.
1744//
1745// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1746// compiled against the device bootclasspath.
1747//
1748// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1749// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001750func BinaryFactory() android.Module {
1751 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001752
Colin Cross36242852017-06-23 15:06:31 -07001753 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001754 &module.Module.properties,
1755 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001756 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001757 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001758 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001759
Colin Cross9ae1b922018-06-26 17:59:05 -07001760 module.Module.properties.Installable = proptools.BoolPtr(true)
1761
Colin Cross6b4a32d2017-12-05 13:42:45 -08001762 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1763 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001764 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001765}
1766
Colin Cross1b16b0e2019-02-12 14:41:32 -08001767// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1768//
1769// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1770// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001771func BinaryHostFactory() android.Module {
1772 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001773
Colin Cross36242852017-06-23 15:06:31 -07001774 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001775 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001776 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001777 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001778
Colin Cross9ae1b922018-06-26 17:59:05 -07001779 module.Module.properties.Installable = proptools.BoolPtr(true)
1780
Colin Cross6b4a32d2017-12-05 13:42:45 -08001781 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1782 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001783 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001784}
1785
1786//
1787// Java prebuilts
1788//
1789
Colin Cross74d73e22017-08-02 11:05:49 -07001790type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001791 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001792
Nan Zhangea568a42017-11-08 21:20:04 -08001793 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001794
1795 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001796
1797 // List of shared java libs that this module has dependencies to
1798 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001799
1800 // List of files to remove from the jar file(s)
1801 Exclude_files []string
1802
1803 // List of directories to remove from the jar file(s)
1804 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001805
1806 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001807 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001808}
1809
1810type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001811 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001812 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001813 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001814
Colin Cross74d73e22017-08-02 11:05:49 -07001815 properties ImportProperties
1816
Colin Cross0a6e0072017-08-30 14:24:55 -07001817 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001818 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001819}
1820
Colin Cross83bb3162018-06-25 15:48:06 -07001821func (j *Import) sdkVersion() string {
1822 return String(j.properties.Sdk_version)
1823}
1824
1825func (j *Import) minSdkVersion() string {
1826 return j.sdkVersion()
1827}
1828
Colin Cross74d73e22017-08-02 11:05:49 -07001829func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001830 return &j.prebuilt
1831}
1832
Colin Cross74d73e22017-08-02 11:05:49 -07001833func (j *Import) PrebuiltSrcs() []string {
1834 return j.properties.Jars
1835}
1836
1837func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001838 return j.prebuilt.Name(j.ModuleBase.Name())
1839}
1840
Colin Cross74d73e22017-08-02 11:05:49 -07001841func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001842 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001843}
1844
Colin Cross74d73e22017-08-02 11:05:49 -07001845func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001846 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001847
Nan Zhang4c819fb2018-08-27 18:31:46 -07001848 jarName := ctx.ModuleName() + ".jar"
1849 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001850 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1851 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001852 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001853 inputFile := outputFile
1854 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1855 TransformJetifier(ctx, outputFile, inputFile)
1856 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001857 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001858
1859 ctx.VisitDirectDeps(func(module android.Module) {
1860 otherName := ctx.OtherModuleName(module)
1861 tag := ctx.OtherModuleDependencyTag(module)
1862
1863 switch dep := module.(type) {
1864 case Dependency:
1865 switch tag {
1866 case libTag, staticLibTag:
1867 // sdk lib names from dependencies are re-exported
1868 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1869 }
1870 case SdkLibraryDependency:
1871 switch tag {
1872 case libTag:
1873 // names of sdk libs that are directly depended are exported
1874 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1875 }
1876 }
1877 })
1878
1879 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001880 if Bool(j.properties.Installable) {
1881 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1882 ctx.ModuleName()+".jar", outputFile)
1883 }
Colin Cross2fe66872015-03-30 17:20:39 -07001884}
1885
Colin Cross74d73e22017-08-02 11:05:49 -07001886var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001887
Nan Zhanged19fc32017-10-19 13:06:22 -07001888func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001889 if j.combinedClasspathFile == nil {
1890 return nil
1891 }
Colin Cross37f6d792018-07-12 12:28:41 -07001892 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001893}
1894
1895func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001896 if j.combinedClasspathFile == nil {
1897 return nil
1898 }
Colin Cross37f6d792018-07-12 12:28:41 -07001899 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001900}
1901
Colin Cross331a1212018-08-15 20:40:52 -07001902func (j *Import) ResourceJars() android.Paths {
1903 return nil
1904}
1905
1906func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001907 if j.combinedClasspathFile == nil {
1908 return nil
1909 }
Colin Cross331a1212018-08-15 20:40:52 -07001910 return android.Paths{j.combinedClasspathFile}
1911}
1912
Colin Crossf24a22a2019-01-31 14:12:44 -08001913func (j *Import) DexJar() android.Path {
1914 return nil
1915}
1916
Colin Cross74d73e22017-08-02 11:05:49 -07001917func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001918 return nil
1919}
1920
Jiyong Park1be96912018-05-28 18:02:19 +09001921func (j *Import) ExportedSdkLibs() []string {
1922 return j.exportedSdkLibs
1923}
1924
albaltai36ff7dc2018-12-25 14:35:23 +08001925// Add compile time check for interface implementation
1926var _ android.IDEInfo = (*Import)(nil)
1927var _ android.IDECustomizedModuleName = (*Import)(nil)
1928
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001929// Collect information for opening IDE project files in java/jdeps.go.
1930const (
1931 removedPrefix = "prebuilt_"
1932)
1933
1934func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1935 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1936}
1937
1938func (j *Import) IDECustomizedModuleName() string {
1939 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1940 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1941 // solution to get the Import name.
1942 name := j.Name()
1943 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001944 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001945 }
1946 return name
1947}
1948
Colin Cross74d73e22017-08-02 11:05:49 -07001949var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001950
Colin Cross1b16b0e2019-02-12 14:41:32 -08001951// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1952//
1953// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
1954// compiled against an Android classpath.
1955//
1956// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
1957// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07001958func ImportFactory() android.Module {
1959 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07001960
Colin Cross74d73e22017-08-02 11:05:49 -07001961 module.AddProperties(&module.properties)
1962
1963 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001964 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07001965 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001966}
1967
Colin Cross1b16b0e2019-02-12 14:41:32 -08001968// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
1969// module.
1970//
1971// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
1972// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07001973func ImportFactoryHost() android.Module {
1974 module := &Import{}
1975
1976 module.AddProperties(&module.properties)
1977
1978 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07001979 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07001980 return module
1981}
1982
Colin Cross42be7612019-02-21 18:12:14 -08001983// dex_import module
1984
1985type DexImportProperties struct {
1986 Jars []string
1987}
1988
1989type DexImport struct {
1990 android.ModuleBase
1991 android.DefaultableModuleBase
1992 prebuilt android.Prebuilt
1993
1994 properties DexImportProperties
1995
1996 dexJarFile android.Path
1997 maybeStrippedDexJarFile android.Path
1998
1999 dexpreopter
2000}
2001
2002func (j *DexImport) Prebuilt() *android.Prebuilt {
2003 return &j.prebuilt
2004}
2005
2006func (j *DexImport) PrebuiltSrcs() []string {
2007 return j.properties.Jars
2008}
2009
2010func (j *DexImport) Name() string {
2011 return j.prebuilt.Name(j.ModuleBase.Name())
2012}
2013
2014func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
2015 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2016}
2017
2018func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2019 if len(j.properties.Jars) != 1 {
2020 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2021 }
2022
2023 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2024 j.dexpreopter.isInstallable = true
2025 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2026
2027 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2028 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2029
2030 if j.dexpreopter.uncompressedDex {
2031 rule := android.NewRuleBuilder()
2032
2033 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2034 rule.Temporary(temporary)
2035
2036 // use zip2zip to uncompress classes*.dex files
2037 rule.Command().
2038 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2039 FlagWithInput("-i ", inputJar).
2040 FlagWithOutput("-o ", temporary).
2041 FlagWithArg("-0 ", "'classes*.dex'")
2042
2043 // use zipalign to align uncompressed classes*.dex files
2044 rule.Command().
2045 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2046 Flag("-f").
2047 Text("4").
2048 Input(temporary).
2049 Output(dexOutputFile)
2050
2051 rule.DeleteTemporaryFiles()
2052
2053 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2054 } else {
2055 ctx.Build(pctx, android.BuildParams{
2056 Rule: android.Cp,
2057 Input: inputJar,
2058 Output: dexOutputFile,
2059 })
2060 }
2061
2062 j.dexJarFile = dexOutputFile
2063
2064 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2065
2066 j.maybeStrippedDexJarFile = dexOutputFile
2067
2068 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2069 ctx.ModuleName()+".jar", dexOutputFile)
2070}
2071
2072func (j *DexImport) DexJar() android.Path {
2073 return j.dexJarFile
2074}
2075
2076// dex_import imports a `.jar` file containing classes.dex files.
2077//
2078// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2079// to the device.
2080func DexImportFactory() android.Module {
2081 module := &DexImport{}
2082
2083 module.AddProperties(&module.properties)
2084
2085 android.InitPrebuiltModule(module, &module.properties.Jars)
2086 InitJavaModule(module, android.DeviceSupported)
2087 return module
2088}
2089
Colin Cross89536d42017-07-07 14:35:50 -07002090//
2091// Defaults
2092//
2093type Defaults struct {
2094 android.ModuleBase
2095 android.DefaultsModuleBase
2096}
2097
2098func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2099}
2100
Colin Cross1b16b0e2019-02-12 14:41:32 -08002101// java_defaults provides a set of properties that can be inherited by other java or android modules.
2102//
2103// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2104// property in the defaults module that exists in the depending module will be prepended to the depending module's
2105// value for that property.
2106//
2107// Example:
2108//
2109// java_defaults {
2110// name: "example_defaults",
2111// srcs: ["common/**/*.java"],
2112// javacflags: ["-Xlint:all"],
2113// aaptflags: ["--auto-add-overlay"],
2114// }
2115//
2116// java_library {
2117// name: "example",
2118// defaults: ["example_defaults"],
2119// srcs: ["example/**/*.java"],
2120// }
2121//
2122// is functionally identical to:
2123//
2124// java_library {
2125// name: "example",
2126// srcs: [
2127// "common/**/*.java",
2128// "example/**/*.java",
2129// ],
2130// javacflags: ["-Xlint:all"],
2131// }
Colin Cross89536d42017-07-07 14:35:50 -07002132func defaultsFactory() android.Module {
2133 return DefaultsFactory()
2134}
2135
2136func DefaultsFactory(props ...interface{}) android.Module {
2137 module := &Defaults{}
2138
2139 module.AddProperties(props...)
2140 module.AddProperties(
2141 &CompilerProperties{},
2142 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002143 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002144 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002145 &aaptProperties{},
2146 &androidLibraryProperties{},
2147 &appProperties{},
2148 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002149 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002150 &ImportProperties{},
2151 &AARImportProperties{},
2152 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002153 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002154 )
2155
2156 android.InitDefaultsModule(module)
2157
2158 return module
2159}
Nan Zhangea568a42017-11-08 21:20:04 -08002160
2161var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002162var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002163var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002164var inList = android.InList