blob: bf80cedeea99ac51430ed622178837e99698cdbc [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
Colin Cross2fe66872015-03-30 17:20:39 -0700270
Colin Cross89536d42017-07-07 14:35:50 -0700271 properties CompilerProperties
Colin Cross6af17aa2017-09-20 12:59:05 -0700272 protoProperties android.ProtoProperties
Colin Cross89536d42017-07-07 14:35:50 -0700273 deviceProperties CompilerDeviceProperties
Colin Cross2fe66872015-03-30 17:20:39 -0700274
Colin Cross331a1212018-08-15 20:40:52 -0700275 // jar file containing header classes including static library dependencies, suitable for
276 // inserting into the bootclasspath/classpath of another compile
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 headerJarFile android.Path
278
Colin Cross331a1212018-08-15 20:40:52 -0700279 // jar file containing implementation classes including static library dependencies but no
280 // resources
Nan Zhanged19fc32017-10-19 13:06:22 -0700281 implementationJarFile android.Path
Colin Cross2fe66872015-03-30 17:20:39 -0700282
Colin Cross331a1212018-08-15 20:40:52 -0700283 // jar file containing only resources including from static library dependencies
284 resourceJar android.Path
285
Colin Cross0c4ce212019-05-03 15:28:19 -0700286 // args and dependencies to package source files into a srcjar
287 srcJarArgs []string
288 srcJarDeps android.Paths
289
Colin Cross331a1212018-08-15 20:40:52 -0700290 // jar file containing implementation classes and resources including static library
291 // dependencies
292 implementationAndResourcesJar android.Path
293
294 // output file containing classes.dex and resources
Colin Cross6ade34f2017-09-15 13:00:47 -0700295 dexJarFile android.Path
296
Colin Cross43f08db2018-11-12 10:13:39 -0800297 // output file that contains classes.dex if it should be in the output file
298 maybeStrippedDexJarFile android.Path
299
Colin Crosscb933592017-11-22 13:49:43 -0800300 // output file containing uninstrumented classes that will be instrumented by jacoco
301 jacocoReportClassesFile android.Path
302
Colin Cross66dbc0b2017-12-28 12:23:20 -0800303 // output file containing mapping of obfuscated names
304 proguardDictionary android.Path
305
Colin Cross331a1212018-08-15 20:40:52 -0700306 // output file of the module, which may be a classes jar or a dex jar
Colin Crosse560c4a2019-03-19 16:03:11 -0700307 outputFile android.Path
308 extraOutputFiles android.Paths
Colin Crossb7a63242015-04-16 14:09:14 -0700309
Colin Cross635c3b02016-05-18 15:37:25 -0700310 exportAidlIncludeDirs android.Paths
Colin Crossc0b06f12015-04-08 13:03:43 -0700311
Colin Cross635c3b02016-05-18 15:37:25 -0700312 logtagsSrcs android.Paths
Colin Crossf05fe972015-04-10 17:45:20 -0700313
Colin Cross2fe66872015-03-30 17:20:39 -0700314 // installed file for binary dependency
Colin Cross635c3b02016-05-18 15:37:25 -0700315 installFile android.Path
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800316
317 // list of .java files and srcjars that was passed to javac
318 compiledJavaSrcs android.Paths
319 compiledSrcJars android.Paths
Colin Cross66dbc0b2017-12-28 12:23:20 -0800320
321 // list of extra progurad flag files
322 extraProguardFlagFiles android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900323
Colin Cross094054a2018-10-17 15:10:48 -0700324 // manifest file to use instead of properties.Manifest
325 overrideManifest android.OptionalPath
326
Jiyong Park1be96912018-05-28 18:02:19 +0900327 // list of SDK lib names that this java moudule is exporting
328 exportedSdkLibs []string
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700329
330 // list of source files, collected from compiledJavaSrcs and compiledSrcJars
331 // filter out Exclude_srcs, will be used by android.IDEInfo struct
332 expandIDEInfoCompiledSrcs []string
Colin Cross43f08db2018-11-12 10:13:39 -0800333
Steven Morelandc4efd9c2019-01-18 11:51:25 -0800334 // expanded Jarjar_rules
335 expandJarjarRules android.Path
336
Vladimir Marko0975ee02019-04-02 10:29:55 +0100337 // list of additional targets for checkbuild
338 additionalCheckedModules android.Paths
339
Colin Cross988708c2019-05-06 14:04:11 -0700340 // Extra files generated by the module type to be added as java resources.
341 extraResources android.Paths
342
Colin Crossf24a22a2019-01-31 14:12:44 -0800343 hiddenAPI
Colin Cross43f08db2018-11-12 10:13:39 -0800344 dexpreopter
Colin Cross2fe66872015-03-30 17:20:39 -0700345}
346
Colin Cross41955e82019-05-29 14:40:35 -0700347func (j *Module) OutputFiles(tag string) (android.Paths, error) {
348 switch tag {
349 case "":
350 return append(android.Paths{j.outputFile}, j.extraOutputFiles...), nil
Colin Cross375ca3c2019-05-29 14:40:58 -0700351 case ".jar":
352 return android.Paths{j.implementationAndResourcesJar}, nil
Colin Cross41955e82019-05-29 14:40:35 -0700353 default:
354 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
355 }
Colin Cross54250902017-12-05 09:28:08 -0800356}
357
Jiyong Park8fd61922018-11-08 02:50:25 +0900358func (j *Module) DexJarFile() android.Path {
359 return j.dexJarFile
360}
361
Colin Cross41955e82019-05-29 14:40:35 -0700362var _ android.OutputFileProducer = (*Module)(nil)
Colin Cross54250902017-12-05 09:28:08 -0800363
Colin Crossf506d872017-07-19 15:53:04 -0700364type Dependency interface {
Nan Zhanged19fc32017-10-19 13:06:22 -0700365 HeaderJars() android.Paths
366 ImplementationJars() android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700367 ResourceJars() android.Paths
368 ImplementationAndResourcesJars() android.Paths
Colin Crossf24a22a2019-01-31 14:12:44 -0800369 DexJar() android.Path
Colin Cross635c3b02016-05-18 15:37:25 -0700370 AidlIncludeDirs() android.Paths
Jiyong Park1be96912018-05-28 18:02:19 +0900371 ExportedSdkLibs() []string
Colin Cross0c4ce212019-05-03 15:28:19 -0700372 SrcJarArgs() ([]string, android.Paths)
Colin Cross2fe66872015-03-30 17:20:39 -0700373}
374
Jiyong Parkc678ad32018-04-10 13:07:10 +0900375type SdkLibraryDependency interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700376 SdkHeaderJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
377 SdkImplementationJars(ctx android.BaseModuleContext, sdkVersion string) android.Paths
Jiyong Parkc678ad32018-04-10 13:07:10 +0900378}
379
Nan Zhangb2b33de2018-02-23 11:18:47 -0800380type SrcDependency interface {
381 CompiledSrcs() android.Paths
382 CompiledSrcJars() android.Paths
383}
384
385func (j *Module) CompiledSrcs() android.Paths {
386 return j.compiledJavaSrcs
387}
388
389func (j *Module) CompiledSrcJars() android.Paths {
390 return j.compiledSrcJars
391}
392
393var _ SrcDependency = (*Module)(nil)
394
Colin Cross89536d42017-07-07 14:35:50 -0700395func InitJavaModule(module android.DefaultableModule, hod android.HostOrDeviceSupported) {
396 android.InitAndroidArchModule(module, hod, android.MultilibCommon)
397 android.InitDefaultableModule(module)
398}
399
Colin Crossbe1da472017-07-07 15:59:46 -0700400type dependencyTag struct {
401 blueprint.BaseDependencyTag
402 name string
Colin Cross2fe66872015-03-30 17:20:39 -0700403}
404
Colin Crossa4f08812018-10-02 22:03:40 -0700405type jniDependencyTag struct {
406 blueprint.BaseDependencyTag
407 target android.Target
408}
409
Colin Crossbe1da472017-07-07 15:59:46 -0700410var (
Colin Cross4b964c02018-10-15 16:18:06 -0700411 staticLibTag = dependencyTag{name: "staticlib"}
412 libTag = dependencyTag{name: "javalib"}
Colin Crossbe9cdb82019-01-21 21:37:16 -0800413 pluginTag = dependencyTag{name: "plugin"}
Colin Cross4b964c02018-10-15 16:18:06 -0700414 bootClasspathTag = dependencyTag{name: "bootclasspath"}
415 systemModulesTag = dependencyTag{name: "system modules"}
416 frameworkResTag = dependencyTag{name: "framework-res"}
417 frameworkApkTag = dependencyTag{name: "framework-apk"}
418 kotlinStdlibTag = dependencyTag{name: "kotlin-stdlib"}
Colin Crossafbb1732019-01-17 15:42:52 -0800419 kotlinAnnotationsTag = dependencyTag{name: "kotlin-annotations"}
Colin Cross4b964c02018-10-15 16:18:06 -0700420 proguardRaiseTag = dependencyTag{name: "proguard-raise"}
421 certificateTag = dependencyTag{name: "certificate"}
422 instrumentationForTag = dependencyTag{name: "instrumentation_for"}
Colin Cross50ddcc42019-05-16 12:28:22 -0700423 usesLibTag = dependencyTag{name: "uses-library"}
Colin Crossbe1da472017-07-07 15:59:46 -0700424)
Colin Cross2fe66872015-03-30 17:20:39 -0700425
Colin Crossfc3674a2017-09-18 17:41:52 -0700426type sdkDep struct {
Colin Cross47ff2522017-10-02 14:22:08 -0700427 useModule, useFiles, useDefaultLibs, invalidVersion bool
428
Colin Cross86a60ae2018-05-29 14:44:55 -0700429 modules []string
Colin Cross1369cdb2017-09-29 17:58:17 -0700430 systemModules string
431
Colin Crossa97c5d32018-03-28 14:58:31 -0700432 frameworkResModule string
433
Colin Cross86a60ae2018-05-29 14:44:55 -0700434 jars android.Paths
Colin Cross3047fa22019-04-18 10:56:44 -0700435 aidl android.OptionalPath
Paul Duffin250e6192019-06-07 10:44:37 +0100436
437 noStandardLibs, noFrameworksLibs bool
438}
439
440func (s sdkDep) hasStandardLibs() bool {
441 return !s.noStandardLibs
442}
443
444func (s sdkDep) hasFrameworkLibs() bool {
445 return !s.noStandardLibs && !s.noFrameworksLibs
Colin Cross1369cdb2017-09-29 17:58:17 -0700446}
447
Colin Crossa4f08812018-10-02 22:03:40 -0700448type jniLib struct {
449 name string
450 path android.Path
451 target android.Target
452}
453
Colin Cross0ea8ba82019-06-06 14:33:29 -0700454func (j *Module) shouldInstrument(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800455 return j.properties.Instrument && ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
456}
457
Colin Cross0ea8ba82019-06-06 14:33:29 -0700458func (j *Module) shouldInstrumentStatic(ctx android.BaseModuleContext) bool {
Colin Cross3144dfc2018-01-03 15:06:47 -0800459 return j.shouldInstrument(ctx) &&
460 (ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_STATIC") ||
461 ctx.Config().UnbundledBuild())
462}
463
Colin Cross83bb3162018-06-25 15:48:06 -0700464func (j *Module) sdkVersion() string {
465 return String(j.deviceProperties.Sdk_version)
466}
467
468func (j *Module) minSdkVersion() string {
469 if j.deviceProperties.Min_sdk_version != nil {
470 return *j.deviceProperties.Min_sdk_version
471 }
472 return j.sdkVersion()
473}
474
Dan Willemsen419290a2018-10-31 15:28:47 -0700475func (j *Module) targetSdkVersion() string {
476 if j.deviceProperties.Target_sdk_version != nil {
477 return *j.deviceProperties.Target_sdk_version
478 }
479 return j.sdkVersion()
480}
481
Colin Crossbe1da472017-07-07 15:59:46 -0700482func (j *Module) deps(ctx android.BottomUpMutatorContext) {
Colin Cross1369cdb2017-09-29 17:58:17 -0700483 if ctx.Device() {
Paul Duffin250e6192019-06-07 10:44:37 +0100484 sdkDep := decodeSdkDep(ctx, sdkContext(j))
485 if sdkDep.hasStandardLibs() {
Colin Crossfc3674a2017-09-18 17:41:52 -0700486 if sdkDep.useDefaultLibs {
Colin Cross42d48b72018-08-29 14:10:52 -0700487 ctx.AddVariationDependencies(nil, bootClasspathTag, config.DefaultBootclasspathLibraries...)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100488 ctx.AddVariationDependencies(nil, systemModulesTag, config.DefaultSystemModules)
Paul Duffin250e6192019-06-07 10:44:37 +0100489 if sdkDep.hasFrameworkLibs() {
Colin Cross42d48b72018-08-29 14:10:52 -0700490 ctx.AddVariationDependencies(nil, libTag, config.DefaultLibraries...)
Colin Crossfa5eb232017-10-01 20:33:03 -0700491 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700492 } else if sdkDep.useModule {
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100493 ctx.AddVariationDependencies(nil, systemModulesTag, sdkDep.systemModules)
Colin Cross42d48b72018-08-29 14:10:52 -0700494 ctx.AddVariationDependencies(nil, bootClasspathTag, sdkDep.modules...)
Sasha Smundak2057f822019-04-16 17:16:58 -0700495 if j.deviceProperties.EffectiveOptimizeEnabled() {
Colin Cross42d48b72018-08-29 14:10:52 -0700496 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultBootclasspathLibraries...)
497 ctx.AddVariationDependencies(nil, proguardRaiseTag, config.DefaultLibraries...)
Colin Cross66dbc0b2017-12-28 12:23:20 -0800498 }
Colin Crossbe1da472017-07-07 15:59:46 -0700499 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700500 } else if j.deviceProperties.System_modules == nil {
Paul Duffina3d09862019-06-11 13:40:47 +0100501 ctx.PropertyErrorf("sdk_version",
Paul Duffin5c2f9632019-06-12 14:21:31 +0100502 `system_modules is required to be set when sdk_version is "none", did you mean "core_platform"`)
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100503 } else if *j.deviceProperties.System_modules != "none" {
Colin Cross42d48b72018-08-29 14:10:52 -0700504 ctx.AddVariationDependencies(nil, systemModulesTag, *j.deviceProperties.System_modules)
Colin Cross2fe66872015-03-30 17:20:39 -0700505 }
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100506 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross42d48b72018-08-29 14:10:52 -0700507 ctx.AddVariationDependencies(nil, frameworkResTag, "framework-res")
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800508 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800509 if ctx.ModuleName() == "android_stubs_current" ||
510 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700511 ctx.ModuleName() == "android_test_stubs_current" {
Colin Cross42d48b72018-08-29 14:10:52 -0700512 ctx.AddVariationDependencies(nil, frameworkApkTag, "framework-res")
Nan Zhangb2b33de2018-02-23 11:18:47 -0800513 }
Colin Cross2fe66872015-03-30 17:20:39 -0700514 }
Colin Cross1369cdb2017-09-29 17:58:17 -0700515
Colin Cross42d48b72018-08-29 14:10:52 -0700516 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
517 ctx.AddVariationDependencies(nil, staticLibTag, j.properties.Static_libs...)
Colin Crossa4f08812018-10-02 22:03:40 -0700518
Colin Crossbe9cdb82019-01-21 21:37:16 -0800519 ctx.AddFarVariationDependencies([]blueprint.Variation{
520 {Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant},
521 }, pluginTag, j.properties.Plugins...)
522
Colin Crossfe17f6f2019-03-28 19:30:56 -0700523 android.ProtoDeps(ctx, &j.protoProperties)
Colin Cross6af17aa2017-09-20 12:59:05 -0700524 if j.hasSrcExt(".proto") {
525 protoDeps(ctx, &j.protoProperties)
526 }
Colin Cross93e85952017-08-15 13:34:18 -0700527
528 if j.hasSrcExt(".kt") {
529 // TODO(ccross): move this to a mutator pass that can tell if generated sources contain
530 // Kotlin files
Colin Cross0b03d972019-05-13 11:06:25 -0700531 ctx.AddVariationDependencies(nil, kotlinStdlibTag,
532 "kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8")
Colin Cross7788c122019-01-23 16:14:02 -0800533 if len(j.properties.Plugins) > 0 {
Colin Crossafbb1732019-01-17 15:42:52 -0800534 ctx.AddVariationDependencies(nil, kotlinAnnotationsTag, "kotlin-annotations")
535 }
Colin Cross93e85952017-08-15 13:34:18 -0700536 }
Colin Cross3144dfc2018-01-03 15:06:47 -0800537
538 if j.shouldInstrumentStatic(ctx) {
Colin Cross42d48b72018-08-29 14:10:52 -0700539 ctx.AddVariationDependencies(nil, staticLibTag, "jacocoagent")
Colin Cross3144dfc2018-01-03 15:06:47 -0800540 }
Colin Cross6af17aa2017-09-20 12:59:05 -0700541}
542
543func hasSrcExt(srcs []string, ext string) bool {
544 for _, src := range srcs {
545 if filepath.Ext(src) == ext {
546 return true
547 }
548 }
549
550 return false
551}
552
Nan Zhang61eaedb2017-11-02 13:28:15 -0700553func shardPaths(paths android.Paths, shardSize int) []android.Paths {
554 ret := make([]android.Paths, 0, (len(paths)+shardSize-1)/shardSize)
555 for len(paths) > shardSize {
556 ret = append(ret, paths[0:shardSize])
557 paths = paths[shardSize:]
558 }
559 if len(paths) > 0 {
560 ret = append(ret, paths)
561 }
562 return ret
563}
564
Colin Cross6af17aa2017-09-20 12:59:05 -0700565func (j *Module) hasSrcExt(ext string) bool {
566 return hasSrcExt(j.properties.Srcs, ext)
Colin Cross2fe66872015-03-30 17:20:39 -0700567}
568
Colin Cross46c9b8b2017-06-22 16:51:17 -0700569func (j *Module) aidlFlags(ctx android.ModuleContext, aidlPreprocess android.OptionalPath,
Colin Cross3047fa22019-04-18 10:56:44 -0700570 aidlIncludeDirs android.Paths) (string, android.Paths) {
Colin Crossc0b06f12015-04-08 13:03:43 -0700571
Colin Crossebe1a512017-11-14 13:12:14 -0800572 aidlIncludes := android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Local_include_dirs)
573 aidlIncludes = append(aidlIncludes,
574 android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)...)
575 aidlIncludes = append(aidlIncludes,
576 android.PathsForSource(ctx, j.deviceProperties.Aidl.Include_dirs)...)
Colin Crossc0b06f12015-04-08 13:03:43 -0700577
Colin Cross3047fa22019-04-18 10:56:44 -0700578 var flags []string
579 var deps android.Paths
Steven Moreland667f6882018-07-26 12:55:08 -0700580
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700581 if aidlPreprocess.Valid() {
582 flags = append(flags, "-p"+aidlPreprocess.String())
Colin Cross3047fa22019-04-18 10:56:44 -0700583 deps = append(deps, aidlPreprocess.Path())
584 } else if len(aidlIncludeDirs) > 0 {
Colin Cross635c3b02016-05-18 15:37:25 -0700585 flags = append(flags, android.JoinWithPrefix(aidlIncludeDirs.Strings(), "-I"))
Colin Crossc0b06f12015-04-08 13:03:43 -0700586 }
587
Colin Cross3047fa22019-04-18 10:56:44 -0700588 if len(j.exportAidlIncludeDirs) > 0 {
589 flags = append(flags, android.JoinWithPrefix(j.exportAidlIncludeDirs.Strings(), "-I"))
590 }
591
592 if len(aidlIncludes) > 0 {
593 flags = append(flags, android.JoinWithPrefix(aidlIncludes.Strings(), "-I"))
594 }
595
Colin Cross635c3b02016-05-18 15:37:25 -0700596 flags = append(flags, "-I"+android.PathForModuleSrc(ctx).String())
Colin Cross32f38982018-02-22 11:47:25 -0800597 if src := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "src"); src.Valid() {
Colin Crossd48633a2017-07-13 14:41:17 -0700598 flags = append(flags, "-I"+src.String())
599 }
Colin Crossc0b06f12015-04-08 13:03:43 -0700600
Martijn Coeneneab15642018-03-09 09:29:59 +0100601 if Bool(j.deviceProperties.Aidl.Generate_traces) {
602 flags = append(flags, "-t")
603 }
604
Olivier Gaillard0a4cfbc2018-07-16 23:37:03 +0100605 if Bool(j.deviceProperties.Aidl.Generate_get_transaction_name) {
606 flags = append(flags, "--transaction_names")
607 }
608
Colin Cross3047fa22019-04-18 10:56:44 -0700609 return strings.Join(flags, " "), deps
Colin Crossc0b06f12015-04-08 13:03:43 -0700610}
611
Colin Cross32f676a2017-09-06 13:41:06 -0700612type deps struct {
Nan Zhang581fd212018-01-10 16:06:12 -0800613 classpath classpath
614 bootClasspath classpath
Colin Cross6a77c982018-06-19 22:43:34 -0700615 processorPath classpath
Colin Crossbe9cdb82019-01-21 21:37:16 -0800616 processorClasses []string
Colin Cross6ade34f2017-09-15 13:00:47 -0700617 staticJars android.Paths
Nan Zhanged19fc32017-10-19 13:06:22 -0700618 staticHeaderJars android.Paths
Colin Cross331a1212018-08-15 20:40:52 -0700619 staticResourceJars android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700620 aidlIncludeDirs android.Paths
Nan Zhangb2b33de2018-02-23 11:18:47 -0800621 srcs android.Paths
Colin Cross59149b62017-10-16 18:07:29 -0700622 srcJars android.Paths
Colin Cross1369cdb2017-09-29 17:58:17 -0700623 systemModules android.Path
Dan Willemsenff60a732019-06-13 16:52:01 +0000624 systemModulesDeps android.Paths
Colin Cross6ade34f2017-09-15 13:00:47 -0700625 aidlPreprocess android.OptionalPath
Colin Cross93e85952017-08-15 13:34:18 -0700626 kotlinStdlib android.Paths
Colin Crossafbb1732019-01-17 15:42:52 -0800627 kotlinAnnotations android.Paths
Colin Crossbe9cdb82019-01-21 21:37:16 -0800628
629 disableTurbine bool
Colin Cross32f676a2017-09-06 13:41:06 -0700630}
Colin Cross2fe66872015-03-30 17:20:39 -0700631
Colin Cross54250902017-12-05 09:28:08 -0800632func checkProducesJars(ctx android.ModuleContext, dep android.SourceFileProducer) {
633 for _, f := range dep.Srcs() {
634 if f.Ext() != ".jar" {
635 ctx.ModuleErrorf("genrule %q must generate files ending with .jar to be used as a libs or static_libs dependency",
636 ctx.OtherModuleName(dep.(blueprint.Module)))
637 }
638 }
639}
640
Jiyong Park2d492942018-03-05 17:44:10 +0900641type linkType int
642
643const (
644 javaCore linkType = iota
645 javaSdk
646 javaSystem
647 javaPlatform
648)
649
Jiyong Park46f78fb2018-10-20 16:33:17 +0900650func getLinkType(m *Module, name string) (ret linkType, stubs bool) {
Colin Cross83bb3162018-06-25 15:48:06 -0700651 ver := m.sdkVersion()
Colin Crossf19b9bb2018-03-26 14:42:44 -0700652 switch {
Jiyong Park46f78fb2018-10-20 16:33:17 +0900653 case name == "core.current.stubs" || name == "core.platform.api.stubs" ||
654 name == "stub-annotations" || name == "private-stub-annotations-jar" ||
Pete Gillincbff3262019-05-08 15:10:06 +0100655 name == "core-lambda-stubs" || name == "core-generated-annotation-stubs":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900656 return javaCore, true
Neil Fuller401eeba2018-10-18 19:48:58 +0100657 case ver == "core_current":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900658 return javaCore, false
659 case name == "android_system_stubs_current":
660 return javaSystem, true
661 case strings.HasPrefix(ver, "system_"):
662 return javaSystem, false
663 case name == "android_test_stubs_current":
664 return javaSystem, true
665 case strings.HasPrefix(ver, "test_"):
666 return javaPlatform, false
667 case name == "android_stubs_current":
668 return javaSdk, true
669 case ver == "current":
670 return javaSdk, false
Paul Duffin50c217c2019-06-12 13:25:22 +0100671 case ver == "" || ver == "none" || ver == "core_platform":
Jiyong Park46f78fb2018-10-20 16:33:17 +0900672 return javaPlatform, false
Colin Crossf19b9bb2018-03-26 14:42:44 -0700673 default:
674 if _, err := strconv.Atoi(ver); err != nil {
675 panic(fmt.Errorf("expected sdk_version to be a number, got %q", ver))
676 }
Jiyong Park46f78fb2018-10-20 16:33:17 +0900677 return javaSdk, false
Jiyong Park2d492942018-03-05 17:44:10 +0900678 }
679}
680
Jiyong Park750e5572018-01-31 00:20:13 +0900681func checkLinkType(ctx android.ModuleContext, from *Module, to *Library, tag dependencyTag) {
Colin Crossf19b9bb2018-03-26 14:42:44 -0700682 if ctx.Host() {
683 return
684 }
685
Jiyong Park46f78fb2018-10-20 16:33:17 +0900686 myLinkType, stubs := getLinkType(from, ctx.ModuleName())
687 if stubs {
688 return
689 }
690 otherLinkType, _ := getLinkType(&to.Module, ctx.OtherModuleName(to))
Jiyong Park2d492942018-03-05 17:44:10 +0900691 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."
692
693 switch myLinkType {
694 case javaCore:
695 if otherLinkType != javaCore {
696 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 +0900697 ctx.OtherModuleName(to))
698 }
Jiyong Park2d492942018-03-05 17:44:10 +0900699 break
700 case javaSdk:
701 if otherLinkType != javaCore && otherLinkType != javaSdk {
702 ctx.ModuleErrorf("compiles against Android API, but dependency %q is compiling against non-public Android API."+commonMessage,
703 ctx.OtherModuleName(to))
704 }
705 break
706 case javaSystem:
707 if otherLinkType == javaPlatform {
708 ctx.ModuleErrorf("compiles against system API, but dependency %q is compiling against private API."+commonMessage,
709 ctx.OtherModuleName(to))
710 }
711 break
712 case javaPlatform:
713 // no restriction on link-type
714 break
Jiyong Park750e5572018-01-31 00:20:13 +0900715 }
716}
717
Colin Cross32f676a2017-09-06 13:41:06 -0700718func (j *Module) collectDeps(ctx android.ModuleContext) deps {
719 var deps deps
Colin Crossfc3674a2017-09-18 17:41:52 -0700720
Colin Cross300f0382018-03-06 13:11:51 -0800721 if ctx.Device() {
Colin Cross83bb3162018-06-25 15:48:06 -0700722 sdkDep := decodeSdkDep(ctx, sdkContext(j))
Colin Cross300f0382018-03-06 13:11:51 -0800723 if sdkDep.invalidVersion {
Colin Cross86a60ae2018-05-29 14:44:55 -0700724 ctx.AddMissingDependencies(sdkDep.modules)
Colin Cross300f0382018-03-06 13:11:51 -0800725 } else if sdkDep.useFiles {
726 // sdkDep.jar is actually equivalent to turbine header.jar.
Colin Cross86a60ae2018-05-29 14:44:55 -0700727 deps.classpath = append(deps.classpath, sdkDep.jars...)
Colin Cross3047fa22019-04-18 10:56:44 -0700728 deps.aidlPreprocess = sdkDep.aidl
729 } else {
730 deps.aidlPreprocess = sdkDep.aidl
Colin Cross300f0382018-03-06 13:11:51 -0800731 }
Colin Crossfc3674a2017-09-18 17:41:52 -0700732 }
733
Colin Crossd11fcda2017-10-23 17:59:01 -0700734 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross2fe66872015-03-30 17:20:39 -0700735 otherName := ctx.OtherModuleName(module)
Colin Crossec7a0422017-07-07 14:47:12 -0700736 tag := ctx.OtherModuleDependencyTag(module)
737
Colin Crossa4f08812018-10-02 22:03:40 -0700738 if _, ok := tag.(*jniDependencyTag); ok {
Colin Crossbd01e2a2018-10-04 15:21:03 -0700739 // Handled by AndroidApp.collectAppDeps
740 return
741 }
742 if tag == certificateTag {
743 // Handled by AndroidApp.collectAppDeps
Colin Crossa4f08812018-10-02 22:03:40 -0700744 return
745 }
746
Jiyong Park750e5572018-01-31 00:20:13 +0900747 if to, ok := module.(*Library); ok {
Colin Crossa97c5d32018-03-28 14:58:31 -0700748 switch tag {
749 case bootClasspathTag, libTag, staticLibTag:
750 checkLinkType(ctx, j, to, tag.(dependencyTag))
751 }
Jiyong Park750e5572018-01-31 00:20:13 +0900752 }
Colin Cross54250902017-12-05 09:28:08 -0800753 switch dep := module.(type) {
Colin Cross897d2ed2019-02-11 14:03:51 -0800754 case SdkLibraryDependency:
755 switch tag {
756 case libTag:
757 deps.classpath = append(deps.classpath, dep.SdkHeaderJars(ctx, j.sdkVersion())...)
758 // names of sdk libs that are directly depended are exported
759 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
Colin Cross79c7c262019-04-17 11:11:46 -0700760 case staticLibTag:
Colin Cross897d2ed2019-02-11 14:03:51 -0800761 ctx.ModuleErrorf("dependency on java_sdk_library %q can only be in libs", otherName)
762 }
Colin Cross54250902017-12-05 09:28:08 -0800763 case Dependency:
764 switch tag {
765 case bootClasspathTag:
766 deps.bootClasspath = append(deps.bootClasspath, dep.HeaderJars()...)
Colin Cross4b964c02018-10-15 16:18:06 -0700767 case libTag, instrumentationForTag:
Colin Cross54250902017-12-05 09:28:08 -0800768 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900769 // sdk lib names from dependencies are re-exported
770 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700771 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Cross54250902017-12-05 09:28:08 -0800772 case staticLibTag:
773 deps.classpath = append(deps.classpath, dep.HeaderJars()...)
774 deps.staticJars = append(deps.staticJars, dep.ImplementationJars()...)
775 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.HeaderJars()...)
Colin Cross331a1212018-08-15 20:40:52 -0700776 deps.staticResourceJars = append(deps.staticResourceJars, dep.ResourceJars()...)
Jiyong Park1be96912018-05-28 18:02:19 +0900777 // sdk lib names from dependencies are re-exported
778 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
Colin Cross3047fa22019-04-18 10:56:44 -0700779 deps.aidlIncludeDirs = append(deps.aidlIncludeDirs, dep.AidlIncludeDirs()...)
Colin Crossbe9cdb82019-01-21 21:37:16 -0800780 case pluginTag:
781 if plugin, ok := dep.(*Plugin); ok {
782 deps.processorPath = append(deps.processorPath, dep.ImplementationAndResourcesJars()...)
783 if plugin.pluginProperties.Processor_class != nil {
784 deps.processorClasses = append(deps.processorClasses, *plugin.pluginProperties.Processor_class)
785 }
786 deps.disableTurbine = deps.disableTurbine || Bool(plugin.pluginProperties.Generates_api)
787 } else {
788 ctx.PropertyErrorf("plugins", "%q is not a java_plugin module", otherName)
789 }
Colin Cross54250902017-12-05 09:28:08 -0800790 case frameworkResTag:
Mathew Inwoodebe29ce2018-09-04 14:26:19 +0100791 if (ctx.ModuleName() == "framework") || (ctx.ModuleName() == "framework-annotation-proc") {
Colin Cross54250902017-12-05 09:28:08 -0800792 // framework.jar has a one-off dependency on the R.java and Manifest.java files
793 // generated by framework-res.apk
794 deps.srcJars = append(deps.srcJars, dep.(*AndroidApp).aaptSrcJar)
795 }
Nan Zhangb2b33de2018-02-23 11:18:47 -0800796 case frameworkApkTag:
797 if ctx.ModuleName() == "android_stubs_current" ||
798 ctx.ModuleName() == "android_system_stubs_current" ||
Nan Zhang863f05b2018-08-07 13:41:10 -0700799 ctx.ModuleName() == "android_test_stubs_current" {
Nan Zhangb2b33de2018-02-23 11:18:47 -0800800 // framework stubs.jar need to depend on framework-res.apk, in order to pull the
801 // resource files out of there for aapt.
802 //
803 // Normally the package rule runs aapt, which includes the resource,
804 // but we're not running that in our package rule so just copy in the
805 // resource files here.
Colin Cross331a1212018-08-15 20:40:52 -0700806 deps.staticResourceJars = append(deps.staticResourceJars, dep.(*AndroidApp).exportPackage)
Nan Zhangb2b33de2018-02-23 11:18:47 -0800807 }
Colin Cross54250902017-12-05 09:28:08 -0800808 case kotlinStdlibTag:
Colin Cross0b03d972019-05-13 11:06:25 -0700809 deps.kotlinStdlib = append(deps.kotlinStdlib, dep.HeaderJars()...)
Colin Crossafbb1732019-01-17 15:42:52 -0800810 case kotlinAnnotationsTag:
811 deps.kotlinAnnotations = dep.HeaderJars()
Colin Cross54250902017-12-05 09:28:08 -0800812 }
813
Colin Cross54250902017-12-05 09:28:08 -0800814 case android.SourceFileProducer:
815 switch tag {
816 case libTag:
817 checkProducesJars(ctx, dep)
818 deps.classpath = append(deps.classpath, dep.Srcs()...)
819 case staticLibTag:
820 checkProducesJars(ctx, dep)
821 deps.classpath = append(deps.classpath, dep.Srcs()...)
822 deps.staticJars = append(deps.staticJars, dep.Srcs()...)
823 deps.staticHeaderJars = append(deps.staticHeaderJars, dep.Srcs()...)
Colin Cross54250902017-12-05 09:28:08 -0800824 }
825 default:
Colin Crossec7a0422017-07-07 14:47:12 -0700826 switch tag {
Colin Cross1369cdb2017-09-29 17:58:17 -0700827 case systemModulesTag:
828 if deps.systemModules != nil {
829 panic("Found two system module dependencies")
830 }
831 sm := module.(*SystemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000832 if sm.outputDir == nil || len(sm.outputDeps) == 0 {
Colin Cross1369cdb2017-09-29 17:58:17 -0700833 panic("Missing directory for system module dependency")
834 }
Dan Willemsenff60a732019-06-13 16:52:01 +0000835 deps.systemModules = sm.outputDir
836 deps.systemModulesDeps = sm.outputDeps
Colin Cross2fe66872015-03-30 17:20:39 -0700837 }
Colin Crossec7a0422017-07-07 14:47:12 -0700838 }
Colin Cross2fe66872015-03-30 17:20:39 -0700839 })
840
Jiyong Park1be96912018-05-28 18:02:19 +0900841 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
842
Colin Cross32f676a2017-09-06 13:41:06 -0700843 return deps
Colin Cross2fe66872015-03-30 17:20:39 -0700844}
845
Colin Cross83bb3162018-06-25 15:48:06 -0700846func getJavaVersion(ctx android.ModuleContext, javaVersion string, sdkContext sdkContext) string {
Nan Zhang357466b2018-04-17 17:38:36 -0700847 var ret string
Colin Cross98fd5742019-01-09 23:04:25 -0800848 v := sdkContext.sdkVersion()
849 // For PDK builds, use the latest SDK version instead of "current"
Paul Duffin50c217c2019-06-12 13:25:22 +0100850 if ctx.Config().IsPdkBuild() &&
851 (v == "" || v == "none" || v == "core_platform" || v == "current") {
Colin Cross3047fa22019-04-18 10:56:44 -0700852 sdkVersions := ctx.Config().Get(sdkVersionsKey).([]int)
Colin Cross98fd5742019-01-09 23:04:25 -0800853 latestSdkVersion := 0
854 if len(sdkVersions) > 0 {
855 latestSdkVersion = sdkVersions[len(sdkVersions)-1]
856 }
857 v = strconv.Itoa(latestSdkVersion)
858 }
859
860 sdk, err := sdkVersionToNumber(ctx, v)
Colin Cross83bb3162018-06-25 15:48:06 -0700861 if err != nil {
862 ctx.PropertyErrorf("sdk_version", "%s", err)
863 }
Nan Zhang357466b2018-04-17 17:38:36 -0700864 if javaVersion != "" {
865 ret = javaVersion
866 } else if ctx.Device() && sdk <= 23 {
867 ret = "1.7"
Pete Gillin9c640142019-05-20 15:44:53 +0100868 } else if ctx.Device() && sdk <= 29 || !ctx.Config().TargetOpenJDK9() {
Nan Zhang357466b2018-04-17 17:38:36 -0700869 ret = "1.8"
Paul Duffin50c217c2019-06-12 13:25:22 +0100870 } else if ctx.Device() &&
871 sdkContext.sdkVersion() != "" &&
872 sdkContext.sdkVersion() != "none" &&
873 sdkContext.sdkVersion() != "core_platform" &&
874 sdk == android.FutureApiLevel {
Nan Zhang357466b2018-04-17 17:38:36 -0700875 // TODO(ccross): once we generate stubs we should be able to use 1.9 for sdk_version: "current"
876 ret = "1.8"
877 } else {
878 ret = "1.9"
879 }
880
881 return ret
882}
883
Nan Zhanged19fc32017-10-19 13:06:22 -0700884func (j *Module) collectBuilderFlags(ctx android.ModuleContext, deps deps) javaBuilderFlags {
Colin Crossc0b06f12015-04-08 13:03:43 -0700885
Colin Crossf03c82b2015-04-13 13:53:40 -0700886 var flags javaBuilderFlags
887
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100888 // javaVersion flag.
889 flags.javaVersion = getJavaVersion(ctx, String(j.properties.Java_version), sdkContext(j))
890
Nan Zhanged19fc32017-10-19 13:06:22 -0700891 // javac flags.
Colin Crossf03c82b2015-04-13 13:53:40 -0700892 javacFlags := j.properties.Javacflags
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100893 if flags.javaVersion == "1.9" {
Colin Cross1369cdb2017-09-29 17:58:17 -0700894 javacFlags = append(javacFlags, j.properties.Openjdk9.Javacflags...)
Nan Zhanged19fc32017-10-19 13:06:22 -0700895 }
Colin Cross6510f912017-11-29 00:27:14 -0800896 if ctx.Config().MinimizeJavaDebugInfo() {
Colin Cross126a25c2017-10-31 13:55:34 -0700897 // Override the -g flag passed globally to remove local variable debug info to reduce
898 // disk and memory usage.
899 javacFlags = append(javacFlags, "-g:source,lines")
900 }
Colin Cross64162712017-08-08 13:17:59 -0700901
Colin Cross66548102018-06-19 22:47:35 -0700902 if ctx.Config().RunErrorProne() {
903 if config.ErrorProneClasspath == nil {
904 ctx.ModuleErrorf("cannot build with Error Prone, missing external/error_prone?")
905 }
906
907 errorProneFlags := []string{
908 "-Xplugin:ErrorProne",
909 "${config.ErrorProneChecks}",
910 }
911 errorProneFlags = append(errorProneFlags, j.properties.Errorprone.Javacflags...)
912
913 flags.errorProneExtraJavacFlags = "${config.ErrorProneFlags} " +
914 "'" + strings.Join(errorProneFlags, " ") + "'"
915 flags.errorProneProcessorPath = classpath(android.PathsForSource(ctx, config.ErrorProneClasspath))
Andreas Gampef3e5b552018-01-22 21:27:21 -0800916 }
917
Nan Zhanged19fc32017-10-19 13:06:22 -0700918 // classpath
Nan Zhang581fd212018-01-10 16:06:12 -0800919 flags.bootClasspath = append(flags.bootClasspath, deps.bootClasspath...)
920 flags.classpath = append(flags.classpath, deps.classpath...)
Colin Cross6a77c982018-06-19 22:43:34 -0700921 flags.processorPath = append(flags.processorPath, deps.processorPath...)
Colin Cross7fdd2b72018-01-02 18:14:25 -0800922
Colin Crossbe9cdb82019-01-21 21:37:16 -0800923 flags.processor = strings.Join(deps.processorClasses, ",")
924
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100925 if len(flags.bootClasspath) == 0 && ctx.Host() && flags.javaVersion != "1.9" &&
Paul Duffin250e6192019-06-07 10:44:37 +0100926 decodeSdkDep(ctx, sdkContext(j)).hasStandardLibs() &&
Colin Cross7fdd2b72018-01-02 18:14:25 -0800927 inList(flags.javaVersion, []string{"1.6", "1.7", "1.8"}) {
928 // Give host-side tools a version of OpenJDK's standard libraries
929 // close to what they're targeting. As of Dec 2017, AOSP is only
930 // bundling OpenJDK 8 and 9, so nothing < 8 is available.
931 //
932 // When building with OpenJDK 8, the following should have no
933 // effect since those jars would be available by default.
934 //
935 // When building with OpenJDK 9 but targeting a version < 1.8,
936 // putting them on the bootclasspath means that:
937 // a) code can't (accidentally) refer to OpenJDK 9 specific APIs
938 // b) references to existing APIs are not reinterpreted in an
939 // OpenJDK 9-specific way, eg. calls to subclasses of
940 // java.nio.Buffer as in http://b/70862583
941 java8Home := ctx.Config().Getenv("ANDROID_JAVA8_HOME")
942 flags.bootClasspath = append(flags.bootClasspath,
943 android.PathForSource(ctx, java8Home, "jre/lib/jce.jar"),
944 android.PathForSource(ctx, java8Home, "jre/lib/rt.jar"))
Nan Zhang5f8cb422018-02-06 10:34:32 -0800945 if Bool(j.properties.Use_tools_jar) {
946 flags.bootClasspath = append(flags.bootClasspath,
947 android.PathForSource(ctx, java8Home, "lib/tools.jar"))
948 }
Colin Cross7fdd2b72018-01-02 18:14:25 -0800949 }
950
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100951 if j.properties.Patch_module != nil && flags.javaVersion == "1.9" {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800952 // Manually specify build directory in case it is not under the repo root.
953 // (javac doesn't seem to expand into symbolc links when searching for patch-module targets, so
954 // just adding a symlink under the root doesn't help.)
955 patchPaths := ".:" + ctx.Config().BuildDir()
956 classPath := flags.classpath.FormJavaClassPath("")
957 if classPath != "" {
958 patchPaths += ":" + classPath
959 }
960 javacFlags = append(javacFlags, "--patch-module="+String(j.properties.Patch_module)+"="+patchPaths)
Colin Cross81440082018-08-15 20:21:55 -0700961 }
962
Nan Zhanged19fc32017-10-19 13:06:22 -0700963 // systemModules
Colin Cross1369cdb2017-09-29 17:58:17 -0700964 if deps.systemModules != nil {
965 flags.systemModules = append(flags.systemModules, deps.systemModules)
Dan Willemsenff60a732019-06-13 16:52:01 +0000966 flags.systemModulesDeps = append(flags.systemModulesDeps, deps.systemModulesDeps...)
Colin Cross1369cdb2017-09-29 17:58:17 -0700967 }
968
Nan Zhanged19fc32017-10-19 13:06:22 -0700969 // aidl flags.
Colin Cross3047fa22019-04-18 10:56:44 -0700970 flags.aidlFlags, flags.aidlDeps = j.aidlFlags(ctx, deps.aidlPreprocess, deps.aidlIncludeDirs)
Colin Cross2fe66872015-03-30 17:20:39 -0700971
Colin Cross81440082018-08-15 20:21:55 -0700972 if len(javacFlags) > 0 {
973 // optimization.
974 ctx.Variable(pctx, "javacFlags", strings.Join(javacFlags, " "))
975 flags.javacFlags = "$javacFlags"
976 }
977
Nan Zhanged19fc32017-10-19 13:06:22 -0700978 return flags
979}
Colin Crossc0b06f12015-04-08 13:03:43 -0700980
Jaewoong Junga24af3b2019-05-13 09:23:20 -0700981func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) {
982
Colin Crossebe1a512017-11-14 13:12:14 -0800983 j.exportAidlIncludeDirs = android.PathsForModuleSrc(ctx, j.deviceProperties.Aidl.Export_include_dirs)
Nan Zhanged19fc32017-10-19 13:06:22 -0700984
985 deps := j.collectDeps(ctx)
986 flags := j.collectBuilderFlags(ctx, deps)
987
Tobias Thierer06dd04f2018-09-11 16:21:05 +0100988 if flags.javaVersion == "1.9" {
Nan Zhanged19fc32017-10-19 13:06:22 -0700989 j.properties.Srcs = append(j.properties.Srcs, j.properties.Openjdk9.Srcs...)
990 }
Colin Cross8a497952019-03-05 22:25:09 -0800991 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
Colin Cross6af17aa2017-09-20 12:59:05 -0700992 if hasSrcExt(srcFiles.Strings(), ".proto") {
Colin Cross0f2ee152017-12-14 15:22:43 -0800993 flags = protoFlags(ctx, &j.properties, &j.protoProperties, flags)
Colin Cross6af17aa2017-09-20 12:59:05 -0700994 }
995
Colin Crossaf050172017-11-15 23:01:59 -0800996 srcFiles = j.genSources(ctx, srcFiles, flags)
997
998 srcJars := srcFiles.FilterByExt(".srcjar")
Colin Cross59149b62017-10-16 18:07:29 -0700999 srcJars = append(srcJars, deps.srcJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001000 if aaptSrcJar != nil {
1001 srcJars = append(srcJars, aaptSrcJar)
1002 }
Colin Crossb7a63242015-04-16 14:09:14 -07001003
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001004 // Collect source files from compiledJavaSrcs, compiledSrcJars and filter out Exclude_srcs
1005 // that IDEInfo struct will use
1006 j.expandIDEInfoCompiledSrcs = append(j.expandIDEInfoCompiledSrcs, srcFiles.Strings()...)
1007
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001008 if j.properties.Jarjar_rules != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001009 j.expandJarjarRules = android.PathForModuleSrc(ctx, *j.properties.Jarjar_rules)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001010 }
1011
Colin Cross1ee23172017-10-18 14:44:18 -07001012 jarName := ctx.ModuleName() + ".jar"
1013
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001014 javaSrcFiles := srcFiles.FilterByExt(".java")
1015 var uniqueSrcFiles android.Paths
1016 set := make(map[string]bool)
1017 for _, v := range javaSrcFiles {
1018 if _, found := set[v.String()]; !found {
1019 set[v.String()] = true
1020 uniqueSrcFiles = append(uniqueSrcFiles, v)
1021 }
1022 }
1023
Colin Cross55f63ea2018-08-27 12:37:09 -07001024 var kotlinJars android.Paths
1025
Colin Cross93e85952017-08-15 13:34:18 -07001026 if srcFiles.HasExt(".kt") {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001027 // user defined kotlin flags.
1028 kotlincFlags := j.properties.Kotlincflags
1029 CheckKotlincFlags(ctx, kotlincFlags)
1030
Colin Cross93e85952017-08-15 13:34:18 -07001031 // If there are kotlin files, compile them first but pass all the kotlin and java files
1032 // kotlinc will use the java files to resolve types referenced by the kotlin files, but
1033 // won't emit any classes for them.
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001034 kotlincFlags = append(kotlincFlags, "-no-stdlib")
Colin Cross93e85952017-08-15 13:34:18 -07001035 if ctx.Device() {
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001036 kotlincFlags = append(kotlincFlags, "-no-jdk")
1037 }
1038 if len(kotlincFlags) > 0 {
1039 // optimization.
1040 ctx.Variable(pctx, "kotlincFlags", strings.Join(kotlincFlags, " "))
1041 flags.kotlincFlags += "$kotlincFlags"
Colin Cross93e85952017-08-15 13:34:18 -07001042 }
1043
Przemyslaw Szczepaniak4b5fe9d2018-02-13 14:32:54 +00001044 var kotlinSrcFiles android.Paths
1045 kotlinSrcFiles = append(kotlinSrcFiles, uniqueSrcFiles...)
1046 kotlinSrcFiles = append(kotlinSrcFiles, srcFiles.FilterByExt(".kt")...)
1047
Colin Crossafbb1732019-01-17 15:42:52 -08001048 flags.classpath = append(flags.classpath, deps.kotlinStdlib...)
1049 flags.classpath = append(flags.classpath, deps.kotlinAnnotations...)
1050
1051 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.bootClasspath...)
1052 flags.kotlincClasspath = append(flags.kotlincClasspath, flags.classpath...)
1053
1054 if len(flags.processorPath) > 0 {
1055 // Use kapt for annotation processing
1056 kaptSrcJar := android.PathForModuleOut(ctx, "kapt", "kapt-sources.jar")
1057 kotlinKapt(ctx, kaptSrcJar, kotlinSrcFiles, srcJars, flags)
1058 srcJars = append(srcJars, kaptSrcJar)
1059 // Disable annotation processing in javac, it's already been handled by kapt
1060 flags.processorPath = nil
Colin Cross3a3e94c2019-01-23 15:39:50 -08001061 flags.processor = ""
Colin Crossafbb1732019-01-17 15:42:52 -08001062 }
Colin Cross93e85952017-08-15 13:34:18 -07001063
Colin Cross1ee23172017-10-18 14:44:18 -07001064 kotlinJar := android.PathForModuleOut(ctx, "kotlin", jarName)
Colin Cross21fc9bb2019-01-18 15:05:09 -08001065 kotlinCompile(ctx, kotlinJar, kotlinSrcFiles, srcJars, flags)
Colin Cross93e85952017-08-15 13:34:18 -07001066 if ctx.Failed() {
1067 return
1068 }
1069
1070 // Make javac rule depend on the kotlinc rule
1071 flags.classpath = append(flags.classpath, kotlinJar)
Przemyslaw Szczepaniak66c0c402018-03-08 13:21:55 +00001072
Colin Cross93e85952017-08-15 13:34:18 -07001073 // Jar kotlin classes into the final jar after javac
Colin Cross55f63ea2018-08-27 12:37:09 -07001074 kotlinJars = append(kotlinJars, kotlinJar)
Colin Cross9b38aef2018-08-27 15:42:25 -07001075 kotlinJars = append(kotlinJars, deps.kotlinStdlib...)
Colin Cross93e85952017-08-15 13:34:18 -07001076 }
1077
Colin Cross55f63ea2018-08-27 12:37:09 -07001078 jars := append(android.Paths(nil), kotlinJars...)
1079
Colin Cross5ab4e6d2017-11-22 16:20:45 -08001080 // Store the list of .java files that was passed to javac
1081 j.compiledJavaSrcs = uniqueSrcFiles
1082 j.compiledSrcJars = srcJars
1083
Nan Zhang61eaedb2017-11-02 13:28:15 -07001084 enable_sharding := false
Colin Crossbe9cdb82019-01-21 21:37:16 -08001085 if ctx.Device() && !ctx.Config().IsEnvFalse("TURBINE_ENABLED") && !deps.disableTurbine {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001086 if j.properties.Javac_shard_size != nil && *(j.properties.Javac_shard_size) > 0 {
1087 enable_sharding = true
Ashley Rosee36efcf2019-01-16 17:34:08 -05001088 // Formerly, there was a check here that prevented annotation processors
1089 // from being used when sharding was enabled, as some annotation processors
1090 // do not function correctly in sharded environments. It was removed to
1091 // allow for the use of annotation processors that do function correctly
1092 // with sharding enabled. See: b/77284273.
Nan Zhang61eaedb2017-11-02 13:28:15 -07001093 }
Colin Cross55f63ea2018-08-27 12:37:09 -07001094 j.headerJarFile = j.compileJavaHeader(ctx, uniqueSrcFiles, srcJars, deps, flags, jarName, kotlinJars)
Colin Crossf19b9bb2018-03-26 14:42:44 -07001095 if ctx.Failed() {
1096 return
Nan Zhanged19fc32017-10-19 13:06:22 -07001097 }
1098 }
Colin Cross8eadbf02017-10-24 17:46:00 -07001099 if len(uniqueSrcFiles) > 0 || len(srcJars) > 0 {
Colin Crossd6891432017-09-27 17:39:56 -07001100 var extraJarDeps android.Paths
Colin Cross66548102018-06-19 22:47:35 -07001101 if ctx.Config().RunErrorProne() {
Colin Crossc6bbef32017-08-14 14:16:06 -07001102 // If error-prone is enabled, add an additional rule to compile the java files into
1103 // a separate set of classes (so that they don't overwrite the normal ones and require
Colin Crossd6891432017-09-27 17:39:56 -07001104 // a rebuild when error-prone is turned off).
Colin Crossc6bbef32017-08-14 14:16:06 -07001105 // TODO(ccross): Once we always compile with javac9 we may be able to conditionally
1106 // enable error-prone without affecting the output class files.
Colin Cross1ee23172017-10-18 14:44:18 -07001107 errorprone := android.PathForModuleOut(ctx, "errorprone", jarName)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001108 RunErrorProne(ctx, errorprone, uniqueSrcFiles, srcJars, flags)
Colin Crossc6bbef32017-08-14 14:16:06 -07001109 extraJarDeps = append(extraJarDeps, errorprone)
1110 }
1111
Nan Zhang61eaedb2017-11-02 13:28:15 -07001112 if enable_sharding {
Nan Zhang581fd212018-01-10 16:06:12 -08001113 flags.classpath = append(flags.classpath, j.headerJarFile)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001114 shardSize := int(*(j.properties.Javac_shard_size))
1115 var shardSrcs []android.Paths
1116 if len(uniqueSrcFiles) > 0 {
1117 shardSrcs = shardPaths(uniqueSrcFiles, shardSize)
1118 for idx, shardSrc := range shardSrcs {
1119 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(idx))
1120 TransformJavaToClasses(ctx, classes, idx, shardSrc, nil, flags, extraJarDeps)
1121 jars = append(jars, classes)
1122 }
1123 }
1124 if len(srcJars) > 0 {
1125 classes := android.PathForModuleOut(ctx, "javac", jarName+strconv.Itoa(len(shardSrcs)))
1126 TransformJavaToClasses(ctx, classes, len(shardSrcs), nil, srcJars, flags, extraJarDeps)
1127 jars = append(jars, classes)
1128 }
1129 } else {
1130 classes := android.PathForModuleOut(ctx, "javac", jarName)
1131 TransformJavaToClasses(ctx, classes, -1, uniqueSrcFiles, srcJars, flags, extraJarDeps)
1132 jars = append(jars, classes)
1133 }
Colin Crossd6891432017-09-27 17:39:56 -07001134 if ctx.Failed() {
1135 return
1136 }
Colin Cross2fe66872015-03-30 17:20:39 -07001137 }
1138
Colin Cross0c4ce212019-05-03 15:28:19 -07001139 j.srcJarArgs, j.srcJarDeps = resourcePathsToJarArgs(srcFiles), srcFiles
1140
1141 var includeSrcJar android.WritablePath
1142 if Bool(j.properties.Include_srcs) {
1143 includeSrcJar = android.PathForModuleOut(ctx, ctx.ModuleName()+".srcjar")
1144 TransformResourcesToJar(ctx, includeSrcJar, j.srcJarArgs, j.srcJarDeps)
1145 }
1146
Colin Crosscedd4762018-09-13 11:26:19 -07001147 dirArgs, dirDeps := ResourceDirsToJarArgs(ctx, j.properties.Java_resource_dirs,
1148 j.properties.Exclude_java_resource_dirs, j.properties.Exclude_java_resources)
Colin Cross0f37af02017-09-27 17:42:05 -07001149 fileArgs, fileDeps := ResourceFilesToJarArgs(ctx, j.properties.Java_resources, j.properties.Exclude_java_resources)
Colin Cross988708c2019-05-06 14:04:11 -07001150 extraArgs, extraDeps := resourcePathsToJarArgs(j.extraResources), j.extraResources
Colin Cross0f37af02017-09-27 17:42:05 -07001151
1152 var resArgs []string
1153 var resDeps android.Paths
1154
1155 resArgs = append(resArgs, dirArgs...)
1156 resDeps = append(resDeps, dirDeps...)
1157
1158 resArgs = append(resArgs, fileArgs...)
1159 resDeps = append(resDeps, fileDeps...)
1160
Colin Cross988708c2019-05-06 14:04:11 -07001161 resArgs = append(resArgs, extraArgs...)
1162 resDeps = append(resDeps, extraDeps...)
1163
Colin Cross40a36712017-09-27 17:41:35 -07001164 if len(resArgs) > 0 {
Colin Cross1ee23172017-10-18 14:44:18 -07001165 resourceJar := android.PathForModuleOut(ctx, "res", jarName)
Colin Crosse9a275b2017-10-16 17:09:48 -07001166 TransformResourcesToJar(ctx, resourceJar, resArgs, resDeps)
Colin Cross331a1212018-08-15 20:40:52 -07001167 j.resourceJar = resourceJar
Colin Cross65bf4f22015-04-03 16:54:17 -07001168 if ctx.Failed() {
1169 return
1170 }
1171 }
1172
Colin Cross0c4ce212019-05-03 15:28:19 -07001173 var resourceJars android.Paths
1174 if j.resourceJar != nil {
1175 resourceJars = append(resourceJars, j.resourceJar)
1176 }
1177 if Bool(j.properties.Include_srcs) {
1178 resourceJars = append(resourceJars, includeSrcJar)
1179 }
1180 resourceJars = append(resourceJars, deps.staticResourceJars...)
Colin Cross331a1212018-08-15 20:40:52 -07001181
Colin Cross0c4ce212019-05-03 15:28:19 -07001182 if len(resourceJars) > 1 {
Colin Cross331a1212018-08-15 20:40:52 -07001183 combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
Colin Cross0c4ce212019-05-03 15:28:19 -07001184 TransformJarsToJar(ctx, combinedJar, "for resources", resourceJars, android.OptionalPath{},
Colin Cross331a1212018-08-15 20:40:52 -07001185 false, nil, nil)
1186 j.resourceJar = combinedJar
Colin Cross0c4ce212019-05-03 15:28:19 -07001187 } else if len(resourceJars) == 1 {
1188 j.resourceJar = resourceJars[0]
Colin Cross331a1212018-08-15 20:40:52 -07001189 }
1190
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001191 if len(deps.staticJars) > 0 {
1192 jars = append(jars, deps.staticJars...)
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001193 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001194
Colin Cross094054a2018-10-17 15:10:48 -07001195 manifest := j.overrideManifest
1196 if !manifest.Valid() && j.properties.Manifest != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001197 manifest = android.OptionalPathForPath(android.PathForModuleSrc(ctx, *j.properties.Manifest))
Colin Cross366938f2017-12-11 16:29:02 -08001198 }
Colin Cross635acc92017-09-12 22:50:46 -07001199
Colin Cross8a497952019-03-05 22:25:09 -08001200 services := android.PathsForModuleSrc(ctx, j.properties.Services)
Alex Light7f004a72019-02-21 13:27:37 -08001201 if len(services) > 0 {
1202 servicesJar := android.PathForModuleOut(ctx, "services", jarName)
1203 var zipargs []string
1204 for _, file := range services {
1205 serviceFile := file.String()
1206 zipargs = append(zipargs, "-C", filepath.Dir(serviceFile), "-f", serviceFile)
1207 }
1208 ctx.Build(pctx, android.BuildParams{
1209 Rule: zip,
1210 Output: servicesJar,
1211 Implicits: services,
1212 Args: map[string]string{
Colin Cross0b9f31f2019-02-28 11:00:01 -08001213 "jarArgs": "-P META-INF/services/ " + strings.Join(proptools.NinjaAndShellEscapeList(zipargs), " "),
Alex Light7f004a72019-02-21 13:27:37 -08001214 },
1215 })
1216 jars = append(jars, servicesJar)
1217 }
1218
Colin Cross0a6e0072017-08-30 14:24:55 -07001219 // Combine the classes built from sources, any manifests, and any static libraries into
Nan Zhanged19fc32017-10-19 13:06:22 -07001220 // classes.jar. If there is only one input jar this step will be skipped.
Colin Cross3063b782018-08-15 11:19:12 -07001221 var outputFile android.ModuleOutPath
Colin Crosse9a275b2017-10-16 17:09:48 -07001222
1223 if len(jars) == 1 && !manifest.Valid() {
Colin Cross3063b782018-08-15 11:19:12 -07001224 if moduleOutPath, ok := jars[0].(android.ModuleOutPath); ok {
1225 // Optimization: skip the combine step if there is nothing to do
1226 // TODO(ccross): this leaves any module-info.class files, but those should only come from
1227 // prebuilt dependencies until we support modules in the platform build, so there shouldn't be
1228 // any if len(jars) == 1.
1229 outputFile = moduleOutPath
1230 } else {
1231 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
1232 ctx.Build(pctx, android.BuildParams{
1233 Rule: android.Cp,
1234 Input: jars[0],
1235 Output: combinedJar,
1236 })
1237 outputFile = combinedJar
1238 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001239 } else {
Colin Cross1ee23172017-10-18 14:44:18 -07001240 combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001241 TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
Colin Cross9b38aef2018-08-27 15:42:25 -07001242 false, nil, nil)
Colin Crosse9a275b2017-10-16 17:09:48 -07001243 outputFile = combinedJar
1244 }
Colin Cross0a6e0072017-08-30 14:24:55 -07001245
Colin Cross331a1212018-08-15 20:40:52 -07001246 // jarjar implementation jar if necessary
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001247 if j.expandJarjarRules != nil {
Colin Cross8649b262017-09-27 18:03:17 -07001248 // Transform classes.jar into classes-jarjar.jar
Colin Cross1ee23172017-10-18 14:44:18 -07001249 jarjarFile := android.PathForModuleOut(ctx, "jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001250 TransformJarJar(ctx, jarjarFile, outputFile, j.expandJarjarRules)
Colin Crosse9a275b2017-10-16 17:09:48 -07001251 outputFile = jarjarFile
Colin Cross331a1212018-08-15 20:40:52 -07001252
1253 // jarjar resource jar if necessary
1254 if j.resourceJar != nil {
1255 resourceJarJarFile := android.PathForModuleOut(ctx, "res-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001256 TransformJarJar(ctx, resourceJarJarFile, j.resourceJar, j.expandJarjarRules)
Colin Cross331a1212018-08-15 20:40:52 -07001257 j.resourceJar = resourceJarJarFile
1258 }
1259
Colin Cross0a6e0072017-08-30 14:24:55 -07001260 if ctx.Failed() {
1261 return
1262 }
1263 }
Vladimir Marko0975ee02019-04-02 10:29:55 +01001264
1265 // Check package restrictions if necessary.
1266 if len(j.properties.Permitted_packages) > 0 {
1267 // Check packages and copy to package-checked file.
1268 pkgckFile := android.PathForModuleOut(ctx, "package-check.stamp")
1269 CheckJarPackages(ctx, pkgckFile, outputFile, j.properties.Permitted_packages)
1270 j.additionalCheckedModules = append(j.additionalCheckedModules, pkgckFile)
1271
1272 if ctx.Failed() {
1273 return
1274 }
1275 }
1276
Nan Zhanged19fc32017-10-19 13:06:22 -07001277 j.implementationJarFile = outputFile
1278 if j.headerJarFile == nil {
1279 j.headerJarFile = j.implementationJarFile
1280 }
Colin Cross2fe66872015-03-30 17:20:39 -07001281
Colin Cross6510f912017-11-29 00:27:14 -08001282 if ctx.Config().IsEnvTrue("EMMA_INSTRUMENT_FRAMEWORK") {
Colin Crosscb933592017-11-22 13:49:43 -08001283 if inList(ctx.ModuleName(), config.InstrumentFrameworkModules) {
1284 j.properties.Instrument = true
1285 }
1286 }
1287
Colin Cross3144dfc2018-01-03 15:06:47 -08001288 if j.shouldInstrument(ctx) {
Colin Crosscb933592017-11-22 13:49:43 -08001289 outputFile = j.instrument(ctx, flags, outputFile, jarName)
1290 }
1291
Colin Cross331a1212018-08-15 20:40:52 -07001292 // merge implementation jar with resources if necessary
1293 implementationAndResourcesJar := outputFile
1294 if j.resourceJar != nil {
Colin Cross08a409d2019-04-29 10:22:44 -07001295 jars := android.Paths{j.resourceJar, implementationAndResourcesJar}
Colin Cross331a1212018-08-15 20:40:52 -07001296 combinedJar := android.PathForModuleOut(ctx, "withres", jarName)
Colin Cross08a409d2019-04-29 10:22:44 -07001297 TransformJarsToJar(ctx, combinedJar, "for resources", jars, manifest,
Colin Cross331a1212018-08-15 20:40:52 -07001298 false, nil, nil)
1299 implementationAndResourcesJar = combinedJar
1300 }
1301
1302 j.implementationAndResourcesJar = implementationAndResourcesJar
1303
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001304 if ctx.Device() && j.hasCode(ctx) &&
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001305 (Bool(j.properties.Installable) || Bool(j.deviceProperties.Compile_dex)) {
Colin Cross8faf8fc2019-01-16 15:15:52 -08001306 // Dex compilation
Colin Cross3063b782018-08-15 11:19:12 -07001307 var dexOutputFile android.ModuleOutPath
David Brazdil17ef5632018-06-27 10:27:45 +01001308 dexOutputFile = j.compileDex(ctx, flags, outputFile, jarName)
Colin Cross2fe66872015-03-30 17:20:39 -07001309 if ctx.Failed() {
1310 return
1311 }
Colin Cross331a1212018-08-15 20:40:52 -07001312
Colin Cross9c74a1e2019-05-28 14:06:17 -07001313 if !ctx.Config().UnbundledBuild() {
1314 // Hidden API CSV generation and dex encoding
1315 dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, dexOutputFile, j.implementationJarFile,
1316 j.deviceProperties.UncompressDex)
1317 }
Colin Cross8faf8fc2019-01-16 15:15:52 -08001318
Colin Cross331a1212018-08-15 20:40:52 -07001319 // merge dex jar with resources if necessary
1320 if j.resourceJar != nil {
1321 jars := android.Paths{dexOutputFile, j.resourceJar}
1322 combinedJar := android.PathForModuleOut(ctx, "dex-withres", jarName)
1323 TransformJarsToJar(ctx, combinedJar, "for dex resources", jars, android.OptionalPath{},
1324 false, nil, nil)
Nicolas Geoffrayf3438722019-01-23 15:57:21 +00001325 if j.deviceProperties.UncompressDex {
1326 combinedAlignedJar := android.PathForModuleOut(ctx, "dex-withres-aligned", jarName)
1327 TransformZipAlign(ctx, combinedAlignedJar, combinedJar)
1328 dexOutputFile = combinedAlignedJar
1329 } else {
1330 dexOutputFile = combinedJar
1331 }
Colin Cross331a1212018-08-15 20:40:52 -07001332 }
1333
1334 j.dexJarFile = dexOutputFile
1335
Colin Cross8faf8fc2019-01-16 15:15:52 -08001336 // Dexpreopting
Colin Cross43f08db2018-11-12 10:13:39 -08001337 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
1338
1339 j.maybeStrippedDexJarFile = dexOutputFile
1340
Colin Cross3063b782018-08-15 11:19:12 -07001341 outputFile = dexOutputFile
Colin Cross43f08db2018-11-12 10:13:39 -08001342
1343 if ctx.Failed() {
1344 return
1345 }
Colin Cross331a1212018-08-15 20:40:52 -07001346 } else {
1347 outputFile = implementationAndResourcesJar
Colin Cross2fe66872015-03-30 17:20:39 -07001348 }
Colin Cross331a1212018-08-15 20:40:52 -07001349
Colin Crossb7a63242015-04-16 14:09:14 -07001350 ctx.CheckbuildFile(outputFile)
Colin Cross3063b782018-08-15 11:19:12 -07001351
1352 // Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
1353 j.outputFile = outputFile.WithoutRel()
Colin Cross2fe66872015-03-30 17:20:39 -07001354}
1355
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001356// Check for invalid kotlinc flags. Only use this for flags explicitly passed by the user,
1357// since some of these flags may be used internally.
1358func CheckKotlincFlags(ctx android.ModuleContext, flags []string) {
1359 for _, flag := range flags {
1360 flag = strings.TrimSpace(flag)
1361
1362 if !strings.HasPrefix(flag, "-") {
1363 ctx.PropertyErrorf("kotlincflags", "Flag `%s` must start with `-`", flag)
1364 } else if strings.HasPrefix(flag, "-Xintellij-plugin-root") {
1365 ctx.PropertyErrorf("kotlincflags",
1366 "Bad flag: `%s`, only use internal compiler for consistency.", flag)
1367 } else if inList(flag, config.KotlincIllegalFlags) {
1368 ctx.PropertyErrorf("kotlincflags", "Flag `%s` already used by build system", flag)
1369 } else if flag == "-include-runtime" {
1370 ctx.PropertyErrorf("kotlincflags", "Bad flag: `%s`, do not include runtime.", flag)
1371 } else {
1372 args := strings.Split(flag, " ")
1373 if args[0] == "-kotlin-home" {
1374 ctx.PropertyErrorf("kotlincflags",
1375 "Bad flag: `%s`, kotlin home already set to default (path to kotlinc in the repo).", flag)
1376 }
1377 }
1378 }
1379}
1380
Colin Cross8eadbf02017-10-24 17:46:00 -07001381func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars android.Paths,
Colin Cross55f63ea2018-08-27 12:37:09 -07001382 deps deps, flags javaBuilderFlags, jarName string, extraJars android.Paths) android.Path {
Nan Zhanged19fc32017-10-19 13:06:22 -07001383
1384 var jars android.Paths
Colin Cross8eadbf02017-10-24 17:46:00 -07001385 if len(srcFiles) > 0 || len(srcJars) > 0 {
Nan Zhanged19fc32017-10-19 13:06:22 -07001386 // Compile java sources into turbine.jar.
1387 turbineJar := android.PathForModuleOut(ctx, "turbine", jarName)
1388 TransformJavaToHeaderClasses(ctx, turbineJar, srcFiles, srcJars, flags)
1389 if ctx.Failed() {
1390 return nil
1391 }
1392 jars = append(jars, turbineJar)
1393 }
1394
Colin Cross55f63ea2018-08-27 12:37:09 -07001395 jars = append(jars, extraJars...)
1396
Nan Zhanged19fc32017-10-19 13:06:22 -07001397 // Combine any static header libraries into classes-header.jar. If there is only
1398 // one input jar this step will be skipped.
1399 var headerJar android.Path
1400 jars = append(jars, deps.staticHeaderJars...)
1401
Colin Cross5c6ecc12017-10-23 18:12:27 -07001402 // we cannot skip the combine step for now if there is only one jar
1403 // since we have to strip META-INF/TRANSITIVE dir from turbine.jar
1404 combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001405 TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
Colin Cross6c6e6cd2019-05-08 14:30:12 -07001406 false, nil, []string{"META-INF/TRANSITIVE"})
Colin Cross5c6ecc12017-10-23 18:12:27 -07001407 headerJar = combinedJar
Nan Zhanged19fc32017-10-19 13:06:22 -07001408
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001409 if j.expandJarjarRules != nil {
Nan Zhanged19fc32017-10-19 13:06:22 -07001410 // Transform classes.jar into classes-jarjar.jar
1411 jarjarFile := android.PathForModuleOut(ctx, "turbine-jarjar", jarName)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001412 TransformJarJar(ctx, jarjarFile, headerJar, j.expandJarjarRules)
Nan Zhanged19fc32017-10-19 13:06:22 -07001413 headerJar = jarjarFile
1414 if ctx.Failed() {
1415 return nil
1416 }
1417 }
1418
1419 return headerJar
1420}
1421
Colin Crosscb933592017-11-22 13:49:43 -08001422func (j *Module) instrument(ctx android.ModuleContext, flags javaBuilderFlags,
Colin Cross3063b782018-08-15 11:19:12 -07001423 classesJar android.Path, jarName string) android.ModuleOutPath {
Colin Crosscb933592017-11-22 13:49:43 -08001424
Colin Cross7a3139e2017-12-19 13:57:50 -08001425 specs := j.jacocoModuleToZipCommand(ctx)
Colin Crosscb933592017-11-22 13:49:43 -08001426
Colin Cross84c38822018-01-03 15:59:46 -08001427 jacocoReportClassesFile := android.PathForModuleOut(ctx, "jacoco-report-classes", jarName)
Colin Crosscb933592017-11-22 13:49:43 -08001428 instrumentedJar := android.PathForModuleOut(ctx, "jacoco", jarName)
1429
1430 jacocoInstrumentJar(ctx, instrumentedJar, jacocoReportClassesFile, classesJar, specs)
1431
1432 j.jacocoReportClassesFile = jacocoReportClassesFile
1433
1434 return instrumentedJar
1435}
1436
albaltai36ff7dc2018-12-25 14:35:23 +08001437var _ Dependency = (*Module)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001438
Nan Zhanged19fc32017-10-19 13:06:22 -07001439func (j *Module) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001440 if j.headerJarFile == nil {
1441 return nil
1442 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001443 return android.Paths{j.headerJarFile}
1444}
1445
1446func (j *Module) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001447 if j.implementationJarFile == nil {
1448 return nil
1449 }
Nan Zhanged19fc32017-10-19 13:06:22 -07001450 return android.Paths{j.implementationJarFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001451}
1452
Colin Crossf24a22a2019-01-31 14:12:44 -08001453func (j *Module) DexJar() android.Path {
1454 return j.dexJarFile
1455}
1456
Colin Cross331a1212018-08-15 20:40:52 -07001457func (j *Module) ResourceJars() android.Paths {
1458 if j.resourceJar == nil {
1459 return nil
1460 }
1461 return android.Paths{j.resourceJar}
1462}
1463
1464func (j *Module) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001465 if j.implementationAndResourcesJar == nil {
1466 return nil
1467 }
Colin Cross331a1212018-08-15 20:40:52 -07001468 return android.Paths{j.implementationAndResourcesJar}
1469}
1470
Colin Cross46c9b8b2017-06-22 16:51:17 -07001471func (j *Module) AidlIncludeDirs() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001472 // exportAidlIncludeDirs is type android.Paths already
Colin Crossc0b06f12015-04-08 13:03:43 -07001473 return j.exportAidlIncludeDirs
1474}
1475
Jiyong Park1be96912018-05-28 18:02:19 +09001476func (j *Module) ExportedSdkLibs() []string {
albaltai36ff7dc2018-12-25 14:35:23 +08001477 // exportedSdkLibs is type []string
Jiyong Park1be96912018-05-28 18:02:19 +09001478 return j.exportedSdkLibs
1479}
1480
Colin Cross0c4ce212019-05-03 15:28:19 -07001481func (j *Module) SrcJarArgs() ([]string, android.Paths) {
1482 return j.srcJarArgs, j.srcJarDeps
1483}
1484
Colin Cross46c9b8b2017-06-22 16:51:17 -07001485var _ logtagsProducer = (*Module)(nil)
Colin Crossf05fe972015-04-10 17:45:20 -07001486
Colin Cross46c9b8b2017-06-22 16:51:17 -07001487func (j *Module) logtags() android.Paths {
Colin Crossf05fe972015-04-10 17:45:20 -07001488 return j.logtagsSrcs
1489}
1490
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001491// Collect information for opening IDE project files in java/jdeps.go.
1492func (j *Module) IDEInfo(dpInfo *android.IdeInfo) {
1493 dpInfo.Deps = append(dpInfo.Deps, j.CompilerDeps()...)
1494 dpInfo.Srcs = append(dpInfo.Srcs, j.expandIDEInfoCompiledSrcs...)
patricktu18c82ff2019-05-10 15:48:50 +08001495 dpInfo.SrcJars = append(dpInfo.SrcJars, j.compiledSrcJars.Strings()...)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001496 dpInfo.Aidl_include_dirs = append(dpInfo.Aidl_include_dirs, j.deviceProperties.Aidl.Include_dirs...)
Steven Morelandc4efd9c2019-01-18 11:51:25 -08001497 if j.expandJarjarRules != nil {
1498 dpInfo.Jarjar_rules = append(dpInfo.Jarjar_rules, j.expandJarjarRules.String())
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001499 }
1500}
1501
1502func (j *Module) CompilerDeps() []string {
1503 jdeps := []string{}
1504 jdeps = append(jdeps, j.properties.Libs...)
1505 jdeps = append(jdeps, j.properties.Static_libs...)
1506 return jdeps
1507}
1508
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001509func (j *Module) hasCode(ctx android.ModuleContext) bool {
1510 srcFiles := android.PathsForModuleSrcExcludes(ctx, j.properties.Srcs, j.properties.Exclude_srcs)
1511 return len(srcFiles) > 0 || len(ctx.GetDirectDepsWithTag(staticLibTag)) > 0
1512}
1513
Colin Cross2fe66872015-03-30 17:20:39 -07001514//
1515// Java libraries (.jar file)
1516//
1517
Colin Crossf506d872017-07-19 15:53:04 -07001518type Library struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001519 Module
Colin Cross2fe66872015-03-30 17:20:39 -07001520}
1521
Colin Cross42be7612019-02-21 18:12:14 -08001522func shouldUncompressDex(ctx android.ModuleContext, dexpreopter *dexpreopter) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001523 // Store uncompressed (and do not strip) dex files from boot class path jars.
1524 if inList(ctx.ModuleName(), ctx.Config().BootJars()) {
1525 return true
1526 }
1527
1528 // Store uncompressed dex files that are preopted on /system.
Colin Cross42be7612019-02-21 18:12:14 -08001529 if !dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, dexpreopter.installPath)) {
Vladimir Markoe8b00d62018-12-21 15:54:16 +00001530 return true
1531 }
Colin Cross083a2aa2019-02-06 16:37:12 -08001532 if ctx.Config().UncompressPrivAppDex() &&
1533 inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules()) {
1534 return true
1535 }
1536
Colin Cross2fc72f62018-12-21 12:59:54 -08001537 return false
1538}
1539
Colin Crossf506d872017-07-19 15:53:04 -07001540func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross43f08db2018-11-12 10:13:39 -08001541 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
1542 j.dexpreopter.isSDKLibrary = j.deviceProperties.IsSDKLibrary
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001543 j.dexpreopter.isInstallable = Bool(j.properties.Installable)
Colin Cross42be7612019-02-21 18:12:14 -08001544 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +00001545 j.deviceProperties.UncompressDex = j.dexpreopter.uncompressedDex
Jaewoong Junga24af3b2019-05-13 09:23:20 -07001546 j.compile(ctx, nil)
Colin Crossb7a63242015-04-16 14:09:14 -07001547
Nicolas Geoffray4bdea392019-01-15 11:48:08 +00001548 if (Bool(j.properties.Installable) || ctx.Host()) && !android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
Colin Cross2c429dc2017-08-31 16:45:16 -07001549 j.installFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1550 ctx.ModuleName()+".jar", j.outputFile)
1551 }
Colin Crossb7a63242015-04-16 14:09:14 -07001552}
1553
Colin Crossf506d872017-07-19 15:53:04 -07001554func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross46c9b8b2017-06-22 16:51:17 -07001555 j.deps(ctx)
1556}
1557
Colin Cross1b16b0e2019-02-12 14:41:32 -08001558// java_library builds and links sources into a `.jar` file for the device, and possibly for the host as well.
1559//
1560// By default, a java_library has a single variant that produces a `.jar` file containing `.class` files that were
1561// compiled against the device bootclasspath. This jar is not suitable for installing on a device, but can be used
1562// as a `static_libs` dependency of another module.
1563//
1564// Specifying `installable: true` will product a `.jar` file containing `classes.dex` files, suitable for installing on
1565// a device.
1566//
1567// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1568// compiled against the host bootclasspath.
Colin Cross9ae1b922018-06-26 17:59:05 -07001569func LibraryFactory() android.Module {
1570 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001571
Colin Cross9ae1b922018-06-26 17:59:05 -07001572 module.AddProperties(
1573 &module.Module.properties,
1574 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001575 &module.Module.dexpreoptProperties,
Colin Cross9ae1b922018-06-26 17:59:05 -07001576 &module.Module.protoProperties)
Colin Cross2fe66872015-03-30 17:20:39 -07001577
Colin Cross9ae1b922018-06-26 17:59:05 -07001578 InitJavaModule(module, android.HostAndDeviceSupported)
1579 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001580}
1581
Colin Cross1b16b0e2019-02-12 14:41:32 -08001582// java_library_static is an obsolete alias for java_library.
1583func LibraryStaticFactory() android.Module {
1584 return LibraryFactory()
1585}
1586
1587// java_library_host builds and links sources into a `.jar` file for the host.
1588//
1589// A java_library_host has a single variant that produces a `.jar` file containing `.class` files that were
1590// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001591func LibraryHostFactory() android.Module {
1592 module := &Library{}
Colin Cross2fe66872015-03-30 17:20:39 -07001593
Colin Cross6af17aa2017-09-20 12:59:05 -07001594 module.AddProperties(
1595 &module.Module.properties,
1596 &module.Module.protoProperties)
Colin Cross36242852017-06-23 15:06:31 -07001597
Colin Cross9ae1b922018-06-26 17:59:05 -07001598 module.Module.properties.Installable = proptools.BoolPtr(true)
1599
Colin Cross89536d42017-07-07 14:35:50 -07001600 InitJavaModule(module, android.HostSupported)
Colin Cross36242852017-06-23 15:06:31 -07001601 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001602}
1603
1604//
Colin Crossb628ea52018-08-14 16:42:33 -07001605// Java Tests
Colin Cross05638fc2018-04-09 18:40:24 -07001606//
1607
1608type testProperties struct {
Colin Cross05638fc2018-04-09 18:40:24 -07001609 // list of compatibility suites (for example "cts", "vts") that the module should be
1610 // installed into.
1611 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -07001612
1613 // the name of the test configuration (for example "AndroidTest.xml") that should be
1614 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001615 Test_config *string `android:"path,arch_variant"`
Colin Crossd96ca352018-08-10 16:06:24 -07001616
Jack He33338892018-09-19 02:21:28 -07001617 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
1618 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -08001619 Test_config_template *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -07001620
Colin Crossd96ca352018-08-10 16:06:24 -07001621 // list of files or filegroup modules that provide data that should be installed alongside
1622 // the test
Colin Cross27b922f2019-03-04 22:35:41 -08001623 Data []string `android:"path"`
Colin Cross05638fc2018-04-09 18:40:24 -07001624}
1625
Paul Duffin42df1442019-03-20 12:45:53 +00001626type testHelperLibraryProperties struct {
1627 // list of compatibility suites (for example "cts", "vts") that the module should be
1628 // installed into.
1629 Test_suites []string `android:"arch_variant"`
1630}
1631
Colin Cross05638fc2018-04-09 18:40:24 -07001632type Test struct {
1633 Library
1634
1635 testProperties testProperties
Colin Cross303e21f2018-08-07 16:49:25 -07001636
1637 testConfig android.Path
Colin Crossd96ca352018-08-10 16:06:24 -07001638 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -07001639}
1640
Paul Duffin42df1442019-03-20 12:45:53 +00001641type TestHelperLibrary struct {
1642 Library
1643
1644 testHelperLibraryProperties testHelperLibraryProperties
1645}
1646
Colin Cross303e21f2018-08-07 16:49:25 -07001647func (j *Test) GenerateAndroidBuildActions(ctx android.ModuleContext) {
yangbill4f41bc22019-02-13 21:45:47 +08001648 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 -08001649 j.data = android.PathsForModuleSrc(ctx, j.testProperties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -07001650
1651 j.Library.GenerateAndroidBuildActions(ctx)
Colin Cross05638fc2018-04-09 18:40:24 -07001652}
1653
Paul Duffin42df1442019-03-20 12:45:53 +00001654func (j *TestHelperLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1655 j.Library.GenerateAndroidBuildActions(ctx)
1656}
1657
Colin Cross1b16b0e2019-02-12 14:41:32 -08001658// java_test builds a and links sources into a `.jar` file for the device, and possibly for the host as well, and
1659// creates an `AndroidTest.xml` file to allow running the test with `atest` or a `TEST_MAPPING` file.
1660//
1661// By default, a java_test has a single variant that produces a `.jar` file containing `classes.dex` files that were
1662// compiled against the device bootclasspath.
1663//
1664// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1665// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001666func TestFactory() android.Module {
1667 module := &Test{}
1668
1669 module.AddProperties(
1670 &module.Module.properties,
1671 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001672 &module.Module.dexpreoptProperties,
Colin Cross05638fc2018-04-09 18:40:24 -07001673 &module.Module.protoProperties,
1674 &module.testProperties)
1675
Colin Cross9ae1b922018-06-26 17:59:05 -07001676 module.Module.properties.Installable = proptools.BoolPtr(true)
Colin Crosse3026872019-01-05 22:30:13 -08001677 module.Module.dexpreopter.isTest = true
Colin Cross9ae1b922018-06-26 17:59:05 -07001678
Colin Cross05638fc2018-04-09 18:40:24 -07001679 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001680 return module
1681}
1682
Paul Duffin42df1442019-03-20 12:45:53 +00001683// java_test_helper_library creates a java library and makes sure that it is added to the appropriate test suite.
1684func TestHelperLibraryFactory() android.Module {
1685 module := &TestHelperLibrary{}
1686
1687 module.AddProperties(
1688 &module.Module.properties,
1689 &module.Module.deviceProperties,
1690 &module.Module.dexpreoptProperties,
1691 &module.Module.protoProperties,
1692 &module.testHelperLibraryProperties)
1693
Colin Cross9a4abed2019-04-24 13:19:28 -07001694 module.Module.properties.Installable = proptools.BoolPtr(true)
1695 module.Module.dexpreopter.isTest = true
1696
Paul Duffin42df1442019-03-20 12:45:53 +00001697 InitJavaModule(module, android.HostAndDeviceSupported)
1698 return module
1699}
1700
Colin Cross1b16b0e2019-02-12 14:41:32 -08001701// java_test_host builds a and links sources into a `.jar` file for the host, and creates an `AndroidTest.xml` file to
1702// allow running the test with `atest` or a `TEST_MAPPING` file.
1703//
1704// A java_test_host has a single variant that produces a `.jar` file containing `.class` files that were
1705// compiled against the host bootclasspath.
Colin Cross05638fc2018-04-09 18:40:24 -07001706func TestHostFactory() android.Module {
1707 module := &Test{}
1708
1709 module.AddProperties(
1710 &module.Module.properties,
1711 &module.Module.protoProperties,
1712 &module.testProperties)
1713
Colin Cross9ae1b922018-06-26 17:59:05 -07001714 module.Module.properties.Installable = proptools.BoolPtr(true)
1715
Colin Cross05638fc2018-04-09 18:40:24 -07001716 InitJavaModule(module, android.HostSupported)
Colin Cross05638fc2018-04-09 18:40:24 -07001717 return module
1718}
1719
1720//
Colin Cross2fe66872015-03-30 17:20:39 -07001721// Java Binaries (.jar file plus wrapper script)
1722//
1723
Colin Crossf506d872017-07-19 15:53:04 -07001724type binaryProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -07001725 // installable script to execute the resulting jar
Colin Cross27b922f2019-03-04 22:35:41 -08001726 Wrapper *string `android:"path"`
Colin Cross094054a2018-10-17 15:10:48 -07001727
1728 // Name of the class containing main to be inserted into the manifest as Main-Class.
1729 Main_class *string
Colin Cross7d5136f2015-05-11 13:39:40 -07001730}
1731
Colin Crossf506d872017-07-19 15:53:04 -07001732type Binary struct {
1733 Library
Colin Cross2fe66872015-03-30 17:20:39 -07001734
Colin Crossf506d872017-07-19 15:53:04 -07001735 binaryProperties binaryProperties
Colin Cross10a03492017-08-10 17:09:43 -07001736
Colin Cross6b4a32d2017-12-05 13:42:45 -08001737 isWrapperVariant bool
1738
Colin Crossc3315992017-12-08 19:12:36 -08001739 wrapperFile android.Path
Colin Cross10a03492017-08-10 17:09:43 -07001740 binaryFile android.OutputPath
Colin Cross2fe66872015-03-30 17:20:39 -07001741}
1742
Alex Light24237172017-10-26 09:46:21 -07001743func (j *Binary) HostToolPath() android.OptionalPath {
1744 return android.OptionalPathForPath(j.binaryFile)
1745}
1746
Colin Crossf506d872017-07-19 15:53:04 -07001747func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001748 if ctx.Arch().ArchType == android.Common {
1749 // Compile the jar
Colin Cross094054a2018-10-17 15:10:48 -07001750 if j.binaryProperties.Main_class != nil {
1751 if j.properties.Manifest != nil {
1752 ctx.PropertyErrorf("main_class", "main_class cannot be used when manifest is set")
1753 }
1754 manifestFile := android.PathForModuleOut(ctx, "manifest.txt")
1755 GenerateMainClassManifest(ctx, manifestFile, String(j.binaryProperties.Main_class))
1756 j.overrideManifest = android.OptionalPathForPath(manifestFile)
1757 }
1758
Colin Cross6b4a32d2017-12-05 13:42:45 -08001759 j.Library.GenerateAndroidBuildActions(ctx)
Nan Zhang3c807db2017-11-03 14:53:31 -07001760 } else {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001761 // Handle the binary wrapper
1762 j.isWrapperVariant = true
1763
Colin Cross366938f2017-12-11 16:29:02 -08001764 if j.binaryProperties.Wrapper != nil {
Colin Cross8a497952019-03-05 22:25:09 -08001765 j.wrapperFile = android.PathForModuleSrc(ctx, *j.binaryProperties.Wrapper)
Colin Cross6b4a32d2017-12-05 13:42:45 -08001766 } else {
1767 j.wrapperFile = android.PathForSource(ctx, "build/soong/scripts/jar-wrapper.sh")
1768 }
1769
1770 // Depend on the installed jar so that the wrapper doesn't get executed by
1771 // another build rule before the jar has been installed.
1772 jarFile := ctx.PrimaryModule().(*Binary).installFile
1773
1774 j.binaryFile = ctx.InstallExecutable(android.PathForModuleInstall(ctx, "bin"),
1775 ctx.ModuleName(), j.wrapperFile, jarFile)
Nan Zhang3c807db2017-11-03 14:53:31 -07001776 }
Colin Cross2fe66872015-03-30 17:20:39 -07001777}
1778
Colin Crossf506d872017-07-19 15:53:04 -07001779func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross6b4a32d2017-12-05 13:42:45 -08001780 if ctx.Arch().ArchType == android.Common {
1781 j.deps(ctx)
1782 }
Colin Cross46c9b8b2017-06-22 16:51:17 -07001783}
1784
Colin Cross1b16b0e2019-02-12 14:41:32 -08001785// java_binary builds a `.jar` file and a shell script that executes it for the device, and possibly for the host
1786// as well.
1787//
1788// By default, a java_binary has a single variant that produces a `.jar` file containing `classes.dex` files that were
1789// compiled against the device bootclasspath.
1790//
1791// Specifying `host_supported: true` will produce two variants, one compiled against the device bootclasspath and one
1792// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001793func BinaryFactory() android.Module {
1794 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001795
Colin Cross36242852017-06-23 15:06:31 -07001796 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001797 &module.Module.properties,
1798 &module.Module.deviceProperties,
Colin Cross43f08db2018-11-12 10:13:39 -08001799 &module.Module.dexpreoptProperties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001800 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001801 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001802
Colin Cross9ae1b922018-06-26 17:59:05 -07001803 module.Module.properties.Installable = proptools.BoolPtr(true)
1804
Colin Cross6b4a32d2017-12-05 13:42:45 -08001805 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommonFirst)
1806 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001807 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001808}
1809
Colin Cross1b16b0e2019-02-12 14:41:32 -08001810// java_binary_host builds a `.jar` file and a shell script that executes it for the host.
1811//
1812// A java_binary_host has a single variant that produces a `.jar` file containing `.class` files that were
1813// compiled against the host bootclasspath.
Colin Crossf506d872017-07-19 15:53:04 -07001814func BinaryHostFactory() android.Module {
1815 module := &Binary{}
Colin Cross2fe66872015-03-30 17:20:39 -07001816
Colin Cross36242852017-06-23 15:06:31 -07001817 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -07001818 &module.Module.properties,
Colin Cross6af17aa2017-09-20 12:59:05 -07001819 &module.Module.protoProperties,
Colin Cross540eff82017-06-22 17:01:52 -07001820 &module.binaryProperties)
Colin Cross36242852017-06-23 15:06:31 -07001821
Colin Cross9ae1b922018-06-26 17:59:05 -07001822 module.Module.properties.Installable = proptools.BoolPtr(true)
1823
Colin Cross6b4a32d2017-12-05 13:42:45 -08001824 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibCommonFirst)
1825 android.InitDefaultableModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001826 return module
Colin Cross2fe66872015-03-30 17:20:39 -07001827}
1828
1829//
1830// Java prebuilts
1831//
1832
Colin Cross74d73e22017-08-02 11:05:49 -07001833type ImportProperties struct {
Colin Cross27b922f2019-03-04 22:35:41 -08001834 Jars []string `android:"path"`
Colin Cross461bd1a2017-10-20 13:59:18 -07001835
Nan Zhangea568a42017-11-08 21:20:04 -08001836 Sdk_version *string
Colin Cross535e2cf2017-10-20 17:57:49 -07001837
1838 Installable *bool
Jiyong Park1be96912018-05-28 18:02:19 +09001839
1840 // List of shared java libs that this module has dependencies to
1841 Libs []string
Colin Cross37f6d792018-07-12 12:28:41 -07001842
1843 // List of files to remove from the jar file(s)
1844 Exclude_files []string
1845
1846 // List of directories to remove from the jar file(s)
1847 Exclude_dirs []string
Nan Zhang4c819fb2018-08-27 18:31:46 -07001848
1849 // if set to true, run Jetifier against .jar file. Defaults to false.
Colin Cross1001a792019-03-21 22:21:39 -07001850 Jetifier *bool
Colin Cross74d73e22017-08-02 11:05:49 -07001851}
1852
1853type Import struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001854 android.ModuleBase
Colin Cross48de9a42018-10-02 13:53:33 -07001855 android.DefaultableModuleBase
Colin Crossec7a0422017-07-07 14:47:12 -07001856 prebuilt android.Prebuilt
Colin Cross2fe66872015-03-30 17:20:39 -07001857
Colin Cross74d73e22017-08-02 11:05:49 -07001858 properties ImportProperties
1859
Colin Cross0a6e0072017-08-30 14:24:55 -07001860 combinedClasspathFile android.Path
Jiyong Park1be96912018-05-28 18:02:19 +09001861 exportedSdkLibs []string
Colin Cross2fe66872015-03-30 17:20:39 -07001862}
1863
Colin Cross83bb3162018-06-25 15:48:06 -07001864func (j *Import) sdkVersion() string {
1865 return String(j.properties.Sdk_version)
1866}
1867
1868func (j *Import) minSdkVersion() string {
1869 return j.sdkVersion()
1870}
1871
Colin Cross74d73e22017-08-02 11:05:49 -07001872func (j *Import) Prebuilt() *android.Prebuilt {
Colin Crossec7a0422017-07-07 14:47:12 -07001873 return &j.prebuilt
1874}
1875
Colin Cross74d73e22017-08-02 11:05:49 -07001876func (j *Import) PrebuiltSrcs() []string {
1877 return j.properties.Jars
1878}
1879
1880func (j *Import) Name() string {
Colin Cross5ea9bcc2017-07-27 15:41:32 -07001881 return j.prebuilt.Name(j.ModuleBase.Name())
1882}
1883
Colin Cross74d73e22017-08-02 11:05:49 -07001884func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
Colin Cross42d48b72018-08-29 14:10:52 -07001885 ctx.AddVariationDependencies(nil, libTag, j.properties.Libs...)
Colin Cross1e676be2016-10-12 14:38:15 -07001886}
1887
Colin Cross74d73e22017-08-02 11:05:49 -07001888func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -08001889 jars := android.PathsForModuleSrc(ctx, j.properties.Jars)
Colin Crosse1d62a82015-04-03 16:53:05 -07001890
Nan Zhang4c819fb2018-08-27 18:31:46 -07001891 jarName := ctx.ModuleName() + ".jar"
1892 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
Colin Cross37f6d792018-07-12 12:28:41 -07001893 TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
1894 false, j.properties.Exclude_files, j.properties.Exclude_dirs)
Colin Cross1001a792019-03-21 22:21:39 -07001895 if Bool(j.properties.Jetifier) {
Nan Zhang4c819fb2018-08-27 18:31:46 -07001896 inputFile := outputFile
1897 outputFile = android.PathForModuleOut(ctx, "jetifier", jarName)
1898 TransformJetifier(ctx, outputFile, inputFile)
1899 }
Colin Crosse9a275b2017-10-16 17:09:48 -07001900 j.combinedClasspathFile = outputFile
Jiyong Park1be96912018-05-28 18:02:19 +09001901
1902 ctx.VisitDirectDeps(func(module android.Module) {
1903 otherName := ctx.OtherModuleName(module)
1904 tag := ctx.OtherModuleDependencyTag(module)
1905
1906 switch dep := module.(type) {
1907 case Dependency:
1908 switch tag {
1909 case libTag, staticLibTag:
1910 // sdk lib names from dependencies are re-exported
1911 j.exportedSdkLibs = append(j.exportedSdkLibs, dep.ExportedSdkLibs()...)
1912 }
1913 case SdkLibraryDependency:
1914 switch tag {
1915 case libTag:
1916 // names of sdk libs that are directly depended are exported
1917 j.exportedSdkLibs = append(j.exportedSdkLibs, otherName)
1918 }
1919 }
1920 })
1921
1922 j.exportedSdkLibs = android.FirstUniqueStrings(j.exportedSdkLibs)
Nan Zhang4973ecf2018-08-10 13:42:12 -07001923 if Bool(j.properties.Installable) {
1924 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
1925 ctx.ModuleName()+".jar", outputFile)
1926 }
Colin Cross2fe66872015-03-30 17:20:39 -07001927}
1928
Colin Cross74d73e22017-08-02 11:05:49 -07001929var _ Dependency = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001930
Nan Zhanged19fc32017-10-19 13:06:22 -07001931func (j *Import) HeaderJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001932 if j.combinedClasspathFile == nil {
1933 return nil
1934 }
Colin Cross37f6d792018-07-12 12:28:41 -07001935 return android.Paths{j.combinedClasspathFile}
Nan Zhanged19fc32017-10-19 13:06:22 -07001936}
1937
1938func (j *Import) ImplementationJars() android.Paths {
shinwang9e4c07a2018-12-24 15:41:04 +08001939 if j.combinedClasspathFile == nil {
1940 return nil
1941 }
Colin Cross37f6d792018-07-12 12:28:41 -07001942 return android.Paths{j.combinedClasspathFile}
Colin Cross2fe66872015-03-30 17:20:39 -07001943}
1944
Colin Cross331a1212018-08-15 20:40:52 -07001945func (j *Import) ResourceJars() android.Paths {
1946 return nil
1947}
1948
1949func (j *Import) ImplementationAndResourcesJars() android.Paths {
albaltai36ff7dc2018-12-25 14:35:23 +08001950 if j.combinedClasspathFile == nil {
1951 return nil
1952 }
Colin Cross331a1212018-08-15 20:40:52 -07001953 return android.Paths{j.combinedClasspathFile}
1954}
1955
Colin Crossf24a22a2019-01-31 14:12:44 -08001956func (j *Import) DexJar() android.Path {
1957 return nil
1958}
1959
Colin Cross74d73e22017-08-02 11:05:49 -07001960func (j *Import) AidlIncludeDirs() android.Paths {
Colin Crossc0b06f12015-04-08 13:03:43 -07001961 return nil
1962}
1963
Jiyong Park1be96912018-05-28 18:02:19 +09001964func (j *Import) ExportedSdkLibs() []string {
1965 return j.exportedSdkLibs
1966}
1967
Colin Cross0c4ce212019-05-03 15:28:19 -07001968func (j *Import) SrcJarArgs() ([]string, android.Paths) {
1969 return nil, nil
1970}
1971
albaltai36ff7dc2018-12-25 14:35:23 +08001972// Add compile time check for interface implementation
1973var _ android.IDEInfo = (*Import)(nil)
1974var _ android.IDECustomizedModuleName = (*Import)(nil)
1975
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001976// Collect information for opening IDE project files in java/jdeps.go.
1977const (
1978 removedPrefix = "prebuilt_"
1979)
1980
1981func (j *Import) IDEInfo(dpInfo *android.IdeInfo) {
1982 dpInfo.Jars = append(dpInfo.Jars, j.PrebuiltSrcs()...)
1983}
1984
1985func (j *Import) IDECustomizedModuleName() string {
1986 // TODO(b/113562217): Extract the base module name from the Import name, often the Import name
1987 // has a prefix "prebuilt_". Remove the prefix explicitly if needed until we find a better
1988 // solution to get the Import name.
1989 name := j.Name()
1990 if strings.HasPrefix(name, removedPrefix) {
patricktubb640e02018-10-11 18:33:16 +08001991 name = strings.TrimPrefix(name, removedPrefix)
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001992 }
1993 return name
1994}
1995
Colin Cross74d73e22017-08-02 11:05:49 -07001996var _ android.PrebuiltInterface = (*Import)(nil)
Colin Cross2fe66872015-03-30 17:20:39 -07001997
Colin Cross1b16b0e2019-02-12 14:41:32 -08001998// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library module.
1999//
2000// By default, a java_import has a single variant that expects a `.jar` file containing `.class` files that were
2001// compiled against an Android classpath.
2002//
2003// Specifying `host_supported: true` will produce two variants, one for use as a dependency of device modules and one
2004// for host modules.
Colin Cross74d73e22017-08-02 11:05:49 -07002005func ImportFactory() android.Module {
2006 module := &Import{}
Colin Cross36242852017-06-23 15:06:31 -07002007
Colin Cross74d73e22017-08-02 11:05:49 -07002008 module.AddProperties(&module.properties)
2009
2010 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002011 InitJavaModule(module, android.HostAndDeviceSupported)
Colin Cross36242852017-06-23 15:06:31 -07002012 return module
Colin Cross2fe66872015-03-30 17:20:39 -07002013}
2014
Colin Cross1b16b0e2019-02-12 14:41:32 -08002015// java_import imports one or more `.jar` files into the build graph as if they were built by a java_library_host
2016// module.
2017//
2018// A java_import_host has a single variant that expects a `.jar` file containing `.class` files that were
2019// compiled against a host bootclasspath.
Colin Cross74d73e22017-08-02 11:05:49 -07002020func ImportFactoryHost() android.Module {
2021 module := &Import{}
2022
2023 module.AddProperties(&module.properties)
2024
2025 android.InitPrebuiltModule(module, &module.properties.Jars)
Colin Cross48de9a42018-10-02 13:53:33 -07002026 InitJavaModule(module, android.HostSupported)
Colin Cross74d73e22017-08-02 11:05:49 -07002027 return module
2028}
2029
Colin Cross42be7612019-02-21 18:12:14 -08002030// dex_import module
2031
2032type DexImportProperties struct {
2033 Jars []string
2034}
2035
2036type DexImport struct {
2037 android.ModuleBase
2038 android.DefaultableModuleBase
2039 prebuilt android.Prebuilt
2040
2041 properties DexImportProperties
2042
2043 dexJarFile android.Path
2044 maybeStrippedDexJarFile android.Path
2045
2046 dexpreopter
2047}
2048
2049func (j *DexImport) Prebuilt() *android.Prebuilt {
2050 return &j.prebuilt
2051}
2052
2053func (j *DexImport) PrebuiltSrcs() []string {
2054 return j.properties.Jars
2055}
2056
2057func (j *DexImport) Name() string {
2058 return j.prebuilt.Name(j.ModuleBase.Name())
2059}
2060
2061func (j *DexImport) DepsMutator(ctx android.BottomUpMutatorContext) {
2062 android.ExtractSourcesDeps(ctx, j.properties.Jars)
2063}
2064
2065func (j *DexImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
2066 if len(j.properties.Jars) != 1 {
2067 ctx.PropertyErrorf("jars", "exactly one jar must be provided")
2068 }
2069
2070 j.dexpreopter.installPath = android.PathForModuleInstall(ctx, "framework", ctx.ModuleName()+".jar")
2071 j.dexpreopter.isInstallable = true
2072 j.dexpreopter.uncompressedDex = shouldUncompressDex(ctx, &j.dexpreopter)
2073
2074 inputJar := ctx.ExpandSource(j.properties.Jars[0], "jars")
2075 dexOutputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar")
2076
2077 if j.dexpreopter.uncompressedDex {
2078 rule := android.NewRuleBuilder()
2079
2080 temporary := android.PathForModuleOut(ctx, ctx.ModuleName()+".jar.unaligned")
2081 rule.Temporary(temporary)
2082
2083 // use zip2zip to uncompress classes*.dex files
2084 rule.Command().
2085 Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
2086 FlagWithInput("-i ", inputJar).
2087 FlagWithOutput("-o ", temporary).
2088 FlagWithArg("-0 ", "'classes*.dex'")
2089
2090 // use zipalign to align uncompressed classes*.dex files
2091 rule.Command().
2092 Tool(ctx.Config().HostToolPath(ctx, "zipalign")).
2093 Flag("-f").
2094 Text("4").
2095 Input(temporary).
2096 Output(dexOutputFile)
2097
2098 rule.DeleteTemporaryFiles()
2099
2100 rule.Build(pctx, ctx, "uncompress_dex", "uncompress dex")
2101 } else {
2102 ctx.Build(pctx, android.BuildParams{
2103 Rule: android.Cp,
2104 Input: inputJar,
2105 Output: dexOutputFile,
2106 })
2107 }
2108
2109 j.dexJarFile = dexOutputFile
2110
2111 dexOutputFile = j.dexpreopt(ctx, dexOutputFile)
2112
2113 j.maybeStrippedDexJarFile = dexOutputFile
2114
2115 ctx.InstallFile(android.PathForModuleInstall(ctx, "framework"),
2116 ctx.ModuleName()+".jar", dexOutputFile)
2117}
2118
2119func (j *DexImport) DexJar() android.Path {
2120 return j.dexJarFile
2121}
2122
2123// dex_import imports a `.jar` file containing classes.dex files.
2124//
2125// A dex_import module cannot be used as a dependency of a java_* or android_* module, it can only be installed
2126// to the device.
2127func DexImportFactory() android.Module {
2128 module := &DexImport{}
2129
2130 module.AddProperties(&module.properties)
2131
2132 android.InitPrebuiltModule(module, &module.properties.Jars)
2133 InitJavaModule(module, android.DeviceSupported)
2134 return module
2135}
2136
Colin Cross89536d42017-07-07 14:35:50 -07002137//
2138// Defaults
2139//
2140type Defaults struct {
2141 android.ModuleBase
2142 android.DefaultsModuleBase
2143}
2144
Colin Cross1b16b0e2019-02-12 14:41:32 -08002145// java_defaults provides a set of properties that can be inherited by other java or android modules.
2146//
2147// A module can use the properties from a java_defaults module using `defaults: ["defaults_module_name"]`. Each
2148// property in the defaults module that exists in the depending module will be prepended to the depending module's
2149// value for that property.
2150//
2151// Example:
2152//
2153// java_defaults {
2154// name: "example_defaults",
2155// srcs: ["common/**/*.java"],
2156// javacflags: ["-Xlint:all"],
2157// aaptflags: ["--auto-add-overlay"],
2158// }
2159//
2160// java_library {
2161// name: "example",
2162// defaults: ["example_defaults"],
2163// srcs: ["example/**/*.java"],
2164// }
2165//
2166// is functionally identical to:
2167//
2168// java_library {
2169// name: "example",
2170// srcs: [
2171// "common/**/*.java",
2172// "example/**/*.java",
2173// ],
2174// javacflags: ["-Xlint:all"],
2175// }
Colin Cross89536d42017-07-07 14:35:50 -07002176func defaultsFactory() android.Module {
2177 return DefaultsFactory()
2178}
2179
2180func DefaultsFactory(props ...interface{}) android.Module {
2181 module := &Defaults{}
2182
2183 module.AddProperties(props...)
2184 module.AddProperties(
2185 &CompilerProperties{},
2186 &CompilerDeviceProperties{},
Colin Cross43f08db2018-11-12 10:13:39 -08002187 &DexpreoptProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08002188 &android.ProtoProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002189 &aaptProperties{},
2190 &androidLibraryProperties{},
2191 &appProperties{},
2192 &appTestProperties{},
Jaewoong Jung525443a2019-02-28 15:35:54 -08002193 &overridableAppProperties{},
Colin Cross48de9a42018-10-02 13:53:33 -07002194 &ImportProperties{},
2195 &AARImportProperties{},
2196 &sdkLibraryProperties{},
Colin Cross42be7612019-02-21 18:12:14 -08002197 &DexImportProperties{},
Colin Cross89536d42017-07-07 14:35:50 -07002198 )
2199
2200 android.InitDefaultsModule(module)
2201
2202 return module
2203}
Nan Zhangea568a42017-11-08 21:20:04 -08002204
2205var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07002206var BoolDefault = proptools.BoolDefault
Nan Zhangea568a42017-11-08 21:20:04 -08002207var String = proptools.String
Colin Cross0d0ba592018-02-20 13:33:42 -08002208var inList = android.InList