blob: 5f4a0909366e17f2559293d95ac864c5daf2deae [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
Colin Cross7d5136f2015-05-11 13:39:40 -070085 // list of module-specific flags that will be used for javac compiles
86 Javacflags []string `android:"arch_variant"`
87
Zoran Jovanovic8736ce22018-08-21 17:10:29 +020088 // list of module-specific flags that will be used for kotlinc compiles
89 Kotlincflags []string `android:"arch_variant"`
90
Colin Cross7d5136f2015-05-11 13:39:40 -070091 // list of of java libraries that will be in the classpath
Colin Crosse8dc34a2017-07-19 11:22:16 -070092 Libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070093
94 // list of java libraries that will be compiled into the resulting jar
Colin Crosse8dc34a2017-07-19 11:22:16 -070095 Static_libs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -070096
97 // manifest file to be included in resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -080098 Manifest *string `android:"path"`
Colin Cross7d5136f2015-05-11 13:39:40 -070099
Colin Cross540eff82017-06-22 17:01:52 -0700100 // if not blank, run jarjar using the specified rules file
Colin Cross27b922f2019-03-04 22:35:41 -0800101 Jarjar_rules *string `android:"path,arch_variant"`
Colin Cross64162712017-08-08 13:17:59 -0700102
103 // If not blank, set the java version passed to javac as -source and -target
104 Java_version *string
Colin Cross2c429dc2017-08-31 16:45:16 -0700105
Colin Cross9ae1b922018-06-26 17:59:05 -0700106 // If set to true, allow this module to be dexed and installed on devices. Has no
107 // effect on host modules, which are always considered installable.
Colin Cross2c429dc2017-08-31 16:45:16 -0700108 Installable *bool
Colin Cross32f676a2017-09-06 13:41:06 -0700109
Colin Cross0f37af02017-09-27 17:42:05 -0700110 // If set to true, include sources used to compile the module in to the final jar
111 Include_srcs *bool
112
Vladimir Marko0975ee02019-04-02 10:29:55 +0100113 // If not empty, classes are restricted to the specified packages and their sub-packages.
114 // This restriction is checked after applying jarjar rules and including static libs.
115 Permitted_packages []string
116
Colin Crossbe9cdb82019-01-21 21:37:16 -0800117 // List of modules to use as annotation processors
118 Plugins []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700119
Nan Zhang61eaedb2017-11-02 13:28:15 -0700120 // The number of Java source entries each Javac instance can process
121 Javac_shard_size *int64
122
Nan Zhang5f8cb422018-02-06 10:34:32 -0800123 // Add host jdk tools.jar to bootclasspath
124 Use_tools_jar *bool
125
Colin Cross1369cdb2017-09-29 17:58:17 -0700126 Openjdk9 struct {
127 // List of source files that should only be used when passing -source 1.9
Colin Cross27b922f2019-03-04 22:35:41 -0800128 Srcs []string `android:"path"`
Colin Cross1369cdb2017-09-29 17:58:17 -0700129
130 // List of javac flags that should only be used when passing -source 1.9
131 Javacflags []string
132 }
Colin Crosscb933592017-11-22 13:49:43 -0800133
Colin Cross81440082018-08-15 20:21:55 -0700134 // When compiling language level 9+ .java code in packages that are part of
135 // a system module, patch_module names the module that your sources and
136 // dependencies should be patched into. The Android runtime currently
137 // doesn't implement the JEP 261 module system so this option is only
138 // supported at compile time. It should only be needed to compile tests in
139 // packages that exist in libcore and which are inconvenient to move
140 // elsewhere.
Tobias Thiererdda713d2018-09-19 16:16:19 +0100141 Patch_module *string `android:"arch_variant"`
Colin Cross81440082018-08-15 20:21:55 -0700142
Colin Crosscb933592017-11-22 13:49:43 -0800143 Jacoco struct {
144 // List of classes to include for instrumentation with jacoco to collect coverage
145 // information at runtime when building with coverage enabled. If unset defaults to all
146 // classes.
147 // Supports '*' as the last character of an entry in the list as a wildcard match.
148 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
149 // it matches classes in the package that have the class name as a prefix.
150 Include_filter []string
151
152 // List of classes to exclude from instrumentation with jacoco to collect coverage
153 // information at runtime when building with coverage enabled. Overrides classes selected
154 // by the include_filter property.
155 // Supports '*' as the last character of an entry in the list as a wildcard match.
156 // If preceded by '.' it matches all classes in the package and subpackages, otherwise
157 // it matches classes in the package that have the class name as a prefix.
158 Exclude_filter []string
159 }
160
Andreas Gampef3e5b552018-01-22 21:27:21 -0800161 Errorprone struct {
162 // List of javac flags that should only be used when running errorprone.
163 Javacflags []string
164 }
165
Colin Cross0f2ee152017-12-14 15:22:43 -0800166 Proto struct {
167 // List of extra options that will be passed to the proto generator.
168 Output_params []string
169 }
170
Colin Crosscb933592017-11-22 13:49:43 -0800171 Instrument bool `blueprint:"mutated"`
Alex Light7f004a72019-02-21 13:27:37 -0800172
173 // List of files to include in the META-INF/services folder of the resulting jar.
Colin Cross27b922f2019-03-04 22:35:41 -0800174 Services []string `android:"path,arch_variant"`
Colin Cross540eff82017-06-22 17:01:52 -0700175}
176
Colin Cross89536d42017-07-07 14:35:50 -0700177type CompilerDeviceProperties struct {
Colin Cross540eff82017-06-22 17:01:52 -0700178 // list of module-specific flags that will be used for dex compiles
179 Dxflags []string `android:"arch_variant"`
180
Colin Cross83bb3162018-06-25 15:48:06 -0700181 // if not blank, set to the version of the sdk to compile against. Defaults to compiling against the current
182 // sdk if platform_apis is not set.
Nan Zhangea568a42017-11-08 21:20:04 -0800183 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700184
Colin Cross83bb3162018-06-25 15:48:06 -0700185 // if not blank, set the minimum version of the sdk that the compiled artifacts will run against.
186 // Defaults to sdk_version if not set.
187 Min_sdk_version *string
188
Dan Willemsen419290a2018-10-31 15:28:47 -0700189 // if not blank, set the targetSdkVersion in the AndroidManifest.xml.
190 // Defaults to sdk_version if not set.
191 Target_sdk_version *string
192
Colin Cross6af2e492018-05-22 11:12:33 -0700193 // if true, compile against the platform APIs instead of an SDK.
194 Platform_apis *bool
195
Colin Crossebe1a512017-11-14 13:12:14 -0800196 Aidl struct {
197 // Top level directories to pass to aidl tool
198 Include_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -0700199
Colin Crossebe1a512017-11-14 13:12:14 -0800200 // Directories rooted at the Android.bp file to pass to aidl tool
201 Local_include_dirs []string
202
203 // directories that should be added as include directories for any aidl sources of modules
204 // that depend on this module, as well as to aidl for this module.
205 Export_include_dirs []string
Martijn Coeneneab15642018-03-09 09:29:59 +0100206
207 // whether to generate traces (for systrace) for this interface
208 Generate_traces *bool
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100209
210 // whether to generate Binder#GetTransaction name method.
211 Generate_get_transaction_name *bool
Colin Crossebe1a512017-11-14 13:12:14 -0800212 }
Colin Cross92430102017-10-09 14:59:32 -0700213
214 // If true, export a copy of the module as a -hostdex module for host testing.
215 Hostdex *bool
Colin Cross1369cdb2017-09-29 17:58:17 -0700216
Colin Cross7f87f4f2019-04-24 13:41:45 -0700217 Target struct {
218 Hostdex struct {
219 // Additional required dependencies to add to -hostdex modules.
220 Required []string
221 }
222 }
223
David Brazdil17ef5632018-06-27 10:27:45 +0100224 // If set to true, compile dex regardless of installable. Defaults to false.
225 Compile_dex *bool
226
Colin Cross66dbc0b2017-12-28 12:23:20 -0800227 Optimize struct {
Colin Crossae5caf52018-05-22 11:11:52 -0700228 // If false, disable all optimization. Defaults to true for android_app and android_test
229 // modules, false for java_library and java_test modules.
Colin Cross66dbc0b2017-12-28 12:23:20 -0800230 Enabled *bool
Sasha Smundak2057f822019-04-16 17:16:58 -0700231 // True if the module containing this has it set by default.
232 EnabledByDefault bool `blueprint:"mutated"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800233
234 // If true, optimize for size by removing unused code. Defaults to true for apps,
235 // false for libraries and tests.
236 Shrink *bool
237
238 // If true, optimize bytecode. Defaults to false.
239 Optimize *bool
240
241 // If true, obfuscate bytecode. Defaults to false.
242 Obfuscate *bool
243
244 // If true, do not use the flag files generated by aapt that automatically keep
245 // classes referenced by the app manifest. Defaults to false.
246 No_aapt_flags *bool
247
248 // Flags to pass to proguard.
249 Proguard_flags []string
250
251 // Specifies the locations of files containing proguard flags.
Colin Cross27b922f2019-03-04 22:35:41 -0800252 Proguard_flags_files []string `android:"path"`
Colin Cross66dbc0b2017-12-28 12:23:20 -0800253 }
254
Colin Cross1369cdb2017-09-29 17:58:17 -0700255 // When targeting 1.9, override the modules to use with --system
256 System_modules *string
Colin Cross5a0dcd52018-10-05 14:20:06 -0700257
258 UncompressDex bool `blueprint:"mutated"`
Colin Cross43f08db2018-11-12 10:13:39 -0800259 IsSDKLibrary bool `blueprint:"mutated"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700260}
261
Sasha Smundak2057f822019-04-16 17:16:58 -0700262func (me *CompilerDeviceProperties) EffectiveOptimizeEnabled() bool {
263 return BoolDefault(me.Optimize.Enabled, me.Optimize.EnabledByDefault)
264}
265
Colin Cross46c9b8b2017-06-22 16:51:17 -0700266// Module contains the properties and members used by all java module types
267type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700268 android.ModuleBase
Colin Cross89536d42017-07-07 14:35:50 -0700269 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +0900270 android.ApexModuleBase
Colin Cross2fe66872015-03-30 17:20:39 -0700271
Colin Cross89536d42017-07-07 14:35:50 -0700272 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700273 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700274 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700275
Colin Cross331a1212018-08-15 20:40:52 -0700276 // jar file containing header classes including static library dependencies, suitable for
277 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700278 headerJarFile android.Path
279
Colin Cross331a1212018-08-15 20:40:52 -0700280 // jar file containing implementation classes including static library dependencies but no
281 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700282 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700283
Colin Cross331a1212018-08-15 20:40:52 -0700284 // jar file containing only resources including from static library dependencies
285 resourceJar android.Path
286
Colin Cross0c4ce212019-05-03 15:28:19 -0700287 // args and dependencies to package source files into a srcjar
288 srcJarArgs []string
289 srcJarDeps android.Paths
290
Colin Cross331a1212018-08-15 20:40:52 -0700291 // jar file containing implementation classes and resources including static library
292 // dependencies
293 implementationAndResourcesJar android.Path
294
295 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700296 dexJarFile android.Path
297
Colin Cross43f08db2018-11-12 10:13:39 -0800298 // output file that contains classes.dex if it should be in the output file
299 maybeStrippedDexJarFile android.Path
300
Colin Crosscb933592017-11-22 13:49:43 -0800301 // output file containing uninstrumented classes that will be instrumented by jacoco
302 jacocoReportClassesFile android.Path
303
Colin Cross66dbc0b2017-12-28 12:23:20 -0800304 // output file containing mapping of obfuscated names
305 proguardDictionary android.Path
306
Colin Cross331a1212018-08-15 20:40:52 -0700307 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700308 outputFile android.Path
309 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700310
Colin Cross635c3b02016-05-18 15:37:25 -0700311 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700312
Colin Cross635c3b02016-05-18 15:37:25 -0700313 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700314
Colin Cross2fe66872015-03-30 17:20:39 -0700315 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700316 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800317
318 // list of .java files and srcjars that was passed to javac
319 compiledJavaSrcs android.Paths
320 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800321
322 // list of extra progurad flag files
323 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900324
Colin Cross094054a2018-10-17 15:10:48 -0700325 // manifest file to use instead of properties.Manifest
326 overrideManifest android.OptionalPath
327
Jiyong Park1be96912018-05-28 18:02:19 +0900328 // list of SDK lib names that this java moudule is exporting
329 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700330
331 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
332 // filter out Exclude_srcs, will be used by android.IDEInfo struct
333 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800334
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800335 // expanded Jarjar_rules
336 expandJarjarRules android.Path
337
Vladimir Marko0975ee02019-04-02 10:29:55 +0100338 // list of additional targets for checkbuild
339 additionalCheckedModules android.Paths
340
Colin Cross988708c2019-05-06 14:04:11 -0700341 // Extra files generated by the module type to be added as java resources.
342 extraResources android.Paths
343
Colin Crossf24a22a2019-01-31 14:12:44 -0800344 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800345 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700346}
347
Colin Cross41955e82019-05-29 14:40:35 -0700348func (j *Module) OutputFiles(tag string) (android.Paths, error) {
349 switch tag {
350 case "":
351 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700352 case ".jar":
353 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700354 default:
355 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
356 }
Colin Cross54250902017-12-05 09:28:08 -0800357}
358
Jiyong Park8fd61922018-11-08 02:50:25 +0900359func (j *Module) DexJarFile() android.Path {
360 return j.dexJarFile
361}
362
Colin Cross41955e82019-05-29 14:40:35 -0700363var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800364
Colin Crossf506d872017-07-19 15:53:04 -0700365type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700366 HeaderJars() android.Paths
367 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700368 ResourceJars() android.Paths
369 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800370 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700371 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900372 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700373 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700374}
375
Jiyong Parkc678ad32018-04-10 13:07:10 +0900376type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700377 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
378 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900379}
380
Nan Zhangb2b33de2018-02-23 11:18:47 -0800381type SrcDependency interface {
382 CompiledSrcs() android.Paths
383 CompiledSrcJars() android.Paths
384}
385
386func (j *Module) CompiledSrcs() android.Paths {
387 return j.compiledJavaSrcs
388}
389
390func (j *Module) CompiledSrcJars() android.Paths {
391 return j.compiledSrcJars
392}
393
394var _ SrcDependency = (*Module)(nil)
395
Colin Cross89536d42017-07-07 14:35:50 -0700396func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
397 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
398 android.InitDefaultableModule(module)
399}
400
Colin Crossbe1da472017-07-07 15:59:46 -0700401type dependencyTag struct {
402 blueprint.BaseDependencyTag
403 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700404}
405
Colin Crossa4f08812018-10-02 22:03:40 -0700406type jniDependencyTag struct {
407 blueprint.BaseDependencyTag
408 target android.Target
409}
410
Colin Crossbe1da472017-07-07 15:59:46 -0700411var (
Colin Cross4b964c02018-10-15 16:18:06 -0700412 staticLibTag = dependencyTag{name: "staticlib"}
413 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800414 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700415 bootClasspathTag = dependencyTag{name: "bootclasspath"}
416 systemModulesTag = dependencyTag{name: "system modules"}
417 frameworkResTag = dependencyTag{name: "framework-res"}
418 frameworkApkTag = dependencyTag{name: "framework-apk"}
419 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800420 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700421 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
422 certificateTag = dependencyTag{name: "certificate"}
423 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700424 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700425)
Colin Cross2fe66872015-03-30 17:20:39 -0700426
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900427func defaultSdkVersion(ctx checkVendorModuleContext) string {
428 if ctx.SocSpecific() || ctx.DeviceSpecific() {
429 return "system_current"
430 }
431 return ""
432}
433
434type checkVendorModuleContext interface {
435 SocSpecific() bool
436 DeviceSpecific() bool
437}
438
Colin Crossfc3674a2017-09-18 17:41:52 -0700439type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700440 useModule, useFiles, useDefaultLibs, invalidVersion bool
441
Colin Cross86a60ae2018-05-29 14:44:55 -0700442 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700443 systemModules string
444
Colin Crossa97c5d32018-03-28 14:58:31 -0700445 frameworkResModule string
446
Colin Cross86a60ae2018-05-29 14:44:55 -0700447 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700448 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100449
450 noStandardLibs, noFrameworksLibs bool
451}
452
453func (s sdkDep) hasStandardLibs() bool {
454 return !s.noStandardLibs
455}
456
457func (s sdkDep) hasFrameworkLibs() bool {
458 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700459}
460
Colin Crossa4f08812018-10-02 22:03:40 -0700461type jniLib struct {
462 name string
463 path android.Path
464 target android.Target
465}
466
Colin Cross0ea8ba82019-06-06 14:33:29 -0700467func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800468 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
469}
470
Colin Cross0ea8ba82019-06-06 14:33:29 -0700471func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800472 return j.shouldInstrument(ctx) &&
473 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
474 ctx.Config().UnbundledBuild())
475}
476
Colin Cross83bb3162018-06-25 15:48:06 -0700477func (j *Module) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900478 return proptools.StringDefault(j.deviceProperties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -0700479}
480
481func (j *Module) minSdkVersion() string {
482 if j.deviceProperties.Min_sdk_version != nil {
483 return *j.deviceProperties.Min_sdk_version
484 }
485 return j.sdkVersion()
486}
487
Dan Willemsen419290a2018-10-31 15:28:47 -0700488func (j *Module) targetSdkVersion() string {
489 if j.deviceProperties.Target_sdk_version != nil {
490 return *j.deviceProperties.Target_sdk_version
491 }
492 return j.sdkVersion()
493}
494
Colin Crossbe1da472017-07-07 15:59:46 -0700495func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700496 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100497 sdkDep := decodeSdkDep(ctx, sdkContext(j))
498 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700499 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700500 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100501 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100502 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700503 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700504 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700505 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100506 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700507 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700508 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700509 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
510 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800511 }
Colin Crossbe1da472017-07-07 15:59:46 -0700512 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700513 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100514 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100515 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100516 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700517 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700518 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100519 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700520 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800521 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800522 if ctx.ModuleName() == "android_stubs_current" ||
523 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700524 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700525 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800526 }
Colin Cross2fe66872015-03-30 17:20:39 -0700527 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700528
Colin Cross42d48b72018-08-29 14:10:52 -0700529 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
530 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700531
Colin Crossbe9cdb82019-01-21 21:37:16 -0800532 ctx.AddFarVariationDependencies([]blueprint.Variation{
533 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
534 }, pluginTag, j.properties.Plugins...)
535
Colin Crossfe17f6f2019-03-28 19:30:56 -0700536 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700537 if j.hasSrcExt(".proto") {
538 protoDeps(ctx, &j.protoProperties)
539 }
Colin Cross93e85952017-08-15 13:34:18 -0700540
541 if j.hasSrcExt(".kt") {
542 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
543 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700544 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
545 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800546 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800547 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
548 }
Colin Cross93e85952017-08-15 13:34:18 -0700549 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800550
551 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700552 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800553 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700554}
555
556func hasSrcExt(srcs []string, ext string) bool {
557 for _, src := range srcs {
558 if filepath.Ext(src) == ext {
559 return true
560 }
561 }
562
563 return false
564}
565
Nan Zhang61eaedb2017-11-02 13:28:15 -0700566func shardPaths(paths android.Paths, shardSize int) []android.Paths {
567 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
568 for len(paths) > shardSize {
569 ret = append(ret, paths[0:shardSize])
570 paths = paths[shardSize:]
571 }
572 if len(paths) > 0 {
573 ret = append(ret, paths)
574 }
575 return ret
576}
577
Colin Cross6af17aa2017-09-20 12:59:05 -0700578func (j *Module) hasSrcExt(ext string) bool {
579 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700580}
581
Colin Cross46c9b8b2017-06-22 16:51:17 -0700582func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700583 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700584
Colin Crossebe1a512017-11-14 13:12:14 -0800585 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
586 aidlIncludes = append(aidlIncludes,
587 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
588 aidlIncludes = append(aidlIncludes,
589 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700590
Colin Cross3047fa22019-04-18 10:56:44 -0700591 var flags []string
592 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700593
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700594 if aidlPreprocess.Valid() {
595 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700596 deps = append(deps, aidlPreprocess.Path())
597 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700598 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700599 }
600
Colin Cross3047fa22019-04-18 10:56:44 -0700601 if len(j.exportAidlIncludeDirs) > 0 {
602 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
603 }
604
605 if len(aidlIncludes) > 0 {
606 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
607 }
608
Colin Cross635c3b02016-05-18 15:37:25 -0700609 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800610 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700611 flags = append(flags, "-I"+src.String())
612 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700613
Martijn Coeneneab15642018-03-09 09:29:59 +0100614 if Bool(j.deviceProperties.Aidl.Generate_traces) {
615 flags = append(flags, "-t")
616 }
617
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100618 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
619 flags = append(flags, "--transaction_names")
620 }
621
Colin Cross3047fa22019-04-18 10:56:44 -0700622 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700623}
624
Colin Cross32f676a2017-09-06 13:41:06 -0700625type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800626 classpath classpath
627 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700628 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800629 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700630 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700631 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700632 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700633 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800634 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700635 srcJars android.Paths
Colin Crossb77043e2019-07-16 13:57:13 -0700636 systemModules *systemModules
Colin Cross6ade34f2017-09-15 13:00:47 -0700637 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700638 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800639 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800640
641 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700642}
Colin Cross2fe66872015-03-30 17:20:39 -0700643
Colin Cross54250902017-12-05 09:28:08 -0800644func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
645 for _, f := range dep.Srcs() {
646 if f.Ext() != ".jar" {
647 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
648 ctx.OtherModuleName(dep.(blueprint.Module)))
649 }
650 }
651}
652
Jiyong Park2d492942018-03-05 17:44:10 +0900653type linkType int
654
655const (
656 javaCore linkType = iota
657 javaSdk
658 javaSystem
659 javaPlatform
660)
661
Jiyong Park46f78fb2018-10-20 16:33:17 +0900662func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700663 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700664 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900665 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
666 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100667 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900668 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100669 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900670 return javaCore, false
671 case name == "android_system_stubs_current":
672 return javaSystem, true
673 case strings.HasPrefix(ver, "system_"):
674 return javaSystem, false
675 case name == "android_test_stubs_current":
676 return javaSystem, true
677 case strings.HasPrefix(ver, "test_"):
678 return javaPlatform, false
679 case name == "android_stubs_current":
680 return javaSdk, true
681 case ver == "current":
682 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100683 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900684 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700685 default:
686 if _, err := strconv.Atoi(ver); err != nil {
687 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
688 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900689 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900690 }
691}
692
Jiyong Park750e5572018-01-31 00:20:13 +0900693func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700694 if ctx.Host() {
695 return
696 }
697
Jiyong Park46f78fb2018-10-20 16:33:17 +0900698 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
699 if stubs {
700 return
701 }
702 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900703 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."
704
705 switch myLinkType {
706 case javaCore:
707 if otherLinkType != javaCore {
708 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 +0900709 ctx.OtherModuleName(to))
710 }
Jiyong Park2d492942018-03-05 17:44:10 +0900711 break
712 case javaSdk:
713 if otherLinkType != javaCore && otherLinkType != javaSdk {
714 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
715 ctx.OtherModuleName(to))
716 }
717 break
718 case javaSystem:
719 if otherLinkType == javaPlatform {
720 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
721 ctx.OtherModuleName(to))
722 }
723 break
724 case javaPlatform:
725 // no restriction on link-type
726 break
Jiyong Park750e5572018-01-31 00:20:13 +0900727 }
728}
729
Colin Cross32f676a2017-09-06 13:41:06 -0700730func (j *Module) collectDeps(ctx android.ModuleContext) deps {
731 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700732
Colin Cross300f0382018-03-06 13:11:51 -0800733 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700734 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800735 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700736 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800737 } else if sdkDep.useFiles {
738 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700739 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700740 deps.aidlPreprocess = sdkDep.aidl
741 } else {
742 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800743 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700744 }
745
Colin Crossd11fcda2017-10-23 17:59:01 -0700746 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700747 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700748 tag := ctx.OtherModuleDependencyTag(module)
749
Colin Crossa4f08812018-10-02 22:03:40 -0700750 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700751 // Handled by AndroidApp.collectAppDeps
752 return
753 }
754 if tag == certificateTag {
755 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700756 return
757 }
758
Jiyong Park750e5572018-01-31 00:20:13 +0900759 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700760 switch tag {
761 case bootClasspathTag, libTag, staticLibTag:
762 checkLinkType(ctx, j, to, tag.(dependencyTag))
763 }
Jiyong Park750e5572018-01-31 00:20:13 +0900764 }
Colin Cross54250902017-12-05 09:28:08 -0800765 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800766 case SdkLibraryDependency:
767 switch tag {
768 case libTag:
769 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
770 // names of sdk libs that are directly depended are exported
771 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700772 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800773 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
774 }
Colin Cross54250902017-12-05 09:28:08 -0800775 case Dependency:
776 switch tag {
777 case bootClasspathTag:
778 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700779 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800780 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900781 // sdk lib names from dependencies are re-exported
782 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700783 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800784 case staticLibTag:
785 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
786 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
787 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700788 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900789 // sdk lib names from dependencies are re-exported
790 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700791 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800792 case pluginTag:
793 if plugin, ok := dep.(*Plugin); ok {
794 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
795 if plugin.pluginProperties.Processor_class != nil {
796 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
797 }
798 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
799 } else {
800 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
801 }
Colin Cross54250902017-12-05 09:28:08 -0800802 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100803 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800804 // framework.jar has a one-off dependency on the R.java and Manifest.java files
805 // generated by framework-res.apk
806 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
807 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800808 case frameworkApkTag:
809 if ctx.ModuleName() == "android_stubs_current" ||
810 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700811 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800812 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
813 // resource files out of there for aapt.
814 //
815 // Normally the package rule runs aapt, which includes the resource,
816 // but we're not running that in our package rule so just copy in the
817 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700818 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800819 }
Colin Cross54250902017-12-05 09:28:08 -0800820 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700821 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800822 case kotlinAnnotationsTag:
823 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800824 }
825
Colin Cross54250902017-12-05 09:28:08 -0800826 case android.SourceFileProducer:
827 switch tag {
828 case libTag:
829 checkProducesJars(ctx, dep)
830 deps.classpath = append(deps.classpath, dep.Srcs()...)
831 case staticLibTag:
832 checkProducesJars(ctx, dep)
833 deps.classpath = append(deps.classpath, dep.Srcs()...)
834 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
835 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800836 }
837 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700838 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700839 case systemModulesTag:
840 if deps.systemModules != nil {
841 panic("Found two system module dependencies")
842 }
843 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000844 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700845 panic("Missing directory for system module dependency")
846 }
Colin Crossb77043e2019-07-16 13:57:13 -0700847 deps.systemModules = &systemModules{sm.outputDir, sm.outputDeps}
Colin Cross2fe66872015-03-30 17:20:39 -0700848 }
Colin Crossec7a0422017-07-07 14:47:12 -0700849 }
Colin Cross2fe66872015-03-30 17:20:39 -0700850 })
851
Jiyong Park1be96912018-05-28 18:02:19 +0900852 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
853
Colin Cross32f676a2017-09-06 13:41:06 -0700854 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700855}
856
Colin Cross83bb3162018-06-25 15:48:06 -0700857func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700858 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800859 v := sdkContext.sdkVersion()
860 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100861 if ctx.Config().IsPdkBuild() &&
862 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700863 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800864 latestSdkVersion := 0
865 if len(sdkVersions) > 0 {
866 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
867 }
868 v = strconv.Itoa(latestSdkVersion)
869 }
870
871 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700872 if err != nil {
873 ctx.PropertyErrorf("sdk_version", "%s", err)
874 }
Nan Zhang357466b2018-04-17 17:38:36 -0700875 if javaVersion != "" {
876 ret = javaVersion
877 } else if ctx.Device() && sdk <= 23 {
878 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100879 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700880 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100881 } else if ctx.Device() &&
882 sdkContext.sdkVersion() != "" &&
883 sdkContext.sdkVersion() != "none" &&
884 sdkContext.sdkVersion() != "core_platform" &&
885 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700886 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
887 ret = "1.8"
888 } else {
889 ret = "1.9"
890 }
891
892 return ret
893}
894
Nan Zhanged19fc32017-10-19 13:06:22 -0700895func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700896
Colin Crossf03c82b2015-04-13 13:53:40 -0700897 var flags javaBuilderFlags
898
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100899 // javaVersion flag.
900 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
901
Nan Zhanged19fc32017-10-19 13:06:22 -0700902 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700903 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100904 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700905 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700906 }
Colin Cross6510f912017-11-29 00:27:14 -0800907 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700908 // Override the -g flag passed globally to remove local variable debug info to reduce
909 // disk and memory usage.
910 javacFlags = append(javacFlags, "-g:source,lines")
911 }
Colin Cross64162712017-08-08 13:17:59 -0700912
Colin Cross66548102018-06-19 22:47:35 -0700913 if ctx.Config().RunErrorProne() {
914 if config.ErrorProneClasspath == nil {
915 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
916 }
917
918 errorProneFlags := []string{
919 "-Xplugin:ErrorProne",
920 "${config.ErrorProneChecks}",
921 }
922 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
923
924 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
925 "'" + strings.Join(errorProneFlags, " ") + "'"
926 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800927 }
928
Nan Zhanged19fc32017-10-19 13:06:22 -0700929 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800930 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
931 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700932 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800933
Colin Crossbe9cdb82019-01-21 21:37:16 -0800934 flags.processor = strings.Join(deps.processorClasses, ",")
935
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100936 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100937 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800938 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
939 // Give host-side tools a version of OpenJDK's standard libraries
940 // close to what they're targeting. As of Dec 2017, AOSP is only
941 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
942 //
943 // When building with OpenJDK 8, the following should have no
944 // effect since those jars would be available by default.
945 //
946 // When building with OpenJDK 9 but targeting a version < 1.8,
947 // putting them on the bootclasspath means that:
948 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
949 // b) references to existing APIs are not reinterpreted in an
950 // OpenJDK 9-specific way, eg. calls to subclasses of
951 // java.nio.Buffer as in http://b/70862583
952 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
953 flags.bootClasspath = append(flags.bootClasspath,
954 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
955 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800956 if Bool(j.properties.Use_tools_jar) {
957 flags.bootClasspath = append(flags.bootClasspath,
958 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
959 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800960 }
961
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100962 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800963 // Manually specify build directory in case it is not under the repo root.
964 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
965 // just adding a symlink under the root doesn't help.)
966 patchPaths := ".:" + ctx.Config().BuildDir()
967 classPath := flags.classpath.FormJavaClassPath("")
968 if classPath != "" {
969 patchPaths += ":" + classPath
970 }
971 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700972 }
973
Nan Zhanged19fc32017-10-19 13:06:22 -0700974 // systemModules
Colin Crossb77043e2019-07-16 13:57:13 -0700975 flags.systemModules = deps.systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700976
Nan Zhanged19fc32017-10-19 13:06:22 -0700977 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700978 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700979
Colin Cross81440082018-08-15 20:21:55 -0700980 if len(javacFlags) > 0 {
981 // optimization.
982 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
983 flags.javacFlags = "$javacFlags"
984 }
985
Nan Zhanged19fc32017-10-19 13:06:22 -0700986 return flags
987}
Colin Crossc0b06f12015-04-08 13:03:43 -0700988
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700989func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
990
Colin Crossebe1a512017-11-14 13:12:14 -0800991 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700992
993 deps := j.collectDeps(ctx)
994 flags := j.collectBuilderFlags(ctx, deps)
995
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100996 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700997 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
998 }
Colin Cross8a497952019-03-05 22:25:09 -0800999 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -07001000 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -08001001 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -07001002 }
1003
Colin Crossaf050172017-11-15 23:01:59 -08001004 srcFiles = j.genSources(ctx, srcFiles, flags)
1005
1006 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -07001007 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001008 if aaptSrcJar != nil {
1009 srcJars = append(srcJars, aaptSrcJar)
1010 }
Colin Crossb7a63242015-04-16 14:09:14 -07001011
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001012 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1013 // that IDEInfo struct will use
1014 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1015
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001016 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001017 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001018 }
1019
Colin Cross1ee23172017-10-18 14:44:18 -07001020 jarName := ctx.ModuleName() + ".jar"
1021
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001022 javaSrcFiles := srcFiles.FilterByExt(".java")
1023 var uniqueSrcFiles android.Paths
1024 set := make(map[string]bool)
1025 for _, v := range javaSrcFiles {
1026 if _, found := set[v.String()]; !found {
1027 set[v.String()] = true
1028 uniqueSrcFiles = append(uniqueSrcFiles, v)
1029 }
1030 }
1031
Colin Cross55f63ea2018-08-27 12:37:09 -07001032 var kotlinJars android.Paths
1033
Colin Cross93e85952017-08-15 13:34:18 -07001034 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001035 // user defined kotlin flags.
1036 kotlincFlags := j.properties.Kotlincflags
1037 CheckKotlincFlags(ctx, kotlincFlags)
1038
Colin Cross93e85952017-08-15 13:34:18 -07001039 // If there are kotlin files, compile them first but pass all the kotlin and java files
1040 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1041 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001042 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001043 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001044 kotlincFlags = append(kotlincFlags, "-no-jdk")
1045 }
1046 if len(kotlincFlags) > 0 {
1047 // optimization.
1048 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1049 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001050 }
1051
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001052 var kotlinSrcFiles android.Paths
1053 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1054 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1055
Colin Crossafbb1732019-01-17 15:42:52 -08001056 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1057 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1058
1059 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1060 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1061
1062 if len(flags.processorPath) > 0 {
1063 // Use kapt for annotation processing
1064 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1065 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1066 srcJars = append(srcJars, kaptSrcJar)
1067 // Disable annotation processing in javac, it's already been handled by kapt
1068 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001069 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001070 }
Colin Cross93e85952017-08-15 13:34:18 -07001071
Colin Cross1ee23172017-10-18 14:44:18 -07001072 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001073 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001074 if ctx.Failed() {
1075 return
1076 }
1077
1078 // Make javac rule depend on the kotlinc rule
1079 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001080
Colin Cross93e85952017-08-15 13:34:18 -07001081 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001082 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001083 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001084 }
1085
Colin Cross55f63ea2018-08-27 12:37:09 -07001086 jars := append(android.Paths(nil), kotlinJars...)
1087
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001088 // Store the list of .java files that was passed to javac
1089 j.compiledJavaSrcs = uniqueSrcFiles
1090 j.compiledSrcJars = srcJars
1091
Nan Zhang61eaedb2017-11-02 13:28:15 -07001092 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001093 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001094 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1095 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001096 // Formerly, there was a check here that prevented annotation processors
1097 // from being used when sharding was enabled, as some annotation processors
1098 // do not function correctly in sharded environments. It was removed to
1099 // allow for the use of annotation processors that do function correctly
1100 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001101 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001102 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001103 if ctx.Failed() {
1104 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001105 }
1106 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001107 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001108 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001109 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001110 // If error-prone is enabled, add an additional rule to compile the java files into
1111 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001112 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001113 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1114 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001115 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001116 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001117 extraJarDeps = append(extraJarDeps, errorprone)
1118 }
1119
Nan Zhang61eaedb2017-11-02 13:28:15 -07001120 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001121 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001122 shardSize := int(*(j.properties.Javac_shard_size))
1123 var shardSrcs []android.Paths
1124 if len(uniqueSrcFiles) > 0 {
1125 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1126 for idx, shardSrc := range shardSrcs {
1127 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1128 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1129 jars = append(jars, classes)
1130 }
1131 }
1132 if len(srcJars) > 0 {
1133 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1134 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1135 jars = append(jars, classes)
1136 }
1137 } else {
1138 classes := android.PathForModuleOut(ctx, "javac", jarName)
1139 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1140 jars = append(jars, classes)
1141 }
Colin Crossd6891432017-09-27 17:39:56 -07001142 if ctx.Failed() {
1143 return
1144 }
Colin Cross2fe66872015-03-30 17:20:39 -07001145 }
1146
Colin Cross0c4ce212019-05-03 15:28:19 -07001147 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1148
1149 var includeSrcJar android.WritablePath
1150 if Bool(j.properties.Include_srcs) {
1151 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1152 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1153 }
1154
Colin Crosscedd4762018-09-13 11:26:19 -07001155 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1156 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001157 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001158 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001159
1160 var resArgs []string
1161 var resDeps android.Paths
1162
1163 resArgs = append(resArgs, dirArgs...)
1164 resDeps = append(resDeps, dirDeps...)
1165
1166 resArgs = append(resArgs, fileArgs...)
1167 resDeps = append(resDeps, fileDeps...)
1168
Colin Cross988708c2019-05-06 14:04:11 -07001169 resArgs = append(resArgs, extraArgs...)
1170 resDeps = append(resDeps, extraDeps...)
1171
Colin Cross40a36712017-09-27 17:41:35 -07001172 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001173 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001174 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001175 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001176 if ctx.Failed() {
1177 return
1178 }
1179 }
1180
Colin Cross0c4ce212019-05-03 15:28:19 -07001181 var resourceJars android.Paths
1182 if j.resourceJar != nil {
1183 resourceJars = append(resourceJars, j.resourceJar)
1184 }
1185 if Bool(j.properties.Include_srcs) {
1186 resourceJars = append(resourceJars, includeSrcJar)
1187 }
1188 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001189
Colin Cross0c4ce212019-05-03 15:28:19 -07001190 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001191 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001192 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001193 false, nil, nil)
1194 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001195 } else if len(resourceJars) == 1 {
1196 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001197 }
1198
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001199 if len(deps.staticJars) > 0 {
1200 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001201 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001202
Colin Cross094054a2018-10-17 15:10:48 -07001203 manifest := j.overrideManifest
1204 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001205 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001206 }
Colin Cross635acc92017-09-12 22:50:46 -07001207
Colin Cross8a497952019-03-05 22:25:09 -08001208 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001209 if len(services) > 0 {
1210 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1211 var zipargs []string
1212 for _, file := range services {
1213 serviceFile := file.String()
1214 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1215 }
1216 ctx.Build(pctx, android.BuildParams{
1217 Rule: zip,
1218 Output: servicesJar,
1219 Implicits: services,
1220 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001221 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001222 },
1223 })
1224 jars = append(jars, servicesJar)
1225 }
1226
Colin Cross0a6e0072017-08-30 14:24:55 -07001227 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001228 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001229 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001230
1231 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001232 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1233 // Optimization: skip the combine step if there is nothing to do
1234 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1235 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1236 // any if len(jars) == 1.
1237 outputFile = moduleOutPath
1238 } else {
1239 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1240 ctx.Build(pctx, android.BuildParams{
1241 Rule: android.Cp,
1242 Input: jars[0],
1243 Output: combinedJar,
1244 })
1245 outputFile = combinedJar
1246 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001247 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001248 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001249 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001250 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001251 outputFile = combinedJar
1252 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001253
Colin Cross331a1212018-08-15 20:40:52 -07001254 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001255 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001256 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001257 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001258 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001259 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001260
1261 // jarjar resource jar if necessary
1262 if j.resourceJar != nil {
1263 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001264 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001265 j.resourceJar = resourceJarJarFile
1266 }
1267
Colin Cross0a6e0072017-08-30 14:24:55 -07001268 if ctx.Failed() {
1269 return
1270 }
1271 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001272
1273 // Check package restrictions if necessary.
1274 if len(j.properties.Permitted_packages) > 0 {
1275 // Check packages and copy to package-checked file.
1276 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1277 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1278 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1279
1280 if ctx.Failed() {
1281 return
1282 }
1283 }
1284
Nan Zhanged19fc32017-10-19 13:06:22 -07001285 j.implementationJarFile = outputFile
1286 if j.headerJarFile == nil {
1287 j.headerJarFile = j.implementationJarFile
1288 }
Colin Cross2fe66872015-03-30 17:20:39 -07001289
Colin Cross6510f912017-11-29 00:27:14 -08001290 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001291 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1292 j.properties.Instrument = true
1293 }
1294 }
1295
Colin Cross3144dfc2018-01-03 15:06:47 -08001296 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001297 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1298 }
1299
Colin Cross331a1212018-08-15 20:40:52 -07001300 // merge implementation jar with resources if necessary
1301 implementationAndResourcesJar := outputFile
1302 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001303 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001304 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001305 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001306 false, nil, nil)
1307 implementationAndResourcesJar = combinedJar
1308 }
1309
1310 j.implementationAndResourcesJar = implementationAndResourcesJar
1311
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001312 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001313 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001314 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001315 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001316 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001317 if ctx.Failed() {
1318 return
1319 }
Colin Cross331a1212018-08-15 20:40:52 -07001320
Jiyong Park09cb6292019-07-15 15:29:23 +09001321 // Hidden API CSV generation and dex encoding
1322 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1323 j.deviceProperties.UncompressDex)
Colin Cross8faf8fc2019-01-16 15:15:52 -08001324
Colin Cross331a1212018-08-15 20:40:52 -07001325 // merge dex jar with resources if necessary
1326 if j.resourceJar != nil {
1327 jars := android.Paths{dexOutputFile, j.resourceJar}
1328 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1329 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1330 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001331 if j.deviceProperties.UncompressDex {
1332 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1333 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1334 dexOutputFile = combinedAlignedJar
1335 } else {
1336 dexOutputFile = combinedJar
1337 }
Colin Cross331a1212018-08-15 20:40:52 -07001338 }
1339
1340 j.dexJarFile = dexOutputFile
1341
Colin Cross8faf8fc2019-01-16 15:15:52 -08001342 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001343 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1344
1345 j.maybeStrippedDexJarFile = dexOutputFile
1346
Colin Cross3063b782018-08-15 11:19:12 -07001347 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001348
1349 if ctx.Failed() {
1350 return
1351 }
Colin Cross331a1212018-08-15 20:40:52 -07001352 } else {
1353 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001354 }
Colin Cross331a1212018-08-15 20:40:52 -07001355
Colin Crossb7a63242015-04-16 14:09:14 -07001356 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001357
1358 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1359 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001360}
1361
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001362// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1363// since some of these flags may be used internally.
1364func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1365 for _, flag := range flags {
1366 flag = strings.TrimSpace(flag)
1367
1368 if !strings.HasPrefix(flag, "-") {
1369 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1370 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1371 ctx.PropertyErrorf("kotlincflags",
1372 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1373 } else if inList(flag, config.KotlincIllegalFlags) {
1374 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1375 } else if flag == "-include-runtime" {
1376 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1377 } else {
1378 args := strings.Split(flag, " ")
1379 if args[0] == "-kotlin-home" {
1380 ctx.PropertyErrorf("kotlincflags",
1381 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1382 }
1383 }
1384 }
1385}
1386
Colin Cross8eadbf02017-10-24 17:46:00 -07001387func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001388 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001389
1390 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001391 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001392 // Compile java sources into turbine.jar.
1393 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1394 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1395 if ctx.Failed() {
1396 return nil
1397 }
1398 jars = append(jars, turbineJar)
1399 }
1400
Colin Cross55f63ea2018-08-27 12:37:09 -07001401 jars = append(jars, extraJars...)
1402
Nan Zhanged19fc32017-10-19 13:06:22 -07001403 // Combine any static header libraries into classes-header.jar. If there is only
1404 // one input jar this step will be skipped.
1405 var headerJar android.Path
1406 jars = append(jars, deps.staticHeaderJars...)
1407
Colin Cross5c6ecc12017-10-23 18:12:27 -07001408 // we cannot skip the combine step for now if there is only one jar
1409 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1410 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001411 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001412 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001413 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001414
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001415 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001416 // Transform classes.jar into classes-jarjar.jar
1417 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001418 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001419 headerJar = jarjarFile
1420 if ctx.Failed() {
1421 return nil
1422 }
1423 }
1424
1425 return headerJar
1426}
1427
Colin Crosscb933592017-11-22 13:49:43 -08001428func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001429 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001430
Colin Cross7a3139e2017-12-19 13:57:50 -08001431 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001432
Colin Cross84c38822018-01-03 15:59:46 -08001433 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001434 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1435
1436 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1437
1438 j.jacocoReportClassesFile = jacocoReportClassesFile
1439
1440 return instrumentedJar
1441}
1442
albaltai36ff7dc2018-12-25 14:35:23 +08001443var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001444
Nan Zhanged19fc32017-10-19 13:06:22 -07001445func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001446 if j.headerJarFile == nil {
1447 return nil
1448 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001449 return android.Paths{j.headerJarFile}
1450}
1451
1452func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001453 if j.implementationJarFile == nil {
1454 return nil
1455 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001456 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001457}
1458
Colin Crossf24a22a2019-01-31 14:12:44 -08001459func (j *Module) DexJar() android.Path {
1460 return j.dexJarFile
1461}
1462
Colin Cross331a1212018-08-15 20:40:52 -07001463func (j *Module) ResourceJars() android.Paths {
1464 if j.resourceJar == nil {
1465 return nil
1466 }
1467 return android.Paths{j.resourceJar}
1468}
1469
1470func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001471 if j.implementationAndResourcesJar == nil {
1472 return nil
1473 }
Colin Cross331a1212018-08-15 20:40:52 -07001474 return android.Paths{j.implementationAndResourcesJar}
1475}
1476
Colin Cross46c9b8b2017-06-22 16:51:17 -07001477func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001478 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001479 return j.exportAidlIncludeDirs
1480}
1481
Jiyong Park1be96912018-05-28 18:02:19 +09001482func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001483 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001484 return j.exportedSdkLibs
1485}
1486
Colin Cross0c4ce212019-05-03 15:28:19 -07001487func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1488 return j.srcJarArgs, j.srcJarDeps
1489}
1490
Colin Cross46c9b8b2017-06-22 16:51:17 -07001491var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001492
Colin Cross46c9b8b2017-06-22 16:51:17 -07001493func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001494 return j.logtagsSrcs
1495}
1496
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001497// Collect information for opening IDE project files in java/jdeps.go.
1498func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1499 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1500 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001501 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001502 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001503 if j.expandJarjarRules != nil {
1504 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001505 }
1506}
1507
1508func (j *Module) CompilerDeps() []string {
1509 jdeps := []string{}
1510 jdeps = append(jdeps, j.properties.Libs...)
1511 jdeps = append(jdeps, j.properties.Static_libs...)
1512 return jdeps
1513}
1514
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001515func (j *Module) hasCode(ctx android.ModuleContext) bool {
1516 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1517 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1518}
1519
Colin Cross2fe66872015-03-30 17:20:39 -07001520//
1521// Java libraries (.jar file)
1522//
1523
Colin Crossf506d872017-07-19 15:53:04 -07001524type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001525 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001526}
1527
Colin Cross42be7612019-02-21 18:12:14 -08001528func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001529 // Store uncompressed (and do not strip) dex files from boot class path jars.
1530 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1531 return true
1532 }
1533
1534 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001535 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001536 return true
1537 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001538 if ctx.Config().UncompressPrivAppDex() &&
1539 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1540 return true
1541 }
1542
Colin Cross2fc72f62018-12-21 12:59:54 -08001543 return false
1544}
1545
Colin Crossf506d872017-07-19 15:53:04 -07001546func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001547 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1548 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001549 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001550 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001551 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001552 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001553
Jiyong Park7f7766d2019-07-25 22:02:35 +09001554 exclusivelyForApex := android.InAnyApex(ctx.ModuleName()) && !j.IsForPlatform()
1555 if (Bool(j.properties.Installable) || ctx.Host()) && !exclusivelyForApex {
Colin Cross2c429dc2017-08-31 16:45:16 -07001556 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1557 ctx.ModuleName()+".jar", j.outputFile)
1558 }
Colin Crossb7a63242015-04-16 14:09:14 -07001559}
1560
Colin Crossf506d872017-07-19 15:53:04 -07001561func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001562 j.deps(ctx)
1563}
1564
Colin Cross1b16b0e2019-02-12 14:41:32 -08001565// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1566//
1567// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1568// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1569// as a `static_libs` dependency of another module.
1570//
1571// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1572// a device.
1573//
1574// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1575// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001576func LibraryFactory() android.Module {
1577 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001578
Colin Cross9ae1b922018-06-26 17:59:05 -07001579 module.AddProperties(
1580 &module.Module.properties,
1581 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001582 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001583 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001584
Colin Cross9ae1b922018-06-26 17:59:05 -07001585 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001586 android.InitApexModule(module)
Colin Cross9ae1b922018-06-26 17:59:05 -07001587 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001588}
1589
Colin Cross1b16b0e2019-02-12 14:41:32 -08001590// java_library_static is an obsolete alias for java_library.
1591func LibraryStaticFactory() android.Module {
1592 return LibraryFactory()
1593}
1594
1595// java_library_host builds and links sources into a `.jar` file for the host.
1596//
1597// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1598// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001599func LibraryHostFactory() android.Module {
1600 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001601
Colin Cross6af17aa2017-09-20 12:59:05 -07001602 module.AddProperties(
1603 &module.Module.properties,
1604 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001605
Colin Cross9ae1b922018-06-26 17:59:05 -07001606 module.Module.properties.Installable = proptools.BoolPtr(true)
1607
Colin Cross89536d42017-07-07 14:35:50 -07001608 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09001609 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001610 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001611}
1612
1613//
Colin Crossb628ea52018-08-14 16:42:33 -07001614// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001615//
1616
1617type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001618 // list of compatibility suites (for example "cts", "vts") that the module should be
1619 // installed into.
1620 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001621
1622 // the name of the test configuration (for example "AndroidTest.xml") that should be
1623 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001624 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001625
Jack He33338892018-09-19 02:21:28 -07001626 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1627 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001628 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001629
Colin Crossd96ca352018-08-10 16:06:24 -07001630 // list of files or filegroup modules that provide data that should be installed alongside
1631 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001632 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001633}
1634
Paul Duffin42df1442019-03-20 12:45:53 +00001635type testHelperLibraryProperties struct {
1636 // list of compatibility suites (for example "cts", "vts") that the module should be
1637 // installed into.
1638 Test_suites []string `android:"arch_variant"`
1639}
1640
Colin Cross05638fc2018-04-09 18:40:24 -07001641type Test struct {
1642 Library
1643
1644 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001645
1646 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001647 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001648}
1649
Paul Duffin42df1442019-03-20 12:45:53 +00001650type TestHelperLibrary struct {
1651 Library
1652
1653 testHelperLibraryProperties testHelperLibraryProperties
1654}
1655
Colin Cross303e21f2018-08-07 16:49:25 -07001656func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001657 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 -08001658 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001659
1660 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001661}
1662
Paul Duffin42df1442019-03-20 12:45:53 +00001663func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1664 j.Library.GenerateAndroidBuildActions(ctx)
1665}
1666
Colin Cross1b16b0e2019-02-12 14:41:32 -08001667// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1668// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1669//
1670// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1671// compiled against the device bootclasspath.
1672//
1673// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1674// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001675func TestFactory() android.Module {
1676 module := &Test{}
1677
1678 module.AddProperties(
1679 &module.Module.properties,
1680 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001681 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001682 &module.Module.protoProperties,
1683 &module.testProperties)
1684
Colin Cross9ae1b922018-06-26 17:59:05 -07001685 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001686 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001687
Colin Cross05638fc2018-04-09 18:40:24 -07001688 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001689 return module
1690}
1691
Paul Duffin42df1442019-03-20 12:45:53 +00001692// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1693func TestHelperLibraryFactory() android.Module {
1694 module := &TestHelperLibrary{}
1695
1696 module.AddProperties(
1697 &module.Module.properties,
1698 &module.Module.deviceProperties,
1699 &module.Module.dexpreoptProperties,
1700 &module.Module.protoProperties,
1701 &module.testHelperLibraryProperties)
1702
Colin Cross9a4abed2019-04-24 13:19:28 -07001703 module.Module.properties.Installable = proptools.BoolPtr(true)
1704 module.Module.dexpreopter.isTest = true
1705
Paul Duffin42df1442019-03-20 12:45:53 +00001706 InitJavaModule(module, android.HostAndDeviceSupported)
1707 return module
1708}
1709
Colin Cross1b16b0e2019-02-12 14:41:32 -08001710// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1711// allow running the test with `atest` or a `TEST_MAPPING` file.
1712//
1713// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1714// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001715func TestHostFactory() android.Module {
1716 module := &Test{}
1717
1718 module.AddProperties(
1719 &module.Module.properties,
1720 &module.Module.protoProperties,
1721 &module.testProperties)
1722
Colin Cross9ae1b922018-06-26 17:59:05 -07001723 module.Module.properties.Installable = proptools.BoolPtr(true)
1724
Colin Cross05638fc2018-04-09 18:40:24 -07001725 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001726 return module
1727}
1728
1729//
Colin Cross2fe66872015-03-30 17:20:39 -07001730// Java Binaries (.jar file plus wrapper script)
1731//
1732
Colin Crossf506d872017-07-19 15:53:04 -07001733type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001734 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001735 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001736
1737 // Name of the class containing main to be inserted into the manifest as Main-Class.
1738 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001739}
1740
Colin Crossf506d872017-07-19 15:53:04 -07001741type Binary struct {
1742 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001743
Colin Crossf506d872017-07-19 15:53:04 -07001744 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001745
Colin Cross6b4a32d2017-12-05 13:42:45 -08001746 isWrapperVariant bool
1747
Colin Crossc3315992017-12-08 19:12:36 -08001748 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001749 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001750}
1751
Alex Light24237172017-10-26 09:46:21 -07001752func (j *Binary) HostToolPath() android.OptionalPath {
1753 return android.OptionalPathForPath(j.binaryFile)
1754}
1755
Colin Crossf506d872017-07-19 15:53:04 -07001756func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001757 if ctx.Arch().ArchType == android.Common {
1758 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001759 if j.binaryProperties.Main_class != nil {
1760 if j.properties.Manifest != nil {
1761 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1762 }
1763 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1764 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1765 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1766 }
1767
Colin Cross6b4a32d2017-12-05 13:42:45 -08001768 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001769 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001770 // Handle the binary wrapper
1771 j.isWrapperVariant = true
1772
Colin Cross366938f2017-12-11 16:29:02 -08001773 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001774 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001775 } else {
1776 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1777 }
1778
1779 // Depend on the installed jar so that the wrapper doesn't get executed by
1780 // another build rule before the jar has been installed.
1781 jarFile := ctx.PrimaryModule().(*Binary).installFile
1782
1783 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1784 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001785 }
Colin Cross2fe66872015-03-30 17:20:39 -07001786}
1787
Colin Crossf506d872017-07-19 15:53:04 -07001788func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001789 if ctx.Arch().ArchType == android.Common {
1790 j.deps(ctx)
1791 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001792}
1793
Colin Cross1b16b0e2019-02-12 14:41:32 -08001794// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1795// as well.
1796//
1797// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1798// compiled against the device bootclasspath.
1799//
1800// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1801// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001802func BinaryFactory() android.Module {
1803 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001804
Colin Cross36242852017-06-23 15:06:31 -07001805 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001806 &module.Module.properties,
1807 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001808 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001809 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001810 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001811
Colin Cross9ae1b922018-06-26 17:59:05 -07001812 module.Module.properties.Installable = proptools.BoolPtr(true)
1813
Colin Cross6b4a32d2017-12-05 13:42:45 -08001814 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1815 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001816 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001817}
1818
Colin Cross1b16b0e2019-02-12 14:41:32 -08001819// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1820//
1821// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1822// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001823func BinaryHostFactory() android.Module {
1824 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001825
Colin Cross36242852017-06-23 15:06:31 -07001826 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001827 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001828 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001829 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001830
Colin Cross9ae1b922018-06-26 17:59:05 -07001831 module.Module.properties.Installable = proptools.BoolPtr(true)
1832
Colin Cross6b4a32d2017-12-05 13:42:45 -08001833 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1834 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001835 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001836}
1837
1838//
1839// Java prebuilts
1840//
1841
Colin Cross74d73e22017-08-02 11:05:49 -07001842type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001843 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001844
Nan Zhangea568a42017-11-08 21:20:04 -08001845 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001846
1847 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001848
1849 // List of shared java libs that this module has dependencies to
1850 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001851
1852 // List of files to remove from the jar file(s)
1853 Exclude_files []string
1854
1855 // List of directories to remove from the jar file(s)
1856 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001857
1858 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001859 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001860}
1861
1862type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001863 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001864 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09001865 android.ApexModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001866 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001867
Colin Cross74d73e22017-08-02 11:05:49 -07001868 properties ImportProperties
1869
Colin Cross0a6e0072017-08-30 14:24:55 -07001870 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001871 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001872}
1873
Colin Cross83bb3162018-06-25 15:48:06 -07001874func (j *Import) sdkVersion() string {
Jeongik Cha6bd33c12019-06-25 16:26:18 +09001875 return proptools.StringDefault(j.properties.Sdk_version, defaultSdkVersion(j))
Colin Cross83bb3162018-06-25 15:48:06 -07001876}
1877
1878func (j *Import) minSdkVersion() string {
1879 return j.sdkVersion()
1880}
1881
Colin Cross74d73e22017-08-02 11:05:49 -07001882func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001883 return &j.prebuilt
1884}
1885
Colin Cross74d73e22017-08-02 11:05:49 -07001886func (j *Import) PrebuiltSrcs() []string {
1887 return j.properties.Jars
1888}
1889
1890func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001891 return j.prebuilt.Name(j.ModuleBase.Name())
1892}
1893
Colin Cross74d73e22017-08-02 11:05:49 -07001894func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001895 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001896}
1897
Colin Cross74d73e22017-08-02 11:05:49 -07001898func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001899 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001900
Nan Zhang4c819fb2018-08-27 18:31:46 -07001901 jarName := ctx.ModuleName() + ".jar"
1902 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001903 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1904 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001905 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001906 inputFile := outputFile
1907 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1908 TransformJetifier(ctx, outputFile, inputFile)
1909 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001910 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001911
1912 ctx.VisitDirectDeps(func(module android.Module) {
1913 otherName := ctx.OtherModuleName(module)
1914 tag := ctx.OtherModuleDependencyTag(module)
1915
1916 switch dep := module.(type) {
1917 case Dependency:
1918 switch tag {
1919 case libTag, staticLibTag:
1920 // sdk lib names from dependencies are re-exported
1921 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1922 }
1923 case SdkLibraryDependency:
1924 switch tag {
1925 case libTag:
1926 // names of sdk libs that are directly depended are exported
1927 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1928 }
1929 }
1930 })
1931
1932 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001933 if Bool(j.properties.Installable) {
1934 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1935 ctx.ModuleName()+".jar", outputFile)
1936 }
Colin Cross2fe66872015-03-30 17:20:39 -07001937}
1938
Colin Cross74d73e22017-08-02 11:05:49 -07001939var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001940
Nan Zhanged19fc32017-10-19 13:06:22 -07001941func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001942 if j.combinedClasspathFile == nil {
1943 return nil
1944 }
Colin Cross37f6d792018-07-12 12:28:41 -07001945 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001946}
1947
1948func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001949 if j.combinedClasspathFile == nil {
1950 return nil
1951 }
Colin Cross37f6d792018-07-12 12:28:41 -07001952 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001953}
1954
Colin Cross331a1212018-08-15 20:40:52 -07001955func (j *Import) ResourceJars() android.Paths {
1956 return nil
1957}
1958
1959func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001960 if j.combinedClasspathFile == nil {
1961 return nil
1962 }
Colin Cross331a1212018-08-15 20:40:52 -07001963 return android.Paths{j.combinedClasspathFile}
1964}
1965
Colin Crossf24a22a2019-01-31 14:12:44 -08001966func (j *Import) DexJar() android.Path {
1967 return nil
1968}
1969
Colin Cross74d73e22017-08-02 11:05:49 -07001970func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001971 return nil
1972}
1973
Jiyong Park1be96912018-05-28 18:02:19 +09001974func (j *Import) ExportedSdkLibs() []string {
1975 return j.exportedSdkLibs
1976}
1977
Colin Cross0c4ce212019-05-03 15:28:19 -07001978func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1979 return nil, nil
1980}
1981
albaltai36ff7dc2018-12-25 14:35:23 +08001982// Add compile time check for interface implementation
1983var _ android.IDEInfo = (*Import)(nil)
1984var _ android.IDECustomizedModuleName = (*Import)(nil)
1985
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001986// Collect information for opening IDE project files in java/jdeps.go.
1987const (
1988 removedPrefix = "prebuilt_"
1989)
1990
1991func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1992 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1993}
1994
1995func (j *Import) IDECustomizedModuleName() string {
1996 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1997 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1998 // solution to get the Import name.
1999 name := j.Name()
2000 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08002001 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07002002 }
2003 return name
2004}
2005
Colin Cross74d73e22017-08-02 11:05:49 -07002006var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07002007
Colin Cross1b16b0e2019-02-12 14:41:32 -08002008// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
2009//
2010// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2011// compiled against an Android classpath.
2012//
2013// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2014// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002015func ImportFactory() android.Module {
2016 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002017
Colin Cross74d73e22017-08-02 11:05:49 -07002018 module.AddProperties(&module.properties)
2019
2020 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002021 InitJavaModule(module, android.HostAndDeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002022 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07002023 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002024}
2025
Colin Cross1b16b0e2019-02-12 14:41:32 -08002026// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2027// module.
2028//
2029// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2030// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002031func ImportFactoryHost() android.Module {
2032 module := &Import{}
2033
2034 module.AddProperties(&module.properties)
2035
2036 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002037 InitJavaModule(module, android.HostSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002038 android.InitApexModule(module)
Colin Cross74d73e22017-08-02 11:05:49 -07002039 return module
2040}
2041
Colin Cross42be7612019-02-21 18:12:14 -08002042// dex_import module
2043
2044type DexImportProperties struct {
Colin Cross5cfc70d2019-07-15 13:36:55 -07002045 Jars []string `android:"path"`
Colin Cross42be7612019-02-21 18:12:14 -08002046}
2047
2048type DexImport struct {
2049 android.ModuleBase
2050 android.DefaultableModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002051 android.ApexModuleBase
Colin Cross42be7612019-02-21 18:12:14 -08002052 prebuilt android.Prebuilt
2053
2054 properties DexImportProperties
2055
2056 dexJarFile android.Path
2057 maybeStrippedDexJarFile android.Path
2058
2059 dexpreopter
2060}
2061
2062func (j *DexImport) Prebuilt() *android.Prebuilt {
2063 return &j.prebuilt
2064}
2065
2066func (j *DexImport) PrebuiltSrcs() []string {
2067 return j.properties.Jars
2068}
2069
2070func (j *DexImport) Name() string {
2071 return j.prebuilt.Name(j.ModuleBase.Name())
2072}
2073
Colin Cross42be7612019-02-21 18:12:14 -08002074func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2075 if len(j.properties.Jars) != 1 {
2076 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2077 }
2078
2079 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2080 j.dexpreopter.isInstallable = true
2081 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2082
2083 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2084 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2085
2086 if j.dexpreopter.uncompressedDex {
2087 rule := android.NewRuleBuilder()
2088
2089 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2090 rule.Temporary(temporary)
2091
2092 // use zip2zip to uncompress classes*.dex files
2093 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002094 BuiltTool(ctx, "zip2zip").
Colin Cross42be7612019-02-21 18:12:14 -08002095 FlagWithInput("-i ", inputJar).
2096 FlagWithOutput("-o ", temporary).
2097 FlagWithArg("-0 ", "'classes*.dex'")
2098
2099 // use zipalign to align uncompressed classes*.dex files
2100 rule.Command().
Colin Crossee94d6a2019-07-08 17:08:34 -07002101 BuiltTool(ctx, "zipalign").
Colin Cross42be7612019-02-21 18:12:14 -08002102 Flag("-f").
2103 Text("4").
2104 Input(temporary).
2105 Output(dexOutputFile)
2106
2107 rule.DeleteTemporaryFiles()
2108
2109 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2110 } else {
2111 ctx.Build(pctx, android.BuildParams{
2112 Rule: android.Cp,
2113 Input: inputJar,
2114 Output: dexOutputFile,
2115 })
2116 }
2117
2118 j.dexJarFile = dexOutputFile
2119
2120 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2121
2122 j.maybeStrippedDexJarFile = dexOutputFile
2123
2124 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2125 ctx.ModuleName()+".jar", dexOutputFile)
2126}
2127
2128func (j *DexImport) DexJar() android.Path {
2129 return j.dexJarFile
2130}
2131
2132// dex_import imports a `.jar` file containing classes.dex files.
2133//
2134// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2135// to the device.
2136func DexImportFactory() android.Module {
2137 module := &DexImport{}
2138
2139 module.AddProperties(&module.properties)
2140
2141 android.InitPrebuiltModule(module, &module.properties.Jars)
2142 InitJavaModule(module, android.DeviceSupported)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002143 android.InitApexModule(module)
Colin Cross42be7612019-02-21 18:12:14 -08002144 return module
2145}
2146
Colin Cross89536d42017-07-07 14:35:50 -07002147//
2148// Defaults
2149//
2150type Defaults struct {
2151 android.ModuleBase
2152 android.DefaultsModuleBase
Jiyong Park7f7766d2019-07-25 22:02:35 +09002153 android.ApexModuleBase
Colin Cross89536d42017-07-07 14:35:50 -07002154}
2155
Colin Cross1b16b0e2019-02-12 14:41:32 -08002156// java_defaults provides a set of properties that can be inherited by other java or android modules.
2157//
2158// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2159// property in the defaults module that exists in the depending module will be prepended to the depending module's
2160// value for that property.
2161//
2162// Example:
2163//
2164// java_defaults {
2165// name: "example_defaults",
2166// srcs: ["common/**/*.java"],
2167// javacflags: ["-Xlint:all"],
2168// aaptflags: ["--auto-add-overlay"],
2169// }
2170//
2171// java_library {
2172// name: "example",
2173// defaults: ["example_defaults"],
2174// srcs: ["example/**/*.java"],
2175// }
2176//
2177// is functionally identical to:
2178//
2179// java_library {
2180// name: "example",
2181// srcs: [
2182// "common/**/*.java",
2183// "example/**/*.java",
2184// ],
2185// javacflags: ["-Xlint:all"],
2186// }
Colin Cross89536d42017-07-07 14:35:50 -07002187func defaultsFactory() android.Module {
2188 return DefaultsFactory()
2189}
2190
2191func DefaultsFactory(props ...interface{}) android.Module {
2192 module := &Defaults{}
2193
2194 module.AddProperties(props...)
2195 module.AddProperties(
2196 &CompilerProperties{},
2197 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002198 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002199 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002200 &aaptProperties{},
2201 &androidLibraryProperties{},
2202 &appProperties{},
2203 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002204 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002205 &ImportProperties{},
2206 &AARImportProperties{},
2207 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002208 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002209 )
2210
2211 android.InitDefaultsModule(module)
Jiyong Park7f7766d2019-07-25 22:02:35 +09002212 android.InitApexModule(module)
Colin Cross89536d42017-07-07 14:35:50 -07002213 return module
2214}
Nan Zhangea568a42017-11-08 21:20:04 -08002215
2216var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002217var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002218var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002219var inList = android.InList